Scott Hanselman

Generating complex math visualizations in SVG using C# and ILNumerics

October 17, '13 Comments [15] Posted in Open Source
Sponsored By

I was recently turned on to the ILNumerics library. It's a high performance math library for .NET developers that my math skills can barely comprehend. It has a clean and elegant syntax, but more importantly, it's visualization graphics engine is thoughtful, flexible, and well-factored.

Having worked on a lot of websites, including ones that do a lot of backend image generation, resizing and analysis (like check imaging almost 10 years ago) I was impressed at how easily I was able to get an equation onto a basic website with ILNumerics and SVG.

Of course, it's not just a web library, in fact, most of the samples are WPF and WinForms, so it's an engine that you can use anywhere. Regardless, as a web person, I wanted to see how quickly I could get something into my browser.

The ILNumerics website has a cool sample 3D graph on their home page that was generated with this code:

var scene = new ILScene {
new ILPlotCube(twoDMode: false) {
new ILSurface(ILSpecialData.sincf(40, 60, 2.5f)) {
}
}
};
scene.First<ILPlotCube>().Rotation = Matrix4.Rotation(
new Vector3(1f,0.23f,1), 0.7f);
scene;

However, you'll notice in their sample they just end with the variable "scene." That's a no-op there, but it's their coder way of saying "at this point, the scene variable holds a representation of our plot. Do with it as you will."

NOTE: Do check out their home page...the little sample there is deeper than you'd think. The dropdown shows they can generate PNGs, JPGs, JPG HD, SVG, but also "EXE." Hm, download a random EXE from the internet? Yes please! ;) Take a risk and you'll get a nice self-contained EXE visualizer that not only renders the graph but lets you rotate it. You can download the ILView 3D viewer and play around, it's harmless - all the code for ILView is on GitHub! The best part is that it has a built in REPL so you can type your C# right there and see the results! It even runs on Linux and uses Mono. ;)

ILNumeric

Back to my goal. I want to use the library on a basic website and dynamically generate an SVG of this plot.

Here's the same plot, put inside an ASP.NET HttpHandler (which could also be made routable and used in ASP.NET MVC/Web Forms, etc.)

public void ProcessRequest(HttpContext context)
{
var scene = new ILScene {
new ILPlotCube(twoDMode: false) {
new ILSurface(ILSpecialData.sincf(40, 60, 2.5f)) {
}
}
};
scene.First<ILPlotCube>().Rotation = Matrix4.Rotation(
new Vector3(1f, 0.23f, 1), 0.7f);

var driver = new ILSVGDriver(context.Response.OutputStream, 1200, 800, scene, Color.White);
driver.Render();
}

Here I'm passing context.Response.OutputStream to their ILSVGDriver and saving the result not to a file, but directly out to the browser. I could certainly save it to cloud blob storage or a local file system for caching, reuse or email.

using (FileStream fs = new FileStream(@"test.svg", FileMode.Create)) {
new ILSVGDriver(fs, scene: whateveryoursceneis).Render();
}

While a SVG is preferable, one could also make a PNG.

var driver = new ILGDIDriver(1280, 800, whateveryoursceneis); 
driver.Render();
driver.BackBuffer.Bitmap.Save("whatever", System.Drawing.Imaging.ImageFormat.Png);

Their docs are excellent and many include a similar interactive viewer within the website itself.

It's so much more than a plot visualizer, though. It reminds me a little of D3.js, except more math focused and less live-data binding. It's almost as flexible though, with many kinds of visualizations beyond what you'd expect.

3D graph that looks like a mountainDonut graph

Three 3D graphs in one plotTopographical plot

Infinite TrianglesHalf a sphere intersected by a blue gear

Here's the code to show a green sphere that's composed of triangles, but has the top chopped off, as an example. This is just 10 lines of code, and could be made less.

var scene = new ILScene(); 
// create a new sphere
var sphere = new ILSphere();
// the sphere is a group containing the Fill (ILTriangles)
// and the Wireframe (ILLines) of the sphere. Both shapes
// share the same vertex positions buffer. Hence, we only
// need to alter one of them:
using (ILScope.Enter()) {
// take the vertex positions from the Fill.Positions buffer
ILArray<float> pos = sphere.Fill.Positions.Storage;
// set all vertices with a Y coordinate larger than 0.3 to 0.3
pos[1, pos[1, ":"] > 0.3f] = 0.3f;
// write all values back to the buffer
sphere.Fill.Positions.Update(pos);
}
// add the "sphere" to the scene
scene.Camera.Add(sphere);
// add another light (for niceness only)
scene.Add(new ILPointLight() {
Position = new Vector3(-0, 1, -2)
});
// move the camera upwards
scene.Camera.Position = new Vector3(0,3,-10);
// display the scene
scene;

