Ejemplo n.º 1
0
def perturbateAndAnalyzeFromJsonToPath(json_file_path, dest_folder_path):

    dest_folder_path += "/" + "results/"

    # Read JSON
    logger.debug('Read JSON')
    logger.debug(json_file_path)
    logger.debug(dest_folder_path)
    with open(json_file_path, 'r') as fp:
        full_json = json.load(fp)
    
    # Get model file
    model_file_path = files_aux.moFilePathFromJSONMoPath(full_json["model_mo_path"])
    
    # Horcoded for now
    base_dir = "/home/omsens/Documents/results_experiments/logging/"    # HARCODED, ISNT USED RIGHT NOW IN SWEEP
    results_dirname = base_dir + full_json["model_name"] + '_res.csv'   # HARCODED, ISNT USED RIGHT NOW IN SWEEP
    
    perturbateAndAnalyze_kwargs = {
        "model_name"            : full_json["model_name"],
        "model_file_path"       : model_file_path,
        "start_time"            : full_json["start_time"],
        "stop_time"             : full_json["stop_time"],
        "parameters_to_perturb" : full_json["parameters_to_perturb"],
        "percentage"            : full_json["percentage"],
        "target_vars"           : full_json["vars_to_analyze"],
        "dest_folder_path"      : dest_folder_path,
        "base_dir"              : base_dir,
        "results_dirname"       : results_dirname
    }
    logger.debug('Init perturbation')
    logger.debug(perturbateAndAnalyze_kwargs)
    perturbateAndAnalyze(**perturbateAndAnalyze_kwargs)
Ejemplo n.º 2
0
def perturbateAndAnalyzeFromJsonToPath(json_file_path, dest_folder_path):
    # Read JSON
    with open(json_file_path, 'r') as fp:
        full_json = json.load(fp)
    model_file_path = files_aux.moFilePathFromJSONMoPath(
        full_json["model_mo_path"])
    perturbateAndAnalyze_kwargs = {
        "model_name": full_json["model_name"],
        "model_file_path": model_file_path,
        "start_time": full_json["start_time"],
        "stop_time": full_json["stop_time"],
        "parameters_to_perturb": full_json["parameters_to_perturb"],
        "percentage": full_json["percentage"],
        "target_vars": full_json["vars_to_analyze"],
        "dest_folder_path": dest_folder_path,
    }
    perturbateAndAnalyze(**perturbateAndAnalyze_kwargs)
Ejemplo n.º 3
0
def sweepAndPlotFromJSON(dest_folder_path, json_file_path):
    with open(json_file_path, 'r') as fp:
        full_json = json.load(fp)
    # Prepare sweep init args
    model_mo_path = files_aux.moFilePathFromJSONMoPath(
        full_json["model_mo_path"])
    sweep_kwargs = \
        {
            "model_name"                  : full_json["model_name"],
            "model_file_path"             : model_mo_path,
            "start_time"                  : full_json["start_time"],
            "stop_time"                   : full_json["stop_time"],
            "perturbation_info_per_param" : full_json["parameters_to_sweep"],
            "fixed_params"                : full_json["fixed_params"],
            "build_folder_path"           : dest_folder_path,

        }
    # Initialize sweeper
    sweep_runner = running.sweep.ParametersSweeper(**sweep_kwargs)
    # Run sweep
    sweep_results = sweep_runner.runSweep(dest_folder_path)
    # Initialize sweep results plotter
    sweep_plotter = plot_sweep.SweepPlotter(sweep_results)
    # Make folder for plots
    plot_folder_path = os.path.join(dest_folder_path, "plots")
    files_aux.makeFolderWithPath(plot_folder_path)
    # Plot sweep for each var
    vars_plots_paths = {}
    for var_name in full_json["vars_to_analyze"]:
        plot_path = sweep_plotter.plotInFolder(var_name, plot_folder_path)
        vars_plots_paths[var_name] = plot_path
    # Add sweep plots to paths dict
    paths_dict = \
        {
            "sweep_plots": vars_plots_paths,
        }
    # Write paths dict as json
    paths_json_str = json.dumps(paths_dict, sort_keys=True, indent=2)
    paths_json_file_name = "result.json"
    paths_json_file_path = os.path.join(dest_folder_path, paths_json_file_name)
    files_aux.writeStrToFile(paths_json_str, paths_json_file_path)
    logger.info("Finished. The file {0} has all the sweep files paths.".format(
        paths_json_file_path))
