def map_pspace(fn, pspace): """Maps a function to parameter space values. Parameters ---------- fn : function Function to evaluate on parameter space. Has to return a dictionary. pspace : :class:`.pspace._PSpaceObj` Parameter space providing parameter values to evaluate function on. Returns ------- dict Dictionary with the input parameter values and the function return values. Examples -------- >>> def fn(x): ... return {'y': x * x} >>> >>> from pprint import pprint >>> from psyrun import Param >>> pprint(map_pspace(fn, Param(x=[1, 2]))) {'x': [1, 2], 'y': [1, 4]} """ return dict_concat(list(get_result(fn, p) for p in pspace.iterate()))
def split(self): """Perform splitting of parameters space and save input files for processing.""" items_remaining = len(self.pspace) param_iter = self.pspace.iterate() for i, filename in enumerate(self._iter_filenames()): split_size = max( self.min_items, items_remaining // (self.max_splits - i)) items_remaining -= split_size block = dict_concat( [row for row in self._iter_n(param_iter, split_size)]) save_dict_h5(os.path.join(self.indir, filename), block)
def map_pspace_parallel(fn, pspace, n_jobs=-1, backend='multiprocessing'): """Maps a function to parameter space values in parallel. Requires `joblib <https://pythonhosted.org/joblib/>`_. Parameters ---------- fn : function Function to evaluate on parameter space. Has to return a dictionary. pspace : :class:`.pspace._PSpaceObj` Parameter space providing parameter values to evaluate function on. n_jobs : int, optional Number of parallel jobs. Set to -1 to automatically determine. backend : str, optional Backend to use. See `joblib documentation <https://pythonhosted.org/joblib/parallel.html#using-the-threading-backend>`_ for details. Returns ------- dict Dictionary with the input parameter values and the function return values. Examples -------- >>> from pprint import pprint >>> from psyrun import Param >>> from psyrun.example import square >>> >>> pprint(map_pspace_parallel(square, Param(x=[1, 2]))) {'x': [1, 2], 'y': [1, 4]} """ import joblib parallel = joblib.Parallel(n_jobs=n_jobs, backend=backend) return dict_concat(parallel( joblib.delayed(get_result)(fn, p) for p in pspace.iterate()))
def test_dict_concat(args, result): assert dict_concat(args) == result