Restaurant Classification Via the Yellow Pages API Using F#

As part of the restaurant analysis I did for open data day, I built a crude classifier to identify Chinese restaurants.  The classifier looked at the name of the establishment and if certain key words were in the name, it was tagged as a Chinese restaurant.

  1. member public x.IsEstablishmentAChineseRestraurant (establishmentName:string) =
  2.     let upperCaseEstablishmentName = establishmentName.ToUpper()
  3.     let numberOfMatchedWords = upperCaseEstablishmentName.Split(' ')
  4.                                 |> Seq.map(fun x -> match x with
  5.                                                         | "ASIA" -> 1
  6.                                                         | "ASIAN" -> 1
  7.                                                         | "CHINA" -> 1
  8.                                                         | "CHINESE" -> 1
  9.                                                         | "PANDA" -> 1
  10.                                                         | "PEKING" -> 1
  11.                                                         | "WOK" -> 1
  12.                                                         | _ -> 0)
  13.                                 |> Seq.sum
  14.     match numberOfMatchedWords with
  15.         | 0 -> false
  16.         | _ -> true

Although this worked well enough for the analysis, I was interested in seeing if there was a way of using something that is more precise.  To that end, I thought of the Yellow Pages – they classify restaurants into categories and assuming that the restaurant is in the yellow pages, it is a better way to determine the restaurant category versus just a name search.

The first thing I did was head over to the Yellow Pages (YP.com) website and sure enough, they have an API and a developers program.  I signed up and had an API key within a couple of minutes.

The first thing I did was to try and search for a restaurant in the browser.  I picked the first restaurant I came across in the dataset – Jumbo China #5.  I created a request uri based on their API like so

http://pubapi.atti.com/search-api/search/devapi/search?term=Jumbo+China+5&searchloc=6108+Falls+Of+Neuse+Rd+27609&format=json&key=XXXXXXXXXX

When I plugged the name into the browser, I got this:

image

After screwing around with the code for about ten minutes thinking it was my API Key (Invalid Key would lead you to believe that, no?), Mike Thomas came over and told me that the url encoding was messing with my request – specifically the ‘#’ in Jumbo China #5.  When I removed the # symbol, I got Json back:

image

Throwing the Json into Json2CSharp, the results look great:

image

I then took this URL and tried to load it into a F# type provider, I couldn’t understand why I was getting a red squiggly line of approbation (Json and XML):

image

 

so I pulled out Fiddler to see I was getting a 400.  Digging into the response value, I found that “User Agent” was a required field. 

image

The problem was then compounded because the FSharp Json type provider does not allow you to enter a User Agent into the constructor.  I headed over to Stack Overflow where Thomas Petricek was kind enough to answer the question – basically you have to use the FSharp Http class to make the request (which you can add the user agent to) and then parse the response via the JsonProvider using the “Parse” versus the “Load” method.  So spinning up the method like so:

image

