[GRASS-SVN] r67042 - in grass-addons/grass7/raster: . r.series.diversity

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Dec 9 01:22:40 PST 2015


Author: pvanbosgeo
Date: 2015-12-09 01:22:40 -0800 (Wed, 09 Dec 2015)
New Revision: 67042

Added:
   grass-addons/grass7/raster/r.series.diversity/
   grass-addons/grass7/raster/r.series.diversity/Makefile
   grass-addons/grass7/raster/r.series.diversity/r.series.diversity.html
   grass-addons/grass7/raster/r.series.diversity/r.series.diversity.py
Removed:
   grass-addons/grass7/raster/r.series.diversity/Makefile
   grass-addons/grass7/raster/r.series.diversity/r.biodiversity.html
   grass-addons/grass7/raster/r.series.diversity/r.biodiversity.py
Log:
Change name to r.series.diversity to better reflect what this addon does (and thus also better distinguish this from r.diversity)

Deleted: grass-addons/grass7/raster/r.series.diversity/Makefile
===================================================================
--- grass-addons/grass7/raster/r.biodiversity/Makefile	2015-12-08 23:54:17 UTC (rev 67040)
+++ grass-addons/grass7/raster/r.series.diversity/Makefile	2015-12-09 09:22:40 UTC (rev 67042)
@@ -1,7 +0,0 @@
-MODULE_TOPDIR = ../..
-
-PGM = r.biodiversity
-
-include $(MODULE_TOPDIR)/include/Make/Script.make
-
-default: script

