예제 #1
0
    def reset_transfer_functions(sim):
        """
        Reset transfer functions
        """
        # Delete all TFs
        old_tfs, _ = sim.cle.get_simulation_transfer_functions()
        for tf in old_tfs:
            sim.cle.delete_simulation_transfer_function(get_tf_name(tf))

        experiment_basepath = os.path.join(get_experiment_basepath(),
                                           sim.experiment_conf)
        _, bibi_conf = get_experiment_data(str(experiment_basepath))
        if bibi_conf is None:
            return

        # Reset Transfer functions
        import_referenced_python_tfs(bibi_conf,
                                     os.path.dirname(experiment_basepath))

        for tf in bibi_conf.transferFunction:
            tf_code = generate_tf(tf, bibi_conf)
            tf_code = correct_indentation(tf_code, 0)
            tf_code = tf_code.strip() + "\n"

            error_message = sim.cle.add_simulation_transfer_function(
                str(tf_code))
            if error_message:
                raise NRPServicesTransferFunctionException(
                    "Transfer function patch failed: " + str(error_message) +
                    "\n" + "Updated source:\n" + str(tf_code))
예제 #2
0
    def reset_state_machines(sim):
        """
        Reset states machines
        """

        sim.delete_all_state_machines()

        sm_base_path = get_experiment_basepath()
        experiment_basepath = os.path.join(sm_base_path, sim.experiment_conf)
        experiment, _ = get_experiment_data(str(experiment_basepath))
        if experiment is None:
            return

        state_machine_paths = {}
        if experiment.experimentControl is not None:
            state_machine_paths.update({
                sm.id: os.path.join(os.path.dirname(experiment_basepath),
                                    sm.src)
                for sm in experiment.experimentControl.stateMachine
                if isinstance(sm, exp_conf_api_gen.SMACHStateMachine)
            })

        if experiment.experimentEvaluation is not None:
            state_machine_paths.update({
                sm.id: os.path.join(os.path.dirname(experiment_basepath),
                                    sm.src)
                for sm in experiment.experimentEvaluation.stateMachine
                if isinstance(sm, exp_conf_api_gen.SMACHStateMachine)
            })

        sim.state_machine_manager.add_all(state_machine_paths, sim.sim_id)
        sim.state_machine_manager.initialize_all()
예제 #3
0
    def reset_brain(sim):
        """
        Reset populations

        :param sim:
        """

        experiments_base_path = os.path.join(get_experiment_basepath(),
                                             sim.experiment_conf)
        models_base_path = get_model_basepath()
        _, bibi_conf = get_experiment_data(str(experiments_base_path))
        if bibi_conf is None:
            return

        neurons_config = get_all_neurons_as_dict(
            bibi_conf.brainModel.populations)

        # Convert the populations to a JSON dictionary
        for (name, s) in neurons_config.iteritems():
            v = dict()
            v['from'] = s.start
            v['to'] = s.stop
            if s.step <= 0:
                v['step'] = 1
            else:
                v['step'] = s.step

            neurons_config[name] = v

        neurons_config = json.dumps(neurons_config)
        if 'storage://' in bibi_conf.brainModel.file:
            for entry in os.listdir(tempfile.gettempdir()):
                if entry.startswith('nrpTemp'):
                    brain_path = os.path.join(
                        tempfile.gettempdir(), entry,
                        os.path.basename(bibi_conf.brainModel.file))
        else:
            brain_path = os.path.join(models_base_path,
                                      bibi_conf.brainModel.file)

        with open(brain_path, 'r') as myfile:
            data = myfile.read()
            DO_CHANGE_POPULATION = 1
            result = sim.cle.set_simulation_brain('py', data, "text",
                                                  neurons_config,
                                                  DO_CHANGE_POPULATION)
            if result.error_message is not "":
                # Error in given brain
                raise ROSCLEClientException(
                    '{}, line:{}, column:{}, population_change:{}'.format(
                        result.error_message, result.error_line,
                        result.error_column, result.handle_population_change))
예제 #4
0
    def get(self, exp_id):
        """
        Get preview image of the experiment specified with experiment ID.

        :param exp_id: The experiment ID

        :> json image_as_base64: The PNG image as base64

        :status 500: {0}
        :status 404: {1} or {2}
        :status 200: Success. The preview image is returned
        """

        # Check experiment
        experiment_file_path = os.path.join(get_experiment_basepath(),
                                            get_experiment_rel(exp_id))
        if not os.path.isfile(experiment_file_path):
            raise NRPServicesClientErrorException(
                ErrorMessages.EXPERIMENT_CONF_FILE_NOT_FOUND_404,
                error_code=404)

        experiment_dir = os.path.split(experiment_file_path)[0]
        # Parse the experiment XML and get the thumbnail path
        with open(experiment_file_path) as exd_file:
            try:
                experiment_file = exp_conf_api_gen.CreateFromDocument(
                    exd_file.read())
                preview_file = os.path.join(experiment_dir,
                                            experiment_file.thumbnail)
            except ValidationError:
                raise NRPServicesClientErrorException(
                    ErrorMessages.EXPERIMENT_CONF_FILE_INVALID_500,
                    error_code=500)

        # Check thumbnail
        if not os.path.isfile(preview_file):
            raise NRPServicesClientErrorException(
                ErrorMessages.EXPERIMENT_PREVIEW_NOT_FOUND_404, error_code=404)
        mime_type = mimetypes.guess_type(preview_file)[
            0]  # returns tuple (type, encoding)
        if not mime_type or not mime_type.startswith('image'):
            raise NRPServicesClientErrorException(
                ErrorMessages.EXPERIMENT_PREVIEW_INVALID_500, error_code=500)

        with open(preview_file, "rb") as _file:
            data = _file.read()

        # Base64
        return dict(image_as_base64=base64.b64encode(data))
예제 #5
0
 def test_get_experiment_basepath_ok(self, mock_os):
     mock_os.environ.get.return_value = "/test1"
     self.assertEqual("/test1", get_experiment_basepath())