And this gives you:

Half a green sphere

It's a really amazing project. ILNumerics is GPL3 and also uses OpenTK for OpenGL bindings, and Mono.CSharp for C# compiling and evaluation. ILView is under the MIT/X11 license.

You can get it via NuGet with just "Install-Package ILNumerics." Check it out and tell your friends, scientists, and friends of Edward Tufte.


Sponsor: Thanks to Red Gate for sponsoring the feed this week! Check out a simpler way to deploy with Red Gate’s Deployment Manager. It can deploy your .NET apps, services, and databases in a single, repeatable process. Get your free Starter edition now.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

CSI: Re-enabling Remote Desktop with PowerShell after you've blocked it with your own firewall rule

October 14, '13 Comments [8] Posted in Azure | PowerShell
Sponsored By

Got a great email from reader Seán McDonnell.

The Big Problem:

I set up an Azure virtual machine running Windows Server 2012.

I accidentally disabled the Remote Desktop Windows firewall rule (while I was remotely connected). The connection dropped as you would expect.

I have been pulling my hair out ever since trying to re-enable this rule.

Doh. Ouch. I didn't ask how this happened, but you know, one gets to clicking and typing and you can feel the mistake about to happen as your hand drops towards the keyboard, but by then it's too late. Gravity has screwed you.

I suggested that Seán use Remote Powershell to get in and add the enabling Firewall Rule for RDC. Remote PowerShell is like "SSH" in *nix. You get a remote terminal and can pretty much do whatever you want from there.

TL;DR version of Seán's experience.

  • Make sure PowerShell is enabled in the Endpoints section of the Azure portal.
  • Get the server's certificate (PowerShell needs this for remote commands). You can get the server certificate by going to your domains' URL: https://yourdomain.cloudapp.net:12345 (where :12345 is the port that PowerShell uses).
  • Export the SSL certificate of the site as a .CER file and install it on your local machine.
  • Save it to the "Trusted Root Certification Authorities" store on your machine.
  • Open PowerShell with administrative privileges on your local machine and type:
    Enter-PSSession -ComputerName yourdomain.cloudapp.net -Port 5986 -Credential YourUserName -UseSSL
  • A login popup will appear, enter your VM's login credentials here.
  • You will now be able to execute commands against the Azure VM. In Seán's case, he ran
    netsh advfirewall firewall set rule group="remote desktop" new enable=Yes
    and exited the PowerShell session and was able to remotely connect to my machine.

Long Detailed Version with Screenshots

Long version with screenshots:

Make sure PowerShell is publically accessible in the 'endpoints' section of the Azure portal.

 01 - VM Endpoints

Get the server's certificate (PowerShell needs this for establishing a remote session). You can get the server certificate by going to your domains' URL: https://yourdomain.cloudapp.net:5986 (where :5986 is the port that PowerShell uses).

 image

Go to the Details tab and click Copy to File...

 03 - Certificate Export

Leave the first option selected and save the file to a local drive. 

 04 - Certificate Export

05 - Certificate Export

Once the file is generated and saved locally, install the certificate by double clicking on the certificate-name.cer file.

 06 - Certificate Install

Install the certificate in the following store:

cert install

Open up PowerShell with administrative privileges and execute the following command (replacing the domain name and username with your own one):

 08 - Remote PowerShell Session

A logon credential popup should appear where you will need to enter your VM's username and password:

07 - Remote PowerShell Session

If successful, it should be pretty obvious that you have successfully initiated a remote session with the VM.

Enter-PSSession -ComputerName yourdomain.cloudapp.net -Port 5986 -Credential YourUserName -UseSSL

09 - Remote PowerShell Session Verification

To open re-enable the firewall rule you issue the command:

netsh advfirewall firewall set rule group="remote desktop" new enable=Yes  

 10 - Remote PowerShell Session Firewall Rule Update

The final step was to quit the PowerShell session and RDC to the VM. Success! 