Ejemplo n.º 4
0
def analyzeFromJSON(dest_folder_path, json_file_path):

    # Define destination folder path
    dest_folder_path += "/" + "results/"
    with open(json_file_path, 'r') as fp:
        full_json = json.load(fp)

    # Prepare init args
    model_mo_path = files_aux.moFilePathFromJSONMoPath(
        full_json["model_mo_path"])

    # Note: Check that the string in 'restriction_path' is effectively a directory'
    base_dir = full_json["restriction_path"]
    results_dirname = base_dir + full_json["model_name"] + '_res.csv'

    # Future work: implement 'intelligent alpha' so that the user doesn't need to iterate every time
    plot_std = full_json["plot_std_run"]
    plot_restriction = full_json["plot_restriction"]

    optim_kwargs = {
        "model_file_path": model_mo_path,
        "build_folder_path": dest_folder_path,
        "target_var_name": full_json["target_var_name"],
        "parameters_to_perturb": full_json["parameters_to_perturb"],
        "model_name": full_json["model_name"],
        "start_time": full_json["start_time"],
        "stop_time": full_json["stop_time"],
        "max_or_min": full_json["max_or_min"],
        "objective_function_name": full_json["objective_function_name"],
        "optimizer_name": full_json["optimizer_name"],
        "alpha_value": full_json["alpha_value"],
        "constrained_time_path_file": full_json["constrained_time_path_file"],
        "constrained_variable": full_json["constrained_variable"],
        "constrained_epsilon": full_json["constrained_epsilon"],
        "base_dir": base_dir,
        "results_dirname": results_dirname
    }
    # logger.debug('Generated optim_kwargs')

    # Initialize builder and compile model:
    # We compile the model again for now to avoid having remnants of the optimization in its compiled model
    plots_folder_name = "plots"
    plots_folder_path = os.path.join(dest_folder_path, plots_folder_name)
    files_aux.makeFolderWithPath(plots_folder_path)

    # Make sub-folder for new model. in Windows we can't use "aux" for folder names
    model_folder_name = "aux_folder"
    model_folder_path = os.path.join(plots_folder_path, model_folder_name)
    files_aux.makeFolderWithPath(model_folder_path)
    model_builder = build_model.ModelicaModelBuilder(full_json["model_name"],
                                                     full_json["start_time"],
                                                     full_json["stop_time"],
                                                     model_mo_path)
    compiled_model = model_builder.buildToFolderPath(model_folder_path,
                                                     base_dir, results_dirname)

    # Run STD Model
    x0_csv_name = "x0_run.csv"
    x0_csv_path = os.path.join(model_folder_path, x0_csv_name)
    x0_dict = {
        p: compiled_model.defaultParameterValue(p)
        for p in full_json["parameters_to_perturb"]
    }
    compiled_model.simulate(x0_csv_path, params_vals_dict=x0_dict)
    df_x0_run = pd.read_csv(x0_csv_path, index_col=False)
    df_x0_run.to_csv(results_dirname, index=False)
    # logger.debug('Generated df_x0_run')

    # Run optimization and re-run with optimal parameters
    model_optimizer = model_optimizer_f.ModelOptimizer(**optim_kwargs)
    # logger.debug('model optimizer generated')

    optim_result = model_optimizer.optimize(full_json["percentage"],
                                            full_json["epsilon"])
    x_opt_csv_name = "x_opt_run.csv"
    x_opt_csv_path = os.path.join(model_folder_path, x_opt_csv_name)
    compiled_model.simulate(x_opt_csv_path,
                            params_vals_dict=optim_result.x_opt)
    df_x_opt_run = pd.read_csv(x_opt_csv_path)

    # Update optimum value of 'optim_result'. IMPORTANT: assumes the last contents of csv file is the optimum
    final_optimum_value = df_x_opt_run[
        full_json["target_var_name"]].tolist()[-1]
    optim_result.target_optimum = final_optimum_value

    # Plot in folder
    var_optimization = full_json["target_var_name"]
    var_restriction = full_json['constrained_variable']
    if plot_std and plot_restriction:
        restriction = pd.read_csv(full_json["constrained_time_path_file"],
                                  index_col=False)
        restriction = restriction[
            restriction['time'] <= df_x_opt_run['time'].max()]
        vect_plotter = plot_vect_f.VectorialPlotter(optim_result, df_x_opt_run,
                                                    df_x0_run, restriction,
                                                    var_optimization,
                                                    var_restriction)
    elif plot_std:
        vect_plotter = plot_vect_f.VectorialPlotter(optim_result, df_x_opt_run,
                                                    df_x0_run, None,
                                                    var_optimization, None)
    elif plot_restriction:
        restriction = pd.read_csv(full_json["constrained_time_path_file"],
                                  index_col=False)
        restriction = restriction[
            restriction['time'] <= df_x_opt_run['time'].max()]
        vect_plotter = plot_vect_f.VectorialPlotter(optim_result, df_x_opt_run,
                                                    None, restriction,
                                                    var_optimization,
                                                    var_restriction)
    else:
        raise Exception(
            "Please choose a plot option. We are still working on making this choice automatically."
        )

    # Plot
    plot_path = vect_plotter.plotInFolder(plots_folder_path)

    # Prepare JSON output dict
    vect_json_dict = {
        "x0": optim_result.x0,
        "x_opt": optim_result.x_opt,
        "f(x0)": optim_result.f_x0,
        "f(x)_opt": optim_result.target_optimum,
        "stop_time": optim_result.stop_time,
        "variable": optim_result.variable_name,
        "plot_path": plot_path,
    }
    # Write dict as json
    optim_json_str = json.dumps(vect_json_dict, sort_keys=True, indent=2)
    optim_json_file_name = "result.json"
    optim_json_file_path = os.path.join(dest_folder_path, optim_json_file_name)
    files_aux.writeStrToFile(optim_json_str, optim_json_file_path)
