-
Notifications
You must be signed in to change notification settings - Fork 35
/
internalCategorical.ts
193 lines (176 loc) · 5.33 KB
/
internalCategorical.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import {ISequence, isSeqEmpty, empty} from '../internal';
import {FIRST_IS_MISSING, IDataRow, ECompareValueType, ICompareValue, ICategory, ICategoricalColumn, ICategoricalDesc, ICategoricalFilter} from '.';
import {colorPool} from './internal';
import {DEFAULT_COLOR} from './interfaces';
import {ICategoricalsColumn} from './ICategoricalColumn';
/** @internal */
export function toCategory(cat: (string | Partial<ICategory>), value: number, nextColor: () => string = () => DEFAULT_COLOR) {
if (typeof cat === 'string') {
//just the category value
return {name: cat, label: cat, color: nextColor(), value};
}
const name = cat.name == null ? String(cat.value) : cat.name;
return {
name,
label: cat.label || name,
color: cat.color || nextColor(),
value: cat.value != null ? cat.value : value
};
}
/** @internal */
export function toCompareCategoryValue(v: ICategory | null) {
if (v == null) {
return NaN;
}
return v.value;
}
export const COMPARE_CATEGORY_VALUE_TYPES = ECompareValueType.FLOAT_ASC;
function findMostFrequent(rows: ISequence<ICategory | null>, valueCache?: ISequence<ICategory | null>): {cat: ICategory | null, count: number} {
const hist = new Map<ICategory | null, number>();
if (valueCache) {
valueCache.forEach((cat) => {
hist.set(cat, (hist.get(cat) || 0) + 1);
});
} else {
rows.forEach((cat) => {
hist.set(cat, (hist.get(cat) || 0) + 1);
});
}
if (hist.size === 0) {
return {
cat: null,
count: 0
};
}
let topCat: ICategory | null = null;
let topCount = 0;
hist.forEach((count, cat) => {
if (count > topCount) {
topCat = cat;
topCount = count;
}
});
return {
cat: topCat,
count: topCount
};
}
/** @internal */
export function toMostFrequentCategoricals(rows: ISequence<IDataRow>, col: ICategoricalsColumn): (ICategory | null)[] {
if (isSeqEmpty(rows)) {
return empty(col.dataLength!);
}
const maps = empty(col.dataLength!).map(() => new Map<ICategory | null, number>());
rows.forEach((row) => {
const vs = col.getCategories(row);
if (!vs) {
return;
}
for (let i = 0; i < maps.length; ++i) {
const hist = maps[i];
const cat = vs[i] || null;
hist.set(cat, (hist.get(cat) || 0) + 1);
}
});
return maps.map((hist) => {
if (hist.size === 0) {
return null;
}
let topCat: ICategory | null = null;
let topCount = 0;
hist.forEach((count, cat) => {
if (count > topCount) {
topCat = cat;
topCount = count;
}
});
return topCat;
});
}
/** @internal */
export function toGroupCompareCategoryValue(rows: ISequence<IDataRow>, col: ICategoricalColumn, valueCache?: ISequence<ICategory | null>): ICompareValue[] {
if (isSeqEmpty(rows)) {
return [NaN, 0];
}
const mostFrequent = findMostFrequent(rows.map((d) => col.getCategory(d)), valueCache);
if (mostFrequent.cat == null) {
return [NaN, 0];
}
return [mostFrequent.cat.value, mostFrequent.count];
}
export const COMPARE_GROUP_CATEGORY_VALUE_TYPES = [ECompareValueType.FLOAT, ECompareValueType.COUNT];
/** @internal */
function compareCategory(a: ICategory | null, b: ICategory | null) {
const aNull = a == null || isNaN(a.value);
const bNull = b == null || isNaN(b.value);
if (aNull || a == null) {
return bNull ? 0 : FIRST_IS_MISSING;
}
if (bNull || b == null) {
return -FIRST_IS_MISSING;
}
if (a.value === b.value) {
return a.label.toLowerCase().localeCompare(b.label.toLowerCase());
}
return a.value - b.value;
}
export function toCategories(desc: ICategoricalDesc) {
if (!desc.categories) {
return [];
}
const nextColor = colorPool();
const l = desc.categories.length - 1;
const cats = desc.categories.map((cat, i) => toCategory(cat, i / l, nextColor));
return cats.sort(compareCategory);
}
/** @internal */
function isEmptyFilter(f: ICategoricalFilter | null) {
return f == null || (!f.filterMissing && (f.filter == null || f.filter === ''));
}
/** @internal */
export function isEqualCategoricalFilter(a: ICategoricalFilter | null, b: ICategoricalFilter | null) {
if (a === b) {
return true;
}
if (a == null || b == null) {
return isEmptyFilter(a) === isEmptyFilter(b);
}
if (a.filterMissing !== b.filterMissing || (typeof a.filter !== typeof b.filter)) {
return false;
}
if (Array.isArray(a.filter)) {
return arrayEquals(<string[]>a.filter, <string[]>b.filter);
}
return String(a.filter) === String(b.filter);
}
function arrayEquals<T>(a: T[], b: T[]) {
const al = a != null ? a.length : 0;
const bl = b != null ? b.length : 0;
if (al !== bl) {
return false;
}
if (al === 0) {
return true;
}
return a.every((ai, i) => ai === b[i]);
}
/** @internal */
export function isCategoryIncluded(filter: ICategoricalFilter | null, category: ICategory | null) {
if (filter == null) {
return true;
}
if (category == null || isNaN(category.value)) {
return !filter.filterMissing;
}
const filterObj = filter.filter;
if (Array.isArray(filterObj)) { //array mode
return filterObj.includes(category.name);
}
if (typeof filterObj === 'string' && filterObj.length > 0) { //search mode
return category.name.toLowerCase().includes(filterObj.toLowerCase());
}
if (filterObj instanceof RegExp) { //regex match mode
return filterObj.test(category.name);
}
return true;
}