class pmedian_instance_from_data_Task(pywst.spot.core.Task): alias('pmedian_instance_from_data') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('dat_filename') self.inputs.declare('impact_data') # self.outputs.declare('pyomo_instance') def execute(self): pmedian_model = os.path.abspath( dirname(dirname(abspath(__file__))) + os.sep + "models" + os.sep + "pmedian.py") options = Options() if not self.dat_filename is None: util.apply_preprocessing(options, None, [pmedian_model, self.dat_filename]) model = util.create_model(options, [pmedian_model, self.dat_filename]) self.pyomo_instance = model.instance else: print "WARNING: it's not clear how to use a registered function for loading model data directly."
class DefaultTestDriver(Plugin): """ This is the 'default' test driver, which simply prints the arguments being passed into a test. """ implements(plugins.ITestDriver) alias('default') def setUpClass(self, cls, options): """Set-up the class that defines the suite of tests""" def tearDownClass(self, cls, options): """Tear-down the class that defines the suite of tests""" def setUp(self, testcase, options): """Set-up a single test in the suite""" def tearDown(self, testcase, options): """Tear-down a single test in the suite""" sys.stdout.flush() def run_test(self, testcase, name, options): """Execute a single test in the suite""" print('run_test ' + name) print(options)
class tso2Impact_SPTK(tso2Impact_Task): alias('pywst.spot.sptk.tso2Impact', doc='Run tso2Impact') def __init__(self, *args, **kwds): tso2Impact_Task.__init__(self, *args, **kwds) self.add_argument( '--tso_file', dest='tso_filename', default=[], help= "This argument indicates either a TSO file or a directory name for TSO files. If the later, then the filenames must be specified with the --tsoPattern option." ) self.add_argument( '--impact', dest='impact_list', default=[], action='append', help= "Specify an impact value. This option can be specified multiple times to define a list of impact values. Options include detected extent of contamination (dec), detected mass consumed (dmc), detected population dosed (dpd), detected population exposed (dpe), detected population killed (dpk), detected time-to-detection (dtd), detected volume consumed (dvc), extent of contamination (ec), mass consumed (mc), number-of-failed detections (nfd), population dosed (pd), population exposed (pe), population killed (pk), time-to-detection (td), timed extent of contamination (tec), volume consumed (vc)." ) self.add_argument( '--detectionLimit', dest='detectionLimit', default=None, help= "If this is specified, then only concentration values above this limit are archived." ) self.add_argument( '--responseTime', dest='responseTime', default=None, help= "The number of minutes that are needed to respond to the detection of a contaminant." )
class Default_Config(BaseProblemConfig): alias('default') def config_block(self, init=False): config, blocks = minlp_config_block(init=init) return config, blocks
class tuning_summary_Task(pywst.spot.core.Task): alias('pywst.spot.tuning_summary') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('results') self.inputs.declare('pyomo_instance') def execute(self): print self.results print "" for i in range(0, len(self.results.solution)): vars = self.results.solution[i].variable ctr = 0 for id in vars.y: if vars.y[id].value == 1 and not '_1' in id: ctr += 1 print "CTR", ctr print "P", self.pyomo_instance.P() print "FAR", vars.far.value print "IMPACT", self.results.solution[i].objective.f.value for id in vars.y: if vars.y[id].value == 1 and not '_1' in id: print "Y", id
class Switch_Task(TaskPlugin): alias('workflow.switch') def __init__(self, *args, **kwds): TaskPlugin.__init__(self, *args, **kwds) self._branches = {} self.inputs.declare('value') def add_branch(self, value, task): self._branches[value] = 'task' + str(task.id) self.output_controls.declare(self._branches[value]) task.input_controls.declare('task' + str(self.id)) setattr(task.input_controls, 'task' + str(self.id), self.output_controls[self._branches[value]]) def execute(self): flag = False for key in self._branches: if self.value == key: self.output_controls[self._branches[key]].set_ready() flag = True else: self.output_controls[self._branches[key]].reset() if not flag: raise ValueError( "Branch condition has value '%s' but no branch is indexed with that value.\n Valid branch indices: %s" % (str(self.value), sorted(self._branches.keys()))) def __repr__(self): return TaskPlugin.__repr__(self) #pragma:nocover
class sptkHW_workflow(pywst.spot.core.Workflow): alias('pywst.spot.sptk.hw', doc='A "Hello World" task example') def __init__(self, *args, **kwds): pywst.spot.core.Workflow.__init__(self, *args, **kwds) self.add(pywst.spot.core.TaskFactory('pywst.spot.hw'))
class IfThen_Task(Switch_Task): alias('workflow.branch') def __init__(self, *args, **kwds): Switch_Task.__init__(self, *args, **kwds) def __repr__(self): return Switch_Task.__repr__(self) #pragma:nocover
class Selection_Task(TaskPlugin): alias('workflow.selection') def __init__(self, *args, **kwds): TaskPlugin.__init__(self, *args, **kwds) # self.inputs.declare('index') self.inputs.declare('data') # self.outputs.declare('selection') def execute(self): self.selection = self.data[self.index]
class tevasim_Task(pywst.spot.core.Task): alias('run_tevasim') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('tsg_filename', optional=True) self.inputs.declare('tsi_filename', optional=True) self.inputs.declare('tso_filename', optional=True) self.inputs.declare('epanet_model') self.inputs.declare('epanet_output', optional=True) self.inputs.declare('tevasim_logfile', optional=True) # self.outputs.declare('epanet_output') # in/out self.outputs.declare('tso_filename') self.outputs.declare('sdx_filename') self.outputs.declare('tevasim_logfile') # in/out # self.add_resource(pyutilib.workflow.ExecutableResource('tevasim')) def execute(self): if self.tso_filename is None: self.tso_filename = pyutilib.services.TempfileManager.create_tempfile( suffix=".tso") self.sdx_filename = os.path.splitext(self.tso_filename)[0] + ".sdx" if self.tsg_filename is None and self.tsi_filename is None: raise IOError, "tevasim_Task requires either a TSG filename or TSI filename" # args = "" if not self.tsg_filename is None: args += " --tsg %s" % self.tsg_filename if not self.tsi_filename is None: args += " --tsi %s" % self.tsi_filename if not self.tso_filename is None: args += " --tso %s" % self.tso_filename args += " " + self.epanet_model if not self.epanet_output is None: args += " " + self.epanet_output else: self.epanet_output = pyutilib.services.TempfileManager.create_tempfile( prefix="EPANET_log", suffix=".out") args += " " + self.epanet_output if self.tevasim_logfile is None: self.tevasim_logfile = pyutilib.services.TempfileManager.create_tempfile( prefix='tevasim_', suffix=".log") # Manage debugging in tasks self.resource('tevasim').run(args, logfile=self.tevasim_logfile, debug=True)
class sptkSP_workflow(pywst.spot.core.Workflow): alias('pywst.spot.sptk.sp', doc='Sensor placement command') def __init__(self, *args, **kwds): pywst.spot.core.Workflow.__init__(self, *args, **kwds) # self.create_pmedian_workflow() def create_pmedian_workflow(self): # T0 = TaskFactory('sptkSP.set_input') # #T1 = TaskFactory('run_tso2impact') # #T2_1 = TaskFactory('workflow.selection') #T2_1.inputs.data = T1.outputs.impact_data #T2_1.inputs.index.set_value(imp) # T2_2 = TaskFactory('parse_simple_impact') T2_2.inputs.impact_filename = T0.outputs.impact_filename # T2 = TaskFactory('impact2dat_pmedian') T2.inputs.impact_data = T2_2.outputs.impact_data T2.inputs.num_sensors = T0.outputs.num_sensors # T3 = TaskFactory('pmedian_instance_from_data') T3.inputs.dat_filename = T2.outputs.dat_filename # T4 = TaskFactory('pyomoInstance2results') T4.inputs.pyomo_instance = T3.outputs.pyomo_instance T4.inputs.solver = T0.outputs.solver # T5 = TaskFactory('pywst.spot.results2sensors') T5.inputs.results = T4.outputs.results # #T6 = TaskFactory('pywst.spot.run_evalsensor') #T6.inputs.tso_impact_data = T1.outputs.impact_data #T6.inputs.sensor_filename = T5.outputs.sensor_filename # # Create workflow # self.add(T0) def execute(self): print "Starting SP" pywst.spot.core.Workflow.execute(self) print "Finishing SP"
class impact2simpleDat_Task(pywst.spot.core.Task): alias('impact2dat_pmedian') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('impact_data') self.inputs.declare('num_sensors') # self.outputs.declare('dat_filename') def execute(self): if self.dat_filename is None: self.dat_filename = pyutilib.services.TempfileManager.create_tempfile( suffix='.dat') convert_pmedian(self.impact_data, self.dat_filename, self)
class sptkHW_task(pywst.spot.core.Task): alias('pywst.spot.hw', doc='A "Hello World" task example') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('input', optional=True) self.add_argument( '--input', dest='input', default=None, help="An input value that is printed after HELLO WORLD") # #self.outputs.declare('out') def execute(self): print "HELLO WORLD", self.input
class pyomoInstance2results_Task(pywst.spot.core.Task): alias('pyomoInstance2results') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('pyomo_instance') self.inputs.declare('solver') self.inputs.declare('solver_options') # self.outputs.declare('results') def execute(self): options = Options() options.keepfiles = True options.solver = self.solver options.solver_options = self.solver_options self.results, opt = util.apply_optimizer(options, self.instance) print self.results
class parseSimpleImpact_Task(pywst.spot.core.Task): alias('parse_simple_impact') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('impact_filename') self.inputs.declare('tso_impact_data') # self.outputs.declare('impact_data') self.outputs.declare('default_dat_filename') def execute(self): if not self.impact_filename is None: filename = self.impact_filename elif not self.tso_impact_data is None: filename = self.tso_impact_data.impact # self.impact_data = parse_simple_impact(filename) self.default_dat_filename = output_file(filename)
class ExampleTestDriver(pyutilib.autotest.TestDriverBase): """ This test driver executes a unix command and compares its output with a baseline value. """ alias('example') def run_test(self, testcase, name, options): """Execute a single test in the suite""" name = options.suite+'_'+name cmd = options.solver+' ' if not options.cat_options is None: cmd += options.cat_options+' ' cmd += options.file print( "Running test suite '%s' test '%s' command '%s'" % \ (options.suite, name, cmd)) pyutilib.subprocess.run(cmd, outfile=options.currdir+'test_'+name+".out") testcase.failUnlessFileEqualsBaseline( options.currdir+'test_'+name+".out", options.currdir+'test_'+name+".txt")
class sptkSP_task(pywst.spot.core.Task): alias('sptkSP.set_input') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('impact_filename', optional=True) self.inputs.declare('num_sensors') self.inputs.declare('solver') self.add_argument( '--impact_file', dest='impact_filename', default=None, help= " A file that contains the impact data concerning contamination incidents. " ) self.add_argument('--num_sensors', dest='num_sensors', default=None, help="Number of sensors") self.add_argument( '--solver', dest='solver', default=None, help= "This option specifies the type of solver that is used to find sensor placement(s). The following solver types are currently supported: AT&T multistart local search heuristic (att_grasp), TEVA-SPOT license-free grasp clone (snl_grasp), lagrangian relaxation heuristic solver (lagrangian), mixed-integer programming solver (pico), mixed-integer programming solver (glpk), MIP solver with AMPL (picoamp), commercial MIP solver (cplexamp). The default solver is snl_grasp." ) self.outputs.declare('impact_filename') self.outputs.declare('num_sensors') self.outputs.declare('solver') def execute(self): self.outputs.impact_filename = self.inputs.impact_filename self.outputs.num_sensors = self.inputs.num_sensors self.outputs.solver = self.inputs.solver
class impact2dat_tuning_Task(pywst.spot.core.Task): alias('impact2dat_tuning') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('impact_data') self.inputs.declare('num_sensors') self.inputs.declare('num_levels') self.inputs.declare('fp_threshold') self.inputs.declare('fp') self.inputs.declare('pd') #self.inputs.declare('nmap') self.inputs.declare('location_categories') # self.outputs.declare('dat_filename') def execute(self): if self.dat_filename is None: self.dat_filename = pyutilib.services.TempfileManager.create_tempfile( suffix='.dat') convert_tuning(self.impact_data, self.dat_filename, self.location_categories, self)
class tso2Impact_Task(pywst.spot.core.Task): alias('run_tso2impact') def __init__(self, *args, **kwds): pywst.spot.core.Task.__init__(self, *args, **kwds) # self.inputs.declare('impact_list') self.inputs.declare('detectionLimit', optional=True) self.inputs.declare('responseTime', optional=True) self.inputs.declare('output_prefix', optional=True) self.inputs.declare('tso_filename') self.inputs.declare('tai_filename', optional=True) self.inputs.declare('logfile', optional=True) # self.outputs.declare('impact_data') self.outputs.declare('logfile') # in/out # self.add_resource(pyutilib.workflow.ExecutableResource('tso2Impact')) def execute(self): if len(self.impact_list) is 0: raise IOError, "tso2Impact_Task expected non-zero list for input 'impact_list'" # args = "" for impact in self.impact_list: args += " --%s" % str(impact) if not self.detectionLimit is None: args += " --detectionLimit=%s" % str(self.detectionLimit) if not self.responseTime is None: args += " --responseTime=%s" % str(self.responseTime) if self.output_prefix is None: impact_tempfiles = True self.output_prefix = pyutilib.services.TempfileManager.create_tempfile( prefix='tso2Impact_') else: impact_tempfiles = False args += " %s" % str(self.output_prefix) args += " %s" % str(self.tso_filename) if not self.tai_filename is None: args += " %s" % str(self.tai_filename) # if self.logfile is None: self.logfile = pyutilib.services.TempfileManager.create_tempfile( suffix=".log") # TODO: manage debugging in tasks self.resource('tso2Impact').run(args, logfile=self.logfile, debug=True) # self.impact_data = {} for impact in self.impact_list: data = Container( type=str(impact), impact="%s_%s.impact" % (self.output_prefix, impact), id="%s_%s.impact.id" % (self.output_prefix, impact), nodemap="%s.nodemap" % self.output_prefix, scenariomap="%s.scenariomap" % self.output_prefix) pyutilib.services.TempfileManager.add_tempfile(data.impact) pyutilib.services.TempfileManager.add_tempfile(data.id) pyutilib.services.TempfileManager.add_tempfile(data.nodemap) pyutilib.services.TempfileManager.add_tempfile(data.scenariomap) self.impact_data[impact] = data