Copied: grass-addons/grass7/raster/r.series.diversity/Makefile (from rev 67041, grass-addons/grass7/raster/r.biodiversity/Makefile)
===================================================================
--- grass-addons/grass7/raster/r.series.diversity/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.series.diversity/Makefile	2015-12-09 09:22:40 UTC (rev 67042)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.series.diversity
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Deleted: grass-addons/grass7/raster/r.series.diversity/r.biodiversity.html
===================================================================
--- grass-addons/grass7/raster/r.biodiversity/r.biodiversity.html	2015-12-08 23:54:17 UTC (rev 67040)
+++ grass-addons/grass7/raster/r.series.diversity/r.biodiversity.html	2015-12-09 09:22:40 UTC (rev 67042)
@@ -1,191 +0,0 @@
-<h2>DESCRIPTION</h2>
-
-<em>r.biodiversity</em> computes one or more biodiversity indices 
-based on 2 or more input layers. Each layer should represents a 
-species (or other categories being used), and its raster values the 
-category count/value. The name of the output layers will consist of 
-the base name provided by the user. Currently implemented are the 
-Renyi entropy index and a number of specialized cases of the Renyi 
-enthropy, viz.the species richness, the Shannon index, the Shannon 
-based effective number of species (ENS), the Simpson index (inverse 
-and gini variants), pielou's eveness (Legendre & Legendre, 1998). 
-
-<h4>The Renyi enthropy</h4>
-
-This index quantify the diversity, uncertainty, or randomness of a 
-system. The user can define the order of diversity by setting the 
-order (<i>alpha</i>) value. The order of a diversity indicates its 
-sensitivity to common and rare species. The diversity of order zero 
-( <i>alpha = 0</i>)  is completely insensitive to species 
-frequencies and is better known as species richness. Increasing the 
-order diminishes the relative weights of rare species in the 
-resulting index (Jost 2006, Legendre & Legendre 1998). The name of 
-the output layer is composed of the basename + renyi + alpha.
-
-<h4>Species richness</h4>
-
-The species richness is simply the count of the number of layers. 
-It is a special case of the Reny enthropy: <i>S = exp(R0)</i>, 
-whereby <i>S</i> is the species richness <i>R0</i> the renyi index 
-for <i>alpha=0</i>. The name of the output layer is composed of the 
-basename + richness.
-
-<h4>Shannon index</h4>
-
-The Shannon (also called the Shannon-Weaver or Shannon-Wiener) 
-index is defined as <i>H' = -sum(p_i x log(p_i))</i>, where <i>p_i
-</i> is the proportional abundance of species <i>i</i>. The function 
-uses the natural logarithm (one can also use other bases for the 
-log, but that is currently not implemented, and doesn't make a real 
-difference). Note the Shannon index is a special case of the Renyi 
-enthropy for <i>alpha = 2</i>. The name of the output layer is 
-composed of the basename + shannon.
-
-<h4>Effective number of species (ENS)</h4>
-
-This option gives the Shannon index, converted to into equivalent 
-or effective numbers of species (also known as Hill numbers) (Lou 
-Jost, 2006). The Shannon index, and other indice, can be converted 
-so they represent the number of equally abundant species necessary 
-to produce the observed value of diversity (an analogue the concept 
-of effective population size in genetics). An advantage of the ENS 
-is a more intuitive behavious, e.g., if two communities with equally 
-abundant but totally distinct species are combined, the ENS of the 
-combined community is twice that of the original communities. See 
-for an explanation and examples this
-<a href="http://www.loujost.com/Statistics%20and%20Physics/Diversity%20and%20Similarity/EffectiveNumberOfSpecies.htm">blog post</a>
-or <a href="http://jonlefcheck.net/2012/10/23/diversity-as-effective-numbers">this one</a>.
-The name of the output layer is composed of the 
-basename + ens.
-
-<h4>Pielou's eveness (equitability) index</h4>
-
-Species evenness refers to how close in numbers each species in 
-an environment are. The evenness of a community can be represented 
-by Pielou's evenness index, which is defined as <i>H' / Hmax</i>. H' 
-is the Shannon diversity index and Hmax the maximum value of H', 
-equal to log(species richness). Note that a weakness of this index 
-is its dependence on species counts, and more specifically that it 
-is a ratio of a relatively stable index, H', and one that is 
-strongly dependent on sample size, S. The name of the output layer 
-is composed of the basename + pielou.
-
-<h4>Inverse Simpson index (Simpson's Reciprocal Index)</h4>
-
-The Simpson's index is defined as <i>D = sum p_i^2</i>. This is 
-equivalent to <i>-1 * 1 / exp(R2)</i>, with <i>R2</i> the renyi 
-index for <i>alpha=2</i>. With this index, 0 represents infinite 
-diversity and 1, no diversity. This is counterintuitive behavior for 
-a diversity index. An alternative is the inverse Simpson index is 
-defined as <i>ID = 1 / D)</i>. The index represents the probability 
-that two individuals randomly selected from a sample will belong to 
-different species. The value ranges between 0 and 1, with greater 
-values representing greater sample diversity. The name of the output 
-layer is composed of the basename + invsimpson.
-
-<h4>Gini-Simpson index</h4>
-
-An alternative way to overcome the problem of the 
-counter-intuitive nature of Simpson's Index is to use <i>1 - D)</i>. 
-The lowest value of this index is 1 and represent a community 
-containing only one species. The higher the value, the greater the 
-diversity. The maximum value is the number of species in the sample. 
-The name of the output layer is composed of the basename + 
-ginisimpson.
-
-<h2>NOTES</h2>
-
-Note that if you are interested in the landscape diversity, you 
-should have a look at the
-<a href="https://grass.osgeo.org/grass70/manuals/addons/r.diversity.html">r.diversity</a>
-addon or the various related r.li.* addons (see 
-below). These functions requires one input layer and compute the 
-diversity using a moving window.
-
-<h2>EXAMPLES</h2>
-
-Suppose we have five layers, each representing number of 
-individuals of a different species. To keep it simple, let's assume 
-individuals of all five species are homogeneous distributed, with 
-respectively 60, 10, 25, 1 and 4 individuals / raster cell densities.
-
-<div class="code"><pre>
-r.mapcalc "spec1 = 60"
-r.mapcalc "spec2 = 10"
-r.mapcalc "spec3 = 25"
-r.mapcalc "spec4 = 1"
-r.mapcalc "spec5 = 4"
-</pre></div>
-
-<p>Now we can calculate the renyi index for alpha is 0, 1 and 2 
-(this should be 1.61, 1.06 and 0.83 respectively)
-
-<div class="code"><pre>
-r.biodiversity -r in=spec1,spec2,spec3,spec4,spec5 out=renyi alpha=0,1,2
-
-r.info -r map=renyi_Renyi_0_0
-min=1.6094379124341
-max=1.6094379124341
-
-r.info -r map=renyi_Renyi_1_0
-min=1.05813420869358
-max=1.05813420869358
-
-r.info -r map=renyi_Renyi_2_0
-min=0.834250021537946
-max=0.834250021537946
-</pre></div>
-
-<p>You can also compute the species richness, shannon, inverse 
-simpson and gini-simpson indices
-
-<div class="code"><pre>
-r.biodiversity -s -h -p -g in=spec1,spec2,spec3,spec4,spec5 out=biodiversity
-</pre></div>
-
-<p>The species richness you get should of course be 5. The shannon 
-index is the same as the renyi index with alpha=1 (1.06). The 
-inverse simpson and gini-simpson should be 2.3 and 0.57 
-respectively. Let's check:
-
-<div class="code"><pre>
-r.info -r map=biodiversity_richness
-min=5
-max=5
-
-r.info -r map=biodiversity_shannon
-min=1.05813420869358
-max=1.05813420869358
-
-r.info -r map=biodiversity_invsimpson
-min=2.30308613542147
-max=2.30308613542147
-
-r.info -r map=biodiversity_ginisimpson
-min=0.5658
-max=0.5658
-</pre></div>
-
-
-
-<h2>SEE ALSO</h2>
-
-<em>
-<a href="r.li.html">r.li</a>,
-<a href="r.li.pielou.html">r.li.pielou</a>,
-<a href="r.li.renyi.html">r.li.renyi</a>,
-<a href="r.li.shannon.html">r.li.shannon</a>,
-<a href="r.li.simpson.html">r.li.simpson</a>
-</em>
-
-<h2>REFERENCES</h2>
-<ul>
-<li>Jost L. 2006. Entropy and diversity. Oikos 113:363-75</li>
-<li>Legendre P, Legendre L. 1998. Numerical Ecology. Second English edition. Elsevier, Amsterdam</li>
-</ul>
-
-
-<h2>AUTHOR</h2>
-Paulo van Breugel, paulo at ecodiv.org
-
-<p><i>Last changed: $Date$</i>

