Public imports

  • mir.ndslice.slice

The module contains data structure with special iteration and indexing methods. It is aimed to construct index or time-series using Mir and Phobos algorithms.

Public_imports

.

Copyright

Copyright © 2017, Kaleidic Associates Advisory Limited

Authors

Ilya Yaroshenko

Example

See Also:

, , .
import mir.ndslice;
import mir.series;

import mir.array.allocation: array;
import mir.algorithm.setops: multiwayUnion;

import std.datetime: Date;
static if (__VERSION__ >= 2085) import core.lifetime: move; else import std.algorithm.mutation: move; 
import std.exception: collectExceptionMsg;

//////////////////////////////////////
// Constructs two time-series.
//////////////////////////////////////
auto index0 = [
    Date(2017, 01, 01),
    Date(2017, 03, 01),
    Date(2017, 04, 01)];

auto data0 = [1.0, 3, 4];
auto series0 = index0.series(data0);

auto index1 = [
    Date(2017, 01, 01),
    Date(2017, 02, 01),
    Date(2017, 05, 01)];

auto data1 = [10.0, 20, 50];
auto series1 = index1.series(data1);    

//////////////////////////////////////
// asSlice method
//////////////////////////////////////
assert(series0
    .asSlice
    // ref qualifier is optional
    .map!((ref key, ref value) => key.month == value)
    .all);

//////////////////////////////////////
// get* methods
//////////////////////////////////////

auto refDate = Date(2017, 03, 01);
auto missingDate = Date(2016, 03, 01);

// default value
double defaultValue = 100;
assert(series0.get(refDate, defaultValue) == 3);
assert(series0.get(missingDate, defaultValue) == defaultValue);

// Exceptions handlers
assert(series0.get(refDate) == 3);
assert(series0.get(refDate, new Exception("My exception msg")) == 3);
assert(series0.getVerbose(refDate) == 3);    
assert(series0.getExtraVerbose(refDate, "My exception msg") == 3);    

assert(collectExceptionMsg!Exception(
        series0.get(missingDate)
    ) == "Series double[Date]: Missing required key");

assert(collectExceptionMsg!Exception(
        series0.get(missingDate, new Exception("My exception msg"))
    ) == "My exception msg");

assert(collectExceptionMsg!Exception(
        series0.getVerbose(missingDate)
    ) == "Series double[Date]: Missing 2016-Mar-01 key");

assert(collectExceptionMsg!Exception(
        series0.getExtraVerbose(missingDate, "My exception msg")
    ) == "My exception msg. Series double[Date]: Missing 2016-Mar-01 key");

// assign with get*
series0.get(refDate) = 100; 
assert(series0.get(refDate) == 100); 
series0.get(refDate) = 3; 

// tryGet
double val;
assert(series0.tryGet(refDate, val));
assert(val == 3);
assert(!series0.tryGet(missingDate, val));
assert(val == 3); // val was not changed

//////////////////////////////////////
// Merges multiple series into one.
// Allocates using GC. M
// Makes exactly two allocations per merge:
// one for index/time and one for data.
//////////////////////////////////////
auto m0 = unionSeries(series0, series1);
auto m1 = unionSeries(series1, series0); // order is matter

assert(m0.index == [
    Date(2017, 01, 01),
    Date(2017, 02, 01),
    Date(2017, 03, 01),
    Date(2017, 04, 01),
    Date(2017, 05, 01)]);

assert(m0.index == m1.index);
assert(m0.data == [ 1, 20,  3,  4, 50]);
assert(m1.data == [10, 20,  3,  4, 50]);

//////////////////////////////////////
// Joins two time-series into a one with two columns.
//////////////////////////////////////
auto u = [index0, index1].multiwayUnion;
auto index = u.move.array;
auto data = slice!double([index.length, 2], 0); // initialized to 0 value
auto series = index.series(data);

series[0 .. $, 0][].opIndexAssign(series0); // fill first column
series[0 .. $, 1][] = series1; // fill second column

assert(data == [
    [1, 10],
    [0, 20],
    [3,  0],
    [4,  0],
    [0, 50]]);

Example


import mir.series;

double[int] map;
map[1] = 4.0;
map[2] = 5.0;
map[4] = 6.0;
map[5] = 10.0;
map[10] = 11.0;

const s = series(map);

double value;
int key;
assert(s.tryGet(2, value) && value == 5.0);
assert(!s.tryGet(8, value));

assert(s.tryGetNext(2, value) && value == 5.0);
assert(s.tryGetPrev(2, value) && value == 5.0);
assert(s.tryGetNext(8, value) && value == 11.0);
assert(s.tryGetPrev(8, value) && value == 10.0);
assert(!s.tryGetFirst(8, 9, value));
assert(s.tryGetFirst(2, 10, value) && value == 5.0);
assert(s.tryGetLast(2, 10, value) && value == 11.0);
assert(s.tryGetLast(2, 8, value) && value == 10.0);

key = 2; assert(s.tryGetNextUpdateKey(key, value) && key == 2 && value == 5.0);
key = 2; assert(s.tryGetPrevUpdateKey(key, value) && key == 2 && value == 5.0);
key = 8; assert(s.tryGetNextUpdateKey(key, value) && key == 10 && value == 11.0);
key = 8; assert(s.tryGetPrevUpdateKey(key, value) && key == 5 && value == 10.0);
key = 2; assert(s.tryGetFirstUpdateLower(key, 10, value) && key == 2 && value == 5.0);
key = 10; assert(s.tryGetLastUpdateKey(2, key, value) && key == 10 && value == 11.0);
key = 8; assert(s.tryGetLastUpdateKey(2, key, value) && key == 5 && value == 10.0);

Aliases

Observation
mir_observation
SeriesMap
mir_series!(K*, V*)
Series
mir_series

Functions

observationauto
seriesauto
seriesauto
seriesauto
seriesauto
seriesReturns:
seriesReturns:
seriesReturns:
seriesautoReturns:
rcseriesautoReturns:
rcseriesautoReturns:
rcseriesautoReturns:
rcseriesautoReturns:
makeSeriesReturns:

sorted manually allocated series.

makeSeriesReturns:

sorted manually allocated series.

assocArrayautoReturns:

Associative arrays have unique keys. If r contains duplicate keys, then the result will contain the value of the last pair for that key in r.

findIndexReturns:

size_t.max
if the series does not contain the key and appropriate index otherwise.

findReturns:

0
if the series does not contain the key and appropriate backward index otherwise.

troykaLengthReturns:

Total count of lambda function calls in union handler.

troykaLengthReturns:

Total count of lambda function calls in union handler.

unionSeriesautoReturns:

sorted GC-allocated series. See_also

makeUnionSeriesautoReturns:

sorted manually allocated series. See_also

rcUnionSeriesautoReturns:

sorted manually allocated series. See_also

unionSeriesImplauto
insertOrAssign@propertyReturns:

associative array

insert@propertyReturns:

associative array

Structs

mir_observation
mir_series

Templates

troykaGalop
troykaSeries
rcTroykaSeries
troykaSeriesImpl