This gave me the results back that I wanted.  I then created a couple of methods to clean up any characters that might screw up the url encoding, added some argument validation, and I had a pretty good module to consume the YP.com listings:

  1. namespace ChickenSoftware.RestaurantClassifier
  2.  
  3. open System
  4. open FSharp.Data
  5. open FSharp.Net
  6.  
  7. type ypProvider = JsonProvider< @"YP.txt">
  8.  
  9. type RestaurantCatagoryRepository() =
  10.    member this.GetCatagories(restaurantName: string, restaurantAddress: string) =
  11.         if(String.IsNullOrEmpty(restaurantName)) then
  12.             failwith("restaurantName cannot be null or empty.")
  13.         if(String.IsNullOrEmpty(restaurantAddress)) then
  14.             failwith("restaurantAddress cannot be null or empty.")
  15.         let cleanedName = this.CleanName(restaurantName)
  16.         let cleanedAddress = this.CleanAddress(restaurantAddress);
  17.         let uri = "http://pubapi.atti.com/search-api/search/devapi/search?term=&quot;+cleanedName+"&searchloc="+cleanedAddress+"&format=json&key=XXXXXX"
  18.         let response = FSharp.Net.Http.Request(uri, headers=["user-agent", "None"])
  19.         let ypResult = ypProvider.Parse(response)
  20.         try
  21.             ypResult.SearchResult.SearchListings.SearchListing.[0].Categories
  22.         with
  23.             | ex -> String.Empty
  24.  
  25.     member this.CleanName(name: string) =
  26.                 name.Replace("#","").Replace(" ","+")
  27.  
  28.     member this.CleanAddress(address: string)=
  29.                 address.Replace("#","").Replace(" ","+")
  30.     
  31.     member this.IsCatagoryInCatagories(catagories: string, catagory: string) =
  32.         if(String.IsNullOrEmpty(catagories)) then false
  33.         else if (String.IsNullOrEmpty(catagory)) then false
  34.         else catagories.Contains(catagory)
  35.  
  36.     member this.IsRestaurantInCatagory(restaurantName: string, restaurantAddress: string, restaurantCatagory: string) =
  37.         if(String.IsNullOrEmpty(restaurantName)) then
  38.             failwith("restaurantName cannot be null or empty.")
  39.         if(String.IsNullOrEmpty(restaurantAddress)) then
  40.             failwith("restaurantAddress cannot be null or empty.")
  41.         if(String.IsNullOrEmpty(restaurantCatagory)) then
  42.             failwith("restaurantCatagory cannot be null or empty.")
  43.  
  44.         System.Threading.Thread.Sleep(new System.TimeSpan(0,0,1))
  45.         let catagories = this.GetCatagories(restaurantName, restaurantAddress)
  46.         if(String.IsNullOrEmpty(catagories)) then false
  47.         else this.IsCatagoryInCatagories(catagories,restaurantCatagory)
  48.  
  49.     member this.IsRestaurantInCatagoryAsync(restaurantName: string, restaurantAddress: string, restaurantCatagory: string) =
  50.         async {
  51.             if(String.IsNullOrEmpty(restaurantName)) then
  52.                 failwith("restaurantName cannot be null or empty.")
  53.             if(String.IsNullOrEmpty(restaurantAddress)) then
  54.                 failwith("restaurantAddress cannot be null or empty.")
  55.             if(String.IsNullOrEmpty(restaurantCatagory)) then
  56.                 failwith("restaurantCatagory cannot be null or empty.")
  57.  
  58.             let catagories = this.GetCatagories(restaurantName, restaurantAddress)
  59.             if(String.IsNullOrEmpty(catagories)) then return false
  60.             else return this.IsCatagoryInCatagories(catagories,restaurantCatagory)
  61.         }

