Skip to content

Commit 7382858

Browse files
committed
update
1 parent 2c683fe commit 7382858

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

test_pit_sim.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
#usage: test_pit_sim.py [-h] [--iters [integer]] [--bounds [integer]] [--padding [integer]] [--sampres [integer]] [--sampint [integer]] [--repeats [integer]] [--sampdist [map name]]
1010
parser = argparse.ArgumentParser(description='This model simulates additive sampling strategies on a known distribution of artifacts.')
1111
parser.add_argument('--iters', metavar='integer', type=int, nargs='?', const=3, default=3, help='The number of steps in the additive sampling routine (per iteration)')
12-
parser.add_argument('--bounds', metavar='integer', type=int, nargs='?', const=300, default=300, help='The size of the sampling universe (a square)')
13-
parser.add_argument('--padding', metavar='integer', type=int, nargs='?', const=100, default=100, help='The number of additional units to pad the edges of the sampling universe (to allow for grid rotation, etc)')
12+
parser.add_argument('--bounds', metavar='integer', type=int, nargs='?', const=450, default=450, help='The size of the sampling universe (a square)')
13+
parser.add_argument('--padding', metavar='integer', type=int, nargs='?', const=50, default=50, help='The number of additional units to pad the edges of the sampling universe (to allow for grid rotation, etc)')
1414
parser.add_argument('--sampres', metavar='integer', type=int, nargs='?', const=3, default=3, help='The size of each sampling units (also a square)')
1515
parser.add_argument('--sampint', metavar='integer', type=int, nargs='?', const=50, default=50, help='The interval of the initial sampling units')
1616
parser.add_argument('--repeats', metavar='integer', type=int, nargs='?', const=100, default=100, help='The number of times to shuffle the grid and resample the distribution (number of iterations for the simulation)')
17-
parser.add_argument('--sampdist', metavar='map name', type=str, nargs='?', const="", default="", help='The artifact distribution to be sampled (a kernel density map)')
17+
parser.add_argument('--sampdist', metavar='map name', type=str, nargs='?', const="", default="", help='The artifact distribution to be sampled (a binary presence/absence map or a density map)')
18+
parser.add_argument('--sites', metavar='map name', type=str, nargs='?', const="Site_areas@gridsim", default="Site_areas@gridsim", help='A binary raster map where areas within sites are coded 1, and areas without are coded 0)')
1819
# Read in the entered values to internal variables
1920
args = vars(parser.parse_args())
2021
iters = args['iters']
@@ -26,6 +27,7 @@
2627
grass.message("Resampling distance is %s squares" % resamp)
2728
repeats = args['repeats']
2829
sampdist = args['sampdist']
30+
sites = args['sites']
2931
prefix = '%s_%s' % (sampdist,sampint)
3032

3133
def main(iters,bounds,padding,sampres,sampint,repeats,sampdist,prefix):
@@ -38,10 +40,13 @@ def main(iters,bounds,padding,sampres,sampint,repeats,sampdist,prefix):
3840
grass.mapcalc("${realpresab}=if(${sampdist} > 0, 1, 0)", overwrite = True, quiet = True, sampdist = sampdist, realpresab = realpresab)
3941
#extract the number of squares that have at least one artifact
4042
truepres = int(grass.parse_command("r.stats", quiet = True, flags="cn", input=realpresab, separator="=")["1"])
41-
#set up statsfile
43+
#set up statsfiles
4244
f = open('%s%s%s_stats.csv' % (os.getcwd(), os.sep, prefix), 'w+')
4345
f.write("Iteration,Positive Squares,Interpolated Positives,True Positives,Numeric Difference,Percent Difference,R,R2\n")
4446
f.flush()
47+
f2 = open('%s%s%s_sites_stats.csv' % (os.getcwd(), os.sep, prefix), 'w+')
48+
f2.write("Iteration,Inside Positives,Inside Negatives,Outside Positives,Outside Negatives\n")
49+
f2.flush()
4550
#initiate the outer loop, which will randomly rotate/move the sampling grid within the sampling universe
4651
pad = len(str(repeats)) # set the zfill pad to match the number of repeats
4752
for x in range(repeats):
@@ -84,8 +89,8 @@ def main(iters,bounds,padding,sampres,sampint,repeats,sampdist,prefix):
8489
grass.run_command("r.fillnulls", quiet=True, overwrite=True, input=outmap, output=dsurf, method="bicubic")
8590
# turn density surface into presab to compare to real presab map via regression analysis
8691
outpres = "%s_%s_interp_pres" % (prefix,str(x + 1).zfill(pad))
87-
grass.mapcalc("${outpres}=round(${dsurf})", quiet=True, overwrite=True, dsurf=dsurf, outpres=outpres)
88-
# pull some stats
92+
grass.mapcalc("${outpres}=eval(a=round(${dsurf}), if(a >= 1, 1, 0))", quiet=True, overwrite=True, dsurf=dsurf, outpres=outpres)
93+
# pull some general stats
8994
presab = grass.parse_command("r.stats", quiet = True,flags="cn", input=outmap, separator="=")
9095
interp_presab = grass.parse_command("r.stats", quiet = True,flags="cn", input=outpres, separator="=")
9196
try:
@@ -99,8 +104,29 @@ def main(iters,bounds,padding,sampres,sampint,repeats,sampdist,prefix):
99104
R = grass.parse_command('r.regression.line', flags='g', mapx=realpresab, mapy=outpres)["R"]
100105
f.write("%s,%s,%s,%s,%s,%s,%s,%s\n" % (x + 1,pres,intpres,truepres,truepres - intpres,(truepres - intpres)/float(truepres),R, float(R) * float(R)))
101106
f.flush()
107+
# pull some site stats
108+
sites_stats = grass.read_command("r.stats", quiet = True,flags="cn", input="%s,%s" % (sites,outmap), separator=",")
109+
site_negs = 0
110+
site_pos = 0
111+
out_negs = 0
112+
out_pos = 0
113+
for lines in sites_stats.split('\n'):
114+
line=lines.split(',')
115+
if line[0] is '1' and line[1] is '0':
116+
site_negs = line[2]
117+
if line[0] is '1' and line[1] is '1':
118+
site_pos = line[2]
119+
if line[0] is '0' and line[1] is '0':
120+
out_negs = line[2]
121+
if line[0] is '0' and line[1] is '1':
122+
out_pos = line[2]
123+
else:
124+
pass
125+
f2.write("%s,%s,%s,%s,%s\n" % (x+1,site_pos,site_negs,out_pos,out_negs))
126+
f.flush()
102127
#clean up
103128
f.close()
129+
f2.close()
104130
grass.run_command("g.remove", quiet = True, flags = 'f', type = 'raster', pattern = "*%s" % handle)
105131

106132
if __name__ == "__main__":

0 commit comments

Comments
 (0)