def improve_solution(projectf, mixture_index, solution, residual, l_bfgs_b_kwargs={}): if projectf is not None: from pyxrd.data import settings settings.initialize() # Retrieve project and mixture: project = setup_project(projectf) del projectf mixture = project.mixtures[mixture_index] with mixture.data_changed.ignore(): # Setup context again: mixture.update_refinement_treestore() refiner = mixture.refinement.get_refiner() # Refine solution vals = fmin_l_bfgs_b( refiner.get_residual, solution, approx_grad=True, bounds=refiner.ranges, **l_bfgs_b_kwargs ) new_solution, new_residual = tuple(vals[0:2]) # Return result return new_solution, new_residual else: return solution, residual
def _worker_initializer(pool_stop, debug, *args): # Spoof command line arguments so settings are loaded with correct # debugging flag if debug and not "-d" in sys.argv: sys.argv.insert(1, "-d") if not debug and "-d" in sys.argv: sys.argv.remove("-d") # Load settings from pyxrd.data import settings settings.initialize() if settings.DEBUG: from pyxrd import stacktracer stacktracer.trace_start( "trace-worker-%s.html" % multiprocessing.current_process().name, interval=5, auto=True) # Set auto flag to always update file! logger.info("Worker process initialized, DEBUG=%s" % debug) pass
def __init__(self): from pyxrd.data import settings settings.initialize() logger.warning("Creating pool, DEBUG=%s" % settings.DEBUG) self.pool_stop = multiprocessing.Event() self.pool_stop.clear() maxtasksperchild = 10 if 'nt' == os.name else None self.pool = multiprocessing.Pool(initializer=_worker_initializer, maxtasksperchild=maxtasksperchild, initargs=( self.pool_stop, settings.DEBUG, )) # register the shutdown settings.FINALIZERS.append(self.shutdown)
def reload_settings(clear_script=True): """ This will reload the PyXRD settings after clearing the script path from the command line arguments. This allows to run the GUI when needed. """ import sys from copy import copy # Make a copy to prevent errors args = copy(sys.argv) for i, arg in enumerate(args): if arg == "-s": # Clear the next key (contains the script name del sys.argv[i + 1] # Clear the flag del sys.argv[i] # Exit the loop break from pyxrd.data import settings settings.SETTINGS_APPLIED = False # clear this flag to reload settings settings.initialize() # reload settings
def __init__(self): from pyxrd.data import settings settings.initialize() logger.warning("Creating pool, DEBUG=%s" % settings.DEBUG) self.pool_stop = multiprocessing.Event() self.pool_stop.clear() maxtasksperchild = 10 if 'nt' == os.name else None self.pool = multiprocessing.Pool( initializer=_worker_initializer, maxtasksperchild=maxtasksperchild, initargs=( self.pool_stop, settings.DEBUG, ) ) # register the shutdown settings.FINALIZERS.append(self.shutdown)
def run_refinement(projectf, mixture_index): """ Runs a refinement setup for - projectf: project data - mixture_index: what mixture in the project to use """ if projectf is not None: from pyxrd.data import settings settings.initialize() # Retrieve project and mixture: project = setup_project(projectf) del projectf import gc gc.collect() mixture = project.mixtures[mixture_index] mixture.refinement.update_refinement_treestore() refiner = mixture.refinement.get_refiner() refiner.refine() return list(refiner.history.best_solution), refiner.history.best_residual
def run_main(): """ Parsers command line arguments and launches PyXRD accordingly. """ # Make sure the current path is used for loading PyXRD modules: mod = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if not mod in sys.path: sys.path.insert(1, mod) # Init settings, first import will trigger initialization from pyxrd.data import settings settings.initialize() # Setup basic logging from pyxrd.logs import setup_logging setup_logging(basic=True) if settings.DEBUG: from pyxrd import stacktracer stacktracer.trace_start( "trace.html", interval=5, auto=True) # Set auto flag to always update file! try: if settings.ARGS.script: # Run the specified user script: _run_user_script() else: # Run the GUI: _run_gui() except: raise # re-raise the error finally: for finalizer in settings.FINALIZERS: finalizer() if settings.DEBUG: stacktracer.trace_stop()
def setUp(self): settings.initialize() self.phase = Phase(R=0, G=1)
def mock_settings(): from pyxrd.data import settings settings._parse_args = mock.Mock(return_value=_mocked_parse_args()) settings.initialize()