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: requiredParameters) {
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: requiredParameters) {
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 */
66 changes: 66 additions & 0 deletions bindings/python/config_validator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
//@HEADER
// *****************************************************************************
//
// config_validator.h
// DARMA/vt-tv => Virtual Transport -- Task Visualizer
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
// (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 {
/**
* ConfiValidator Class
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
*/
class ConfigValidator
{
public:
std::array<std::string, 2> requiredParameters = {"output_visualization_dir", "output_visualization_file_stem"};
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
YAML::Node config;
bool isValid();
std::string getMissingRequiredParameters();
ConfigValidator(YAML::Node configData) {
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
config = configData;
}
};
}

#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 not valid
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
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
44 changes: 29 additions & 15 deletions tests/test_bindings.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
'''TESTS BINDING'''
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved

import os
import json
import yaml
import vttv

import sys
import os

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') as stream:
try:
params = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
try:
params = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)

# Check main key is "visualization"
if 'visualization' not in params:
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
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
params["visualization"]["output_visualization_dir"] = os.path.abspath(params["visualization"]["output_visualization_dir"])
if 'output_visualization_dir' in params["visualization"]:
params["visualization"]["output_visualization_dir"] = os.path.abspath(
params["visualization"]["output_visualization_dir"]
)

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

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

# Prepare rank data
rank_data = []
for rank in range(n_ranks):
with open(f'{source_dir}/data/lb_test_data/data.{rank}.json', 'r') as f:
data = json.load(f)

data_serialized = json.dumps(data)
with open(f'{source_dir}/data/lb_test_data/data.{rank}.json', 'r') as f:
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
data = json.load(f)

rank_data.append((data_serialized))
data_serialized = json.dumps(data)
rank_data.append((data_serialized))

# Launch
maxime-bfsquall marked this conversation as resolved.
Show resolved Hide resolved
vttv.tvFromJson(rank_data, params_serialized, n_ranks)