nit: This could be done easier with a `defaultdict`

```python
name_counts = defaultdict(int)
for leaf in tree.leaves():
    newname = namefunc(recon[leaf].name)
    if newname in tree.nodes:   # create unique name
        name_counts[newname] += 1
        newname = "%s_%d" % (newname, name_counts[newname])
    tree.rename(leaf.name, newname)
```