DefaultMetricPoint.tsx 5.93 KB
Newer Older
1
import * as React from 'react';
2
import { Toggle, Stack } from '@fluentui/react';
3
import ReactEcharts from 'echarts-for-react';
4
5
import { EXPERIMENT, TRIALS } from '../../static/datamodel';
import { Trial } from '../../static/model/trial';
Lijiao's avatar
Lijiao committed
6
import { TooltipForAccuracy, EventMap } from '../../static/interface';
7
8
9
10
import 'echarts/lib/chart/scatter';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

Lijiao's avatar
Lijiao committed
11
12
13
14
15
16
const EmptyGraph = {
    grid: {
        left: '8%'
    },
    xAxis: {
        name: 'Trial',
17
        type: 'category'
Lijiao's avatar
Lijiao committed
18
19
20
    },
    yAxis: {
        name: 'Default metric',
21
        type: 'value'
Lijiao's avatar
Lijiao committed
22
23
    }
};
24

25
interface DefaultPointProps {
26
27
    trialIds: string[];
    visible: boolean;
28
29
30
}

interface DefaultPointState {
31
    bestCurveEnabled?: boolean | undefined;
32
    startY: number; // dataZoomY
Lijiao's avatar
Lijiao committed
33
    endY: number;
34
35
36
37
38
}

class DefaultPoint extends React.Component<DefaultPointProps, DefaultPointState> {
    constructor(props: DefaultPointProps) {
        super(props);
Lijiao's avatar
Lijiao committed
39
40
41
        this.state = {
            bestCurveEnabled: false,
            startY: 0, // dataZoomY
42
            endY: 100
Lijiao's avatar
Lijiao committed
43
        };
44
45
    }

46
    loadDefault = (ev: React.MouseEvent<HTMLElement>, checked?: boolean): void => {
47
        this.setState({ bestCurveEnabled: checked });
48
    };
49

Lijiao's avatar
Lijiao committed
50
    shouldComponentUpdate(nextProps: DefaultPointProps): boolean {
51
        return nextProps.visible;
52
    }
53

54
55
56
    metricDataZoom = (e: EventMap): void => {
        if (e.batch !== undefined) {
            this.setState(() => ({
57
58
                startY: e.batch[0].start !== null ? e.batch[0].start : 0,
                endY: e.batch[0].end !== null ? e.batch[0].end : 100
59
60
            }));
        }
61
    };
62
63
64
65
66

    generateGraphConfig(maxSequenceId: number): any {
        const { startY, endY } = this.state;
        return {
            grid: {
67
                left: '8%'
68
69
70
71
            },
            tooltip: {
                trigger: 'item',
                enterable: true,
72
73
74
75
76
                position: (point: number[], data: TooltipForAccuracy): number[] => [
                    data.data[0] < maxSequenceId ? point[0] : point[0] - 300,
                    80
                ],
                formatter: (data: TooltipForAccuracy): React.ReactNode =>
77
                    '<div class="tooldetailAccuracy">' +
78
79
80
81
82
83
84
85
86
                    '<div>Trial No.: ' +
                    data.data[0] +
                    '</div>' +
                    '<div>Default metric: ' +
                    data.data[1] +
                    '</div>' +
                    '<div>Parameters: <pre>' +
                    JSON.stringify(data.data[2], null, 4) +
                    '</pre></div>' +
87
88
89
90
91
92
93
94
95
96
97
98
99
100
                    '</div>'
            },
            dataZoom: [
                {
                    id: 'dataZoomY',
                    type: 'inside',
                    yAxisIndex: [0],
                    filterMode: 'empty',
                    start: startY,
                    end: endY
                }
            ],
            xAxis: {
                name: 'Trial',
101
                type: 'category'
102
103
104
105
            },
            yAxis: {
                name: 'Default metric',
                type: 'value',
106
                scale: true
107
            },
108
            series: undefined
109
110
111
112
        };
    }

    generateScatterSeries(trials: Trial[]): any {
113
        const data = trials.map(trial => [trial.sequenceId, trial.accuracy, trial.description.parameters]);
Lijiao's avatar
Lijiao committed
114
115
116
        return {
            symbolSize: 6,
            type: 'scatter',
117
            data
Lijiao's avatar
Lijiao committed
118
119
        };
    }
120
121

    generateBestCurveSeries(trials: Trial[]): any {
Lijiao's avatar
Lijiao committed
122
123
        let best = trials[0];
        const data = [[best.sequenceId, best.accuracy, best.description.parameters]];
124

Lijiao's avatar
Lijiao committed
125
126
        for (let i = 1; i < trials.length; i++) {
            const trial = trials[i];
127
128
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            const delta = trial.accuracy! - best.accuracy!;
129
            const better = EXPERIMENT.optimizeMode === 'minimize' ? delta < 0 : delta > 0;
130
131
132
133
134
            if (better) {
                data.push([trial.sequenceId, trial.accuracy, trial.description.parameters]);
                best = trial;
            } else {
                data.push([trial.sequenceId, best.accuracy, trial.description.parameters]);
Lijiao's avatar
Lijiao committed
135
136
            }
        }
137

Lijiao's avatar
Lijiao committed
138
139
140
        return {
            type: 'line',
            lineStyle: { color: '#FF6600' },
141
            data
Lijiao's avatar
Lijiao committed
142
143
144
145
        };
    }

    render(): React.ReactNode {
146
        const graph = this.generateGraph();
147
148
        const accNodata = graph === EmptyGraph ? 'No data' : '';
        const onEvents = { dataZoom: this.metricDataZoom };
149

150
151
        return (
            <div>
152
153
                <Stack horizontalAlign='end' className='default-metric'>
                    <Toggle label='Optimization curve' inlineLabel onChange={this.loadDefault} />
154
                </Stack>
155
                <div className='default-metric-graph'>
156
157
158
159
160
                    <ReactEcharts
                        option={graph}
                        style={{
                            width: '100%',
                            height: 402,
161
                            margin: '0 auto'
162
                        }}
163
                        theme='my_theme'
164
165
166
                        notMerge={true} // update now
                        onEvents={onEvents}
                    />
167
                    <div className='default-metric-noData'>{accNodata}</div>
168
                </div>
169
170
171
            </div>
        );
    }
172

Lijiao's avatar
Lijiao committed
173
    private generateGraph(): any {
174
175
176
177
        const trials = TRIALS.getTrials(this.props.trialIds).filter(trial => trial.sortable);
        if (trials.length === 0) {
            return EmptyGraph;
        }
Lijiao's avatar
Lijiao committed
178
        const graph = this.generateGraphConfig(trials[trials.length - 1].sequenceId);
179
        if (this.state.bestCurveEnabled) {
Lijiao's avatar
Lijiao committed
180
            (graph as any).series = [this.generateBestCurveSeries(trials), this.generateScatterSeries(trials)];
181
        } else {
Lijiao's avatar
Lijiao committed
182
            (graph as any).series = [this.generateScatterSeries(trials)];
183
184
185
186
187
188
        }
        return graph;
    }
}

export default DefaultPoint;