16 #include "variables.h"
47 set(new_variables_number);
61 set(new_inputs_number, new_targets_number);
73 set(variables_document);
112 if(
this != &other_variables)
175 if(i >= variables_number)
177 std::ostringstream buffer;
179 buffer <<
"OpenNN Exception: Variables class.\n"
180 <<
"const Item& get_item(const size_t&) method.\n"
181 <<
"Index of variable must be less than number of variables.\n";
183 throw std::logic_error(buffer.str());
219 return(variables_number - unused_variables_number);
233 for(
size_t i = 0; i < variables_number; i++)
235 if(
items[i].use == Unused)
255 for(
size_t i = 0; i < variables_number; i++)
257 if(
items[i].use == Input)
277 for(
size_t i = 0; i < variables_number; i++)
279 if(
items[i].use == Target)
301 for(
size_t i = 0; i < variables_number; i++)
303 if(
items[i].use == Input)
307 else if(
items[i].use == Target)
332 for(
size_t i = 0; i < variables_number; i++)
334 uses[i] =
items[i].use;
353 for(
size_t i = 0; i < variables_number; i++)
355 if(
items[i].use == Unused)
357 uses_string[i] =
"Unused";
359 else if(
items[i].use == Input)
361 uses_string[i] =
"Input";
363 else if(
items[i].use == Target)
365 uses_string[i] =
"Target";
369 std::ostringstream buffer;
371 buffer <<
"OpenNN Exception Variables class.\n"
372 <<
"Vector<std::string> write_uses(void) const method.\n"
375 throw std::logic_error(buffer.str());
390 return(
items[i].use);
401 if(
items[i].use == Unused)
405 else if(
items[i].use == Input)
409 else if(
items[i].use == Target)
415 std::ostringstream buffer;
417 buffer <<
"OpenNN Exception Variables class.\n"
418 <<
"std::string write_use(const size_t&) const method.\n"
421 throw std::logic_error(buffer.str());
439 if(index >= variables_number)
441 std::ostringstream buffer;
443 buffer <<
"OpenNN Exception: Variables class.\n"
444 <<
"bool is_used(const size_t&) const method.\n"
445 <<
"Index of variable must be less than number of variables.\n";
447 throw std::logic_error(buffer.str());
452 if(
items[index].use == Variables::Unused)
477 for(
size_t i = 0; i < variables_number; i++)
479 information(i,0) =
items[i].name;
480 information(i,1) =
items[i].units;
481 information(i,2) =
items[i].description;
502 for(
size_t i = 0; i < variables_number; i++)
504 if(
items[i].use == Input)
506 inputs_indices[index] = i;
511 return(inputs_indices);
528 for(
size_t i = 0; i < variables_number; i++)
530 if(
items[i].use == Target)
532 targets_indices[index] = i;
537 return(targets_indices);
551 for(
size_t i = 0; i < variables_number; i++)
553 names[i] =
items[i].name;
573 if(i >= variables_number)
575 std::ostringstream buffer;
577 buffer <<
"OpenNN Exception: Variables class.\n"
578 <<
"std::string& get_name(size_t) method.\n"
579 <<
"Index of variable must be less than number of variables.\n";
581 throw std::logic_error(buffer.str());
586 return(
items[i].name);
598 for(
size_t i = 0; i < variables_number; i++)
600 if(
items[i].name.empty())
625 for(
size_t i = 0; i < inputs_number; i++)
627 index = inputs_indices[i];
629 inputs_name[i] =
items[index].name;
650 for(
size_t i = 0; i < targets_number; i++)
652 index = targets_indices[i];
654 targets_name[i] =
items[index].name;
657 return(targets_name);
671 for(
size_t i = 0; i < variables_number; i++)
673 units[i] =
items[i].units;
693 if(i >= variables_number)
695 std::ostringstream buffer;
697 buffer <<
"OpenNN Exception: Variables class.\n"
698 <<
"const std::string& get_units(const size_t&) const method.\n"
699 <<
"Index of variable must be less than number of variables.\n";
701 throw std::logic_error(buffer.str());
706 return(
items[i].units);
724 for(
size_t i = 0; i < inputs_number; i++)
726 index = inputs_indices[i];
728 inputs_units[i] =
items[index].units;
731 return(inputs_units);
749 for(
size_t i = 0; i < targets_number; i++)
751 index = targets_indices[i];
753 targets_units[i] =
items[index].units;
756 return(targets_units);
770 for(
size_t i = 0; i < variables_number; i++)
772 descriptions[i] =
items[i].description;
775 return(descriptions);
792 if(i >= variables_number)
794 std::ostringstream buffer;
796 buffer <<
"OpenNN Exception: Variables class.\n"
797 <<
"const std::string& get_description(const size_t&) const method.\n"
798 <<
"Index of variable must be less than number of variables.\n";
800 throw std::logic_error(buffer.str());
805 return(
items[i].description);
823 for(
size_t i = 0; i < inputs_number; i++)
825 index = inputs_indices[i];
827 inputs_description[i] =
items[index].description;
830 return(inputs_description);
848 for(
size_t i = 0; i < targets_number; i++)
850 index = targets_indices[i];
852 target_descriptions[i] =
items[index].description;
855 return(target_descriptions);
874 for(
size_t i = 0; i < inputs_number; i++)
876 input_index = inputs_indices[i];
878 inputs_information(i,0) =
items[input_index].name;
879 inputs_information(i,1) =
items[input_index].units;
880 inputs_information(i,2) =
items[input_index].description;
883 return(inputs_information);
902 for(
size_t i = 0; i < targets_number; i++)
904 target_index = targets_indices[i];
906 targets_information(i,0) =
items[target_index].name;
907 targets_information(i,1) =
items[target_index].units;
908 targets_information(i,2) =
items[target_index].description;
911 return(targets_information);
962 const size_t variables_number = inputs_number + targets_number;
966 for(
size_t i = 0; i < inputs_number; i++)
968 items[i].use = Input;
971 for(
size_t i = inputs_number; i < variables_number; i++)
973 items[i].use = Target;
1027 for(
size_t i = 0; i < variables_number; i++)
1029 items[i].use = new_uses[i];
1042 const size_t new_variables_number = new_uses.size();
1044 for(
size_t i = 0; i < new_variables_number; i++)
1046 if(new_uses[i] ==
"Input")
1048 items[i].use = Input;
1050 else if(new_uses[i] ==
"Target")
1052 items[i].use = Target;
1054 else if(new_uses[i] ==
"Unused")
1056 items[i].use = Unused;
1060 std::ostringstream buffer;
1062 buffer <<
"OpenNN Exception Variables class.\n"
1063 <<
"void set_uses(const Vector<std::string>&) method.\n"
1064 <<
"Unknown use: " << new_uses[i] <<
"\n";
1066 throw std::logic_error(buffer.str());
1086 if(i >= variables_number)
1088 std::ostringstream buffer;
1090 buffer <<
"OpenNN Exception Variables class.\n"
1091 <<
"void set_use(const size_t&, const Use&) method.\n"
1092 <<
"Index of variable must be less than number of variables.\n";
1094 throw std::logic_error(buffer.str());
1099 items[i].use = new_use;
1118 if(i >= variables_number)
1120 std::ostringstream buffer;
1122 buffer <<
"OpenNN Exception Variables class.\n"
1123 <<
"void set_use(const size_t&, const std::string&) method.\n"
1124 <<
"Index of variable must be less than number of variables.\n";
1126 throw std::logic_error(buffer.str());
1131 if(new_use ==
"Unused")
1133 items[i].use = Unused;
1135 else if(new_use ==
"Input")
1137 items[i].use = Input;
1139 else if(new_use ==
"Target")
1141 items[i].use = Target;
1145 std::ostringstream buffer;
1147 buffer <<
"OpenNN Exception Variables class.\n"
1148 <<
"void set_use(const std::string&) method.\n"
1149 <<
"Unknown use.\n";
1151 throw std::logic_error(buffer.str());
1164 for(
size_t i = 0; i < variables_number; i++)
1166 items[i].use = Input;
1179 for(
size_t i = 0; i < variables_number; i++)
1181 items[i].use = Target;
1198 if(variables_number == 0)
1202 else if(variables_number == 1)
1204 items[0].use = Unused;
1210 items[variables_number-1].use = Target;
1229 const size_t size = new_names.size();
1231 if(size != variables_number)
1233 std::ostringstream buffer;
1235 buffer <<
"OpenNN Exception: Variables class.\n"
1236 <<
"void set_names(const Vector<std::string>&) method.\n"
1237 <<
"Size must be equal to number of variables.\n";
1239 throw std::logic_error(buffer.str());
1244 for(
size_t i = 0; i < variables_number; i++)
1246 items[i].name = new_names[i];
1266 if(i >= variables_number)
1268 std::ostringstream buffer;
1270 buffer <<
"OpenNN Exception: Variables class.\n"
1271 <<
"void set_name(const size_t&, const std::string&) method.\n"
1272 <<
"Index of variable must be less than number of variables.\n";
1274 throw std::logic_error(buffer.str());
1281 items[i].name = new_name;
1299 const size_t size = new_units.size();
1301 if(size != variables_number)
1303 std::ostringstream buffer;
1305 buffer <<
"OpenNN Exception: Variables class.\n"
1306 <<
"void set_units(const Vector<std::string>&) method.\n"
1307 <<
"Size must be equal to number of variables.\n";
1309 throw std::logic_error(buffer.str());
1314 for(
size_t i = 0; i < variables_number; i++)
1316 items[i].units = new_units[i];
1336 if(i >= variables_number)
1338 std::ostringstream buffer;
1340 buffer <<
"OpenNN Exception: Variables class.\n"
1341 <<
"void set_units(const size_t&, const std::string&) method.\n"
1342 <<
"Index of variable must be less than number of variables.\n";
1344 throw std::logic_error(buffer.str());
1351 items[i].units = new_units;
1369 const size_t size = new_descriptions.size();
1371 if(size != variables_number)
1373 std::ostringstream buffer;
1375 buffer <<
"OpenNN Exception: Variables class.\n"
1376 <<
"void set_descriptions(const Vector<std::string>&) method.\n"
1377 <<
"Size must be equal to number of variables.\n";
1379 throw std::logic_error(buffer.str());
1384 for(
size_t i = 0; i < variables_number; i++)
1386 items[i].description = new_descriptions[i];
1406 if(i >= variables_number)
1408 std::ostringstream buffer;
1410 buffer <<
"OpenNN Exception: Variables class.\n"
1411 <<
"void set_description(const size_t&, const std::string&) method.\n"
1412 <<
"Index of variable must be less than number of variables.\n";
1414 throw std::logic_error(buffer.str());
1421 items[i].description = new_description;
1436 if(columns_name.size() != nominal_labels.size())
1438 std::ostringstream buffer;
1440 buffer <<
"OpenNN Exception: Variables class.\n"
1441 <<
"void set_names(const Vector<std::string>& , const Vector< Vector<std::string> >&) method.\n"
1442 <<
"Size of columns name (" << columns_name.size() <<
") must be equal to size of nominal labels " << nominal_labels.size() <<
".\n";
1444 throw std::logic_error(buffer.str());
1451 for(
size_t i = 0; i < nominal_labels.size(); i++)
1453 if(nominal_labels[i].size() == 0)
1455 names.push_back(columns_name[i]);
1457 else if(nominal_labels[i].size() == 2)
1459 if(nominal_labels[i].contains(
"true") || nominal_labels[i].contains(
"True") || nominal_labels[i].contains(
"TRUE"))
1461 names.push_back(columns_name[i] +
"_true");
1463 else if(nominal_labels[i].contains(
"positive") || nominal_labels[i].contains(
"Positive") || nominal_labels[i].contains(
"POSITIVE"))
1465 names.push_back(columns_name[i] +
"positive");
1469 names.push_back(columns_name[i]);
1474 for(
size_t j = 0; j < nominal_labels[i].size(); j++)
1476 names.push_back(nominal_labels[i][j]);
1551 const size_t new_variables_number = variables_number*(1+lags_number);
1557 for(
size_t i = 0; i < 1+lags_number; i++)
1559 for(
size_t j = 0; j < variables_number; j++)
1561 new_items[index].name =
items[j].name;
1562 new_items[index].units =
items[j].units;
1563 new_items[index].description =
items[j].description;
1565 if(i != lags_number)
1568 new_items[index].use = Variables::Input;
1572 new_items[index].use = Variables::Target;
1597 for(
size_t i = 0; i < variables_number; i++)
1599 autoassociation_items[i].name =
prepend(
"autoassociation_",
items[i].name);
1600 autoassociation_items[i].units =
items[i].units;
1601 autoassociation_items[i].description =
items[i].description;
1602 autoassociation_items[i].use = Variables::Target;
1618 items.set(new_variables_number);
1629 std::ostringstream buffer;
1635 buffer <<
"Variables object\n"
1636 <<
"Variables number: " << variables_number <<
"\n"
1637 <<
"Inputs number: " << inputs_number <<
"\n"
1638 <<
"Target number: " << targets_number <<
"\n";
1640 buffer <<
"Items:\n";
1644 for(
size_t i = 0; i < variables_number; i++)
1646 buffer <<
"Variable " << i+1 <<
":\n"
1647 <<
"Name: " <<
items[i].name <<
"\n"
1648 <<
"Units: " <<
items[i].units <<
"\n"
1649 <<
"Description: " <<
items[i].description <<
"\n"
1653 buffer <<
"Display: " <<
display <<
"\n";
1655 return(buffer.str());
1666 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
1668 std::ostringstream buffer;
1672 tinyxml2::XMLElement* variables_element = document->NewElement(
"Variables");
1674 document->InsertFirstChild(variables_element);
1677 tinyxml2::XMLElement* element = NULL;
1678 tinyxml2::XMLText* text = NULL;
1684 element = document->NewElement(
"VariablesNumber");
1685 variables_element->LinkEndChild(element);
1688 buffer << variables_number;
1690 text = document->NewText(buffer.str().c_str());
1691 element->LinkEndChild(text);
1694 for(
size_t i = 0; i < variables_number; i++)
1696 element = document->NewElement(
"Item");
1697 element->SetAttribute(
"Index", (
unsigned)i+1);
1698 variables_element->LinkEndChild(element);
1702 tinyxml2::XMLElement* name_element = document->NewElement(
"Name");
1703 element->LinkEndChild(name_element);
1705 tinyxml2::XMLText* name_text = document->NewText(
items[i].name.c_str());
1706 name_element->LinkEndChild(name_text);
1710 tinyxml2::XMLElement* units_element = document->NewElement(
"Units");
1711 element->LinkEndChild(units_element);
1713 tinyxml2::XMLText* units_text = document->NewText(
items[i].units.c_str());
1714 units_element->LinkEndChild(units_text);
1718 tinyxml2::XMLElement* description_element = document->NewElement(
"Description");
1719 element->LinkEndChild(description_element);
1721 tinyxml2::XMLText* descriptionText = document->NewText(
items[i].description.c_str());
1722 description_element->LinkEndChild(descriptionText);
1726 tinyxml2::XMLElement* use_element = document->NewElement(
"Use");
1727 element->LinkEndChild(use_element);
1729 tinyxml2::XMLText* use_text = document->NewText(
write_use(i).c_str());
1730 use_element->LinkEndChild(use_text);
1735 element = document->NewElement(
"Display");
1736 variables_element->LinkEndChild(element);
1741 text = document->NewText(buffer.str().c_str());
1742 element->LinkEndChild(text);
1756 std::ostringstream buffer;
1762 const tinyxml2::XMLElement* variables_element = document.FirstChildElement(
"Variables");
1764 if(!variables_element)
1766 buffer <<
"OpenNN Exception: Variables class.\n"
1767 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1768 <<
"Pointer to variables element is NULL.\n";
1770 throw std::logic_error(buffer.str());
1775 const tinyxml2::XMLElement* variables_number_element = variables_element->FirstChildElement(
"VariablesNumber");
1777 if(!variables_number_element)
1779 buffer <<
"OpenNN Exception: Variables class.\n"
1780 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1781 <<
"Pointer to variables number is NULL.\n";
1783 throw std::logic_error(buffer.str());
1786 const size_t variables_number = atoi(variables_number_element->GetText());
1792 const tinyxml2::XMLElement* start_element = variables_number_element;
1794 for(
size_t i = 0; i < variables_number; i++)
1796 const tinyxml2::XMLElement* item_element = start_element->NextSiblingElement(
"Item");
1797 start_element = item_element;
1801 buffer <<
"OpenNN Exception: Variables class.\n"
1802 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1803 <<
"Item " << i+1 <<
" is NULL.\n";
1805 throw std::logic_error(buffer.str());
1808 item_element->QueryUnsignedAttribute(
"Index", &index);
1812 buffer <<
"OpenNN Exception: Variables class.\n"
1813 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1814 <<
"Index " << index <<
" is not correct.\n";
1816 throw std::logic_error(buffer.str());
1821 const tinyxml2::XMLElement* name_element = item_element->FirstChildElement(
"Name");
1825 buffer <<
"OpenNN Exception: Variables class.\n"
1826 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1827 <<
"Pointer to name element is NULL.\n";
1829 throw std::logic_error(buffer.str());
1832 if(name_element->GetText())
1834 items[index-1].name = name_element->GetText();
1839 const tinyxml2::XMLElement* units_element = item_element->FirstChildElement(
"Units");
1843 buffer <<
"OpenNN Exception: Variables class.\n"
1844 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1845 <<
"Pointer to units element is NULL.\n";
1847 throw std::logic_error(buffer.str());
1850 if(units_element->GetText())
1852 items[index-1].units = units_element->GetText();
1857 const tinyxml2::XMLElement* description_element = item_element->FirstChildElement(
"Description");
1859 if(!description_element)
1861 buffer <<
"OpenNN Exception: Variables class.\n"
1862 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1863 <<
"Pointer to description element is NULL.\n";
1865 throw std::logic_error(buffer.str());
1868 if(description_element->GetText())
1870 items[index-1].description = description_element->GetText();
1875 const tinyxml2::XMLElement* use_element = item_element->FirstChildElement(
"Use");
1879 buffer <<
"OpenNN Exception: Variables class.\n"
1880 <<
"void from_XML(const tinyxml2::XMLElement*) method.\n"
1881 <<
"Pointer to use element is NULL.\n";
1883 throw std::logic_error(buffer.str());
1886 if(use_element->GetText())
1888 set_use(index-1, use_element->GetText());
1900 std::ostringstream buffer;
1904 return(buffer.str());
1914 std::ostringstream buffer;
1916 buffer << pre << str;
1918 return(buffer.str());
virtual ~Variables(void)
Destructor.
const std::string & get_description(const size_t &) const
Use
This enumeration represents the possible uses of a variable (input, target or unused).
void set_name(const size_t &, const std::string &)
Vector< std::string > arrange_inputs_name(void) const
Returns the names of the input variables in the data set.
const std::string & get_name(const size_t &) const
void set_target(void)
Sets all the variables in the data set as target variables.
Vector< std::string > arrange_units(void) const
Returns the units of all the variables in the data set.
void convert_time_series(const size_t &)
Vector< std::string > arrange_inputs_units(void) const
Returns the units of the input variables in the data set.
bool empty(void) const
Returns true if the number of variables is zero, and false otherwise.
void set_descriptions(const Vector< std::string > &)
static std::string unsigned_to_string(const size_t &)
Returns the string representation of number of type size_t.
Matrix< std::string > arrange_targets_information(void) const
Vector< size_t > arrange_targets_indices(void) const
Returns the indices of the target variables.
static std::string prepend(const std::string &, const std::string &)
Prepends the string pre to the beginning of the string str and returns the whole string.
const bool & get_display(void) const
Matrix< std::string > arrange_information(void) const
void set_variables_number(const size_t &)
void set_display(const bool &)
void set_default_uses(void)
void set_items(const Vector< Item > &)
void set_input(void)
Sets all the variables in the data set as input variables.
Vector< std::string > arrange_targets_units(void) const
Returns the units of the target variables in the data set.
void set_uses(const Vector< Use > &)
tinyxml2::XMLDocument * to_XML(void) const
void set(void)
Sets a variables object with zero variables.
size_t count_used_variables_number(void) const
Returns the number of variables which are either input or target.
bool has_names(void) const
Returns true if all the variables have been named, and false otherwise.
Vector< std::string > arrange_targets_description(void) const
Returns the description of the target variables in the data set.
void from_XML(const tinyxml2::XMLDocument &)
bool display
Display messages to screen.
void set_description(const size_t &, const std::string &)
size_t count_inputs_number(void) const
Returns the number of input variables of the data set.
const std::string & get_unit(const size_t &) const
Matrix< std::string > arrange_inputs_information(void) const
void convert_autoassociation(void)
const Use & get_use(const size_t &) const
Vector< std::string > arrange_names(void) const
Returns the names of all the variables in the data set.
void set_use(const size_t &, const Use &)
Vector< size_t > count_uses(void) const
size_t count_targets_number(void) const
Returns the number of target variables of the data set.
void set_units(const Vector< std::string > &)
Vector< std::string > arrange_targets_name(void) const
Returns the names of the target variables in the data set.
Vector< std::string > write_uses(void) const
std::string to_string(void) const
Returns a string representation of the current variables object.
void set_names(const Vector< std::string > &)
const Vector< Item > & get_items(void) const
Returns the vector Item structures in the variables object.
bool is_used(const size_t &) const
Vector< Use > arrange_uses(void) const
size_t get_variables_number(void) const
Returns the total number of variables in the data set.
size_t count_unused_variables_number(void) const
Returns the number of variables which will neither be used as input nor as target.
Variables & operator=(const Variables &)
bool operator==(const Variables &) const
Vector< std::string > arrange_descriptions(void) const
Returns the description of all the variables in the data set.
std::string write_use(const size_t &) const
const Item & get_item(const size_t &) const
Vector< size_t > arrange_inputs_indices(void) const
Returns the indices of the input variables.
Vector< std::string > arrange_inputs_description(void) const
Returns the description of the input variables in the data set.