Package npsgd :: Module matlab_task
[hide private]
[frames] | no frames]

Source Code for Module npsgd.matlab_task

 1  # Author: Thomas Dimson [tdimson@gmail.com] 
 2  # Date:   January 2011 
 3  # For distribution details, see LICENSE 
 4   
 5  """Module containing classes relating to Matlab modelling tasks.""" 
 6  import os 
 7  import logging 
 8  import subprocess 
 9  from model_task import ModelTask 
10  from config import config 
11   
12 -class MatlabError(RuntimeError): pass
13 -class MatlabTask(ModelTask):
14 """Abstract base matlab task. 15 16 This class is meant to be the superclass of the user's various 17 matlab tasks. It takes parameters from the web interface and 18 launches a matlab script with the parameters _directly_ 19 available within Matlab. 20 """ 21 22 abstractModel = "MatlabTask" 23 24 #Must specify matlab script 25
26 - def runModel(self):
27 matlabBase = os.path.dirname(self.matlabScript) 28 matlabFun = os.path.basename(self.matlabScript).rsplit(".",1)[0] 29 30 paramCode = "\n".join(p.asMatlabCode() for p in self.modelParameters) 31 io = "%s;\npath('%s', path);\n%s;\nexit;\n" % (paramCode, matlabBase, matlabFun) 32 33 logging.info("Opening matlab with script:\n %s", io) 34 mProcess = subprocess.Popen([config.matlabPath, "-nodisplay"], stdin=subprocess.PIPE, 35 cwd=self.workingDirectory, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 36 stdout, stderr = mProcess.communicate(io) 37 logging.info("Stdout was: --------\n%s\n-----", stdout) 38 logging.info("Stderr was: --------\n%s\n-----", stderr) 39 40 if mProcess.returncode != 0: 41 raise MatlabError("Bad return code %s from matlab" % mProcess.returncode) 42 logging.info("Matlab all done!")
43