Monday, April 22, 2013

Same Name Game

Found this entry on a blog post about “pouring over” retro sheets to find some data. Not sure if the pouring over is thrown in there for dramatic effect or what. Honestly once you have the retro sheet loaded up in SQL Server you really do not have to pour over anything, just write a query.


I digress. Anyway the blog post wondered how many times a batter shared the same first name of the previous batter’s last name. The author of the blog stated that this has happened 130 times. After some “pouring over” I discovered the author had only reported 1 instance per game. 


I wanted to see how many actual plate appearances this same name game had happened. My data set is only 1952-2012 and I show 315 Times where  a batter had the same first name as the previous batter’s last name.


SELECT e.GAME_ID, e.YEAR_ID,p.lastname, p.firstname, p2.lastname, p2.firstname
FROM dbo.[events] e JOIN dbo.[lkup_people] p ON e.BAT_ID = p.id
JOIN dbo.[events] e2 ON e2.seq_events = e.seq_events+1
JOIN dbo.[lkup_people] p2 ON e2.BAT_ID = p2.id
WHERE p.lastname = p2.firstname
AND e.BAT_TEAM_ID = e2.BAT_TEAM_ID

image



Looking more closely at the Casey Blake/Blake DeWitt combo we see that Casey Blake reached base 48% of the time before Blake DeWitt stepped to the plate. Blake DeWitt would then reach base 38.5% of the time directly following Casey Blake. 


image



SELECT e.GAME_ID, e.YEAR_ID,p.lastname, p.firstname, p2.lastname, p2.firstname,e.EVENT_CD, e2.EVENT_CD
FROM dbo.[events] e JOIN dbo.[lkup_people] p ON e.BAT_ID = p.id
JOIN dbo.[events] e2 ON e2.seq_events = e.seq_events+1
JOIN dbo.[lkup_people] p2 ON e2.BAT_ID = p2.id
WHERE p.lastname = p2.firstname
AND e.BAT_TEAM_ID = e2.BAT_TEAM_ID

Monday, April 15, 2013

Servo Tests

Started playing with servo and the Arduino tonight. My goal is to make a web cam turret controlled by a joystick or game pad of some kind.



Here is a video of the servo with a mind of its own…as in hard-coded movement sequences.


