pub struct Dataset<const COLS: usize, Data: Datapoint<COLS>> { /* private fields */ }Expand description
A dataset is a collection of datapoints (for more information on this see the Datapoint trait).
They can be constructed in a multitude of ways. Two common ways are:
From a set of datapoints:
use delfi::Dataset;
let dp1 = [0, 2];
let dp2 = [1, 3];
let dp3 = [2, 5];
let dp4 = [3, 8];
let ds = Dataset::from_datapoints([dp1, dp2, dp3, dp4]);From columns of data:
use delfi::Dataset;
let t = [0, 1, 2, 3];
let x = [2, 3, 5, 8];
let ds = Dataset::from_columns([t, x]);One can also add labels in a multitude of ways, the simplest being whilst constructing the dataset:
use delfi::Dataset;
let t = [0, 1, 2, 3, 4, 5];
let x = [2, 3, 5, 8, 12, 17];
let ds = Dataset::from_columns([t, x]).with_labels(["time", "length"]);This is equivalent to using the dataset-macro:
use delfi::dataset;
let t = [0, 1, 2, 3, 4, 5];
let x = [2, 3, 5, 8, 12, 17];
let ds = dataset!{
"time" => t,
"length" => x,
};Implementations§
Source§impl<const COLS: usize, Data: Datapoint<COLS>> Dataset<COLS, Data>
impl<const COLS: usize, Data: Datapoint<COLS>> Dataset<COLS, Data>
Sourcepub fn n_rows(&self) -> usize
pub fn n_rows(&self) -> usize
Get current number of rows in dataset, which is equal to the number of datapoints, plus 1 if there is a header row
Sourcepub fn n_datapoints(&self) -> usize
pub fn n_datapoints(&self) -> usize
Get current number of rows in dataset, which is equal to the number of datapoints, plus 1 if there is a header row
Sourcepub fn get_labels(&self) -> Option<&[String; COLS]>
pub fn get_labels(&self) -> Option<&[String; COLS]>
Get current labels
Sourcepub fn set_labels<'a, Labels>(&mut self, labels: Labels)
pub fn set_labels<'a, Labels>(&mut self, labels: Labels)
Set labels for the given dataset. Constructors return dataset with labels set to None unless otherwise specified.
use delfi::Dataset;
let t = [0, 1, 2, 3, 4, 5];
let x = [2, 3, 5, 8, 12, 17];
let mut dataset = Dataset::from_columns([t, x]);
dataset.set_labels(["time", "length"]);Labels can also be turned off
dataset.set_labels(None);They also technically accept labels to be passed via Some(_) (but why would you?):
dataset.set_labels(Some(["time", "length"]));Sourcepub fn with_labels<'a, Labels>(self, labels: Labels) -> Self
pub fn with_labels<'a, Labels>(self, labels: Labels) -> Self
Take dataset, set labels, and return dataset. Useful when constructing datasets.
use delfi::Dataset;
let t = [0, 1, 2, 3, 4, 5];
let x = [2, 3, 5, 8, 12, 17];
let _ = Dataset::from_columns([&t, &x]).with_labels(["time", "length"]);See set_labels() for detail on possible parameters.
Sourcepub fn from_datapoints<IntoIter, Iter>(rows: IntoIter) -> Selfwhere
IntoIter: IntoIterator<Item = Data, IntoIter = Iter>,
Iter: Iterator<Item = Data>,
Data: Datapoint<COLS>,
pub fn from_datapoints<IntoIter, Iter>(rows: IntoIter) -> Selfwhere
IntoIter: IntoIterator<Item = Data, IntoIter = Iter>,
Iter: Iterator<Item = Data>,
Data: Datapoint<COLS>,
Create a dataset from an iterator over datapoints
Source§impl<const COLS: usize, DataElement: ToString> Dataset<COLS, [DataElement; COLS]>
impl<const COLS: usize, DataElement: ToString> Dataset<COLS, [DataElement; COLS]>
Sourcepub fn from_columns<IntoIter, Iter>(columns: [IntoIter; COLS]) -> Selfwhere
IntoIter: IntoIterator<Item = DataElement, IntoIter = Iter>,
Iter: Iterator<Item = DataElement>,
pub fn from_columns<IntoIter, Iter>(columns: [IntoIter; COLS]) -> Selfwhere
IntoIter: IntoIterator<Item = DataElement, IntoIter = Iter>,
Iter: Iterator<Item = DataElement>,
Takes in a set of columns and creates a dataset from these.
§Examples
use delfi::Dataset;
let t = [0, 1, 2, 3, 4, 5];
let x = [2, 3, 5, 8, 12, 17];
let _ = Dataset::from_columns([t, x]);Source§impl<const COLS: usize, Data: Datapoint<COLS>> Dataset<COLS, Data>
impl<const COLS: usize, Data: Datapoint<COLS>> Dataset<COLS, Data>
Sourcepub fn save<P: AsRef<Path>>(self, filepath: P) -> Result<(), Error>
pub fn save<P: AsRef<Path>>(self, filepath: P) -> Result<(), Error>
Saves a dataset to a given file. The filepath must be valid. Accepts anything path-like.
§Examples
dataset.save("./resources/data/examples/save-short.csv").unwrap();let directory = std::fs::canonicalize("./resources/data/examples/").unwrap();
let filepath = directory.join("save-long.csv");
dataset.save(&filepath).unwrap();