1
1
#include " fdp/objects/distribution.hxx"
2
- #include " fdp/utilities/data_io.hxx"
3
2
#include " fdp/exceptions.hxx"
3
+ #include " fdp/utilities/data_io.hxx"
4
4
5
5
#include < vector>
6
6
7
7
namespace FairDataPipeline {
8
- Distribution::Distribution (std::string file_name){
9
- ghc::filesystem::path file_name_ (file_name);
10
- _component = get_first_component (ghc::filesystem::path (file_name_));
11
- read_from_toml (file_name_, _component);
12
- }
13
- Distribution::Distribution (std::string file_name, std::string component){
14
- ghc::filesystem::path file_name_ (file_name);
15
- this ->_component = component;
16
- read_from_toml (file_name_, _component);
8
+ Distribution::Distribution (const std::string &file_name) {
9
+ ghc::filesystem::path file_name_ (file_name);
10
+ _component = get_first_component (ghc::filesystem::path (file_name_));
11
+ read_from_toml (file_name_, _component);
12
+ }
13
+ Distribution::Distribution (const std::string &file_name,
14
+ const std::string &component)
15
+ : _component(component) {
16
+ ghc::filesystem::path file_name_ (file_name);
17
+ read_from_toml (file_name_, _component);
18
+ }
19
+
20
+ void Distribution::read_from_toml (const ghc::filesystem::path &file_path,
21
+ const std::string &component) {
22
+ auto data_ = read_component_from_toml (file_path, component);
23
+ if (!data_.contains (" type" )) {
24
+ throw toml_error (" Error Toml file: " + file_path.string () +
25
+ " does not contain a type tag" );
26
+ }
27
+
28
+ if (data_.at (" type" ).as_string () != " distribution" ) {
29
+ throw toml_error (" Error component: " + component +
30
+ " does not contain a distribution" );
31
+ }
32
+
33
+ std::vector<std::string> required_keys_{" distribution" , " mu" , " sigma" };
34
+
35
+ for (const auto &key : required_keys_) {
36
+ if (!data_.contains (key)) {
37
+ throw toml_error (" Error component: " + component +
38
+ " does not contain a " + key);
17
39
}
18
-
19
- void Distribution::read_from_toml (ghc::filesystem::path file_path, std::string component){
20
- auto data_ = read_component_from_toml (file_path, component);
21
- if (!data_.contains (" type" )){
22
- throw toml_error (" Error Toml file: " + file_path.string () + " does not contain a type tag" );
23
- }
24
-
25
- if (data_.at (" type" ).as_string () != " distribution" ){
26
- throw toml_error (" Error component: " + component + " does not contain a distribution" );
27
- }
28
-
29
- std::vector<std::string> required_keys_{" distribution" , " mu" , " sigma" };
30
-
31
- for (auto &key : required_keys_)
32
- {
33
- if (!data_.contains (key)){
34
- throw toml_error (" Error component: " + component + " does not contain a " + key);
35
- }
36
- }
37
-
38
- _name = data_.at (" distribution" ).as_string ();
39
-
40
-
41
- if (data_.at (" mu" ).is_floating ()){
42
- _mu = data_.at (" mu" ).as_floating ();
43
- }
44
- else if (data_.is_integer ()){
45
- _mu = static_cast <double >(data_.at (" mu" ).as_integer ());
46
- }
47
- else {
48
- throw toml_error (" Error mu value is not a number" );
49
- }
50
-
51
- if (data_.at (" sigma" ).is_floating ()){
52
- _sigma = data_.at (" sigma" ).as_floating ();
53
- }
54
- else if (data_.is_integer ()){
55
- _sigma = static_cast <double >(data_.at (" sigma" ).as_integer ());
56
- }
57
- else {
58
- throw toml_error (" Error sigma value is not a number" );
59
- }
60
-
61
-
62
- }
63
-
64
- std::string Distribution::write_to_toml (std::string &file_name){
65
- return write_to_toml (_component, file_name);
66
- }
67
-
68
- std::string Distribution::write_to_toml (std::string &component, std::string &file_name){
69
- const toml::value data_{
70
- {component, {{" type" , " distribution" },
71
- {" distribution" , _name},
72
- {" mu" , _mu},
73
- {" sigma" , _sigma}}}};
74
-
75
- return write_toml_data (ghc::filesystem::path (file_name), component, data_).string ();
76
-
77
- }
78
-
79
- bool Distribution::isEqual (const Distribution& dist) const {
80
- return this ->_component == dist._component &&
81
- this ->_mu == dist._mu &&
82
- this ->_name == dist._name &&
83
- this ->_sigma == dist._sigma ;
84
- }
85
-
86
- };
40
+ }
41
+
42
+ _name = data_.at (" distribution" ).as_string ();
43
+
44
+ if (data_.at (" mu" ).is_floating ()) {
45
+ _mu = data_.at (" mu" ).as_floating ();
46
+ } else if (data_.is_integer ()) {
47
+ _mu = static_cast <double >(data_.at (" mu" ).as_integer ());
48
+ } else {
49
+ throw toml_error (" Error mu value is not a number" );
50
+ }
51
+
52
+ if (data_.at (" sigma" ).is_floating ()) {
53
+ _sigma = data_.at (" sigma" ).as_floating ();
54
+ } else if (data_.is_integer ()) {
55
+ _sigma = static_cast <double >(data_.at (" sigma" ).as_integer ());
56
+ } else {
57
+ throw toml_error (" Error sigma value is not a number" );
58
+ }
59
+ }
60
+
61
+ std::string Distribution::write_to_toml (std::string &file_name) {
62
+ return write_to_toml (_component, file_name);
63
+ }
64
+
65
+ std::string Distribution::write_to_toml (std::string &component,
66
+ std::string &file_name) {
67
+ const toml::value data_{{component,
68
+ {{" type" , " distribution" },
69
+ {" distribution" , _name},
70
+ {" mu" , _mu},
71
+ {" sigma" , _sigma}}}};
72
+
73
+ return write_toml_data (ghc::filesystem::path (file_name), component, data_)
74
+ .string ();
75
+ }
76
+
77
+ bool Distribution::isEqual (const Distribution &dist) const {
78
+ return this ->_component == dist._component && this ->_mu == dist._mu &&
79
+ this ->_name == dist._name && this ->_sigma == dist._sigma ;
80
+ }
81
+
82
+ }; // namespace FairDataPipeline
0 commit comments