[youtube http://www.youtube.com/watch?v=m3Zyj4mkX8g]


Here is a video with the servo hooked up to the potentiometer 


[youtube http://www.youtube.com/watch?v=Y9t6nbja2Yg]


My next step is to figure out how to hook up the gamepad to control the movements. I have a few different gamepads to try. Just need to make the time to test.

Thursday, April 11, 2013

Hitting bombs 0-2 with 2 outs

So started playing with the retrosheet data. Retrosheet is a non-profit organization that gathers baseball stats and presents them in consumable formats. 


Then some dude wrote this software called chadwick which allows you to parse the data and import it into a database. The article I found created a MySQL database which is ok for small stuff. The event data (every plate appearance since 1952) is 9.4 million rows. I didn’t really have the patience to learn how to optimize this using MySQL, so I quickly moved the data over to a SQL Server instance. So now I have a SQL Server database with every play from 1952-2012. 



I have run several queries against the data, created some stored procedures for Run Expectancy and such. One of the searches I am working on is who is the player that has hit the most Home runs with an 0-2 count with 2 outs. See the chart below.


image



Sammy “No Comprende” Sosa tops the list with 11 jacks on an 0-2 2 out count. In fact we have several who’s who of the steroid era in this list. Jose Valentin has the least amount of career bombs but hits our list at number 7. 7.32% of the time with a 0-2 2 out count he hits a home run, and a full 3.61% of his Home runs were hit on this count. Kendrys Morales has the fewest TotalABs at this count and is leading in PCT HRs on this count. His PCT will go down as he gets more ABs.


CTE used to get Initial Counts.


WITH HRCTE AS 
(select p.firstname + ' ' + p.lastname Player, COUNT(1) TotalHR, p.id
FROM events e JOIN lkup_people p ON e.BAT_ID = p.id
WHERE STRIKES_CT = 2
and BALLS_CT = 0
AND OUTS_CT = 2
AND EVENT_CD = 23
GROUP BY p.firstname + ' ' + p.lastname, p.id)
SELECT r.player, r.TotalHR, COUNT(1)
FROM events e JOIN HRCTE r on e.BAT_ID = r.id
WHERE STRIKES_CT = 2
and BALLS_CT = 0
AND OUTS_CT = 2
GROUP BY r.player, r.TotalHR
ORDER BY 2 DESC

Monday, April 1, 2013

Monday, March 18, 2013

Cost of a Bedroom overnight

After hooking up a humidifier in the boys room overnight, I started to wonder what that actually cost me. Easy enough to calculate. The boys have a clock radio, a night light and the humidifier running overnight. They have not been running the ceiling fan so I did not include that in my calculations. I used 10 hours per night which is about average.


Here are the numbers for the Humidifier.


image



I figured 350 nights per year taking into account the nights we are not at home, probably away from home a few more nights and sure some other variables here but you get the gist. So if we run the humidifier every night that will cost me $107 per year. That is of course if we run the humidifier every night. Costs .30 cents per night.


Since we only run the humidifier when the boys are sick, I wanted so see what their room cost me overnight on a more typical night with just the night light and the radio. Figure below.


image



So 0.08 cents per night on a normal night. The little things add up. Surprised that the radio drew less power than the night light. The radio is playing music and displaying the time all night.

Friday, March 15, 2013

SQL Server xp_dirtree

I needed a way to parse the filenames in a directory and plug them into a RESTORE command. The xp_dirtree works really well for this.



IF OBJECT_ID('tempdb..#restoreFiles') IS NOT NULL
DROP TABLE #restoreFiles

SET QUOTED_IDENTIFIER OFF
CREATE TABLE #restoreFiles
(
[filename] VARCHAR(2000),
[depth] INT,
[file] INT
)

INSERT INTO #restoreFiles
EXECUTE xp_dirtree 'D:backups',1,1

From there I was able to set a variable based off a WHERE clause. My FULL backups are pre-pended with the word FULL so I can plug that into my RESTORE command.


DECLARE @FullBackupFile VARCHAR(2000) = (SELECT TOP 1 [filename] FROM #restoreFiles WHERE [filename] like 'FULL%' ORDER BY [filename] DESC)

PRINT ('RESTORE DATABASE DbName
FROM DISK = "D:Backups' + @FullBackupFile + '"
WITH MOVE "DataFile" TO "D:MSSQLDataData.mdf",
MOVE "LogFile" TO "D:MSSQLDatalog.ldf",
NORECOVERY,
REPLACE,
STATS = 10
GO')

Wednesday, March 13, 2013

Syntax Highlighter using Pygments and Python

I was using this web site to highlight and format my SQL code to HTML to insert into our wiki at work on Sharepoint, when all of a sudden the website stopped working. After some searching I discovered the site was using Pygments, a Syntax Highlighting Library for Python. So I decided to try and roll my own solution. This article will detail the steps I went through to make it happen.


Download


  • Download Python 2.7, 3.3 did not work for Easy Setup which I used to install pygments. http://www.python.org/download/

  • Download Easy Setup also called setuptools. https://pypi.python.org/pypi/setuptools

Install


  • Installed Python 2.7 with the defaults which creates a folder called C:Python27

  • Installed Easy Setup using the EXE taking the defaults

  • Installed Pygments by hitting a console window and running this from the C:Python27Scripts folder. easy_install Pygments

Configure


  • You have to configure IIS to work with Python.

  • IIS 7: I used this article for IIS 7 config: http://forums.iis.net/t/1122937.aspx

  • IIS 6: I used this article for IIS 6 Config: http://www.winservermart.com/HowTo/Install-Python-In-Windows-Server.aspx

  • The IIS 6 and IIS 7 config is a little different specifically on the Executable parameters.

Code


  • First I setup a basic HTML page with a simple form.The page had a single form with a TEXTAREA named code.

  • Second I pieced a codedump.py Python file together using code snippets from the Pygments web site, StackOvervlow, and Python help files.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cgi
# enable debugging
import cgitb
cgitb.enable()

#print "Content-Type: text/plain;charset=utf-8"
print


from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from pygments.styles import get_all_styles


form = cgi.FieldStorage()
code = form["code"].value

print ""
print ""
print ""
print highlight(code, SqlLexer(), HtmlFormatter(noclasses=True,style='default'))
print ""
print ""
print ""
#print HtmlFormatter().get_style_defs('.highlight')


print highlight(code,SqlLexer(),HtmlFormatter(noclasses=True,style='default'))


#print list(get_all_styles())

  • SqlLexer is hardcoded here meaning that we can only convert SQL code with this page.

  • Working on allowing for other languages

  • Pygments Formatter has several options, this code is only using the noclasses and style options.

  • There are several builtin Styles and you can create your own styles.

Here is the semi-finished product. http://www.thejoestory.com/codeschemer



Next Steps


  • Allow for different types of code

  • Create a custom style (don’t like the default background color)

Friday, March 8, 2013

Tuesday, March 5, 2013

Wednesday, February 13, 2013

Work Temps


Took the ol Laser Infrared Thermometer to work last week and recorded temps of things around the desk. Far left is the Baseline temp. The Up/Down trends are between each recording. The Temp Diff column on the  far right is the difference between the 4:14PM reading on 2/5 and the 7:40AM reading on 2/6.