Carolina Code Camp Prep

I am going down to Charlotte for the Carolina Code Camp this weekend.  I am presenting at two sessions:

1) Remote Control Lawnmowers (that I am building with my daughter).  I will show how to intercept and use PWM signals from a RC transmitter/receiver and how to turn those commands into the drive system of the robot.  Here is a couple of clips that we did recently:

 

 

I am also doing a code-only SOLID presentation.  This is pretty much the same presentation that I did at TRINUG’s DEV Craftsmanship SIG in February.

I am looking forward to a fun (half) day and learning some great stuff from the other hackers coders.

This and That

Dear Future Jamie:

DI with a private constructor

When you are doing DI with a private constructor, this syntax will not work:

            String animalTypeName = ConfigurationManager.AppSettings["animalTypeName"].ToString();
            Type animalType = Type.GetType(animalTypeName);
            IAnimal animal = (IAnimal)Activator.CreateInstance(animalType);

Instead use this syntax:

            String animalTypeName = ConfigurationManager.AppSettings["animalTypeName"].ToString();
            Type animalType = Type.GetType(animalTypeName);
            IAnimal animal = (IAnimal)FormatterServices.GetUninitializedObject(animalType);

Params Keyword

When you have a series of overloaded methods that are taking on an ever-growing number of parameters, consider using the params keyword.  For example, instead of this:

        public void NameMe(String FirstName)
        {

        }

        public void NameMe(String FirstName, String LastName)
        {

        }

        public void NameMe(String FirstName, String MiddleName, String LastName)
        {

        }

Do this:

        public void NameMe(params String[] parameters)
        {

        }

Love Current Jamie

Machine Learning For Hackers: Using MSFT technologies

So I picked up Machine Learning for Hackers and started going though the 1st couple of chapters last night.  I love the premise that developers can learn something about machine learning using some easily-understood examples.  Why I didn’t love was that it used R.  After reading the 1st chapter, I said to myself, “self, everything they do in this chapter you can do in MSFT office.”  Now I get that this is supposed to be a simple example to get the reader up and running with R, but I thought, “Hey, I wonder how much of this book can I do using the MSFT stack?”

Chapter 1 was all about cleaning up data to get it ready for analysis.  This brought me back to my 1st job out of school (marketing analyst) when I spent more time collecting and cleaning the data than actually analyzing the data.

My 1st stop was to download the sample UFO dataset from the publisher site and save it locally with a .txt extension.  I then imported the data into Microsoft access: Note that I assigned column names and made sure that the Long Description is memo:

image

 

Note that there were

With that base data imported, I then added an additional field to the table called valid.  The book follows the process of analyzing each column’s data and removing rows that are not analyzable.  I learned the hard way many years ago that you should never permanently remove data because you need to be able to trace the data set back to its initial state for audit purposes.  I like the ability to make queries upon queries in Access to layer up data validation steps.

For example, in the book, the first validation is to parse the DateOccured field into year/month/columns – the problem is that some columns don’t have valid data.  To this 2 step process, the first is to aggregate the values in the column and see what the valid values look like:

imageimage

 

Sure enough, there are over 250 rows of data that cannot be used.  I created a table of the aggregate dates and the inner joined that table back to the base datatable.  Rows with invalid data was flagged as Invalid:

imageimage

I then divided the Location field into City, State:

image

And then created table for valid states – the same way I did for the valid DateOccured.  I had to remove about 10% of the dataset – because of both malformed data and the fact that the dataset is world-wide and the book’s example is only interested in the USA.  The fact that 90% of the world’s UFO sightings is in America probably says something, though I am not sure what.

In any event,  then exported the data into Excel, threw a pivot table on the data and added some conditional formatting:

image

Note that some malformed data slipped in (“NT”, “NE”, etc…) but I was too lazy to go back and clean it up.

Note that this is total sightings, not per-capita so you would expect that the states with larger populations to have more sightings (assuming that UFOs are targeting the USA evenly).  I think the color presentation is more effective and really draw your eye to CA and WA much more than the 50 histogram display that is found in the book.

I then filtered the data to only greater than 1990 like the book did and changed the pivot table to filter on state.  Here is California:

imageimage

The color really shows how the number of sightings are increasing in CA and seem to be more consistent in WA.  I do wonder what happened in 1995 in WA to cause that spike?

The next chapter is about regression analysis, so I assume that I can use the built-in functions of Excel for that too…

Sql Server and “Saving Changes Are Not Permitted”

Dear Future Jamie:

If you are trying to add a identity column to a table after it is created and you get this message:

image

Make the change in Management Studio here:

image

 

Love,

Current Jamie

SignalR: 1st Project

So I wanted to learn more about SignalR so I went over to GitHib and checked out their getting started page.  The page makes some assumptions that a new .NET developer might run into so I thought I would show how I got it working.

Step #1: Open Visual Studio 2012 and File->NewProject  and select ASP.NET Empty Web Application:

image I

Note that I made the solution name different than the name of the project because I will be added another project to this solution.

Step #2: Add a new console application to the solution:

image

Your solution should now look like this:

image

Step #3: Go to the Server project and open the NuGet Package Manager Console:

image

In the Package Manager Console Window, type

Install-Package Microsoft.AspNet.SignalR

Make sure that the Default Project is the Server project you created

image

after you hit enter, NuGet will do a bunch of stuff for you (adding libraries, resolving dependencies, etc…) and open the readme.txt file.  You can close that window.

Step #4: Add a class to your server project  called MyConnection (you are using Shift+Alt+C aren’t you?):

image

Step #5: Type (or copy) the code into your MyConnection class.  Note that you are inheriting from the PersistentConnection class and you will have to resolve (CTRL+.) both that class and the Task class.

image

Your class should look like this:

image

Step #6: Add a Global.asax file to your server project

image

Then type(or copy) the code into your Application_Start event handler:

image

Note that you will have to resolve the RouteTable class (CTRL+.).

The server project is now ready to go.  Make sure the server project is the startup project and run it.  IE will launch and you will get a web page like this:

image

Make a note of the address.  You can then stop the project from running.

Step #7: Go to the Client project and open the NuGet Package Manager Console and install the SignalR Client package.  Important, make sure that the default project is pointed to the client project

image

Step #8 Go to the Program.Main method and type (or copy) the code in.  Note that you will have to resolve the Connection class

image

Note that the address for the connection will be different for your machine.  Match that connection address back to the uri that IE showed you when you ran the server project.

Step #9: Open up the Configuration of the solution file and change the solution to have multiple startup projects.  Make sure you change the order so the client starts AFTER the server

image

Step #10 Run the solution and make the console window have focus.  Type in something and see the server push that message back out to you

image