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

Input guard for flagging used input (JSON entries), raising exception when some remain unused #395

Merged
merged 29 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5435793
Add: Basic InputGuard implementation
Griger5 Jan 3, 2025
11fcd1c
Merge branch 'main' of github.com:Griger5/PyPartMC into input-flagging
Griger5 Jan 3, 2025
68fb60d
Merge branch 'open-atmos:main' into input-flagging
Griger5 Jan 3, 2025
3dc6951
Add: Integrated InputGuard into the library's basic inner workflow
Griger5 Jan 3, 2025
3507e94
Merge branch 'input-flagging' of github.com:Griger5/PyPartMC into inp…
Griger5 Jan 3, 2025
3c5f50f
Fix: Updated tests, so they don't have unused parameters
Griger5 Jan 20, 2025
9fcf0b2
Add: InputGuard now processes reading of integers and arrays
Griger5 Jan 20, 2025
99d0acf
Add&fix: InputGuard now catches boolean values. Fixed typo in TestCon…
Griger5 Jan 21, 2025
99a8525
Fix: Deleted 'do_optical' from an initializing loop in RunPartOpt obj…
Griger5 Jan 21, 2025
0cb419d
Updated: Updated InputGuards inner logic, so it works properly with m…
Griger5 Feb 11, 2025
3024ee6
Update: Updated function calls for the new InputGuard
Griger5 Feb 11, 2025
6876613
Updated code style of InputGuard, so it matches the rest of the codebase
Griger5 Feb 11, 2025
7b539f6
Update: Moved InputGuard definition to seperate file
Griger5 Feb 27, 2025
6afd364
Update: Added try-catch statement to InputGuard's destructor, which p…
Griger5 Feb 27, 2025
be669ae
Add: Tests for InputGuard
Griger5 Feb 27, 2025
95b6066
Update: removed check_used_inputs from the InputGuard destructor, a J…
Griger5 Mar 13, 2025
e13cddd
Add: objects accepting JSONs now check if all inputs were parsed
Griger5 Mar 13, 2025
4afebb5
Add: added tests for all objects that check if inputs were used
Griger5 Mar 13, 2025
131060f
Add: add missing check_parameters calls
Griger5 Mar 13, 2025
9f387e7
Fix: pre-commit hooks
Griger5 Mar 13, 2025
f4d8504
Fix: added missing includes
Griger5 Mar 13, 2025
0f9de0d
Merge branch 'main' into input-flagging
slayoo Mar 13, 2025
413cb48
Fix: got rid of unused parameters in the running of PyPartMC model
Griger5 Mar 14, 2025
5625880
Merge branch 'main' into input-flagging
slayoo Mar 14, 2025
eaedb09
skip exception tests on arm64 + mark with a TODO label
slayoo Mar 15, 2025
6f8d09d
add missing import
slayoo Mar 15, 2025
5f4ab07
rerun additive intercomparison notebook to bring back Droplets.jl + a…
slayoo Mar 15, 2025
ed7006d
bring back a fishy pylint disable
slayoo Mar 15, 2025
6239aa5
add Gracjan to list of contributors in .zenodo.json
slayoo Mar 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update: Moved InputGuard definition to seperate file
  • Loading branch information
Griger5 committed Feb 27, 2025
commit 7b539f6784d56ac69513450dc546f1d7edade90c
155 changes: 155 additions & 0 deletions src/input_guard.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <iostream>
#include <set>
#include <map>
#include "pybind11_json/pybind11_json.hpp"
#include "nlohmann/json.hpp"

