Skip to content

Commit

Permalink
r.niche.similarity: address #897 (#898)
Browse files Browse the repository at this point in the history
* Partially address #897

This small PR solves the Attribute error part reported in #897

* address the type error in printing

This solves the second error. The text file does not look great though...

* Update src/raster/r.niche.similarity/r.niche.similarity.py

Co-authored-by: Anna Petrasova <kratochanna@gmail.com>

* update r.niche.similarity bugfix

Changes to solve the errors reported in #897. The printing in the csv file is fixed as well (was caused by earlier solution, reverted to old code used to print to csv).

---------

Co-authored-by: Anna Petrasova <kratochanna@gmail.com>
Co-authored-by: Paulo van Breugel <paulo@ecodiv.earth>
  • Loading branch information
3 people committed Jun 4, 2023
1 parent 7155c1c commit 3b15657
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/raster/r.niche.similarity/r.niche.similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import sys
import atexit
import uuid
import string
import grass.script as gs

# Cleanup
Expand All @@ -84,19 +83,23 @@ def cleanup():
gs.run_command("g.remove", type="raster", name=rast, quiet=True, flags="f")


def tmpname(prefix):
"""Generate a tmp name which contains prefix, store the name in the
global list.
def create_unique_name(name):
"""Generate a tmp name which contains prefix
Store the name in the global list.
Use only for raster maps.
"""
tmpf = prefix + str(uuid.uuid4())
tmpf = string.replace(tmpf, "-", "_")
return name + str(uuid.uuid4().hex)


def create_temporary_name(prefix):
tmpf = create_unique_name(prefix)
CLEAN_LAY.append(tmpf)
return tmpf


def D_index(n1, n2, v1, v2, txtf):
"""Calculate D (Schoener's 1968)"""
tmpf0 = tmpname("rniche")
tmpf0 = create_temporary_name("rniche")
gs.mapcalc(
"$tmpf0 = abs(double($n1)/$v1 - double($n2)/$v2)",
tmpf0=tmpf0,
Expand All @@ -120,7 +123,7 @@ def I_index(n1, v1, n2, v2, txtf):
"""Calculate I (Warren et al. 2008). Note that the sqrt in the
H formulation and the ^2 in the I formation cancel each other out,
hence the formulation below"""
tmpf1 = tmpname("rniche")
tmpf1 = create_temporary_name("rniche")
gs.mapcalc(
"$tmpf1 = (sqrt(double($n1)/$v1) - sqrt(double($n2)/$v2))^2",
tmpf1=tmpf1,
Expand Down Expand Up @@ -222,7 +225,7 @@ def main(options, flags):
IND = [["Statistic", "Layer 1", "Layer 2", "value"]] + Dind + Iind + Cind
import csv

with open(OPF, "wb") as f:
with open(OPF, "w") as f:
writer = csv.writer(f)
writer.writerows(IND)
gs.info(_("Results written to {}").format(OPF))
Expand Down

0 comments on commit 3b15657

Please sign in to comment.