Some Bits: Nelson's log

6 posts in March 2013

GitHub pull requests are too heavyweight

GitHub is a marvel. But it’s a PITA to submit pull requests. It’s too heavyweight; in particular you’re required to log in to GitHub and make a fork. Here’s what you have to do:

  1. Log in or sign up to GitHub
  2. Fork the project
  3. Check out your fork
  4. Manually “git remote add upstream”
  5. Create a branch
  6. Make your changes and commit them to the branch
  7. Push your changes back to GitHub
  8. Submit a pull request
  9. (time passes, you want to submit another patch)
  10. Pull changes from upstream to your local repo
  11. Push your merging of those changes back to GitHub
  12. Go back to step 5 for next patch.

The thing that really gets me is if I want to make a one line patch to a repo, I’m committing to creating and maintaining a fork. The necessity that I remember to pull changes from upstream and commit them if I ever revisit that project is particularly onerous. In practice I find myself deleting my forks after my pull request is done.

I’d really like to be able to do all this without GitHub, just using git’s native pull request support. Ie, let me just check out a copy of the master project (read-only), then make my changes locally and use some combination of git pull-request and git format-patch to upload my pull request to GitHub. But now that I look at it, maybe those tools aren’t robust enough. Not sure.

MacOS broken build between versions

Well this is fun; MacOS binaries built on MacOS 10.8 don’t necessarily run on 10.7. I tried copying some homebrew binaries between machines and got the useless error message “Illegal Instruction: 4”.

Turns out this is working as intended; by default clang is happy to build binaries that only work on the current OS, not older versions. Good discussion here. The fix is to build with -mmacosx-version-min=10.5 (or however far back you need to go). Homebrew doesn’t build with this flag, I imagine because their idea is you recompile code on every machine. That gets tedious with multiple macs on a slow network.

Breaking binary compatibility is a stupid thing.

 

SMASH JSON

D3 3.1 includes support for SMASH, a way to make subset builds of D3. One thing I use D3 a lot for is the XmlHttpRequest support, particularly d3.json(). Most people use JQuery for AJAX, but I like D3’s version, but in either case I don’t want a 100k library just for a wrapper for XHR. Anyway..

