| IOLink C# 1.11.0

Loading...
Searching...
No Matches
CreateDataFrame.cs

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.

using IOLink;
using System;
using System.Linq;
namespace Examples
{
public class CreateDataFrame
{
// Create a DataFrame object with 4 columns, using given
// values:
// - Indexer (int)
// - Diameter (double)
// - Elongation Factor (double)
// - Type (string)
private static DataFrameView CreateDataFrameView(double[] diameterValues,
double[] elongFactValues,
string[] typeValues)
{
// each list should have the same size
if (diameterValues.Length != elongFactValues.Length
|| diameterValues.Length != typeValues.Length)
{
throw new ApplicationException("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
string[] columNames = new string[] { "Index", "Diameter", "Elongation Factor", "Type" };
DataType[] columnTypes = new DataType[] {
DataTypeId.UINT64, DataTypeId.DOUBLE, DataTypeId.DOUBLE, DataTypeId.UTF8_STRING
};
var dataFrame = DataFrameViewFactory.Allocate(
new Vector2u64((ulong)columnTypes.Length, (ulong)diameterValues.Length),
columNames,
columnTypes);
// set the unit of the Diameter column
dataFrame.SetUnit(dataFrame.ColumnIndex("Diameter"), "μm");
ulong[] indexValues = new ulong[diameterValues.Length];
for (ulong i = 0; i < (ulong)diameterValues.Length; ++i)
{
indexValues[i] = i + 1;
}
// fill each column with the given data, one shot
dataFrame.Write(
dataFrame.ColumnIndex("Index"), 0, (uint)indexValues.Length, indexValues);
dataFrame.Write(
dataFrame.ColumnIndex("Diameter"), 0, (uint)diameterValues.Length, diameterValues);
dataFrame.Write(dataFrame.ColumnIndex("Elongation Factor"),
0,
(uint)elongFactValues.Length,
elongFactValues);
dataFrame.Write(dataFrame.ColumnIndex("Type"), 0, (uint)typeValues.Length, typeValues);
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
private static void AddFormulaToDataFrame(DataFrameView df)
{
// add a new column "formula" to the DataFrame object
df.AddColumn("Formula", DataTypeId.DOUBLE);
// get the number of rows in the DataFrame object
ulong numRows = df.Shape[1];
// create a vector to store the result of the
// multiplication of the Diameter and Elongation
// Factor columns
double[] formulaValues = new double[numRows];
// calculate the result of the multiplication of the
// Diameter and Elongation Factor columns
for (uint i = 0; i < (uint)numRows; ++i)
{
formulaValues[i]
= df.At<double>("Diameter", i) * df.At<double>("Elongation Factor", i);
// write the result of the multiplication of the
// Diameter and Elongation Factor columns to the
// new column "formula"
df.SetAt<double>("Formula", i, formulaValues[i]);
}
}
public static void Main(string[] args)
{
// create dataframe
DataFrameView df
= CreateDataFrameView(new double[] { 6.52, 2.47, 5.78 },
new double[] { 0.93, 0.37, 0.86 },
new string[] { "Nucleus", "Mitochondrion", "Nucleus" });
// print the DataFrame object
Console.WriteLine(df.ToString());
// 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"
Console.WriteLine(df.ToString());
}
}
}
IOLink.DataFrameViewFactory
Definition DataFrameViewFactory.cs:13
IOLink.DataFrameViewFactory.Allocate
static DataFrameView Allocate(Vector2u64 shape, string[] columnNames, DataType[] columnDataTypes)
This factory is aimed at creating a DataFrame view.
Definition DataFrameViewFactory.cs:72
IOLink.DataFrameView
View to model tabular data.
Definition DataFrameView.cs:19
IOLink.DataFrameView.ToString
string ToString()
Return a string representation.
Definition DataFrameView.cs:217
IOLink.DataFrameView.AddColumn
virtual void AddColumn(string name, DataType dtype)
Add a column to the dataframe.
Definition DataFrameView.cs:173
IOLink.DataFrameView.Shape
Vector2u64 Shape
The shape on the DataFrameView
Definition DataFrameView.cs:53
IOLink.DataType
Stores information about a data type.
Definition DataType.cs:24
IOLink.Vector2u64
An arithmetic vector.
Definition Vector2u64.cs:29
Examples
Definition CreateDataFrame.cs:6
IOLink
Definition AccessCapabilities.cs:11
IOLink.DataTypeId
DataTypeId
A collection of built-in data types.
Definition DataTypeId.cs:15