Deleted: grass-addons/grass7/raster/r.series.diversity/r.biodiversity.py
===================================================================
--- grass-addons/grass7/raster/r.biodiversity/r.biodiversity.py	2015-12-08 23:54:17 UTC (rev 67040)
+++ grass-addons/grass7/raster/r.series.diversity/r.biodiversity.py	2015-12-09 09:22:40 UTC (rev 67042)
@@ -1,358 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-########################################################################
-#
-# MODULE:       r.biodiversity
-# AUTHOR(S):    Paulo van Breugel <p.vanbreugel AT gmail.com>
-# PURPOSE:      Compute biodiversity indici over input layers
-#
-# COPYRIGHT: (C) 2014 Paulo van Breugel
-#            http://ecodiv.org
-#            http://pvanb.wordpress.com/
-#
-#            This program is free software under the GNU General Public
-#            License (>=v2). Read the file COPYING that comes with GRASS
-#            for details.
-#
-########################################################################
-#
-#%Module
-#% description: Compute biodiversity indici over input layers
-#% keyword: raster
-#% keyword: diversity index
-#% keyword: renyi entrophy
-#% keyword: shannon
-#% keyword: simpson
-#% keyword: richness
-#% keyword: biodiversity
-#% keyword: eveness
-#%End
-
-#%option
-#% key: input
-#% type: string
-#% gisprompt: old,cell,raster
-#% description: input layers
-#% label: input layers
-#% key_desc: name
-#% required: yes
-#% multiple: yes
-#%end
-
-#%option
-#% key: output
-#% type: string
-#% gisprompt: new,cell,raster
-#% description: prefix name output layer
-#% key_desc: name
-#% required: yes
-#% multiple: no
-#%end
-
-#%flag
-#% key: r
-#% description: Renyi enthropy index
-#% guisection: Indices
-#%end
-
-#%option
-#% key: alpha
-#% type: double
-#% description: Order of generalized entropy
-#% key_desc: number(s)
-#% multiple: yes
-#% options: 0.0-*
-#% guisection: Indices
-#%end
-
-#%rules
-#% collective: -r,alpha
-#%end
-
-#%flag
-#% key: s
-#% description: Richness index
-#% guisection: Indices
-#%end
-
-#%flag
-#% key: h
-#% description: Shannon index
-#% guisection: Indices
-#%end
-
-#%flag
-#% key: p
-#% description: Reversed Simpson index
-#% guisection: Indices
-#%end
-
-#%flag
-#% key: g
-#% description: Gini-Simpson index
-#% guisection: Indices
-#%end
-
-#%flag
-#% key: e
-#% description: Pielou's evenness index
-#% guisection: Indices
-#%end
-
-#%flag
-#% key: n
-#% description: Shannon effective number of species
-#% guisection: Indices
-#%end
-
-#%rules
-#% required: -r,-s,-h,-e,-p,-g,-n
-#%end
-
-#----------------------------------------------------------------------------
-# Standard
-#----------------------------------------------------------------------------
-
-# import libraries
-import os
-import sys
-import uuid
-import atexit
-import string
-import grass.script as grass
-if not os.environ.has_key("GISBASE"):
-    grass.message( "You must be in GRASS GIS to run this program." )
-    sys.exit(1)
-
-#----------------------------------------------------------------------------
-# Functions
-#----------------------------------------------------------------------------
-
-# create set to store names of temporary maps to be deleted upon exit
-clean_rast = set()
-def cleanup():
-    for rast in clean_rast:
-        grass.run_command("g.remove",
-        type="rast", name = rast, quiet = True)
-
-def CheckLayer(envlay):
-    for chl in xrange(len(envlay)):
-        ffile = grass.find_file(envlay[chl], element = 'cell')
-        if ffile['fullname'] == '':
-            grass.fatal("The layer " + envlay[chl] + " does not exist.")
-
-# Create temporary name
-def tmpname(name):
-    tmpf = name + "_" + str(uuid.uuid4())
-    tmpf = string.replace(tmpf, '-', '_')
-    clean_rast.add(tmpf)
-    return tmpf
-
-#----------------------------------------------------------------------------
-# main function
-#----------------------------------------------------------------------------
-
-def main():
-    #options = {"input":"spec1,spec2,spec3,spec4,spec5", "output":"AAA9", "alpha":"4"}
-    #flags = {"r":"False", "s":True, "h":True, "e":True, "p":True, "n":True, "g":True}
-
-    #--------------------------------------------------------------------------
-    # Variables
-    #--------------------------------------------------------------------------
-
-    # Layers
-    OUT = options['output']
-    IN = options['input']
-    IN = IN.split(',')
-    CheckLayer(IN)
-
-    # Diversity indici
-    flag_r = flags['r']
-    flag_s = flags['s']
-    flag_h = flags['h']
-    flag_e = flags['e']
-    flag_p = flags['p']
-    flag_g = flags['g']
-    flag_n = flags['n']
-    if options['alpha']:
-        Q = map(float, options['alpha'].split(','))
-    else:
-        Q = map(float, [])
-    Qoriginal = list(Q)
-
-    #--------------------------------------------------------------------------
-    # Create list of what need to be computed
-    #--------------------------------------------------------------------------
-    if not flag_r:
-        flag_r = []
-    if flag_s and not 0.0 in Q:
-        Q.append(0.0)
-    if flag_h and not 1.0 in Q:
-        Q.append(1.0)
-    if flag_e and not 0.0 in Q:
-        Q.append(0.0)
-    if flag_e and not 1.0 in Q:
-        Q.append(1.0)
-    if flag_p and not 2.0 in Q:
-        Q.append(2.0)
-    if flag_g and not 2.0 in Q:
-        Q.append(2.0)
-    if flag_n and not 1.0 in Q:
-        Q.append(1.0)
-
-    #--------------------------------------------------------------------------
-    # Renyi entropy
-    #--------------------------------------------------------------------------
-    tmpt = tmpname("sht")
-    clean_rast.add(tmpt)
-    grass.run_command("r.series", quiet=True, output=tmpt,
-                      input=IN, method="sum")
-
-    for n in xrange(len(Q)):
-
-        Qn = str(Q[n])
-        Qn = Qn.replace('.', '_')
-        out_renyi = OUT + "_Renyi_" + Qn
-        tmpl = []
-
-        if Q[n] == 1:
-            # If alpha = 1
-            for i in xrange(len(IN)):
-                tmpi = tmpname('shi' + str(i) + "_")
-                tmpl.append(tmpi)
-                clean_rast.add(tmpi)
-                grass.mapcalc("$tmpi = ($inl/$tmpt) * log(($inl/$tmpt))",
-                              tmpi=tmpi,
-                              inl=IN[i],
-                              tmpt=tmpt,
-                              quiet=True)
-            tmpta = tmpname("sht")
-            clean_rast.add(tmpta)
-            grass.run_command("r.series", output=tmpta, input=tmpl,
-                                  method="sum", quiet=True)
-            grass.mapcalc("$outl = -1 * $tmpta",
-                              outl=out_renyi,
-                              tmpta=tmpta,
-                              overwrite=True,
-                              quiet=True)
-            grass.run_command("g.remove", type="raster",
-                              name=tmpta, flags="f")
-        else:
-            # If alpha != 1
-            for i in xrange(len(IN)):
-                tmpi = tmpname('reni')
-                tmpl.append(tmpi)
-                grass.mapcalc("$tmpi = if($inl==0,0,pow($inl/$tmpt,$alpha))",
-                              tmpi=tmpi,
-                              tmpt=tmpt,
-                              inl=IN[i],
-                              alpha=Q[n],
-                              quiet=True)
-            tmpta = tmpname("sht")
-            clean_rast.add(tmpta)
-            grass.run_command("r.series", output=tmpta, input=tmpl,
-                              method="sum", quiet=True)
-            grass.mapcalc("$outl = (1/(1-$alpha)) * log($tmpta)",
-                              outl=out_renyi,
-                              tmpta=tmpta,
-                              alpha=Q[n],
-                              quiet=True)
-            grass.run_command("g.remove", type="raster",
-                              name=tmpta, flags="f", quiet=True)
-        grass.run_command("g.remove", type="raster",
-                          name=tmpl, flags="f", quiet=True)
-
-    #--------------------------------------------------------------------------
-    # Species richness
-    #--------------------------------------------------------------------------
-    if flag_s:
-        out_div = OUT + "_richness"
-        in_div = OUT + "_Renyi_0_0"
-        grass.mapcalc("$out_div = exp($in_div)",
-                      out_div=out_div,
-                      in_div=in_div,
-                      quiet=True)
-        if 0.0 not in Qoriginal and not flag_e:
-            grass.run_command("g.remove", flags="f", type="raster",
-                              name=in_div, quiet=True)
-
-    #--------------------------------------------------------------------------
-    # Shannon index
-    #--------------------------------------------------------------------------
-    if flag_h:
-        out_div = OUT + "_shannon"
-        in_div = OUT + "_Renyi_1_0"
-        if 1.0 in Qoriginal or flag_e or flag_n:
-            grass.run_command("g.copy", raster=(in_div,out_div), quiet=True)
-        else:
-            grass.run_command("g.rename", raster=(in_div,out_div), quiet=True)
-
-    #--------------------------------------------------------------------------
-    # Shannon Effective Number of Species (ENS)
-    #--------------------------------------------------------------------------
-    if flag_n:
-        out_div = OUT + "_ens"
-        in_div = OUT + "_Renyi_1_0"
-        grass.mapcalc("$out_div = exp($in_div)",
-                      out_div=out_div,
-                      in_div=in_div,
-                      quiet=True)
-        if 1.0 not in Qoriginal and not flag_e:
-            grass.run_command("g.remove", flags="f", type="raster",
-                              name=in_div, quiet=True)
-
-    #--------------------------------------------------------------------------
-    # Eveness
-    #--------------------------------------------------------------------------
-    if flag_e:
-        out_div = OUT + "_eveness"
-        in_div1 = OUT + "_Renyi_0_0"
-        in_div2 = OUT + "_Renyi_1_0"
-        grass.mapcalc("$out_div = $in_div2 / $in_div1",
-                      out_div=out_div,
-                      in_div1=in_div1,
-                      in_div2=in_div2,
-                      quiet=True)
-        if 0.0 not in Qoriginal:
-            grass.run_command("g.remove", flags="f", type="raster",
-                              name=in_div1, quiet=True)
-        if 1.0 not in Qoriginal:
-            grass.run_command("g.remove", flags="f", type="raster",
-                              name=in_div2, quiet=True)
-    #--------------------------------------------------------------------------
-    # Inversed Simpson index
-    #--------------------------------------------------------------------------
-    if flag_p:
-        out_div = OUT + "_invsimpson"
-        in_div = OUT + "_Renyi_2_0"
-        grass.mapcalc("$out_div = exp($in_div)",
-                      out_div=out_div,
-                      in_div=in_div,
-                      quiet=True)
-        if 2.0 not in Qoriginal and not flag_g:
-            grass.run_command("g.remove", flags="f", type="raster",
-                              name=in_div, quiet=True)
-
-    #--------------------------------------------------------------------------
-    # Gini Simpson index
-    #--------------------------------------------------------------------------
-    if flag_g:
-        out_div = OUT + "_ginisimpson"
-        in_div = OUT + "_Renyi_2_0"
-        grass.mapcalc("$out_div = 1.0 - (1.0 / exp($in_div))",
-                      out_div=out_div,
-                      in_div=in_div,
-                      quiet=True)
-        if 2.0 not in Qoriginal:
-            grass.run_command("g.remove", flags="f", type="raster",
-                              name=in_div, quiet=True)
-
-    # Clean up temporary files
-    grass.run_command("g.remove", type="raster", name=tmpt,
-                      flags="f", quiet=True)
-
-if __name__ == "__main__":
-    options, flags = grass.parser()
-    atexit.register(cleanup)
-    sys.exit(main())

