index.ts 7.3 KB
Newer Older
1 2 3 4 5
import * as chart from '~/utils/chart';

import {ChartDataParams, RangeParams, TooltipData, TransformParams, xAxisMap} from './types';
import {formatTime, quantile} from '~/utils';

6
import BigNumber from 'bignumber.js';
7
import {I18n} from '@visualdl/i18n';
8
import cloneDeep from 'lodash/cloneDeep';
9
import compact from 'lodash/compact';
10
import maxBy from 'lodash/maxBy';
11
import minBy from 'lodash/minBy';
12 13
import sortBy from 'lodash/sortBy';

14 15
BigNumber.config({EXPONENTIAL_AT: [-6, 7]});

16 17 18 19 20 21 22 23 24 25 26 27 28 29
export * from './types';

export const sortingMethodMap = {
    default: null,
    descending: (points: TooltipData[]) => sortBy(points, point => point.item[3]).reverse(),
    ascending: (points: TooltipData[]) => sortBy(points, point => point.item[3]),
    // Compare other ponts width the trigger point, caculate the nearest sort.
    nearest: (points: TooltipData[], data: number[]) => sortBy(points, point => point.item[3] - data[2])
};

export const transform = ({datasets, smoothing}: TransformParams) =>
    // https://en.wikipedia.org/wiki/Moving_average
    datasets.map(seriesData => {
        const data = cloneDeep(seriesData);
30
        let last = new BigNumber(data.length > 0 ? 0 : Number.NaN);
31 32
        let numAccum = 0;
        let startValue = 0;
33
        const bigSmoothing = new BigNumber(smoothing);
34
        data.forEach((d, i) => {
35
            const nextVal = new BigNumber(d[2]);
36 37 38 39 40 41 42
            // second to millisecond.
            const millisecond = (d[0] = Math.floor(d[0] * 1000));
            if (i === 0) {
                startValue = millisecond;
            }
            // Relative time, millisecond to hours.
            d[4] = Math.floor(millisecond - startValue) / (60 * 60 * 1000);
43 44
            if (!nextVal.isFinite()) {
                d[3] = nextVal.toNumber();
45
            } else {
46 47
                // last = last * smoothing + (1 - smoothing) * nextVal;
                last = last.multipliedBy(bigSmoothing).plus(bigSmoothing.minus(1).negated().multipliedBy(nextVal));
48
                numAccum++;
49 50 51 52
                let debiasWeight = new BigNumber(1);
                if (!bigSmoothing.isEqualTo(1)) {
                    //debiasWeight = 1.0 - Math.pow(smoothing, numAccum);
                    debiasWeight = bigSmoothing.exponentiatedBy(numAccum).minus(1).negated();
53
                }
54 55
                // d[3] = last / debiasWeight;
                d[3] = last.dividedBy(debiasWeight).toNumber();
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            }
        });
        return data;
    });

export const chartData = ({data, runs, smooth, xAxis}: ChartDataParams) =>
    data
        .map((dataset, i) => {
            // smoothed data:
            // [0] wall time
            // [1] step
            // [2] orginal value
            // [3] smoothed value
            // [4] relative
            const name = runs[i];
71 72
            const color = chart.color[i % chart.color.length];
            const colorAlt = chart.colorAlt[i % chart.colorAlt.length];
73 74 75 76 77
            return [
                {
                    name,
                    z: i,
                    lineStyle: {
78 79
                        color: colorAlt,
                        width: chart.series.lineStyle.width
80 81 82 83 84 85 86 87 88 89 90
                    },
                    data: dataset,
                    encode: {
                        x: [xAxisMap[xAxis]],
                        y: [2]
                    },
                    smooth
                },
                {
                    name,
                    z: runs.length + i,
91 92 93
                    itemStyle: {
                        color
                    },
94 95 96 97 98 99 100 101 102 103 104
                    data: dataset,
                    encode: {
                        x: [xAxisMap[xAxis]],
                        y: [3]
                    },
                    smooth
                }
            ];
        })
        .flat();

105 106 107 108 109
export const singlePointRange = (value: number) => ({
    min: value ? Math.min(value * 2, 0) : -0.5,
    max: value ? Math.max(value * 2, 0) : 0.5
});

110 111 112 113
export const range = ({datasets, outlier}: RangeParams) => {
    const ranges = compact(
        datasets?.map(dataset => {
            if (dataset.length == 0) return;
114
            const values = dataset.map(v => v[2]);
115 116 117
            if (!outlier) {
                // Get the orgin data range.
                return {
118 119
                    min: Math.min(...values) ?? 0,
                    max: Math.max(...values) ?? 0
120 121 122
                };
            } else {
                // Get the quantile range.
123
                const sorted = dataset.map(v => v[2]).sort();
124
                return {
125 126
                    min: quantile(sorted, 0.05),
                    max: quantile(values, 0.95)
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
                };
            }
        })
    );

    const min = minBy(ranges, range => range.min)?.min ?? 0;
    const max = maxBy(ranges, range => range.max)?.max ?? 0;

    if (!(min === 0 && max === 0)) {
        return {
            min: min > 0 ? min * 0.9 : min * 1.1,
            max: max > 0 ? max * 1.1 : max * 0.9
        };
    }
};

// TODO: make it better, don't concat html
export const tooltip = (data: TooltipData[], i18n: I18n) => {
    const indexPropMap = {
        Time: 0,
        Step: 1,
        Value: 2,
        Smoothed: 3,
        Relative: 4
    };
    const widthPropMap = {
        Run: 60,
        Time: 120,
        Step: 40,
156
        Value: 60,
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        Smoothed: 60,
        Relative: 60
    };
    const translatePropMap = {
        Run: 'common:runs',
        Time: 'scalars:x-axis-value.wall',
        Step: 'scalars:x-axis-value.step',
        Value: 'scalars:value',
        Smoothed: 'scalars:smoothed',
        Relative: 'scalars:x-axis-value.relative'
    };
    const transformedData = data.map(item => {
        const data = item.item;
        return {
            Run: item.run,
172 173 174
            // use precision then toString to remove trailling 0
            Smoothed: new BigNumber(data[indexPropMap.Smoothed] ?? Number.NaN).precision(5).toString(),
            Value: new BigNumber(data[indexPropMap.Smoothed] ?? Number.NaN).precision(5).toString(),
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            Step: data[indexPropMap.Step],
            Time: formatTime(data[indexPropMap.Time], i18n.language),
            // Relative display value should take easy-read into consideration.
            // Better to tranform data to 'day:hour', 'hour:minutes', 'minute: seconds' and second only.
            Relative: Math.floor(data[indexPropMap.Relative] * 60 * 60) + 's'
        };
    });

    let headerHtml = '<tr style="font-size:14px;">';
    headerHtml += (Object.keys(transformedData[0]) as (keyof typeof transformedData[0])[])
        .map(key => {
            return `<td style="padding: 0 4px; font-weight: bold; width: ${widthPropMap[key]}px;">${i18n.t(
                translatePropMap[key]
            )}</td>`;
        })
        .join('');
    headerHtml += '</tr>';

    const content = transformedData
        .map(item => {
            let str = '<tr style="font-size:12px;">';
            str += Object.keys(item)
                .map(val => {
                    return `<td style="padding: 0 4px; overflow: hidden;">${item[val as keyof typeof item]}</td>`;
                })
                .join('');
            str += '</tr>';
            return str;
        })
        .join('');

    // eslint-disable-next-line
    return `<table style="text-align: left;table-layout: fixed;width: 500px;"><thead>${headerHtml}</thead><tbody>${content}</tbody><table>`;
};