struct InputGuard {
public:
InputGuard(const nlohmann::json &j) {
process_json(j);

this->dict_key_present = false;
}

~InputGuard() {
check_used_inputs();
}

void mark_used_input(const std::string &input_name) {
if (this->prefixes.find(input_name) == this->prefixes.end()) {
std::string prefix = combine_str_vec(this->curr_prefix);

if (!prefix.empty()) {
this->used_inputs[prefix + "/" + input_name] = true;
}
else {
this->used_inputs[input_name] = true;
}
}
}

void update_dict_key(std::string dict_key) {
this->curr_dict_key = dict_key;
}

void check_read_line(std::string line) {
if (line == "mode_name") {
if (this->dict_key_present) {
this->curr_prefix.pop_back();
}
this->curr_prefix.push_back(this->curr_dict_key);
this->dict_key_present = true;
}
else if (line.empty()) {
this->curr_prefix.pop_back();
this->dict_key_present = false;
}
}

void open_spec_file(std::string spec_file_name) {
this->curr_prefix.push_back(spec_file_name);
}

void close_spec_file() {
this->curr_prefix.pop_back();
}

private:
std::map<std::string, bool> used_inputs;

std::set<std::string> prefixes;
std::string curr_dict_key;
std::vector<std::string> curr_prefix;

bool dict_key_present;

void check_used_inputs() {
for (auto item : this->used_inputs) {
if (!item.second) {
std::string err = std::string("Failed: \"") + item.first + std::string("\" parameter remains unused.");
throw std::runtime_error(err);
}
}
}

void process_json(const nlohmann::json &j) {
nlohmann::json flat = j.flatten();

// JSON Pointer, as in a string syntax for identifying a specific value in JSON
std::vector<std::string> json_pointers;

for (auto f : flat.items()) {
json_pointers.push_back(clean_string(f.key()));
}

std::set<std::string> json_pointers_set(json_pointers.begin(), json_pointers.end());

for (auto s : json_pointers_set) {
this->used_inputs[s] = false;
}

get_prefixes(json_pointers_set);
}

std::string clean_string(std::string str) {
bool after_slash = false;

for (size_t i = 0; i < str.size(); i++) {
if (str.at(i) == '/' && i+1 < str.size()) {
if (isdigit(str.at(i+1))) {
after_slash = true;
str.erase(i, 1);
i -= 1;
}
}
else if (isdigit(str.at(i)) && after_slash) {
str.erase(i, 1);
i -= 1;
}
else {
after_slash = false;
}
}

str.erase(0, 1);

return str;
}

std::string combine_str_vec(std::vector<std::string> vec) {
if (vec.size() == 0) return "";

std::string temp = vec[0];

for (size_t i = 1; i < vec.size(); i++) {
temp += "/";
temp += vec[i];
}

return temp;
}

void get_prefixes(std::set<std::string> json_pointers_set) {
std::string temp;
std::vector<std::string> temp_vec;

for (auto s : json_pointers_set) {
std::stringstream line(s);

while(getline(line, temp, '/')) {
temp_vec.push_back(temp);
}

if (temp_vec.size() > 1) {
temp_vec.pop_back();

for (auto v : temp_vec) {
this->prefixes.insert(v);
}
}

temp_vec.clear();
}
}
};
150 changes: 1 addition & 149 deletions src/json_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,155 +14,7 @@
#include "nlohmann/json.hpp"
#include <tcb/span.hpp>
#include <bpstd/string_view.hpp>

struct InputGuard {
public:
InputGuard(const nlohmann::json &j) {
process_json(j);

this->dict_key_present = false;
}

~InputGuard() {
check_used_inputs();
}

void mark_used_input(const std::string &input_name) {
if (this->prefixes.find(input_name) == this->prefixes.end()) {
std::string prefix = combine_str_vec(this->curr_prefix);

if (!prefix.empty()) {
this->used_inputs[prefix + "/" + input_name] = true;
}
else {
this->used_inputs[input_name] = true;
}
}
}

void update_dict_key(std::string dict_key) {
this->curr_dict_key = dict_key;
}

void check_read_line(std::string line) {
if (line == "mode_name") {
if (this->dict_key_present) {
this->curr_prefix.pop_back();
}
this->curr_prefix.push_back(this->curr_dict_key);
this->dict_key_present = true;
}
else if (line.empty()) {
this->curr_prefix.pop_back();
this->dict_key_present = false;
}
}

void open_spec_file(std::string spec_file_name) {
this->curr_prefix.push_back(spec_file_name);
}

void close_spec_file() {
this->curr_prefix.pop_back();
}

private:
std::map<std::string, bool> used_inputs;

std::set<std::string> prefixes;
std::string curr_dict_key;
std::vector<std::string> curr_prefix;

bool dict_key_present;

void check_used_inputs() {
for (auto item : this->used_inputs) {
if (!item.second) {
throw std::logic_error(std::string("Failed: \"") + item.first + std::string("\" parameter remains unused."));
}
}
}

void process_json(const nlohmann::json &j) {
nlohmann::json flat = j.flatten();

// JSON Pointer, as in a string syntax for identifying a specific value in JSON
std::vector<std::string> json_pointers;

for (auto f : flat.items()) {
json_pointers.push_back(clean_string(f.key()));
}

std::set<std::string> json_pointers_set(json_pointers.begin(), json_pointers.end());

for (auto s : json_pointers_set) {
this->used_inputs[s] = false;
}

get_prefixes(json_pointers_set);
}

std::string clean_string(std::string str) {
bool after_slash = false;

for (size_t i = 0; i < str.size(); i++) {
if (str.at(i) == '/' && i+1 < str.size()) {
if (isdigit(str.at(i+1))) {
after_slash = true;
str.erase(i, 1);
i -= 1;
}
}
else if (isdigit(str.at(i)) && after_slash) {
str.erase(i, 1);
i -= 1;
}
else {
after_slash = false;
}
}

str.erase(0, 1);

return str;
}

std::string combine_str_vec(std::vector<std::string> vec) {
if (vec.size() == 0) return "";

std::string temp = vec[0];

for (size_t i = 1; i < vec.size(); i++) {
temp += "/";
temp += vec[i];
}

return temp;
}

void get_prefixes(std::set<std::string> json_pointers_set) {
std::string temp;
std::vector<std::string> temp_vec;

for (auto s : json_pointers_set) {
std::stringstream line(s);

while(getline(line, temp, '/')) {
temp_vec.push_back(temp);
}

if (temp_vec.size() > 1) {
temp_vec.pop_back();

for (auto v : temp_vec) {
this->prefixes.insert(v);
}
}

temp_vec.clear();
}
}
};
#include "input_guard.hpp"

struct JSONResource {
private:
Expand Down
Loading