Copied: grass-addons/grass7/raster/r.series.diversity/r.series.diversity.html (from rev 67041, grass-addons/grass7/raster/r.biodiversity/r.series.diversity.html)
===================================================================
--- grass-addons/grass7/raster/r.series.diversity/r.series.diversity.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.series.diversity/r.series.diversity.html	2015-12-09 09:22:40 UTC (rev 67042)
@@ -0,0 +1,191 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.biodiversity</em> computes one or more biodiversity indices 
+based on 2 or more input layers. Each layer should represents a 
+species (or other categories being used), and its raster values the 
+category count/value. The name of the output layers will consist of 
+the base name provided by the user. Currently implemented are the 
+Renyi entropy index and a number of specialized cases of the Renyi 
+enthropy, viz.the species richness, the Shannon index, the Shannon 
+based effective number of species (ENS), the Simpson index (inverse 
+and gini variants), pielou's eveness (Legendre & Legendre, 1998). 
+
+<h4>The Renyi enthropy</h4>
+
+This index quantify the diversity, uncertainty, or randomness of a 
+system. The user can define the order of diversity by setting the 
+order (<i>alpha</i>) value. The order of a diversity indicates its 
+sensitivity to common and rare species. The diversity of order zero 
+( <i>alpha = 0</i>)  is completely insensitive to species 
+frequencies and is better known as species richness. Increasing the 
+order diminishes the relative weights of rare species in the 
+resulting index (Jost 2006, Legendre & Legendre 1998). The name of 
+the output layer is composed of the basename + renyi + alpha.
+
+<h4>Species richness</h4>
+
+The species richness is simply the count of the number of layers. 
+It is a special case of the Reny enthropy: <i>S = exp(R0)</i>, 
+whereby <i>S</i> is the species richness <i>R0</i> the renyi index 
+for <i>alpha=0</i>. The name of the output layer is composed of the 
+basename + richness.
+
+<h4>Shannon index</h4>
+
+The Shannon (also called the Shannon-Weaver or Shannon-Wiener) 
+index is defined as <i>H' = -sum(p_i x log(p_i))</i>, where <i>p_i
+</i> is the proportional abundance of species <i>i</i>. The function 
+uses the natural logarithm (one can also use other bases for the 
+log, but that is currently not implemented, and doesn't make a real 
+difference). Note the Shannon index is a special case of the Renyi 
+enthropy for <i>alpha = 2</i>. The name of the output layer is 
+composed of the basename + shannon.
+
+<h4>Effective number of species (ENS)</h4>
+
+This option gives the Shannon index, converted to into equivalent 
+or effective numbers of species (also known as Hill numbers) (Lou 
+Jost, 2006). The Shannon index, and other indice, can be converted 
+so they represent the number of equally abundant species necessary 
+to produce the observed value of diversity (an analogue the concept 
+of effective population size in genetics). An advantage of the ENS 
+is a more intuitive behavious, e.g., if two communities with equally 
+abundant but totally distinct species are combined, the ENS of the 
+combined community is twice that of the original communities. See 
+for an explanation and examples this
+<a href="http://www.loujost.com/Statistics%20and%20Physics/Diversity%20and%20Similarity/EffectiveNumberOfSpecies.htm">blog post</a>
+or <a href="http://jonlefcheck.net/2012/10/23/diversity-as-effective-numbers">this one</a>.
+The name of the output layer is composed of the 
+basename + ens.
+
+<h4>Pielou's eveness (equitability) index</h4>
+
+Species evenness refers to how close in numbers each species in 
+an environment are. The evenness of a community can be represented 
+by Pielou's evenness index, which is defined as <i>H' / Hmax</i>. H' 
+is the Shannon diversity index and Hmax the maximum value of H', 
+equal to log(species richness). Note that a weakness of this index 
+is its dependence on species counts, and more specifically that it 
+is a ratio of a relatively stable index, H', and one that is 
+strongly dependent on sample size, S. The name of the output layer 
+is composed of the basename + pielou.
+
+<h4>Inverse Simpson index (Simpson's Reciprocal Index)</h4>
+
+The Simpson's index is defined as <i>D = sum p_i^2</i>. This is 
+equivalent to <i>-1 * 1 / exp(R2)</i>, with <i>R2</i> the renyi 
+index for <i>alpha=2</i>. With this index, 0 represents infinite 
+diversity and 1, no diversity. This is counterintuitive behavior for 
+a diversity index. An alternative is the inverse Simpson index is 
+defined as <i>ID = 1 / D)</i>. The index represents the probability 
+that two individuals randomly selected from a sample will belong to 
+different species. The value ranges between 0 and 1, with greater 
+values representing greater sample diversity. The name of the output 
+layer is composed of the basename + invsimpson.
+
+<h4>Gini-Simpson index</h4>
+
+An alternative way to overcome the problem of the 
+counter-intuitive nature of Simpson's Index is to use <i>1 - D)</i>. 
+The lowest value of this index is 1 and represent a community 
+containing only one species. The higher the value, the greater the 
+diversity. The maximum value is the number of species in the sample. 
+The name of the output layer is composed of the basename + 
+ginisimpson.
+
+<h2>NOTES</h2>
+
+Note that if you are interested in the landscape diversity, you 
+should have a look at the
+<a href="https://grass.osgeo.org/grass70/manuals/addons/r.diversity.html">r.diversity</a>
+addon or the various related r.li.* addons (see 
+below). These functions requires one input layer and compute the 
+diversity using a moving window.
+
+<h2>EXAMPLES</h2>
+
+Suppose we have five layers, each representing number of 
+individuals of a different species. To keep it simple, let's assume 
+individuals of all five species are homogeneous distributed, with 
+respectively 60, 10, 25, 1 and 4 individuals / raster cell densities.
+
+<div class="code"><pre>
+r.mapcalc "spec1 = 60"
+r.mapcalc "spec2 = 10"
+r.mapcalc "spec3 = 25"
+r.mapcalc "spec4 = 1"
+r.mapcalc "spec5 = 4"
+</pre></div>
+
+<p>Now we can calculate the renyi index for alpha is 0, 1 and 2 
+(this should be 1.61, 1.06 and 0.83 respectively)
+
+<div class="code"><pre>
+r.biodiversity -r in=spec1,spec2,spec3,spec4,spec5 out=renyi alpha=0,1,2
+
+r.info -r map=renyi_Renyi_0_0
+min=1.6094379124341
+max=1.6094379124341
+
+r.info -r map=renyi_Renyi_1_0
+min=1.05813420869358
+max=1.05813420869358
+
+r.info -r map=renyi_Renyi_2_0
+min=0.834250021537946
+max=0.834250021537946
+</pre></div>
+
+<p>You can also compute the species richness, shannon, inverse 
+simpson and gini-simpson indices
+
+<div class="code"><pre>
+r.biodiversity -s -h -p -g in=spec1,spec2,spec3,spec4,spec5 out=biodiversity
+</pre></div>
+
+<p>The species richness you get should of course be 5. The shannon 
+index is the same as the renyi index with alpha=1 (1.06). The 
+inverse simpson and gini-simpson should be 2.3 and 0.57 
+respectively. Let's check:
+
+<div class="code"><pre>
+r.info -r map=biodiversity_richness
+min=5
+max=5
+
+r.info -r map=biodiversity_shannon
+min=1.05813420869358
+max=1.05813420869358
+
+r.info -r map=biodiversity_invsimpson
+min=2.30308613542147
+max=2.30308613542147
+
+r.info -r map=biodiversity_ginisimpson
+min=0.5658
+max=0.5658
+</pre></div>
+
+
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.li.html">r.li</a>,
+<a href="r.li.pielou.html">r.li.pielou</a>,
+<a href="r.li.renyi.html">r.li.renyi</a>,
+<a href="r.li.shannon.html">r.li.shannon</a>,
+<a href="r.li.simpson.html">r.li.simpson</a>
+</em>
+
+<h2>REFERENCES</h2>
+<ul>
+<li>Jost L. 2006. Entropy and diversity. Oikos 113:363-75</li>
+<li>Legendre P, Legendre L. 1998. Numerical Ecology. Second English edition. Elsevier, Amsterdam</li>
+</ul>
+
+
+<h2>AUTHOR</h2>
+Paulo van Breugel, paulo at ecodiv.org
+
+<p><i>Last changed: $Date$</i>