I hope this write-up helps other people as well. Thanks Seán for a great question and for sharing the screenshot of your experience!


Sponsor: Thanks to Red Gate for sponsoring the feed this week! Check out a simpler way to deploy with Red Gate’s Deployment Manager. It can deploy your .NET apps, services, and databases in a single, repeatable process. Get your free Starter edition now.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Hanselman's Newsletter of Wonderful Things: August 26th, 2013

October 14, '13 Comments [2] Posted in Newsletter
Sponsored By

I have a "whenever I get around to doing it" Newsletter of Wonderful Things. Why a newsletter? I dunno. It seems more personal somehow. You can view all the previous newsletters here.

Here's the newsletter that I sent out August 26th. You can sign up here to the Newsletter of Wonderful Things or just wait and get them weeks later on the blog, which hopefully you have subscribed to. Subscribers get the goodness first!


Hi Interfriends,

Thanks again for signing up for this experiment. Here's some interesting things I've come upon this week. If you forwarded this (or if it was forwarded to you) a reminder: You can sign up at http://hanselman.com/newsletter and the archive of all previous Newsletters is here.

Remember, you get the newsletter here first. This one will be posted to the blog as an archive in a few weeks.

Scott Hanselman

(BTW, since you *love* email you can subscribe to my blog via email here: http://feeds.hanselman.com/ScottHanselman DO IT!)

P.P.S. You know you can forward this to your friends, right?


Sponsor: Big thanks to Telerik Icenium for sponsoring the blog's feed last week! Telerik Icenium now includes Visual Studio integration. Code once using HTML5 & JavaScript - build, test, and publish native-running iOS & Android apps. Start your 30 day trial with support!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Review: Nokia Purity Pro Wireless Headphones by Monster

October 9, '13 Comments [34] Posted in Reviews
Sponsored By

imageI like gadgets, but my tolerance for price is low. But a friend of mine showed me his Nokia Purity Pro headphones (made by Monster) and I tried them...and bought a pair.

I've been using the same Beats by Dre Tour In-Ear Headphones for over a year. I've taken them all over the world and they've worked great. However, on planes they hurt the insides of my ear, and once I got them stuck in my ear real good so I started re-thinking my aversion to over-the-ear headphones.

I used to have a pair of the original Bose noise-cancelling from years ago. Noise-cancelling technology has apparently improved a lot since then. When my friend showed me his Purity Pro's we were standing in a parking lot after lunch. I put them on and the street noise just stopped. Any low-level or repeated noise just disappeared. They work at least as well as my Bose did and are a joy to use on a plane.

The model I bought was the "Nokia 02734L7 BH-940 Purity Pro Wireless Stereo Headset by Monster" which is a mouthful, and I paid $300 for them, which was about $150 more than I wanted to pay. Sadly, I'm looking at Amazon today and seeing these headphones as low as $235. There's also wired versions for $79, although they lack the noise-cancelling and Bluetooth (of course) features.

I consider myself a power user and I don't put up with a lot of trouble from my devices. If it sucks or doesn't work in some way, it's going back.

The "oh gee" feature of the Purity Pro's is that it has NFC (Near Field Communication) to aid the initial paring process. The resulting pair is still Bluetooth. If your phone has NFC, more power to you, but after it's paired you'll never use this again.

The Bluetooth feature that I really appreciate is the ability to pair with multiple devices - up to 8. You can also be simultaneously connected to two devices which is REALLY convenient.

Note: Read the manual (PDF) and turn on "Bluetooth Multipoint" to use this feature.

For example, right now as I type this I'm connected to my laptop's audio (for Skype and Lync) and to my phone for music (Spotify and phone calls). I could certainly go along with connecting to just one, but when you're sitting in a café working, it's really nice to have "all electronic audio" from your devices routed into your headphones. I can listen to music, take Skype calls AND answer phone calls using the headphones.

I also have them paired with my tablet (so phone, laptop, tablet) and I must say, having no wires to worry about really IS a nice touch. I feel like just NOW, today, in 2013 the promise of Bluetooth is happening.

image

It's also a microphone (the mic is near the left ear) so I can take calls OR use it as a mic for Skype/Lync.

image

Of course, if I have a better mic or a laptop with an Array Mic I can tell the computer to NOT use the headphone's mic and use another. (You'd be surprised how often folks forget that they have full control over what mics are used for regular apps or communications apps.)

