DefaultMetricPoint.tsx 7.58 KB
Newer Older
1
import * as React from 'react';
2
import { Stack, Dropdown, Toggle, IDropdownOption } from '@fluentui/react';
3
import ReactEcharts from 'echarts-for-react';
Lijiaoa's avatar
Lijiaoa committed
4
5
6
7
import { Trial } from '@model/trial';
import { EXPERIMENT, TRIALS } from '@static/datamodel';
import { TooltipForAccuracy, EventMap } from '@static/interface';
import { reformatRetiariiParameter } from '@static/function';
8
import { gap15 } from '@components/fluent/ChildrenGap';
Lijiaoa's avatar
Lijiaoa committed
9
import { optimizeModeValue } from './optimizeMode';
10
11
12
13
import 'echarts/lib/chart/scatter';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

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

28
interface DefaultPointProps {
29
    trialIds: string[];
30
31
    chartHeight: number;
    hasBestCurve: boolean;
32
    changeExpandRowIDs: Function;
33
34
35
}

interface DefaultPointState {
36
    bestCurveEnabled?: boolean | undefined;
37
    startY: number; // dataZoomY
Lijiao's avatar
Lijiao committed
38
    endY: number;
39
    userSelectOptimizeMode: string;
40
41
42
43
44
}

class DefaultPoint extends React.Component<DefaultPointProps, DefaultPointState> {
    constructor(props: DefaultPointProps) {
        super(props);
Lijiao's avatar
Lijiao committed
45
46
47
        this.state = {
            bestCurveEnabled: false,
            startY: 0, // dataZoomY
48
            endY: 100,
Lijiaoa's avatar
Lijiaoa committed
49
            userSelectOptimizeMode: optimizeModeValue(EXPERIMENT.optimizeMode)
Lijiao's avatar
Lijiao committed
50
        };
51
52
    }

53
    loadDefault = (ev: React.MouseEvent<HTMLElement>, checked?: boolean): void => {
54
        this.setState({ bestCurveEnabled: checked });
55
    };
56

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

66
    pointClick = (params: any): void => {
67
68
69
        // [hasBestCurve: true]: is detail page, otherwise, is overview page
        const { hasBestCurve } = this.props;
        if (!hasBestCurve) {
70
71
72
73
74
            this.props.changeExpandRowIDs(params.data[2], 'chart');
        }
    };

    generateGraphConfig(_maxSequenceId: number): any {
75
        const { startY, endY } = this.state;
76
        const { hasBestCurve } = this.props;
77
78
        return {
            grid: {
79
                left: '8%'
80
81
82
            },
            tooltip: {
                trigger: 'item',
83
                enterable: hasBestCurve,
84
                confine: true, // confirm always show tooltip box rather than hidden by background
85
86
87
88
89
                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>
90
91
92
93
94
                        <div>Parameters: <pre>${JSON.stringify(
                            reformatRetiariiParameter(data.data[3]),
                            null,
                            4
                        )}</pre></div>
95
96
                    </div>
                `
97
98
99
100
101
102
103
104
105
106
107
108
109
            },
            dataZoom: [
                {
                    id: 'dataZoomY',
                    type: 'inside',
                    yAxisIndex: [0],
                    filterMode: 'empty',
                    start: startY,
                    end: endY
                }
            ],
            xAxis: {
                name: 'Trial',
110
                type: 'category'
111
112
113
114
            },
            yAxis: {
                name: 'Default metric',
                type: 'value',
115
                scale: true
116
            },
117
            series: undefined
118
119
120
121
        };
    }

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

    generateBestCurveSeries(trials: Trial[]): any {
131
        const { userSelectOptimizeMode } = this.state;
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 = userSelectOptimizeMode === '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
156
157
158
159
160
161
    // get user mode number 'max' or 'min'
    updateUserOptimizeMode = (event: React.FormEvent<HTMLDivElement>, item?: IDropdownOption): void => {
        if (item !== undefined) {
            this.setState({ userSelectOptimizeMode: item.key.toString() });
        }
    };

Lijiao's avatar
Lijiao committed
162
    render(): React.ReactNode {
163
        const { hasBestCurve, chartHeight } = this.props;
164
        const { userSelectOptimizeMode } = this.state;
165
        const graph = this.generateGraph();
166
        const accNodata = graph === EmptyGraph ? 'No data' : '';
167
        const onEvents = { dataZoom: this.metricDataZoom, click: this.pointClick };
168

169
170
        return (
            <div>
171
                {hasBestCurve && (
172
                    <Stack horizontal reversed tokens={gap15} className='default-metric'>
173
                        <Toggle label='Optimization curve' inlineLabel onChange={this.loadDefault} />
174
175
176
177
178
179
180
181
182
183
                        <Dropdown
                            selectedKey={userSelectOptimizeMode}
                            onChange={this.updateUserOptimizeMode}
                            options={[
                                { key: 'maximize', text: 'Maximize' },
                                { key: 'minimize', text: 'Minimize' }
                            ]}
                            styles={{ dropdown: { width: 100 } }}
                            className='para-filter-percent'
                        />
184
185
                    </Stack>
                )}
186
                <div className='default-metric-graph graph'>
187
188
189
190
                    <ReactEcharts
                        option={graph}
                        style={{
                            width: '100%',
191
                            height: chartHeight,
192
                            margin: '0 auto'
193
                        }}
194
                        theme='nni_theme'
195
196
197
                        notMerge={true} // update now
                        onEvents={onEvents}
                    />
Lijiaoa's avatar
Lijiaoa committed
198
                    <div className='default-metric-noData fontColor333'>{accNodata}</div>
199
                </div>
200
201
202
            </div>
        );
    }
203

Lijiao's avatar
Lijiao committed
204
    private generateGraph(): any {
205
206
207
208
        const trials = TRIALS.getTrials(this.props.trialIds).filter(trial => trial.sortable);
        if (trials.length === 0) {
            return EmptyGraph;
        }
Lijiao's avatar
Lijiao committed
209
        const graph = this.generateGraphConfig(trials[trials.length - 1].sequenceId);
210
        if (this.state.bestCurveEnabled) {
Lijiao's avatar
Lijiao committed
211
            (graph as any).series = [this.generateBestCurveSeries(trials), this.generateScatterSeries(trials)];
212
        } else {
Lijiao's avatar
Lijiao committed
213
            (graph as any).series = [this.generateScatterSeries(trials)];
214
215
216
217
218
219
        }
        return graph;
    }
}

export default DefaultPoint;