Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shannon Diversity Index
#!/usr/bin/env python
# Shannon Diversity Index
import sys
def sdi(data):
""" Given a hash { 'species': count } , returns the SDI
>>> sdi({'a': 10, 'b': 20, 'c': 30,})
from math import log as ln
def p(n, N):
""" Relative abundance """
if n is 0:
return 0
return (float(n)/N) * ln(float(n)/N)
N = sum(data.values())
return -sum(p(n, N) for n in data.values() if n is not 0)
if __name__ == '__main__':
import doctest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.