#!/usr/bin/python

import os
from nlputil import *

# Run a series of disambiguation experiments.

tgdir = os.environ['TEXTGROUNDER_DIR']
runcmd = '%s/python/run-run-disambig' % tgdir
#common_args='--max-time-per-stage 5'
common_args='--max-time-per-stage 300'

degrees_per_region=[90, 30, 10, 5, 3, 2, 1, 0.5]
degrees_per_region2=[90, 75, 60, 50, 40, 30, 25, 20, 15, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2.5, 2, 1.75, 1.5, 1.25, 1, 0.87, 0.75, 0.63, 0.5, 0.4, 0.3, 0.25, 0.2, 0.15, 0.1]
 
strategy=['partial-kl-divergence', 'per-word-region-distribution', 'baseline']
baseline_strategy=['internal-link', 'random', 'num-articles',
    'region-distribution-most-common-proper-noun']

def runit(id, args):
  command='%s --id %s documents %s %s' % (runcmd, id, common_args, args)
  errprint("Executing: %s" % command)
  os.system("%s" % command)

def iter_strategy(fun, id, args):
  for strat in strategy:
    if strat == 'baseline':
      for basestrat in baseline_strategy:
        fun('%s.%s' % (id, basestrat),
            '%s --strategy baseline --baseline-strategy %s' %
            (args, basestrat))
    else:
      fun('%s.%s' % (id, strat), '%s --strategy %s' % (args, strat))

def iter_dpr(fun, id, args):
  for dpr in degrees_per_region:
    fun('%s.%s' % (id, dpr), '%s --degrees-per-region %s' % (args, dpr))

def recurse(funs, *args):
  if not funs:
    return
  (funs[0])(lambda *args: recurse(funs[1:], *args), *args)

recurse([iter_strategy, iter_dpr, (lambda fun, id, args: runit(id[1:], args))],
    '', '')

#!/usr/bin/python

import os
from nlputil import *

dry_run = False

# Run a series of disambiguation experiments.

tgdir = os.environ['TEXTGROUNDER_DIR']
runcmd = '%s/python/run-run-disambig' % tgdir

def runit(fun, id, args):
  command='%s --id %s documents %s' % (runcmd, id, args)
  errprint("Executing: %s" % command)
  if not dry_run:
    os.system("%s" % command)

def combine(*funs):
  def do_combine(fun, *args):
    for f in funs:
      f(fun, *args)
  return do_combine

def iterate(paramname, vals):
  def do_iterate(fun, id, args):
    for val in vals:
      fun('%s.%s' % (id, val), '%s %s %s' % (args, paramname, val))
  return do_iterate

def recurse(funs, *args):
  if not funs:
    return
  (funs[0])(lambda *args: recurse(funs[1:], *args), *args)

def nest(*nest_funs):
  def do_nest(fun, *args):
    recurse(nest_funs + (fun,), *args)
  return do_nest

def run_exper(exper, expername):
  exper(lambda fun, *args: runit(id, *args), expername, '')

def main():
  op = OptionParser(usage="%prog [options] experiment [...]")
  op.add_option("-n", "--dry-run", action="store_true",
		  help="Don't execute anything; just output the commands that would be executed.")
  (opts, args) = op.parse_args()
  global dry_run
  if opts.dry_run:
    dry_run = True
  for exper in args:
    run_exper(eval(exper), exper)

##############################################################################
#                       Description of experiments                           #
##############################################################################

MTS300 = iterate('--max-time-per-stage', [300])
NonBaselineStrategies = iterate('--strategy',
    ['partial-kl-divergence', 'per-word-region-distribution'])
BaselineStrategies = iterate('--strategy baseline --baseline-strategy',
    ['internal-link', 'random', 'num-articles',
    'region-distribution-most-common-proper-noun'])
AllStrategies = combine(NonBaselineStrategies, BaselineStrategies)

CoarseDPR = iterate('--degrees-per-region',
    [90, 30, 10, 5, 3, 2, 1, 0.5])
FineDPR = iterate('--degrees-per-region',
    [90, 75, 60, 50, 40, 30, 25, 20, 15, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2.5, 2,
     1.75, 1.5, 1.25, 1, 0.87, 0.75, 0.63, 0.5, 0.4, 0.3, 0.25, 0.2, 0.15, 0.1]
    )

CoarseDisambig = nest(MTS300, AllStrategies, CoarseDPR)

# PCL experiments
PCLDPR = iterate('--degrees-per-region', [1.5, 0.5, 1, 2, 3, 5])
PCLEvalFile = iterate('-f pcl-travel -e', ['/groups/corpora/pcl_travel/books'])
PCLDisambig = nest(MTS300, PCLEvalFile, NonBaselineStrategies, PCLDPR)

main()

Rewrite run-disambig-exper.py so experiment configs can be specified using vars and the particular config given as a command-line arg