The associated unit and integration tests that I made in building this module look like this:

  1. [TestClass]
  2. public class CatagoryBuilderTests
  3. {
  4.  
  5.     [TestMethod]
  6.     public void CleanName_ReturnsExpectedValue()
  7.     {
  8.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  9.         String restaurantName = "Jumbo China #5";
  10.  
  11.         String expected = "Jumbo+China+5";
  12.         String actual = repository.CleanName(restaurantName);
  13.         Assert.AreEqual(expected, actual);
  14.     }
  15.  
  16.     [TestMethod]
  17.     public void CleanAddress_ReturnsExpectedValue()
  18.     {
  19.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  20.         String restaurantAddress = "6108 Falls Of Neuse Rd 27609";
  21.  
  22.         String expected = "6108+Falls+Of+Neuse+Rd+27609";
  23.         String actual = repository.CleanAddress(restaurantAddress);
  24.         Assert.AreEqual(expected, actual);
  25.     }
  26.  
  27.  
  28.     [TestMethod]
  29.     public void GetCatagories_ReturnsExpectedValue()
  30.     {
  31.         string restaurantName = "Jumbo China #5";
  32.         String restaurantAddress = "6108 Falls Of Neuse Rd 27609";
  33.  
  34.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  35.         var result = repository.GetCatagories(restaurantName, restaurantAddress);
  36.         Assert.IsNotNull(result);
  37.     }
  38.  
  39.     [TestMethod]
  40.     public void CatagoryIsContainedInCatagoriesUsingValidTrueData_ReturnsExpectedValue()
  41.     {
  42.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  43.  
  44.         String catagories = "Chinese Restaurants|Restaurants|";
  45.         String catagory = "Chinese";
  46.  
  47.         Boolean expected = true;
  48.         Boolean actual = repository.IsCatagoryInCatagories(catagories, catagory);
  49.  
  50.         Assert.AreEqual(expected, actual);
  51.     }
  52.  
  53.     [TestMethod]
  54.     public void CatagoryIsContainedInCatagoriesUsingValidFalseData_ReturnsExpectedValue()
  55.     {
  56.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  57.  
  58.         String catagories = "Chinese Restaurants|Restaurants|";
  59.         String catagory = "Seafood";
  60.  
  61.         Boolean expected = false;
  62.         Boolean actual = repository.IsCatagoryInCatagories(catagories, catagory);
  63.  
  64.         Assert.AreEqual(expected, actual);
  65.     }
  66.  
  67.     [TestMethod]
  68.     public void IsJumboChinaAChineseRestaurant_ReturnsTrue()
  69.     {
  70.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  71.  
  72.         string restaurantName = "Jumbo China #5";
  73.         String restaurantAddress = "6108 Falls Of Neuse Rd 27609";
  74.         String restaurantCatagory = "Chinese";
  75.  
  76.         Boolean expected = true;
  77.         Boolean actual = repository.IsRestaurantInCatagory(restaurantName, restaurantAddress, restaurantCatagory);
  78.  
  79.         Assert.AreEqual(expected, actual);
  80.     }
  81.  
  82.     [TestMethod]
  83.     public void IsJumboChinaAnItalianRestaurant_ReturnsFalse()
  84.     {
  85.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  86.  
  87.         string restaurantName = "Jumbo China #5";
  88.         String restaurantAddress = "6108 Falls Of Neuse Rd 27609";
  89.         String restaurantCatagory = "Italian";
  90.  
  91.         Boolean expected = false;
  92.         Boolean actual = repository.IsRestaurantInCatagory(restaurantName, restaurantAddress, restaurantCatagory);
  93.  
  94.         Assert.AreEqual(expected, actual);
  95.     }
  96.  
  97.     [TestMethod]
  98.     public void IsUnknownAnItalianRestaurant_ReturnsFalse()
  99.     {
  100.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  101.  
  102.         string restaurantName = "Some Unknown Restaurant";
  103.         String restaurantAddress = "Some Address";
  104.         String restaurantCatagory = "Italian";
  105.  
  106.         Boolean expected = false;
  107.         Boolean actual = repository.IsRestaurantInCatagory(restaurantName, restaurantAddress, restaurantCatagory);
  108.  
  109.         Assert.AreEqual(expected, actual);
  110.     }
  111.  
  112.  
  113.  
  114.     [TestMethod]
  115.     public void CatagoryIsContainedInCatagoriesUsingEmptyCatagory_ReturnsExpectedValue()
  116.     {
  117.         RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  118.  
  119.         String catagories = "Chinese Restaurants|Restaurants|";
  120.         String catagory = String.Empty;
  121.  
  122.         Boolean expected = false;
  123.         Boolean actual = repository.IsCatagoryInCatagories(catagories, catagory);
  124.  
  125.         Assert.AreEqual(expected, actual);
  126.     }

The hardest test to get run green was the negative test – passing in a restaurant name that is not recognized

  1. [TestMethod]
  2. public void IsUnknownAnItalianRestaurant_ReturnsFalse()
  3. {
  4.     RestaurantCatagoryRepository repository = new RestaurantCatagoryRepository();
  5.  
  6.     string restaurantName = "Some Unknown Restaurant";
  7.     String restaurantAddress = "Some Address";
  8.     String restaurantCatagory = "Italian";
  9.  
  10.     Boolean expected = false;
  11.     Boolean actual = repository.IsRestaurantInCatagory(restaurantName, restaurantAddress, restaurantCatagory);
  12.  
  13.     Assert.AreEqual(expected, actual);
  14. }

To code around the fact that a different set of Json came back and the original code is expecting a specific structure, I finally resorted to a try…catch

  1. try
  2.     ypResult.SearchResult.SearchListings.SearchListing.[0].Categories
  3. with
  4.     | ex -> String.Empty

I feel dirty, but I don’t know how else to get around it.  In any event, I then coded up a module that pulled the list of restaurants from Azure and put them through the classifier.

  1. namespace ChickenSoftware.RestaurantClassifier
  2.  
  3. open FSharp.Data
  4. open System.Linq
  5. open System.Configuration
  6. open Microsoft.FSharp.Linq
  7. open Microsoft.FSharp.Data.TypeProviders
  8.  
  9. type internal SqlConnection = SqlEntityConnection<ConnectionStringName="azureData">
  10.  
  11. type public RestaurantBuilder () =
  12.     
  13.     let connectionString = ConfigurationManager.ConnectionStrings.["azureData"].ConnectionString;
  14.     
  15.     member public this.GetRestaurants () =
  16.         SqlConnection.GetDataContext(connectionString).Restaurants
  17.             |> Seq.map(fun x -> x.EstablishmentName, x.EstablishmentAddress + " " + x.EstablishmnetZipCode)
  18.             |> Seq.toArray
  19.             
  20.     member public this.GetChineseRestaurants () =
  21.         let catagoryRepository = new RestaurantCatagoryRepository()
  22.         let catagory = "Chinese"
  23.         this.GetRestaurants()
  24.                 |> Seq.filter(fun (name, address) -> catagoryRepository.IsRestaurantInCatagory(name, address,catagory))
  25.                 |> Seq.toList

This code is almost identical to the code I posted 2 weeks ago.  Sure enough, When I threw my integration tests at the functions, check out fiddler. 

image

I was getting responses.  I ran into the problem on the 50th request though.

image

To get around this occasional timeout issue, I threw in a second delay between each request, which seemed the solve the problem.

  1. System.Threading.Thread.Sleep(new System.TimeSpan(0,0,1))
  2. let catagories = this.GetCatagories(restaurantName, restaurantAddress)
  3. if(String.IsNullOrEmpty(catagories)) then false
  4. else this.IsCatagoryInCatagories(catagories,restaurantCatagory)

However, this then introduced a new problem.  There are 4,000 or so restaurants, so that is over 66 minutes of running.  Not good.  Next week, I hope to add some parallelism to speed things up…

 

 

 

 

 

Advertisements

Trigrams and F#

Rob Seder wrote a great post of trigrams last week.  He then asked me how the same functionality would be implemented in F# – specifically dropping the for..each.  Challenge accepted!.

The first thing I did was hit Stack Overflow to see if there is a built in function to parse a string by groups and  I had a answer within minutes for exactly what I was looking for (thanks MattNewport).

So to match Rob’s BuildTrigram function, I wrote this:

  1. type TrigramBuilder() =
  2.     member this.BuildTrigrams(inputString: string) =
  3.         inputString
  4.             |> Seq.windowed 3
  5.             |> Seq.map(fun a -> System.String a)
  6.             |> Seq.toArray

And I had a covering unit test already created:

  1. [TestMethod]
  2. public void GetTrigrams_ReturnsExpectedValue()
  3. {
  4.     var builder = new TrigramBuilder();
  5.     String inputString = "ABCDEFG";
  6.  
  7.     String[] expected = new String[] { "ABC", "BCD", "CDE", "DEF", "EFG" };
  8.     String[] actual = builder.BuildTrigrams(inputString);
  9.  
  10.     CollectionAssert.AreEqual(expected, actual);
  11. }

I then Implemented a function that matches his double loops (can’t tell the function name from the code snippet on the blog post):

  1. member this.GetMatchPercent(baseString: string, compareString: string) =
  2.     let trigrams = this.BuildTrigrams(compareString)
  3.     let matchCount = trigrams
  4.                         |> Seq.map(fun t -> match baseString.Contains(t) with
  5.                                                 | true -> 1
  6.                                                 | false -> 0)
  7.                         |> Seq.sum
  8.     let totalCount = trigrams.Length
  9.     float matchCount/float totalCount

And throwing in some covering unit tests:

  1. public void GetMatchPercentageOfExactMatch_ReturnsExpectedValue()
  2. {
  3.     var builder = new TrigramBuilder();
  4.     
  5.     String baseString = "ABCDEF";
  6.     String compareString = "ABCDEF";
  7.  
  8.     double expected = 1.0;
  9.     double actual = builder.GetMatchPercent(baseString, compareString);
  10.  
  11.     Assert.AreEqual(expected, actual);
  12. }
  13.  
  14. [TestMethod]
  15. public void GetMatchPercentageOf50PercentMatch_ReturnsExpectedValue()
  16. {
  17.     var builder = new TrigramBuilder();
  18.  
  19.     String baseString = "ABCD";
  20.     String compareString = "ABCDEF";
  21.  
  22.     double expected = 0.5;
  23.     double actual = builder.GetMatchPercent(baseString, compareString);
  24.  
  25.     Assert.AreEqual(expected, actual);
  26. }

Sure enough, green across the board:

image

 

Analysis of Health Inspection Data using F#

As part of the TRINUG F#/Analytics SIG, I did a public records request from Wake County for all of the restaurant inspections in 2013.  If you are not familiar, the inspectors go out and then give a score to the restaurant.  The restaurant then has to display their score like this:

image

After some back and forth, I got the data as an Excel spreadsheet that looks like this

image

I then loaded the spreadsheet into a sql server and exposed it as some OData endpoints.

  1. // GET odata/Restaurant
  2. [Queryable]
  3. public IQueryable<Restaurant> GetRestaurant()
  4. {
  5.     return db.Restaurants;
  6. }
  7.  
  8. // GET odata/Restaurant(5)
  9. [Queryable]
  10. public SingleResult<Restaurant> GetRestaurant([FromODataUri] int key)
  11. {
  12.     return SingleResult.Create(db.Restaurants.Where(restaurant => restaurant.Id == key));
  13. }

I then dove into the data to see if there were any interesting conclusions to be found.  Following my pattern of doing analytics using F# and unit testing using C#, I created a project with the following code:

  1. namespace ChickenSoftware.RestraurantChicken.Analysis
  2.  
  3. open System.Linq
  4. open System.Configuration
  5. open Microsoft.FSharp.Linq
  6. open Microsoft.FSharp.Data.TypeProviders
  7.  
  8. type internal SqlConnection = SqlEntityConnection<ConnectionStringName="azureData">
  9.  
  10. type public RestaurantAnalysis () =
  11.     
  12.     let connectionString = ConfigurationManager.ConnectionStrings.["azureData"].ConnectionString;

Note that I am using the connection string in two places – the 1st for the type provider to do its magic at design time and the second for actually accessing the data at run time.  With that set up, the 1st question I had was “ is there seasonality in inspection scores like there are in traffic tickets?”  To that end, I created the following function:

  1. member public x.GetAverageScoreByMonth () =
  2.     SqlConnection.GetDataContext(connectionString).Restaurants
  3.         |> Seq.map(fun x -> x.InspectionDate.Value.Month, x.InspectionScore.Value)
  4.         |> Seq.groupBy(fun x -> fst x)
  5.         |> Seq.map(fun (x,y) -> (x,y |> Seq.averageBy snd))
  6.         |> Seq.map(fun (x,y) -> x, System.Math.Round(y,2))
  7.         |> Seq.toArray
  8.         |> Array.sort

This is pretty vanilla F# code, with the tricky part being the average by month (lines 4 and 5 here).  What the code is doing is grouping up the 4,000 or so tuples that were created on line 3 into another tuple – with the fst being the groupBy value (in this case month) and then the second tuple being a tuple with the month and score.  Then, by averaging up the score of the second tuple, we get an average for each month.  I create a unit (really integration) test like so:

  1. [TestMethod]
  2. public void GetAverageScoreByMonth_ReturnsTwelveItems()
  3. {
  4.     var analysis = new RestaurantAnalysis();
  5.     var scores = analysis.GetAverageScoreByMonth();
  6.     Int32 expected = 12;
  7.     Int32 actual = scores.Length;
  8.     Assert.AreEqual(expected, actual);
  9. }

And the result ran green. 

image

Putting a break on the Assert and a watch on scores, you can see the values:

image

A couple of things stand out

1) The overall average is around 96 and change