Copied: grass-addons/grass7/raster/r.series.diversity/r.series.diversity.py (from rev 67041, grass-addons/grass7/raster/r.biodiversity/r.series.diversity.py)
===================================================================
--- grass-addons/grass7/raster/r.series.diversity/r.series.diversity.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.series.diversity/r.series.diversity.py	2015-12-09 09:22:40 UTC (rev 67042)
@@ -0,0 +1,358 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+########################################################################
+#
+# MODULE:       r.biodiversity
+# AUTHOR(S):    Paulo van Breugel <p.vanbreugel AT gmail.com>
+# PURPOSE:      Compute biodiversity indici over input layers
+#
+# COPYRIGHT: (C) 2014 Paulo van Breugel
+#            http://ecodiv.org
+#            http://pvanb.wordpress.com/
+#
+#            This program is free software under the GNU General Public
+#            License (>=v2). Read the file COPYING that comes with GRASS
+#            for details.
+#
+########################################################################
+#
+#%Module
+#% description: Compute biodiversity indici over input layers
+#% keyword: raster
+#% keyword: diversity index
+#% keyword: renyi entrophy
+#% keyword: shannon
+#% keyword: simpson
+#% keyword: richness
+#% keyword: biodiversity
+#% keyword: eveness
+#%End
+
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: input layers
+#% label: input layers
+#% key_desc: name
+#% required: yes
+#% multiple: yes
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,cell,raster
+#% description: prefix name output layer
+#% key_desc: name
+#% required: yes
+#% multiple: no
+#%end
+
+#%flag
+#% key: r
+#% description: Renyi enthropy index
+#% guisection: Indices
+#%end
+
+#%option
+#% key: alpha
+#% type: double
+#% description: Order of generalized entropy
+#% key_desc: number(s)
+#% multiple: yes
+#% options: 0.0-*
+#% guisection: Indices
+#%end
+
+#%rules
+#% collective: -r,alpha
+#%end
+
+#%flag
+#% key: s
+#% description: Richness index
+#% guisection: Indices
+#%end
+
+#%flag
+#% key: h
+#% description: Shannon index
+#% guisection: Indices
+#%end
+
+#%flag
+#% key: p
+#% description: Reversed Simpson index
+#% guisection: Indices
+#%end
+
+#%flag
+#% key: g
+#% description: Gini-Simpson index
+#% guisection: Indices
+#%end
+
+#%flag
+#% key: e
+#% description: Pielou's evenness index
+#% guisection: Indices
+#%end
+
+#%flag
+#% key: n
+#% description: Shannon effective number of species
+#% guisection: Indices
+#%end
+
+#%rules
+#% required: -r,-s,-h,-e,-p,-g,-n
+#%end
+
+#----------------------------------------------------------------------------
+# Standard
+#----------------------------------------------------------------------------
+
+# import libraries
+import os
+import sys
+import uuid
+import atexit
+import string
+import grass.script as grass
+if not os.environ.has_key("GISBASE"):
+    grass.message( "You must be in GRASS GIS to run this program." )
+    sys.exit(1)
+
+#----------------------------------------------------------------------------
+# Functions
+#----------------------------------------------------------------------------
+
+# create set to store names of temporary maps to be deleted upon exit
+clean_rast = set()
+def cleanup():
+    for rast in clean_rast:
+        grass.run_command("g.remove",
+        type="rast", name = rast, quiet = True)
+
+def CheckLayer(envlay):
+    for chl in xrange(len(envlay)):
+        ffile = grass.find_file(envlay[chl], element = 'cell')
+        if ffile['fullname'] == '':
+            grass.fatal("The layer " + envlay[chl] + " does not exist.")
+
+# Create temporary name
+def tmpname(name):
+    tmpf = name + "_" + str(uuid.uuid4())
+    tmpf = string.replace(tmpf, '-', '_')
+    clean_rast.add(tmpf)
+    return tmpf
+
+#----------------------------------------------------------------------------
+# main function
+#----------------------------------------------------------------------------
+
+def main():
+    #options = {"input":"spec1,spec2,spec3,spec4,spec5", "output":"AAA9", "alpha":"4"}
+    #flags = {"r":"False", "s":True, "h":True, "e":True, "p":True, "n":True, "g":True}
+
+    #--------------------------------------------------------------------------
+    # Variables
+    #--------------------------------------------------------------------------
+
+    # Layers
+    OUT = options['output']
+    IN = options['input']
+    IN = IN.split(',')
+    CheckLayer(IN)
+
+    # Diversity indici
+    flag_r = flags['r']
+    flag_s = flags['s']
+    flag_h = flags['h']
+    flag_e = flags['e']
+    flag_p = flags['p']
+    flag_g = flags['g']
+    flag_n = flags['n']
+    if options['alpha']:
+        Q = map(float, options['alpha'].split(','))
+    else:
+        Q = map(float, [])
+    Qoriginal = list(Q)
+
+    #--------------------------------------------------------------------------
+    # Create list of what need to be computed
+    #--------------------------------------------------------------------------
+    if not flag_r:
+        flag_r = []
+    if flag_s and not 0.0 in Q:
+        Q.append(0.0)
+    if flag_h and not 1.0 in Q:
+        Q.append(1.0)
+    if flag_e and not 0.0 in Q:
+        Q.append(0.0)
+    if flag_e and not 1.0 in Q:
+        Q.append(1.0)
+    if flag_p and not 2.0 in Q:
+        Q.append(2.0)
+    if flag_g and not 2.0 in Q:
+        Q.append(2.0)
+    if flag_n and not 1.0 in Q:
+        Q.append(1.0)
+
+    #--------------------------------------------------------------------------
+    # Renyi entropy
+    #--------------------------------------------------------------------------
+    tmpt = tmpname("sht")
+    clean_rast.add(tmpt)
+    grass.run_command("r.series", quiet=True, output=tmpt,
+                      input=IN, method="sum")
+
+    for n in xrange(len(Q)):
+
+        Qn = str(Q[n])
+        Qn = Qn.replace('.', '_')
+        out_renyi = OUT + "_Renyi_" + Qn
+        tmpl = []
+
+        if Q[n] == 1:
+            # If alpha = 1
+            for i in xrange(len(IN)):
+                tmpi = tmpname('shi' + str(i) + "_")
+                tmpl.append(tmpi)
+                clean_rast.add(tmpi)
+                grass.mapcalc("$tmpi = ($inl/$tmpt) * log(($inl/$tmpt))",
+                              tmpi=tmpi,
+                              inl=IN[i],
+                              tmpt=tmpt,
+                              quiet=True)
+            tmpta = tmpname("sht")
+            clean_rast.add(tmpta)
+            grass.run_command("r.series", output=tmpta, input=tmpl,
+                                  method="sum", quiet=True)
+            grass.mapcalc("$outl = -1 * $tmpta",
+                              outl=out_renyi,
+                              tmpta=tmpta,
+                              overwrite=True,
+                              quiet=True)
+            grass.run_command("g.remove", type="raster",
+                              name=tmpta, flags="f")
+        else:
+            # If alpha != 1
+            for i in xrange(len(IN)):
+                tmpi = tmpname('reni')
+                tmpl.append(tmpi)
+                grass.mapcalc("$tmpi = if($inl==0,0,pow($inl/$tmpt,$alpha))",
+                              tmpi=tmpi,
+                              tmpt=tmpt,
+                              inl=IN[i],
+                              alpha=Q[n],
+                              quiet=True)
+            tmpta = tmpname("sht")
+            clean_rast.add(tmpta)
+            grass.run_command("r.series", output=tmpta, input=tmpl,
+                              method="sum", quiet=True)
+            grass.mapcalc("$outl = (1/(1-$alpha)) * log($tmpta)",
+                              outl=out_renyi,
+                              tmpta=tmpta,
+                              alpha=Q[n],
+                              quiet=True)
+            grass.run_command("g.remove", type="raster",
+                              name=tmpta, flags="f", quiet=True)
+        grass.run_command("g.remove", type="raster",
+                          name=tmpl, flags="f", quiet=True)
+
+    #--------------------------------------------------------------------------
+    # Species richness
+    #--------------------------------------------------------------------------
+    if flag_s:
+        out_div = OUT + "_richness"
+        in_div = OUT + "_Renyi_0_0"
+        grass.mapcalc("$out_div = exp($in_div)",
+                      out_div=out_div,
+                      in_div=in_div,
+                      quiet=True)
+        if 0.0 not in Qoriginal and not flag_e:
+            grass.run_command("g.remove", flags="f", type="raster",
+                              name=in_div, quiet=True)
+
+    #--------------------------------------------------------------------------
+    # Shannon index
+    #--------------------------------------------------------------------------
+    if flag_h:
+        out_div = OUT + "_shannon"
+        in_div = OUT + "_Renyi_1_0"
+        if 1.0 in Qoriginal or flag_e or flag_n:
+            grass.run_command("g.copy", raster=(in_div,out_div), quiet=True)
+        else:
+            grass.run_command("g.rename", raster=(in_div,out_div), quiet=True)
+
+    #--------------------------------------------------------------------------
+    # Shannon Effective Number of Species (ENS)
+    #--------------------------------------------------------------------------
+    if flag_n:
+        out_div = OUT + "_ens"
+        in_div = OUT + "_Renyi_1_0"
+        grass.mapcalc("$out_div = exp($in_div)",
+                      out_div=out_div,
+                      in_div=in_div,
+                      quiet=True)
+        if 1.0 not in Qoriginal and not flag_e:
+            grass.run_command("g.remove", flags="f", type="raster",
+                              name=in_div, quiet=True)
+
+    #--------------------------------------------------------------------------
+    # Eveness
+    #--------------------------------------------------------------------------
+    if flag_e:
+        out_div = OUT + "_eveness"
+        in_div1 = OUT + "_Renyi_0_0"
+        in_div2 = OUT + "_Renyi_1_0"
+        grass.mapcalc("$out_div = $in_div2 / $in_div1",
+                      out_div=out_div,
+                      in_div1=in_div1,
+                      in_div2=in_div2,
+                      quiet=True)
+        if 0.0 not in Qoriginal:
+            grass.run_command("g.remove", flags="f", type="raster",
+                              name=in_div1, quiet=True)
+        if 1.0 not in Qoriginal:
+            grass.run_command("g.remove", flags="f", type="raster",
+                              name=in_div2, quiet=True)
+    #--------------------------------------------------------------------------
+    # Inversed Simpson index
+    #--------------------------------------------------------------------------
+    if flag_p:
+        out_div = OUT + "_invsimpson"
+        in_div = OUT + "_Renyi_2_0"
+        grass.mapcalc("$out_div = exp($in_div)",
+                      out_div=out_div,
+                      in_div=in_div,
+                      quiet=True)
+        if 2.0 not in Qoriginal and not flag_g:
+            grass.run_command("g.remove", flags="f", type="raster",
+                              name=in_div, quiet=True)
+
+    #--------------------------------------------------------------------------
+    # Gini Simpson index
+    #--------------------------------------------------------------------------
+    if flag_g:
+        out_div = OUT + "_ginisimpson"
+        in_div = OUT + "_Renyi_2_0"
+        grass.mapcalc("$out_div = 1.0 - (1.0 / exp($in_div))",
+                      out_div=out_div,
+                      in_div=in_div,
+                      quiet=True)
+        if 2.0 not in Qoriginal:
+            grass.run_command("g.remove", flags="f", type="raster",
+                              name=in_div, quiet=True)
+
+    # Clean up temporary files
+    grass.run_command("g.remove", type="raster", name=tmpt,
+                      flags="f", quiet=True)
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    sys.exit(main())



More information about the grass-commit mailing list