DefaultMetricPoint.tsx 6.43 KB
Newer Older
1
import * as React from 'react';
2
import { Toggle, Stack } 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
9
10
11
import 'echarts/lib/chart/scatter';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

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

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

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

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

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

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

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

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

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

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

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

Lijiao's avatar
Lijiao committed
143
144
145
        return {
            type: 'line',
            lineStyle: { color: '#FF6600' },
146
            data
Lijiao's avatar
Lijiao committed
147
148
149
150
        };
    }

    render(): React.ReactNode {
151
        const { hasBestCurve, chartHeight } = this.props;
152
        const graph = this.generateGraph();
153
        const accNodata = graph === EmptyGraph ? 'No data' : '';
154
        const onEvents = { dataZoom: this.metricDataZoom, click: this.pointClick };
155

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

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

export default DefaultPoint;