Purity Pro Headphones foldedThe headphones turn off when you close them up and reconnect when you open them. They have an internal battery and recharge using a mini-USB connector. They say they last for almost 24 hours of use, and if the battery runs out you can just switch to a standard 1/8" headphone plug. I have used them for a few months and have never needed to move to wired but it's nice to know they will keep working even with a dead battery. That said, they top off fast using mini-USB cables I have in my bag already. I did a 6 hour flight from New York watching movies last week and didn't give headphone battery life a thought.

One other cool feature is that the headphones talk to you. When you're pairing with Bluetooth or the battery is doing something it talks in your ear and says stuff like "Connected."

There's switches behind each ear for answering calls, changing volume, and going previous and next track. There's also some more subtle gestures, so RTFM. For example, double-press the Call button to decline a call, or hold the Call button to switch to phone from the headphones. Press and hold the call button when not in a call to use Siri or Voice Dial. You can even pair with more than one phone and use the < button to manage calls from both phones while on one call.

I got the black ones because I'm fly, but not that fly. There is also white, yellow, and red. I've seen those as cheap as $230 on Amazon, so search around.

They were somewhat expensive but I'm using them every day and I'm happy with the purchase. They are comfortable, light, fold up into my backpack easily and don't beg to be charged all day. Recommended if you're looking for a great pair of tech-savvy wireless Bluetooth headphones with noise-cancellation.


Sponsor: Big thanks to Telerik Icenium for sponsoring the blog's feed this week! Telerik Icenium now includes Visual Studio integration. Code once using HTML5 & JavaScript - build, test, and publish native-running iOS & Android apps. Start your 30 day trial with support!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Iterating quickly at a Hackathon with Azure Mobile Services and Dynamic Schema

October 8, '13 Comments [11] Posted in Azure
Sponsored By
Photo from Digital Undivided's Facebook

This last weekend I was in New York presenting at FOCUS100. I also was a mentor for the 24 hour mobile hackathon along with Tara Walker. The attendees were trying to create a mobile app (responsive site, or a native app, it didn't matter) in a single day (we were up all night.) A few of the teams didn't have any back-end experience, but had deep HTML and JavaScript experience. Rather than having them hardcode data a few ended up using Azure Mobile Services (which isn't the best name, but it's something.)

I call it a backend in a box. Rather than creating your own RESTful service with CRUD (Create, Read, Update, Delete) to return JSON stored in a database, you show Mobile Services what your data should be shaped like and lets you query it dynamically. So it's an abstraction over the boring stuff. You can add custom APIs and fully edit all operations later, but it gets you 80% of what you want for 20% of the time. Later you spend 80% of your time doing all your custom stuff; it's a great accelerator. It also abstracts away social identity logins (Facebook, Twitter, Google, Microsoft ID) as well as mobile push notifications.

Even though it's called Azure Mobile Services, it could also be called "Backend in a Box" or "Azure Backend Accelerator" but somehow those aren't really good names either. ;)

Here's how we iterated very quickly with one project. There's some tricks that we discovered to make things easier and more obvious. I'm pushing all the things we/I learned back into the Mobile Services team as they iterate quickly and are always improving the service.

Initial Iterations with Azure Mobile Services

First, let's assume you've signed for a free trial at Azure.com. You get $200 in credits (which is enough to run two VMs full time or dozens of standard sites on a single VM for a month, as well as 10 free websites, and 10 free mobile services).

You might also install the Azure CLI (Command Line Interface) via NPM as in "npm install azure-cli --g" then run "azure account download" then "azure account import" to associate your subscription's certs with the command line.

NOTE: If you are a command line person you can do most everything with the CLI. You can "azure mobile create 'servicename'" or "azure mobile table create" as well as "azure mobile data read" and on and on. We used the HTML5 Portal at the hackathon because it was easier for newbies to visualize.

From the Portal, make a Mobile Service:

You have no mobile services

Make a mobile service:

Making a mobile service

As an FYI, here's doing the same thing at the command line:

c:\>azure mobile create
info: Executing command mobile create
Mobile service name: hanselmanmobile
SQL administrator user name: focus100
SQL administrator password: ********
Confirm password: ********
+ Creating mobile service
info: Overall application state: Healthy
info: Mobile service (hanselmanmobile) state: ProvisionConfigured
info: SQL database (hanselmanmobile_db) state: Provisioned
info: SQL server (aq8l9z9gcb) state: ProvisionConfigured
info: mobile create command OK