$ smash src/start.js src/xhr/*.js src/dsv/*.js src/end.js | uglifyjs -c -m | gzip | wc -c
2276

Just JSON support is only 1464 bytes compressed, but it’s nice to bring in the CSV/TSV parsing. It’s 12k unminimized. Nice way to build a minimal XHR library. Didn’t test that it actually works ;-P

e00 GIS format notes

Trying to work with the data files for this awesome Volcano map from USGS. They helpfully published their GIS data for free download; unhelpfully it’s in ESRI’s proprietary E00 format. Some notes.

An E00 file is a container for geography data with annotations like centroids, labels, etc. The volcano files also contain auxiliary .avl and .lyr files that seem to be graphical legend definitions, etc.

The format is mostly straightforward ASCII and somewhat documented. It’s a container format; inside it are geometries and other properties. The part I’m really interested in is the PAT section, which seems to have labels and auxilliary data for the geometries defined in the ARC section. I could probably fake out a join of my own from the ARCs to the PAT data without too much coding. To complicate things I think the geometries are line boundaries between polygons, like TopoJSON, and not the polygons themselves..

Couldn’t find any Python E00 code, but there is “version 0.05” of a Perl module.

QGIS can load an E00 file, but all it seems to do is grab the geometry as LineStrings. It loses all the metadata. It’s also super slow. Way faster loading the ARC shapefile that ogr2ogr generated. I forget, do shapefiles contain spatial indices by default?

Seth F wrote to tell me about avce00, a program that converts binary coverages to ASCII E00 and back. What’s a coverage? That’s ESRI’s name for “a bunch of spatial data with auxiliary data”, ie the bundle in the E00 file. The main thing it seems to do is covert between binary and ASCII E00; I don’t need it for the volcano data.

GDAL ogr2ogr has some E00 support. A simple ogr2ogr is doing a game job of creating multiple shapefiles with my data. It is terribly slow, 3+ hours on my 30 meg input file. But it does seem to convert the data. There’s a shapefile named ARC that contains the boundary LineStrings and a shapefile named PAL that doesn’t have any geometry in it, but contains the auxiliary data I care about from the PAT section.

Anyway, the ogr2ogr converted shapefile seems reasonable to work with. The properties for the main geometry table named ARC includes integer fields named FNODE_, TNODE_, LPOLY_, RPOLY_, BIMGEO_FN, BIMGEO___1 (copied? to UserId), and LINE-TYPE. There’s 21,563 rows in the properties table, and roughly 6745 unique LPOLY_ ids and an unknown but < 16578 unique BIMPGEO__1.

Meanwhile, the PAL table has 6744 rows with the fields I really care about: YEAR1 and YEAR2 as well as LABEL. It also has BIMPGEO_FN, which looks to be a unique ascending integer 1..6746 and a BIMPGEO__1 field, 0..6980. I’m betting one of those is a key I can join to LPOLY_ on the ARC table to associate the years and label to the geometry.

Animating CSS class changes with D3

In a previous blog post I discovered that using .style(“fill”, ...) to set colors was way, way slower than using .attr(“class”, ...) to just switch the class. Which is all well and good, but what if I want to animate the color change? Ie: not just switch from white to black but use D3 transitions to show the grey between? Here are some options.

Class hackery: create a bunch of CSS classes “grey0”, “grey1”, “grey2”, ... that are a ramp from black to white. Then set .attr(“class”, greyN) with a tweening function that uses the classes. Seems awfully inelegant and hard to get right, particularly when you consider nonlinear easing functions.

Stylesheet manipulation. You can grab the CSS stylesheet itself and modify it. I found one D3 example. I have read lots of admonishments that modifying the CSS is hacky, flaky,  and works differently in different browsers. No idea if this would be suitably fast; I can’t imagine browsers optimize this usage.

CSS transitions. Nominal support is pretty good, although I’m breaking my rule not to use vendor prefix CSS in my projects (stupid WebKit). Something as simple as rect { -webkit-transition: fill 0.4s } looks pretty good in Chrome. It does not look good in Firefox: it doesn’t seem to actually tween the color so really all you get is a 0.4s delay. No idea how it looks in Opera or IE. Oh well, I design for WebKit anyway.

CSS transitions have nice control over the easing function. However I don’t see any control over the interpolation, something that’s quite important and subtle when tweening colors. The spec says it interpolates in RGBA space which is just about the worst possible choice. Fortunately my example is tweening white and bright yellow, so I can get away with it, but a red to blue tween probably looks awful using CSS.

Optimizing zipdecode

Are there any good tools for profiling rich D3 webapps, testing the changes in Javascript, CSS, and SVG design for how much time it saves? The best I’ve come up with so far is inserting calls to Date.now() in my code and using console.log() to print numbers. It works, but it’s primitive.

Chrome’s Profile tool doesn’t help me much. The Javascript profiler does, actually, giving me detailed analysis of why something is taking so long. But that’s a forensics tool, what I need is a benchmark wrapper around what I’m doing, something like Python’s timeit. But I don’t want to measure just Javascript code; I’m more worried about the CSS selection time, the DOM manipulation time, the browser render time.

Anyway, my Zipdecode thing is too slow. I’d like to optimize it, in particular so the interaction on the user typing a zip code is faster (and ideally, animateable). My original code is intentionally naive: select all 40,000 dots and set their “fill” CSS style to a specific color. It’s bad:

    d3.select("#zipdots").selectAll("rect")
        .style("fill", function(d, i) {
            if (l > 0 && d.zip.substr(0, l) == selectedZip) {
                return selectedColor;
            } else {
                return unselectedColor;
            }
        });

This take 140ms to do the update (and 650ms for the initial render).

Short circuiting the logic test about d.zip.substr() reveals that very little time is spent doing that. Replacing the call to .style() with a call to .each() that doesn’t manipulate the style drops the time down to 6ms; all my time is spent modifying the style, not creating the selection and iterating through it.

So the problem is mutating the .style(); no big surprise, but I can measure it! Let’s see how I can change the code to be better.

The obvious thing to try is to set the CSS class instead of a style property. The drawback with classes is there’s no way to interpolate the color easily. More on this in another blog post. But it works great, dropping the update time from 140ms to 25-30ms. Feels much faster. I tried this two ways

.attr("class", function(d) { return zipSelected(d.zip) ? "selected" : "unselected" })

.classed("selected", function(d) { return zipSelected(d.zip); })
.classed("unselected", function(d) { return ! zipSelected(d.zip); })

The first way using .attr() is 25-30ms; the second way calling .classed() twice is about 40ms, probably all from the overhead of that extra iterative function. I don’t think there’s anything particularly wrong about using .attr() to set classes, and since it’s simpler and faster I’ll stick with it.

A whole different optimization is treating to be smarter about the selection, not select all 40,000 dots every time. The code is not efficient in one important way; if I change the selection from “94” to “941” only 30 dots really need to be updated, but I go ahead and modify all 40,000 dots. But I don’t much care about fixing that because the case I’m most worried about is going from “” to “9”, where a lot of dots need to be touch (at most 1/10th of all zip codes, about 4000 dots).

But apparently I’m wrong. A version of this pull request from Ziggy Jonsson drops my interaction time from 140ms to 20ms, even less if the user is selecting a very narrow range of zips like “9414”. The key improvement is he’s only modifying the dots that are affected. (In addition he’s avoiding some calls to d3.select(), but I think that’s a smaller effect). Works well, but for my tutorial purposes I don’t like how it maintains this giant cached list of  all possible zip selections.

Another way to achieve the same effect would be  to use the hierarchy of the DOM to model the hierarchy of zip codes. Ie: instead of all the dots being top level <rect> elements, have a <g id=”9″> that contains all the 9xxxx zip codes (and in turn a <g id=”94″> inside that, etc. This would allow me to select with precision just the dots that need addressing, and might make the slower .style() method acceptable. But it significantly complicates the code and I don’t feel like doing that now that I’ve got stuff running pretty well. If I do it might be worth not going for the full hierarchical thing, just break zip code dots into the 10 top level categories.