Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#61: Create configuration validator #119

Merged
merged 9 commits into from
Sep 27, 2024
45 changes: 45 additions & 0 deletions bindings/python/config_validator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "config_validator.h"

namespace vt::tv::bindings::python {

/**
* Check if the configuration file is valid
*
* @return true if the configuration is valid
*/
bool ConfigValidator::isValid()
{
bool is_valid = true;
for (std::string requiredParameter: required_parameters) {
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
if (!config[requiredParameter]) {
is_valid = false;
break;
}
}
return is_valid;
}


/**
* Get the list of missing parameters
*
* @return A string containing the list of the missing parameters
*/
std::string ConfigValidator::getMissingRequiredParameters()
{
int i = 0;
std::string parameters;
for (std::string requiredParameter: required_parameters) {
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
if (!config[requiredParameter]) {
if (i == 0 ) {
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
parameters = parameters + requiredParameter;
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
} else {
parameters = parameters + ", " + requiredParameter;
}
i++;
}
}
return parameters;
}

} /* end namespace vt::tv::bindings::python */
65 changes: 65 additions & 0 deletions bindings/python/config_validator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
//@HEADER
// *****************************************************************************
//
// config_validator.h
// DARMA/vt-tv => Virtual Transport -- Task Visualizer
//
// Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact [email protected]
//
// *****************************************************************************
//@HEADER
*/
// A2DD.h
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
#ifndef vt_tv_config_validator_h
#define vt_tv_config_validator_h

#include <yaml-cpp/yaml.h>

namespace vt::tv::bindings::python {
/**
* ConfigValidator Class
*/
class ConfigValidator
{
public:
std::array<std::string, 2> required_parameters = {"output_visualization_dir", "output_visualization_file_stem"};
YAML::Node config;
bool isValid();
std::string getMissingRequiredParameters();
ConfigValidator(YAML::Node in_config)
:config(in_config) {}
};
}

#endif
11 changes: 11 additions & 0 deletions bindings/python/tv.cc
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tv.h"
#include "config_validator.h"

namespace vt::tv::bindings::python {

Expand All @@ -17,6 +18,16 @@ void tvFromJson(const std::vector<std::string>& input_json_per_rank_list, const
// Load the configuration from serialized YAML
YAML::Node viz_config = YAML::Load(input_yaml_params_str);

// Config Validator
ConfigValidator config_validator(viz_config);

// Check configuration
bool is_config_valid = config_validator.isValid();

// Throw error if configuration is invalid
if (!is_config_valid) {
throw std::runtime_error("The YAML configuration file is not valid: missing required paramaters: " + config_validator.getMissingRequiredParameters());
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
}

std::array<std::string, 3> qoi_request = {
viz_config["rank_qoi"].as<std::string>(),
Expand Down
29 changes: 18 additions & 11 deletions tests/test_bindings.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
"""This module calls vttv module to test that vttv bindings work as expected"""
import json
import os

import json
import sys
import yaml
import vttv


# source dir is the directory a level above this file
source_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Read the YAML config file
with open(f'{source_dir}/tests/test_bindings_conf.yaml', 'r', encoding='utf-8') as stream:
try:
params = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
exit(1)

# Check main key is "visualization"
if "visualization" not in params:
print("The YAML configuration file is not valid: missing required paramaters: visualization")
sys.exit(1)
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved

# make output_visualization_dir directory parameter absolute
if not os.path.isabs(params["visualization"]["output_visualization_dir"]):
params["visualization"]["output_visualization_dir"] = source_dir + \
"/" + params["visualization"]["output_visualization_dir"]
if "output_visualization_dir" in params["visualization"]:
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
if not os.path.isabs(params["visualization"]["output_visualization_dir"]):
params["visualization"]["output_visualization_dir"] = source_dir + \
"/" + params["visualization"]["output_visualization_dir"]

# Serialize visualization parameters
params_serialized = yaml.dump(params["visualization"])

# Calcul n_ranks
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
n_ranks = params["visualization"]["x_ranks"] * \
params["visualization"]["y_ranks"] * params["visualization"]["z_ranks"]
rank_data = []

rank_data = []
for rank in range(n_ranks):
with open(f'{source_dir}/data/lb_test_data/data.{rank}.json', 'r', encoding='utf-8') as f:
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
data = json.load(f)

data_serialized = json.dumps(data)

rank_data.append((data_serialized))
# Add serialized data into the rank
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
rank_data.append((json.dumps(data)))

# Launch VT TV from JSON data
vttv.tvFromJson(rank_data, params_serialized, n_ranks)
Loading