Ejemplo n.º 5
0
def sweepAndPlotFromJSON(dest_folder_path_base,
                         json_file_path,
                         communicator=None):
    logger.debug("Entra en sweepAndPlotFromJSON")
    dest_folder_path = dest_folder_path_base + "/" + "results/"
    with open(json_file_path, 'r') as fp:
        full_json = json.load(fp)

    # Prepare sweep init args
    model_mo_path = files_aux.moFilePathFromJSONMoPath(
        full_json["model_mo_path"])

    # Horcoded for now
    base_dir = "/home/omsens/Documents/results_experiments/logging/"  # HARCODED, ISNT USED RIGHT NOW IN SWEEP
    results_dirname = base_dir + full_json[
        "model_name"] + '_res.csv'  # HARCODED, ISNT USED RIGHT NOW IN SWEEP

    # Fetch upper/lower bounds
    with_upper_and_lower = full_json["plot_upper_lower_limit"]

    # Set kwargs
    logger.debug("setting kwargs")
    sweep_kwargs = \
        {
            "model_name"                  : full_json["model_name"],
            "model_file_path"             : model_mo_path,
            "start_time"                  : full_json["start_time"],
            "stop_time"                   : full_json["stop_time"],
            "perturbation_info_per_param" : full_json["parameters_to_sweep"],
            "fixed_params"                : full_json["fixed_params"],
            "build_folder_path"           : dest_folder_path,

            "base_dir"                    : base_dir,
            "results_dirname"             : results_dirname
        }

    # Initialize sweeper
    logger.debug("Initialize sweeper")
    logger.debug(sweep_kwargs)
    sweep_runner = running.sweep.ParametersSweeper(**sweep_kwargs)

    # Run sweep
    logger.debug("Run sweep")
    sweep_results, perturbed_param_run_id_map = sweep_runner.runSweep(
        dest_folder_path, communicator)
    logger.debug(perturbed_param_run_id_map)

    # Initialize sweep results plotter
    logger.debug("Sweep plotter")
    sweep_plotter = plot_sweep.SweepPlotter(sweep_results)
    # Make folder for plots
    logger.debug("Goto plot folder path")
    plot_folder_path = os.path.join(dest_folder_path, "plots")
    files_aux.makeFolderWithPath(plot_folder_path)

    # Save results.json
    logger.debug("Save results.json")
    vars_plots_paths = {}
    for var_name in full_json['vars_to_analyze']:
        plot_path_without_extension = os.path.join(plot_folder_path, var_name)
        vars_plots_paths[var_name] = plot_path_without_extension
    paths_dict = {"sweep_plots": vars_plots_paths}
    paths_json_str = json.dumps(paths_dict, sort_keys=True, indent=2)
    paths_json_file_name = "result.json"
    paths_json_file_path = os.path.join(dest_folder_path, paths_json_file_name)
    files_aux.writeStrToFile(paths_json_str, paths_json_file_path)

    # Generate plots
    # logger.debug("Generate plots")
    for var_name in full_json["vars_to_analyze"]:
        # logger.debug("Var to analyze: " + var_name)
        # logger.debug(vars_plots_paths)
        # logger.debug(var_name in vars_plots_paths.keys())
        plot_path_without_extension = vars_plots_paths[var_name]
        # logger.debug("Call sweep plotter plot in folder")
        sweep_plotter.plotInFolder(var_name, plot_path_without_extension,
                                   with_upper_and_lower)

    # Paramters run save
    logger.debug("Parameters run save")
    params_df = pd.DataFrame()
    for param_comb, run_id in perturbed_param_run_id_map.items():

        logger.debug("parameter combination")
        logger.debug(param_comb)

        params = {
            v[0]: v[1]
            for v in [x.split(":") for x in param_comb.split(",")]
        }
        params['run_id'] = run_id
        df_row = pd.DataFrame(data=params, index=[0])
        if params_df.shape[0] == 0:
            params_df = df_row
        else:
            params_df = params_df.append(df_row)
        params_df = params_df.reset_index().drop('index', 1)
    params_df.to_csv(dest_folder_path + '/' + 'parameters_run.csv',
                     index=False)

    with open(dest_folder_path_base + '/' + 'model_info.json') as f:
        model_info = json.load(f)
        variables = model_info['aux_variables']
        pd.DataFrame(columns=variables).to_csv(dest_folder_path + '/' +
                                               'variables.csv',
                                               index=False)

    logger.debug("Termina en sweepAndPlotFromJSON")