DefaultMetricPoint.tsx 6.53 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
88
89
90
91
92
93
94
95
96
97
                formatter: (data: TooltipForAccuracy): React.ReactNode => {
                    return (
                        '<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>'
                    );
                }
98
99
100
101
102
103
104
105
106
107
108
109
110
            },
            dataZoom: [
                {
                    id: 'dataZoomY',
                    type: 'inside',
                    yAxisIndex: [0],
                    filterMode: 'empty',
                    start: startY,
                    end: endY
                }
            ],
            xAxis: {
                name: 'Trial',
111
                type: 'category'
112
113
114
115
            },
            yAxis: {
                name: 'Default metric',
                type: 'value',
116
                scale: true
117
            },
118
            series: undefined
119
120
121
122
        };
    }

    generateScatterSeries(trials: Trial[]): any {
123
        const data = trials.map(trial => [trial.sequenceId, trial.accuracy, trial.id, trial.description.parameters]);
Lijiao's avatar
Lijiao committed
124
125
126
        return {
            symbolSize: 6,
            type: 'scatter',
127
            data
Lijiao's avatar
Lijiao committed
128
129
        };
    }
130
131

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

Lijiao's avatar
Lijiao committed
135
136
        for (let i = 1; i < trials.length; i++) {
            const trial = trials[i];
137
138
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            const delta = trial.accuracy! - best.accuracy!;
139
            const better = EXPERIMENT.optimizeMode === 'minimize' ? delta < 0 : delta > 0;
140
            if (better) {
141
                data.push([trial.sequenceId, trial.accuracy, best.id, trial.description.parameters]);
142
143
                best = trial;
            } else {
144
                data.push([trial.sequenceId, best.accuracy, best.id, trial.description.parameters]);
Lijiao's avatar
Lijiao committed
145
146
            }
        }
147

Lijiao's avatar
Lijiao committed
148
149
150
        return {
            type: 'line',
            lineStyle: { color: '#FF6600' },
151
            data
Lijiao's avatar
Lijiao committed
152
153
154
155
        };
    }

    render(): React.ReactNode {
156
        const { hasBestCurve, chartHeight } = this.props;
157
        const graph = this.generateGraph();
158
        const accNodata = graph === EmptyGraph ? 'No data' : '';
159
        const onEvents = { dataZoom: this.metricDataZoom, click: this.pointClick };
160

161
162
        return (
            <div>
163
164
165
166
167
                {hasBestCurve && (
                    <Stack horizontalAlign='end' className='default-metric'>
                        <Toggle label='Optimization curve' inlineLabel onChange={this.loadDefault} />
                    </Stack>
                )}
168
                <div className='default-metric-graph'>
169
170
171
172
                    <ReactEcharts
                        option={graph}
                        style={{
                            width: '100%',
173
                            height: chartHeight,
174
                            margin: '0 auto'
175
                        }}
176
                        theme='nni_theme'
177
178
179
                        notMerge={true} // update now
                        onEvents={onEvents}
                    />
180
                    <div className='default-metric-noData'>{accNodata}</div>
181
                </div>
182
183
184
            </div>
        );
    }
185

Lijiao's avatar
Lijiao committed
186
    private generateGraph(): any {
187
188
189
190
        const trials = TRIALS.getTrials(this.props.trialIds).filter(trial => trial.sortable);
        if (trials.length === 0) {
            return EmptyGraph;
        }
Lijiao's avatar
Lijiao committed
191
        const graph = this.generateGraphConfig(trials[trials.length - 1].sequenceId);
192
        if (this.state.bestCurveEnabled) {
Lijiao's avatar
Lijiao committed
193
            (graph as any).series = [this.generateBestCurveSeries(trials), this.generateScatterSeries(trials)];
194
        } else {
Lijiao's avatar
Lijiao committed
195
            (graph as any).series = [this.generateScatterSeries(trials)];
196
197
198
199
200
201
        }
        return graph;
    }
}

export default DefaultPoint;