from pyNN import nest import sys import mozaik from mozaik.cli import parse_workflow_args from mozaik.controller import run_workflow, setup_logging from mozaik.tools.misc import result_directory_name from experiments import create_experiments from model import VogelsAbbott from mozaik.storage.datastore import Hdf5DataStore,PickledDataStore from analysis_and_visualization import perform_analysis_and_visualization from parameters import ParameterSet #mpi_comm = MPI.COMM_WORLD logger = mozaik.getMozaikLogger() simulation_name = "VogelsAbbott2005" simulation_run_name, _, _, _, modified_parameters = parse_workflow_args() if True: data_store,model = run_workflow(simulation_name,VogelsAbbott,create_experiments) model.connectors['ExcExcConnection'].store_connections(data_store) else: setup_logging() data_store = PickledDataStore( load=True, parameters=ParameterSet( { "root_directory": result_directory_name( simulation_run_name, simulation_name, modified_parameters ), "store_stimuli": False, }
def run_workflow(simulation_name, model_class, create_experiments): """ This is the main function that executes a workflow. It expects it gets the simulation, class of the model, and a function that will create_experiments. The create experiments function get a instance of a model as the only parameter and it is expected to return a list of Experiment instances that should be executed over the model. The run workflow will automatically parse the command line to determine the simulator to be used and the path to the root parameter file. It will also accept . (point) delimited path to parameteres in the configuration tree, and corresponding values. It will replace each such provided parameter's value with the provided one on the command line. Parameters ---------- simulation_name : str The name of the simulation. model_class : class The class from which the model instance will be created from. create_experiments : func The function that returns the list of experiments that will be executed on the model. Examples -------- The intended syntax of the commandline is as follows (note that the simulation run name is the last argument): >>> python userscript simulator_name num_threads parameter_file_path modified_parameter_path_1 modified_parameter_value_1 ... modified_parameter_path_n modified_parameter_value_n simulation_run_name """ ( simulation_run_name, simulator_name, num_threads, parameters_url, modified_parameters, ) = parse_workflow_args() print "Loading parameters" parameters = load_parameters(parameters_url, modified_parameters) print "Finished loading parameters" p = {} if parameters.has_key('mozaik_seed'): p['mozaik_seed'] = parameters['mozaik_seed'] if parameters.has_key('pynn_seed'): p['pynn_seed'] = parameters['pynn_seed'] print "START MPI" mozaik.setup_mpi(**p) # Read parameters exec "import pyNN.nest as sim" in globals(), locals() # Create results directory timestamp = datetime.now().strftime('%Y%m%d-%H%M%S') ddir = result_directory_name(simulation_run_name, simulation_name, modified_parameters) if mozaik.mpi_comm and mozaik.mpi_comm.rank != 0: Global.root_directory = parameters.results_dir + ddir + '/' + str( mozaik.mpi_comm.rank) + '/' mozaik.mpi_comm.barrier() else: Global.root_directory = parameters.results_dir + ddir + '/' os.makedirs(Global.root_directory) if mozaik.mpi_comm and mozaik.mpi_comm.rank == 0: mozaik.mpi_comm.barrier() if mozaik.mpi_comm.rank == 0: #let's store the full and modified parameters, if we are the 0 rank process parameters.save(Global.root_directory + "parameters", expand_urls=True) import pickle f = open(Global.root_directory + "modified_parameters", "w") pickle.dump(modified_parameters, f) f.close() setup_logging() model = model_class(sim, num_threads, parameters) if mozaik.mpi_comm.rank == 0: #let's store some basic info about the simulation run f = open(Global.root_directory + "info", "w") f.write( str({ 'model_class': str(model_class), 'model_docstring': model_class.__doc__, 'simulation_run_name': simulation_run_name, 'model_name': simulation_name, 'creation_data': datetime.now().strftime('%d/%m/%Y-%H:%M:%S') })) f.close() #import cProfile #cProfile.run('run_experiments(model,create_experiments(model),parameters)','stats_new') data_store = run_experiments(model, create_experiments(model), parameters) if mozaik.mpi_comm.rank == 0: data_store.save() import resource print "Final memory usage: %iMB" % ( resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / (1024)) return (data_store, model)
def prepare_workflow(simulation_name, model_class): """ Executes the following preparatory steps for simulation workflow: - Load simulation parameters - Initialize random seeds - Create directory for results - Store loaded parameters - Setup logging - Store some initial info about the simulation Returns ------- sim : module NEST module, to use for simulation num_threads : int Number of threads to use for the simulation parameters : dict Loaded parameters to initialize the simulation and model with """ ( simulation_run_name, simulator_name, num_threads, parameters_url, modified_parameters, ) = parse_workflow_args() # First we load the parameters just to retrieve seeds. We will throw them away, because at this stage the PyNNDistribution values were not yet initialized correctly. parameters = load_parameters(parameters_url, modified_parameters) p = {} if parameters.has_key('mozaik_seed'): p['mozaik_seed'] = parameters['mozaik_seed'] if parameters.has_key('pynn_seed'): p['pynn_seed'] = parameters['pynn_seed'] # Now initialize mpi with the seeds print "START MPI" mozaik.setup_mpi(**p) # Now really load parameters print "Loading parameters" parameters = load_parameters(parameters_url, modified_parameters) print "Finished loading parameters" exec "import pyNN.nest as sim" in globals(), locals() # Create results directory timestamp = datetime.now().strftime('%Y%m%d-%H%M%S') ddir = result_directory_name(simulation_run_name, simulation_name, modified_parameters) if mozaik.mpi_comm and mozaik.mpi_comm.rank != 0: Global.root_directory = parameters.results_dir + ddir + '/' + str( mozaik.mpi_comm.rank) + '/' mozaik.mpi_comm.barrier() else: Global.root_directory = parameters.results_dir + ddir + '/' os.makedirs(Global.root_directory) if mozaik.mpi_comm and mozaik.mpi_comm.rank == 0: mozaik.mpi_comm.barrier() if mozaik.mpi_comm.rank == 0: # Let's store the full and modified parameters, if we are the 0 rank process parameters.save(Global.root_directory + "parameters", expand_urls=True) import pickle f = open(Global.root_directory + "modified_parameters", "w") pickle.dump(modified_parameters, f) f.close() setup_logging() if mozaik.mpi_comm.rank == 0: # Let's store some basic info about the simulation run f = open(Global.root_directory + "info", "w") f.write( str({ 'model_class': str(model_class), 'model_docstring': model_class.__doc__, 'simulation_run_name': simulation_run_name, 'model_name': simulation_name, 'creation_data': datetime.now().strftime('%d/%m/%Y-%H:%M:%S') })) f.close() return sim, num_threads, parameters