Class TimeSeriesDataSet
- Namespace
- TimeSeriesAnalysis
- Assembly
- TimeSeriesAnalysis.dll
A class that holds time-series data for any number of tags, and with built in support for serializing/deserializing to a csv-file.
Time is either treated by giving a timeBase in seconds and a starting time, or by specifying a vector of timestamps.
public class TimeSeriesDataSet
- Inheritance
-
TimeSeriesDataSet
- Inherited Members
Constructors
TimeSeriesDataSet()
Default constructor
[JsonConstructor]
public TimeSeriesDataSet()
TimeSeriesDataSet(string, char, string)
Constructor that builds a dataset object from a csv-file, by loading LoadFromCsv(). If file does not exist, it is created
public TimeSeriesDataSet(string csvFileName, char separator = ';', string dateTimeFormat = "yyyy-MM-dd HH:mm:ss")
Parameters
csvFileNamestringname of csv-file
separatorcharthe separator in the csv-file
dateTimeFormatstringthe format of date-time strings in the csv-file
TimeSeriesDataSet(TimeSeriesDataSet)
Constructor that copies another dataset into the returned object
public TimeSeriesDataSet(TimeSeriesDataSet inputDataSet)
Parameters
inputDataSetTimeSeriesDataSet
TimeSeriesDataSet(TimeSeriesDataSet, List<int>)
Constructor that copies another dataset into the returned object, but only the values and times for the indices given
public TimeSeriesDataSet(TimeSeriesDataSet inputDataSet, List<int> indicesToNotCopy)
Parameters
inputDataSetTimeSeriesDataSetindicesToNotCopyList<int>
TimeSeriesDataSet(CsvContent, char, string)
Constructor that builds a dataset object from a csv-file, by loading LoadFromCsv() This version of the constructor is useful when receiving the csv-data across an API.
public TimeSeriesDataSet(CsvContent csvContent, char separator = ';', string dateTimeFormat = "yyyy-MM-dd HH:mm:ss")
Parameters
csvContentCsvContentthe csv data loaded into a CsvContent object
separatorcharthe separator in the csv-file
dateTimeFormatstringthe format of date-time strings in the csv-file
Properties
BadDataID
Some systems for storing data do not support "NaN", but instead some other magic value is reserved for indicating that a value is bad or missing.
public double BadDataID { get; set; }
Property Value
Methods
Add(string, double[])
Add an entire time-series to the dataset
public bool Add(string signalName, double[] values)
Parameters
Returns
AddConstant(string, double)
Add a constant value to the time-series to the dataset
public bool AddConstant(string signalName, double value)
Parameters
Returns
AddDataPoint(string, int, double)
Add a single data point
public bool AddDataPoint(string signalID, int idx, double value)
Parameters
Returns
- bool
returns false if signal does not already exist or if index is beyond dataset size
AddNoiseToSignal(string, double, int)
Adds noise to a given signal in the dataset. (This is mainly intended for testing identification algorithms against simulated data.)
public bool AddNoiseToSignal(string signalName, double noiseAmplitude, int seed)
Parameters
signalNamestringname of signal to have noise added to it
noiseAmplitudedoublethe amplutide of noise, the noise will be [-noiseAmplitude, noiseAmplitude]
seedinta integer seed number is
Returns
AddSet(TimeSeriesDataSet)
Adds all signals in a given set to this set
public bool AddSet(TimeSeriesDataSet inputDataSet)
Parameters
inputDataSetTimeSeriesDataSet
Returns
AppendDataPoint(string, double)
Append a single value to a given signal.The signal is added if the signal name does not exist. This method is useful for logging values in a for-loop. Combine with AppendTimeStamp to ensure there is a time-stamp. If this variable does not exist, then previous values are padded.
public void AppendDataPoint(string signalName, double value)
Parameters
AppendTimeStamp(DateTime)
Used in conjunction with AppendDataPoint when appending data to the data set. Appends a time stamp, and updates the number of data points N, if the timestamp does not already exist and is newer than the newest time stamp in the current dataset.
public bool AppendTimeStamp(DateTime timestamp)
Parameters
timestampDateTime
Returns
- bool
true if able to append, otherwise false
Combine(TimeSeriesDataSet)
Combine this data set with the inputDataset into a new set
public TimeSeriesDataSet Combine(TimeSeriesDataSet inputDataSet)
Parameters
inputDataSetTimeSeriesDataSet
Returns
- TimeSeriesDataSet
the newly created dataset
ContainsSignal(string)
Determine if a specific signal is in the dataset
public bool ContainsSignal(string signalID)
Parameters
signalIDstring
Returns
CreateDownsampledCopy(int, int)
Returns a copy of the dataset that is downsampled by the given factor.
public TimeSeriesDataSet CreateDownsampledCopy(int downsampleFactor, int keyIndex = 0)
Parameters
downsampleFactorintvalue greater than 1 indicating that every Floor(n*factor) value of the original data will be transferred.
keyIndexintoptional index around which to perform the downsampling.
Returns
CreateOversampledCopy(double)
Returns a copy of the dataset that is oversampled by the given factor.
public TimeSeriesDataSet CreateOversampledCopy(double timeBaseOversampled)
Parameters
timeBaseOversampleddoubledesired time base
Returns
- TimeSeriesDataSet
a downsampled copy, or null if operation failed. (Method will return null if no timestamps are given.)
CreateTimestamps(double, int, DateTime?)
Creates internal timestamps from a given start time and time-base, must be called after filling the values
public void CreateTimestamps(double timeBase_s, int N = 0, DateTime? t0 = null)
Parameters
timeBase_sdoublethe time between samples in the dataset, in total seconds
Nintnumber of datapoints
t0DateTime?start time, can be null, which can be useful for testing
GetAsMatrix(List<int>)
Get all signals in the dataset as a matrix
public (double[,], string[]) GetAsMatrix(List<int> indicesToIgnore = null)
Parameters
Returns
- (double[,], string[])
the signals as a 2d-matrix, and the an array of strings with corresponding signal names
GetData(string[], int)
Get Data for multiple signals at a specific time index
public double[] GetData(string[] signalNames, int timeIdx)
Parameters
Returns
- double[]
May return null if an error occurred
GetDescription()
Gets the description of the dataset
public string GetDescription()
Returns
GetIndicesToIgnore()
Get a list of the indices in the dataset that are flagged to be ignored in identification
public List<int> GetIndicesToIgnore()
Returns
GetLength()
Get the length in samples of the data set
The length of the dataset is determined by the number of timestamps.
public int? GetLength()
Returns
- int?
an integer indicating the length of the dataset
GetNumDataPoints()
Returns the number of data points in the dataset
public int GetNumDataPoints()
Returns
GetNumSimulatorRestarts()
Get the number of time the simulator was restarted due to periods of bad data(only applicable for simulated datasets)
public int GetNumSimulatorRestarts()
Returns
GetSignalNames()
Get the names of all the signals, whether constant or varying
public string[] GetSignalNames()
Returns
- string[]
GetTimeBase()
Get the time-base, the average time between two samples in the dataset
public double GetTimeBase()
Returns
- double
The time-base in seconds
GetTimeDiff_s(int, int)
Returns the time difference in seconds between two indices (in seconds)
public double GetTimeDiff_s(int newerIndex, int olderIndex)
Parameters
Returns
GetTimeStamps()
Get a vector of the timestamps of the data-set, or null if no timestamps
public DateTime[] GetTimeStamps()
Returns
- DateTime[]
GetValue(string, int)
Get Data for multiple signals at a specific time index
public double? GetValue(string signalName, int timeIdx)
Parameters
Returns
- double?
May return null if an error occurred
GetValues(string)
Get the values of a specific signal
public double[] GetValues(string signalName)
Parameters
signalNamestring
Returns
- double[]
null if signal not found
GetValues(string, SignalType, int)
Get the values of a specific signal
public double[] GetValues(string processID, SignalType signalType, int index = 0)
Parameters
processIDstringsignalTypeSignalTypeindexint
Returns
- double[]
GetValuesAtTime(string[], int)
Get one or more signals from the dataset at a given time
public double[] GetValuesAtTime(string[] signalIds, int timeIndex)
Parameters
Returns
- double[]
InitNewSignal(string, double, int, double)
Define a new signal, specifying only its initial value
public void InitNewSignal(string signalName, double initialValue, int N, double nonYetSimulatedValue = NaN)
Parameters
signalNamestringinitialValuedoublethe value of time zero
Nintnumber of time stamps
nonYetSimulatedValuedoublewhat value to fill in for future undefined times, default:nan
IsIndicesToIgnoreSet()
Determine if internal IndicesToIgnore is "null" i.e. has not been specified
public bool IsIndicesToIgnoreSet()
Returns
- bool
true if IndicesToIgnore is not null, or false it is null
LoadFromCsv(string, char, string)
Reads data form a csv-file (such as that created by ToCSV())
public bool LoadFromCsv(string csvFileName, char separator = ';', string dateTimeFormat = "yyyy-MM-dd HH:mm:ss")
Parameters
csvFileNamestringcsv file name
separatorchardefault separator
dateTimeFormatstringformat string of the time-series vector to be read
Returns
LoadFromCsv(CsvContent, char, string)
Loads the CsvContent(which can be read from a file) into a TimeSeriesDataSet object
public bool LoadFromCsv(CsvContent csvContent, char separator = ';', string dateTimeFormat = "yyyy-MM-dd HH:mm:ss")
Parameters
csvContentCsvContentseparatorchardateTimeFormatstring
Returns
Remove(string)
Removes a signal from the dataset
public bool Remove(string signalName)
Parameters
signalNamestring
Returns
ReplaceValues(string, double[], int)
Replaces the values of a specific signal
public void ReplaceValues(string signalName, double[] newValues, int index = 0)
Parameters
ReplaceValues(string, SignalType, double[], int)
Replaces the values of a specific signal
public void ReplaceValues(string processID, SignalType signalType, double[] newValues, int index = 0)
Parameters
processIDstringsignalTypeSignalTypenewValuesdouble[]indexint
SetDescription(string, bool)
Set the description of the dataset
public void SetDescription(string description, bool doAppend = false)
Parameters
SetIndicesToIgnore(List<int>)
The given indices will be skipped in any subsequent simulation of the dataset
public void SetIndicesToIgnore(List<int> indicesToIgnore)
Parameters
SetNumSimulatorRestarts(int)
Save the number of simulator restarts that has been performed for a simulated dataset (not applicable for measured datasets)
public void SetNumSimulatorRestarts(int numSimulatorRestarts)
Parameters
numSimulatorRestartsint
SetTimeStamps(List<DateTime>)
Explicitly sets the timestamps of the time-series (possibly overriding any timeBase_s that was given during init) If times is null, then the method creates timestamps based on timeBase_s and t0.
public void SetTimeStamps(List<DateTime> times)
Parameters
SubSetPrc(double, double)
Create a copy of the data set that is a "subset", given using start and end percentages of the original data span.
public TimeSeriesDataSet SubSetPrc(double startPrc, double endPrc)
Parameters
Returns
SubsetInd(int, int)
Create a copy of the data set that is a "subset", given using start and end indices of the original data span.
public TimeSeriesDataSet SubsetInd(int startInd, int endInd)
Parameters
Returns
ToCsv(string, string, int)
Exports the time-series data set to a csv-file
Times are encoded as "yyyy-MM-dd HH:mm:ss" and be loaded with CSV.LoadFromFile() afterwards
public bool ToCsv(string fileName, string csvSeparator = ";", int nSignificantDigits = 5)
Parameters
fileNamestringThe CSV-file name
csvSeparatorstringthe separator to use in the csv-file(despite the name, the most common is perhaps ";" which Excel will recognize automatically)
nSignificantDigitsintthe number of significant digits to include for each variable
Returns
- bool
true if successful, otherwise false
ToCsvText(string, int)
Create a comma-separated-variable(CSV) string of the dataset
public string ToCsvText(string csvSeparator = ";", int nSignificantDigits = 5)
Parameters
csvSeparatorstringsymbol used to separate columns in the string
nSignificantDigitsintnumber of significant digits per value
Returns
- string
The CSV-string
ToDict()
Create a dictionary of all dataset values. Constants are padded out to be of N length.
public Dictionary<string, double[]> ToDict()
Returns
- Dictionary<string, double[]>
Returns the dataset as a dictionary
ToString()
Summaize dataset in string
public override string ToString()
Returns
UpdateCsv()
When a dataset has been loaded from csv-file and changed, this method writes said changes back to the csv-file.
public bool UpdateCsv()
Returns
- bool
true if successful, otherwise false