2) There does not seem to be any significant variance among months.

Since I am trying to also teach myself D3, I then added a MVC5 project to my solution and added an analysis controller that calls the function in the analysis module and serves the results as json:

  1. public JsonResult AverageScoreByMonth()
  2. {
  3.     var analysis = new RestaurantAnalysis();
  4.     var scores = analysis.GetAverageScoreByMonth();
  5.     return Json(scores,JsonRequestBehavior.AllowGet);
  6. }

I then made a page with a simple D3 chart that calls this controller

  1. @{
  2.     Layout = "~/Views/Shared/_Layout.cshtml";
  3. }
  4.  
  5. <svg class="chart"></svg>
  6.  
  7. <style>
  8.     .bar {
  9.         fill: steelblue;
  10.     }
  11.  
  12.         .bar:hover {
  13.             fill: brown;
  14.         }
  15.  
  16.     .axis {
  17.         font: 10px sans-serif;
  18.     }
  19.  
  20.         .axis path,
  21.         .axis line {
  22.             fill: none;
  23.             stroke: #000;
  24.             shape-rendering: crispEdges;
  25.         }
  26.  
  27.     .x.axis path {
  28.         display: none;
  29.     }
  30. </style>
  31.  
  32.  
  33.  
  34. <script>
  35.  
  36.     var margin = { top: 20, right: 20, bottom: 30, left: 40 },
  37.         width = 960 – margin.left – margin.right,
  38.         height = 500 – margin.top – margin.bottom;
  39.  
  40.     var x = d3.scale.ordinal()
  41.         .rangeRoundBands([0, width], .1);
  42.  
  43.     var y = d3.scale.linear()
  44.         .range([height, 0]);
  45.  
  46.     var xAxis = d3.svg.axis()
  47.         .scale(x)
  48.         .orient("bottom");
  49.  
  50.     var yAxis = d3.svg.axis()
  51.         .scale(y)
  52.         .orient("left")
  53.         .ticks(10, "%");
  54.  
  55.     var svg = d3.select("body").append("svg")
  56.         .attr("width", width + margin.left + margin.right)
  57.         .attr("height", height + margin.top + margin.bottom)
  58.       .append("g")
  59.         .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  60.  
  61.  
  62.  
  63.     $.ajax({
  64.         url: "http://localhost:3057/Analysis/AverageScoreByMonth/&quot;,
  65.         dataType: "json",
  66.         success: function (data) {
  67.             x.domain(data.map(function (d) { return d.Item1; }));
  68.             y.domain([0, d3.max(data, function (d) { return d.Item2; })]);
  69.  
  70.             svg.append("g")
  71.                 .attr("class", "x axis")
  72.                 .attr("transform", "translate(0," + height + ")")
  73.                 .call(xAxis);
  74.  
  75.             svg.append("g")
  76.                 .attr("class", "y axis")
  77.                 .call(yAxis)
  78.               .append("text")
  79.                 .attr("transform", "rotate(-90)")
  80.                 .attr("y", 6)
  81.                 .attr("dy", ".71em")
  82.                 .style("text-anchor", "end")
  83.                 .text("Frequency");
  84.  
  85.             svg.selectAll(".bar")
  86.                 .data(data)
  87.               .enter().append("rect")
  88.                 .attr("class", "bar")
  89.                 .attr("x", function (d) { return x(d.Item1); })
  90.                 .attr("width", x.rangeBand())
  91.                 .attr("y", function (d) { return y(d.Item2); })
  92.                 .attr("height", function (d) { return height – y(d.Item2); });
  93.  
  94.         },
  95.         error: function (e) {
  96.             alert("error");
  97.         }
  98.     });
  99.  
  100.     function type(d) {
  101.         d.Item2 = +d.Item2;
  102.         return d;
  103.     }
  104. </script>

