This code shows how to create a DataFrame object and add data to it.
This code shows how to create a DataFrame object and add data to it.
#include <iostream>
#include <numeric>
#include <vector>
#include <iolink/view/DataFrameViewFactory.h>
std::shared_ptr<DataFrameView>
createDataFrame(const std::vector<double>& diameterValues,
const std::vector<double>& elongFactValues,
const std::vector<std::string>& typeValues)
{
if (diameterValues.size() != elongFactValues.size() || diameterValues.size() != typeValues.size())
{
throw std::invalid_argument("All lists should have the same size");
}
std::vector<std::string> columnNames = {"Index", "Diameter", "Elongation Factor", "Type"};
std::vector<DataType> columnTypes = {
DataTypeId::UINT64, DataTypeId::DOUBLE, DataTypeId::DOUBLE, DataTypeId::UTF8_STRING};
std::shared_ptr<DataFrameView> dataFrame =
DataFrameViewFactory::allocate(
Vector2u64{4, diameterValues.
size()}, columnNames.data(), columnTypes.data());
dataFrame->setUnit(dataFrame->columnIndex("Diameter"), "μm");
std::vector<uint64_t> indexValues(diameterValues.size());
std::iota(indexValues.begin(), indexValues.end(), 1);
dataFrame->write(dataFrame->columnIndex("Index"), 0, indexValues.size(), indexValues.data());
dataFrame->write(dataFrame->columnIndex("Diameter"), 0, diameterValues.size(), diameterValues.data());
dataFrame->write(dataFrame->columnIndex("Elongation Factor"), 0, elongFactValues.size(), elongFactValues.data());
dataFrame->write(dataFrame->columnIndex("Type"), 0, typeValues.size(), typeValues.data());
return dataFrame;
}
void
addFormulaToDataFrame(std::shared_ptr<DataFrameView> df)
{
df->addColumn("Formula", DataTypeId::DOUBLE);
size_t diameterColumnIndex = df->columnIndex("Diameter");
size_t elongationFactorColumnIndex = df->columnIndex("Elongation Factor");
size_t formulaColumnIndex = df->columnIndex("Formula");
size_t numRows = df->shape()[1];
std::vector<double> formulaValues(numRows);
for (size_t i = 0; i < numRows; ++i)
{
formulaValues[i] = df->at<double>(diameterColumnIndex, i) * df->at<double>(elongationFactorColumnIndex, i);
df->setAt<double>(formulaColumnIndex, i, formulaValues[i]);
}
}
int
main(int argc, char** argv)
{
std::shared_ptr<DataFrameView> df =
createDataFrame({6.52, 2.47, 5.78}, {0.93, 0.37, 0.86}, {"Nucleus", "Mitochondrion", "Nucleus"});
std::cout << df->toString() << std::endl;
addFormulaToDataFrame(df);
std::cout << df->toString() << std::endl;
return EXIT_SUCCESS;
}
An arithmetic vector.
Definition Vector.h:37
size_t size() const
Returns the size of the vector, which also correspond to vector number of dimension.
Definition Vector.h:104
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8