/
DiscreteEventSeries.cs
100 lines (83 loc) · 3.16 KB
/
DiscreteEventSeries.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections;
using System.Collections.Generic;
using Whetstone;
using System.Linq;
namespace TextCharacteristicLearner
{
public class DiscreteEventSeries<Ty> : IEnumerable<Ty>
{
public Dictionary<string, string> labels;
public Ty[] data;
public DiscreteEventSeries (Dictionary<string, string> labels, Ty[] data)
{
this.labels = labels;
this.data = data;
}
public IEnumerator<Ty> GetEnumerator(){
return ((IEnumerable<Ty>)data).GetEnumerator(); //Late binding of Array generic types, sorry.
}
IEnumerator IEnumerable.GetEnumerator(){
return GetEnumerator ();
}
}
public static class DiscreteEventSeriesExtensions{
//
//Raw multiset:
//
public static void AddDiscreteEventSeries<Ty>(this Multiset<Ty> multiset, DiscreteEventSeries<Ty> series){
//TODO: Is it faster to make the set into a multiset, and then add the counts, so there are fewer lookups in the bigger multiset?
series.data.ForEach(a => multiset.Add (a));
}
public static Multiset<Ty> ToMultiset<Ty>(this DiscreteEventSeries<Ty> series){
Multiset<Ty> multiset = new Multiset<Ty>();
series.data.ForEach(a => multiset.Add (a)); //Doesn't use the above, as an optimization.
return multiset;
}
public static Multiset<Ty> ToMultiset<Ty>(this IEnumerable<DiscreteEventSeries<Ty>> series){
Multiset<Ty> multiset = new Multiset<Ty>();
series.ForEach (multiset.AddDiscreteEventSeries);
return multiset;
}
//
//Kmer fixed k multiset
//
public static void AddDiscreteEventSeriesKmer<Ty>(this Multiset<Kmer<Ty>> multiset, DiscreteEventSeries<Ty> series, int k ){
//Ty[] arr = series.data.ToArray();
Ty[] arr = series.data; //TODO: This is a decision.
for(int i = 0; i <= arr.Length - k; i++){
multiset.Add (new Kmer<Ty>(arr, i, k));
}
}
public static Multiset<Kmer<Ty>> ToMultisetKmer<Ty>(this DiscreteEventSeries<Ty> series, int k){
Multiset<Kmer<Ty>> multiset = new Multiset<Kmer<Ty>>();
multiset.AddDiscreteEventSeriesKmer(series, k);
return multiset;
}
public static Multiset<Kmer<Ty>> ToMultisetKmer<Ty>(this IEnumerable<DiscreteEventSeries<Ty>> series, int k){
Multiset<Kmer<Ty>> multiset = new Multiset<Kmer<Ty>>();
series.ForEach (item => multiset.AddDiscreteEventSeriesKmer(item, k));
return multiset;
}
//
//Kmer variadic k multiset
//
public static void AddDiscreteEventSeriesVarKmer<Ty>(this MultisetKmer<Ty> multiset, DiscreteEventSeries<Ty> series, int k ){
Ty[] arr = series.data;
multiset.ConsumeEventSeriesKmer(arr);
}
public static MultisetKmer<Ty> ToMultisetVarKmer<Ty>(this DiscreteEventSeries<Ty> series, int k){
MultisetKmer<Ty> multiset = new MultisetKmer<Ty>(k);
multiset.AddDiscreteEventSeriesVarKmer(series, k);
return multiset;
}
public static MultisetKmer<Ty> ToMultisetVarKmer<Ty>(this IEnumerable<DiscreteEventSeries<Ty>> series, int k){
MultisetKmer<Ty> multiset = new MultisetKmer<Ty>(k);
series.ForEach (item => multiset.AddDiscreteEventSeriesVarKmer(item, k));
return multiset;
}
public static int TotalItemCount<Ty>(this IEnumerable<DiscreteEventSeries<Ty>> data){
return data.Select (item => item.data.Length).Sum ();
}
}
}