And when I run it, a run-of-the mill barchart (I did have to adjust the F# to shift the decimal to the left two positions so that I could match the scale of the chart’s template.  For me, it is easier to alter the F# than the javascript:

image

Following this pattern, I did some other season analysis like average by DayOfMonth

image

DayOf Week.

image

So there does not seem to be any seasonality in inspection scores.

I then did an average of inspectors

image

And there looks to be some variance, but it is getting lost of the scale of the map.  The problem is that the range of the scores is not 0 to 100

Here is a function that counts the number of scores (rounded to 0)

  1. member public x.CountOfRoundedScores () =
  2.     SqlConnection.GetDataContext(connectionString).Restaurants
  3.         |> Seq.map(fun x -> System.Math.Round(x.InspectionScore.Value,0), x.InspectionID)
  4.         |> Seq.groupBy(fun x -> fst x)
  5.         |> Seq.map(fun (x,y) -> (x,y |> Seq.countBy snd))
  6.         |> Seq.map(fun (x,y) -> (x,y |> Seq.sumBy snd))
  7.         |> Seq.toArray

That graphically looks like:

image

So back to inspectors, I needed to adjust the scale from 0 to 100 to 80 to 100.  I also needed to remove the null inspection Ids and the records that were for the ‘test facility’ and the 6 records that were below 80.

  1. member public x.AverageScoreByInspector () =
  2.     SqlConnection.GetDataContext(connectionString).Restaurants
  3.         |> Seq.filter(fun x -> x.EstablishmentName <> "Test Facility")
  4.         |> Seq.filter(fun x -> x.InspectionScore.Value > 80.)
  5.         |> Seq.filter(fun x -> x.InspectionID <> null)
  6.         |> Seq.map(fun x -> x.InspectorID, x.InspectionScore.Value)
  7.         |> Seq.groupBy(fun x -> fst x)
  8.         |> Seq.map(fun (x,y) -> (x,y |> Seq.averageBy snd))
  9.         |> Seq.map(fun (x,y) -> x, y/100.)
  10.         |> Seq.map(fun (x,y) -> x, System.Math.Round(y,4))
  11.         |> Seq.toArray
  12.         |> Array.sort

I then adjusted the scale of the inspector graph to have to domain from 80 to 100 (versus 0 to 100) and the scale of the y axis.  This was a good article explaining Scales and Domains in D3.

  1. var yAxis = d3.svg.axis()
  2.     .scale(y)
  3.     .orient("left")
  4.     .ticks(10);

  1. $.ajax({
  2.     url: "http://localhost:3057/Analysis/AverageScoreByInspector/&quot;,
  3.     dataType: "json",
  4.     success: function (data) {
  5.         x.domain(data.map(function (d) { return d.Item1; }));
  6.         y.domain([80, d3.max(data, function (d) { return d.Item2; })]);

and now there is pretty good graph showing the variance among inspectors:

image

So the interesting this is that #1168 is 2 below the average – which of a domain of 10 is pretty significant.  Interestingly, 1168 is also the inspector who has all of the “Test facility” records – so they are probably the trainer and/or lead inspector.  With this analysis in the back pocket, ran a function that did the inspection score by establishment type:

image

This is kinda interesting (esp that pushcarts got the highest scores) but I wanted to see if there was any truth the the common perception that Chinese restaurants are less sanitary than other kinds of restaurants.  To that end, I created a rudimentary classifier that searched the name of the establishment to see if it had a name that is typically associated with fast-food Chinese:

  1. member public x.IsEstablishmentAChineseRestraurant (establishmentName:string) =
  2.     let upperCaseEstablishmentName = establishmentName.ToUpper()
  3.     let numberOfMatchedWords = upperCaseEstablishmentName.Split(' ')
  4.                                 |> Seq.map(fun x -> match x with
  5.                                                         | "ASIA" -> 1
  6.                                                         | "ASIAN" -> 1
  7.                                                         | "CHINA" -> 1
  8.                                                         | "CHINESE" -> 1
  9.                                                         | "PANDA" -> 1
  10.                                                         | "PEKING" -> 1
  11.                                                         | "WOK" -> 1
  12.                                                         | _ -> 0)
  13.                                 |> Seq.sum
  14.     match numberOfMatchedWords with
  15.         | 0 -> false
  16.         | _ -> true

I then created a function that returned the average and ran my unit tests.

  1. [TestMethod]
  2. public void IsEstablishmentAChineseRestraurantUsingWOK_ReturnsTrue()
  3. {
  4.     var analysis = new RestaurantAnalysis();
  5.     String establishmentName = "JAMIE'S WOK";
  6.  
  7.     var expected = true;
  8.     var actual = analysis.IsEstablishmentAChineseRestraurant(establishmentName);
  9.     Assert.AreEqual(expected, actual);
  10. }
  11.  
  12. [TestMethod]
  13. public void IsEstablishmentAChineseRestraurantUsingWok_ReturnsTrue()
  14. {
  15.     var analysis = new RestaurantAnalysis();
  16.     String establishmentName = "Jamie's Wok";
  17.  
  18.     var expected = true;
  19.     var actual = analysis.IsEstablishmentAChineseRestraurant(establishmentName);
  20.     Assert.AreEqual(expected, actual);
  21. }
  22.  
  23. [TestMethod]
  24. public void AverageScoreForChineseRestaurants_ReturnsExpected()
  25. {
  26.     var analysis = new RestaurantAnalysis();
  27.     var actual = analysis.AverageScoreForChineseRestaurants();
  28.     Assert.IsNotNull(actual);
  29. }

When a break was put on the value of the average, it was apparent that Chinese restaurants scored significantly lower than the average of 96

image

So then I applied 1 more segmentation: Chinese versus Non-Chinese scores by inspector:

  1. member public x.AverageScoresOfChineseAndNonChineseByInspector () =
  2.     let dataSet = SqlConnection.GetDataContext(connectionString).Restaurants
  3.                     |> Seq.map(fun x -> x.EstablishmentName, x.InspectorID,x.InspectionScore.Value)
  4.     let chineseRestraurants = dataSet
  5.                                 |> Seq.filter(fun (a,b,c) -> x.IsEstablishmentAChineseRestraurant(a))
  6.                                 |> Seq.map(fun (a,b,c) -> b,c)
  7.                                 |> Seq.groupBy(fun x -> fst x)
  8.                                 |> Seq.map(fun (x,y) -> (x,y |> Seq.averageBy snd))
  9.                                 |> Seq.map(fun (x,y) -> x, System.Math.Round(y,2))
  10.                                 |> Seq.toArray
  11.                                 |> Array.sort
  12.     let nonChineseRestraurants = dataSet
  13.                                 |> Seq.filter(fun (a,b,c) -> not(x.IsEstablishmentAChineseRestraurant(a)))
  14.                                 |> Seq.map(fun (a,b,c) -> b,c)
  15.                                 |> Seq.groupBy(fun x -> fst x)
  16.                                 |> Seq.map(fun (x,y) -> (x,y |> Seq.averageBy snd))
  17.                                 |> Seq.map(fun (x,y) -> x, System.Math.Round(y,2))
  18.                                 |> Seq.toArray
  19.                                 |> Array.sort
  20.     Seq.zip chineseRestraurants nonChineseRestraurants
  21.            |> Seq.map(fun ((a,b),(c,d)) -> a,b,d)
  22.            |> Seq.toList

And in graphics using a double-bar chart:

image

So this is kinda interesting.  The lead inspector (1168) who grades everyone lower actually gives Chinese restaurants higher marks.  Everyone else pretty much grades Chinese restaurants lower except for 1 inspector.  Also, 1708 must really not like Chinese restaurants – or their inspection list has a series of really bad Chinese restaurants.

Note that this may not be statistically significant (I didn’t control for sample size, etc..) – but further analysis might be warranted, no?  If you are interested, here is the endpoint: http://restaurantchicken.cloudapp.net/odata/Restaurant

Finally, when I presented this analysis to TRINUG last week, lots of people became interested in F# and analytics (ok, maybe 3).  You can see the comments here.  Also, I now have an appointment with the head of the health department department and the CIO of Wake County later this week – let’s see what they say…

 

 

Unit Testing F# Projects

I am a big believer of unit tests and as I write more and more code, I suffer what sociologists call “confirmation bias” whereby I keep finding more and more reasons to confirm that I am right.  But I am at the point where I don’t believe in developer documentation (sorry Sandcastle), reflector (sorry Redgate), code comments, or architectural diagrams.  I believe in the code, the whole code, and nothing but the code.  Or as Rasheed Wallace might say if he was a coder versus a professional basketball player: “Code Don’t Lie!”.

And the only code that tells you what a module is doing is the unit tests.  If you want to see how the module behaves, look at the green unit tests.  If you want to see how the module is supposed to behave but is not, look at the red unit tests.  If you want to see how the module might or might behave because the code is out of control, look for the non-existent unit tests.

So when I started writing code in F#, the unit tests went along for the ride.  F# folks will tell you to use the REPL to get your code working and/or use a unit test project in F#.  I don’t do either because:

1) The REPL is designed for quick prototyping, not for having a durable, cantonal example of module behavior. Having a full suite of unit tests gives you code coverage, tests at the build, and a fail-proof way of documenting the code’s behavior.

2) Most of the other developers I work with are CSharpers.  Having the unit tests in C# allows them to understand the behavior in a language in which they are familiar.  Since the tests need to communicate the working code’s intent, having that in a language they understand is critical.  They don’t have to understand F# to use a F# module.  Also, porting from C# in MSTest to NUnit in C# is a snap.

So when you add a C# Unit Test project to your solution that has a F# module you want to test, there are a couple of things you need to do.

1) Add a reference from the Unit Test Project to the F# Project (Right Click, Add Reference)

image

2) Add a reference to F#

