bpp-core
2.2.0
|
This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns. More...
#include <Bpp/Numeric/DataTable.h>
Public Member Functions | |
DataTable (size_t nRow, size_t nCol) | |
Build a new void DataTable object with nRow rows and nCol columns. More... | |
DataTable (size_t nCol) | |
Build a new void DataTable object with nCol columns. More... | |
DataTable (const std::vector< std::string > &colNames) throw (DuplicatedTableColumnNameException) | |
Build a new void DataTable object with named columns. More... | |
DataTable (const DataTable &table) | |
DataTable & | operator= (const DataTable &table) |
DataTable * | clone () const |
Create a copy of this object and send a pointer to it. More... | |
virtual | ~DataTable () |
std::string & | operator() (size_t rowIndex, size_t colIndex) throw (IndexOutOfBoundsException) |
const std::string & | operator() (size_t rowIndex, size_t colIndex) const throw (IndexOutOfBoundsException) |
std::string & | operator() (const std::string &rowName, const std::string &colName) throw (NoTableRowNamesException, NoTableColumnNamesException, TableNameNotFoundException) |
const std::string & | operator() (const std::string &rowName, const std::string &colName) const throw (NoTableRowNamesException, NoTableColumnNamesException, TableNameNotFoundException) |
std::string & | operator() (const std::string &rowName, size_t colIndex) throw (NoTableRowNamesException, TableNameNotFoundException, IndexOutOfBoundsException) |
const std::string & | operator() (const std::string &rowName, size_t colIndex) const throw (NoTableRowNamesException, TableNameNotFoundException, IndexOutOfBoundsException) |
std::string & | operator() (size_t rowIndex, const std::string &colName) throw (IndexOutOfBoundsException, NoTableColumnNamesException, TableNameNotFoundException) |
const std::string & | operator() (size_t rowIndex, const std::string &colName) const throw (IndexOutOfBoundsException, NoTableColumnNamesException, TableNameNotFoundException) |
Work on columns. | |
size_t | getNumberOfColumns () const |
void | setColumnNames (const std::vector< std::string > &colNames) throw (DimensionException, DuplicatedTableColumnNameException) |
Set the column names of this table. More... | |
std::vector< std::string > | getColumnNames () const throw (NoTableColumnNamesException) |
Get the column names of this table. More... | |
std::string | getColumnName (size_t index) const throw (NoTableColumnNamesException, IndexOutOfBoundsException) |
Get a given column name. More... | |
bool | hasColumnNames () const |
std::vector< std::string > & | getColumn (size_t index) throw (IndexOutOfBoundsException) |
const std::vector< std::string > & | getColumn (size_t index) const throw (IndexOutOfBoundsException) |
std::vector< std::string > & | getColumn (const std::string &colName) throw (NoTableColumnNamesException, TableColumnNameNotFoundException) |
const std::vector< std::string > & | getColumn (const std::string &colName) const throw (NoTableColumnNamesException, TableColumnNameNotFoundException) |
bool | hasColumn (const std::string &colName) const |
Tell is a given column exists. More... | |
void | deleteColumn (size_t index) throw (IndexOutOfBoundsException) |
Delete the given column. More... | |
void | deleteColumn (const std::string &colName) throw (NoTableColumnNamesException, TableColumnNameNotFoundException) |
Delete the given column. More... | |
void | addColumn (const std::vector< std::string > &newColumn) throw (DimensionException, TableColumnNamesException) |
Add a new column. More... | |
void | addColumn (const std::string &colName, const std::vector< std::string > &newColumn) throw (DimensionException, NoTableColumnNamesException, DuplicatedTableColumnNameException) |
Add a new column. More... | |
Work on rows. | |
size_t | getNumberOfRows () const |
void | setRowNames (const std::vector< std::string > &rowNames) throw (DimensionException, DuplicatedTableRowNameException) |
Set the row names of this table. More... | |
std::vector< std::string > | getRowNames () const throw (NoTableRowNamesException) |
Get the row names of this table. More... | |
bool | hasRow (const std::string &rowName) const |
Tell is a given row exists. More... | |
std::string | getRowName (size_t index) const throw (NoTableRowNamesException, IndexOutOfBoundsException) |
Get a given row name. More... | |
bool | hasRowNames () const |
std::vector< std::string > | getRow (size_t index) const throw (IndexOutOfBoundsException) |
std::vector< std::string > | getRow (const std::string &rowName) const throw (NoTableRowNamesException, TableRowNameNotFoundException) |
void | deleteRow (size_t index) throw (IndexOutOfBoundsException) |
Delete the given row. More... | |
void | deleteRow (const std::string &rowName) throw (NoTableRowNamesException, TableRowNameNotFoundException) |
Delete the given row. More... | |
void | addRow (const std::vector< std::string > &newRow) throw (DimensionException, TableRowNamesException) |
Add a new row. More... | |
void | addRow (const std::string &rowName, const std::vector< std::string > &newRow) throw (DimensionException, NoTableRowNamesException, DuplicatedTableRowNameException) |
Add a new row. More... | |
Static Public Member Functions | |
static DataTable * | read (std::istream &in, const std::string &sep="\, bool header=true, int rowNames=-1) throw (DimensionException, IndexOutOfBoundsException, DuplicatedTableRowNameException) |
Read a table form a stream in CSV-like format. More... | |
static void | write (const DataTable &data, std::ostream &out, const std::string &sep="\, bool alignHeaders=false) |
Write a DataTable object to stream in CVS-like format. More... | |
static void | write (const DataTable &data, bpp::OutputStream &out, const std::string &sep="\, bool alignHeaders=false) |
Protected Attributes | |
size_t | nRow_ |
size_t | nCol_ |
std::vector< std::vector< std::string > > | data_ |
std::vector< std::string > * | rowNames_ |
std::vector< std::string > * | colNames_ |
This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns.
Data are stored as string objects, by column. A DataTable object is hence similar to a ColMatrix<string>.object. (NB: actually, ColMatrix does not exist yet...)
Definition at line 64 of file DataTable.h.
DataTable::DataTable | ( | size_t | nRow, |
size_t | nCol | ||
) |
DataTable::DataTable | ( | size_t | nCol | ) |
Build a new void DataTable object with nCol columns.
nCol | The number of columns of the DataTable. |
Definition at line 64 of file DataTable.cpp.
DataTable::DataTable | ( | const std::vector< std::string > & | colNames | ) | |
throw | ( | DuplicatedTableColumnNameException | |||
) |
Build a new void DataTable object with named columns.
colNames | The names of the columns of the DataTable. |
DuplicatedTableColumnNameException | If colnames contains identical names. |
Definition at line 72 of file DataTable.cpp.
DataTable::DataTable | ( | const DataTable & | table | ) |
Definition at line 83 of file DataTable.cpp.
|
virtual |
Definition at line 116 of file DataTable.cpp.
void DataTable::addColumn | ( | const std::vector< std::string > & | newColumn | ) | |
throw | ( | DimensionException, | |||
TableColumnNamesException | |||||
) |
Add a new column.
newColumn | The new column values. |
DimensionException | If the number of values does not match the number of rows. |
TableColumnNamesException | If the table has row names. |
Definition at line 425 of file DataTable.cpp.
void DataTable::addColumn | ( | const std::string & | colName, |
const std::vector< std::string > & | newColumn | ||
) | |||
throw | ( | DimensionException, | |
NoTableColumnNamesException, | |||
DuplicatedTableColumnNameException | |||
) |
Add a new column.
colName | The name of the column. |
newColumn | The new column values. |
DimensionException | If the number of values does not match the number of rows. |
NoTableColumnNamesException | If the table does not have row names. |
DuplicatedTableColumnNameException | If colName is already used. |
Definition at line 436 of file DataTable.cpp.
void DataTable::addRow | ( | const std::vector< std::string > & | newRow | ) | |
throw | ( | DimensionException, | |||
TableRowNamesException | |||||
) |
Add a new row.
newRow | The new row values. |
DimensionException | If the number of values does not match the number of columns. |
TableRowNamesException | If the table has column names. |
Definition at line 542 of file DataTable.cpp.
Referenced by read().
void DataTable::addRow | ( | const std::string & | rowName, |
const std::vector< std::string > & | newRow | ||
) | |||
throw | ( | DimensionException, | |
NoTableRowNamesException, | |||
DuplicatedTableRowNameException | |||
) |
Add a new row.
rowName | The name of the row. |
newRow | The new row values. |
DimensionException | If the number of values does not match the number of columns. |
NoTableRowNamesException | If the table does not have column names. |
DuplicatedTableRowNameException | If rowName is already used. |
Definition at line 556 of file DataTable.cpp.
|
inlinevirtual |
Create a copy of this object and send a pointer to it.
Implements bpp::Clonable.
Definition at line 101 of file DataTable.h.
References DataTable().
void DataTable::deleteColumn | ( | size_t | index | ) | |
throw | ( | IndexOutOfBoundsException | |||
) |
Delete the given column.
index | The index of the column. |
IndexOutOfBoundsException | If index is >= number of columns. |
Definition at line 396 of file DataTable.cpp.
Referenced by read().
void DataTable::deleteColumn | ( | const std::string & | colName | ) | |
throw | ( | NoTableColumnNamesException, | |||
TableColumnNameNotFoundException | |||||
) |
Delete the given column.
colName | The name of the column. |
NoTableColumnNamesException | If no column names are associated to this table. |
TableColumnNameNotFoundException | If colName do not match existing column names. |
Definition at line 407 of file DataTable.cpp.
References bpp::VectorTools::which().
void DataTable::deleteRow | ( | size_t | index | ) | |
throw | ( | IndexOutOfBoundsException | |||
) |
Delete the given row.
index | The index of the row. |
IndexOutOfBoundsException | If index is >= number of row. |
Definition at line 505 of file DataTable.cpp.
void DataTable::deleteRow | ( | const std::string & | rowName | ) | |
throw | ( | NoTableRowNamesException, | |||
TableRowNameNotFoundException | |||||
) |
Delete the given row.
rowName | The name of the row. |
NoTableRowNamesException | If no row names are associated to this table. |
TableRowNameNotFoundException | If rowName do not match existing column names. |
Definition at line 520 of file DataTable.cpp.
References bpp::VectorTools::which().
vector< string > & DataTable::getColumn | ( | size_t | index | ) | |
throw | ( | IndexOutOfBoundsException | |||
) |
index | The index of the column. |
IndexOutOfBoundsException | If index is >= number of columns. |
Definition at line 336 of file DataTable.cpp.
Referenced by read().
const vector< string > & DataTable::getColumn | ( | size_t | index | ) | const |
throw | ( | IndexOutOfBoundsException | |||
) |
index | The index of the column. |
IndexOutOfBoundsException | If index is >= number of columns. |
Definition at line 344 of file DataTable.cpp.
vector< string > & DataTable::getColumn | ( | const std::string & | colName | ) | |
throw | ( | NoTableColumnNamesException, | |||
TableColumnNameNotFoundException | |||||
) |
colName | The name of the column. |
NoTableColumnNamesException | If no column names are associated to this table. |
TableColumnNameNotFoundException | If colName do not match existing column names. |
Definition at line 352 of file DataTable.cpp.
References bpp::VectorTools::which().
const vector< string > & DataTable::getColumn | ( | const std::string & | colName | ) | const |
throw | ( | NoTableColumnNamesException, | |||
TableColumnNameNotFoundException | |||||
) |
colName | The name of the column. |
NoTableColumnNamesException | If no column names are associated to this table. |
TableColumnNameNotFoundException | If colName do not match existing column names. |
Definition at line 368 of file DataTable.cpp.
References bpp::VectorTools::which().
string DataTable::getColumnName | ( | size_t | index | ) | const |
throw | ( | NoTableColumnNamesException, | |||
IndexOutOfBoundsException | |||||
) |
Get a given column name.
index | The index of the column. |
NoTableColumnNamesException | If no column names are associated to this table. |
IndexOutOfBoundsException | If index is >= number of columns. |
Definition at line 323 of file DataTable.cpp.
vector< string > DataTable::getColumnNames | ( | ) | const | |
throw | ( | NoTableColumnNamesException | ||
) |
Get the column names of this table.
NoTableColumnNamesException | If no column names are associated to this table. |
Definition at line 316 of file DataTable.cpp.
References colNames_.
Referenced by write().
|
inline |
Definition at line 197 of file DataTable.h.
References nCol_.
Referenced by write().
|
inline |
Definition at line 311 of file DataTable.h.
References nRow_.
Referenced by write().
vector< string > DataTable::getRow | ( | size_t | index | ) | const |
throw | ( | IndexOutOfBoundsException | |||
) |
index | The index of the row. |
IndexOutOfBoundsException | If index is >= number of rows. |
Definition at line 459 of file DataTable.cpp.
vector< string > DataTable::getRow | ( | const std::string & | rowName | ) | const |
throw | ( | NoTableRowNamesException, | |||
TableRowNameNotFoundException | |||||
) |
rowName | The name of the row. |
NoTableRowNamesException | If no row names are associated to this table. |
TableRowNameNotFoundException | If rowName do not match existing row names. |
Definition at line 472 of file DataTable.cpp.
References bpp::VectorTools::which().
string DataTable::getRowName | ( | size_t | index | ) | const |
throw | ( | NoTableRowNamesException, | |||
IndexOutOfBoundsException | |||||
) |
Get a given row name.
index | The index of the row. |
NoTableRowNamesException | If no row names are associated to this table. |
IndexOutOfBoundsException | If index is >= number of rows. |
Definition at line 290 of file DataTable.cpp.
Referenced by write().
vector< string > DataTable::getRowNames | ( | ) | const | |
throw | ( | NoTableRowNamesException | ||
) |
Get the row names of this table.
NoTableRowNamesException | If no row names are associated to this table. |
Definition at line 283 of file DataTable.cpp.
References rowNames_.
bool DataTable::hasColumn | ( | const std::string & | colName | ) | const |
Tell is a given column exists.
colName | The name of the column to look for. |
Definition at line 384 of file DataTable.cpp.
References colNames_.
|
inline |
Definition at line 227 of file DataTable.h.
References colNames_.
Referenced by write().
bool DataTable::hasRow | ( | const std::string & | rowName | ) | const |
Tell is a given row exists.
rowName | The name of the row to look for. |
Definition at line 493 of file DataTable.cpp.
References rowNames_.
|
inline |
Definition at line 351 of file DataTable.h.
References rowNames_.
Referenced by write().
string & DataTable::operator() | ( | size_t | rowIndex, |
size_t | colIndex | ||
) | |||
throw | ( | IndexOutOfBoundsException | |
) |
rowIndex | Row number. |
colIndex | Column number. |
IndexOutOfBoundsException | If one of the index is greater or equal to the corresponding number of columns/rows. |
Definition at line 128 of file DataTable.cpp.
const string & DataTable::operator() | ( | size_t | rowIndex, |
size_t | colIndex | ||
) | const | ||
throw | ( | IndexOutOfBoundsException | |
) |
rowIndex | Row number. |
colIndex | Column number. |
IndexOutOfBoundsException | If one of the index is greater or equal to the corresponding number of columns/rows. |
Definition at line 137 of file DataTable.cpp.
string & DataTable::operator() | ( | const std::string & | rowName, |
const std::string & | colName | ||
) | |||
throw | ( | NoTableRowNamesException, | |
NoTableColumnNamesException, | |||
TableNameNotFoundException | |||
) |
rowName | Row name. |
colName | Column name. |
NoTableRowNamesException | If the table does not have names associated to rows. |
NoTableColumnNamesException | If the table does not have names associated to columns. |
TableNameNotFoundException | If one of rowName or colName do not match existing names. |
Definition at line 148 of file DataTable.cpp.
References bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().
const string & DataTable::operator() | ( | const std::string & | rowName, |
const std::string & | colName | ||
) | const | ||
throw | ( | NoTableRowNamesException, | |
NoTableColumnNamesException, | |||
TableNameNotFoundException | |||
) |
rowName | Row name. |
colName | Column name. |
NoTableRowNamesException | If the table does not have names associated to rows. |
NoTableColumnNamesException | If the table does not have names associated to columns. |
TableNameNotFoundException | If one of rowName or colName do not match existing names. |
Definition at line 167 of file DataTable.cpp.
References bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().
string & DataTable::operator() | ( | const std::string & | rowName, |
size_t | colIndex | ||
) | |||
throw | ( | NoTableRowNamesException, | |
TableNameNotFoundException, | |||
IndexOutOfBoundsException | |||
) |
rowName | Row name. |
colIndex | Column number. |
NoTableRowNamesException | If the table does not have names associated to rows. |
IndexOutOfBoundsException | If the index is greater or equal to the number of columns. |
TableNameNotFoundException | If rowName do not match existing names. |
Definition at line 188 of file DataTable.cpp.
References bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().
const string & DataTable::operator() | ( | const std::string & | rowName, |
size_t | colIndex | ||
) | const | ||
throw | ( | NoTableRowNamesException, | |
TableNameNotFoundException, | |||
IndexOutOfBoundsException | |||
) |
rowName | Row name. |
colIndex | Column number. |
NoTableRowNamesException | If the table does not have names associated to rows. |
IndexOutOfBoundsException | If the index is greater or equal to the number of columns. |
TableNameNotFoundException | If rowName do not match existing names. |
Definition at line 206 of file DataTable.cpp.
References bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().
string & DataTable::operator() | ( | size_t | rowIndex, |
const std::string & | colName | ||
) | |||
throw | ( | IndexOutOfBoundsException, | |
NoTableColumnNamesException, | |||
TableNameNotFoundException | |||
) |
rowIndex | Row number. |
colName | Column name. |
IndexOutOfBoundsException | If the index is greater or equal to the number of rows. |
NoTableColumnNamesException | If the table does not have names associated to columns. |
TableNameNotFoundException | If colName do not match existing names. |
Definition at line 226 of file DataTable.cpp.
References bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().
const string & DataTable::operator() | ( | size_t | rowIndex, |
const std::string & | colName | ||
) | const | ||
throw | ( | IndexOutOfBoundsException, | |
NoTableColumnNamesException, | |||
TableNameNotFoundException | |||
) |
rowIndex | Row number. |
colName | Column name. |
IndexOutOfBoundsException | If the index is greater or equal to the number of rows. |
NoTableColumnNamesException | If the table does not have names associated to columns. |
TableNameNotFoundException | If colName do not match existing names. |
Definition at line 244 of file DataTable.cpp.
References bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().
|
static |
Read a table form a stream in CSV-like format.
The number of rows is given by the second line in the file. By default, if the first line as one column less than the second one, the first line is taken as column names, and the first column as row names. Otherwise, no column names and no row names are specified, unless explicitely precised by the user.
in | The input stream. |
sep | The column delimiter. |
header | Tell if the first line must be used as column names, otherwise use default. |
rowNames | Use a column as rowNames. If positive, use the specified column to compute rownames, otherwise use default; |
Definition at line 582 of file DataTable.cpp.
References addRow(), deleteColumn(), getColumn(), bpp::FileTools::getNextLine(), bpp::StringTokenizer::getTokens(), bpp::TextTools::isEmpty(), setColumnNames(), and setRowNames().
void DataTable::setColumnNames | ( | const std::vector< std::string > & | colNames | ) | |
throw | ( | DimensionException, | |||
DuplicatedTableColumnNameException | |||||
) |
Set the column names of this table.
colNames | The row names. |
DimensionException | If the number of names do not match the number of columns in the table. |
DuplicatedTableColumnNameException | If names are not unique. |
Definition at line 301 of file DataTable.cpp.
References bpp::VectorTools::isUnique().
Referenced by read().
void DataTable::setRowNames | ( | const std::vector< std::string > & | rowNames | ) | |
throw | ( | DimensionException, | |||
DuplicatedTableRowNameException | |||||
) |
Set the row names of this table.
rowNames | The row names. |
DimensionException | If the number of names do not match the number of rows in the table. |
DuplicatedTableRowNameException | If names are not unique. |
Definition at line 266 of file DataTable.cpp.
References bpp::VectorTools::isUnique().
Referenced by read().
|
static |
Write a DataTable object to stream in CVS-like format.
data | The table to write. |
out | The output stream. |
sep | The column delimiter. |
alignHeaders | If true, add a delimiter before the first column header if there is row names. |
Definition at line 653 of file DataTable.cpp.
References getColumnNames(), getNumberOfColumns(), getNumberOfRows(), getRowName(), hasColumnNames(), and hasRowNames().
|
static |
Definition at line 684 of file DataTable.cpp.
References bpp::OutputStream::endLine(), getColumnNames(), getNumberOfColumns(), getNumberOfRows(), getRowName(), hasColumnNames(), and hasRowNames().
|
protected |
Definition at line 71 of file DataTable.h.
Referenced by DataTable(), getColumnNames(), hasColumn(), hasColumnNames(), operator=(), and ~DataTable().
|
protected |
Definition at line 69 of file DataTable.h.
Referenced by DataTable(), and operator=().
|
protected |
Definition at line 68 of file DataTable.h.
Referenced by getNumberOfColumns(), and operator=().
|
protected |
Definition at line 68 of file DataTable.h.
Referenced by getNumberOfRows(), and operator=().
|
protected |
Definition at line 70 of file DataTable.h.
Referenced by DataTable(), getRowNames(), hasRow(), hasRowNames(), operator=(), and ~DataTable().