Navigation Menu

Skip to content

Commit

Permalink
added random templating engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-lunt committed Jul 17, 2015
1 parent b125130 commit 01d07d5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
35 changes: 30 additions & 5 deletions python/scripts/par_template_processor.py
Expand Up @@ -45,10 +45,13 @@ def normal(mu,std):
#coop_range = S.array([1, 100])
#q_btm_range = S.array([0.001, 0.01])

import pystache

from itertools import count
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--values",default=None,type=str,help="file to get values from for templating")
parser.add_argument("--outpre",default=None,type=str,help="Prefix for generating multiple, files will be OUTPRE_N.par")
parser.add_argument("--N",default=None,type=int,help="Number to generate, ignored if values provided")
parser.add_argument("--base",default=1,type=int,help="Starting point for numbering")
parser.add_argument("INFILE", metavar="IN_FILE", type=str)
#parser.add_argument("OUTFILE", metavar="OUT_FILE", type=str)
args, other = parser.parse_known_args()
Expand All @@ -60,7 +63,29 @@ def normal(mu,std):
my_regex = re.compile("{{(.*?)}}")

things = my_regex.findall(thetemplate)
replaced_things = [foo[one_thing] for one_thing in things]

final_template = my_regex.sub("%f",thetemplate)
print(final_template.strip() % tuple(replaced_things))



if args.values:
#load up the values and whatnot
values = S.loadtxt(args.values,ndmin=2)
Num,M = values.shape
assert M == len(things) , "If you provide values, you must provide all the values."
for i,one_row in zip(range(args.base,S.minimum(args.N,Num)+args.base),values):
substituted = final_template % tuple(one_row)
outfile = open(args.outpre + ("_%i.par" % i),"w")
outfile.write(substituted)
outfile.close()
sys.exit(0)

elif args.N == None: #use the templating engine
replaced_things = [foo[one_thing] for one_thing in things]
print(final_template.strip() % tuple(replaced_things))
elif args.N >= 1:
for i in range(args.base,args.base+args.N):
replaced_things = [foo[one_thing] for one_thing in things]
substituted = final_template % tuple(replaced_things)
outfile = open(args.outpre + ("_%i.par" % i),"w")
outfile.write(substituted)
outfile.close()
28 changes: 28 additions & 0 deletions python/src/gemstat/scores.py
@@ -0,0 +1,28 @@
def sse(gt,prediction):
"""Weighting not yet implemented.
"""

return S.power(gt - prediction).mean()

def wPGP(gt_prediction):
"""
Not yet weighted.
"""
r_max = gt.max()

prediction = S.minimum(prediction,1.0)

reward = (gt*S.minimum(gt,prediction)).sum()/S.power(gt,2.0).sum()

penalty_num = (
(r_max - gt)
*(prediction - gt)
*S.array(prediction > gt,dtype=S.float_)
).sum()

penalty_denom = S.power(r_max - gt,2.0).sum()
penalty = penalty_num / penalty_denom

wpgp_score = 0.5 + 0.5*(reward-penalty)
assert wpgp_score <= 1.0 and wpgp_score >= 0.0, "PGP score not in acceptable range"
return wpgp_score

0 comments on commit 01d07d5

Please sign in to comment.