Chuck Grimmett

May 2016

Index

  • Counting and JSON output in Jekyll

    Counting and JSON output in Jekyll

    Today I learned:

    Jekyll has a built-in filter called jsonify to turn data into JSON and Liquid has a filter called json. These is pretty useful for turning standard Jekyll output arrays into JSON, but if you need something more customized, you’ll need to build it yourself because Liquid’s ability to create arrays is limited. (Not a criticism, just an observation. It is primarily a templating language.)

    For example, I wanted to store the dates I have blog posts for and the number of posts on each of those dates in order to pass it to a heatmap calendar. I couldn’t figure out a way to build that into a single array with Liquid (and I didn’t want to drop down into Ruby), so I figured out a way build JSON manually.

    This outputs my blog’s post dates in the Unix timestamp format and displays the number of posts on a given day and leaves the trailing comma off to comply with the JSON standard:

    {% assign counter = 0 %}{ {% for post in site.posts %}{% capture day %}{{ post.date | date: '%s' }}{% endcapture %}{% capture prevday %}{{ post.previous.date | date: '%s' }}{% endcapture %}{% assign counter = counter | plus: 1 %}{% if day != prevday %}"{{ post.date | date: '%s' }}": {{ counter }}{% assign counter = 0 %}{% if forloop.last == false %},{% endif %} {% endif %}{% endfor %}} 

    Output:

    { 	"1463716800": 1, 	"1463371200": 1, 	"1462593600": 2, 	"1462507200": 1, 	"1462248000": 1, 	"1462161600": 1, 	"1461643200": 1, 	"1461297600": 3, 	"1461124800": 1, 	"1460952000": 1, 	"1460606400": 1, 	"1460347200": 1, 	"1459915200": 1, 	"1458792000": 1, 	"1458705600": 1, 	"1458619200": 1, 	"1458532800": 1, 	"1458446400": 1, 	"1458273600": 2, 	"1457586000": 1, 	"1457499600": 1, 	"1457413200": 1, 	"1457326800": 1, 	"1457240400": 1, 	"1456981200": 1, 	"1456894800": 1, 	"1456808400": 1, 	"1456722000": 1, 	"1456376400": 1, 	"1456290000": 1, 	"1456203600": 1, 	"1456117200": 1, 	"1456030800": 1, 	"1455944400": 1, 	"1455858000": 1, 	"1455771600": 2, 	"1455685200": 1, 	"1455598800": 1, 	"1455512400": 1, 	"1455339600": 1, 	"1455253200": 1, 	"1455166800": 1, 	"1455080400": 1, 	"1449032400": 1, 	"1441944000": 1, 	"1441598400": 1, 	"1441166400": 1, 	"1416978000": 1, 	"1377662400": 1 }

    The key to getting the counts right was advancing the counter by 1, comparing the date of the current post to the previous post, outputting the date and the count if they did not match, and if they did match advancing the counter by 1 and comparing again. After the date and count are output, the counter is reset to 0.

    You can see the heatmap calendar on my TIL page.


    Thanks to Eric Davis for advice on using less code and pointing out how I can make the trailing comma if statement more efficient: %if forloop.last == false% , %endif%

    He said:

    If there’s one bit of “programming advice” I would give to anybody mucking around with this stuff, it’s this: Write as little code as humanly possible. The most bug-free line of code is the line not written.

    What I need to ask myself: If something is being repeated, why?

  • Photo Metadata Analysis Project

    Photo Metadata Analysis Project

    I’m working my way through some data science and visualization books right now. I found that I learn better by doing small projects than I do by copying examples in books, so I designed a little project to apply some of what I learned and to learn some new skills along the way.

    My goal was to make a project where I do everything from start to finish: Create my own data set, format it, analyze it, then visualize it. I also wanted to make it fairly common and as automated as possible so it could be repeated by others.

    Here is what I came up with: Extracting metadata from my iPhone photos, analyzing it in different ways (days, months, hours, seasons), and visualizing it. I used free tools to do the extraction, formatting, and visualization, then scripted everything with AppleScript and Python to automate it.

    Technical Details

    You can find the full repository on GitHub. If you have a recent Mac with Photos.app and TextWrangler, you can run the scripts and produce your own charts!

    • I used AppleScript to loop through photo metadata in Photos.app and write it out to a CSV files that it creates in the same directory as the scripts.
    • I used TextWrangler’s grep functionality via AppleScript to break apart the date strings into days of the week, dates, and times, and to remove bad or null location strings (lat,long). I know I could have written this in Python, but I didn’t want to reinvent the wheel. TextWrangler’s AppleScript library is very powerful and easy to use.
    • Python was my tool of choice for analyzing the CSV files in various ways and visualizing the results with one of its plotting libraries, matplotlib.
    • The map of where photos were taken in the US was generated with D3.js.
    • Once everything has been generated and saved, AppleScript opens the images in Preview and launches a simple Python webserver to show the map.

    The Results

    Photos by hour block

    AM vs PM percentage

    Months count breakdown

    Seasons count breakdown

    Seasons percentages

    Weekday count breakdown

    Weekday percentages

    Photo Map

    Lessons

    • I really beefed up my understanding of basic Python (with help from Eric Davis!)
    • I dusted off my AppleScript knowledge and gave it a workout. I learned that AppleScript has a concept of lists that you can pass into and out of programs. This was the key to launching all of the charts in a single preview window.
    • This was a great exercise in UX. How can this be both easy to use and easy to interpret?
    • This was an exercise in thinking programatically. How can this be built in a way that makes it reusable?
    • I learned how to project location information onto a map with D3.js. I’ve used D3.js for charts before, so this was a good way to expand my skills.
    • This was a good way to practice my git skills and think through how to structure a project and make executable code.
    • There is a lot more I can add to this (more mapping options, more ways to count the photos, outputting the photos in a calendar heatmap), but I feel comfortable stopping and moving on because I learned what I wanted to from it and I’m ready to start a new project. I might come back to this in the future and I might not, but either way I’m happy with this.

    Cavaets

    • This analysis is not scientific, it was for fun. Since my photos were taken with different cell phones across non-controlled time periods, I can’t use this analysis to say things like, “I’m more likely to take photos in the spring than the fall.” The truth is that there are photos in here from three springs but only two falls.
    • I can’t guarantee that my code will work for everyone. It is still a little buggy and hasn’t been tested for all scenarios. I know this and know how I would test it, but this project isn’t big enough to warrant it.
    • The color palettes I used aren’t bulletproof. If you use F.lux or Night Shift the yellows will blend in to the screen, and if you have visual impairments you might not be able to distinguish between the greens and blues.

    Try it for yourself

    You can download the repository from GitHub and run it against your Photos.app library. The requirements and instructions are in the README. Let me know if you have any issues and I’ll do my best to help you out.

  • Glacier National Park

    Glacier National Park

    Last year Amanda and I went to Montana for a week in early May. We started in Yellowstone and then went up to Glacier. It was before the official start of the season and Going to the Sun Road was still closed, but we avoided the crowds, explored the park in solitude, took in the wonderful scenery, and ran into some incredible wildlife.

    Here is the best of what I shot over three days in Glacier.

    Up the trail
    Sunset through the trees
    Glacier National Park
    Little Chief Mountain
    Bighorn Sheep at Glacier National Park
    Plants and Rocks
    Grouse
    A friend in the weeds
    Mt Grinnell and Mt Gould
    Bighorn Sheep at Glacier National Park
    Glacier Textures
    Bighorn Sheep at Glacier National Park
    Bighorn Sheep at Glacier National Park
    My Patagonia Bag
  • Identifying and killing background Python processes

    Identifying and killing background Python processes

    Today I learned:

    How to kill rogue background Python processes

    I started a simple Python http server as a background process at the end of a Python script. That was a mistake. Since it isn’t running in an open Terminal window, it is easy to forget about and it will run until you kill the process or reboot your machine. I don’t like that.

    Here is how to identify and kill background Python processes in Terminal.app:

    Run:

    ps -elf | grep python

    This will return something like this:

    501 74440 74439     4004   0  31  0  2505592   9276 -      S        0 ??         0:00.29 python -m Simple  8:43AM 501 77045 77016     4006   0  31  0  2436888    812 -      S+       0 ttys000    0:00.00 grep python       8:57AM

    The second column is the PID. Make note of which one you want to kill. Then run:

    kill -9 PID

    Example. Let’s say I want to kill the python -m SimpleHTTPServer process. I see that its PID is 74440. So I would run:

    kill -9 74440
  • Simple HTTP server with Python

    Simple HTTP server with Python

    Today I learned:

    Simple HTTP server

    Need to quickly test how something works/functions but don’t want to go through the hassle of putting it on a webserver? Python has you covered.

    $ cd /home/some/dir $ python -m SimpleHTTPServer

    Then you can access the stuff in that directory it via http://localhost:8000/ in your browser!

    Control + c stops it.

  • Regex Lookahead and Lookbehind

    Regex Lookahead and Lookbehind

    Today I learned:

    Regex Lookahead and Lookbehind

    (?=foo) Lookahead Asserts that what immediately follows the current position in the string is foo
    (?<=foo) Lookbehind Asserts that what immediately precedes the current position in the string is foo
    (?!foo) Negative Lookahead Asserts that what immediately follows the current position in the string is not foo
    (? Negative Lookbehind Asserts that what immediately precedes the current position in the string is not foo

    Example

    Expression: Hell(?=o) - Lookahead

    • Hellish fails
    • Hello passes
    • MelloHello passes

    Use

    The basic example above isn’t very helpful. But it can be very useful for password rule validation or advanced find/replaces.