IOLink 1.11.0
Loading...
Searching...
No Matches
createDataFrame.cpp

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.

Index Diameter(μm) Elongation factor Type
1 6.52 0.93 Nucleus
2 2.47 0.37 Mitochondrion
3 5.78 0.86 Nucleus

Then another Formula column is added to give the result of the multiplication of Diameter and Elongation factor columns.

#include <iostream>
#include <numeric>
#include <vector>
#include <iolink/view/DataFrameViewFactory.h>
using namespace iolink;
// Create a DataFrame object with 4 columns, using given values:
// - Indexer (int)
// - Diameter (double)
// - Elongation Factor (double)
// - Type (string)
std::shared_ptr<DataFrameView>
createDataFrame(const std::vector<double>& diameterValues,
const std::vector<double>& elongFactValues,
const std::vector<std::string>& typeValues)
{
// each list should have the same size
if (diameterValues.size() != elongFactValues.size() || diameterValues.size() != typeValues.size())
{
throw std::invalid_argument("All lists should have the same size");
}
// create a DataFrame object with 4 columns and the number of rows equal to the size of the given data
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());
// set the unit of the Diameter column
dataFrame->setUnit(dataFrame->columnIndex("Diameter"), "μm");
std::vector<uint64_t> indexValues(diameterValues.size());
std::iota(indexValues.begin(), indexValues.end(), 1);
// fill each column with the given data, one shot
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;
}
// Add a new column "formula" to the DataFrame object which is the result of the multiplication of the Diameter and
// Elongation Factor columns
void
addFormulaToDataFrame(std::shared_ptr<DataFrameView> df)
{
// add a new column "formula" to the DataFrame object
df->addColumn("Formula", DataTypeId::DOUBLE);
// get the column index of the Diameter and Elongation Factor columns
size_t diameterColumnIndex = df->columnIndex("Diameter");
size_t elongationFactorColumnIndex = df->columnIndex("Elongation Factor");
// get the column index of the new column "formula"
size_t formulaColumnIndex = df->columnIndex("Formula");
// get the number of rows in the DataFrame object
size_t numRows = df->shape()[1];
// create a vector to store the result of the multiplication of the Diameter and Elongation Factor columns
std::vector<double> formulaValues(numRows);
// calculate the result of the multiplication of the Diameter and Elongation Factor columns
for (size_t i = 0; i < numRows; ++i)
{
formulaValues[i] = df->at<double>(diameterColumnIndex, i) * df->at<double>(elongationFactorColumnIndex, i);
// write the result of the multiplication of the Diameter and Elongation Factor columns to the new column "formula"
df->setAt<double>(formulaColumnIndex, i, formulaValues[i]);
}
}
int
main(int argc, char** argv)
{
// create a DataFrame object with 4 columns and 3 rows
// and fill it with the following data:
// | Index | Diameter(μm) | Elongation factor | Type |
// |------ |--------------|-------------------|---------------|
// | 1 | 6.52 | 0.93 | Nucleus |
// | 2 | 2.47 | 0.37 | Mitochondrion |
// | 3 | 5.78 | 0.86 | Nucleus |
std::shared_ptr<DataFrameView> df =
createDataFrame({6.52, 2.47, 5.78}, {0.93, 0.37, 0.86}, {"Nucleus", "Mitochondrion", "Nucleus"});
// print the DataFrame object
std::cout << df->toString() << std::endl;
// add one column "formula" to the DataFrame object which contains the result of the multiplication of the Diameter
// and Elongation Factor columns
addFormulaToDataFrame(df);
// print the DataFrame object after adding the new column "formula"
std::cout << df->toString() << std::endl;
return EXIT_SUCCESS;
}