[GRASS-SVN] r64016 - in grass-addons/grass7/raster: . r.niche.similarity
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jan 8 14:05:12 PST 2015
Author: pvanbosgeo
Date: 2015-01-08 14:05:12 -0800 (Thu, 08 Jan 2015)
New Revision: 64016
Added:
grass-addons/grass7/raster/r.niche.similarity/
grass-addons/grass7/raster/r.niche.similarity/Makefile
grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.html
grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.py
Log:
Rewrite to run in GRASS 7 (shell to python)
Added: grass-addons/grass7/raster/r.niche.similarity/Makefile
===================================================================
--- grass-addons/grass7/raster/r.niche.similarity/Makefile (rev 0)
+++ grass-addons/grass7/raster/r.niche.similarity/Makefile 2015-01-08 22:05:12 UTC (rev 64016)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.niche.similarity
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Property changes on: grass-addons/grass7/raster/r.niche.similarity/Makefile
___________________________________________________________________
Added: svn:mime-type
+ text/x-makefile
Added: svn:eol-style
+ native
Added: grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.html
===================================================================
--- grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.html (rev 0)
+++ grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.html 2015-01-08 22:05:12 UTC (rev 64016)
@@ -0,0 +1,54 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.niche.similarity</em>
+
+<p>A module to compute two metrics to quantify niche similarity or
+overlap between all pairs of input raster layers.
+
+<p>One is the niche equivalency or similarity for two species
+following Warren et al. (2008) based on Schoeners D (Schoener,
+1968). This metric ranges from 0 to 1, representing respectively no
+overlap and an identical distribution.
+
+<p>The other is the niche overlap metric which indicates the niche
+overlap from predictions of species distributions with the I
+similarity statistic of Warren et al. (2009), which is based on
+Hellinger Distances (van der Vaart, 1998). The statistic ranges from
+0 (no overlap) to 1 (the distributions are identical).
+
+<p>By default the results are written to screen, but they can also
+be written to a text file with two columns for the names of each
+pair of rasters, a third column for the type of statistic (D or I)
+and a fourth column for the D or I statistic.
+
+<h2>Notes</h2>
+
+<p> This implementation is especially suitable if you are working
+with very large data sets. Results were checked against the
+niche.overlap function in the phyloclim package for R, which also uses
+the corrected formulation of I as published in an erratum of the
+article by Warren et al (2008).
+
+<h2>REFERENCES</h2>
+
+<ul>
+<li>Warren, D. L., Glor, R. E., & Turelli, M. 2008.
+Environmental Niche Equivalency Versus Conservatism: Quantitative
+Approaches to Niche Evolution. Evolution 62(11): 2868–2883</li>
+<li> Warren, D. L., R. E. Glor, and M. Turelli. 2010. ENMTools: a
+toolbox for comparative studies of environmental niche models. Ecography
+33:607–611.</li>
+<li>Robert J. Hijmans, Steven Phillips, John
+Leathwick and Jane Elith (2013). dismo: Species distribution
+modeling. R package version 0.8-5.
+http://CRAN.R-project.org/package=dismo</li>
+<li>Christoph Heibl and Clement Calenge (2012). phyloclim: Integrating
+Phylogenetics and Climatic Niche Modeling. R package version 0.9-0.
+http://CRAN.R-project.org/package=phyloclim</li>
+</ul>
+
+<h2>AUTHOR</h2>
+
+Paulo van Breugel, paulo at ecodiv.org
+
+<p><i>Last changed: $Date$</i>
Property changes on: grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.html
___________________________________________________________________
Added: svn:mime-type
+ text/html
Added: svn:keywords
+ Author Date Id
Added: svn:eol-style
+ native
Added: grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.py
===================================================================
--- grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.py (rev 0)
+++ grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.py 2015-01-08 22:05:12 UTC (rev 64016)
@@ -0,0 +1,220 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+########################################################################
+#
+# MODULE: r.niche.similarity
+# AUTHOR(S): Paulo van Breugel <paulo AT ecodiv.org>
+# PURPOSE: Compute degree of niche overlap using the statistics D
+# and I (as proposed by Warren et al., 2008) based on
+# Schoeners D (Schoener, 1968) and Hellinger Distances
+# (van der Vaart, 1998)
+#
+# COPYRIGHT: (C) 2015 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: Computes niche overlap or similarity
+#%End
+
+#%option
+#% key: maps
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Input maps
+#% key_desc: name
+#% required: yes
+#% multiple: yes
+#% guisection: Suitability distribution maps
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new
+#% description: Name of output text file
+#% key_desc: name
+#% required: no
+#%end
+
+#%flag
+#% key: i
+#% description: Calculate I niche similarity
+#% guisection: Statistics
+#%end
+
+#%flag
+#% key: d
+#% description: Calculate D niche similarity
+#% guisection: Statistics
+#%end
+
+##----------------------------------------------------------------------------
+# Test purposes
+##----------------------------------------------------------------------------
+#options = {"maps":"bio1,bio5,bio6", "output":""}
+#flags = {"i":True, "d":True}
+
+##----------------------------------------------------------------------------
+## STANDARD ##
+##----------------------------------------------------------------------------
+
+# import libraries
+import os
+import sys
+import atexit
+import uuid
+import string
+import tempfile
+import grass.script as grass
+
+# Check if running in GRASS
+if not os.environ.has_key("GISBASE"):
+ grass.message( "You must be in GRASS GIS to run this program." )
+ sys.exit(1)
+
+# Cleanup
+clean_rast = set()
+
+def cleanup():
+ for rast in clean_rast:
+ grass.run_command("g.remove",
+ type="rast", name = rast, quiet = True)
+
+##----------------------------------------------------------------------------
+## main function
+##----------------------------------------------------------------------------
+
+def main():
+
+ ## input
+ #--------------------------------------------------------------------------
+ INMAPS = options['maps']
+ INMAPS = INMAPS.split(',')
+ VARI = [i.split('@')[0] for i in INMAPS]
+ OPF = options['output']
+ if OPF == '':
+ OPF = tempfile.mkstemp()[1]
+ flag_i = flags['i']
+ flag_d = flags['d']
+
+ ## Checks
+ #--------------------------------------------------------------------------
+
+ # Check if there are more than 1 input maps
+ NLAY = len(INMAPS)
+ if NLAY < 2:
+ grass.fatal("You need to provide 2 or more raster maps")
+
+ # Check that at least one statistic is selected
+ if flag_i == '' and flag_d == '':
+ grass.fatal("You need to select at least one statistic")
+
+ # Test if text file exists. If so, append _v1 to file name
+ #--------------------------------------------------------------------------
+ k = 0
+ OPFold = OPF[:]
+ OPFold = OPFold.split('.')
+ while os.path.isfile(OPF):
+ k = k + 1
+ opft = OPF.split('.')
+ if len(opft) == 1:
+ OPF = opft[0] + "_" + str(k)
+ else:
+ OPF = OPFold[0] + "_v" + str(k) + "." + OPFold[1]
+ if k > 0:
+ grass.info("there is already a file " + OPFold[0] + ".")
+ grass.info("Using " + OPF + " instead")
+
+ #=======================================================================
+ ## Calculate D and I and write to standard output (& optionally to file)
+ #=======================================================================
+
+ # Open text file for writing and write heading
+ text_file = open(OPF, "w")
+ text_file.write("raster1,raster2,statistic,value" + "\n")
+
+ # Write D and I values to standard output and optionally to text file
+ i=0
+ while i < NLAY:
+ nlay1 = INMAPS[i]
+ nvar1 = VARI[i]
+ vsum1 = float(grass.parse_command("r.univar", quiet=True, flags="g", map=nlay1)['sum'])
+
+ j = i + 1
+ while j < NLAY:
+ nlay2 = INMAPS[j]
+ nvar2 = VARI[j]
+ vsum2 = float(grass.parse_command("r.univar", quiet=True, flags="g", map=nlay2)['sum'])
+
+ ## Calculate D (Schoener's 1968)
+ #=======================================================================
+
+ if flag_d:
+ tmpf0 = "rniche_" + str(uuid.uuid4())
+ tmpf0 = string.replace(tmpf0, '-', '_')
+ clean_rast.add(tmpf0)
+ grass.mapcalc("$tmpf0 = abs(double($nlay1)/$vsum1 - double($nlay2)/$vsum2)",
+ tmpf0 = tmpf0,
+ nlay1 = nlay1,
+ vsum1 = vsum1,
+ nlay2 = nlay2,
+ vsum2 = vsum2,
+ quiet=True)
+ NO = float(grass.parse_command("r.univar", quiet=True, flags="g", map=tmpf0)['sum'])
+ NOV = 1 - ( 0.5 * NO )
+ grass.run_command("g.remove", quiet=True, flags="f", type="raster", name=tmpf0)
+ text_file.write("D, " + nvar1 + "," + nvar2 + "," + str(NOV) + "\n")
+ grass.message("niche overlap (D) of " + nvar1 + " and " + nvar2 + ": " + str(round(NOV,3)))
+
+ ## Calculate I (Warren et al. 2008), but note that the original formulation
+ ## was corrected in erratum by Warren et al, using I = 1 - H^2 * 0.5
+ ## The sqrt in the H formulation and this new ^2 cancel each other out,
+ ## leading to the formulation used below.
+ #=======================================================================
+
+ if flag_i:
+ tmpf1 = "rniche_" + str(uuid.uuid4())
+ tmpf1 = string.replace(tmpf1, '-', '_')
+ clean_rast.add(tmpf1)
+ grass.mapcalc("$tmpf1 = (sqrt(double($nlay1)/$vsum1) - sqrt(double($nlay2)/$vsum2))^2",
+ tmpf1 = tmpf1,
+ nlay1 = nlay1,
+ vsum1 = vsum1,
+ nlay2 = nlay2,
+ vsum2 = vsum2,
+ quiet=True)
+ NE = float(grass.parse_command("r.univar", quiet=True, flags="g", map=tmpf1)['sum'])
+ NEQ = 1 - (0.5 * NE)
+ grass.run_command("g.remove", quiet=True, flags="f", type="raster", name=tmpf1)
+ text_file.write("I, " + nvar1 + "," + nvar2 + "," + str(NEQ) + "\n")
+ grass.message("niche overlap (I) of " + nvar1 + " and " + nvar2 + ": " + str(round(NEQ, 3)))
+
+ j = j + 1
+ i = i + 1
+
+ text_file.close()
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ atexit.register(cleanup)
+ sys.exit(main())
+
+
+
+
+
+
+
+
+
+
+
+
Property changes on: grass-addons/grass7/raster/r.niche.similarity/r.niche.similarity.py
___________________________________________________________________
Added: svn:executable
+ *
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
More information about the grass-commit
mailing list