diff --git a/docs/context/architecture.md b/docs/context/architecture.md index 26f4be5da..185ae8de9 100644 --- a/docs/context/architecture.md +++ b/docs/context/architecture.md @@ -69,6 +69,8 @@ flowchart TD - The app uses Angular Signals heavily for feature state. - `ExperimentStudioService` is the main orchestration layer for selected data models, datasets, variables, covariates, filters, algorithm configuration, run/save flows, preview calls, edit hydration, and sessionStorage persistence. - Transformation (derived-column) state is an ordered `appliedTransformations: TransformationStep[]` signal on that service — not a keyed map. Singletons (`missing_values_handler`, outlier, longitudinal) stay in the preprocessing config; column creators (`categorical_column_creator`, `kmeans_cluster_creator`, `pca_column_creator`) live only in the ordered list and emit in list order after the singletons. Creator output codes come from `new_column_names` (never a legacy `code` field). There is no `pca_with_transformation` algorithm; PCA component scores are created with `pca_column_creator`. +- Pipeline stages follow what a step does to the data. **Preprocessing** edits a variable in place and keeps its meaning and range: `missing_values_handler`, `outlier_winsorizer`, `global_imputer`, `global_winsorizer`. **Data transformation** creates a column or changes what a variable means (range, scale, type, row layout): `kmeans_cluster_creator`, `pca_column_creator`, `categorical_column_creator`, `variable_transformer`, `variable_scaler`, `longitudinal_transformer`, `ordinal_encoder`, `one_hot_encoder`, `missing_indicator`, `outlier_detector` (default `flag` adds a column; `drop` and `set_missing` edit in place, noted on the station). `stepStage` in `core/data-transformation-step-form.utils.ts` is the single classification; a step the engine adds later lands in Data transformation until listed there. The endpoint (`/specifications/preprocessing`), the step machine ids and the request key `preprocessing` are the engine contract and keep their names. +- Catalog steps with no dedicated station (everything in `/specifications/preprocessing` except the missing-values, outlier winsorizer, longitudinal, transformer and scaler singletons and the three column creators) render through `generic-data-transformation-step`, a form built from the step's parameter schema by `core/data-transformation-step-form.utils.ts` (field kinds, payload building, trimming to the selection); the panel mounts each one in the stage `stepStage` names, so the component serves both families. They are singletons in the applied config like the others; the panel carries them over whenever another card applies, the service drops them from a request once the loaded catalog lacks them, and a missing indicator is ordered ahead of the missing-values step. Columns they add (`missing_indicator`, `outlier_detector` flag, `one_hot_encoder`) and variables they drop are derived from their parameters by `core/data-transformation-step-columns.utils.ts` and feed the algorithm role pool. - `ExperimentsDashboardService` owns paged experiment list state, filtering, sharing, renaming, deletion, and result fetches. - `ErrorService` shares global feature errors through a `BehaviorSubject`. diff --git a/docs/context/module-index.md b/docs/context/module-index.md index bdc4f92d6..c30574733 100644 --- a/docs/context/module-index.md +++ b/docs/context/module-index.md @@ -42,11 +42,11 @@ Notes: Stored historical experiment payloads may depend on legacy algorithm keys ## `src/app/pages/experiment-studio` Purpose: Experiment composition workflow. -Key files: `experiment-studio.component.*`, `variables-panel/*`, `statistic-analysis-panel/*` (including `pca-column-source` / `kmeans-cluster-source`), `algorithm-panel/*`, `execution-panel/*` (result view + docked setup summary), `stepper/*`, `shared/*`, `guide/*`. +Key files: `experiment-studio.component.*`, `variables-panel/*`, `statistic-analysis-panel/*` (including `pca-column-source` / `kmeans-cluster-source` / `generic-data-transformation-step`), `algorithm-panel/*`, `execution-panel/*` (result view + docked setup summary), `stepper/*`, `shared/*`, `guide/*`. Used by: `/experiment-studio` route and dashboard edit flows. Rules: Keep page-specific UI here; use `ExperimentStudioService` for shared selection/run state. Transformation cards append to `appliedTransformations` in creation order; each creator step names outputs with `new_column_names`. Do not reintroduce `pca_with_transformation`. Tests: Component specs live near components; e2e covers PCA then K-means then logistic regression payload order in `e2e/experiment-studio.flows.spec.ts`. -Notes: Browser validation often requires a backend with data models, algorithms, and authenticated user state. PCA/K-means creators need the aggregation server in the preprocessing catalog. +Notes: Browser validation often requires a backend with data models, algorithms, and authenticated user state. PCA/K-means creators need the aggregation server in the step catalog (`/specifications/preprocessing`). ## `src/app/pages/experiment-studio/visualisations` Purpose: Chart/table rendering for algorithm outputs, histograms, metadata browsers, and auto-rendered results. diff --git a/e2e/experiment-studio.flows.spec.ts b/e2e/experiment-studio.flows.spec.ts index d30814ada..e7fa8a617 100644 --- a/e2e/experiment-studio.flows.spec.ts +++ b/e2e/experiment-studio.flows.spec.ts @@ -12,6 +12,7 @@ import { applyLongitudinalStrategy, applyMeanImputationToAll, applyOutlierChoice, + pickStep, assignRole, clearRole, collapsePreprocessing, @@ -249,7 +250,7 @@ test.describe('preprocessing', () => { test('batch actions set every numerical variable', async ({ page, api }) => { await openAgeAndSex(page); await applyMeanImputationToAll(page); - await page.locator('button:visible').filter({ hasText: /Add outlier/ }).first().click(); + await pickStep(page, 'preprocessing', 'outlier_winsorizer'); const outlier = page.locator('app-station-card', { hasText: 'Outlier Handling' }); await outlier.getByRole('button', { name: 'Batch' }).click(); await page.getByRole('menuitem', { name: 'Enable for all numerical' }).click(); diff --git a/e2e/studio.ts b/e2e/studio.ts index 146d6b767..e980c80fe 100644 --- a/e2e/studio.ts +++ b/e2e/studio.ts @@ -6,7 +6,8 @@ export async function openNewStudio(page: Page) { await page.goto('/experiments-dashboard'); await page.getByRole('button', { name: 'New experiment' }).click(); await expect(page).toHaveURL(/\/experiment-studio\/?$/); - await expect(page.getByRole('button', { name: /Dementia/ })).toBeVisible(); + // The study chip, not the bubble map's "Dementia" location crumb. + await expect(page.locator('.study-context-chip', { hasText: /Dementia/ })).toBeVisible(); } export async function addVariable(page: Page, label: string) { @@ -125,6 +126,14 @@ export async function collapsePreprocessing(page: Page) { if (await open.isVisible()) await open.click(); } +/** Same for the Data transformation step, where the longitudinal comparison lives. */ +export async function collapseTransformation(page: Page) { + const section = page.locator('[data-guide="analysis-transformation"]'); + const open = section.locator('.pipeline-node-toggle[aria-expanded="true"]'); + await expect(open.or(section.getByRole('list', { name: 'Data transformation steps' }))).toBeVisible(); + if (await open.isVisible()) await open.click(); +} + export async function openMissingValues(page: Page) { await page.getByRole('button', { name: 'Customize rules' }).click(); const missing = page.locator('app-station-card', { hasText: 'Missing Values' }); @@ -142,10 +151,21 @@ export async function applyMeanImputationToAll(page: Page) { await expect(missing).toContainText(/Mean imputation|Applied/); } +/** + * Pick a step from a stage's add-step menu: the rail's while the stage is collapsed, the stage's + * own once it is open. `step` is the engine's step name, e.g. `outlier_winsorizer`. + */ +export async function pickStep(page: Page, stage: 'preprocessing' | 'transformation', step: string) { + const picker = page.locator(`[data-guide="analysis-${stage}"] app-step-picker:visible`).first(); + const trigger = picker.locator('.step-picker-trigger'); + if ((await trigger.getAttribute('aria-expanded')) !== 'true') await trigger.click(); + await picker.locator(`.step-picker-option[data-step="${step}"]`).click(); +} + /** Enable outlier capping for every numerical variable, optionally picking a method and tail. */ export async function applyOutlierChoice(page: Page, method?: string, clip?: string) { await page.getByRole('button', { name: 'Customize rules' }).click(); - await page.locator('button:visible').filter({ hasText: /Add outlier/ }).first().click(); + await pickStep(page, 'preprocessing', 'outlier_winsorizer'); const outlier = page.locator('app-station-card', { hasText: 'Outlier Handling' }); await outlier.getByRole('button', { name: 'Batch' }).click(); await page.getByRole('menuitem', { name: 'Enable for all numerical' }).click(); @@ -172,12 +192,13 @@ export async function openLongitudinalAge(page: Page) { } export async function applyLongitudinalStrategy(page: Page, label: string) { - await page.getByRole('button', { name: 'Customize rules' }).click(); + // The longitudinal comparison changes what the variable means, so it is a data transformation. + await pickStep(page, 'transformation', 'longitudinal_transformer'); const card = page.locator('app-station-card', { hasText: 'Longitudinal Transformation' }); await card.getByRole('button', { name: /^Age/ }).click(); await card.locator('select').last().selectOption({ label }); await card.getByRole('button', { name: 'Apply', exact: true }).click(); - await collapsePreprocessing(page); + await collapseTransformation(page); await expect(page.getByText('Baseline vs Month 12')).toBeVisible(); } @@ -206,8 +227,7 @@ export async function applyFilters(page: Page, { female = false } = {}) { } export async function addNominalColumn(page: Page, code: string, category: string, fallback: string) { - await page.getByTitle('Add and open a derived column').click(); - await page.getByRole('button', { name: /Nominal column from rules/ }).click(); + await pickStep(page, 'transformation', 'categorical_column_creator'); await page.getByPlaceholder('e.g. mrs_good_outcome').fill(code); await page.getByRole('button', { name: /Add the first category/ }).click(); await page.getByPlaceholder('Enumeration value').fill(category); @@ -220,10 +240,10 @@ export async function addNominalColumn(page: Page, code: string, category: strin await expect(page.getByText('Sex is Female')).toBeVisible(); } -/** `stepAdded`: the transformation step is already open (another column was added first). */ +/** `stepAdded` is kept for the callers: the add-step menu is the same whether the stage is open or not. */ export async function addKMeansColumn(page: Page, { stepAdded = false } = {}) { - if (!stepAdded) await page.getByTitle('Add and open a derived column').click(); - await page.getByRole('button', { name: /K-means cluster column/ }).click(); + void stepAdded; + await pickStep(page, 'transformation', 'kmeans_cluster_creator'); const source = page.locator('app-kmeans-cluster-source'); await source.getByRole('button', { name: 'Run K-means report' }).click(); await expect(source.getByText('Cluster 0')).toBeVisible(); @@ -238,8 +258,8 @@ export async function addKMeansColumn(page: Page, { stepAdded = false } = {}) { * `stepAdded`: transformation stage already open with the type chooser visible. */ export async function addPcaColumns(page: Page, { stepAdded = false } = {}) { - if (!stepAdded) await page.getByTitle('Add and open a derived column').click(); - await page.getByRole('button', { name: /PCA component columns/ }).click(); + void stepAdded; + await pickStep(page, 'transformation', 'pca_column_creator'); const source = page.locator('app-pca-column-source'); await source.getByRole('button', { name: 'Fit on selected variables' }).click(); await source.getByRole('button', { name: 'Add component columns' }).click(); @@ -251,8 +271,8 @@ export async function addPcaColumns(page: Page, { stepAdded = false } = {}) { * Deselects every other numerical option in the fit pool. */ export async function addKMeansOnColumns(page: Page, columns: string[], { stepAdded = false } = {}) { - if (!stepAdded) await page.getByTitle('Add and open a derived column').click(); - await page.getByRole('button', { name: /K-means cluster column/ }).click(); + void stepAdded; + await pickStep(page, 'transformation', 'kmeans_cluster_creator'); const source = page.locator('app-kmeans-cluster-source').last(); await source.getByRole('button', { name: 'Fit on selected variables' }).click(); const keep = new Set(columns); diff --git a/scripts/check-dead-code.mjs b/scripts/check-dead-code.mjs index 23b6723d8..6b657ca11 100644 --- a/scripts/check-dead-code.mjs +++ b/scripts/check-dead-code.mjs @@ -65,7 +65,8 @@ const countIn = (haystack, name) => { const DECL = /^\s*export\s+(?:declare\s+)?(?:abstract\s+)?(?:async\s+)?(const|let|var|function|class|interface|type|enum)\s+([A-Za-z0-9_$]+)/; const deadExports = []; const appSources = ts.filter((p) => !p.endsWith('.spec.ts')); -for (const p of appSources) { +// `*.testing.ts` files exist for specs, so an export only specs import is not dead. +for (const p of appSources.filter((q) => !q.endsWith('.testing.ts'))) { const text = read(p); for (const line of text.split('\n')) { const m = DECL.exec(line); diff --git a/src/app/core/data-transformation-step-columns.utils.spec.ts b/src/app/core/data-transformation-step-columns.utils.spec.ts new file mode 100644 index 000000000..b5ed6e67b --- /dev/null +++ b/src/app/core/data-transformation-step-columns.utils.spec.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from 'vitest'; +import { stepColumnEffect } from './data-transformation-step-columns.utils'; + +const sources = new Map([ + ['age', { code: 'age', label: 'Age' }], + ['sex', { code: 'sex', label: 'Sex', enumerations: [{ code: 'M', label: 'Male' }, { code: 'F', label: 'Female' }, { code: 'X', label: 'Other' }] }], +]); + +describe('stepColumnEffect', () => { + it('names indicator columns after the variable and the suffix, with the engine default', () => { + expect(stepColumnEffect('missing_indicator', { variables: ['age'] }, sources).created) + .toEqual([{ code: 'age_missing', label: 'Age (missing)' }]); + expect(stepColumnEffect('missing_indicator', { variables: ['age'], suffix: '_na' }, sources).created[0].code).toBe('age_na'); + }); + + it('adds outlier flag columns only for the flag action', () => { + const strategies = { age: 'zscore' }; + expect(stepColumnEffect('outlier_detector', { strategies }, sources).created).toEqual([{ code: 'age_outlier', label: 'Age (outlier)' }]); + expect(stepColumnEffect('outlier_detector', { strategies, action: 'flag', suffix: '_o' }, sources).created[0].code).toBe('age_o'); + expect(stepColumnEffect('outlier_detector', { strategies, action: 'drop' }, sources).created).toEqual([]); + expect(stepColumnEffect('outlier_detector', { strategies, action: 'set_missing' }, sources).created).toEqual([]); + }); + + it('one-hot encodes sorted levels, dropping the reference level and the original by default', () => { + const effect = stepColumnEffect('one_hot_encoder', { variables: ['sex'] }, sources); + expect(effect.created.map((c) => c.code)).toEqual(['sex_M', 'sex_X']); + expect(effect.created[0].label).toBe('Sex: Male'); + expect(effect.dropped).toEqual(['sex']); + }); + + it('keeps every level and the original when asked to', () => { + const effect = stepColumnEffect('one_hot_encoder', { variables: ['sex'], drop_first: false, drop_original: false }, sources); + expect(effect.created.map((c) => c.code)).toEqual(['sex_F', 'sex_M', 'sex_X']); + expect(effect.dropped).toEqual([]); + }); + + it('has no column effect for other steps', () => { + expect(stepColumnEffect('global_winsorizer', { variables: ['age'] }, sources)).toEqual({ created: [], dropped: [] }); + }); +}); diff --git a/src/app/core/data-transformation-step-columns.utils.ts b/src/app/core/data-transformation-step-columns.utils.ts new file mode 100644 index 000000000..de81c1f73 --- /dev/null +++ b/src/app/core/data-transformation-step-columns.utils.ts @@ -0,0 +1,78 @@ +/** + * Columns the schema-driven catalog steps add to, or remove from, the data. Their request + * parameters name them (`suffix`, the encoder's levels), so the studio can offer them as + * assignable variables and hide the ones a step drops. + */ + +export interface CreatedStepColumn { + code: string; + label: string; +} + +export interface StepColumnEffect { + created: CreatedStepColumn[]; + /** Source variables the step removes from the data. */ + dropped: string[]; +} + +interface SourceVariable { + code: string; + label?: string; + enumerations?: Array<{ code: string; label?: string }>; +} + +const NO_EFFECT: StepColumnEffect = { created: [], dropped: [] }; + +function strings(value: unknown): string[] { + return Array.isArray(value) ? value.map((item) => String(item)) : []; +} + +/** The columns a step adds and the variables it drops, from its applied parameters. */ +export function stepColumnEffect( + name: string, + parameters: Record, + sources: ReadonlyMap, +): StepColumnEffect { + const labelOf = (code: string) => sources.get(code)?.label || code; + + if (name === 'missing_indicator') { + const suffix = typeof parameters['suffix'] === 'string' ? parameters['suffix'] : '_missing'; + return { + created: strings(parameters['variables']).map((code) => ({ + code: `${code}${suffix}`, + label: `${labelOf(code)} (missing)`, + })), + dropped: [], + }; + } + + if (name === 'outlier_detector') { + // Only the default `flag` action adds columns; `drop` removes rows and `set_missing` edits values. + if (parameters['action'] !== undefined && parameters['action'] !== 'flag') return NO_EFFECT; + const suffix = typeof parameters['suffix'] === 'string' ? parameters['suffix'] : '_outlier'; + const strategies = (parameters['strategies'] ?? {}) as Record; + return { + created: Object.keys(strategies).sort().map((code) => ({ + code: `${code}${suffix}`, + label: `${labelOf(code)} (outlier)`, + })), + dropped: [], + }; + } + + if (name === 'one_hot_encoder') { + const dropFirst = parameters['drop_first'] !== false; + const dropOriginal = parameters['drop_original'] !== false; + const variables = strings(parameters['variables']); + const created: CreatedStepColumn[] = []; + for (const code of variables) { + const levels = [...(sources.get(code)?.enumerations ?? [])].sort((a, b) => (a.code < b.code ? -1 : a.code > b.code ? 1 : 0)); + for (const level of dropFirst ? levels.slice(1) : levels) { + created.push({ code: `${code}_${level.code}`, label: `${labelOf(code)}: ${level.label || level.code}` }); + } + } + return { created, dropped: dropOriginal ? variables : [] }; + } + + return NO_EFFECT; +} diff --git a/src/app/core/data-transformation-step-form.utils.spec.ts b/src/app/core/data-transformation-step-form.utils.spec.ts new file mode 100644 index 000000000..fe1463f35 --- /dev/null +++ b/src/app/core/data-transformation-step-form.utils.spec.ts @@ -0,0 +1,192 @@ +import { describe, expect, it } from 'vitest'; +import { RawPreprocessingStep } from '../models/backend-algorithms.model'; +import { + buildStepParameters, + defaultStepValues, + filterStepParameters, + isGenericCatalogStep, + stepFields, + stepValuesFromParameters, + summarizeStepParameters, + unsupportedRequiredFields, + isPreprocessingSingleton, + isTransformationSingleton, + stepNote, + stepStage, + valueOptionsForVariable, +} from './data-transformation-step-form.utils'; + +const param = (extra: Record) => ({ + label: 'L', desc: 'D', types: ['text'], required: false, multiple: false, default: null, + enums: null, dict_keys_enums: null, dict_values_type: null, dict_values_enums: null, min: null, max: null, + ...extra, +}); + +/** Assumed shape of the engine's outlier detector step. */ +const outlierDetector = { + name: 'outlier_detector', label: 'Outlier Detector', desc: 'Flags outliers.', + parameters: { + strategies: param({ + types: ['dict'], required: true, + dict_keys_enums: { type: 'input_var_names', source: ['variables'] }, + dict_values_enums: { type: 'list', source: ['zscore', 'robust'] }, + }), + threshold: param({ types: ['real'], default: 3, min: 0 }), + }, +} as unknown as RawPreprocessingStep; + +const indicator = { + name: 'missing_indicator', label: 'Missing Indicator', desc: 'Adds indicator columns.', + parameters: { + variables: param({ types: ['text'], required: true, multiple: true }), + new_column_names: param({ types: ['text'], required: true, multiple: true }), + sparse: param({ types: ['bool'], default: false }), + }, +} as unknown as RawPreprocessingStep; + +describe('preprocessing step form utils', () => { + it('leaves the dedicated singletons and the creators to their own stations', () => { + for (const name of ['missing_values_handler', 'outlier_winsorizer', 'longitudinal_transformer', + 'variable_transformer', 'variable_scaler', 'categorical_column_creator', 'kmeans_cluster_creator', 'pca_column_creator']) { + expect(isGenericCatalogStep(name)).toBe(false); + } + expect(isGenericCatalogStep('outlier_detector')).toBe(true); + expect(isGenericCatalogStep('missing_indicator')).toBe(true); + }); + + it('derives field kinds from the parameter schema', () => { + expect(stepFields(outlierDetector).map((f) => [f.key, f.kind, f.itemKind, f.required])) + .toEqual([['strategies', 'variable-map', 'enum', true], ['threshold', 'real', 'real', false]]); + expect(stepFields(indicator).map((f) => [f.key, f.kind])) + .toEqual([['variables', 'variables'], ['new_column_names', 'list'], ['sparse', 'bool']]); + }); + + it('builds a per-variable map payload with a number parameter', () => { + const fields = stepFields(outlierDetector); + const values = { ...defaultStepValues(fields), strategies: { age: 'zscore', mmse: 'robust', gone: 'zscore', blank: '' }, threshold: '2.5' }; + const { parameters, errors } = buildStepParameters(fields, values, new Set(['age', 'mmse', 'blank'])); + expect(errors).toEqual({}); + expect(parameters).toEqual({ strategies: { age: 'zscore', mmse: 'robust' }, threshold: 2.5 }); + }); + + it('sends the schema default and omits an emptied optional number', () => { + const fields = stepFields(outlierDetector); + expect(defaultStepValues(fields)['threshold']).toBe('3'); + const cleared = buildStepParameters(fields, { strategies: { age: 'zscore' }, threshold: '' }, null); + expect(cleared.parameters).toEqual({ strategies: { age: 'zscore' } }); + }); + + it('reports a required field left empty and bad numbers instead of guessing', () => { + const fields = stepFields(outlierDetector); + expect(buildStepParameters(fields, defaultStepValues(fields), null).errors['strategies']).toMatch(/required/); + expect(buildStepParameters(fields, { strategies: { a: 'zscore' }, threshold: '-1' }, null).errors['threshold']).toMatch(/at least 0/); + expect(buildStepParameters(fields, { strategies: { a: 'zscore' }, threshold: 'x' }, null).errors['threshold']).toMatch(/number/); + }); + + it('builds variable lists, name lists and booleans', () => { + const fields = stepFields(indicator); + const values = { variables: ['age', 'mmse'], new_column_names: 'age_na, mmse_na', sparse: true }; + expect(buildStepParameters(fields, values, new Set(['age', 'mmse'])).parameters) + .toEqual({ variables: ['age', 'mmse'], new_column_names: ['age_na', 'mmse_na'], sparse: true }); + // A deselected variable drops out of the list; with none left the required list is an error. + expect(buildStepParameters(fields, values, new Set(['x'])).errors['variables']).toMatch(/required/); + }); + + it('round-trips applied parameters into the form', () => { + const fields = stepFields(indicator); + const parameters = { variables: ['age'], new_column_names: ['age_na'], sparse: false }; + expect(buildStepParameters(fields, stepValuesFromParameters(fields, parameters), null).parameters).toEqual(parameters); + }); + + it('trims variable-bound parameters to the selection and drops a step left without any', () => { + const fields = stepFields(outlierDetector); + const params = { strategies: { age: 'zscore', mmse: 'robust' }, threshold: 3 }; + expect(filterStepParameters(fields, params, new Set(['age']))).toEqual({ strategies: { age: 'zscore' }, threshold: 3 }); + expect(filterStepParameters(fields, params, new Set(['other']))).toBeNull(); + }); + + it('marks required parameters it cannot edit', () => { + const spec = { name: 'x', label: 'X', desc: '', parameters: { reusable_preprocessing: param({ types: ['dict'], required: true }) } } as unknown as RawPreprocessingStep; + expect(unsupportedRequiredFields(stepFields(spec)).map((f) => f.key)).toEqual(['reusable_preprocessing']); + }); + + it('summarizes parameters with variable labels', () => { + expect(summarizeStepParameters({ strategies: { age: 'zscore' }, threshold: 3, variables: ['mmse'] }, { age: 'Age', mmse: 'MMSE' })) + .toEqual(['Age: zscore', 'threshold: 3', 'variables: MMSE']); + }); + + describe('global imputer contract', () => { + const imputer = { + name: 'global_imputer', label: 'Global Imputer', desc: '', + parameters: { + strategies: param({ + types: ['dict'], required: true, + dict_keys_enums: { type: 'input_var_names', source: ['variables'] }, + dict_values_enums: { type: 'list', source: ['mean', 'median', 'most_frequent', 'constant'] }, + }), + fill_values: param({ types: ['dict'], dict_keys_enums: { type: 'input_var_names', source: ['variables'] } }), + }, + } as unknown as RawPreprocessingStep; + + it('offers mean and median to numerical variables and most_frequent to nominal ones; constant to both', () => { + const options = ['mean', 'median', 'most_frequent', 'constant']; + expect(valueOptionsForVariable('global_imputer', options, true)).toEqual(['mean', 'median', 'constant']); + expect(valueOptionsForVariable('global_imputer', options, false)).toEqual(['most_frequent', 'constant']); + expect(valueOptionsForVariable('other_step', options, false)).toEqual(options); + }); + + it('sends fill values only for constant variables, as numbers for numerical ones', () => { + const fields = stepFields(imputer); + const values = { + strategies: { age: 'constant', sex: 'constant', mmse: 'mean' }, + fill_values: { age: '0', sex: 'F', mmse: '9' }, + }; + const { parameters, errors } = buildStepParameters(fields, values, null, { stepName: 'global_imputer', numericalCodes: new Set(['age', 'mmse']) }); + expect(errors).toEqual({}); + expect(parameters).toEqual({ + strategies: { age: 'constant', sex: 'constant', mmse: 'mean' }, + fill_values: { age: 0, sex: 'F' }, + }); + }); + + it('rejects a non-numeric fill value for a numerical variable', () => { + const fields = stepFields(imputer); + const result = buildStepParameters(fields, { strategies: { age: 'constant' }, fill_values: { age: 'abc' } }, null, { stepName: 'global_imputer', numericalCodes: new Set(['age']) }); + expect(result.errors['fill_values']).toMatch(/number/); + }); + }); +}); + +describe('pipeline stage of each catalog step', () => { + // In place and the same meaning and range: Preprocessing. A new column or a changed meaning: Data transformation. + const PREPROCESSING = ['missing_values_handler', 'outlier_winsorizer', 'global_imputer', 'global_winsorizer']; + const TRANSFORMATION = [ + 'kmeans_cluster_creator', 'pca_column_creator', 'categorical_column_creator', 'longitudinal_transformer', + 'variable_transformer', 'variable_scaler', 'missing_indicator', 'outlier_detector', 'one_hot_encoder', 'ordinal_encoder', + ]; + + it('puts the in-place steps in Preprocessing and every other catalog step in Data transformation', () => { + for (const name of PREPROCESSING) expect(stepStage(name), name).toBe('preprocessing'); + for (const name of TRANSFORMATION) expect(stepStage(name), name).toBe('transformation'); + expect(PREPROCESSING.length + TRANSFORMATION.length).toBe(14); + }); + + it('keeps the column creators out of both singleton groups', () => { + for (const name of ['kmeans_cluster_creator', 'pca_column_creator', 'categorical_column_creator']) { + expect(isTransformationSingleton(name)).toBe(false); + expect(isPreprocessingSingleton(name)).toBe(false); + } + expect(isTransformationSingleton('variable_scaler')).toBe(true); + expect(isPreprocessingSingleton('global_imputer')).toBe(true); + }); + + it('files a step the engine adds later under Data transformation until it is classified', () => { + expect(stepStage('some_future_step')).toBe('transformation'); + }); + + it('explains the outlier detector and the missing indicator, whose position the stage rule does not cover', () => { + expect(stepNote('outlier_detector')).toMatch(/Drop and Set missing edit the data in place/); + expect(stepNote('missing_indicator')).toMatch(/before missing values/); + expect(stepNote('one_hot_encoder')).toBe(''); + }); +}); diff --git a/src/app/core/data-transformation-step-form.utils.ts b/src/app/core/data-transformation-step-form.utils.ts new file mode 100644 index 000000000..5eb41607d --- /dev/null +++ b/src/app/core/data-transformation-step-form.utils.ts @@ -0,0 +1,470 @@ +/** + * Catalog-driven editor model for pipeline steps that have no dedicated station. + * + * Every step the engine lists in `/services/specifications/preprocessing` (the endpoint keeps that + * name) must be selectable and configurable. Steps with a bespoke station (missing values, outlier + * winsorizer, longitudinal, transformer, scaler and the column creators) keep it; every other step + * gets a form built from its parameter schema, and its payload is built here so it matches the + * schema exactly. + * + * The studio sorts the steps into two stages by what they do to the data: + * - Preprocessing tweaks an existing variable in place and keeps its meaning and range + * (missing values, clipping outliers). + * - Data transformation creates a new column, or changes what a variable means (its range, scale + * or type), so analyses read a different quantity than the one that was collected. + */ + +import { RawParameter, RawPreprocessingStep } from '../models/backend-algorithms.model'; +import { CREATOR_NAMES, CreatorName } from './transformation-steps.utils'; + +/** Singleton steps that own a dedicated station. Their requests are built by the panel. */ +export const DEDICATED_SINGLETON_STEPS: ReadonlySet = new Set([ + 'missing_values_handler', + 'outlier_winsorizer', + 'longitudinal_transformer', + 'variable_transformer', + 'variable_scaler', +]); + +/** The pipeline stage a step belongs to; see the file header for the rule. */ +export type StepStage = 'preprocessing' | 'transformation'; + +/** + * Steps edited in place that keep the variable's meaning and range: the Preprocessing stage. + * Every other step creates a column or changes a variable's meaning, range or type and belongs to + * the Data transformation stage. A step the engine adds later lands in Data transformation until it + * is classified here, because most new steps derive features. + */ +const PREPROCESSING_STEPS: ReadonlySet = new Set([ + 'missing_values_handler', + 'outlier_winsorizer', + 'global_imputer', + 'global_winsorizer', +]); + +export function stepStage(name: string): StepStage { + return PREPROCESSING_STEPS.has(name) ? 'preprocessing' : 'transformation'; +} + +/** + * Singleton steps of the Data transformation stage: every non-creator step outside the + * Preprocessing stage. The three column creators are an ordered list of their own. + */ +export function isTransformationSingleton(name: string): boolean { + return !CREATOR_NAMES.has(name as CreatorName) && stepStage(name) === 'transformation'; +} + +/** Singleton steps of the Preprocessing stage: edited in place, same meaning and range. */ +export function isPreprocessingSingleton(name: string): boolean { + return !CREATOR_NAMES.has(name as CreatorName) && stepStage(name) === 'preprocessing'; +} + +/** + * One line shown on a station when its position needs explaining: the stage rule does not cover + * every setting of the step, or the run order differs from the order of the stages. + */ +const STEP_NOTES: Readonly> = { + missing_indicator: 'Runs before missing values are handled, so it sees the gaps that were in the data.', + outlier_detector: + 'Flag adds a 0/1 column and keeps every value. Drop and Set missing edit the data in place instead (rows removed or values blanked).', +}; + +export function stepNote(name: string): string { + return STEP_NOTES[name] ?? ''; +} + +/** True when a catalog step is rendered by the generic, schema-driven station. */ +export function isGenericCatalogStep(name: string): boolean { + return !DEDICATED_SINGLETON_STEPS.has(name) && !CREATOR_NAMES.has(name as CreatorName); +} + +/** Which selected variables a step can act on. Unlisted steps accept any and let the engine validate. */ +export type StepVariableScope = 'any' | 'numerical' | 'categorical'; + +const STEP_VARIABLE_SCOPE: Readonly> = { + outlier_detector: 'numerical', + global_winsorizer: 'numerical', + one_hot_encoder: 'categorical', + ordinal_encoder: 'categorical', +}; + +export function stepVariableScope(name: string): StepVariableScope { + return STEP_VARIABLE_SCOPE[name] ?? 'any'; +} + +/** + * Generic steps that must run before the missing-values steps: a missing indicator placed after + * an imputation or drop would flag nothing. Everything else keeps its stored order. + */ +export const STEPS_BEFORE_IMPUTATION: ReadonlySet = new Set(['missing_indicator']); + +/** + * Per-variable values only some variable types accept (the engine rejects the rest with HTTP 460), + * so the form never offers them: `global_imputer` fills a numerical variable with mean or median + * and a categorical one with its most frequent code; constant works for both. + */ +const VALUE_VARIABLE_TYPE: Readonly>>> = { + global_imputer: { mean: 'numerical', median: 'numerical', most_frequent: 'categorical' }, +}; + +/** The enum values of a per-variable map that suit a variable of the given kind. */ +export function valueOptionsForVariable(stepName: string, options: string[], numerical: boolean): string[] { + const rules = VALUE_VARIABLE_TYPE[stepName]; + if (!rules) return options; + return options.filter((option) => { + const needs = rules[option]; + return !needs || (needs === 'numerical') === numerical; + }); +} + +/** Per-variable maps that only apply to the variables another map in the step selected. */ +export const DEPENDENT_MAPS: Readonly> = { + global_imputer: [{ key: 'fill_values', on: 'strategies', value: 'constant' }], +}; + +export type StepFieldKind = + | 'enum' + | 'text' + | 'int' + | 'real' + | 'bool' + /** Free list of values, typed in as comma separated text. */ + | 'list' + /** A subset of the selected variables, sent as a list of codes. */ + | 'variables' + /** One value per selected variable, sent as `{code: value}`. */ + | 'variable-map' + /** Needs a dedicated editor (reusable fitted recipes, filter rules, ...). */ + | 'unsupported'; + +export interface StepField { + key: string; + label: string; + description: string; + kind: StepFieldKind; + required: boolean; + /** Scalar kind of each item of a list / each value of a variable map. */ + itemKind: 'enum' | 'text' | 'int' | 'real' | 'bool'; + options: string[]; + min: number | null; + max: number | null; + defaultValue: unknown; +} + +/** Form state: strings for text-like inputs, booleans, string lists, or code→string maps. */ +export type StepFormValues = Record; + +/** Parameter names that hold variable codes rather than free text. */ +const VARIABLE_LIST_KEYS = new Set(['variables', 'columns', 'features', 'variable_names']); + +function toBool(value: unknown): boolean { + return value === true || value === 'true' || value === 'True'; +} + +function toNumber(value: unknown): number | null { + if (value === null || value === undefined || value === '') return null; + const n = Number(value); + return Number.isFinite(n) ? n : null; +} + +function listOptions(definition: { type?: string; source?: unknown } | undefined | null): string[] { + if (!definition || definition.type !== 'list' || !Array.isArray(definition.source)) return []; + return definition.source.map((item) => String(item)); +} + +function scalarKind(type: string | undefined): StepField['itemKind'] { + switch ((type ?? 'text').toLowerCase()) { + case 'int': + return 'int'; + case 'real': + case 'float': + return 'real'; + case 'bool': + case 'boolean': + return 'bool'; + default: + return 'text'; + } +} + +function buildField(key: string, raw: RawParameter): StepField { + const type = String(raw.types?.[0] ?? 'text').toLowerCase(); + const options = listOptions(raw.enums); + const base = { + key, + label: raw.label || key, + description: raw.desc || '', + required: raw.required === true || raw.required === 'true', + options, + min: toNumber(raw.min), + max: toNumber(raw.max), + defaultValue: raw.default ?? raw.default_value ?? null, + }; + const multiple = raw.multiple === true || raw.multiple === 'true'; + + if (type === 'dict') { + const keys = raw.dict_keys_enums; + const valueOptions = listOptions(raw.dict_values_enums); + const keyedByVariable = keys?.type === 'input_var_names' || keys?.type === 'variables'; + if (!keyedByVariable) return { ...base, kind: 'unsupported', itemKind: 'text', options: [] }; + const valueType = scalarKind(raw.dict_values_type === 'boolean' ? 'bool' : raw.dict_values_type); + return { + ...base, + kind: 'variable-map', + itemKind: valueOptions.length ? 'enum' : valueType, + options: valueOptions, + }; + } + + const itemKind: StepField['itemKind'] = options.length ? 'enum' : scalarKind(type); + if (type === 'filter' || type === 'json' || raw.enums?.type === 'fixed_var_CDE_enums' && multiple) { + return { ...base, kind: 'unsupported', itemKind: 'text', options: [] }; + } + if (multiple) { + const namesVariables = raw.enums?.type === 'input_var_names' || raw.enums?.type === 'variables'; + return { + ...base, + kind: namesVariables || VARIABLE_LIST_KEYS.has(key) ? 'variables' : 'list', + itemKind, + }; + } + if (itemKind === 'enum') return { ...base, kind: 'enum', itemKind }; + return { ...base, kind: itemKind, itemKind }; +} + +/** The fields of a step, in schema order. */ +export function stepFields(spec: RawPreprocessingStep | null | undefined): StepField[] { + const parameters = spec?.parameters ?? {}; + return Object.entries(parameters).map(([key, raw]) => buildField(key, raw as RawParameter)); +} + +/** A required field this form cannot fill in blocks Apply: the engine would reject the step. */ +export function unsupportedRequiredFields(fields: StepField[]): StepField[] { + return fields.filter((field) => field.kind === 'unsupported' && field.required); +} + +/** Empty form: schema defaults for scalar fields, nothing selected for the rest. */ +export function defaultStepValues(fields: StepField[]): StepFormValues { + const values: StepFormValues = {}; + for (const field of fields) { + switch (field.kind) { + case 'bool': + values[field.key] = field.defaultValue === null ? false : toBool(field.defaultValue); + break; + case 'variables': + values[field.key] = []; + break; + case 'variable-map': + values[field.key] = {}; + break; + case 'list': + values[field.key] = Array.isArray(field.defaultValue) ? field.defaultValue.join(', ') : ''; + break; + case 'unsupported': + break; + default: + values[field.key] = field.defaultValue === null || field.defaultValue === undefined + ? '' + : String(field.defaultValue); + } + } + return values; +} + +function parseScalar( + kind: StepField['itemKind'], + raw: unknown, + field: StepField, + label: string, + errors: Record, +): string | number | boolean | null { + const text = typeof raw === 'string' ? raw.trim() : raw; + if (kind === 'bool') return toBool(text); + if (text === '' || text === null || text === undefined) return null; + if (kind === 'int' || kind === 'real') { + const n = Number(text); + if (!Number.isFinite(n)) { + errors[field.key] = `${label} must be a number.`; + return null; + } + if (kind === 'int' && !Number.isInteger(n)) { + errors[field.key] = `${label} must be a whole number.`; + return null; + } + if (field.min !== null && n < field.min) { + errors[field.key] = `${label} must be at least ${field.min}.`; + return null; + } + if (field.max !== null && n > field.max) { + errors[field.key] = `${label} must be at most ${field.max}.`; + return null; + } + return n; + } + return String(text); +} + +export interface BuiltStepParameters { + parameters: Record; + errors: Record; +} + +/** + * The request parameters of a step from its form state. Empty optional fields are left out so + * the engine's own defaults apply; a required field left empty is an error, never a guess. + */ +export function buildStepParameters( + fields: StepField[], + values: StepFormValues, + selectedCodes: ReadonlySet | null = null, + context: { stepName?: string; numericalCodes?: ReadonlySet } = {}, +): BuiltStepParameters { + const parameters: Record = {}; + const errors: Record = {}; + + for (const field of fields) { + const raw = values[field.key]; + const missing = `${field.label} is required.`; + switch (field.kind) { + case 'unsupported': + break; + case 'bool': + // A boolean is always a decision, so it is sent. + parameters[field.key] = toBool(raw); + break; + case 'variables': { + const codes = (Array.isArray(raw) ? raw : []).map(String) + .filter((code) => !selectedCodes || selectedCodes.has(code)); + if (codes.length) parameters[field.key] = codes; + else if (field.required) errors[field.key] = missing; + break; + } + case 'list': { + const items = String(raw ?? '').split(',').map((item) => item.trim()).filter(Boolean); + const parsed = items + .map((item) => parseScalar(field.itemKind, item, field, field.label, errors)) + .filter((item) => item !== null); + if (parsed.length) parameters[field.key] = parsed; + else if (field.required && !errors[field.key]) errors[field.key] = missing; + break; + } + case 'variable-map': { + const map: Record = {}; + const dependency = (DEPENDENT_MAPS[context.stepName ?? ''] ?? []).find((rule) => rule.key === field.key); + const governing = dependency ? ((values[dependency.on] ?? {}) as Record) : null; + for (const [code, entry] of Object.entries((raw ?? {}) as Record)) { + if (selectedCodes && !selectedCodes.has(code)) continue; + if (dependency && governing?.[code] !== dependency.value) continue; + // A fill value is a number for a numerical variable and an enumeration code otherwise. + const kind = dependency && field.itemKind === 'text' && context.numericalCodes?.has(code) ? 'real' : field.itemKind; + const value = parseScalar(kind, entry, field, `${field.label} (${code})`, errors); + if (value !== null && value !== '') map[code] = value; + } + if (Object.keys(map).length) parameters[field.key] = map; + else if (field.required) errors[field.key] = missing; + break; + } + default: { + const value = parseScalar(field.kind === 'enum' ? 'text' : field.itemKind, raw, field, field.label, errors); + if (value !== null && value !== '') parameters[field.key] = value; + else if (field.required && !errors[field.key]) errors[field.key] = missing; + } + } + } + return { parameters, errors }; +} + +/** Form state for a saved/applied step: the inverse of `buildStepParameters`. */ +export function stepValuesFromParameters( + fields: StepField[], + parameters: Record | null | undefined, +): StepFormValues { + const values = defaultStepValues(fields); + if (!parameters) return values; + for (const field of fields) { + const raw = parameters[field.key]; + if (raw === undefined || raw === null) { + if (field.kind !== 'bool') continue; + } + switch (field.kind) { + case 'bool': + values[field.key] = raw === undefined || raw === null ? values[field.key] : toBool(raw); + break; + case 'variables': + values[field.key] = Array.isArray(raw) ? raw.map(String) : []; + break; + case 'list': + values[field.key] = Array.isArray(raw) ? raw.join(', ') : ''; + break; + case 'variable-map': + values[field.key] = raw && typeof raw === 'object' && !Array.isArray(raw) + ? Object.fromEntries(Object.entries(raw as Record).map(([k, v]) => [k, String(v)])) + : {}; + break; + case 'unsupported': + break; + default: + values[field.key] = String(raw); + } + } + return values; +} + +/** + * Keep a step's variable-bound parameters on the variables still selected. Returns null when a + * required variable parameter ends up empty, so the step leaves the request instead of failing it. + */ +export function filterStepParameters( + fields: StepField[], + parameters: Record, + allowedCodes: ReadonlySet, +): Record | null { + const next: Record = { ...parameters }; + for (const field of fields) { + const raw = parameters[field.key]; + if (field.kind === 'variables' && Array.isArray(raw)) { + const kept = raw.filter((code) => allowedCodes.has(String(code))); + if (kept.length) next[field.key] = kept; + else { + delete next[field.key]; + if (field.required) return null; + } + } + if (field.kind === 'variable-map' && raw && typeof raw === 'object' && !Array.isArray(raw)) { + const kept = Object.fromEntries( + Object.entries(raw as Record).filter(([code]) => allowedCodes.has(code)), + ); + if (Object.keys(kept).length) next[field.key] = kept; + else { + delete next[field.key]; + if (field.required) return null; + } + } + } + return next; +} + +function humanize(text: string): string { + return text.replace(/_/g, ' '); +} + +/** Readable lines of a step's parameters for the run summary, with variable labels. */ +export function summarizeStepParameters( + parameters: Record | null | undefined, + labelMap: Record = {}, +): string[] { + const lines: string[] = []; + for (const [key, value] of Object.entries(parameters ?? {})) { + if (value === null || value === undefined || value === '') continue; + if (Array.isArray(value)) { + lines.push(`${humanize(key)}: ${value.map((item) => labelMap[String(item)] ?? String(item)).join(', ')}`); + } else if (typeof value === 'object') { + for (const [code, entry] of Object.entries(value as Record)) { + lines.push(`${labelMap[code] ?? code}: ${humanize(String(entry))}`); + } + } else { + lines.push(`${humanize(key)}: ${humanize(String(value))}`); + } + } + return lines; +} diff --git a/src/app/core/step-picker.utils.spec.ts b/src/app/core/step-picker.utils.spec.ts new file mode 100644 index 000000000..c1e76a614 --- /dev/null +++ b/src/app/core/step-picker.utils.spec.ts @@ -0,0 +1,138 @@ +import { describe, expect, it } from 'vitest'; +import { stepIcon, stepPickerGroups } from './step-picker.utils'; + +describe('stepIcon', () => { + it('gives every known step its own icon', () => { + expect(stepIcon('outlier_winsorizer')).toBe('fas fa-line-chart'); + expect(stepIcon('global_imputer')).toBe('fas fa-fill-drip'); + expect(stepIcon('global_winsorizer')).toBe('fas fa-compress'); + expect(stepIcon('variable_transformer')).toBe('fas fa-arrow-up-right-dots'); + expect(stepIcon('variable_scaler')).toBe('fas fa-ruler-horizontal'); + expect(stepIcon('one_hot_encoder')).toBe('fas fa-table-columns'); + expect(stepIcon('ordinal_encoder')).toBe('fas fa-arrow-down-1-9'); + expect(stepIcon('missing_indicator')).toBe('fas fa-flag'); + expect(stepIcon('outlier_detector')).toBe('fas fa-magnifying-glass-chart'); + expect(stepIcon('longitudinal_transformer')).toBe('fas fa-code-compare'); + }); + + it('falls back to the branch icon for a step it does not know', () => { + expect(stepIcon('a_step_the_engine_adds_later')).toBe('fas fa-code-branch'); + }); +}); + +describe('stepPickerGroups', () => { + it('returns no group when nothing can be added', () => { + expect(stepPickerGroups([])).toEqual([]); + }); + + it('sorts the preprocessing steps into missing values, then outliers', () => { + const groups = stepPickerGroups([ + { name: 'outlier_winsorizer' }, + { name: 'global_imputer', label: 'Global Imputer', desc: 'Impute missing values in place.' }, + { name: 'global_winsorizer', label: 'Global Winsorizer', desc: 'Clip numerical variables.' }, + ]); + + expect(groups.map((group) => [group.key, group.label])).toEqual([ + ['missing', 'Fill missing values'], + ['outliers', 'Cap outliers'], + ]); + expect(groups[0].options.map((option) => option.id)).toEqual(['global_imputer']); + // Inside a group the steps keep the order they were given in. + expect(groups[1].options.map((option) => option.id)).toEqual(['outlier_winsorizer', 'global_winsorizer']); + }); + + it('sorts the transformation steps by purpose, in a fixed order', () => { + const groups = stepPickerGroups([ + { name: 'kmeans_cluster_creator', label: 'K-means cluster column', desc: 'Group records.' }, + { name: 'longitudinal_transformer' }, + { name: 'outlier_detector', label: 'Outlier Detector', desc: 'Detect outliers.' }, + { name: 'ordinal_encoder', label: 'Ordinal Encoder', desc: 'Integer codes.' }, + { name: 'variable_scaler' }, + { name: 'missing_indicator', label: 'Missing Indicator', desc: 'Flag gaps.' }, + { name: 'one_hot_encoder', label: 'One-Hot Encoder', desc: 'Dummy columns.' }, + { name: 'variable_transformer' }, + { name: 'categorical_column_creator', label: 'Nominal column from rules', desc: 'Name categories.' }, + { name: 'pca_column_creator', label: 'PCA component columns', desc: 'Project.' }, + ]); + + expect(groups.map((group) => [group.key, group.label])).toEqual([ + ['reshape', 'Reshape and scale'], + ['encode', 'Encode categories'], + ['flag', 'Flag records'], + ['visits', 'Compare visits'], + ['create', 'Create a column'], + ]); + expect(groups.map((group) => group.options.map((option) => option.id))).toEqual([ + ['variable_scaler', 'variable_transformer'], + ['ordinal_encoder', 'one_hot_encoder'], + ['outlier_detector', 'missing_indicator'], + ['longitudinal_transformer'], + ['kmeans_cluster_creator', 'categorical_column_creator', 'pca_column_creator'], + ]); + }); + + it('names the steps that own a station exactly as their station is titled', () => { + const options = stepPickerGroups([ + { name: 'outlier_winsorizer' }, + { name: 'variable_transformer' }, + { name: 'variable_scaler' }, + { name: 'longitudinal_transformer' }, + ]).flatMap((group) => group.options); + + expect(options.map((option) => option.title)).toEqual([ + 'Outlier Handling', + 'Variable Transforms', + 'Variable Scaling', + 'Longitudinal Transformation', + ]); + expect(options[0]).toEqual({ + id: 'outlier_winsorizer', + icon: 'fas fa-line-chart', + title: 'Outlier Handling', + subtitle: 'Cap extreme values so a few outliers do not skew results. Numerical variables only.', + }); + expect(options[1].subtitle).toBe( + 'Reshape skewed values with log, square root, Yeo-Johnson or quantile. Numerical variables only.', + ); + expect(options[2].subtitle).toBe( + 'Bring numerical variables to a comparable range: standard, robust, min-max or max absolute.', + ); + expect(options[3].subtitle).toBe('Compare two visits per variable. Required for two-visit pathologies.'); + }); + + it('takes the title and description of a catalog step from the engine', () => { + const [group] = stepPickerGroups([ + { name: 'one_hot_encoder', label: 'One-Hot Encoder', desc: 'Create 0/1 dummy columns from categorical variables.' }, + ]); + + expect(group.options).toEqual([{ + id: 'one_hot_encoder', + icon: 'fas fa-table-columns', + title: 'One-Hot Encoder', + subtitle: 'Create 0/1 dummy columns from categorical variables.', + }]); + }); + + it('prefers the icon handed in with the step', () => { + const [group] = stepPickerGroups([ + { name: 'kmeans_cluster_creator', label: 'K-means cluster column', desc: 'Group records.', icon: 'fas fa-diagram-project' }, + ]); + + expect(group.options[0].icon).toBe('fas fa-diagram-project'); + }); + + it('keeps a step it does not know, under its own name, in a last group', () => { + const groups = stepPickerGroups([ + { name: 'brand_new_step' }, + { name: 'variable_scaler' }, + ]); + + expect(groups.map((group) => [group.key, group.label])).toEqual([ + ['reshape', 'Reshape and scale'], + ['other', 'More steps'], + ]); + expect(groups[1].options).toEqual([ + { id: 'brand_new_step', icon: 'fas fa-code-branch', title: 'brand_new_step', subtitle: '' }, + ]); + }); +}); diff --git a/src/app/core/step-picker.utils.ts b/src/app/core/step-picker.utils.ts new file mode 100644 index 000000000..21a52f3a0 --- /dev/null +++ b/src/app/core/step-picker.utils.ts @@ -0,0 +1,121 @@ +/** + * The "Add a step" menu of the Preprocessing and Data transformation stages. + * + * A stage used to list one dashed row per step it could still add. With every step of the engine's + * catalog on offer that list grew to nine rows, so the stage now shows one add row that opens a + * menu, and the menu sorts the steps by what the user wants to do with the data. + */ + +/** One step of the menu. `id` is the engine's step name; the panel maps it to the station to open. */ +export interface StepPickerOption { + id: string; + icon: string; + title: string; + subtitle: string; +} + +/** Steps that serve the same purpose, under one plain-language label. */ +export interface StepPickerGroup { + key: string; + label: string; + options: StepPickerOption[]; +} + +/** + * A step the stage can still add. `label` and `desc` come from the engine's catalog and are used + * for the steps this file has no copy for; `icon` overrides the icon of `stepIcon`. + */ +export interface StepCandidate { + name: string; + label?: string; + desc?: string; + icon?: string; +} + +/** Font Awesome classes of a pipeline step, by engine name. Unlisted names get the branch icon. */ +const ICONS: Readonly> = { + outlier_winsorizer: 'fas fa-line-chart', + global_imputer: 'fas fa-fill-drip', + global_winsorizer: 'fas fa-compress', + variable_transformer: 'fas fa-arrow-up-right-dots', + variable_scaler: 'fas fa-ruler-horizontal', + one_hot_encoder: 'fas fa-table-columns', + ordinal_encoder: 'fas fa-arrow-down-1-9', + missing_indicator: 'fas fa-flag', + outlier_detector: 'fas fa-magnifying-glass-chart', + longitudinal_transformer: 'fas fa-code-compare', +}; + +const FALLBACK_ICON = 'fas fa-code-branch'; + +export function stepIcon(name: string): string { + return ICONS[name] ?? FALLBACK_ICON; +} + +/** Menu copy for the steps whose station has a hand-written title and subtitle. */ +const COPY: Readonly> = { + outlier_winsorizer: { + title: 'Outlier Handling', + subtitle: 'Cap extreme values so a few outliers do not skew results. Numerical variables only.', + }, + variable_transformer: { + title: 'Variable Transforms', + subtitle: 'Reshape skewed values with log, square root, Yeo-Johnson or quantile. Numerical variables only.', + }, + variable_scaler: { + title: 'Variable Scaling', + subtitle: 'Bring numerical variables to a comparable range: standard, robust, min-max or max absolute.', + }, + longitudinal_transformer: { + title: 'Longitudinal Transformation', + subtitle: 'Compare two visits per variable. Required for two-visit pathologies.', + }, +}; + +/** Steps that serve the same purpose, in the order the menu shows the groups. */ +const GROUP_TABLE: ReadonlyArray<{ key: string; label: string; steps: readonly string[] }> = [ + { key: 'missing', label: 'Fill missing values', steps: ['global_imputer'] }, + { key: 'outliers', label: 'Cap outliers', steps: ['outlier_winsorizer', 'global_winsorizer'] }, + { key: 'reshape', label: 'Reshape and scale', steps: ['variable_transformer', 'variable_scaler'] }, + { key: 'encode', label: 'Encode categories', steps: ['one_hot_encoder', 'ordinal_encoder'] }, + { key: 'flag', label: 'Flag records', steps: ['missing_indicator', 'outlier_detector'] }, + { key: 'visits', label: 'Compare visits', steps: ['longitudinal_transformer'] }, + { key: 'create', label: 'Create a column', steps: ['categorical_column_creator', 'kmeans_cluster_creator', 'pca_column_creator'] }, +]; + +/** Where a step named in no group ends up. */ +const OTHER_GROUP: { key: string; label: string } = { key: 'other', label: 'More steps' }; + +function toOption(candidate: StepCandidate): StepPickerOption { + const copy = COPY[candidate.name]; + return { + id: candidate.name, + icon: candidate.icon ?? stepIcon(candidate.name), + title: copy?.title ?? candidate.label ?? candidate.name, + subtitle: copy?.subtitle ?? candidate.desc ?? '', + }; +} + +/** + * The menu for the given steps: one group per purpose, in a fixed order, each holding its steps in + * the order they were given. A group without a step is left out. + */ +export function stepPickerGroups(candidates: StepCandidate[]): StepPickerGroup[] { + const options = candidates.map(toOption); + + const groups: StepPickerGroup[] = []; + for (const { key, label, steps } of GROUP_TABLE) { + const groupOptions = options.filter((option) => steps.includes(option.id)); + if (groupOptions.length > 0) { + groups.push({ key, label, options: groupOptions }); + } + } + + const grouped = new Set(GROUP_TABLE.flatMap((group) => group.steps)); + const other = options.filter((option) => !grouped.has(option.id)); + if (other.length > 0) { + groups.push({ key: OTHER_GROUP.key, label: OTHER_GROUP.label, options: other }); + } + + return groups; +} diff --git a/src/app/core/summary-rail.utils.spec.ts b/src/app/core/summary-rail.utils.spec.ts new file mode 100644 index 000000000..df80d6e90 --- /dev/null +++ b/src/app/core/summary-rail.utils.spec.ts @@ -0,0 +1,76 @@ +import { orderByMissing, railCountLabel, railStep, visibleSummaryColumns } from './summary-rail.utils'; + +describe('summary rail utils', () => { + describe('railCountLabel', () => { + it('names the count when nothing is hidden', () => { + expect(railCountLabel(3, 3)).toBe('3 variables'); + expect(railCountLabel(1, 1)).toBe('1 variable'); + }); + + it('says how many of the total the search leaves', () => { + expect(railCountLabel(2, 5)).toBe('2 of 5 variables'); + expect(railCountLabel(1, 4)).toBe('1 of 4 variables'); + expect(railCountLabel(0, 5)).toBe('0 of 5 variables'); + }); + }); + + describe('orderByMissing', () => { + const shares: Record = { a: 0.1, b: 0.5, c: null, d: 0.5, e: 0 }; + const shareOf = (item: string) => shares[item]; + + it('puts the largest share first, keeps ties in place and unpriced items last', () => { + expect(orderByMissing(['a', 'b', 'c', 'd', 'e'], shareOf)).toEqual(['b', 'd', 'a', 'e', 'c']); + }); + + it('returns a new array and leaves the input alone', () => { + const input = ['a', 'b']; + const output = orderByMissing(input, shareOf); + expect(output).toEqual(['b', 'a']); + expect(input).toEqual(['a', 'b']); + expect(output).not.toBe(input); + }); + }); + + describe('railStep', () => { + it('moves one row and stops at the ends', () => { + expect(railStep('ArrowDown', 0, 3)).toBe(1); + expect(railStep('ArrowDown', 2, 3)).toBe(2); + expect(railStep('ArrowUp', 2, 3)).toBe(1); + expect(railStep('ArrowUp', 0, 3)).toBe(0); + }); + + it('jumps to the first and last row', () => { + expect(railStep('Home', 2, 3)).toBe(0); + expect(railStep('End', 0, 3)).toBe(2); + }); + + it('starts at the first row when nothing is selected', () => { + expect(railStep('ArrowDown', -1, 3)).toBe(0); + expect(railStep('ArrowUp', -1, 3)).toBe(0); + }); + + it('ignores other keys and an empty rail', () => { + expect(railStep('Enter', 0, 3)).toBeNull(); + expect(railStep('a', 0, 3)).toBeNull(); + expect(railStep('ArrowDown', 0, 0)).toBeNull(); + }); + }); + + describe('visibleSummaryColumns', () => { + const isRollup = (column: string) => column === 'all'; + + it('drops the pooled column beside a single dataset', () => { + expect(visibleSummaryColumns(['a', 'all'], isRollup)).toEqual(['a']); + expect(visibleSummaryColumns(['all', 'a'], isRollup)).toEqual(['a']); + }); + + it('keeps the pooled column beside two or more datasets', () => { + expect(visibleSummaryColumns(['a', 'b', 'all'], isRollup)).toEqual(['a', 'b', 'all']); + }); + + it('keeps the pooled column when it is the only one', () => { + expect(visibleSummaryColumns(['all'], isRollup)).toEqual(['all']); + expect(visibleSummaryColumns([], isRollup)).toEqual([]); + }); + }); +}); diff --git a/src/app/core/summary-rail.utils.ts b/src/app/core/summary-rail.utils.ts new file mode 100644 index 000000000..cc73606e4 --- /dev/null +++ b/src/app/core/summary-rail.utils.ts @@ -0,0 +1,89 @@ +/** + * Pure helpers for the shared data summary (the variable rail and the statistics table that + * every Data Handling stage previews its records with). No Angular, no DOM. + */ + +/** Rail order: the order the variables were selected in, or most missing first. */ +export type RailOrder = 'selection' | 'missing'; + +/** + * Header text of the rail. `shown` is the number of variables the search leaves, `total` the + * number in the summary. + * - shown === total: "1 variable", "3 variables" + * - otherwise: "2 of 5 variables", "1 of 4 variables", "0 of 5 variables" + */ +export function railCountLabel(shown: number, total: number): string { + if (shown === total) { + return total === 1 ? '1 variable' : `${total} variables`; + } + return `${shown} of ${total} variables`; +} + +/** + * A new array ordered by missing share, largest first. `shareOf` returns the share (0..1) or + * null when the item has no statistics; null items go last. Items with the same share keep + * their original order. The input array is not changed. + */ +export function orderByMissing(items: readonly T[], shareOf: (item: T) => number | null): T[] { + return items + .map((item, index) => ({ item, index, share: shareOf(item) })) + .sort((a, b) => { + if (a.share === null && b.share === null) { + return a.index - b.index; + } + if (a.share === null) { + return 1; + } + if (b.share === null) { + return -1; + } + if (b.share !== a.share) { + return b.share - a.share; + } + return a.index - b.index; + }) + .map(({ item }) => item); +} + +/** + * Where a key press moves the rail selection. `index` is the selected row (-1 when none), + * `count` the number of rows. Returns the new index, or null when the key is not a rail key + * or the rail is empty. + * - ArrowDown: index + 1, stops at the last row (no wrap) + * - ArrowUp: index - 1, stops at the first row (no wrap) + * - Home: 0, End: count - 1 + */ +export function railStep(key: string, index: number, count: number): number | null { + if (count <= 0) { + return null; + } + switch (key) { + case 'ArrowDown': + return Math.min(index + 1, count - 1); + case 'ArrowUp': + return Math.max(index - 1, 0); + case 'Home': + return 0; + case 'End': + return count - 1; + default: + return null; + } +} + +/** + * Columns the statistics table draws. The pooled "all datasets" column repeats the only + * dataset column when a single dataset is selected, so it is dropped then. It stays when two + * or more dataset columns sit beside it, and when it is the only column there is. + * Order is preserved. + */ +export function visibleSummaryColumns( + columns: readonly string[], + isRollup: (column: string) => boolean, +): string[] { + const datasetColumns = columns.filter((column) => !isRollup(column)); + if (datasetColumns.length === 1) { + return datasetColumns; + } + return [...columns]; +} diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.spec.ts b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.spec.ts index dbb668602..f7d8f2ea9 100644 --- a/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.spec.ts +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.spec.ts @@ -748,7 +748,8 @@ describe('ExperimentStudioGuideComponent', () => { expect(exploreStep?.allowTargetInteraction).toBe(true); expect(exploreStep?.body).toContain('Map'); expect(exploreStep?.body).toContain('List'); - expect(exploreStep?.body).toContain('Graph'); + expect(exploreStep?.body).toContain('Tree'); + expect(exploreStep?.body).not.toContain('Graph'); expect(exploreStep?.body).toContain('Next'); expect(exploreStep?.interactionSelectors).toBeUndefined(); expect(steps.some((step: any) => step.id === 'explore-variable-visualizations')).toBe(false); diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide.content.ts b/src/app/pages/experiment-studio/guide/experiment-studio-guide.content.ts index e76557146..0d65a9c40 100644 --- a/src/app/pages/experiment-studio/guide/experiment-studio-guide.content.ts +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide.content.ts @@ -87,7 +87,7 @@ export const EXPERIMENT_STUDIO_GUIDE_STEPS: ExperimentStudioGuideStep[] = [ id: 'variable-selection', section: 'Explore', title: 'Explore Variable Views', - body: 'Browse variables in Map, List, or Graph — try switching modes here, or press Next. Charts and details appear on the right.', + body: 'Browse variables in Map, List, or Tree — try switching modes here, or press Next. Charts and details appear on the right.', selector: '[data-guide="variable-selection"]', allowTargetInteraction: true, }, diff --git a/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.css b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.css new file mode 100644 index 000000000..4903a7d02 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.css @@ -0,0 +1,210 @@ +/* The add row of a pipeline stage. Closed it wears the dashed edge of the rail's other "add" + rows (Filtering's "Add condition"), so it reads as the next node of the flow. Open, the row + becomes the header of one card and the steps list under it as an index: the purpose on the + left, the steps that serve it on the right. Rows, not tiles - nine identical boxes were the + problem this replaces. */ +:host { + display: block; + flex: 1; + min-width: 0; +} + +.step-picker { + margin-bottom: 6px; + border: 1px dashed color-mix(in srgb, var(--primary-color) 25%, transparent); + border-radius: var(--radius-md, 8px); + transition: border-color 0.15s ease; +} + +.step-picker:hover, +.step-picker.is-open { + border-style: solid; + border-color: color-mix(in srgb, var(--primary-color) 45%, transparent); +} + +.step-picker.is-open { + background: var(--card-bg, #ffffff); +} + +.step-picker-trigger { + display: flex; + align-items: center; + gap: 10px; + box-sizing: border-box; + width: 100%; + padding: 10px 12px; + background: transparent; + border: 0; + border-radius: calc(var(--radius-md, 8px) - 1px); + font-family: inherit; + color: inherit; + text-align: left; + cursor: pointer; +} + +.step-picker:not(.is-open) .step-picker-trigger:hover { + background: color-mix(in srgb, var(--primary-color) 7%, white); +} + +.step-picker.is-open .step-picker-trigger { + background: var(--studio-card-header-bg, #f8fbff); + border-bottom: 1px solid var(--border-color, rgba(43, 51, 233, 0.1)); + border-bottom-left-radius: 0; + border-bottom-right-radius: 0; +} + +.step-picker-trigger:focus-visible, +.step-picker-option:focus-visible { + outline: 2px solid var(--primary-color); + outline-offset: -2px; +} + +.step-picker-plus { + flex-shrink: 0; + width: 16px; + color: var(--primary-color); + font-size: 0.7rem; + text-align: center; +} + +.step-picker-text { + display: flex; + flex-direction: column; + gap: 1px; + min-width: 0; +} + +.step-picker-label { + font-size: 0.8rem; + font-weight: 650; + line-height: 1.3; + color: var(--primary-color); +} + +.step-picker-hint { + font-size: 0.72rem; + line-height: 1.35; + color: var(--text-muted); +} + +.step-picker-chevron { + flex-shrink: 0; + margin-left: auto; + font-size: 0.68rem; + color: var(--text-muted); + transition: transform 0.15s ease; +} + +.step-picker.is-open .step-picker-chevron { + transform: rotate(180deg); +} + +.step-picker-menu { + padding: 2px 12px 4px; + animation: step-picker-reveal 0.14s ease-out; +} + +@keyframes step-picker-reveal { + from { + opacity: 0; + transform: translateY(-4px); + } +} + +/* One purpose per band: its label in a fixed left column, its steps beside it. */ +.step-picker-group { + display: grid; + grid-template-columns: 148px minmax(0, 1fr); + column-gap: 12px; + padding: 6px 0; +} + +.step-picker-group + .step-picker-group { + border-top: 1px solid var(--border-color, rgba(43, 51, 233, 0.1)); +} + +.step-picker-group-label { + padding-top: 7px; + font-size: 0.74rem; + font-weight: 600; + line-height: 1.35; + color: var(--text-muted); +} + +.step-picker-options { + display: flex; + flex-direction: column; + gap: 2px; + min-width: 0; +} + +.step-picker-option { + display: flex; + align-items: flex-start; + gap: 10px; + box-sizing: border-box; + width: 100%; + padding: 6px 8px; + background: transparent; + border: 0; + border-radius: var(--radius-sm, 4px); + font-family: inherit; + color: inherit; + text-align: left; + cursor: pointer; +} + +.step-picker-option:hover { + background: var(--tint-nav-hover, #e5ebfa); +} + +.step-picker-option > i { + flex-shrink: 0; + width: 16px; + margin-top: 3px; + color: var(--primary-color); + font-size: 0.82rem; + text-align: center; +} + +.step-picker-option-text { + display: flex; + flex-direction: column; + gap: 1px; + min-width: 0; +} + +.step-picker-option-title { + font-size: 0.8rem; + font-weight: 650; + line-height: 1.3; + color: var(--text-main); +} + +/* The description is the part a clinician reads to choose, so it wraps instead of being cut. */ +.step-picker-option-subtitle { + font-size: 0.72rem; + line-height: 1.4; + color: var(--text-muted); +} + +@media (max-width: 640px) { + .step-picker-group { + grid-template-columns: minmax(0, 1fr); + } + + .step-picker-group-label { + padding: 0 8px 2px; + } +} + +@media (prefers-reduced-motion: reduce) { + .step-picker, + .step-picker-chevron { + transition: none; + } + + .step-picker-menu { + animation: none; + } +} diff --git a/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.html b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.html new file mode 100644 index 000000000..82231e942 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.html @@ -0,0 +1,34 @@ +@if (groups().length) { +
+ + @if (open()) { +
+ @for (group of groups(); track group.key) { +
+ +
+ @for (option of group.options; track option.id) { + + } +
+
+ } +
+ } +
+} diff --git a/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.spec.ts b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.spec.ts new file mode 100644 index 000000000..5b6febe47 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.spec.ts @@ -0,0 +1,152 @@ +import { Component, provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { StepPickerGroup } from '../../../../core/step-picker.utils'; +import { StepPickerComponent } from './step-picker.component'; + +const GROUPS: StepPickerGroup[] = [ + { + key: 'reshape', + label: 'Reshape and scale', + options: [ + { id: 'variable_transformer', icon: 'fas fa-arrow-up-right-dots', title: 'Variable Transforms', subtitle: 'Reshape skewed values.' }, + { id: 'variable_scaler', icon: 'fas fa-ruler-horizontal', title: 'Variable Scaling', subtitle: 'Bring variables to a comparable range.' }, + ], + }, + { + key: 'encode', + label: 'Encode categories', + options: [ + { id: 'one_hot_encoder', icon: 'fas fa-table-columns', title: 'One-Hot Encoder', subtitle: 'Create 0/1 dummy columns.' }, + ], + }, +]; + +@Component({ + imports: [StepPickerComponent], + template: ` + + `, +}) +class HostComponent { + readonly groups = signal(GROUPS); + readonly startOpen = signal(false); + readonly picked: string[] = []; +} + +describe('StepPickerComponent', () => { + let fixture: ComponentFixture; + let host: HostComponent; + let el: HTMLElement; + + const trigger = () => el.querySelector('.step-picker-trigger') as HTMLButtonElement | null; + const options = () => Array.from(el.querySelectorAll('.step-picker-option')) as HTMLButtonElement[]; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HostComponent], + providers: [provideZonelessChangeDetection()], + }); + fixture = TestBed.createComponent(HostComponent); + host = fixture.componentInstance; + el = fixture.nativeElement; + fixture.detectChanges(); + }); + + it('shows one closed add row that says what it adds', () => { + expect(trigger()).not.toBeNull(); + expect(trigger()!.getAttribute('type')).toBe('button'); + expect(trigger()!.getAttribute('aria-expanded')).toBe('false'); + expect(el.querySelector('.step-picker-label')?.textContent?.trim()).toBe('Add a transformation step'); + expect(el.querySelector('.step-picker-hint')?.textContent?.trim()) + .toBe('Reshape values, encode categories, flag records or create columns.'); + // Closed, the steps themselves are not on screen: that is the whole point of the row. + expect(el.querySelector('.step-picker-menu')).toBeNull(); + expect(options()).toEqual([]); + expect(el.querySelector('.step-picker')?.classList.contains('is-open')).toBe(false); + }); + + it('opens the menu from the add row, one labelled group per purpose', () => { + trigger()!.click(); + fixture.detectChanges(); + + expect(trigger()!.getAttribute('aria-expanded')).toBe('true'); + expect(el.querySelector('.step-picker')?.classList.contains('is-open')).toBe(true); + const groups = Array.from(el.querySelectorAll('.step-picker-group')) as HTMLElement[]; + expect(groups.map((group) => group.dataset['group'])).toEqual(['reshape', 'encode']); + expect(groups.map((group) => group.querySelector('.step-picker-group-label')?.textContent?.trim())) + .toEqual(['Reshape and scale', 'Encode categories']); + expect(groups.map((group) => group.querySelectorAll('.step-picker-option').length)).toEqual([2, 1]); + }); + + it('shows each step with its icon, name and full description', () => { + trigger()!.click(); + fixture.detectChanges(); + + expect(options().map((option) => option.dataset['step'])) + .toEqual(['variable_transformer', 'variable_scaler', 'one_hot_encoder']); + const encoder = options()[2]; + expect(encoder.getAttribute('type')).toBe('button'); + expect(encoder.querySelector('i')?.className).toContain('fa-table-columns'); + expect(encoder.querySelector('.step-picker-option-title')?.textContent?.trim()).toBe('One-Hot Encoder'); + expect(encoder.querySelector('.step-picker-option-subtitle')?.textContent?.trim()).toBe('Create 0/1 dummy columns.'); + }); + + it('reports the picked step once and closes the menu', () => { + trigger()!.click(); + fixture.detectChanges(); + + options()[1].click(); + fixture.detectChanges(); + + expect(host.picked).toEqual(['variable_scaler']); + expect(trigger()!.getAttribute('aria-expanded')).toBe('false'); + expect(options()).toEqual([]); + }); + + it('starts open when the parent asks for it, and still folds from the add row', () => { + host.startOpen.set(true); + fixture.detectChanges(); + + expect(trigger()!.getAttribute('aria-expanded')).toBe('true'); + expect(options().length).toBe(3); + + trigger()!.click(); + fixture.detectChanges(); + + expect(trigger()!.getAttribute('aria-expanded')).toBe('false'); + expect(options()).toEqual([]); + }); + + it('closes on Escape without picking anything', () => { + trigger()!.click(); + fixture.detectChanges(); + + options()[0].dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); + fixture.detectChanges(); + + expect(trigger()!.getAttribute('aria-expanded')).toBe('false'); + expect(host.picked).toEqual([]); + }); + + it('leaves the hint line out when there is no hint', () => { + const bare = TestBed.createComponent(StepPickerComponent); + bare.componentRef.setInput('groups', GROUPS); + bare.detectChanges(); + + expect((bare.nativeElement as HTMLElement).querySelector('.step-picker-label')?.textContent?.trim()).toBe('Add a step'); + expect((bare.nativeElement as HTMLElement).querySelector('.step-picker-hint')).toBeNull(); + }); + + it('renders nothing when there is no step left to add', () => { + host.groups.set([]); + fixture.detectChanges(); + + expect(el.querySelector('.step-picker')).toBeNull(); + expect(trigger()).toBeNull(); + }); +}); diff --git a/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.ts b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.ts new file mode 100644 index 000000000..04b65cdf1 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/step-picker/step-picker.component.ts @@ -0,0 +1,39 @@ +import { ChangeDetectionStrategy, Component, input, model, output } from '@angular/core'; +import { StepPickerGroup, StepPickerOption } from '../../../../core/step-picker.utils'; + +/** + * The one "add" row of a pipeline stage. Closed, it is a single dashed row on the stage's rail; + * opened, it lists every step the stage can still add, grouped by purpose (see + * `core/step-picker.utils.ts`). It only reports the pick: the panel opens the station. + */ +@Component({ + selector: 'app-step-picker', + templateUrl: './step-picker.component.html', + styleUrl: './step-picker.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class StepPickerComponent { + /** The menu. With no group the picker renders nothing. */ + readonly groups = input([]); + /** The action on the add row, e.g. "Add a preprocessing step". */ + readonly label = input('Add a step'); + /** One plain sentence under the label saying what kind of steps the menu holds. */ + readonly hint = input(''); + /** Whether the menu is shown. The picker toggles it itself; a parent may also set it. */ + readonly open = model(false); + /** The `id` of the picked option. */ + readonly pick = output(); + + protected toggle(): void { + this.open.update((value) => !value); + } + + protected choose(option: StepPickerOption): void { + this.close(); + this.pick.emit(option.id); + } + + protected close(): void { + this.open.set(false); + } +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.css b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.css new file mode 100644 index 000000000..ef325fc7d --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.css @@ -0,0 +1,188 @@ +:host { + display: block; +} + +/* An idle step draws nothing and takes no room in the stage's flex gap. */ +:host(.is-idle) { + display: none; +} + +.btn-remove-step-node:focus-visible, +.doc-toggle:focus-visible, +.step-quick button:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +.btn-remove-step-node, +.doc-toggle { + display: inline-flex; + align-items: center; + gap: 5px; + padding: 6px 10px; + background: transparent; + border: 0; + color: var(--text-muted); + border-radius: 999px; + font-family: inherit; + font-size: 0.74rem; + font-weight: 600; + cursor: pointer; + transition: all 0.15s ease; +} + +.btn-remove-step-node:hover { + color: #991b1b; + background: color-mix(in srgb, var(--danger) 8%, white); +} + +.doc-toggle:hover { + color: var(--primary-color); + background: color-mix(in srgb, var(--primary-color) 6%, white); +} + +.doc-panel { + margin: 0 0 14px; + padding-left: 12px; + border-left: 2px solid color-mix(in srgb, var(--primary-color) 30%, transparent); + color: var(--text-main); + font-size: 0.8rem; + line-height: 1.5; + white-space: pre-line; +} + +.step-note { + margin: 0 0 14px; + color: var(--text-muted); + font-size: 0.8rem; + line-height: 1.5; +} + +.step-form { + display: grid; + gap: 14px; + padding: 4px 0; +} + +.step-control { + display: grid; + gap: 6px; + max-width: 320px; + color: #1e293b; + font-size: 0.78rem; + font-weight: 700; +} + +.step-control small, +.step-fieldset small, +.step-check small { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 500; +} + +.step-control em, +.step-fieldset em { + color: var(--danger); + font-style: normal; +} + +.step-input, +.step-select { + width: 100%; + box-sizing: border-box; + min-height: 34px; + border: 1px solid rgba(43, 51, 233, 0.15); + background: rgba(255, 255, 255, 0.9); + color: var(--text-main); + border-radius: 8px; + padding: 6px 9px; + font-size: 0.8rem; + transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease; +} + +.step-input:focus-visible, +.step-select:focus-visible { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-color) 18%, transparent); + background: var(--card-bg); +} + +.step-fieldset { + display: grid; + gap: 8px; + margin: 0; + padding: 0; + border: 0; + min-width: 0; +} + +.step-fieldset legend { + padding: 0; + margin-bottom: 4px; + color: #1e293b; + font-size: 0.78rem; + font-weight: 700; +} + +.step-quick { + display: flex; + flex-wrap: wrap; + gap: 6px; +} + +.step-quick button { + padding: 4px 10px; + border: 1px solid rgba(43, 51, 233, 0.2); + border-radius: 999px; + background: transparent; + color: var(--primary-color); + font-family: inherit; + font-size: 0.72rem; + font-weight: 600; + cursor: pointer; +} + +.step-quick button:hover { + background: color-mix(in srgb, var(--primary-color) 7%, white); +} + +.step-variable-list { + display: grid; + gap: 6px; + max-height: 320px; + overflow-y: auto; +} + +.step-row { + display: grid; + grid-template-columns: minmax(120px, 1fr) minmax(140px, 240px); + align-items: center; + gap: 12px; + font-size: 0.8rem; + color: var(--text-main); +} + +.step-check { + display: flex; + align-items: center; + gap: 8px; + font-size: 0.8rem; + color: var(--text-main); +} + +.step-error { + color: #991b1b; + font-size: 0.76rem; + font-weight: 600; +} + +.step-feedback { + padding: 12px 16px; + font-size: 0.8rem; + background: rgba(255, 186, 8, 0.12); + border: 1px solid rgba(255, 186, 8, 0.35); + border-radius: var(--radius-md, 8px); + color: #92400e; +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.html b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.html new file mode 100644 index 000000000..af440144d --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.html @@ -0,0 +1,170 @@ +@if (shown()) { + + @if (isApplied()) { + + } + @if (spec().documentation) { + + } + @if (spec().documentation && docOpen()) { +
{{ spec().documentation }}
+ } + @if (note()) { +

{{ note() }}

+ } + + @if (blockedFields().length) { + + } @else { +
+ @for (field of fields(); track field.key) { + @switch (field.kind) { + @case ('enum') { + + } + @case ('bool') { + + } + @case ('int') { + + } + @case ('real') { + + } + @case ('list') { + + } + @case ('variables') { +
+ {{ field.label }}@if (field.required) {} + @if (field.description) {{{ field.description }}} +
+ + +
+
+ @for (variable of variables(); track variable.code) { + + } +
+
+ } + @case ('variable-map') { +
+ {{ field.label }}@if (field.required) {} + @if (field.description) {{{ field.description }}} + @if (field.itemKind === 'enum' && !isDependent(field)) { +
+ @for (option of field.options; track option) { + + } + +
+ } + @if (isDependent(field) && rowsFor(field).length === 0) { + Appears for the variables set to constant above. + } +
+ @for (variable of rowsFor(field); track variable.code) { + + } +
+
+ } + @default { + + } + } + @if (showErrors() && errors()[field.key]) { + + } + } + @if (variables().length === 0) { +
Select variables before configuring this step.
+ } +
+ } + + +
+} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.spec.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.spec.ts new file mode 100644 index 000000000..91626b149 --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.spec.ts @@ -0,0 +1,134 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { describe, expect, it, beforeEach } from 'vitest'; +import { RawPreprocessingStep } from '../../../../models/backend-algorithms.model'; +import { GenericDataTransformationStepComponent } from './generic-data-transformation-step.component'; + +const param = (extra: Record) => ({ + label: 'L', desc: '', types: ['text'], required: false, multiple: false, default: null, + enums: null, dict_keys_enums: null, dict_values_type: null, dict_values_enums: null, min: null, max: null, + ...extra, +}); + +/** Assumed engine shape: per-variable detection mode plus a threshold. */ +const detector = { + name: 'outlier_detector', label: 'Outlier Detector', desc: 'Flags outliers.', documentation: 'Flags values far from the centre.', + parameters: { + strategies: param({ + label: 'Detection mode', types: ['dict'], required: true, + dict_keys_enums: { type: 'input_var_names', source: ['variables'] }, + dict_values_enums: { type: 'list', source: ['zscore', 'robust'] }, + }), + threshold: param({ label: 'Threshold', types: ['real'], default: 3, min: 0 }), + }, +} as unknown as RawPreprocessingStep; + +const VARIABLES = [{ code: 'age', label: 'Age', type: 'real' }, { code: 'mmse', label: 'MMSE', type: 'real' }]; + +describe('GenericDataTransformationStepComponent', () => { + let fixture: ComponentFixture; + let applied: Record[]; + let removed: number; + let el: HTMLElement; + + async function settle() { + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + } + + beforeEach(async () => { + TestBed.configureTestingModule({ + imports: [GenericDataTransformationStepComponent], + providers: [provideZonelessChangeDetection()], + }); + fixture = TestBed.createComponent(GenericDataTransformationStepComponent); + el = fixture.nativeElement; + applied = []; + removed = 0; + fixture.componentInstance.applyStep.subscribe((value) => applied.push(value)); + fixture.componentInstance.removeStep.subscribe(() => removed++); + fixture.componentRef.setInput('spec', detector); + fixture.componentRef.setInput('variables', VARIABLES); + await settle(); + }); + + it('draws nothing until the step is opened or applied: the stage menu is what adds it', () => { + expect(el.querySelector('app-station-card')).toBeNull(); + expect(el.querySelector('.btn-add-dashed')).toBeNull(); + expect(el.textContent?.trim()).toBe(''); + // An idle step takes no room either: the stage lays its stations out with a gap. + expect(getComputedStyle(el).display).toBe('none'); + }); + + it('draws the station once the panel opens it, and keeps it on screen when it is folded', async () => { + fixture.componentRef.setInput('open', true); + await settle(); + expect(el.querySelector('app-station-card')).not.toBeNull(); + expect(el.querySelector('.btn-add-dashed')).toBeNull(); + expect(getComputedStyle(el).display).toBe('block'); + + fixture.componentRef.setInput('open', false); + await settle(); + expect(el.querySelector('app-station-card')).not.toBeNull(); + }); + + it('uses the step\'s own icon on the station', async () => { + fixture.componentRef.setInput('open', true); + await settle(); + expect(el.querySelector('app-station-card i.fa-magnifying-glass-chart')).not.toBeNull(); + }); + + it('applies the schema-shaped payload built from the form', async () => { + fixture.componentRef.setInput('open', true); + await settle(); + const select = el.querySelector('select[data-variable="age"]') as HTMLSelectElement; + select.value = 'zscore'; + select.dispatchEvent(new Event('change')); + const threshold = el.querySelector('input[data-param="threshold"]') as HTMLInputElement; + threshold.value = '2.5'; + threshold.dispatchEvent(new Event('input')); + await settle(); + + const buttons = Array.from(el.querySelectorAll('app-station-action-bar button')) as HTMLButtonElement[]; + buttons.find((b) => b.textContent?.includes('Apply'))!.click(); + expect(applied).toEqual([{ strategies: { age: 'zscore' }, threshold: 2.5 }]); + }); + + it('blocks Apply and names the missing required field', async () => { + fixture.componentRef.setInput('open', true); + await settle(); + // Only the threshold is touched, so the required per-variable mode is still empty. + const threshold = el.querySelector('input[data-param="threshold"]') as HTMLInputElement; + threshold.value = '4'; + threshold.dispatchEvent(new Event('input')); + await settle(); + const buttons = Array.from(el.querySelectorAll('app-station-action-bar button')) as HTMLButtonElement[]; + buttons.find((b) => b.textContent?.includes('Apply'))!.click(); + await settle(); + expect(applied).toEqual([]); + expect(el.querySelector('.step-error')?.textContent).toContain('required'); + }); + + it('hydrates from the applied parameters and removes the step', async () => { + fixture.componentRef.setInput('applied', { strategies: { mmse: 'robust' }, threshold: 2 }); + fixture.componentRef.setInput('open', true); + await settle(); + expect((el.querySelector('select[data-variable="mmse"]') as HTMLSelectElement).value).toBe('robust'); + expect((el.querySelector('input[data-param="threshold"]') as HTMLInputElement).value).toBe('2'); + expect(el.querySelector('.preprocessing-state-chip')?.textContent).toContain('Applied'); + (el.querySelector('.btn-remove-step-node') as HTMLButtonElement).click(); + expect(removed).toBe(1); + }); + + it('flags a required parameter it cannot edit instead of sending a broken step', async () => { + const recipeStep = { + name: 'x_step', label: 'X Step', desc: 'Needs a recipe.', + parameters: { reusable_preprocessing: param({ types: ['dict'], required: true, label: 'Fitted recipe' }) }, + } as unknown as RawPreprocessingStep; + fixture.componentRef.setInput('spec', recipeStep); + fixture.componentRef.setInput('open', true); + await settle(); + expect(el.querySelector('.step-feedback')?.textContent).toContain('fitted recipe'); + }); +}); diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.ts new file mode 100644 index 000000000..a423319fd --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/generic-data-transformation-step/generic-data-transformation-step.component.ts @@ -0,0 +1,218 @@ +import { ChangeDetectionStrategy, Component, computed, effect, input, output, signal, untracked } from '@angular/core'; +import { FormsModule } from '@angular/forms'; +import { RawPreprocessingStep } from '../../../../models/backend-algorithms.model'; +import { stepIcon } from '../../../../core/step-picker.utils'; +import { + DEPENDENT_MAPS, + StepField, + StepFormValues, + buildStepParameters, + stepFields, + stepNote, + stepValuesFromParameters, + unsupportedRequiredFields, + valueOptionsForVariable, +} from '../../../../core/data-transformation-step-form.utils'; +import { isOutlierEligibleVariable } from '../../../../core/outlier-rules'; +import { StationActionBarComponent } from '../../shared/station-action-bar/station-action-bar.component'; +import { StationCardComponent } from '../../shared/station-card/station-card.component'; + +/** A selected variable as the panel hands it over: only the code, label and type are read. */ +interface StepVariable { + code: string; + label?: string; + name?: string; + type?: string; +} + +/** + * Station for any step the engine lists in its catalog that has no dedicated station. It renders + * both families, in-place preprocessing (global imputer, global winsorizer) and data + * transformation (new columns or changed meaning); the panel places each in its own stage. + * The form is built from the step's parameter schema and the request parameters are built by + * `buildStepParameters`, so the payload matches the engine's specification. + * Apply and Remove are reported to the panel, which owns the applied pipeline config. + */ +@Component({ + selector: 'app-generic-data-transformation-step', + imports: [FormsModule, StationCardComponent, StationActionBarComponent], + templateUrl: './generic-data-transformation-step.component.html', + styleUrl: './generic-data-transformation-step.component.css', + host: { '[attr.data-step]': 'spec().name', '[class.is-idle]': '!shown()' }, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class GenericDataTransformationStepComponent { + readonly spec = input.required(); + /** Variables the step may act on: the experiment's selected, non-derived variables. */ + readonly variables = input([]); + /** Parameters in the applied config, or null when the step is not part of the run. */ + readonly applied = input | null>(null); + readonly open = input(false); + readonly busy = input(false); + + readonly openChange = output(); + readonly applyStep = output>(); + readonly removeStep = output(); + + protected readonly note = computed(() => stepNote(this.spec().name)); + protected readonly icon = computed(() => stepIcon(this.spec().name)); + protected readonly fields = computed(() => stepFields(this.spec())); + protected readonly blockedFields = computed(() => unsupportedRequiredFields(this.fields())); + protected readonly values = signal({}); + protected readonly started = signal(false); + protected readonly showErrors = signal(false); + protected readonly docOpen = signal(false); + + /** Content key of the applied parameters, so an unrelated config change keeps the draft. */ + private readonly appliedKey = computed(() => JSON.stringify(this.applied())); + private readonly codes = computed(() => new Set(this.variables().map((variable) => variable.code))); + private readonly numericalCodes = computed(() => + new Set(this.variables().filter((variable) => isOutlierEligibleVariable(variable)).map((variable) => variable.code))); + private readonly built = computed(() => + buildStepParameters(this.fields(), this.values(), this.codes(), { stepName: this.spec().name, numericalCodes: this.numericalCodes() })); + + protected readonly errors = computed(() => this.built().errors); + protected readonly isApplied = computed(() => this.applied() !== null); + /** The station is drawn once it is applied or the user opened it; otherwise only the add row. */ + protected readonly shown = computed(() => this.isApplied() || this.started()); + + /** 1 when the form differs from what is applied (an incomplete form counts as a change). */ + protected readonly pendingCount = computed(() => { + const draft = JSON.stringify(this.built().parameters); + const applied = JSON.stringify(this.applied() ?? {}); + if (!this.isApplied() && Object.keys(this.built().parameters).length === 0) return 0; + return draft === applied ? 0 : 1; + }); + + protected readonly statusLabel = computed(() => { + if (this.pendingCount() > 0) return 'Not applied'; + return this.isApplied() ? 'Applied' : 'Not set'; + }); + protected readonly statusTone = computed<'applied' | 'pending' | 'default'>(() => + this.pendingCount() > 0 ? 'pending' : this.isApplied() ? 'applied' : 'default', + ); + + constructor() { + // The form follows the applied config: a hydrated saved experiment, a Discard, or a change + // of the selection that trims the applied step all land here. + effect(() => { + this.appliedKey(); + const fields = this.fields(); + untracked(() => this.values.set(stepValuesFromParameters(fields, this.applied()))); + }); + effect(() => { + if (this.open()) untracked(() => this.started.set(true)); + }); + } + + protected toggle(): void { + this.openChange.emit(!this.open()); + } + + protected toggleDoc(): void { + this.docOpen.update((open) => !open); + } + + protected variableLabel(variable: StepVariable): string { + return variable.label || variable.name || variable.code; + } + + /** Enum values of a per-variable row that the variable's type allows. */ + protected optionsFor(field: StepField, variable: StepVariable): string[] { + return valueOptionsForVariable(this.spec().name, field.options, this.numericalCodes().has(variable.code)); + } + + /** Variables with a row in a per-variable map: dependent maps list only the variables that need them. */ + protected rowsFor(field: StepField): StepVariable[] { + const rule = (DEPENDENT_MAPS[this.spec().name] ?? []).find((candidate) => candidate.key === field.key); + if (!rule) return this.variables(); + const governing = (this.values()[rule.on] ?? {}) as Record; + return this.variables().filter((variable) => governing[variable.code] === rule.value); + } + + protected isDependent(field: StepField): boolean { + return (DEPENDENT_MAPS[this.spec().name] ?? []).some((candidate) => candidate.key === field.key); + } + + protected optionLabel(option: string): string { + return option.replace(/_/g, ' '); + } + + protected scalar(field: StepField): string { + const value = this.values()[field.key]; + return typeof value === 'string' ? value : ''; + } + + protected flag(field: StepField): boolean { + return this.values()[field.key] === true; + } + + protected setValue(field: StepField, value: unknown): void { + this.values.update((values) => ({ ...values, [field.key]: value })); + } + + protected isSelected(field: StepField, code: string): boolean { + const value = this.values()[field.key]; + return Array.isArray(value) && value.includes(code); + } + + protected toggleVariable(field: StepField, code: string): void { + const value = this.values()[field.key]; + const current = Array.isArray(value) ? (value as string[]) : []; + this.setValue(field, current.includes(code) ? current.filter((item) => item !== code) : [...current, code]); + } + + protected selectAllVariables(field: StepField): void { + this.setValue(field, this.variables().map((variable) => variable.code)); + } + + protected clearVariables(field: StepField): void { + this.setValue(field, []); + } + + protected mapValue(field: StepField, code: string): string { + const map = this.values()[field.key] as Record | undefined; + return map?.[code] ?? ''; + } + + protected setMapValue(field: StepField, code: string, value: string): void { + const map = { ...((this.values()[field.key] as Record | undefined) ?? {}) }; + if (value === '' || value === null || value === undefined) delete map[code]; + else map[code] = value; + this.setValue(field, map); + } + + /** Set every selected variable that accepts the value (a numerical-only mean skips nominal ones). */ + protected setAllMapValues(field: StepField, value: string): void { + const accepting = value === '' + ? this.variables() + : this.variables().filter((variable) => this.optionsFor(field, variable).includes(value)); + this.setValue(field, Object.fromEntries(accepting.map((variable) => [variable.code, value]))); + } + + protected discard(): void { + this.showErrors.set(false); + this.values.set(stepValuesFromParameters(this.fields(), this.applied())); + } + + protected commitOrClose(): void { + if (this.pendingCount() === 0) { + this.openChange.emit(false); + return; + } + if (this.blockedFields().length) return; + if (Object.keys(this.errors()).length) { + this.showErrors.set(true); + return; + } + this.showErrors.set(false); + this.applyStep.emit(this.built().parameters); + } + + protected remove(): void { + this.started.set(false); + this.showErrors.set(false); + this.removeStep.emit(); + this.openChange.emit(false); + } +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.css b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.css index c2b32d8fa..c3f8d1e08 100644 --- a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.css +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.css @@ -660,42 +660,6 @@ box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05); } -/* Add-on action: the button is the dashed target itself (full width), so the - hover elevation lands on the thing the user is about to click. */ -.btn-add-dashed { - width: 100%; - box-sizing: border-box; - display: inline-flex; - align-items: center; - justify-content: center; - gap: 7px; - padding: 12px 16px; - background: color-mix(in srgb, var(--primary-color) 2.5%, white); - border: 1.5px dashed color-mix(in srgb, var(--primary-color) 30%, transparent); - border-radius: var(--radius-md, 8px); - color: var(--primary-color); - font-family: inherit; - font-size: 0.84rem; - font-weight: 650; - cursor: pointer; - transition: background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease; -} - -.btn-add-dashed:hover { - background: color-mix(in srgb, var(--primary-color) 7%, white); - border-color: var(--primary-color); - box-shadow: 0 2px 8px rgba(43, 51, 233, 0.08); -} - -.btn-add-dashed:focus-visible { - outline: 2px solid var(--color-focus); - outline-offset: 2px; -} - -.btn-add-dashed i { - font-size: 0.78rem; -} - /* Terminal Card */ .pipeline-terminal-card { display: grid; diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.html b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.html index f652f2be8..449a8e350 100644 --- a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.html +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.html @@ -63,23 +63,31 @@

Data Handling Pipeline

@if (!summary.isLoading) {
@if (summary.data.length > 0) { - @if (filteredStatisticBlocks(kind).length > 0) {