image

3) If you are using type providers (and who doesn’t) and you have the connection string in the .config file of the working code project, add a .config file to the unit test project and copy over the connection string

image

Note that you code has to reflect that the connection string is being used in 2 different ways by the type provider, as explained in this post.  Your F# code needs to look like this:

  1. type internal SqlConnection = SqlEntityConnection<ConnectionStringName="azureData">
  2.  
  3.  
  4. type public RestaurantAnalysis () =
  5.     
  6.     let connectionString = ConfigurationManager.ConnectionStrings.["azureData"].ConnectionString;
  7.     member public x.GetScoresByMonth () =
  8.         SqlConnection.GetDataContext(connectionString).Restaurants
  9.             |> Seq.map(fun x -> x.InspectionDate.Value.Month, x.InspectionScore.Value)
  10.             |> Seq.groupBy(fun x -> fst x)
  11.             |> Seq.toList

4) Finally, you have to rebuild the F# project each time you want the unit tests to pick up changes.  That is different from a C# unit test project referencing a C# working code project.

Finally, not related to unit testing but too short for a blog post, if you are using the type providers (and who doesn’t) and you need to expose your classes publicly, you can’t use the SqlEntity provider – you need to use the SqlData provider.  The catch is that SqlData does not work with Azure Sql Storage as far as I can tell.  In my case, I used SqlEntity and exposed tuples and custom types publicly.  Not the best, but still better than using C# and Entity Framework…