def execute_bmds_figure(self, BMDS, model_class, temp_fn): """ Execute BMDS executable to generate BMD figure. """ image = None f_002 = os.path.join(BMDS.temp_path, temp_fn) + '.002' f_plt = os.path.join(BMDS.temp_path, temp_fn) + '.plt' f_emf = os.path.join(BMDS.temp_path, temp_fn) + '.emf' # Attempt to generate wgnuplot output images- an image processing # failure will not result in an overall error in the BMD modeling try: # Create a .plt file from a .002 file exe_plt = os.path.join( BMDS.model_path, model_class.exe_plot + settings.BMD_EXTENSION) run_process([exe_plt, f_002], 10).Run() # Revise settings of .plt file to export as emf image instead # of appear in the GUI # todo: recompile BMDS source to do this automatically plotfile = open(f_plt, 'r') plot_text = plotfile.read() plotfile.close() plot_text = plot_text.replace( "set terminal %s \n" % (settings.BMD_SHELL), "set terminal emf \nset output '%s'\n" % (f_emf)) plot_text = plot_text.replace( "set terminal %s\n" % (settings.BMD_SHELL), "set terminal emf \nset output '%s'\n" % (f_emf)) plotfile = open(f_plt, 'w') plotfile.write(plot_text) plotfile.close() # Call gnuplot to generate image gnu_exe = os.path.join(BMDS.model_path, settings.BMD_PLOT + settings.BMD_EXTENSION) run_process([gnu_exe, f_plt], 10).Run() # Load plot into memory if os.path.exists(f_emf) and (os.path.getsize > 0): image = File(open(f_emf, 'rb')) logging.debug("gnuplot plot generation successful") else: logging.debug("gnuplot plot generation not successful") except: logging.debug("gnuplot plot generation not successful") BMD_model_run.delete_files(([f_002, f_plt, f_emf])) return image
def execute_bmds_figure(self, BMDS, model_class, temp_fn): """ Execute BMDS executable to generate BMD figure. """ image = None f_002 = os.path.join(BMDS.temp_path, temp_fn) + '.002' f_plt = os.path.join(BMDS.temp_path, temp_fn) + '.plt' f_emf = os.path.join(BMDS.temp_path, temp_fn) + '.emf' # Attempt to generate wgnuplot output images- an image processing # failure will not result in an overall error in the BMD modeling try: # Create a .plt file from a .002 file exe_plt = os.path.join(BMDS.model_path, model_class.exe_plot + settings.BMD_EXTENSION) run_process([exe_plt, f_002], 10).Run() # Revise settings of .plt file to export as emf image instead # of appear in the GUI # todo: recompile BMDS source to do this automatically plotfile = open(f_plt, 'r') plot_text = plotfile.read() plotfile.close() plot_text = plot_text.replace("set terminal %s \n" % (settings.BMD_SHELL), "set terminal emf \nset output '%s'\n" % (f_emf)) plot_text = plot_text.replace("set terminal %s\n" % (settings.BMD_SHELL), "set terminal emf \nset output '%s'\n" % (f_emf)) plotfile = open(f_plt, 'w') plotfile.write(plot_text) plotfile.close() # Call gnuplot to generate image gnu_exe = os.path.join(BMDS.model_path, settings.BMD_PLOT + settings.BMD_EXTENSION) run_process([gnu_exe, f_plt], 10).Run() # Load plot into memory if os.path.exists(f_emf) and (os.path.getsize > 0): image = File(open(f_emf, 'rb')) logging.debug("gnuplot plot generation successful") else: logging.debug("gnuplot plot generation not successful") except: logging.debug("gnuplot plot generation not successful") BMD_model_run.delete_files(([f_002, f_plt, f_emf])) return image
def execute_bmds(self, BMDS, model_class, d_file, create_image=True): """ Run series of executables and return a dictionary of results """ outputs = {} # get random filename temp_fn = ''.join( random.choice(string.ascii_lowercase) for x in range(16)) f_in = os.path.join(BMDS.temp_path, temp_fn) + '.(d)' #exponential models prefix = getattr(model_class, 'output_prefix', "") temp_fn = prefix + temp_fn f_out = os.path.join(BMDS.temp_path, temp_fn) + '.out' with open(f_in, 'w') as f: f.write(d_file) # run BMDS logging.debug('running BMDS model...') exe = os.path.join(BMDS.model_path, model_class.exe + settings.BMD_EXTENSION) run_process([exe, f_in], 10).Run() if create_image: outputs['image'] = self.execute_bmds_figure( BMDS, model_class, temp_fn) # import outputs logging.debug('importing BMDS results...') output_text = "" if os.path.exists(f_out): with open(f_out, 'r') as f: output_text = f.read() if model_class.model_name == 'Exponential': dtype = 'E' else: dtype = model_class.dtype o = BMD_output_parser(output_text, dtype, self.model_name) outputs['image_fn'] = temp_fn + '.emf' outputs['output_text'] = output_text outputs['outputs'] = o.output BMD_model_run.delete_files(([f_in, f_out])) return outputs
def execute_bmds(self, BMDS, model_class, d_file, create_image=True): """ Run series of executables and return a dictionary of results """ outputs = {} # get random filename temp_fn = ''.join(random.choice(string.ascii_lowercase) for x in range(16)) f_in = os.path.join(BMDS.temp_path, temp_fn) + '.(d)' #exponential models prefix = getattr(model_class, 'output_prefix', "") temp_fn = prefix + temp_fn f_out = os.path.join(BMDS.temp_path, temp_fn) + '.out' with open(f_in, 'w') as f: f.write(d_file) # run BMDS logging.debug('running BMDS model...') exe = os.path.join(BMDS.model_path, model_class.exe + settings.BMD_EXTENSION) run_process([exe, f_in], 10).Run() if create_image: outputs['image'] = self.execute_bmds_figure(BMDS, model_class, temp_fn) # import outputs logging.debug('importing BMDS results...') output_text="" if os.path.exists(f_out): with open(f_out, 'r') as f: output_text = f.read() if model_class.model_name == 'Exponential': dtype = 'E' else: dtype = model_class.dtype o = BMD_output_parser(output_text, dtype, self.model_name) outputs['image_fn'] = temp_fn + '.emf' outputs['output_text'] = output_text outputs['outputs'] = o.output BMD_model_run.delete_files(([f_in, f_out])) return outputs