Then I can create a Table in my new database. There's several levels of permissions can set, like auth'ed users, or accessible only with the app key. To speed up initial development, I'm setting it to Everyone. Then I'll ratchet up security as the schema starts to gel.

Creating a new table

Here's the same thing at the command line:

c:\>azure mobile table create hanselmanmobile widgets
info: Executing command mobile table create
+ Creating table
info: mobile table create command OK

I could set the permissions at create time with the command line as well, or update permissions after the fact:

c:\>azure mobile table update hanselmanmobile widgets -p read=public,insert=public,delete=public,update=public
info: Executing command mobile table update
+ Updating permissions
info: Updated permissions
info: mobile table update command OK

My endpoint is now http://hanselmanmobile.azure-mobile.net/tables/widgets. (I could have lots of tables, of course.)

How do I get data in there and how do I tell the system what the data looks like? Since JSON is the format Mobile Services uses on the wire, you POST JSON at the service endpoint and it'll store that document.

Posting Data to Mobile Services in Multiple Ways

Some examples for Mobile Services have you/me/developers write an HTML page with JavaScript and run that to POST initial data. I, and the folks at the hackathon, preferred to get the data shape close while the UI folks did their mockups in parallel. Most attendees were familiar with cURL and wanted to use that.

Here's three ways to POST data to the new service.

Note that:

  • The data must be valid JSON.
  • You must include a Content-Type: application/json header or you'll get an HTTP 400 that says "invalid JSON," even though it may be the missing header.
  • A successful record creation will return an HTTP 201 CREATED.

Posting JSON to a Mobile Service via Command line with cURL

I like cURL for GETs, but for POSTing JSON it's complex and a hassle to encode the quotes and double-quotes in JSON correctly. Instead, I POST from a file. Put your JSON exactly as you want it in a text file, then:

c:\>curl -v -X POST -H "Content-Type: application/json" -d @topost.txt http://hanselmanmobile.azure-mobile.net/tables/widgets 

Doing the HTTP POST from a file removes a whole pile of issues. Also, use -v for verbose for ease of debugging.

Posting JSON to a Mobile Service with Fiddler

From my PC using Fiddler I can compose new JSON to POST like this:

POSTING with Fiddler

And note the HTTP 201 CREATED result:

HTTP 201 CREATED

Posting JSON to a Mobile Service with Postman

If you're using Google Chrome, you can get the Postman App (it's a REST debugger/Tivo for Chrome) or the REST Console and also POST and GET to your heart's content. Again, make sure to get the Content-Type set.

POSTing to an Azure Mobile Service with POSTMAN

Once the data is in the table, I can access it with a simple HTTP GET (in a browser or whatever) as well as via the Azure Command Line Tool or via cURL:

c:\>azure mobile data read hanselmanmobile widgets
info: Executing command mobile data read
data: id name price date_available
data: -- -------------- ----- -------------------------
data: 1 Ethernet Cable 5.99 2013-04-12T20:44:55+00:00
info: mobile data read command OK

c:\>curl --header accept:application/json http://hanselmanmobile.azure-mobile.net/tables/widgets
[{"id":1,"name":"Ethernet Cable","price":"5.99","date_available":"2013-04-12T20:44:55.000Z"}]

Of course, I can also see the data in the Portal:

Data in the Mobile Service

If I don't like the shape of this table, I can drop the table and recreate. I can also call "azure mobile data truncate" from the command line. I can delete an individual column and re-POST, or, if I'm just adding a column, I can post differently shaped data. This is the "dynamic schema" setting. When I get everything the way I want and I'm done iterating, I turn dynamic schema off and freeze the schema.

Azure Mobile Services Dynamic Schema

Mobile Services works great for getting a back end database and CRUD services up FAST. At this point, we were able to hook up HTML and JavaScript on the front-end and start pulling in live data quickly.

Later I'm going to learn about adding custom APIs, social logins, and push notifications as they'll be useful for future hackathons.


Sponsor: Big thanks to Telerik Icenium for sponsoring the blog's feed this week! Telerik Icenium now includes Visual Studio integration. Code once using HTML5 & JavaScript - build, test, and publish native-running iOS & Android apps. Start your 30 day trial with support!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.