DefaultMetricPoint.tsx 6.25 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
    trialIds: string[];
27
28
    chartHeight: number;
    hasBestCurve: boolean;
29
    changeExpandRowIDs: Function;
30
31
32
}

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

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

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

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

61
    pointClick = (params: any): void => {
62
63
64
        // [hasBestCurve: true]: is detail page, otherwise, is overview page
        const { hasBestCurve } = this.props;
        if (!hasBestCurve) {
65
66
67
68
69
            this.props.changeExpandRowIDs(params.data[2], 'chart');
        }
    };

    generateGraphConfig(_maxSequenceId: number): any {
70
        const { startY, endY } = this.state;
71
        const { hasBestCurve } = this.props;
72
73
        return {
            grid: {
74
                left: '8%'
75
76
77
            },
            tooltip: {
                trigger: 'item',
78
                enterable: hasBestCurve,
79
                confine: true, // confirm always show tooltip box rather than hidden by background
80
81
82
83
84
85
86
87
                formatter: (data: TooltipForAccuracy): React.ReactNode => `
                    <div class="tooldetailAccuracy">
                        <div>Trial No.: ${data.data[0]}</div>
                        <div>Trial ID: ${data.data[2]}</div>
                        <div>Default metric: ${data.data[1]}</div>
                        <div>Parameters: <pre>${JSON.stringify(data.data[3], null, 4)}</pre></div>
                    </div>
                `
88
89
90
91
92
93
94
95
96
97
98
99
100
            },
            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.id, 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
        let best = trials[0];
123
        const data = [[best.sequenceId, best.accuracy, best.id, 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
            if (better) {
131
                data.push([trial.sequenceId, trial.accuracy, best.id, trial.description.parameters]);
132
133
                best = trial;
            } else {
134
                data.push([trial.sequenceId, best.accuracy, best.id, 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 { hasBestCurve, chartHeight } = this.props;
147
        const graph = this.generateGraph();
148
        const accNodata = graph === EmptyGraph ? 'No data' : '';
149
        const onEvents = { dataZoom: this.metricDataZoom, click: this.pointClick };
150

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

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

export default DefaultPoint;