Skip to content

Commit

Permalink
[ML] DF Analytics: Creation wizard part 3 (elastic#69456)
Browse files Browse the repository at this point in the history
* update clone tests

* validate advanced params with explain

* disable button while fetching validation data

* comment out clone tests for now
  • Loading branch information
alvarezmelissa87 committed Jun 25, 2020
1 parent 0102fb8 commit 3a4f49d
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ export enum ANALYSIS_CONFIG_TYPE {
}

export enum ANALYSIS_ADVANCED_FIELDS {
ETA = 'eta',
FEATURE_BAG_FRACTION = 'feature_bag_fraction',
FEATURE_INFLUENCE_THRESHOLD = 'feature_influence_threshold',
GAMMA = 'gamma',
LAMBDA = 'lambda',
MAX_TREES = 'max_trees',
METHOD = 'method',
N_NEIGHBORS = 'n_neighbors',
NUM_TOP_CLASSES = 'num_top_classes',
NUM_TOP_FEATURE_IMPORTANCE_VALUES = 'num_top_feature_importance_values',
OUTLIER_FRACTION = 'outlier_fraction',
RANDOMIZE_SEED = 'randomize_seed',
}

export enum OUTLIER_ANALYSIS_METHOD {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { FC, Fragment, useMemo } from 'react';
import React, { FC, Fragment, useMemo, useEffect, useState } from 'react';
import {
EuiAccordion,
EuiFieldNumber,
Expand All @@ -23,33 +23,51 @@ import { getModelMemoryLimitErrors } from '../../../analytics_management/hooks/u
import {
ANALYSIS_CONFIG_TYPE,
NUM_TOP_FEATURE_IMPORTANCE_VALUES_MIN,
ANALYSIS_ADVANCED_FIELDS,
} from '../../../../common/analytics';
import { DEFAULT_MODEL_MEMORY_LIMIT } from '../../../analytics_management/hooks/use_create_analytics_form/state';
import { ANALYTICS_STEPS } from '../../page';
import { fetchExplainData } from '../shared';
import { ContinueButton } from '../continue_button';
import { OutlierHyperParameters } from './outlier_hyper_parameters';

export function getNumberValue(value?: number) {
return value === undefined ? '' : +value;
}

export type AdvancedParamErrors = {
[key in ANALYSIS_ADVANCED_FIELDS]?: string;
};

export const AdvancedStepForm: FC<CreateAnalyticsStepProps> = ({
actions,
state,
setCurrentStep,
}) => {
const [advancedParamErrors, setAdvancedParamErrors] = useState<AdvancedParamErrors>({});
const [fetchingAdvancedParamErrors, setFetchingAdvancedParamErrors] = useState<boolean>(false);

const { setFormState } = actions;
const { form, isJobCreated } = state;
const {
computeFeatureInfluence,
eta,
featureBagFraction,
featureInfluenceThreshold,
gamma,
jobType,
lambda,
maxTrees,
method,
modelMemoryLimit,
modelMemoryLimitValidationResult,
nNeighbors,
numTopClasses,
numTopFeatureImportanceValues,
numTopFeatureImportanceValuesValid,
outlierFraction,
predictionFieldName,
randomizeSeed,
} = form;

const mmlErrors = useMemo(() => getModelMemoryLimitErrors(modelMemoryLimitValidationResult), [
Expand All @@ -61,6 +79,43 @@ export const AdvancedStepForm: FC<CreateAnalyticsStepProps> = ({

const mmlInvalid = modelMemoryLimitValidationResult !== null;

const isStepInvalid =
mmlInvalid ||
Object.keys(advancedParamErrors).length > 0 ||
fetchingAdvancedParamErrors === true;

useEffect(() => {
setFetchingAdvancedParamErrors(true);
(async function () {
const { success, errorMessage } = await fetchExplainData(form);
const paramErrors: AdvancedParamErrors = {};

if (!success) {
// Check which field is invalid
Object.values(ANALYSIS_ADVANCED_FIELDS).forEach((param) => {
if (errorMessage.includes(`[${param}]`)) {
paramErrors[param] = errorMessage;
}
});
}
setFetchingAdvancedParamErrors(false);
setAdvancedParamErrors(paramErrors);
})();
}, [
eta,
featureBagFraction,
featureInfluenceThreshold,
gamma,
lambda,
maxTrees,
method,
nNeighbors,
numTopClasses,
numTopFeatureImportanceValues,
outlierFraction,
randomizeSeed,
]);

const outlierDetectionAdvancedConfig = (
<Fragment>
<EuiFlexItem style={{ minWidth: '30%' }}>
Expand Down Expand Up @@ -126,6 +181,10 @@ export const AdvancedStepForm: FC<CreateAnalyticsStepProps> = ({
'The minimum outlier score that a document needs to have in order to calculate its feature influence score. Value range: 0-1. Defaults to 0.1.',
}
)}
isInvalid={
advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.FEATURE_INFLUENCE_THRESHOLD] !== undefined
}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.FEATURE_INFLUENCE_THRESHOLD]}
>
<EuiFieldNumber
onChange={(e) =>
Expand Down Expand Up @@ -315,14 +374,24 @@ export const AdvancedStepForm: FC<CreateAnalyticsStepProps> = ({
>
<EuiFlexGroup wrap>
{jobType === ANALYSIS_CONFIG_TYPE.OUTLIER_DETECTION && (
<OutlierHyperParameters actions={actions} state={state} />
<OutlierHyperParameters
actions={actions}
state={state}
advancedParamErrors={advancedParamErrors}
/>
)}
{isRegOrClassJob && (
<HyperParameters
actions={actions}
state={state}
advancedParamErrors={advancedParamErrors}
/>
)}
{isRegOrClassJob && <HyperParameters actions={actions} state={state} />}
</EuiFlexGroup>
</EuiAccordion>
<EuiSpacer />
<ContinueButton
isDisabled={mmlInvalid}
isDisabled={isStepInvalid}
onClick={() => {
setCurrentStep(ANALYTICS_STEPS.DETAILS);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import React, { FC, Fragment } from 'react';
import { EuiFieldNumber, EuiFlexItem, EuiFormRow } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { CreateAnalyticsFormProps } from '../../../analytics_management/hooks/use_create_analytics_form';
import { getNumberValue } from './advanced_step_form';
import { AdvancedParamErrors, getNumberValue } from './advanced_step_form';
import { ANALYSIS_ADVANCED_FIELDS } from '../../../../common/analytics';

const MAX_TREES_LIMIT = 2000;

export const HyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }) => {
interface Props extends CreateAnalyticsFormProps {
advancedParamErrors: AdvancedParamErrors;
}

export const HyperParameters: FC<Props> = ({ actions, state, advancedParamErrors }) => {
const { setFormState } = actions;

const { eta, featureBagFraction, gamma, lambda, maxTrees, randomizeSeed } = state.form;
Expand All @@ -28,6 +33,8 @@ export const HyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }
defaultMessage:
'Regularization parameter to prevent overfitting on the training data set. Must be a non negative value.',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.LAMBDA] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.LAMBDA]}
>
<EuiFieldNumber
aria-label={i18n.translate('xpack.ml.dataframe.analytics.create.lambdaInputAriaLabel', {
Expand All @@ -52,6 +59,8 @@ export const HyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }
helpText={i18n.translate('xpack.ml.dataframe.analytics.create.maxTreesText', {
defaultMessage: 'The maximum number of trees the forest is allowed to contain.',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.MAX_TREES] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.MAX_TREES]}
>
<EuiFieldNumber
aria-label={i18n.translate(
Expand Down Expand Up @@ -81,6 +90,8 @@ export const HyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }
defaultMessage:
'Multiplies a linear penalty associated with the size of individual trees in the forest. Must be non-negative value.',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.GAMMA] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.GAMMA]}
>
<EuiFieldNumber
aria-label={i18n.translate('xpack.ml.dataframe.analytics.create.gammaInputAriaLabel', {
Expand All @@ -105,6 +116,8 @@ export const HyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }
helpText={i18n.translate('xpack.ml.dataframe.analytics.create.etaText', {
defaultMessage: 'The shrinkage applied to the weights. Must be between 0.001 and 1.',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.ETA] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.ETA]}
>
<EuiFieldNumber
aria-label={i18n.translate('xpack.ml.dataframe.analytics.create.etaInputAriaLabel', {
Expand All @@ -130,6 +143,10 @@ export const HyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }
defaultMessage:
'The fraction of features used when selecting a random bag for each candidate split.',
})}
isInvalid={
advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.FEATURE_BAG_FRACTION] !== undefined
}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.FEATURE_BAG_FRACTION]}
>
<EuiFieldNumber
aria-label={i18n.translate(
Expand Down Expand Up @@ -158,12 +175,14 @@ export const HyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }
<EuiFlexItem style={{ minWidth: '30%' }}>
<EuiFormRow
label={i18n.translate('xpack.ml.dataframe.analytics.create.randomizeSeedLabel', {
defaultMessage: 'Randomized seed',
defaultMessage: 'Randomize seed',
})}
helpText={i18n.translate('xpack.ml.dataframe.analytics.create.randomizeSeedText', {
defaultMessage:
'The seed to the random generator that is used to pick which documents will be used for training.',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.RANDOMIZE_SEED] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.RANDOMIZE_SEED]}
>
<EuiFieldNumber
aria-label={i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import React, { FC, Fragment } from 'react';
import { EuiFieldNumber, EuiFlexItem, EuiFormRow, EuiSelect } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { OUTLIER_ANALYSIS_METHOD } from '../../../../common/analytics';
import { OUTLIER_ANALYSIS_METHOD, ANALYSIS_ADVANCED_FIELDS } from '../../../../common/analytics';
import { CreateAnalyticsFormProps } from '../../../analytics_management/hooks/use_create_analytics_form';
import { getNumberValue } from './advanced_step_form';
import { AdvancedParamErrors, getNumberValue } from './advanced_step_form';

export const OutlierHyperParameters: FC<CreateAnalyticsFormProps> = ({ actions, state }) => {
interface Props extends CreateAnalyticsFormProps {
advancedParamErrors: AdvancedParamErrors;
}

export const OutlierHyperParameters: FC<Props> = ({ actions, state, advancedParamErrors }) => {
const { setFormState } = actions;

const { method, nNeighbors, outlierFraction, standardizationEnabled } = state.form;
Expand All @@ -27,6 +31,8 @@ export const OutlierHyperParameters: FC<CreateAnalyticsFormProps> = ({ actions,
defaultMessage:
'Sets the method that outlier detection uses. If not set, uses an ensemble of different methods and normalises and combines their individual outlier scores to obtain the overall outlier score. We recommend to use the ensemble method',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.METHOD] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.METHOD]}
>
<EuiSelect
options={Object.values(OUTLIER_ANALYSIS_METHOD).map((outlierMethod) => ({
Expand All @@ -51,6 +57,8 @@ export const OutlierHyperParameters: FC<CreateAnalyticsFormProps> = ({ actions,
defaultMessage:
'The value for how many nearest neighbors each method of outlier detection will use to calculate its outlier score. When not set, different values will be used for different ensemble members. Must be a positive integer',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.N_NEIGHBORS] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.N_NEIGHBORS]}
>
<EuiFieldNumber
aria-label={i18n.translate(
Expand Down Expand Up @@ -79,6 +87,8 @@ export const OutlierHyperParameters: FC<CreateAnalyticsFormProps> = ({ actions,
defaultMessage:
'Sets the proportion of the data set that is assumed to be outlying prior to outlier detection.',
})}
isInvalid={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.OUTLIER_FRACTION] !== undefined}
error={advancedParamErrors[ANALYSIS_ADVANCED_FIELDS.OUTLIER_FRACTION]}
>
<EuiFieldNumber
aria-label={i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const MemoizedAnalysisFieldsTable: FC<{
</EuiCallOut>
)}
{tableItems.length > 0 && (
<EuiPanel paddingSize="m">
<EuiPanel paddingSize="m" data-test-subj="mlAnalyticsCreateJobWizardExcludesSelect">
<CustomSelectionTable
data-test-subj="mlAnalyticsCreationAnalysisFieldsTable"
checkboxDisabledCheck={checkboxDisabledCheck}
Expand Down
Loading

0 comments on commit 3a4f49d

Please sign in to comment.