diff --git a/.agent/workflows/experiment-studio-flow.md b/.agent/workflows/experiment-studio-flow.md index 1ab497536..372193e5a 100644 --- a/.agent/workflows/experiment-studio-flow.md +++ b/.agent/workflows/experiment-studio-flow.md @@ -4,21 +4,49 @@ description: How the Experiment Studio frontend flow works # Experiment Studio Frontend Flow -This guide describes the core user journey and technical implementation of the Experiment Studio. +This guide describes the current Experiment Studio user journey and the frontend pieces that coordinate dataset selection, descriptive review, preprocessing, and algorithm execution. ## 1. Core User Journey -1. **Select Pathology (Data Model)**: Choosing the domain of study (e.g., Stroke). -2. **Select Datasets**: Enabling specific data sources to explore. -3. **Explore Variables**: Using the zoomable bubble chart to browse the data hierarchy. -4. **Select Parameters**: - * **Variables (Y)**: Target variables for analysis. - * **Covariates (X)**: Predictor variables. - * **Filters**: Subset the data based on variable values. -5. **View Distributions**: Reviewing histograms of selected variables across datasets. -6. **Run Algorithms**: Selecting and configuring a statistical algorithm based on selected parameters. +1. **Select Pathology (Data Model)**: Choose the domain of study, for example Stroke. +2. **Select Datasets**: Enable the datasets that should participate in exploration and analysis. +3. **Explore Variables**: Use the zoomable bubble chart to browse the data hierarchy and inspect variable metadata. +4. **Select Parameters**: + - **Variables (Y)**: Target variables for analysis. + - **Covariates (X)**: Predictor or grouping variables. + - **Filters**: Optional dataset subset rules based on variable values. +5. **Review Data & Configure Preprocessing**: Use the existing **Descriptive statistics** sidebar item. The internal panel is now **DATA REVIEW & PREPROCESSING** and contains: + - **Raw Data Summary**: Before view, based on the selected raw dataset and variables. + - **Preprocessing Setup**: Configure supported preprocessing rules. Currently only **Missing Values** is executable. + - **Processed Data Summary**: After view, recalculated only after preprocessing is applied. +6. **Run Algorithms**: Select and configure a compatible algorithm. If preprocessing was applied, the Algorithm step receives the applied preprocessing configuration. Pending preprocessing changes do not affect algorithm requests. -## 2. Technical Architecture +## 2. Simple End-to-End Flow + +This is the recommended smoke-test flow for the current setup: + +1. Open **Experiment Studio**. +2. Keep or select the Stroke data model and the desired datasets. +3. Add **Age** as a **Variable**. +4. Add **Sex** as a **Covariate**. +5. Open **Descriptive statistics** in the left sidebar. +6. In **DATA REVIEW & PREPROCESSING**, review **Raw Data Summary**. +7. Open **Preprocessing Setup**. +8. In **Missing Values**, set **Age** to **Remove rows**. +9. Click **Apply Preprocessing**. +10. Confirm **Processed Data Summary** expands and shows recalculated statistics. +11. Open **Algorithm**. +12. Select **One-way ANOVA**. +13. Run the experiment. + +Expected behavior: +- The raw summary uses the original selected data. +- The preprocessing table shows only supported preprocessing capabilities. At the moment, that is **Missing Values**. +- **Remove rows** maps to the Exaflow missing-values strategy `drop`. +- The processed summary is recalculated only after clicking **Apply Preprocessing**. +- The algorithm request uses the applied preprocessing configuration. + +## 3. Technical Architecture ### Component Hierarchy - `ExperimentStudioComponent`: Main orchestrator and route handler. @@ -26,31 +54,80 @@ This guide describes the core user journey and technical implementation of the E - `BubbleChartComponent`: D3-based visualization of the data model. - `VariableFilterSelectionComponent`: Manages selected variables, covariates, and filters. - `DistributionGraphComponent`: Renders histograms for selected nodes. + - `StatisticAnalysisPanelComponent`: Owns the **DATA REVIEW & PREPROCESSING** UI inside the existing **Descriptive statistics** section. - `AlgorithmPanelComponent`: Manages algorithm selection, configuration, and execution. ### State Management -- **Service**: [ExperimentStudioService](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/services/experiment-studio.service.ts) +- **Service**: [`ExperimentStudioService`](../../src/app/services/experiment-studio.service.ts) - **Signals**: Used extensively for reactive state (e.g., `selectedDataModel()`, `selectedVariables()`). -- **Communication**: Components inject the service to read/write state. +- **Communication**: Components inject the service to read/write state and to build descriptive-statistics and algorithm requests. +- **Applied preprocessing**: Stored in the experiment studio service only after the user clicks **Apply Preprocessing**. +- **Pending preprocessing**: Local to the statistic panel and never used by the Algorithm step. ### Important Logic - **D3 Interaction**: Leaf node clicks in the bubble chart update the `selectedNode` in the parent panel, which then triggers a histogram fetch via the service. - **Double-Click Shortcut**: Double-clicking a leaf node in the bubble chart automatically adds it to the **Variables** list. - **Onboarding Hints**: Selection buttons (+Variables, etc.) pulse when a variable is selected but not yet added. -- **Algorithm Rules**: [AlgorithmRulesService](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/services/algorithm-rules.service.ts) contains the logic for enabling/disabling algorithms based on selected variable types and counts. +- **Algorithm Rules**: [`AlgorithmRulesService`](../../src/app/services/algorithm-rules.service.ts) contains the logic for enabling/disabling algorithms based on selected variable types and counts. - **Auto-Expansion**: Algorithm categories automatically expand in the sidebar when compatible variables are selected and algorithms become available. - **Requirement Key**: A legend at the bottom of the algorithm panel clarifies the meaning of requirement icons (Database = Variable, Sliders = Covariate). +- **Descriptive Statistics Request**: Raw summary calls the descriptive overview with `preprocessing: null`. +- **Processed Summary Request**: Processed summary calls the descriptive overview with explicit applied preprocessing. +- **Missing Values Preprocessing**: + - `No action`: omitted from Exaflow `strategies`. + - `Remove rows`: `drop`. + - `Mean imputation`: `mean`, numeric only. + - `Median imputation`: `median`, numeric only. + - `Constant value`: `constant` plus `fill_values[var]`. +- **Unsupported Preprocessing Categories**: Outliers, numeric transformations, category handling, scaling, and encoding are not shown as active choices in the current UI because the current Exaflow preprocessing API does not execute them. + +## 4. Data Review & Preprocessing Details + +The sidebar navigation remains unchanged: -## 3. Common Pitfalls & Tips +1. **Datasets & Variables** +2. **Descriptive statistics** +3. **Algorithm** + +The old descriptive-statistics panel has been expanded internally, but it is still reached through **Descriptive statistics**. Do not add a new sidebar item for preprocessing. + +### Raw Data Summary +- Open by default. +- Shows statistics for the original selected dataset. +- Has only **Variables** and **Distributions** tabs. +- Does not show a **Model** tab. + +### Preprocessing Setup +- Contains the supported preprocessing controls for the selected variables and covariates. +- Current executable scope is **Missing Values** only. +- Search filters visible variable rows by variable label or code. +- Changing a rule creates a pending state. +- **Reset Changes**, **Preview Impact**, and **Apply Preprocessing** are disabled until there are pending changes. +- Applying validates the rules, sends a processed descriptive overview request, stores the applied preprocessing configuration, clears pending changes, and expands the processed summary. + +### Processed Data Summary +- Collapsed/empty until preprocessing has been applied. +- After apply, it mirrors Raw Data Summary layout and shows only **Variables** and **Distributions**. +- Displays statistics recalculated from the processed descriptive overview response. +- Shows the note: **Based on applied preprocessing rules**. + +## 5. Common Pitfalls & Tips - **Change Detection**: The project uses `ChangeDetectionStrategy.OnPush`. When integrating with D3 or other non-Angular events, you MUST manually trigger `ChangeDetectorRef.detectChanges()` to ensure updates propagate to child components. - **Signal Writes**: Reactive effects that update state (like the auto-expansion logic) must use `{ allowSignalWrites: true }`. -- **D3 Hierarchy**: The bubble chart uses [zoomable-circle-packing.ts](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts). Ensure any changes to interaction logic are applied here and emitted via `@Output` in the wrapper component. +- **D3 Hierarchy**: The bubble chart uses [`zoomable-circle-packing.ts`](../../src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts). Ensure any changes to interaction logic are applied here and emitted via `@Output` in the wrapper component. - **Dataset Enrichment**: Adding a variable usually requires "enriching" it (fetching enumerations or metadata) before it can be used in algorithms. This is handled by `addVariableAndEnrich()`. +- **Pending vs Applied Preprocessing**: Pending preprocessing rules must not update processed summaries or algorithm requests. Only applied preprocessing should be used downstream. +- **Describe Calls**: Do not rely on saved descriptive results. Raw and processed summaries are recalculated through descriptive overview calls. +- **Unsupported Preprocessing**: Do not add UI controls for transformations that Exaflow cannot execute unless the backend/API support is added first. + +## 6. Key Files -## 4. Key Files -- [experiment-studio.component.ts](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/pages/experiment-studio/experiment-studio.component.ts) -- [experiment-studio.service.ts](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/services/experiment-studio.service.ts) -- [algorithm-panel.component.ts](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts) -- [algorithm-rules.service.ts](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/services/algorithm-rules.service.ts) -- [zoomable-circle-packing.ts](file:///home/kfilippopolitis/Desktop/platform-ui/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts) +- [`experiment-studio.component.ts`](../../src/app/pages/experiment-studio/experiment-studio.component.ts) +- [`experiment-studio.service.ts`](../../src/app/services/experiment-studio.service.ts) +- [`statistic-analysis-panel.component.ts`](../../src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.ts) +- [`statistic-analysis-panel.component.html`](../../src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.html) +- [`statistic-analysis-panel.component.css`](../../src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.css) +- [`algorithm-panel.component.ts`](../../src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts) +- [`algorithm-rules.service.ts`](../../src/app/services/algorithm-rules.service.ts) +- [`zoomable-circle-packing.ts`](../../src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts) diff --git a/.cursor/rules/browser.mdc b/.cursor/rules/browser.mdc new file mode 100644 index 000000000..f659f8acb --- /dev/null +++ b/.cursor/rules/browser.mdc @@ -0,0 +1,12 @@ +--- +description: Use Cursor IDE browser MCP for UI checks; never Playwright unless asked +alwaysApply: true +--- + +# Browser for UI verification + +Use the Cursor IDE browser MCP only (`browser_navigate`, `browser_lock`, `browser_snapshot`, `browser_take_screenshot`, `browser_click`). + +Do not use Playwright, playwright-cli, `npx playwright`, Puppeteer, or a headed Chromium from the shell unless the user explicitly asks. + +If the MCP browser is missing, stop and say so. Do not install or run Playwright as a substitute. diff --git a/.cursor/rules/llm-wiki.mdc b/.cursor/rules/llm-wiki.mdc new file mode 100644 index 000000000..9a284984b --- /dev/null +++ b/.cursor/rules/llm-wiki.mdc @@ -0,0 +1,23 @@ +--- +description: Use the platform-ui LLM wiki to load minimal repository context. +alwaysApply: true +--- + +# LLM Wiki Context Rule + +Before answering project questions or editing code, start with +`docs/llm-wiki/00-agent-entrypoint.md`. + +Use the wiki as an index, not as bulk context: + +- Read the entrypoint first. +- Pick only one topic page for the task. +- Use `docs/llm-wiki/indexes/files-by-feature.md` or + `docs/llm-wiki/indexes/symbols-by-feature.md` to identify exact source files. +- Search by route, endpoint, symbol, selector, CSS class, algorithm name, or + result key before opening files. +- Open broad folders, large mapper files, templates, or styles only when the + task is complex or a targeted lookup is inconclusive. + +Do not duplicate the wiki in Cursor rules. Keep this rule short so it stays cheap +to include in every Cursor chat. diff --git a/.cursor/rules/ui-design.mdc b/.cursor/rules/ui-design.mdc new file mode 100644 index 000000000..dc876a745 --- /dev/null +++ b/.cursor/rules/ui-design.mdc @@ -0,0 +1,13 @@ +--- +description: MIP UI must match DESIGN.md tokens and existing studio chrome +globs: "**/*.{css,html,scss},**/experiment-studio/**" +alwaysApply: false +--- + +# MIP UI + +Read [DESIGN.md](DESIGN.md) before changing visuals. Reuse existing CSS variables and Angular Material chrome. Do not invent palettes, display type, or animation libraries. + +Conflict rule: live tokens in `src/styles.css` `:root` win for app chrome; charter rules in DESIGN.md still win for logo geometry and logo colorways. + +After any visual change, follow the `ui-review` skill: render the affected page, compare to an unchanged sibling panel, fix mismatches, do not restyle the whole app. Cursor: IDE browser MCP. Pi: `.pi/skills/ui-review` (CDP + `read` PNG). Do not use Playwright unless explicitly asked. diff --git a/.cursor/skills/ui-review/SKILL.md b/.cursor/skills/ui-review/SKILL.md new file mode 100644 index 000000000..cae248efd --- /dev/null +++ b/.cursor/skills/ui-review/SKILL.md @@ -0,0 +1,10 @@ +--- +name: ui-review +description: After frontend changes in MIP, render the affected page in the Cursor IDE browser MCP (not Playwright), compare it to adjacent existing UI, and fix homogeneity gaps before stopping. Use when CSS, templates, or visual layout changed. +--- + +Canonical instructions: [`.pi/skills/ui-review/SKILL.md`](../../../.pi/skills/ui-review/SKILL.md). + +Cursor: IDE browser MCP (`browser_navigate` / `browser_lock` / `browser_snapshot` / `browser_take_screenshot` / `browser_click`). Unlock when done. Do not use Playwright unless asked. + +Pi: follow that file (CDP + `read` PNG). Do not use Cursor `browser_*`. diff --git a/.dockerignore b/.dockerignore index 7fb5c09dc..60f8c238a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,4 +11,4 @@ Dockerfile npm-debug.log README.md AGENTS.md -DESIGN_SYSTEM.yaml +DESIGN.md diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 000000000..d7ee37e9a --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,20 @@ +version: 2 +updates: + # Keep SHA-pinned actions in workflows up to date (updates the pin and its + # version comment). Renovate is scoped to the setup scripts only, so the two + # bots don't overlap (see renovate.json). + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" + # One combined PR for all action bumps instead of one PR per action + groups: + github-actions: + patterns: + - "*" + # 7-day cooldown, matching the scanners: never propose a release younger + # than 7 days, so yanked or compromised releases never reach a PRR + cooldown: + default-days: 7 + labels: + - "dependencies" diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml new file mode 100644 index 000000000..78c6d995f --- /dev/null +++ b/.github/workflows/checks.yml @@ -0,0 +1,29 @@ +name: Static checks + +# Typecheck, dead-code scan and a production build — the three checks that need no +# backend, browser or secrets. The Vitest unit tests run in unit-tests.yml. +on: + pull_request: + workflow_dispatch: + +jobs: + static: + name: Typecheck, dead code, build + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Node + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version-file: .nvmrc + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Verify (typecheck + dead code + build) + run: npm run verify diff --git a/.github/workflows/container-scan.yml b/.github/workflows/container-scan.yml new file mode 100644 index 000000000..b3ac413f6 --- /dev/null +++ b/.github/workflows/container-scan.yml @@ -0,0 +1,108 @@ +name: Container Vulnerability Scanning + +on: + pull_request: + push: + branches: + - master + workflow_dispatch: + schedule: + - cron: '0 2 * * 1' # weekly scan, every Monday 02:00 UTC + +jobs: + container-scan: + name: Container Vulnerability Scanning + runs-on: ubuntu-latest + permissions: + contents: read + security-events: write # required for uploading SCA results to github security + env: + IMAGE_NAME: platform-ui:testing + CONTAINER_SCAN_MERGED_SARIF_OUTPUT: container-scan-platform-ui-merged.sarif + + # SCA / CVE + TRIVY_IGNOREFILE: ci/suppress_trivy.yaml + OSV_IGNOREFILE: ci/suppress_osv_scanner.toml + TRIVY_SCA_SARIF_OUTPUT: sca-trivy-container.sarif + OSV_SCA_SARIF_OUTPUT: sca-osv-container.sarif + # CVSS gate: fail at >= FAIL, warn in between. Override per project. + GATE_FAIL_THRESHOLD: "8.0" + GATE_WARN_THRESHOLD: "5.0" + + # SAST / Linting + SEMGREP_CONFIG_RULESETS: >- + semgrep-rules/dockerfile + OPENGREP_SAST_SARIF_OUTPUT: sast-opengrep-dockerfile.sarif + HADOLINT_SAST_SARIF_OUTPUT: sast-hadolint-dockerfile.sarif + + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Python + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + # renovate: datasource=python-version depName=python + python-version: '3.15.2' + + - name: Build Docker image + run: docker build -t ${{ env.IMAGE_NAME }} . + + - name: Setup tools + run: | + bash ci/setup-tools.sh \ + --install-tool trivy,osv-scanner,opengrep,hadolint,semgrep-rules + + - name: Run SAST scanning + run: python ci/container_scan.py --scan-type sast + + - name: Run SCA scanning + if: ${{ !cancelled() }} + run: python ci/container_scan.py --scan-type sca --image ${{ env.IMAGE_NAME }} + + - name: Merge all SARIF reports + if: ${{ !cancelled() }} + run: | + python ci/container_scan.py \ + --merge-sarif "${{ env.TRIVY_SCA_SARIF_OUTPUT }}" "${{ env.OSV_SCA_SARIF_OUTPUT }}" "${{ env.OPENGREP_SAST_SARIF_OUTPUT }}" "${{ env.HADOLINT_SAST_SARIF_OUTPUT }}" \ + --merge-output "${{ env.CONTAINER_SCAN_MERGED_SARIF_OUTPUT }}" + + - name: Upload merged SARIF to GitHub Security tab + if: ${{ !cancelled() && hashFiles(env.CONTAINER_SCAN_MERGED_SARIF_OUTPUT) != '' }} + uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1 + with: + sarif_file: ${{ env.CONTAINER_SCAN_MERGED_SARIF_OUTPUT }} + category: container-scan + + # Artifact downloads are gated only by repo read access, and GitHub has no + # per-artifact permissions. Encrypt before upload, and delete the plaintext + # so no later step can publish it. + - name: Encrypt SARIF report + id: encrypt + if: ${{ !cancelled() && hashFiles(env.CONTAINER_SCAN_MERGED_SARIF_OUTPUT) != '' }} + env: + PW: ${{ secrets.ARTIFACT_PASSWORD }} + OUT: ${{ env.CONTAINER_SCAN_MERGED_SARIF_OUTPUT }}.gpg + run: | + if [ -z "$PW" ]; then + echo "::warning title=SARIF report not uploaded::ARTIFACT_PASSWORD is not available in this run (Dependabot, Renovate or fork PR). Skipping encryption and upload." + rm -f -- "$CONTAINER_SCAN_MERGED_SARIF_OUTPUT" + exit 0 + fi + printf '%s' "$PW" | gpg --symmetric --batch --yes --quiet \ + --pinentry-mode loopback --passphrase-fd 0 \ + --no-symkey-cache \ + --cipher-algo AES256 \ + --s2k-mode 3 --s2k-digest-algo SHA512 --s2k-count 65011712 \ + --output "$OUT" -- "$CONTAINER_SCAN_MERGED_SARIF_OUTPUT" + rm -f -- "$CONTAINER_SCAN_MERGED_SARIF_OUTPUT" + echo "encrypted=$OUT" >> "$GITHUB_OUTPUT" + + - name: Upload encrypted SARIF artifact + if: ${{ !cancelled() && steps.encrypt.outputs.encrypted != '' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: container-scan-sarif-report + path: ${{ steps.encrypt.outputs.encrypted }} + retention-days: 30 + if-no-files-found: error diff --git a/.github/workflows/e2e-experiment-studio.yml b/.github/workflows/e2e-experiment-studio.yml new file mode 100644 index 000000000..46eef0eb5 --- /dev/null +++ b/.github/workflows/e2e-experiment-studio.yml @@ -0,0 +1,45 @@ +name: Experiment studio e2e + +# Playwright flows in e2e/ (studio, dashboard, access). Backend calls are mocked in the +# browser (every same-origin /services request). No live API and no secrets. +# The Vitest unit tests run in unit-tests.yml. +on: + pull_request: + workflow_dispatch: + +jobs: + experiment-studio: + name: Playwright experiment studio + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Node + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version-file: .nvmrc + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Install Playwright Chromium + run: npx playwright install --with-deps chromium + + - name: E2E flows + run: npm run e2e + env: + CI: true + + # Traces and screenshots of failed tests only (see playwright.config.ts). + - name: Upload failure traces + if: ${{ failure() }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: e2e-test-results + path: e2e/test-results + retention-days: 7 + if-no-files-found: ignore diff --git a/.github/workflows/ebrains.yml b/.github/workflows/ebrains.yml new file mode 100644 index 000000000..33da9030b --- /dev/null +++ b/.github/workflows/ebrains.yml @@ -0,0 +1,26 @@ +name: Mirror to Ebrains + +on: + push: + branches: [master] + tags: + - "*" + +jobs: + to_ebrains: + runs-on: ubuntu-latest + steps: + - name: syncmaster + uses: wei/git-sync@55c6b63b4f21607da0e9877ca9b4d11a29fc6d83 #v3.0.0 + with: + source_repo: "Medical-Informatics-Platform/platform-ui" + source_branch: "master" + destination_repo: "https://ghpusher:${{ secrets.EBRAINS_GITLAB_ACCESS_TOKEN }}@gitlab.ebrains.eu/hbp-mip/platform-ui.git" + destination_branch: "master" + - name: synctags + uses: wei/git-sync@55c6b63b4f21607da0e9877ca9b4d11a29fc6d83 #v3.0.0 + with: + source_repo: "Medical-Informatics-Platform/platform-ui" + source_branch: "refs/tags/*" + destination_repo: "https://ghpusher:${{ secrets.EBRAINS_GITLAB_ACCESS_TOKEN }}@gitlab.ebrains.eu/hbp-mip/platform-ui.git" + destination_branch: "refs/tags/*" diff --git a/.github/workflows/publish_images.yml b/.github/workflows/publish_images.yml new file mode 100644 index 000000000..458a6bbf5 --- /dev/null +++ b/.github/workflows/publish_images.yml @@ -0,0 +1,86 @@ +name: Publish images + +on: + release: + types: [published] + +env: + HARBOR_REGISTRY: docker-registry.ebrains.eu + DOCKER_BUILDKIT: 1 + DOCKER_CLIENT_TIMEOUT: 300 + COMPOSE_HTTP_TIMEOUT: 300 + RETRY_ATTEMPTS: "4" + RETRY_SLEEP: "10" + +jobs: + build_and_push_ui: + name: Build PLATFORM-UI image and push to registries + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 #v4.4.1 + + - name: Harbor connectivity probe + run: | + set -x + getent hosts ${{ env.HARBOR_REGISTRY }} || true + curl -vI --connect-timeout 10 https://${{ env.HARBOR_REGISTRY }}/v2/ || true + + - name: Disable IPv6 (job-scoped) + run: | + sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 + sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1 + + - name: Log in to Docker Hub + uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 #v4.7.0 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Log in to EBRAINS HARBOR (retry) + run: | + for i in $(seq 1 ${{ env.RETRY_ATTEMPTS }}); do + echo "${{ secrets.HARBOR_PASSWORD }}" | \ + docker login ${{ env.HARBOR_REGISTRY }} \ + -u "${{ secrets.HARBOR_USERNAME }}" --password-stdin && exit 0 + echo "Harbor login attempt $i failed; retrying in ${{ env.RETRY_SLEEP }}s..." + sleep ${{ env.RETRY_SLEEP }} + done + echo "Harbor login failed after ${{ env.RETRY_ATTEMPTS }} attempts" >&2 + exit 1 + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 #v6.2.0 + with: + images: | + hbpmip/platform-ui + ${{ env.HARBOR_REGISTRY }}/medical-informatics-platform/platform-ui + + - name: Load PLATFORM-UI service cached image + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 #v6.1.0 + with: + path: /tmp/.buildx-cache/platform-ui + key: ${{ runner.os }}-buildx-platform-ui-${{ hashFiles('Dockerfile', 'nginx.conf.template', 'package.json', 'package-lock.json', 'angular.json', 'src/**', 'public/**') }} + restore-keys: | + ${{ runner.os }}-buildx-platform-ui- + + - name: Build PLATFORM-UI docker image + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc #v7.4.0 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=local,src=/tmp/.buildx-cache/platform-ui + cache-to: type=local,dest=/tmp/.buildx-cache-new/platform-ui + + - name: Move Docker images cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache diff --git a/.github/workflows/publish_staging_images.yml b/.github/workflows/publish_staging_images.yml new file mode 100644 index 000000000..d6e415bef --- /dev/null +++ b/.github/workflows/publish_staging_images.yml @@ -0,0 +1,95 @@ +name: Publish staging images + +on: + push: + branches: [staging-b, staging-c] + workflow_dispatch: + +concurrency: staging-${{ github.ref_name }} + +permissions: + contents: read + +env: + HARBOR_REGISTRY: docker-registry.ebrains.eu + DOCKER_BUILDKIT: 1 + DOCKER_CLIENT_TIMEOUT: 300 + COMPOSE_HTTP_TIMEOUT: 300 + RETRY_ATTEMPTS: "4" + RETRY_SLEEP: "10" + +jobs: + build: + name: Build PLATFORM-UI staging image and push to Docker Hub + runs-on: ubuntu-latest + outputs: + tag: ${{ steps.tag.outputs.tag }} + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1 + + - name: Compute staging tag + id: tag + env: + REF_NAME: ${{ github.ref_name }} + run: | + short_sha=$(git rev-parse --short=7 HEAD) + echo "tag=${REF_NAME}-${short_sha}" >> "$GITHUB_OUTPUT" + echo "Staging image tag: ${REF_NAME}-${short_sha}" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 #v4.4.1 + + - name: Harbor connectivity probe + run: | + set -x + getent hosts ${{ env.HARBOR_REGISTRY }} || true + curl -vI --connect-timeout 10 https://${{ env.HARBOR_REGISTRY }}/v2/ || true + + - name: Disable IPv6 (job-scoped) + run: | + sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 + sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1 + + - name: Log in to Docker Hub + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f #v4.6.0 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Load PLATFORM-UI service cached image + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 #v6.1.0 + with: + path: /tmp/.buildx-cache/platform-ui + key: ${{ runner.os }}-buildx-platform-ui-${{ hashFiles('Dockerfile', 'nginx.conf.template', 'package.json', 'package-lock.json', 'angular.json', 'src/**', 'public/**') }} + restore-keys: | + ${{ runner.os }}-buildx-platform-ui- + + - name: Build PLATFORM-UI docker image + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc #v7.4.0 + with: + context: . + file: ./Dockerfile + push: true + tags: hbpmip/platform-ui:${{ steps.tag.outputs.tag }} + cache-from: type=local,src=/tmp/.buildx-cache/platform-ui + cache-to: type=local,dest=/tmp/.buildx-cache-new/platform-ui + + - name: Move Docker images cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache + + bump: + name: Bump staging image tag in mip + needs: build + uses: Medical-Informatics-Platform/mip/.github/workflows/bump-staging-image.yml@0eff4dd7667262adf39284c7585f2b8ee75dd034 # mip PR #33 (chore/bump-staging-workflow); switch to @master once merged + permissions: + contents: read + with: + branch: ${{ github.ref_name }} + key: platform-ui.image.tag + tag: ${{ needs.build.outputs.tag }} + secrets: + app-id: ${{ secrets.STAGING_BUMP_APP_ID }} + private-key: ${{ secrets.STAGING_BUMP_APP_PRIVATE_KEY }} diff --git a/.github/workflows/sast.yml b/.github/workflows/sast.yml new file mode 100644 index 000000000..0d5fc45e6 --- /dev/null +++ b/.github/workflows/sast.yml @@ -0,0 +1,79 @@ +name: Static Application Security Testing (SAST) + +on: + pull_request: + push: + branches: + - master + workflow_dispatch: + schedule: + - cron: '0 2 * * 1' # weekly scan, every Monday 02:00 UTC + +jobs: + sast: + name: Static Application Security Testing + runs-on: ubuntu-latest + permissions: + contents: read + security-events: write # required for uploading SAST results to github security + env: + SEMGREP_CONFIG_RULESETS: >- + semgrep-rules/generic semgrep-rules/problem-based-packs semgrep-rules/bash + semgrep-rules/javascript semgrep-rules/yaml semgrep-rules/package_managers p/default + semgrep-rules/json + OPENGREP_EXCLUDE: >- + *.sarif ci/ Dockerfile* dist/** build/** node_modules/** .angular/** + OPENGREP_SARIF_OUTPUT: sast-semgrep-app.sarif + + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Python + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + # renovate: datasource=python-version depName=python + python-version: '3.15.2' + + - name: Setup tools + run: bash ci/setup-tools.sh --install-tool opengrep,semgrep-rules + + - name: Run SAST scanning + run: python ci/sast_scan.py + + - name: Upload SARIF to GitHub Security tab + if: ${{ !cancelled() && hashFiles(env.OPENGREP_SARIF_OUTPUT) != '' }} + uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1 + with: + sarif_file: ${{ env.OPENGREP_SARIF_OUTPUT }} + category: semgrep-app + + - name: Encrypt SARIF report + id: encrypt + if: ${{ !cancelled() && hashFiles(env.OPENGREP_SARIF_OUTPUT) != '' }} + env: + PW: ${{ secrets.ARTIFACT_PASSWORD }} + OUT: ${{ env.OPENGREP_SARIF_OUTPUT }}.gpg + run: | + if [ -z "$PW" ]; then + echo "::warning title=SARIF report not uploaded::ARTIFACT_PASSWORD is not available in this run (Dependabot, Renovate or fork PR). Skipping encryption and upload." + rm -f -- "$OPENGREP_SARIF_OUTPUT" + exit 0 + fi + printf '%s' "$PW" | gpg --symmetric --batch --yes --quiet \ + --pinentry-mode loopback --passphrase-fd 0 \ + --no-symkey-cache \ + --cipher-algo AES256 \ + --s2k-mode 3 --s2k-digest-algo SHA512 --s2k-count 65011712 \ + --output "$OUT" -- "$OPENGREP_SARIF_OUTPUT" + rm -f -- "$OPENGREP_SARIF_OUTPUT" + echo "encrypted=$OUT" >> "$GITHUB_OUTPUT" + + - name: Upload encrypted SARIF artifact + if: ${{ !cancelled() && steps.encrypt.outputs.encrypted != '' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: sast-scan-sarif-report + path: ${{ steps.encrypt.outputs.encrypted }} + retention-days: 30 + if-no-files-found: error diff --git a/.github/workflows/sca.yml b/.github/workflows/sca.yml new file mode 100644 index 000000000..71c1f3a87 --- /dev/null +++ b/.github/workflows/sca.yml @@ -0,0 +1,89 @@ +name: Software Composition Analysis (SCA) + +on: + pull_request: + push: + branches: + - master + workflow_dispatch: + schedule: + - cron: '0 2 * * 1' # weekly SCA scan, every Monday 02:00 UTC + +jobs: + sca: + name: Software Composition Analysis + runs-on: ubuntu-latest + permissions: + contents: read + security-events: write # required for uploading SCA results to github security + env: + SBOM_PATH: target/bom.json + TRIVY_IGNOREFILE: ci/suppress_trivy.yaml + OSV_IGNOREFILE: ci/suppress_osv_scanner.toml + TRIVY_SARIF_OUTPUT: trivy-platform-ui.sarif + OSV_SARIF_OUTPUT: osv-scanner-platform-ui.sarif + SCA_MERGED_SARIF_OUTPUT: SCA-platform-ui-merged.sarif + # CVSS gate: fail at >= FAIL, warn in between. Override per project. + GATE_FAIL_THRESHOLD: "8.0" + GATE_WARN_THRESHOLD: "5.0" + + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Python + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + # renovate: datasource=python-version depName=python + python-version: '3.15.2' + + - name: Cache npm packages + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.npm + key: ${{ runner.os }}-npm-v1-${{ hashFiles('**/package-lock.json') }} + restore-keys: ${{ runner.os }}-npm-v1- + + - name: Setup tools and generate SBOM + run: bash ci/setup-tools.sh --install-tool trivy,osv-scanner --sbom-ecosystem npm + + + - name: Run SCA tools + run: python ci/sca_scan.py + + - name: Upload merged SARIF to GitHub Security tab + if: ${{ !cancelled() && hashFiles(env.SCA_MERGED_SARIF_OUTPUT) != '' }} + uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1 + with: + sarif_file: ${{ env.SCA_MERGED_SARIF_OUTPUT }} + category: sca-app + + - name: Encrypt SARIF report + id: encrypt + if: ${{ !cancelled() && hashFiles(env.SCA_MERGED_SARIF_OUTPUT) != '' }} + env: + PW: ${{ secrets.ARTIFACT_PASSWORD }} + OUT: ${{ env.SCA_MERGED_SARIF_OUTPUT }}.gpg + run: | + if [ -z "$PW" ]; then + echo "::warning title=SARIF report not uploaded::ARTIFACT_PASSWORD is not available in this run (Dependabot, Renovate or fork PR). Skipping encryption and upload." + rm -f -- "$SCA_MERGED_SARIF_OUTPUT" + exit 0 + fi + printf '%s' "$PW" | gpg --symmetric --batch --yes --quiet \ + --pinentry-mode loopback --passphrase-fd 0 \ + --no-symkey-cache \ + --cipher-algo AES256 \ + --s2k-mode 3 --s2k-digest-algo SHA512 --s2k-count 65011712 \ + --output "$OUT" -- "$SCA_MERGED_SARIF_OUTPUT" + rm -f -- "$SCA_MERGED_SARIF_OUTPUT" + echo "encrypted=$OUT" >> "$GITHUB_OUTPUT" + + - name: Upload encrypted SARIF artifact + if: ${{ !cancelled() && steps.encrypt.outputs.encrypted != '' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: sca-scan-sarif-report + path: ${{ steps.encrypt.outputs.encrypted }} + retention-days: 30 + if-no-files-found: error diff --git a/.github/workflows/secrets-scan.yml b/.github/workflows/secrets-scan.yml new file mode 100644 index 000000000..db60fa02e --- /dev/null +++ b/.github/workflows/secrets-scan.yml @@ -0,0 +1,40 @@ +name: Secret Scanning (gitleaks) + +on: + pull_request: + push: + branches: + - master + workflow_dispatch: + +permissions: + contents: read + security-events: write + +jobs: + gitleaks: + runs-on: ubuntu-latest + env: + GITLEAKS_SARIF_OUTPUT: gitleaks.sarif + + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Install gitleaks + run: bash ci/setup-tools.sh --install-tool gitleaks + + - name: Install and run gitleaks + run: > + gitleaks dir . + --config ci/suppress_gitleaks.toml + --redact + --report-format sarif + --report-path "$GITLEAKS_SARIF_OUTPUT" + + - name: Upload SARIF to code scanning + if: always() + uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1 + with: + sarif_file: ${{ env.GITLEAKS_SARIF_OUTPUT }} + category: gitleaks diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 000000000..b9ecbcfd5 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,34 @@ +name: Unit tests + +# Vitest specs through `ng test`, in headless Chromium (Vitest browser mode with +# Playwright). No backend and no secrets. +on: + pull_request: + workflow_dispatch: + +jobs: + unit: + name: Vitest unit tests + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Node + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version-file: .nvmrc + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Install Playwright Chromium + run: npx playwright install --with-deps chromium + + - name: Unit tests + run: npm test -- --watch=false + env: + CI: true diff --git a/.gitignore b/.gitignore index 20db50288..cd632b79e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,6 @@ # Node /node_modules -/package-lock.json npm-debug.log yarn-error.log @@ -35,6 +34,7 @@ yarn-error.log # Miscellaneous /.angular/cache +.codegraph/ .sass-cache/ /connect.lock /coverage @@ -49,6 +49,8 @@ Thumbs.db # Compiled source *.js *.js.map +!src/assets/env.js +!src/assets/env.template.js # TypeScript type definition files *.d.ts @@ -64,3 +66,24 @@ yarn-error.log* # Temporary files *.tmp *.swp + +# Local agent scratch (review reports, taste notes) +.commandcode/ + +# Local browser-automation scratch (screenshots, page dumps, review captures) +.playwright-cli/ +output/ +experiment-studio-review/ + +# Local agent tooling (Impeccable skill, hook manifest, detector scratch) +.agents/ +.codex/ +.impeccable/ + +# Playwright experiment-studio flow output +/e2e/test-results/ +/playwright-report/ +/test-results/ + +# Vitest browser-mode attachments (screenshots of failing tests) +/.vitest/ diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 000000000..2bd5a0a98 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +22 diff --git a/.pi/skills/ui-review/SKILL.md b/.pi/skills/ui-review/SKILL.md new file mode 100644 index 000000000..58b76eff8 --- /dev/null +++ b/.pi/skills/ui-review/SKILL.md @@ -0,0 +1,32 @@ +--- +name: ui-review +description: After MIP frontend changes, render the affected page with CDP, read the PNG so the VL model sees pixels, compare to adjacent UI, and fix homogeneity gaps. Use when CSS, templates, or visual layout changed. Do not use Cursor browser MCP, Playwright, qwen_eye, or vision-proxy. +--- + +# UI review (pi) + +After any frontend change. Do not stop after the first implementation. + +Follow the global `frontend-visual` skill (`~/.pi/agent/skills/frontend-visual/SKILL.md`) for capture. MIP extras below. + +## Capture + +1. App at `http://localhost:4200` (`npm start` if down). Do not start Playwright. +2. `/home/kfilippopolitis/.agents/skills/computer-use/scripts/launch_cdp_chrome.sh` +3. `python3 /home/kfilippopolitis/.agents/skills/computer-use/scripts/cdp_browser.py screenshot /tmp/ui.png` +4. **`read` `/tmp/ui.png`** +5. `eval` / `text` for strings, computed colors, rects. + +Reuse authenticated `:9222` (Keycloak). Fresh Chrome hits login — stop, do not type passwords. If studio data is missing, say so; a `file://` CSS harness is only valid for chrome, not live studio state. + +Do not use Cursor `browser_*`, Playwright, Puppeteer, `qwen_eye.sh`, grim, or `:8001`. + +## Check + +typography, font weights/sizes, line heights, spacing rhythm, component density, colors, borders, radius, shadows, icon style, button/input treatment, hover/focus, empty/loading/error, responsive. + +Ask: could a user believe this was designed by the same team as the surrounding interface? + +If no: fix the 3 biggest inconsistencies, render again, repeat. + +Use [DESIGN.md](../../../DESIGN.md) and `src/styles.css` `:root`. Tokens over new hex. Do not restyle unrelated surfaces. Sibling panels: variables, algorithm, statistic analysis. diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..3aae003f9 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,24 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + exclude: .bumpversion.cfg + - id: check-merge-conflict + exclude: LICENSE + - id: check-added-large-files + - id: check-yaml + - id: check-json + - id: pretty-format-json + args: ["--autofix"] + exclude: slack.json + - id: check-xml + - id: detect-private-key + - id: check-case-conflict + - id: mixed-line-ending + +- repo: https://github.com/gitleaks/gitleaks + rev: v8.30.1 + hooks: + - id: gitleaks diff --git a/AGENTS.md b/AGENTS.md index f4843b929..3e969c33b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,96 +1,167 @@ -## Quick Init for Agents - -Use this as a fast orientation to the codebase. - -- **Project**: Angular **21** standalone app (`fl-platform`) serving as the Platform UI for the MIP (Medical Informatics Platform), for composing/running experiments, configuring algorithms, integrating JupyterHub, and reviewing results. Entry: `src/main.ts`, root component: `src/app/app.component.ts`. -- **Routing** (`src/app/app.routes.ts`): - - Main: `/experiments-dashboard`, `/experiment-studio`, `/account`, `/terms` - - Redirects: `''` and `/home` -> `/experiments-dashboard` - - Wildcard: `**` -> `/experiments-dashboard` - - Guards: most routes use `AuthGuard` + `TermsGuard`; `/terms` uses `AuthGuard` only. -- **Auth + session flow** (`src/app/services/auth.service.ts`): - - Session check: `GET /services/activeUser` - - Login redirect: `/services/oauth2/authorization/keycloak?frontend_redirect=...` - - Logout: hard redirect to `/services/logout` - - Redirect handling stored in localStorage (`redirect_url`) - - HTTP interceptor (`auth.interceptor.ts`) adds `withCredentials` for same-origin API calls. -- **Terms/NDA flow**: - - `TermsGuard` checks `user.agreeNDA` and routes to `/terms` if needed. - - `TermsService` stores intended destination in localStorage (`tos_redirect_url`). - - Terms page loads `assets/tos.md` and posts `POST /services/activeUser/agreeNDA`. -- **HTTP config** (`src/app/app.config.ts`): - - `provideHttpClient` with credentials interceptor and XSRF header/cookie config. - - Router uses in-memory scrolling restoration. - - ECharts is provided lazily with `provideEchartsCore`. -- **Backend endpoints** (dev proxy in `src/proxy.conf.json` -> `http://localhost:8080`): - - Auth/user: `GET /services/activeUser`, `POST /services/activeUser/agreeNDA` - - Catalogs: `GET /services/data-models`, `GET /services/algorithms` - - Experiments: - - `GET /services/experiments` (paged list) - - `GET /services/experiments/:id` (metadata/result fetch) - - `POST /services/experiments` (run async + poll by UUID) - - `POST /services/experiments/transient` (quick previews: histograms/descriptive stats) - - `PATCH /services/experiments/:id` (name/shared updates) - - `DELETE /services/experiments/:id` -- **Feature pages**: - - **Experiment Studio** (`src/app/pages/experiment-studio/...`): data model + dataset selection, variable/covariate/filter selection, QueryBuilder filter logic, algorithm configuration, run/edit flows, stats/distribution previews, results rendering (ECharts + D3), PDF export. - - **Experiments Dashboard** (`src/app/pages/experiments-dashboard/...`): list/search/pagination, mine/shared toggles, share link creation, detail view, compare mode, delete/edit/name updates, result export. - - **Terms page** (`src/app/pages/terms-page/...`): markdown-to-HTML rendering and NDA acceptance gating. - - **Account page** (`src/app/pages/account-page/...`): user info/logout view. -- **State patterns**: - - Strong use of Angular Signals across features. - - `ExperimentStudioService` is the main orchestration layer (selection state, algorithm availability, configurations, run/polling, hydration for edit mode, transient calls, sessionStorage persistence). - - `ExperimentsDashboardService` owns paged experiment list signals. - - `ErrorService` shares global feature errors via `BehaviorSubject`. -- **Algorithm availability rules**: - - Encoded in `src/app/services/algorithm-rules.service.ts`. - - Includes special cases for ANOVA variants, one-sample t-test, PCA/transformation variants, role constraints, and filter requirements. -- **Visualization and output mapping**: - - Algorithm/result schema mapping: `src/app/core/algorithm-mappers.ts` - - Chart registry/builders: `src/app/pages/experiment-studio/visualisations/charts/...` - - Enum/label helpers: `src/app/services/experiment-label.service.ts`, `src/app/core/algorithm-result-enum-mapper.ts` -- **PDF exports**: - - `src/app/services/export-results-pdf.service.ts`: experiment results report export. - - `src/app/services/pdf-export.service.ts`: distribution/descriptive statistics export. -- **Theming and branding**: - - `ThemeService` toggles body theme classes (`theme-light`/`theme-dark`) and persists preference. - - Footer surfaces frontend/backend/exaflow versions from `window.__env` (`src/assets/env.js` or template at container startup). -- **Build/Test commands**: - - `npm start` (dev server + proxy) - - `npm run build` (Angular build) - - `npm run watch` (dev build watch) - - `npm test` (Karma) -- **Container**: - - `Dockerfile` builds with Node 20, serves with nginx. - - Runtime `envsubst` injects: - - proxy envs: `PLATFORM_BACKEND_SERVER`, `PLATFORM_BACKEND_CONTEXT` - - version envs: `FRONTEND_VERSION`, `BACKEND_VERSION`, `EXAFLOW_VERSION` - -## Folder Map - -- `/src/app`: main app code. - - `app.component.*`, `app.routes.ts`, `app.config.ts`: shell/bootstrap wiring. - - `guards/`: `auth.guard.ts`, `terms.guard.ts`. - - `services/`: - - auth/session/errors: `auth.service.ts`, `auth.interceptor.ts`, `terms.service.ts`, `session-storage.service.ts`, `error.service.ts`, `theme.service.ts` - - experiment orchestration: `experiment-studio.service.ts`, `experiments-dashboard.service.ts`, `algorithm-rules.service.ts`, `experiment-label.service.ts` - - exports: `pdf-export.service.ts`, `export-results-pdf.service.ts` - - `models/`: frontend/backend DTOs and interfaces (user, algorithms, experiments, filters, data-models). - - `core/`: algorithm mapping and constants. - - `pages/experiment-studio/`: - - `variables-panel/`: model/dataset selection, search, variable actions, filters UI, distribution graph. - - `algorithm-panel/`: algorithm selection/configuration, run/save-as, result rendering. - - `statistic-analysis-panel/`: descriptive stats/model tables/charts. - - `visualisations/`: chart registry/builders, histogram, auto-renderer, bubble chart. - - `pages/experiments-dashboard/`: list/search/detail/compare and mapper utilities. - - `pages/terms-page/`: NDA/TOS gate. - - `pages/account-page/`: account/profile. - - `pages/shared/`: header/footer/navbar/spinner and shared form-control factory. -- `/src/assets`: logos/icons/TOS markdown + runtime env files. -- `/public`: static files copied to build output. -- `/src/styles.css`: global styling + QueryBuilder theming. -- `/src/proxy.conf.json`: dev proxy `/services` -> backend. -- `/Dockerfile`, `/nginx.conf.template`: container build/serve config. - -- Build once you make changes to be sure you did not break something. +# Agent Instructions +## Project Overview +This repository contains `fl-platform`, the Angular 22 standalone frontend for the Medical Informatics Platform (MIP). The app lets authenticated users compose and run experiments, configure algorithms, integrate with JupyterHub when enabled, and review/export experiment results from a backend exposed under `/services`. + +## Repository Layout +- `src/main.ts`: Angular bootstrap entrypoint. +- `src/app/app.component.*`, `src/app/app.routes.ts`, `src/app/app.config.ts`: app shell, routing, providers, HTTP, XSRF, zoneless change detection, and ECharts setup. +- `src/app/guards/`: route guards for authentication, terms/NDA acceptance, and studio guide onboarding. +- `src/app/services/`: auth/session, experiment orchestration, dashboard data access, algorithm rules, labeling, runtime env, theme, errors, and PDF/CSV exports. +- `src/app/models/`: frontend and backend DTOs/interfaces for users, algorithms, experiments, filters, and data models. +- `src/app/core/`: algorithm mapping, result enum mapping, constants, and result utility logic. +- `src/app/pages/experiment-studio/`: step views switched via a sticky horizontal stepper — `experiment-studio.component` shows one step view at a time (Datasets & Variables · Data review & preprocessing · Algorithm); all views stay mounted (hidden via CSS) so state is preserved. On the Data review step the stepper expands a sub-step row (Filtering, Raw Summary, Preprocessing, Processed Summary, Transformation) that drives the `statistic-analysis-panel` sections (`goToSection`). In-panel section targets use `scroll-margin-top` so `scrollIntoView` clears sticky chrome. Also contains variable/filter selection, QueryBuilder filter UI, algorithm configuration with variable role assignment (outcome y / covariates x on the algorithm panel), run/edit flows, statistics, visualizations, and result rendering. +- `src/app/pages/experiments-dashboard/`: experiment list/search/pagination, detail view, compare mode, sharing, delete/edit/name updates, and result export. +- `src/app/pages/terms-page/`: NDA/TOS display and acceptance flow. +- `src/app/pages/account-page/`: account/profile view and logout entry. +- `src/app/pages/notebook/`: optional notebook route gated by runtime env. +- `src/app/pages/shared/`: header, footer, spinner, and shared form-control utilities. +- `src/assets/`: runtime `env.js`, logos/icons, footer assets, and terms markdown. +- `public/`: static files copied to the Angular build output. +- `src/styles.css`: global styling and QueryBuilder theming. +- `DESIGN.md`: MIP visual/brand guidance and app tokens; consult before UI styling changes. +- `Dockerfile`, `docker-entrypoint.sh`, `nginx.conf.template`: container build and nginx/runtime environment injection. +- `.github/workflows/`: image publishing and EBRAINS mirror workflows. +- `docs/`: the durable context system under `docs/context/`, plus `docs/llm-wiki/`. + +## Stack +- Language/runtime: TypeScript, HTML templates, CSS, Node 22+, npm 10+. +- Framework: Angular 22 standalone application, Angular Router, Angular Material/CDK, Angular Signals, zoneless change detection. +- Data/async: Angular `HttpClient`, RxJS, browser localStorage/sessionStorage. +- Visualization/export: ECharts via `ngx-echarts`, D3, html2canvas, jsPDF, jsPDF AutoTable. +- Test framework: Vitest through the Angular CLI `unit-test` builder, in headless Chromium (Vitest browser mode with Playwright); Playwright for e2e flows in `e2e/`. +- Package manager: npm with `package-lock.json`; use `npm ci` for clean installs. +- Container/runtime: Docker multi-stage Node 22 build served by nginx alpine; runtime config injected into `assets/env.js`. + +## Setup Commands +```bash +npm ci +``` + +Requirements: +- Node 22+ and npm 10+. +- Backend reachable at `http://localhost:8080/services` for local development unless `src/proxy.conf.json` is changed. +- Keycloak/OAuth2 endpoints available through the backend proxy for authenticated flows. + +## Development Commands +```bash +npm start +``` +Runs `ng serve` with `src/proxy.conf.json` from `angular.json`. The app is served at `http://localhost:4200/`. + +```bash +npm run watch +``` +Runs a development build in watch mode. + +## Build Commands +```bash +npm run build +``` +Runs the production Angular build. Output is `dist/fl-platform`. + +Docker: +```bash +docker build -t fl-platform . +docker run -e PLATFORM_BACKEND_SERVER=platform-backend-service:8080 -e PLATFORM_BACKEND_CONTEXT=services -p 80:80 fl-platform +``` + +## Test Commands +```bash +npm test +``` +Runs the Vitest unit tests through Angular CLI in headless Chromium. Use `npm test -- --watch=false` for a single run, and `--include ` to focus. Chromium comes from `npx playwright install chromium`, or set `CHROME_BIN` to an installed Chrome. + +```bash +npm run e2e +``` +Runs the Playwright flows in `e2e/` against a mocked `/services` API (starts `ng serve` itself; a few minutes). See `docs/context/testing.md` (E2E Tests). + +Manual browser QA is required for authenticated experiment workflows and backend-dependent chart/result rendering. The checklist lives in `docs/context/testing.md` (Frontend Manual QA). + +## Lint / Format / Typecheck +No ESLint/Prettier setup; `npm run build` remains the template/type check that matters. Static checks that need no browser: + +```bash +npm run verify # typecheck + dead-code scan + build +npm test # Vitest in headless Chromium (see docs/context/testing.md) +``` + +`scripts/check-dead-code.mjs` is a plain Node script, so a finding is a bug in the code or in its allowlist (`RUNTIME_CLASS_PREFIXES`) — never a reason to delete a runtime class Angular applies. Its header explains how emulated encapsulation, `@import` and `@keyframes` scoping are decided; the individual commands are listed in `docs/context/testing.md`. + +`.nvmrc` and `scripts/check-node-version.mjs` both require Node 22; `nvm use` must land on 22 or every npm script fails its pre-hook. + +## Output and Runtime Budget Guardrails +Agents must not run high-output or long-running commands on their own unless the user explicitly asked for that validation, or the agent first states the expected runtime/token cost and gets confirmation. Prefer targeted, bounded commands. + +Require explicit confirmation before running: +- Long-running app/watch commands: `npm start`, `npm run watch`, `ng serve`, `ng build --watch`, persistent browser automation, or any command expected to keep running. +- Full interactive test/watch commands: `npm test`, `ng test`, or Vitest watch mode. Prefer a non-watch/focused test command when available; if not verified, ask first. +- Container-heavy commands: `docker build`, `docker run`, `docker compose ...`, and unbounded `docker logs`. +- Dependency/network audits: `npm ci`, `npm install`, `npm audit`, `npm audit --json`, `npm outdated`, unless dependency setup/update is the explicit task. +- Broad output commands: `find .`, `ls -R`, `tree`, recursive `grep`, unrestricted `rg`, `cat package-lock.json`, `cat dist/*`, `cat coverage/*`, `git diff` without path/stat limits, `git log -p`, or `git show` on large commits. + +Allowed without confirmation when relevant: +- Targeted file reads with line limits, such as `sed -n` on specific files. +- Targeted searches with exclusions, such as `rg "pattern" src docs -g "!node_modules" -g "!dist" -g "!coverage"`. +- Summary commands such as `git status --short`, `git diff --stat`, `git diff --name-only`, and path-limited diffs. +- `npm run build` when required by repo instructions or when validating code changes, because it is bounded and currently moderate-output. + +When a high-output command is justified, announce why it is needed, say it may consume substantial tokens/runtime, and cap output with `--tail`, path filters, `--stat`, `--name-only`, or explicit line ranges wherever possible. + +## Architecture Rules +- Keep feature UI under the owning page directory in `src/app/pages/...`. +- Keep cross-feature orchestration and backend calls in Angular services under `src/app/services/`. +- Keep backend DTOs and frontend-facing interfaces in `src/app/models/`; map backend shapes before rendering when needed. +- Keep algorithm/result schema mapping in `src/app/core/algorithm-mappers.ts` and result enum label logic in `src/app/core/algorithm-result-enum-mapper.ts`. +- Keep chart and table rendering logic in the existing visualization registries under `src/app/pages/experiment-studio/visualisations/`. +- Keep route protection in guards; do not duplicate auth or NDA checks inside unrelated components. +- Read runtime configuration through the existing runtime env pattern (`window.__env` and `RuntimeEnvService`) rather than scattered direct environment reads. +- Use the `/services` same-origin API boundary; the credentials interceptor adds `withCredentials` to same-origin API calls. +- Preserve the session/local storage keys used for auth redirects, terms redirects, and experiment studio state unless a migration is explicitly planned. + +## Coding Conventions +- Prefer standalone Angular components and `imports` arrays over NgModules. +- Prefer Angular Signals for component and feature state where the surrounding code already uses Signals. +- Use `inject()` consistently with nearby services/components. +- Keep TypeScript strictness intact; `tsconfig.json` enables strict templates, no unused locals/parameters, no implicit returns, and related checks. +- Use CSS component styles (`styleLanguage: css`) and global styles only for app-wide concerns. +- Follow `DESIGN.md` for UI aesthetics, brand colors, logo usage, typography, spacing, and visual hierarchy. +- Keep backend API paths relative (`/services/...`) so proxy/nginx routing continues to work. +- Handle backend errors explicitly through local state or `ErrorService`; avoid hiding failures. +- Use existing mapper, label, and registry helpers before adding new presentation logic. +- Add or update Vitest specs near the changed code when behavior changes. + +## Forbidden Patterns +- Do not silently swallow exceptions or failed HTTP calls. +- Do not introduce global mutable state outside established Angular services or runtime env bootstrapping. +- Do not read or write runtime environment values outside the existing config/runtime env layer without a documented reason. +- Do not bypass `AuthGuard`, `TermsGuard`, or the credentials interceptor for authenticated flows. +- Do not change `/services` API contracts, experiment payload shapes, or algorithm result mappings without documenting compatibility impact. +- Do not remove legacy algorithm aliases unless stored historical experiment compatibility has been reviewed. +- Do not introduce new dependencies without explaining why the existing stack is insufficient. +- Do not reformat unrelated files or rewrite unrelated feature structure. +- Do not modify Docker, nginx, CI, auth, sharing, deletion, or runtime env behavior without focused validation and human review. + +## Security and Privacy Rules +- Never commit secrets, tokens, cookies, credentials, or private environment values. +- Never print secrets or sensitive experiment/user data in logs. +- Treat authentication, authorization, terms/NDA gating, experiment sharing, experiment deletion, Docker/CI secrets, runtime proxying, and notebook access as human-review areas. +- Preserve same-origin credential and XSRF behavior unless the backend contract is being intentionally changed. +- Validate redirect, share, delete, and notebook behavior carefully because they affect access boundaries. + +## PR Expectations +Every agent change should include: +- Summary of behavior/documentation changed. +- Files changed. +- Tests/checks run and results. +- Risk assessment, including auth/API/data/deployment impact if relevant. +- Rollback notes when changes affect runtime behavior, deployment, migrations, data deletion, or public contracts. + +## Definition of Done +- The diff is minimal and directly tied to the request. +- Relevant files and existing patterns were read before editing. +- Public API, route, runtime env, and backend contract changes are documented when made. +- Relevant unit tests, build, or manual QA steps passed, or skipped checks are explicitly reported with reasons. +- No unrelated user work, generated artifacts, lockfiles, or formatting churn were introduced. +- For UI changes, `DESIGN.md` was consulted and responsive/authenticated flows were considered. diff --git a/DESIGN.md b/DESIGN.md new file mode 100644 index 000000000..408b7acf2 --- /dev/null +++ b/DESIGN.md @@ -0,0 +1,120 @@ +# MIP visual language + +Source: MIP Charte graphique (Donovan Studio / CHUV, 2023-10-02) plus the tokens implemented in `src/styles.css` `:root`. + +**If charter and code disagree, live CSS tokens win** for app chrome (neutrals, danger/success, studio cards). Charter still wins for logo geometry and approved logo colorways. + +Do not invent a new palette, display serif, or animation library. Reuse CSS variables. Match adjacent Experiment Studio panels (variables, algorithm, statistic analysis). + +## Brand + +- Name: MIP (Medical Informatics Platform) +- Logo type: Mark Pro Bold only (do not substitute, redraw, stretch, skew, rotate, outline, or rearrange) +- Body/UI type: Alaska Regular, then Inter (`--font-sans`: `'Alaska Regular', 'Alaska', 'Inter', system-ui, …`). Alaska is licensed and not shipped; Inter (400/500/600/700, Google Fonts via `index.html`) is what users actually see +- Tabular numbers on data UI (`font-variant-numeric: tabular-nums`) +- Line height ~1.6, letter-spacing ~0.01–0.015em + +### Logo variants + +| Variant | Use | Min size | +|---|---|---| +| symbol_only | avatar, favicon, icon | 30px / 10mm | +| primary (symbol + “mip”) | web, email, stationery | 50px | +| secondary + baseline | formal comms, signage | 90px | + +Keep the protection zone empty. Uncertain: primary for web, symbol-only for icons, secondary for formal, black-on-white for print. + +### Approved logo colorways + +white on dark blue; blue on light blue; black on white; blue on orange; blue on green; white on black. Reject other logo/background pairings. + +## App tokens (`src/styles.css`) + +| Token | Value | +|---|---| +| `--primary-color` | `#2b33e9` | +| `--primary-dark` | `#1b21a3` | +| `--primary-light` | `#7f9ce8` | +| `--accent-color` | `#ffba08` | +| `--bg-color` | `#f3f8ff` | +| `--bg-neutral` | `#f8fafc` | +| `--card-bg` / `--studio-card-bg` | `#ffffff` | +| `--text-main` | `#0f172a` | +| `--text-muted` | `#475569` | +| `--danger` | `#ef4444` | +| `--success` | `#10b981` | +| `--border-color` / `--studio-card-border` | `rgba(43, 51, 233, 0.1)` | +| `--studio-card-shadow` | `0 1px 2px rgba(43, 51, 233, 0.06)` | +| `--studio-card-header-bg` | `#f8fbff` | +| `--radius-sm` / `--radius-md` / `--radius-lg` | 4px / 8px / 12px | +| `--header-height` | 64px (56px ≤768px) | +| `--header-surface` / `--header-border` / `--header-shadow` | `--card-bg` / primary 14% (= `rgba(43, 51, 233, 0.14)`) / two-layer primary tint | +| `--tint-nav-hover` | `primary-light` 20% over white (= `#e5ebfa`) | +| `--ring-avatar` / `--avatar-bg` | `primary-light` 40% over white (= `#ccd7f6`) / `text-main` 30% over white (= `#b7b9bf`) | +| `--chrome-inline-padding` | `24px` (floor inset, not a measure — see the chrome contract) | +| `--font-mono` | `ui-monospace, SFMono-Regular, Menlo, monospace` — the only mono slot. Components use `font-family: var(--font-mono)`; no component writes its own mono stack (four of them used to, so one kind of cell rendered in three faces) | +| `--letter-spacing-config` | `0.015em` | + +**Text on a state fill.** `--danger` and `--success` are fill/edge colours, not text colours (white on them is 3.76:1 and 2.54:1): reach for the dark inks `#991b1b` / `#065f46` / `#78350f` as values, not as tokens — no surface puts text on a state fill today. The status chip's pending ink is `#92400e`. + +### Top chrome contract + +The header (`.header`, z 10000) and the studio sub-header (`.studio-sub-header`, z 9990, `top: var(--header-height)`) are two rows of one bar: same surface, same bottom border, no `backdrop-filter`, both indented by `--chrome-inline-padding` — one flat inset on every route, so no page can show a bar that starts somewhere else. `--chrome-inline-padding` is a **floor inset, never a measure**: a row that sits above a centred column caps its own content rather than re-indenting the bar. That is `.sub-header-inner` — the sub-header's surface stays full-bleed, but its stepper and CTA are capped and centred on `--studio-inner-max`, so past the cap the CTA ends on the panels' right border and the stepper starts on their left border, while row 1 keeps the flat inset that every route shows. + +**The bar is identical on every route.** Three slots — brand, navigation, account — with the same items in the same order everywhere, and **no current-page highlight**: the bar draws hover and the notebook's first-visit edge and nothing else, `aria-current="page"` carries the location for assistive tech, and no CSS in the repo styles it. Which page you are on is never carried by an item appearing, disappearing, or the bar re-indenting. Env-gated and auth-gated items may hide; they may not re-shape the bar, move the nav into another slot, or change the inset. `header.component.spec.ts` asserts this as one signature per route (height, inset, surface, logo, nav labels) plus the flat resting style of the current item. + +Two hairlines split the bar, both 22px in `--header-border` and both drawn by a pseudo-element tied to the content it divides: `.header-nav-slot::before` ends the brand, `.header-actions::before` starts "you". A divider is never a border on a control: on the pill recipe (`border-radius: 999px`) a `border-inline-start` follows the curve and draws a sliver, and on the `auto` grid track it draws mid-bar at tablet widths because an `auto` track absorbs leftover space. + +The Guide control lives in the bar: the page that owns a tour registers `{ label, start }` with `GuideLauncherService` and the bar draws it under `[data-guide="launcher"]`, the selector the studio tour spotlights. A guide must not float a fixed circle over the bar — the previous one (`right: 84px`, z 10001) sat exactly where the account slot's width lands, and its spotlight ring drew under the bar anyway (overlay z 1502 vs 10000). + +`--header-shadow` is a scroll state, not a resting one: `.header` keeps only its hairline at `scrollTop: 0` and gains the shadow through `.header--scrolled` once content has moved under the bar. The bar is also a stacking context (z 10000) above the CDK overlay container (z 1000), so anything the header opens — the account menu — is positioned inside the bar's own box, never in an overlay that would open beneath it. + +The bar's height **is** `--header-height`. Never set a height in `header.component.css`; change the token inside a media query in `styles.css` instead. Pages, the sub-header, the pathology banner, the guide spotlight and the scroll util all derive their offsets from that token — a component-local height silently opens a gap of exposed content under the fixed bar. + +Both rows own their responsive rules. `styles.css` has no global header overrides (the old `display: none !important` block hid the live `.user-icon` on phones). + +Charter brand hex (same blues/orange; extra green `#DFEFE4` for identity, not a default page fill). Semantic: `--covariate-color` `#bba66f`, `--filter-color` `#483300`, `--variable-color` `#ffba08`. Only `--filter-color` (12:1 on white) is safe as text. `--variable-color` (1.71:1) and `--covariate-color` (2.39:1) are under the 3:1 a marker or icon needs, so a role colour always sits next to its text label and never carries the role alone. `--glass-*` aliases solid card tokens (`--glass-blur: none`); do not reintroduce frost. + +Chrome is Angular Material plus these tokens. Component CSS for feature layout; global styles only for app-wide concerns. + +## Density and surfaces + +Experiment Studio is a dense product workspace, not a marketing site. No landing heroes, no new type pairing, no GSAP/Lenis/custom cursors. Cards only when grouping is real. Use `--studio-card-*` for studio panels. + +### Folder chips and the folder canvas + +- The chip strip is the `.experiments-tabs` / `.tab-btn` segmented recipe re-used, not a new chip: the tray carries the surface and the ring, the chips sit transparent inside it, hover is `rgba(148, 163, 184, 0.1)`, selected is the same white pill in `--primary-color` with `--shadow-sm`. The ghost "+ New" chip is the resting chip with a plus. +- Selection on a chip is primary-blue because that is the idiom already within 40px of it (the active tab, the selected experiment row). `--accent-color` marks nothing selected on this page; an orange third reading of "chosen" fails the adjacent-panel test. +- Chips carry no layered edge. A 2px layer is noise at chip height, so the one memorable element sits on the folder canvas instead: its header card wears a single slab behind it, the set read as a stack of runs. The canvas' two other gestures are numbered member rows and an "N algorithms · M domains" line. +- `fa-object-group` is an analysis set. `fa-layer-group` already means domain/data-model in the same pane and is the compare placeholder, so it stays away from folders. +- `.count-badge` is the one count recipe — compare button, chips, row menu, canvas header — with tabular numbers. +- Dragging a row onto a folder is a third way in, beside the row menu and the chip strip; the menu stays the keyboard path, so the drag may stay an affordance. The canvas rings its member stack and a chip rings itself, both with an outline or shadow ring rather than a border: a real border reflows the strip, and a box past the canvas edge clips in a scrolling pane. +- A drop adds; it never toggles. Re-dropping a member is a misaim, not a request to remove it, so the canvas says "Already in …" in its header pill instead of quietly lowering the count, and the row it just added breathes primary once. + +### Compare columns + +- Compare is columns side by side, one per run, read left to right in the order the grouping defines: folder-set runs first, then one group per algorithm label for the runs nobody grouped. A column is a header — number, name, algorithm, status pill, date, plus a set-name pill when a folder set claimed the run — then the collapsed configuration, then the result rendered in place. +- Columns are `minmax(360px, 1fr)`: two or three fill the width evenly, past that the strip scrolls sideways instead of squeezing every result into a sliver. Headers are `position: sticky` inside the strip, so names stay visible while a tall result scrolls; a wide table scrolls sideways inside its column and nothing scales down. +- Numbering runs once across the whole comparison, so "run 7" is one column whichever group it landed in; the set's name rides the header as a tag instead of a section heading. +- ≤900px the strip stacks one column per run — side-by-side is unusable at that width. + +### Component recipes + +- Buttons: `.btn-create` is the one primary action per view (`--primary-color`, white 0.95rem/500, `--radius-md`, 10px 20px, hover `--primary-dark`); `.btn-cancel` sits beside it (`--border-color` fill, `--text-muted`, hover `#cbd5e1`). +- Status: `.status-chip` is an uppercase 0.68rem/700 pill, 0.06em tracking, tinted fill + dark ink + faint edge per state (`.completed` `#065f46`, `.pending` `#92400e`, `.error` `#991b1b`, neutral `#475569`). `.status-chip--compact` for dense rows; `.status-dot` is the same three tones at 8px when the word does not fit, with a `title` or visible text nearby. +- Studio panel: `.statistics-workspace` is one card with two panes — a 240–300px rail on `--studio-card-header-bg` with a right hairline, and the content at 20px padding — capped at `min(72vh, 760px)`. Never two separate boxes side by side. + +### Charts + +- Series in order: `#2b33e9`, `#7f9ce8`, `#ffba08`, `#e34948`, `#4a3aa7`, `#e87ba4`, `#008300`, `#1baf7a` — one list in `visualisations/charts/chart-palette.ts`, used by `charts-renderer.component.ts` and `grouped-bar-chart.ts`. Slots 1–3 are the charter; 4–8 were chosen with the dataviz palette validator so every adjacent pair clears ΔE 15 for both deutan/protan and full colour vision. Assign in order, never cycle: a ninth series folds into "Other" or a table. Axis lines and labels `#475569`, split lines `#e2e8f0`, data labels `--chart-label-color`. +- Only the first series reaches 3:1 on white, so every chart carries a legend or direct labels; colour alone never identifies a series. Orange and green bars get a 1px `#475569` edge. +- Heatmaps (correlation, PCA weights, confusion matrix, k-means centres) share `DIVERGING_COLORS` (`#e34948` → `#f1f5f9` → `#2b33e9`, or its grey-to-blue half) and `cellLabelColor`: ink on every cell except the deep blue end (≥ 0.7), where white wins. White never reaches 4.5:1 anywhere on the red half. +- The adjacent-pair check covers bars and stacks. Lines that cross or dots side by side compare every pair, and slots 1 and 5 (`#2b33e9`, `#4a3aa7`) fail that at ΔE 11: past four series, name them in rows instead (the k-means centres heatmap) rather than in colour. +- `#7f9ce8`, `#ffba08`, `#e87ba4` and `#1baf7a` sit under 3:1 on white, so a chart that reaches them keeps its legend and the exact-value table under it. `#ffba08` is also lighter than the validator's band — the charter colour stays, with its 1px edge. +- `ChartRendererComponent` applies the house style under every chart's own options: the app font (canvas cannot read `--font-sans`; Alaska is left out), 13px/600 ink titles with 12px muted subtitles, a white tooltip card with a hairline border, and ECharts `aria` descriptions. Renderers set only what differs. +- A chart must fit a 360px compare column: put long category names on the y axis (they wrap), slant crowded x labels, keep captions to short lines. +- Lines are drawn as measured: no default smoothing (it invents bends between discrete points such as an elbow or ROC curve) and no default area fill. + +## Adjacent-panel test + +Before shipping a visual change, compare to an unchanged sibling (variables / algorithm / stats). A user should reasonably believe the same team designed both. If not, fix the three largest mismatches (type, spacing, color, radius, shadow, buttons, inputs, states) and look again. diff --git a/DESIGN_SYSTEM.yaml b/DESIGN_SYSTEM.yaml deleted file mode 100644 index 534cbb4da..000000000 --- a/DESIGN_SYSTEM.yaml +++ /dev/null @@ -1,115 +0,0 @@ -agent_name: MIP_BrandGuard -purpose: > - Enforce MIP (Medical Informatics Platform) brand guidelines when generating or reviewing - marketing, web, UI, print, and social assets. Ensure correct logo usage, colors, and typography. - -source_document: - title: "MIP Charte graphique" - date: "2023-10-02" - creator: "Donovan Studio" - client: "CHUV" - -brand_identity: - brand_name: "MIP" - baseline: "Medical Informatics Platform" - -logo_system: - official_variants: - - id: symbol_only - description: "Symbol without wordmark" - usage: ["social avatar", "url/favicon/icon"] - minimum_size: { px: 30, mm: 10 } - - id: primary - description: "Symbol + 'mip' wordmark" - usage: ["website", "email", "stationery", "poster"] - minimum_size: { px: 50 } - - id: secondary_with_baseline - description: "Logo with baseline 'Medical Informatics Platform'" - usage: ["formal comms", "stationery/poster", "website/email", "signage"] - minimum_size: { px: 90 } - - protection_zone: - rule: > - Always keep clear space around the logo (zone de protection) as defined in the charter. - No text, images, borders, or UI elements may enter this exclusion area. - enforcement: "Reject placements that violate clear space." - - construction_rules: - rule: > - Do not redraw, stretch, skew, rotate, outline, add effects, or modify proportions of any logo variant. - Do not rearrange symbol, wordmark, or baseline. - enforcement: "If a requested transformation changes geometry, refuse and propose compliant alternative." - -color_palette: - primary_intent: "Digital-first palette for web, social, video, digital interfaces." - print_guidance: "For physical/print supports, prefer black logo on white." - colors: - - name: dark_blue - hex: "#2B33E9" - rgb: [43, 51, 233] - cmyk_approx: [89.85, 74.4, 0, 0] - - name: light_blue - hex: "#7F9CE8" - rgb: [127, 156, 232] - cmyk_approx: [54.62, 35.3, 0, 0] - - name: orange - hex: "#FFBA08" - rgb: [255, 186, 8] - cmyk_approx: [0, 30.98, 92.37, 0] - - name: light_green - hex: "#DFEFE4" - rgb: [223, 239, 228] - cmyk_approx: [15.88, 0, 13.86, 0] - -approved_logo_color_combinations: - - id: white_on_dark_blue - - id: blue_on_light_blue - - id: black_on_white - - id: blue_on_orange - - id: blue_on_green - - id: white_on_black -rule: "Only these logo/background combinations are permitted. Any other colorway must be rejected." - -typography: - logo_font: - name: "Mark Pro Bold" - foundry: "FontFont" - usage: "Logo only (do not substitute)." - brand_text_font: - name: "Alaska Regular" - foundry: "Newglyph" - usage: "All identity text compositions for HIP and MIP." -rule: "Use Alaska Regular for body/headings unless explicitly constrained; never replace logo type." - -compliance_checks: - - check: logo_variant_selected_correctly - pass_if: "Variant matches intended medium (icon/social vs web/print vs formal baseline)." - - check: minimum_size_respected - pass_if: "symbol_only>=30px/10mm; primary>=50px; secondary>=90px." - - check: clear_space_respected - pass_if: "No elements intrude into protection zone." - - check: approved_colors_only - pass_if: "Palette hex values used; no arbitrary near-matches." - - check: logo_colorway_approved - pass_if: "Logo/background matches one of the approved combinations." - - check: print_recommendation - pass_if: "If print and no strong reason otherwise: black on white preferred." - - check: typography_correct - pass_if: "Alaska Regular used for text; Mark Pro Bold only for logo." - -default_decision_policy: - when_uncertain: > - Choose the most conservative compliant option: - - Primary logo for general web/email - - Symbol-only for avatars/icons - - Secondary with baseline for formal material - - Black-on-white for print - refusal_style: > - If user request conflicts with guidelines, respond with: - (1) why it conflicts, - (2) the closest compliant alternative, - (3) what info is needed to choose between compliant options (only if essential). - -output_expectations: - - "When generating design specs, always list: logo variant, size, clear-space, colors (hex), typography." - - "When reviewing assets, return a pass/fail plus concrete fixes." diff --git a/Dockerfile b/Dockerfile index b2abbb627..4aeebddf0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,21 @@ # Step 1: Build the Angular app -FROM node:20-alpine AS build +FROM node:22-alpine@sha256:fe4f6d74e20370acb04ad889077d14d0f64a9c8ea6a42bc60cddf1e844699914 AS build WORKDIR /app # Install dependencies with cache mount for npm cache -COPY package.json package-lock.json ./ +# Some branches intentionally do not track package-lock.json. +# Copy whichever npm manifest files exist and install accordingly. +COPY package*.json ./ RUN npm config set fetch-retries 10 \ && npm config set fetch-retry-mintimeout 30000 \ && npm config set fetch-retry-maxtimeout 300000 RUN --mount=type=cache,target=/root/.npm \ - npm ci --legacy-peer-deps --no-audit --no-fund --registry=https://registry.npmjs.org/ + if [ -f package-lock.json ]; then \ + npm ci --legacy-peer-deps --no-audit --no-fund --registry=https://registry.npmjs.org/; \ + else \ + npm install --legacy-peer-deps --no-audit --no-fund --registry=https://registry.npmjs.org/; \ + fi # Copy source code @@ -22,19 +28,26 @@ RUN --mount=type=cache,target=/app/.angular/cache \ npm run build -- --configuration ${BUILD_CONFIGURATION} # Step 2: Use Nginx to serve the Angular app -FROM nginx:alpine -RUN apk add --no-cache gettext +FROM nginx:alpine-slim@sha256:858c48843bd53b3f72487a2156071c63954b8e3de4882aeb11efcd92717dd261 +RUN apk upgrade --no-cache \ + && apk add --no-cache gettext-envsubst ENV PLATFORM_BACKEND_SERVER=platform-backend-service:8080 \ PLATFORM_BACKEND_CONTEXT=services \ NOTEBOOK_ENABLED=0 \ JUPYTER_SERVER=jupyterhub:8000 \ JUPYTER_CONTEXT=notebook \ - JUPYTER_LANDING_PATH=/hub/spawn \ - FRONTEND_VERSION=10.0.1 \ - BACKEND_VERSION=8.2.0 \ - EXAFLOW_VERSION=0.28.0 + GUIDE_COVARIATE=Sex \ + GUIDE_VARIABLE=Age +COPY docker-entrypoint.sh /docker-entrypoint.sh +RUN chmod +x /docker-entrypoint.sh +COPY nginx-websocket-map.conf /etc/nginx/conf.d/00-websocket-map.conf COPY nginx.conf.template /etc/nginx/templates/default.conf.template RUN rm -rf /usr/share/nginx/html/* COPY --from=build /app/dist/fl-platform/browser /usr/share/nginx/html + +RUN chown -R nginx:nginx /usr/share/nginx/html /etc/nginx/conf.d /var/cache/nginx \ + && install -o nginx -g nginx -m 0644 /dev/null /var/run/nginx.pid + +USER nginx EXPOSE 80 -CMD ["/bin/sh", "-c", "envsubst '$$PLATFORM_BACKEND_SERVER $$PLATFORM_BACKEND_CONTEXT $$NOTEBOOK_ENABLED $$JUPYTER_SERVER $$JUPYTER_CONTEXT' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf && envsubst '$$FRONTEND_VERSION $$BACKEND_VERSION $$EXAFLOW_VERSION $$NOTEBOOK_ENABLED $$JUPYTER_CONTEXT $$JUPYTER_LANDING_PATH' < /usr/share/nginx/html/assets/env.template.js > /usr/share/nginx/html/assets/env.js && exec nginx -g 'daemon off;'"] +CMD ["/docker-entrypoint.sh"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..00580f1a0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Medical Informatics Platform (MIP) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index ae031569e..2c5235c24 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,29 @@ # Medical Informatics Platform (MIP) - Platform UI -The Platform UI for the Medical Informatics Platform (MIP). An Angular 21 standalone frontend application for composing, running, and analyzing data science experiments, featuring algorithmic configurations and JupyterHub integration. It integrates with a backend over `/services` (proxied in dev) and uses Keycloak for robust OAuth2 authentication. +The Platform UI for the Medical Informatics Platform (MIP). An Angular 22 standalone frontend application for composing, running, and analyzing data science experiments, featuring algorithmic configurations. It integrates with a backend over `/services` (proxied in dev) and uses Keycloak for robust OAuth2 authentication. ## Highlights -- **Experiment Studio** (`src/app/pages/experiment-studio`): select data models/datasets, pick variables and filters (QueryBuilder), configure algorithms, run jobs, view charts/tables. +- **Experiment Studio** (`src/app/pages/experiment-studio`): select data models/datasets, pick variables and filters, configure algorithms, run jobs, view charts/tables. - **Experiments Dashboard** (`src/app/pages/experiments-dashboard`): list/search/paginate, share links, compare results side by side, export to PDF. - **Auth**: Keycloak-based session via `/services/activeUser`; guard protects routes and redirects to login if unauthenticated. ## Requirements -- Node 20+ and npm 10+ +- Node 22+ and npm 10+ - Backend reachable at `http://localhost:8080/services` (adjustable in proxy/Docker env). -- JupyterHub reachable behind the frontend at `/notebook/` (note the trailing slash). - Keycloak endpoints exposed at `/services/oauth2/authorization/keycloak` and `/services/logout`. ## Quick Start (dev) 1) Install dependencies: `npm ci` 2) Start dev server with proxy: `npm start` - Serves at `http://localhost:4200/` - - Proxies `/services` to `http://localhost:8080` and `/notebook/` to your notebook server via `src/proxy.conf.json` + - Proxies `/services` to `http://localhost:8080` via `src/proxy.conf.json` 3) Ensure backend is running and you can authenticate with Keycloak. ## Scripts - `npm start` – dev server (`ng serve`) with proxy. - `npm run build` – production build to `dist/fl-platform`. - `npm run watch` – development build in watch mode. -- `npm test` – unit tests via Karma. +- `npm test` – unit tests via Vitest (headless Chromium). ## Result code → label mappings @@ -34,8 +33,9 @@ See `src/app/core/algorithm-result-enum-mapper.md` for detailed mapping behavior - `GET /services/activeUser` – current user/session. - `GET /services/algorithms` – algorithm catalog. - `GET /services/data-models` – available data models (used to build variable tree and datasets). -- `POST /services/experiments` – run experiment (polls for results). -- `POST /services/experiments/transient` – quick previews (histograms, descriptive stats). +- `POST /services/experiments/preview` – quick previews (histograms, descriptive stats); nothing stored. +- `POST /services/experiments/runs` – run an experiment; the backend keeps the result for 24h under the returned `uuid`. +- `POST /services/experiments` – `{ runId, name }`: save a kept run as an experiment without running it again. - `GET /services/experiments/:id` – fetch experiment (and results). - `PATCH /services/experiments/:id` – update name/description/shared flag. - `DELETE /services/experiments/:id` – delete experiment. @@ -54,17 +54,11 @@ See `src/app/core/algorithm-result-enum-mapper.md` for detailed mapping behavior - `src/app/pages/experiment-studio/` – variables panel, filter modal, algorithm panel, visualisations (charts/histograms/bubble). - `src/app/pages/experiments-dashboard/` – list/search, detail, compare, mapper. - `src/app/pages/account-page/` – basic profile view. -- `src/app/pages/shared/` – header/footer/navbar/accordion/spinner utilities. -- `src/styles.css` – global styles and QueryBuilder theming; assets in `src/assets/`. -- `Dockerfile` / `nginx.conf.template` – container build and runtime proxy (`PLATFORM_BACKEND_SERVER`, `PLATFORM_BACKEND_CONTEXT`, `NOTEBOOK_ENABLED`, `JUPYTER_SERVER`, `JUPYTER_CONTEXT` envs). +- `src/app/pages/shared/` – header/footer/spinner utilities. +- `src/styles.css` – global styles; assets in `src/assets/`. +- `Dockerfile` / `nginx.conf.template` – container build and runtime proxy (`PLATFORM_BACKEND_SERVER`, `PLATFORM_BACKEND_CONTEXT` envs). + -## Notebook Page (`/notebook`) -- Frontend opens JupyterHub via same-origin path `/notebook` (runtime configurable in `window.__env.JUPYTER_CONTEXT_PATH`). -- Notebook auth/session is handled by JupyterHub + OIDC (no frontend token injection). -- Runtime frontend env knobs: - - `NOTEBOOK_ENABLED` (default `0`; when `0`, `/notebook` route does not match) - - `JUPYTER_CONTEXT_PATH` (default `/notebook`) - - `JUPYTER_LANDING_PATH` (default `/hub/spawn`) ## Docker Build and serve with nginx: @@ -73,16 +67,12 @@ docker build -t fl-platform . docker run \ -e PLATFORM_BACKEND_SERVER=platform-backend-service:8080 \ -e PLATFORM_BACKEND_CONTEXT=services \ - -e NOTEBOOK_ENABLED=1 \ - -e JUPYTER_SERVER=jupyterhub:8000 \ - -e JUPYTER_CONTEXT=jupyter \ - -e JUPYTER_LANDING_PATH=/hub/spawn \ -p 80:80 fl-platform ``` -Nginx always proxies `/${PLATFORM_BACKEND_CONTEXT}/` to the backend. Notebook proxying under `/${JUPYTER_CONTEXT}/` is active only when `NOTEBOOK_ENABLED=1`. +Nginx always proxies `/${PLATFORM_BACKEND_CONTEXT}/` to the backend. ## Testing -- Unit: `npm test` (Karma). +- Unit: `npm test` (Vitest, headless Chromium). - Manual: run `npm start`, authenticate, and exercise Experiment Studio and Dashboard against a running backend. ## Notes diff --git a/angular.json b/angular.json index 4f2e6148f..eaee9348f 100644 --- a/angular.json +++ b/angular.json @@ -11,18 +11,15 @@ "prefix": "app", "architect": { "build": { - "builder": "@angular-devkit/build-angular:application", + "builder": "@angular/build:application", "options": { "outputPath": "dist/fl-platform", "index": "src/index.html", "browser": "src/main.ts", - "polyfills": [ - "zone.js" - ], + "polyfills": [], "tsConfig": "tsconfig.app.json", "assets": [ "src/assets", - "src/favicon.ico", { "glob": "**/*", "input": "public" @@ -31,14 +28,9 @@ "styles": [ "@angular/material/prebuilt-themes/azure-blue.css", "node_modules/@fortawesome/fontawesome-free/css/all.min.css", - "node_modules/jQuery-QueryBuilder/dist/css/query-builder.default.css", "src/styles.css" ], - "scripts": [ - "node_modules/jquery/dist/jquery.min.js", - "node_modules/jquery-ui-dist/jquery-ui.min.js", - "node_modules/jQuery-QueryBuilder/dist/js/query-builder.standalone.js" - ], + "scripts": [], "allowedCommonJsDependencies": [ "canvg", "core-js", @@ -49,6 +41,11 @@ }, "configurations": { "production": { + "optimization": { + "scripts": true, + "styles": true, + "fonts": false + }, "budgets": [ { "type": "initial", @@ -58,7 +55,7 @@ { "type": "anyComponentStyle", "maximumWarning": "20kB", - "maximumError": "25kB" + "maximumError": "40kB" } ], "outputHashing": "all" @@ -72,7 +69,7 @@ "defaultConfiguration": "production" }, "serve": { - "builder": "@angular-devkit/build-angular:dev-server", + "builder": "@angular/build:dev-server", "configurations": { "production": { "buildTarget": "fl-platform:build:production" @@ -87,29 +84,19 @@ } }, "extract-i18n": { - "builder": "@angular-devkit/build-angular:extract-i18n" + "builder": "@angular/build:extract-i18n" }, "test": { - "builder": "@angular-devkit/build-angular:karma", + "builder": "@angular/build:unit-test", "options": { - "polyfills": [ - "zone.js", - "zone.js/testing" - ], "tsConfig": "tsconfig.spec.json", - "assets": [ - "src/assets", - { - "glob": "**/*", - "input": "public" - } + "setupFiles": [ + "src/test-setup.ts" ], - "styles": [ - "@angular/material/prebuilt-themes/azure-blue.css", - "node_modules/@fortawesome/fontawesome-free/css/all.min.css", - "src/styles.css" + "browsers": [ + "ChromiumHeadless" ], - "scripts": [] + "browserViewport": "1280x720" } } } diff --git a/ci/README.md b/ci/README.md new file mode 100644 index 000000000..27ccf1cd3 --- /dev/null +++ b/ci/README.md @@ -0,0 +1,229 @@ +# Application Security Pipelines + +Automated security scanning for this service, enforced during Continuous Integration (CI) to block known vulnerabilities and insecure code before merge, automatically on Pull Requests. + +Following the OWASP DevSecOps model, scanning is split into three independent pipelines, each with its own workflow, orchestrator script, and gate: + +| Pipeline | Workflow | Scans | Tools | +|---|---|---|---| +| **Container Scanning** | `container-scan.yml` | The built Docker image + the Dockerfile | Trivy, OSV-Scanner (image CVEs) . Hadolint, OpenGrep (Dockerfile SAST) | +| **SCA** (Software Composition Analysis) | `sca.yml` | Application dependencies, via SBOM | Trivy, OSV-Scanner | +| **SAST** (Static Application Security Testing) | `sast.yml` | Application source code | OpenGrep | + +## Table of Contents + +- [1. Repository layout](#1-repository-layout) +- [2. Architecture](#2-architecture) +- [3. Tool installation (`setup-tools.sh`)](#3-tool-installation-setup-toolssh) +- [4. Pipeline: Container Scanning](#4-pipeline-container-scanning) +- [5. Pipeline: Software Composition Analysis (SCA)](#5-pipeline-software-composition-analysis-sca) +- [6. Pipeline: Static Application Security Testing (SAST)](#6-pipeline-static-application-security-testing-sast) +- [7. Gate status reference](#7-gate-status-reference) +- [8. Suppressing a false positive](#8-suppressing-a-false-positive) + +## 1. Repository layout + +``` +.github/ +└── workflows/ + ├── container-scan.yml # builds the image, scans the Dockerfile (SAST) and image (SCA) + ├── sca.yml # resolves deps, generates SBOM, scans it (SCA) + ├── sast.yml # scans source code (SAST) + ├── publish_images.yml # publishes built images + └── ebrains.yml # EBRAINS-specific pipeline + +ci/ +├── setup-tools.sh # installs trivy, osv-scanner, opengrep, hadolint, semgrep-rules +├── container_scan.py # orchestrator for container-scan.yml +├── sca_scan.py # orchestrator for sca.yml +├── sast_scan.py # orchestrator for sast.yml +├── parse_sarif.py # shared: reads SARIF security-severity scores +├── suppress_trivy.yaml # shared Trivy ignore file +└── suppress_osv_scanner.toml # shared OSV-Scanner ignore file +``` + +> **Note:** all three workflows trigger on `pull_request`, `workflow_dispatch`, and a weekly Monday 02:00 UTC schedule, and run independently in parallel. Each has its own gate and its own category in the GitHub Security tab. + +## 2. Architecture + +```mermaid +flowchart LR + PR["Pull Request"] --> CS["container-scan.yml"] --> SEC[("GitHub Security Tab")] + PR --> SCA["sca.yml"] --> SEC + PR --> SAST["sast.yml"] --> SEC +``` + +All three trigger independently and run in parallel; each uploads its own SARIF category to the Security tab. + +--- + +## 3. Tool installation (`setup-tools.sh`) + +```bash +bash ci/setup-tools.sh --install-tool [--sbom-ecosystem maven|npm|none] +``` + +`--install-tool` accepts a comma-separated list (or `all`): + +| Tool | Installed from | Used by | +|---|---|---| +| `trivy` | official release tarball, SHA256-pinned | Container Scanning (sca), SCA | +| `osv-scanner` | GitHub release binary, SHA256-pinned | Container Scanning (sca), SCA | +| `opengrep` | GitHub release binary, SHA256-pinned | Container Scanning (sast), SAST | +| `hadolint` | GitHub release binary, SHA256-pinned | Container Scanning (sast) | +| `semgrep-rules` | cloned from `semgrep/semgrep-rules` at a pinned commit | Container Scanning (sast), SAST | + +`--sbom-ecosystem npm` generates `target/bom.json` afterward. `container-scan.yml`, scans the built image directly and needs no SBOM. + +All tool versions and SHA256 checksums are pinned at the top of the script (with `# renovate:` markers so Renovate bumps version + checksum together). The script stops and prints the failing line/command on any error rather than continuing silently. + +--- + +## 4. Pipeline: Container Scanning + +`container-scan.yml` builds the Docker image once, then runs `container_scan.py` twice against it, once per `--scan-type`: + +- **`--scan-type sast`** → runs **Hadolint** and **OpenGrep** against the `Dockerfile` itself (bad practices, missing pinning, insecure instructions). +- **`--scan-type sca`** → runs **Trivy** and **OSV-Scanner** against the *built image* (OS packages, layers). + +Both steps run regardless of each other (`if: always()`), all four SARIF files are uploaded individually to the Security tab, then merged into one artifact via `--merge-sarif` for retention. + +`container_scan.py` is a single CLI shared by both scan types: + +``` +$ python3 ci/container_scan.py --help +usage: sec-orchestrator [-h] [-s {sast,sca}] [-i IMAGE] [--merge-sarif SARIF_FILE [SARIF_FILE ...]] [--merge-output MERGE_OUTPUT] + +Agnostic DevSecOps Container scanning Pipeline Orchestrator + +options: + -h, --help show this help message and exit + -s, --scan-type {sast,sca} + Specify the security methodology to execute (e.g., sast, sca) + -i, --image IMAGE Target Docker image reference + --merge-sarif SARIF_FILE [SARIF_FILE ...] + List of SARIF files to merge into one report + --merge-output MERGE_OUTPUT + Output path for the merged SARIF file +``` + +**Running it locally:** +```bash +docker build -t app:local . +bash ci/setup-tools.sh --install-tool trivy,osv-scanner,opengrep,hadolint,semgrep-rules +python ci/container_scan.py --scan-type sast +python ci/container_scan.py --scan-type sca --image app:local +``` + +## 5. Pipeline: Software Composition Analysis (SCA) + +`sca.yml` scans **application dependencies**, not the container. It installs dependencies, generates an SBOM (CycloneDX), and scans that SBOM with **Trivy** and **OSV-Scanner** via `sca_scan.py`. + +Both tools need to be installed first, same as Container Scanning, via `setup-tools.sh --install-tool trivy,osv-scanner`. + +**Running it locally:** +```bash +npm ci +bash ci/setup-tools.sh --install-tool trivy,osv-scanner --sbom-ecosystem npm # -> npx @cyclonedx/cyclonedx-npm -> target/bom.json +python ci/sca_scan.py +``` + +Use `npm ci`, not `npm install`, before generating the SBOM: `npm ci` installs strictly from `package-lock.json`, deletes `node_modules` first for a clean install, and **fails immediately** if `package.json` and `package-lock.json` are out of sync, and it never rewrites the lockfile. If it fails, that's a signal `package-lock.json` is stale and needs to be regenerated locally (`npm install`, then commit the updated lockfile), not something to patch around in the pipeline. + +Trivy and OSV-Scanner both run against the SBOM, findings are evaluated by `parse_sarif.evaluate()`, and the two SARIF files are merged into one artifact. This uses the same CVSS-score gate model as the SCA half of Container Scanning. + +## 6. Pipeline: Static Application Security Testing (SAST) + +`sast.yml` scans **source code** (not the Dockerfile, not dependencies) with **OpenGrep**. + +`run_opengrep()` runs twice: once to write the full SARIF report, once as the actual gate, using the same command both times with different flags. + +**Running it locally:** +```bash +bash ci/setup-tools.sh --install-tool opengrep,semgrep-rules +python ci/sast_scan.py +``` + +--- + +## 7. Gate status reference + +Two different gate models are in play, depending on whether a tool reports **CVE severity** or **rule severity**: + +### CVSS-score gate (SCA tools: Trivy, OSV-Scanner; both container-image and SBOM scans) + +`parse_sarif.evaluate()` reads the `security-severity` property of each SARIF result and takes the **highest score across all results**. That single number decides the status: + +| Status | Meaning | Blocks the pipeline? | +|---|---|---| +| `PASSED` | Highest score < 5.0 | No | +| `WARNING` | Highest score 5.0 to 7.9 | No (logged only) | +| `FAILED` | Highest score ≥ 8.0 | **Yes** | +| `ERROR` | Tool crashed / SARIF missing | **Yes** | + +### Rule-severity gate (SAST tools: OpenGrep, Hadolint) + +These tools don't report CVSS. Each tool's own severity threshold (`--severity=ERROR --error` for OpenGrep, `--failure-threshold error` for Hadolint) decides the status directly: + +| Status | Meaning | Blocks the pipeline? | +|---|---|---| +| `PASSED` | No error-severity findings | No | +| `FAILED` | Error-severity findings present | **Yes** | +| `ERROR` | Tool did not run correctly | **Yes** | + +Both `container_scan.py --scan-type sast` and `sast_scan.py` use this model. `container_scan.py --scan-type sca` and `sca_scan.py` use the CVSS-score model above. + +All three pipelines write their findings as SARIF files, which are uploaded to the GitHub Security tab, but they're also plain JSON you can inspect directly. To browse a SARIF file locally without the Security tab (e.g. one downloaded from the workflow artifacts), drop it into a SARIF viewer such as [Microsoft's SARIF Web Component](https://microsoft.github.io/sarif-web-component/). + +--- + +## 8. Suppressing a false positive + +Suppression applies to the **CVSS-score tools** (Trivy, OSV-Scanner) and is shared across Container Scanning and SCA, since both point at the same two ignore files. + +**Trivy** (`suppress_trivy.yaml`): +```yaml +vulnerabilities: + # Example 1: non-reachable code path + - id: CVE-2026-54515 + statement: "Vulnerable code path is not reachable: affected function is dead code in our build." + expires: 2026-09-30 # The expiration date of the ignore finding + + # Example 2: low severity, accepted risk with an owner and a ticket + - id: CVE-2025-11111 + statement: "Low severity, affects an optional dev-only dependency not shipped in production images. Risk accepted." + expires: 2026-10-15 + + # Example 3: scope the ignore instead of ignoring everywhere. paths limits it + # to specific files, purls limits it to specific packages (by PURL). Without + # either, the ignore applies to every file/package where this id shows up. + - id: CVE-2024-33333 + paths: + - "test/fixtures/legacy-bundle.jar" + purls: + - "pkg:maven/org.example/legacy-lib" + statement: "Only present in test fixtures; not part of the shipped artifact." + expires: 2026-11-01 +``` + +**OSV-Scanner** (`suppress_osv_scanner.toml`): +```toml +# Example 1: vulnerable code path is not reachable in how we use the library. +[[IgnoredVulns]] +id = "GHSA-5jmj-h7xm-6q6v" +ignoreUntil = 2026-09-30 +reason = "Vulnerable function is never called." + +# Example 2: low-severity, accepted as risk. +# Only use this pattern for LOW/MEDIUM severity findings with limited impact, +[[IgnoredVulns]] +id = "GHSA-9jx5-6pgf-crrp" +ignoreUntil = 2026-10-15 +reason = "Low severity DoS in a dev-only tool, not present in production build. Risk accepted by security team." +``` + +Refer to the official docs for complete suppression options: +- **Trivy**: [Filtering and ignore files](https://trivy.dev/docs/latest/configuration/filtering/#trivyignoreyaml) +- **OSV-Scanner**: [Ignore vulnerabilities by ID](https://google.github.io/osv-scanner/configuration/#ignore-vulnerabilities-by-id) + +OpenGrep/Hadolint findings (SAST) aren't suppressed through a shared ignore file in this setup, handle those at the rule/finding level instead. diff --git a/ci/container_scan.py b/ci/container_scan.py new file mode 100644 index 000000000..7fb352bbc --- /dev/null +++ b/ci/container_scan.py @@ -0,0 +1,246 @@ +import subprocess +import os +import sys +import logging +import json +import argparse +from parse_sarif import evaluate, GATE_FAIL_THRESHOLD, GATE_WARN_THRESHOLD + +GREEN = '\033[92m' +RED = '\033[91m' +RESET = '\033[0m' +BOLD = '\033[1m' +YELLOW = '\033[93m' + +logging.basicConfig( + level=logging.INFO, + format='%(message)s' # Clean format to prevent double-timestamps in CI logs +) +logger = logging.getLogger("container-scan-orchestrator") + +# --- Configurable values +IMAGE_NAME = os.getenv("IMAGE_NAME", "platform-ui:local") + +# --- SCA / CVE (Trivy + OSV) --- +TRIVY_IGNOREFILE = os.getenv("TRIVY_IGNOREFILE", "ci/suppress_trivy.yaml") +OSV_IGNOREFILE = os.getenv("OSV_IGNOREFILE", "ci/suppress_osv_scanner.toml") +TRIVY_SCA_SARIF_OUTPUT = os.getenv("TRIVY_SCA_SARIF_OUTPUT", "sca-trivy-container.sarif") +OSV_SCA_SARIF_OUTPUT = os.getenv("OSV_SCA_SARIF_OUTPUT", "sca-osv-container.sarif") + +# --- SAST/Code Linting (OpenGrep + Hadolint) --- +SEMGREP_CONFIG_RULESETS = os.getenv( + "SEMGREP_CONFIG_RULESETS", + "semgrep-rules/dockerfile" +).split() +OPENGREP_SAST_SARIF_OUTPUT = os.getenv("OPENGREP_SAST_SARIF_OUTPUT", "sast-opengrep-dockerfile.sarif") +HADOLINT_SAST_SARIF_OUTPUT = os.getenv("HADOLINT_SAST_SARIF_OUTPUT", "sast-hadolint-dockerfile.sarif") + +# --- Functions to run each SCA tool and handle their outputs +def run_trivy(): + cmd = [ + "trivy", "image", + IMAGE_NAME, + "--format", "sarif", + "--ignorefile", TRIVY_IGNOREFILE, + "--output", TRIVY_SCA_SARIF_OUTPUT + ] + return subprocess.run(cmd).returncode + +def run_osv_scanner(): + cmd = [ + "osv-scanner", "scan", "image", + IMAGE_NAME, + "--config", OSV_IGNOREFILE, + "--format", "sarif", + "--output-file", OSV_SCA_SARIF_OUTPUT + ] + exit_code = subprocess.run(cmd).returncode + if exit_code == 1: + return 0 # OSV Scanner returns 1 if vulnerabilities are found, but we want to continue the pipeline + return exit_code + +def handle_sca(): + + tools = {"trivy": run_trivy, "osv-scanner": run_osv_scanner} + sarif_files = {"trivy": TRIVY_SCA_SARIF_OUTPUT, "osv-scanner": OSV_SCA_SARIF_OUTPUT} + tool_status = {} # "PASSED" | "WARNING" | "FAILED" | "ERROR" + gate_failed = False + + # Run each SCA tool and collect their exit codes + for name, tool_fn in tools.items(): + exit_code = tool_fn() + logger.info("-" * 40) + + path = sarif_files[name] + if exit_code != 0 and os.path.exists(path): + logger.error(f"{RED}[!] {name} exit code {exit_code} but wrote {path}{RESET}") + tool_status[name] = "ERROR" + gate_failed = True + + # Evaluate each SARIF file for gate decision + for name, path in sarif_files.items(): + if name in tool_status: + continue # already flagged ERROR above, don't overwrite it + + if not os.path.exists(path): + logger.error(f"{RED}[!] {name} SARIF missing: {path},tool failed to run (not a vulnerability){RESET}") + tool_status[name] = "ERROR" + gate_failed = True + continue + + eval_result = evaluate(path) + + if eval_result.gate_failed: + tool_status[name] = "FAILED" + gate_failed = True + elif eval_result.gate_warn: + tool_status[name] = "WARNING" + else: + tool_status[name] = "PASSED" + + # Print summary of results + logger.info(f"\n{BOLD}========== SCA PIPELINE SUMMARY =========={RESET}") + for name, status in tool_status.items(): + if status == "PASSED": + logger.info(f"[{name}]: {GREEN}PASSED{RESET}") + elif status == "WARNING": + logger.warning(f"[{name}]: {YELLOW}WARNING (findings between {GATE_WARN_THRESHOLD} and {GATE_FAIL_THRESHOLD}){RESET}") + elif status == "ERROR": + logger.error(f"[{name}]: {RED}ERROR (tool did not run correctly){RESET}") + else: + logger.error(f"[{name}]: {RED}FAILED (CVSS >= {GATE_FAIL_THRESHOLD} found){RESET}") + + # Exit with non-zero code if any tool failed the gate + if gate_failed: + logger.error(f"{RED}One or more SCA tools failed the gate check.{RESET}") + sys.exit(1) + + +# --- Functions to run each SAST tool and handle their outputs +def run_hadolint(): + cmd = [ + "hadolint", "Dockerfile", + "--failure-threshold", "error", + "--format", "sarif", + ] + with open(HADOLINT_SAST_SARIF_OUTPUT, "w") as f: + result = subprocess.run(cmd, stdout=f) + + return result.returncode + +def run_opengrep(): + base_cmd = ["opengrep", "scan", "--include=Dockerfile", "-q"] + \ + [f"--config {config}" for config in SEMGREP_CONFIG_RULESETS] + + report_cmd = (base_cmd + ["--sarif", "--output", OPENGREP_SAST_SARIF_OUTPUT]) + report_cmd = " ".join(report_cmd).split() + logger.info(f"{BOLD}Running (report):{RESET} {' '.join(report_cmd)}") + subprocess.run(report_cmd) + + gate_cmd = (base_cmd + ["--severity=ERROR", "--error"]) + gate_cmd = " ".join(gate_cmd).split() + logger.info(f"{BOLD}Running (gate):{RESET} {' '.join(gate_cmd)}") + return subprocess.run(gate_cmd).returncode + +def handle_sast(): + tools = { + "hadolint": run_hadolint, + "opengrep": run_opengrep, + } + + tool_status = {} + exit_codes = {} + + for name, run_fn in tools.items(): + exit_code = run_fn() + exit_codes[name] = exit_code + if exit_code == 0: + tool_status[name] = "PASSED" + elif exit_code == 1: + tool_status[name] = "FAILED" + else: + tool_status[name] = "ERROR" + + logger.info(f"\n{BOLD}========== SAST PIPELINE SUMMARY =========={RESET}") + for name, status in tool_status.items(): + if status == "PASSED": + logger.info(f"[{name}]: {GREEN}PASSED (exit code 0){RESET}") + elif status == "FAILED": + logger.error(f"[{name}]: {RED}FAILED (exit code 1 - error-severity findings){RESET}") + else: + logger.error(f"[{name}]: {RED}ERROR (exit code {exit_codes[name]}, tool did not run correctly){RESET}") + logger.info(f"{BOLD}==========================================={RESET}\n") + + if any(status != "PASSED" for status in tool_status.values()): + sys.exit(1) + +def merge_sarifs(sarif_paths, output_path): + merged = { + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", + "version": "2.1.0", + "runs": [], + } + + for path in sarif_paths: + if not os.path.exists(path): + logger.warning(f"{path} not found, skipping in merge") + continue + with open(path) as f: + sarif = json.load(f, strict=False) + merged["runs"].extend(sarif.get("runs", [])) + + with open(output_path, "w") as f: + json.dump(merged, f) + + logger.info(f"SARIF files merged successfully into {output_path}") + +def main(): + parser = argparse.ArgumentParser( + prog="sec-orchestrator", + description="Agnostic DevSecOps Container scanning Pipeline Orchestrator" + ) + + # The primary router + parser.add_argument( + "-s", "--scan-type", + choices=["sast", "sca"], + help="Specify the security methodology to execute (e.g., sast, sca)" + ) + + # Image flag specifically SCA approach + parser.add_argument( + "-i", "--image", + help="Target Docker image reference" + ) + + parser.add_argument( + "--merge-sarif", + nargs="+", + metavar="SARIF_FILE", + help="List of SARIF files to merge into one report" + ) + + parser.add_argument( + "--merge-output", + default="merged-container-scan.sarif", + help="Output path for the merged SARIF file" + ) + args = parser.parse_args() + + if args.scan_type == "sast": + logger.info(f"{BOLD}Initiating SAST pipeline: {RESET}") + handle_sast() + # Execution + elif args.scan_type == "sca": + logger.info(f"{BOLD}Initiating SCA pipeline on {args.image}{RESET}") + global IMAGE_NAME + if args.image: # Override the global IMAGE_NAME + IMAGE_NAME = args.image + handle_sca() + + if args.merge_sarif: + merge_sarifs(args.merge_sarif, args.merge_output) + return + +if __name__ == "__main__": + main() diff --git a/ci/parse_sarif.py b/ci/parse_sarif.py new file mode 100644 index 000000000..793a2e6ed --- /dev/null +++ b/ci/parse_sarif.py @@ -0,0 +1,43 @@ +import json +import os +from dataclasses import dataclass + +GATE_FAIL_THRESHOLD = float(os.getenv("GATE_FAIL_THRESHOLD", "8.0")) +GATE_WARN_THRESHOLD = float(os.getenv("GATE_WARN_THRESHOLD", "5.0")) + +@dataclass +class EvaluationResult: + gate_failed: bool + gate_warn: bool + +def evaluate(sarif_paths): + if isinstance(sarif_paths, str): + sarif_paths = [sarif_paths] + + max_score = 0.0 + for path in sarif_paths: + try: + with open(path) as f: + sarif = json.load(f, strict=False) + except FileNotFoundError: + raise FileNotFoundError(f"[parse_sarif] SARIF file not found: {path}") + except json.JSONDecodeError as e: + raise ValueError(f"[parse_sarif] SARIF file at {path} is not valid JSON: {e}") + + for run in sarif.get("runs", []): + try: + rules = run["tool"]["driver"].get("rules", []) + except KeyError as e: + raise KeyError(f"[parse_sarif] SARIF file at {path} has a run missing expected field {e}") + + severities = {r["id"]: r.get("properties", {}).get("security-severity") for r in rules} + + for result in run.get("results", []): + score = severities.get(result["ruleId"]) + if score is not None: + max_score = max(max_score, float(score)) + + return EvaluationResult( + gate_failed=max_score >= GATE_FAIL_THRESHOLD, + gate_warn=GATE_WARN_THRESHOLD <= max_score < GATE_FAIL_THRESHOLD, + ) diff --git a/ci/sast_scan.py b/ci/sast_scan.py new file mode 100644 index 000000000..725ae5341 --- /dev/null +++ b/ci/sast_scan.py @@ -0,0 +1,77 @@ +import subprocess +import os +import sys +import logging + +GREEN = '\033[92m' +RED = '\033[91m' +RESET = '\033[0m' +BOLD = '\033[1m' +YELLOW = '\033[93m' + +logging.basicConfig( + level=logging.INFO, + format='%(message)s' # Clean format to prevent double-timestamps in CI logs +) +logger = logging.getLogger("sast-orchestrator") +# --- Configurable values ----------------------------------------------- +SEMGREP_CONFIG_RULESETS = os.getenv( + "SEMGREP_CONFIG_RULESETS", + " semgrep-rules/generic semgrep-rules/problem-based-packs semgrep-rules/bash " + " semgrep-rules/java auto semgrep-rules/yaml semgrep-rules/package_managers p/default" +).split() +OPENGREP_EXCLUDE = os.getenv( + "OPENGREP_EXCLUDE", + "ci/ *.sarif Dockerfile* dist/** build/** node_modules/** .angular/**" +).split() +OPENGREP_SARIF_OUTPUT = os.getenv("OPENGREP_SARIF_OUTPUT", "sast-opengrep-app.sarif") + +def run_opengrep(): + base_cmd = ["opengrep", "scan"] + \ + [f"--config {config}" for config in SEMGREP_CONFIG_RULESETS] + \ + [f"--exclude={pattern}" for pattern in OPENGREP_EXCLUDE] + + report_cmd = (base_cmd + ["--sarif", "--output", OPENGREP_SARIF_OUTPUT]) + report_cmd = " ".join(report_cmd).split() + logger.info(f"{BOLD}Running (report):{RESET} {' '.join(report_cmd)}") + subprocess.run(report_cmd) + + gate_cmd = (base_cmd + ["--severity=ERROR", "--error"]) + gate_cmd = " ".join(gate_cmd).split() + logger.info(f"{BOLD}Running (gate):{RESET} {' '.join(gate_cmd)}") + return subprocess.run(gate_cmd).returncode + +def main(): + + exit_code = run_opengrep() + logger.info("-" * 40) + + if exit_code == 0: + status = "PASSED" + elif exit_code == 1: + status = "FAILED" + else: + status = "ERROR" + + if not os.path.exists(OPENGREP_SARIF_OUTPUT): + logger.error(f"{RED}[!] opengrep SARIF missing: {OPENGREP_SARIF_OUTPUT}, tool failed to run{RESET}") + status = "ERROR" + + logger.info(f"\n{BOLD}========== SAST PIPELINE SUMMARY =========={RESET}") + if status == "PASSED": + logger.info(f"[opengrep]: {GREEN}PASSED (exit code 0){RESET}") + elif status == "FAILED": + logger.error(f"[opengrep]: {RED}FAILED (exit code 1 - error-severity findings){RESET}") + else: + logger.error(f"[opengrep]: {RED}ERROR (exit code {exit_code}, tool did not run correctly){RESET}") + logger.info(f"{BOLD}==========================================={RESET}\n") + + if status == "ERROR": + sys.exit(1) + + if status == "FAILED": + logger.error(f"{RED}SAST gate failed: blocking-severity findings present.{RESET}") + sys.exit(1) + +if __name__ == "__main__": + main() diff --git a/ci/sca_scan.py b/ci/sca_scan.py new file mode 100644 index 000000000..2c222468a --- /dev/null +++ b/ci/sca_scan.py @@ -0,0 +1,129 @@ +import subprocess +import os +import sys +import logging +import json +from parse_sarif import evaluate, GATE_FAIL_THRESHOLD, GATE_WARN_THRESHOLD + +GREEN = '\033[92m' +RED = '\033[91m' +RESET = '\033[0m' +BOLD = '\033[1m' +YELLOW = '\033[93m' + +logging.basicConfig( + level=logging.INFO, + format='%(message)s' # Clean format to prevent double-timestamps in CI logs +) +logger = logging.getLogger("sca-orchestrator") + +# Configurable values +SBOM_PATH = os.getenv("SBOM_PATH", "target/bom.json") +TRIVY_IGNOREFILE = os.getenv("TRIVY_IGNOREFILE", "ci/suppress_trivy.yaml") +OSV_IGNOREFILE = os.getenv("OSV_IGNOREFILE", "ci/suppress_osv_scanner.toml") +TRIVY_SARIF_OUTPUT = os.getenv("TRIVY_SARIF_OUTPUT", "trivy-app.sarif") +OSV_SARIF_OUTPUT = os.getenv("OSV_SARIF_OUTPUT", "osv-scanner-app.sarif") +SCA_MERGED_SARIF_OUTPUT = os.getenv("SCA_MERGED_SARIF_OUTPUT", "SCA-platform-ui-merged.sarif") + +def run_trivy(): + cmd = [ + "trivy", "sbom", SBOM_PATH, + "--format", "sarif", + "--ignorefile", TRIVY_IGNOREFILE, + "--output", TRIVY_SARIF_OUTPUT + ] + return subprocess.run(cmd).returncode + +def run_osv_scanner(): + cmd = [ + "osv-scanner", "scan", "source", + "--lockfile", SBOM_PATH, + "--config", OSV_IGNOREFILE, + "--format", "sarif", + "--output-file", OSV_SARIF_OUTPUT + ] + exit_code = subprocess.run(cmd).returncode + if exit_code == 1: + return 0 # OSV Scanner returns 1 if vulnerabilities are found, but we want to continue the pipeline + return exit_code + +def merge_sarifs(): + merged = { + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", + "version": "2.1.0", + "runs": [], + } + + for path in (TRIVY_SARIF_OUTPUT, OSV_SARIF_OUTPUT): + if not os.path.exists(path): + logger.warning(f"{path} not found, skipping in merge") + continue + with open(path) as f: + sarif = json.load(f, strict=False) + merged["runs"].extend(sarif.get("runs", [])) + + with open(SCA_MERGED_SARIF_OUTPUT, "w") as f: + json.dump(merged, f) + + logger.info("SARIF files merged successfully.") + +def main(): + + tools = {"trivy": run_trivy, "osv-scanner": run_osv_scanner} + sarif_files = {"trivy": TRIVY_SARIF_OUTPUT, "osv-scanner": OSV_SARIF_OUTPUT} + tool_status = {} # "PASSED" | "WARNING" | "FAILED" | "ERROR" + gate_failed = False + + # Run each SCA tool and collect their exit codes + for name, tool_fn in tools.items(): + exit_code = tool_fn() + logger.info("-" * 40) + + path = sarif_files[name] + if exit_code != 0 and os.path.exists(path): + logger.error(f"{RED}[!] {name} exit code {exit_code} but wrote {path}{RESET}") + tool_status[name] = "ERROR" + gate_failed = True + + merge_sarifs() # combined artifact only, not used for the gate decision + + # Evaluate each SARIF file for gate decision + for name, path in sarif_files.items(): + if name in tool_status: + continue # already flagged ERROR above, don't overwrite it + + if not os.path.exists(path): + logger.error(f"{RED}[!] {name} SARIF missing: {path},tool failed to run (not a vulnerability){RESET}") + tool_status[name] = "ERROR" + gate_failed = True + continue + + eval_result = evaluate(path) + + if eval_result.gate_failed: + tool_status[name] = "FAILED" # this tool found CVSS >= 8.0 + gate_failed = True + elif eval_result.gate_warn: + tool_status[name] = "WARNING" # this tool found 5.0 <= CVSS < 8.0 + else: + tool_status[name] = "PASSED" # this tool found nothing >= 5.0 + + # Print summary of results + logger.info(f"\n{BOLD}========== SCA PIPELINE SUMMARY =========={RESET}") + for name, status in tool_status.items(): + if status == "PASSED": + logger.info(f"[{name}]: {GREEN}PASSED{RESET}") + elif status == "WARNING": + logger.warning(f"[{name}]: {YELLOW}WARNING (findings between {GATE_WARN_THRESHOLD} and {GATE_FAIL_THRESHOLD}){RESET}") + elif status == "ERROR": + logger.error(f"[{name}]: {RED}ERROR (tool did not run correctly){RESET}") + else: + logger.error(f"[{name}]: {RED}FAILED (CVSS >= {GATE_FAIL_THRESHOLD} found){RESET}") + + # Exit with non-zero code if any tool failed the gate + if gate_failed: + logger.error(f"{RED}One or more SCA tools failed the gate check.{RESET}") + sys.exit(1) + +if __name__ == "__main__": + main() diff --git a/ci/setup-tools.sh b/ci/setup-tools.sh new file mode 100644 index 000000000..62da1437d --- /dev/null +++ b/ci/setup-tools.sh @@ -0,0 +1,167 @@ +#!/bin/bash +set -e # stop the pipeline if any command fails +set -o pipefail # Prevents silent pipeline successes if the curl download drops +set -u # treat unset variables as an error + +trap 'echo "[setup-tools] ERROR: command failed (exit $?) at line $LINENO: $BASH_COMMAND" >&2' ERR + +# Tool versions and the SHA256 of the release asset we download. +# The "# renovate:" markers let Renovate bump version and checksum together +# (see renovate.json). When overriding a *_VERSION via env, the matching +# *_SHA256 must be overridden as well or verification will fail. + +# renovate: datasource=github-release-attachments depName=aquasecurity/trivy +TRIVY_VERSION="${TRIVY_VERSION:-v0.76.0}" +TRIVY_SHA256="${TRIVY_SHA256:-e24b1c51293c1b15e4ba8d45bbc7891ba053f90278ec2698f3562c993e145fe7}" + +# renovate: datasource=github-release-attachments depName=google/osv-scanner +OSV_SCANNER_VERSION="${OSV_SCANNER_VERSION:-v2.7.0}" +OSV_SCANNER_SHA256="${OSV_SCANNER_SHA256:-160c6253e2922e9e0a6a9771c5c4c40a4b3c6090b34a3cdd5e80c96cc2e4218b}" + +# renovate: datasource=github-release-attachments depName=opengrep/opengrep +OPENGREP_VERSION="${OPENGREP_VERSION:-v1.32.0}" +OPENGREP_SHA256="${OPENGREP_SHA256:-7360d60174880bd9214f2dfbe439b7bce82b7a3d44cfe945ba833c85cc8bda75}" + +# renovate: datasource=git-refs depName=https://github.com/semgrep/semgrep-rules +SEMGREP_RULES_REF="${SEMGREP_RULES_REF:-17e31e11ebac99fb7dfe1cd288f7a32151b6f3f9}" +SEMGREP_RULES_DIR="semgrep-rules" + +# renovate: datasource=github-release-attachments depName=hadolint/hadolint +HADOLINT_VERSION="${HADOLINT_VERSION:-v2.15.1}" +HADOLINT_SHA256="${HADOLINT_SHA256:-c7187db94eeeeca956519a6af171adc31453941a1e777961f6e680f697c8c507}" + +# renovate: datasource=github-release-attachments depName=gitleaks/gitleaks +GITLEAKS_VERSION="${GITLEAKS_VERSION:-v8.30.1}" +GITLEAKS_SHA256="${GITLEAKS_SHA256:-551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb}" + +# renovate: datasource=npm depName=@cyclonedx/cyclonedx-npm +CYCLONEDX_NPM_VERSION="${CYCLONEDX_NPM_VERSION:-6.0.1}" + +TMP_DIR="$(mktemp -d)" +trap 'rm -rf "${TMP_DIR}"' EXIT + +# --- Flag parsing ----------------------------------------------------- +INSTALL_TOOL="none" +SBOM_ECOSYSTEM="none" + +while [[ $# -gt 0 ]]; do + case "$1" in + --install-tool) + [[ $# -ge 2 ]] || { echo "[setup-tools] --install-tool requires a value (e.g. trivy,osv-scanner|all)" >&2; exit 1; } + INSTALL_TOOL="$2" + shift 2 + ;; + --sbom-ecosystem) + [[ $# -ge 2 ]] || { echo "[setup-tools] --sbom-ecosystem requires a value (e.g. maven|npm|none)" >&2; exit 1; } + SBOM_ECOSYSTEM="$2" + shift 2 + ;; + *) + echo "[setup-tools] Unknown flag: $1" >&2 + exit 1 + ;; + esac +done + +should_install() { + [[ "$INSTALL_TOOL" == "all" || ",$INSTALL_TOOL," == *",$1,"* ]] +} + +# Download a file and refuse to proceed unless its SHA256 matches the pinned one +download_and_verify() { + local url="$1" dest="$2" sha256="$3" + curl -fsSL --retry 3 "${url}" -o "${dest}" + echo "${sha256} ${dest}" | sha256sum -c - +} + +# --- Trivy -------------------------------------------------------------- +if should_install "trivy"; then + echo "[setup-tools] Installing Trivy ${TRIVY_VERSION}" + TRIVY_TARBALL="trivy_${TRIVY_VERSION#v}_Linux-64bit.tar.gz" + download_and_verify \ + "https://github.com/aquasecurity/trivy/releases/download/${TRIVY_VERSION}/${TRIVY_TARBALL}" \ + "${TMP_DIR}/${TRIVY_TARBALL}" \ + "${TRIVY_SHA256}" + sudo tar -xzf "${TMP_DIR}/${TRIVY_TARBALL}" -C /usr/local/bin trivy + trivy --version + echo "Trivy installed OK" +fi + +# --- OSV Scanner ---------------------------------------------------------- +if should_install "osv-scanner"; then + echo "[setup-tools] Installing OSV Scanner ${OSV_SCANNER_VERSION}" + download_and_verify \ + "https://github.com/google/osv-scanner/releases/download/${OSV_SCANNER_VERSION}/osv-scanner_linux_amd64" \ + "${TMP_DIR}/osv-scanner" \ + "${OSV_SCANNER_SHA256}" + sudo install -m 0755 "${TMP_DIR}/osv-scanner" /usr/local/bin/osv-scanner + osv-scanner --version + echo "OSV Scanner installed OK" +fi + +# --- OpenGrep ------------------------------------------------------------- +if should_install "opengrep"; then + echo "[setup-tools] Installing OpenGrep ${OPENGREP_VERSION}" + download_and_verify \ + "https://github.com/opengrep/opengrep/releases/download/${OPENGREP_VERSION}/opengrep_manylinux_x86" \ + "${TMP_DIR}/opengrep" \ + "${OPENGREP_SHA256}" + sudo install -m 0755 "${TMP_DIR}/opengrep" /usr/local/bin/opengrep + opengrep --version + echo "OpenGrep installed OK" +fi + +# --- Semgrep community ruleset (cloned, not registry) --------- +if should_install "semgrep-rules"; then + echo "[setup-tools] Cloning semgrep-rules @ ${SEMGREP_RULES_REF}" + rm -rf "${SEMGREP_RULES_DIR}" + git clone --quiet https://github.com/semgrep/semgrep-rules.git "${SEMGREP_RULES_DIR}" + git -C "${SEMGREP_RULES_DIR}" checkout --quiet "${SEMGREP_RULES_REF}" + echo "semgrep-rules ready at ${SEMGREP_RULES_DIR} (ref: ${SEMGREP_RULES_REF})" +fi + +# --- Hadolint --------------------------------------------------------- +if should_install "hadolint"; then + echo "[setup-tools] Installing Hadolint ${HADOLINT_VERSION}" + download_and_verify \ + "https://github.com/hadolint/hadolint/releases/download/${HADOLINT_VERSION}/hadolint-linux-x86_64" \ + "${TMP_DIR}/hadolint" \ + "${HADOLINT_SHA256}" + sudo install -m 0755 "${TMP_DIR}/hadolint" /usr/local/bin/hadolint + hadolint --version + echo "Hadolint installed OK" +fi + +# --- Gitleaks ----------------------------------------------------------- +if should_install "gitleaks"; then + echo "[setup-tools] Installing Gitleaks ${GITLEAKS_VERSION}" + GITLEAKS_TARBALL="gitleaks_${GITLEAKS_VERSION#v}_linux_x64.tar.gz" + download_and_verify \ + "https://github.com/gitleaks/gitleaks/releases/download/${GITLEAKS_VERSION}/${GITLEAKS_TARBALL}" \ + "${TMP_DIR}/${GITLEAKS_TARBALL}" \ + "${GITLEAKS_SHA256}" + sudo tar -xzf "${TMP_DIR}/${GITLEAKS_TARBALL}" -C /usr/local/bin gitleaks + gitleaks version + echo "Gitleaks installed OK" +fi + +# --- SBOM generation ---------------------------------------------------- +case "$SBOM_ECOSYSTEM" in + maven) + echo "Generating SBOM for Maven project" + mvn -B -ntp dependency:resolve -q + mvn -B -ntp org.cyclonedx:cyclonedx-maven-plugin:makeAggregateBom -q + ;; + npm) + echo "Generating SBOM for NPM project" + npm ci + npx --yes "@cyclonedx/cyclonedx-npm@${CYCLONEDX_NPM_VERSION}" --output-file target/bom.json + ;; + none) + echo "No SBOM generation needed" + ;; + *) + echo "Unknown SBOM_ECOSYSTEM: $SBOM_ECOSYSTEM" >&2 + exit 1 + ;; +esac diff --git a/ci/suppress_gitleaks.toml b/ci/suppress_gitleaks.toml new file mode 100644 index 000000000..939307ae2 --- /dev/null +++ b/ci/suppress_gitleaks.toml @@ -0,0 +1,6 @@ +# No allowlist needed currently. +# Add an [allowlist] section here if false positives need excluding later: +# [allowlist] +# paths = [ +# '''test/fixtures/.*''', +# ] diff --git a/ci/suppress_osv_scanner.toml b/ci/suppress_osv_scanner.toml new file mode 100644 index 000000000..a5b0a367c --- /dev/null +++ b/ci/suppress_osv_scanner.toml @@ -0,0 +1 @@ +# Ignored vulnerabilities for osv-scanner. Add [[IgnoredVulns]] entries with id, ignoreUntil and reason. diff --git a/ci/suppress_trivy.yaml b/ci/suppress_trivy.yaml new file mode 100644 index 000000000..a7468a2ca --- /dev/null +++ b/ci/suppress_trivy.yaml @@ -0,0 +1,4 @@ +# No active suppressions. Add entries as: +# - id: CVE-XXXX-YYYY +# statement: "why it is safe" +vulnerabilities: [] diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 000000000..163bee0e5 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +set -eu + +escape_js() { + printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' +} + +MIP_VERSION_ESCAPED=$(escape_js "${MIP_VERSION:-}") +NOTEBOOK_ENABLED_ESCAPED=$(escape_js "${NOTEBOOK_ENABLED:-0}") +JUPYTER_CONTEXT_ESCAPED=$(escape_js "${JUPYTER_CONTEXT:-notebook}") +GUIDE_COVARIATE_ESCAPED=$(escape_js "${GUIDE_COVARIATE:-Sex}") +GUIDE_VARIABLE_ESCAPED=$(escape_js "${GUIDE_VARIABLE:-Age}") + +envsubst '$PLATFORM_BACKEND_SERVER $PLATFORM_BACKEND_CONTEXT $NOTEBOOK_ENABLED $JUPYTER_SERVER $JUPYTER_CONTEXT' \ + < /etc/nginx/templates/default.conf.template \ + > /etc/nginx/conf.d/default.conf + +mkdir -p /usr/share/nginx/html/assets + +cat > /usr/share/nginx/html/assets/env.js < Angular[Angular standalone app] + Angular --> Router[Angular Router + Guards] + Router --> Studio[Experiment Studio] + Router --> Dashboard[Experiments Dashboard] + Router --> Terms[Terms/NDA Page] + Router --> Account[Account Page] + Router --> Notebook[Notebook Page] + Studio --> StudioService[ExperimentStudioService] + Dashboard --> DashboardService[ExperimentsDashboardService] + StudioService --> API[Same-origin /services API] + DashboardService --> API + API --> Backend[MIP backend] + Notebook --> Jupyter[/Notebook proxy when enabled/] +``` + +## UI Flow +- Default route redirects to `/experiments-dashboard`. +- Main routes are `/experiments-dashboard`, `/experiment-studio`, `/account`, `/terms`, and optional `/notebook`. +- Most routes use `AuthGuard` and `TermsGuard`; `/terms` uses `AuthGuard`; `/notebook` is additionally gated by a runtime `NOTEBOOK_ENABLED` match check. +- The app shell lives in `app.component.*` and wraps pages with shared header/footer components. + +## Data and API Flow +- API calls use relative `/services/...` URLs so local proxy and nginx deployment routing remain consistent. +- `src/proxy.conf.json` proxies `/services` to `http://localhost:8080` and `/notebook/` to `http://localhost:8000` during local development. +- `auth.interceptor.ts` adds `withCredentials` for same-origin API calls. +- `app.config.ts` configures `HttpClient` with the credentials interceptor and XSRF cookie/header names. +- Main backend endpoints visible in the frontend: + - `GET /services/activeUser` + - `POST /services/activeUser/agreeNDA` + - `GET /services/data-models` + - `GET /services/specifications/inputdata` + - `GET /services/specifications/preprocessing` + - `GET /services/specifications/algorithms` + - `GET /services/experiments` + - `GET /services/experiments/:id` + - `POST /services/experiments` (`{ runId, name }`: Save experiment from a kept run, without re-running) + - `POST /services/experiments/preview` (stateless previews; nothing is stored) + - `POST /services/experiments/runs` (studio Run; keeps the result for saving) + - `PATCH /services/experiments/:id` + - `DELETE /services/experiments/:id` + - `GET|POST|PATCH|DELETE /services/experiment-folders/**` (folders, members, and sets) + +## Authentication and Terms +- `AuthService` checks the current session with `GET /services/activeUser`. +- Login redirects to `/services/oauth2/authorization/keycloak?frontend_redirect=...`. +- Logout hard-redirects to `/services/logout`. +- Auth redirect state is stored in localStorage with `redirect_url`. +- `TermsGuard` routes users to `/terms` when `user.agreeNDA` is false. +- `TermsService` stores the intended terms destination in localStorage with `tos_redirect_url`. +- The terms page loads `src/assets/tos.md` and posts acceptance to `/services/activeUser/agreeNDA`. + +## Module Boundaries +- Page components own feature presentation and local UI state. +- Services under `src/app/services` own cross-component state, backend calls, orchestration, and persistence to browser storage. +- Models under `src/app/models` define backend/frontend DTOs and interfaces. +- Core mapping and constants live under `src/app/core`. +- Visualization registries and renderers live under `src/app/pages/experiment-studio/visualisations`. +- Shared app chrome and generic helpers live under `src/app/pages/shared`. + +## State Management +- 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`. +- `ExperimentsDashboardService` owns paged experiment list state, filtering, sharing, renaming, deletion, and result fetches. +- `ErrorService` shares global feature errors through a `BehaviorSubject`. + +## Visualization and Export +- Algorithm result table rendering is driven by `AlgorithmTableRegistry`. +- Chart rendering is driven by `AlgorithmChartRegistry` and renderer files under `visualisations/charts/renderers`. +- Backend algorithm definitions are mapped in `src/app/core/algorithm-mappers.ts`. +- Result labels and enum mappings are handled by `ExperimentLabelService` and `algorithm-result-enum-mapper`. +- Exports: `PdfExportService` builds the experiment report, the descriptive statistics PDF and the variable chart PDF on one flowing layout (setup block, contents list, then tables and charts, two charts per page). Charts are captured as JPEG through `core/export.utils.ts` (the ECharts instance's own export, then SVG, then an html2canvas screenshot as a last resort); PDFs are compressed and embed the logo once. `CsvExportService` writes single-table CSVs with a UTF-8 BOM and the result ZIP (`tables/NN_*.csv`, unrounded `result.json`, `README.txt` with the setup). JSZip is loaded on demand. + +## Runtime and Deployment +- The Dockerfile builds with Node 22 and serves the Angular output with nginx. +- `docker-entrypoint.sh` writes `/usr/share/nginx/html/assets/env.js` from runtime environment variables. +- Runtime env values include backend proxy context/server, MIP version, notebook settings, and guided tutorial defaults. +- `nginx.conf.template` proxies `/${PLATFORM_BACKEND_CONTEXT}/` to the backend and `/${JUPYTER_CONTEXT}/` to Jupyter only when notebook support is enabled. + +## Known Gaps +- No lint or format scripts are defined. `npm run typecheck` and `npm run check:dead-code` cover types and dead code. +- E2E flows (Playwright, `e2e/`) run against a mocked `/services` API; no e2e run exercises a real backend. +- Many authenticated experiment flows require a running backend and Keycloak-compatible environment for full validation. diff --git a/docs/context/code-review-checklist.md b/docs/context/code-review-checklist.md new file mode 100644 index 000000000..841b9ea15 --- /dev/null +++ b/docs/context/code-review-checklist.md @@ -0,0 +1,44 @@ +# Code Review Checklist + +## Correctness +- [ ] Does the change solve the stated problem? +- [ ] Are affected routes, services, and components updated consistently? +- [ ] Are backend DTOs and frontend models/mappers compatible? +- [ ] Are edge cases and error paths handled? + +## Tests +- [ ] Are relevant specs added or updated? +- [ ] Were the correct commands run? +- [ ] Is manual browser QA documented for authenticated or backend-dependent flows? +- [ ] Are skipped checks explained with reason and risk? + +## Architecture +- [ ] Does the change respect page/service/model/core boundaries? +- [ ] Is orchestration kept in services rather than scattered across components? +- [ ] Are visualization changes registered through the existing table/chart registries? +- [ ] Are runtime env reads kept in the existing runtime env layer? + +## Security and Privacy +- [ ] No secrets, tokens, cookies, credentials, or private env values are committed. +- [ ] Auth, NDA, sharing, deletion, and notebook access behavior is preserved or explicitly reviewed. +- [ ] Sensitive user or experiment data is not logged. +- [ ] Redirect behavior remains same-origin and intentional. + +## UI and Accessibility +- [ ] Visible UI changes follow `DESIGN.md`. +- [ ] Text, controls, and layouts work at relevant desktop/mobile sizes. +- [ ] Interactive controls have clear labels/states. +- [ ] Existing keyboard and screen-reader affordances are not degraded. + +## Maintainability +- [ ] `npm run verify` passes (typecheck, dead-code scan, build). +- [ ] The change is small enough to review. +- [ ] Naming and structure match the repo. +- [ ] No unrelated refactors or formatting churn were introduced. +- [ ] Documentation/context files are updated if commands, architecture, or conventions changed. + +## Release Risk +- [ ] Docker/nginx/runtime env changes are smoke-tested or clearly flagged. +- [ ] Dependency changes include lockfile review and build/test results. +- [ ] Rollback considerations are included for runtime behavior changes. +- [ ] Human review is requested for high-risk areas in `risk-register.md`. diff --git a/docs/context/commands.md b/docs/context/commands.md new file mode 100644 index 000000000..710ba070f --- /dev/null +++ b/docs/context/commands.md @@ -0,0 +1,90 @@ +# Commands + +## Package Manager +Detected: npm. + +Evidence: +- `package-lock.json` exists. +- `package.json` defines npm scripts. +- Dockerfile uses `npm ci` when a lockfile exists. + +## Install +```bash +npm ci +``` + +Use Node 22+ and npm 10+. + +## Run Locally +```bash +npm start +``` + +Runs `ng serve`. `angular.json` configures `src/proxy.conf.json` for the dev server: +- `/services` -> `http://localhost:8080` +- `/notebook/` -> `http://localhost:8000` + +## Build +```bash +npm run build +``` + +Runs `ng build` with production as the default configuration. Build output is `dist/fl-platform`. + +Development watch build: +```bash +npm run watch +``` + +## Test +```bash +npm test +``` + +Runs the Vitest specs through Angular CLI in headless Chromium. `npm test -- --watch=false` runs once. + +## Typecheck / Static Checks +No lint or format script is defined in `package.json`. `npm run verify` runs the typecheck, the dead-code scan and a production build; `docs/context/testing.md` says what each one covers. + +## Database / Migrations +Not applicable in this frontend repository. + +## Docker +Build: +```bash +docker build -t fl-platform . +``` + +Run: +```bash +docker run -e PLATFORM_BACKEND_SERVER=platform-backend-service:8080 -e PLATFORM_BACKEND_CONTEXT=services -p 80:80 fl-platform +``` + +Runtime env values are written to `assets/env.js` by `docker-entrypoint.sh`. + +## Output and Runtime Budget Guardrails + +Do not run these without explicit user confirmation unless the user directly requested them and the cost is stated first: +- `npm start`, `npm run watch`, `ng serve`, `ng build --watch`: long-running server/watch commands. +- `npm test` / `ng test`: Vitest watches in a terminal; use `--watch=false` and `--include ` for a bounded run. +- `docker build`, `docker run`, `docker compose ...`, unbounded `docker logs`: heavy or noisy container operations. +- `npm ci`, `npm install`, `npm audit`, `npm audit --json`, `npm outdated`: network/dependency operations that can be slow or noisy. +- Broad repository output: `find .`, `ls -R`, `tree`, recursive `grep`, unrestricted `rg`, `cat package-lock.json`, `cat dist/*`, `cat coverage/*`, full `git diff`, `git log -p`, or large `git show`. + +Preferred bounded alternatives: +```bash +git status --short +git diff --stat +git diff --name-only +rg "pattern" src docs -g "!node_modules" -g "!dist" -g "!coverage" +sed -n "1,160p" path/to/file +``` + +If a high-output command is necessary, first tell the user why, state that it may consume substantial tokens/runtime, and use output caps or path filters. + +## CI +Detected workflows: +- `.github/workflows/publish_images.yml`: on published releases, builds and pushes Docker images to Docker Hub and EBRAINS Harbor. +- `.github/workflows/ebrains.yml`: mirrors `master` and tags to EBRAINS GitLab. + +- `.github/workflows/checks.yml`: on pull requests, runs `npm run verify` (typecheck, dead-code scan, build). diff --git a/docs/context/conventions.md b/docs/context/conventions.md new file mode 100644 index 000000000..ee621fc5c --- /dev/null +++ b/docs/context/conventions.md @@ -0,0 +1,52 @@ +# Conventions + +## Confirmed Conventions + +### Angular and TypeScript +- The app uses Angular 22 standalone components. +- `app.config.ts` provides router, HTTP client, XSRF, zoneless change detection, and ECharts. +- TypeScript is strict. `tsconfig.json` enables strict templates, no unused locals/parameters, no implicit returns, and related checks. +- Component style language is CSS. +- Tests use Vitest and Angular `TestBed`. + +### State and Services +- Signals are the dominant state primitive for feature state. +- Services use `inject()` heavily for dependencies. +- `ExperimentStudioService` owns experiment composition state and persistence to sessionStorage. +- `ExperimentsDashboardService` owns paged dashboard state and experiment list mutations. +- `ErrorService` is used for global feature errors. + +### Routing and Access +- Routes are lazy-loaded with `loadComponent`. +- Route guards enforce auth, terms/NDA acceptance, and selected onboarding behavior. +- Optional notebook access is gated both in Angular routing and nginx runtime config. + +### API and Runtime Config +- API calls use same-origin relative `/services/...` URLs. +- The credentials interceptor adds `withCredentials` for same-origin API calls. +- Runtime env is read from `window.__env`, generated by `docker-entrypoint.sh` in containers. +- `RuntimeEnvService` is the preferred abstraction for app code that needs runtime env values. + +### UI and Styling +- Use component CSS for feature-specific styles. +- Use `src/styles.css` for app-wide styles and QueryBuilder theming. +- A component may list several stylesheets in `styleUrls`; Angular concatenates them **in array order**, so that order is the cascade. Splitting a file must preserve the original order and must not move a rule across another rule that shares its selector — `statistic-analysis-panel` is split this way (`.component.css` shell + pipeline canvas, then `.preprocessing.css`, then `.results.css`). +- Consult `DESIGN.md` before visual changes. +- Existing branding assets live under `src/assets`. + +### Visualization and Algorithm Results +- Backend algorithm definitions are mapped in `src/app/core/algorithm-mappers.ts`. +- Result table and chart rendering use registry patterns under `src/app/pages/experiment-studio/visualisations`. +- Legacy algorithm keys may exist for historical stored experiment compatibility. + +### Testing Patterns +- Specs are colocated with source files. +- Component tests commonly use `TestBed`, service stubs, and Signals. +- Add focused regression tests for mapper, registry, and service behavior changes. + +## Recommended Conventions To Verify +- Add dedicated `lint`, `format`, and `typecheck` scripts if the team wants separate quality gates. +- Add a pull-request CI workflow that runs at least `npm ci`, `npm run build`, and `npm test`. +- Establish shared fixture builders for repeated backend experiment/result payloads. +- Prefer typed interfaces over `any` in new code where existing backend payload uncertainty does not require flexibility. +- Add e2e coverage for login-protected flows if a stable test identity/backend environment is available. diff --git a/docs/context/decision-log.md b/docs/context/decision-log.md new file mode 100644 index 000000000..1e7e1f924 --- /dev/null +++ b/docs/context/decision-log.md @@ -0,0 +1,217 @@ +# Decision Log + +Use this file for durable architectural decisions. + +## Template + +### Decision: + +Status: Proposed / Accepted / Deprecated + +Context: + +Decision: + +Consequences: + +Files affected: + +Date: + +## Inferred / Verify Decisions + +### Decision: Use Angular standalone application structure + +Status: Inferred / verify + +Context: `angular.json`, `app.config.ts`, and routes use standalone Angular entrypoints and lazy `loadComponent`. + +Decision: Keep new UI code in standalone components and configure app-wide providers in `app.config.ts`. + +Consequences: Avoid introducing NgModules unless there is a clear compatibility reason. + +Files affected: `src/main.ts`, `src/app/app.config.ts`, `src/app/app.routes.ts`. + +Date: Unknown / TODO: verify. + +### Decision: Use zoneless change detection + +Status: Inferred / verify + +Context: `app.config.ts` calls `provideZonelessChangeDetection()`. + +Decision: Prefer Signal-driven and explicit reactive patterns that work under zoneless change detection. + +Consequences: Be careful with code that assumes Zone.js-triggered view updates. + +Files affected: `src/app/app.config.ts`, component/service state code. + +Date: Unknown / TODO: verify. + +### Decision: Keep backend API under same-origin `/services` + +Status: Inferred / verify + +Context: Services call `/services/...`; local proxy and nginx route this path to the backend. + +Decision: Keep frontend API calls relative to `/services` instead of hard-coding backend hosts. + +Consequences: Local dev, nginx deployment, credentials, and XSRF behavior stay aligned. + +Files affected: `src/proxy.conf.json`, `nginx.conf.template`, `src/app/services/*`. + +Date: Unknown / TODO: verify. + +### Decision: Inject runtime config through `assets/env.js` + +Status: Inferred / verify + +Context: `docker-entrypoint.sh` writes `assets/env.js`; `RuntimeEnvService` reads `window.__env`. + +Decision: Keep deployment-specific values out of the compiled Angular bundle when possible. + +Consequences: Container runtime can change backend/notebook/version settings without rebuilding the app. + +Files affected: `docker-entrypoint.sh`, `src/assets/env.js`, `src/app/services/runtime-env.service.ts`. + +Date: Unknown / TODO: verify. + +### Decision: Step 0 of the Data Handling Pipeline is a read-only source snapshot + +Status: Accepted + +Context: The pipeline canvas started at `1. Cohort Filtering`, so the earliest preview a user could reach (`Raw Summary`) was already the filtered cohort. Nothing showed the selection before any rule was applied. + +Decision: Add pipeline node `0. Source Data` to the statistic analysis panel: a permanent read-only node that describes the selected variables with no filter payload and no preprocessing steps. It renders the shared summary workspace - the `#summaryWorkspace` template the Raw and Processed previews use (variable browser, statistics, charts, histograms, PDF/CSV) - fed by its own `SummaryView`, with the exit copy and the empty-state line passed through the outlet context. It is deliberately not a stage: it never enters `addedSteps`, the stage counter, or the experiment request, and it exposes no rule editor. + +Consequences: Step 0 costs no new presentation code and the Processed summary now renders through the same outlet, so a summary change lands once for all three surfaces; each surface keeps its own `data-guide` anchors through `SUMMARY_GUIDE_ANCHORS`. Both `loadDescriptiveOverview` and `getAlgorithmResults` / `buildRequestBody` gained a trailing `includeFilters` flag (default `true`) so the numeric histogram tab cannot show the filtered cohort under a read-only "filters are not applied" heading. One extra on-demand `/services/experiments/transient` describe per opened snapshot, fetched on open and refetched when the selected variables or datasets change while it is open; the cohort filter and the applied preprocessing are deliberately outside that cache key because the request omits both. Payload shapes are unchanged; the flag only omits `inputdata.filters`, so Raw and Processed keep their filtered behaviour. Because two live instances would double every count-based DOM query and hand the User Guide anchors to a hidden surface, the step-0 instance renders only while open and keeps no `data-guide` anchors. + +Files affected: `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.*`, `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.pipeline.spec.ts`, `src/app/services/experiment-studio.service.ts`. + +Date: 2026-09-08. + +### Decision: The dashed pipeline card means "contributes nothing", not "not customised" + +Status: Deprecated — superseded by "Every pipeline step is a solid card that toggles from its header". + +Context: Preprocessing rendered as a dashed dormant card until the user opened it, carrying a `Default: Missing NaN removal` pill and a `Default Active` badge. That was visually indistinguishable from Filtering and Transformation, which really do nothing until added, even though MIP drops rows with missing values on every run whether or not anyone opens the stage. Growing a stage also required expanding it first to reach the dashed add button inside the station. + +Decision: Make the card frame describe the request rather than the effort. A node wears the dashed card only when it will contribute nothing to the next run (Filtering, Transformations). Preprocessing always renders as an added node and its collapsed sub-node rail tags the untouched handler `Default` (`PipelineSubNode.statusTone: 'default'`) instead of `Applied`. The same rail gains dashed rows (`addablePreprocessingSlots`, the transformation `Add derived column` row) that add and open their sub-step in one click. The editor body stays gated on `isStepAdded('setup')`, so the untouched state costs no station DOM, and the canvas counter reads `of 3 stages configured` because a default is not user work. + +Consequences: `is-dormant` no longer applies to `setup`, so anything that identified that stage by dormancy has to target `.pipeline-subnode-item` instead; the rail's `default` tone is now the only "untouched" marker and `.pipeline-default-pill` / `.pipeline-node-title-row` are gone. The counter can legitimately read `0 of 3 stages configured` under a solid Preprocessing card. No request payload, endpoint, or guard changes; authenticated pipeline behaviour still needs manual browser QA. + +Files affected: `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.*`, `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.pipeline.spec.ts`, `src/app/pages/experiment-studio/guide/experiment-studio-guide.content.ts`. + +Date: 2026-09-08. + +### Decision: The Execution step describes the run from a snapshot, not from live state + +Status: Accepted + +Context: The result view sat in the left column of `.experiment-panel-workspace--result`, whose second column (`minmax(260px, 320px)`) has been reserved since the docked-summary commit but never filled, so the right-hand third of the step showed nothing. Nothing on the step said which dataset, roles, filters, preprocessing, or parameters produced the chart. Live studio state cannot answer that: changing the algorithm or the y/x roles clears `runResult`, but changing a **parameter**, a **cohort filter**, or the **preprocessing** of a step does not, so a summary read from live signals would describe a run that never happened. + +Decision: `ExperimentStudioService.captureRunSetup(finalAlgorithmName)` freezes `{ data model, datasets, y/x codes, filterLogic, effective preprocessing rows, configured parameters }` the moment the algorithm panel has stored the final parameter values and is about to dispatch the run; `runSetup` holds it beside `runResult`, and both are cleared together and neither is persisted. The new `app-experiment-setup-summary` (a `<aside>` in the reserved column, sticky under the header + sub-header pair, scrolling inside its own card) renders only that snapshot, labels codes at render time through the existing label maps, and reuses the existing truth sources: `getEffectivePreprocessingEntries` for preprocessing (the request's own resolution, so the default missing-value removal appears and is labelled `Missing values` rather than implied), `withLabels` for variables, and the algorithm `configSchema` for parameter labels and option labels. The first row is labelled **Pathology**, not "Data model": the Data Exploration chip says `Select pathology`, the tour step is "Pathology & Datasets", and `Pathology / Stroke 3.7` reads as one name. The snapshot keeps the record-level `dataModelLabel` / `dataModelCode` because that is the record it copies, and the dashboard card keeps its own older word, "Domain". `formatAlgorithmParameterValue` (display) sits next to `serializeAlgorithmParameterValue` (request) in `core/algorithm-parameter.utils.ts`; the rule sentence comes from the new `core/filter-display.utils.ts`. Every block links to the step that owns it (`variables-top`, `filters`, `setup`, `algorithm-section`) through `ExperimentStudioNavigationService`. The dashboard's Configuration card keeps its private filter formatter for now — adopting the core util changes its card and PDF text, which is a separate review. + +Consequences: A result's summary cannot drift from the result, and `getEffectivePreprocessingSummary` is now a string wrapper over the same rows, so both read one resolution path. The aside is outside `#resultSection`, so **Export PDF** is byte-identical; `Export CSV`, run payloads, API shapes, and guards are untouched. `data-guide="experiment-setup-summary"` is inert until the studio tour gains a step for it. Below the 1100px workspace breakpoint the column drops under the result and unsticks. Needs authenticated browser QA: a run, then an edit-and-return, to confirm the rail still names the values that produced the chart. + +Files affected: `src/app/models/experiment-run-setup.model.ts`, `src/app/core/filter-display.utils.ts`, `src/app/core/algorithm-parameter.utils.ts`, `src/app/services/experiment-studio.service.ts`, `src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts`, `src/app/pages/experiment-studio/execution-panel/**`. + +Date: 2026-09-08. + +### Decision: Studio charts must survive renders while their step is hidden + +Status: Accepted + +Context: The four Studio steps stay mounted and are switched with `display: none` (`.studio-section`), so a D3 chart can be asked to draw while it measures 0x0. Two things made that routine rather than rare: `[config]="{ color: ... }"` literals hand `app-histogram` a fresh object on every change-detection pass, which re-runs its render effect, and any signal written while a step is hidden (closing the pathology popover through the `document:click` listener, changing the variable pool from the Algorithm step, a histogram response arriving after the user moved on) causes such a pass. Drawing into an unlaid-out container is not neutral: `getBBox()` reports an empty rect, so the height fit collapsed the histogram to a few pixels, and the ResizeObserver pattern used by the metadata browsers compared against a size recorded *before* the draw, so the redraw on return was skipped. The result was a Data Exploration step that looked closed and showed nothing until the variable was reselected. + +Decision: Treat a zero-size box as "not renderable", never as a render target, and make every draw idempotent. `HistogramComponent` skips the draw when the container measures no width, remembers what the drawing in the DOM was built from (`data` reference, config values) and the width it measured, and only redraws when one of those changes; a draw request it cannot honour is remembered by forgetting the rendered width. A `ResizeObserver` on the same container, outside the zone and debounced like the sibling charts, is the redraw path for the step becoming visible, for a first render behind a hidden deep link, and for window resizes; it compares the width only, because the draw writes an inline height. Call sites pass stable config objects (`chartConfig()` / `histogramChartConfig`) instead of template literals, and `createHistogram` guards every `getBBox()`-derived measurement so an unlaid-out container degrades to the computed height rather than to nothing. The metadata browsers keep their observers but compare against the size of the last actual draw. Charts are re-drawn from cached data only: returning to a step never re-requests from `/services`, so a chart stays the snapshot of the moment its variable was selected. + +Consequences: Chart renders are cheap enough to leave on a plain change-detection pass, so config objects must keep a stable identity — a new object literal in a template reintroduces the per-pass rebuild. Each chart now owns one piece of state describing the drawing it currently holds, which is the thing to update whenever a draw path is added. Charts drawn before their data is measurable wait for the observer, so tests need to outlast the 150ms debounce. No route, service, storage, API or payload change. + +Files affected: `src/app/pages/experiment-studio/visualisations/histogram/**`, `src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.*`, `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.*`, `src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.ts`, `src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.ts`. + +Date: 2026-09-09. + +### Decision: The derived-column gate blocks what the user can fix, and only that + +Status: Deprecated — superseded by "The derived-column stage has no Preview/Apply gate at all". + +Context: The Transformation stage used to refuse Preview/Apply while any started card was unfinished. That is right for a card the user is typing into and wrong for a card the app merely *loaded*: a stored experiment can carry a category rule this builder cannot author, and under the old gate such a rule disabled both buttons over a card whose message told the user to fix something they had already written. The two were indistinguishable because the stage only had one kind of message. Day one of the filter round-trip work (shared operator table, bare-condition hydration, unary `value: null`) removed the shapes the builder could not read; the gate itself was taken out as a temporary unblock and left `Preview`/`Apply` permanently live, which traded a stuck stage for a half-typed column reaching exaflow and failing there. + +Decision: One list, two classes of entry. `transformationBlockingIssues()` reports each card's problems and marks each `blocking` or not. A blocking entry always names a fix that exists on that card — a missing column name, a name two cards share, a named category with no filter, a condition the builder itself rejects — and drives `previewDisabled`/`applyDisabled`, the stage-level `role="alert"` list, and `holdTransformationOnCreate()`, which keeps the stage on Create and expands the blocked cards so a folded card cannot hide its own fix. A non-blocking entry is a rule the builder could not read (`exportFilterLogic()` empty while `unloadedInput()` reports stored rules): the commit keeps the stored filter, the card says so, and the stage still applies. The entry the old gate had beside these — category filters must reference a *selected* variable — is gone for good: a stored rule may point at a CDE outside the pool, which cannot be selected from this card. + +Consequences: Apply is destructive for neither kind of rule, and Preview cannot describe a column the cards cannot name. A builder error blocks, which means an off-pool variable renders its free-text value box and the user can still satisfy the condition — verified by the specs, not yet in a browser against a stored experiment. `transformationBlockingIssues()` gained a `blocking` field; the `transformationApplyError` field is gone and the stage list is the single place a refused Apply speaks. `docs/context/risk-register.md` names the two ways to reintroduce the old failure. + +Files affected: `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.*`, `docs/context/risk-register.md`, `docs/context/module-index.md`. + +Date: 2026-09-14. + +### Decision: The derived-column stage has no Preview/Apply gate at all + +Status: Accepted + +Context: Browser feedback on a stored experiment. A card the app had merely loaded — a derived `Pro` column whose `old`/`yound` categories carry no filter this UI can show — was answered with a stage-level `Fix before Preview or Apply` alert, a red row on the card, and both actions dimmed. Nothing on that card could clear it: the card was authored elsewhere, and the user cannot tell a fact about stored data apart from a mistake they are mid-way through typing. The superseded decision narrowed the gate to "what the user can still fix"; the field report is that the distinction does not survive a real experiment, because an unfinished loaded card is not a fix in progress. + +Decision: Remove the gate and every reference to it. `commitTransformationRuleFilters()` returns `void` and keeps the one rule-safety invariant that mattered — a rule is rewritten only when its builder can speak for it, so a builder that rejects its own condition, or that could not read what the store holds, leaves that rule untouched instead of exporting an empty filter. Preview commits and describes; Apply commits and folds the stage. Deleted: `transformationBlockingIssues()`, `transformationHasBlockingIssues`, `transformationBlockingMessages()`, `transformationDraftIssues()`, `holdTransformationOnCreate()`, `expandInvalidTransformationDrafts()`, `transformationRuleFilterErrors`, the orphan `transformationIsValid`, the `role="alert"` list, the per-card `row-error`s under the category rules, and the `[previewDisabled]`/`[applyDisabled]` bindings on the Transformation station — the shared bar lost its `previewDisabled` input with them, since no station passes it. What replaces the gate is three facts, each stated where it is knowable. A rule row whose builder reports `unloadedInput()` carries its own note ("A saved filter on this category could not be opened in this builder. It is applied exactly as stored; rebuild the rule only to replace it."): the builder owns that fact, so the note is there before the first Preview and asks for no fix. Apply dims on `transformationCanApply` alone — true when at least one card forms a config — which is a statement about having nothing to write and never about what a loaded rule says. And the stage folds after Apply only when `transformationHasPendingChange` is false, so a fold cannot hide the chip that is still reporting unfinished work. + +Consequences: An unfinished card now behaves like every other optional station: `buildTransformationConfigForDraft()` drops an unfiltered category, so the describe and the persisted payload cover the complete cards only, while the card keeps its `Pending` chip and the stage keeps `Pending` — with the stage kept open, those chips are the whole warning. A duplicate column name no longer refuses Apply; only the first occurrence is persisted, as before, and the second card is named by its `Duplicate name` chip. Statistics names what is missing whenever a named card has no config to describe: `transformationStatsGapNotice()` phrases one line for every gap the cards actually hold (a named category, a filter on each one), because the alternative was a column heading over a dash-only table under "Category counts from a describe run" — a describe that never ran. Re-adding a gate needs a shape that separates stored data from an in-progress edit, not a flag on one message list; `transformationCanApply` is the only hold left and it must stay a statement about an empty payload, not about loaded rules. + +Files affected: `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.*`, `src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.*`, `docs/context/decision-log.md`, `docs/context/risk-register.md`. + +Date: 2026-09-14. + +### Decision: Every pipeline step is a solid card that toggles from its header + +Status: Accepted + +Context: Browser feedback on the Data Handling pipeline. Each step carried a `Collapse` pill (or `Preview raw data` / `Edit rules`) next to its status chip, and Filtering and Transformations sat behind a dashed dormant card whose only way in was an `Add filtering` / `Add transformation` button. The dormant card did not react to a click on its title, so the first click a user tried did nothing. + +Decision: Remove the header toggle buttons and the dormant cards. Every step renders as a solid card; an optional step that is not added shows its collapsed rail (`Add condition`, `Add derived column`) and the `Not in run yet` chip. A click anywhere on the header folds an open step or opens a folded one (`onStepHeaderClick` → `toggleStep`); opening an optional step adds it, which is a read, as before. The step title is a `<button class="pipeline-node-toggle">` inside its `h3`, so the toggle is reachable by keyboard and screen readers without losing the heading. Clicks on the header's own buttons and at the end of a text selection do not toggle. A chevron marks the fold state. Remove Step shows only while the step is added. Step bodies stay gated on `isStepAdded`, so an un-added step mounts no editor and Remove Step still destroys unapplied builder state. Cohort filtering's `sectionOpen.filters` starts `false`, and `isStepOpen` reports a step outside the pipeline as closed. + +Consequences: `is-dormant`, `.pipeline-node-dormant`, `.dormant-left`, `.dormant-right`, the no-op `.pipeline-node.is-active` rule and the header `station-preview-toggle` buttons are gone; the summary `Close` buttons keep that class. Solid versus dashed no longer separates a step that changes the run from one that does not — the status chip does. The guide copy names `Add condition`, the column-type choice and the header click. No request payload, endpoint, or guard changes; authenticated pipeline behaviour still needs manual browser QA. + +Files affected: `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.*`, `src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.pipeline.spec.ts`, `src/app/pages/experiment-studio/guide/experiment-studio-guide.content.ts`, `src/app/pages/experiment-studio/experiment-studio.component.css`, `docs/context/decision-log.md`. + +Date: 2026-10-14. + +### Decision: Save experiment persists the reviewed run instead of running it again + +Status: Accepted + +Context: Run experiment used `/services/experiments/transient`, which discards the result, so Save experiment rebuilt the request from the live parameter form and posted it to `/services/experiments`, then polled for a second run. The saved experiment could differ from the result on screen (parameters edited after the run, randomised algorithms), saving took as long as running, and `describe` never saved at all because its save path also went through `/transient`. Save also flipped `isRunning`, so the Execution header reset its run time and Unsaved/Saved state mid-save. + +Decision: Run posts to `/services/experiments/runs`; the backend keeps a successful result and returns its id, which the algorithm panel stores as `runId` next to `runResult`. Save sends only that id and a name to `/services/experiments/runs/:runId/save`, through `ExperimentStudioService.saveRun`, called directly by the Execution panel (saving no longer needs the parameter form, so the algorithm panel's `onSaveAs` and the shell's `(saveAs)` relay are gone). Saving has its own `isSaving` flag and never touches `isRunning`. Failures show next to the Save As form through `saveError`; a 404 (the kept run expired after 24h) asks the user to run again. The guide's `experiment-saved-as` step now waits only for the new experiment id, since the result no longer disappears during a save. + +Consequences: The old persisted-run path (`runSelectedAlgorithm` posting to `/services/experiments` and polling) is removed; the transient run method took its name. `/services/experiments/transient` stays for previews (histogram, describe overview, outlier report, K-means report). Needs the backend endpoints from the matching platform-backend change. + +Files affected: `src/app/services/experiment-studio.service.ts`, `src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts`, `src/app/pages/experiment-studio/execution-panel/execution-panel.component.*`, `src/app/pages/experiment-studio/experiment-studio.component.html`, `src/app/pages/experiment-studio/guide/experiment-studio-guide.component.ts`, and their specs. + +Date: 2026-10-14. + +### Decision: Ordered `appliedTransformations` list; PCA via `pca_column_creator` + +Status: Accepted + +Context: Exaflow dropped `pca_with_transformation`, replaced creator `code` with `new_column_names`, and added `pca_column_creator` (plus K-means fit mode). Downstream UI had stored column creators as keyed preprocessing config entries, which could not express repeated steps or step order when a later creator reads an earlier one's outputs. + +Decision: Keep singleton handlers in the preprocessing config map. Store every column-creating step in `ExperimentStudioService.appliedTransformations` as an ordered `TransformationStep[]` (id, name, parameters). Emit singletons first, then creators in list order. Name every created column with `new_column_names`. Offer PCA component scores only through `pca_column_creator` (reuse a PCA result's `reusable_preprocessing`, or fit on selected numerical variables). Do not offer or send `pca_with_transformation`. + +Consequences: Cards append in creation order (no drag-reorder yet); a K-means card can cluster on earlier PCA columns. No backwards compatibility: experiments saved with a creator `code` key or with `pca_with_transformation` are not migrated. Their creators do not hydrate on edit, and their PCA results render no tables or charts; rebuild the columns and rerun. Step ids come from `newStepId()` (`crypto.getRandomValues`), so plain-HTTP origins work. Session storage persists the ordered list under `appliedTransformations`. + +Files affected: `src/app/services/experiment-studio.service.ts`, `src/app/pages/experiment-studio/statistic-analysis-panel/**`, `src/app/core/pca-column-source.utils.ts`, `src/app/core/preprocessing-outputs.utils.ts`, `e2e/experiment-studio-api.ts`, `e2e/experiment-studio.flows.spec.ts`, `docs/context/*.md`. + +Date: 2026-10-27. + +### Decision: Experiment calls follow the consolidated `/services/experiments` API + +Status: Accepted + +Context: platform-backend PR #34 moved every experiment route under `/services/experiments` and removed `/services/analysis`, `/services/experiments/transient`, `/services/experiments/runs/:runId/save` and the asynchronous create-and-run `POST /services/experiments` that returned `pending` and was polled. + +Decision: Previews (histogram, describe overview, outlier report, K-means report) post to `/services/experiments/preview`, which takes and returns the same shapes as `/transient` did. Save posts `{ runId, name }` to `/services/experiments`; the 201 response has `result: null`, and the studio only reads its `uuid` and `shared`, keeping the reviewed result it already holds. `getAlgorithmResults` sends every algorithm to the preview endpoint (only histogram calls it today), so `submitRequest` and `pollForResults` are gone. + +Consequences: Breaking: this UI needs a backend with PR #34 and does not work against an older one. Payload shapes of previews and runs are unchanged; the JSON field stays `analysis` (only the DB column was renamed). The dashboard still renders a stored `pending` status for older rows. The e2e mock API follows the new routes. + +Files affected: `src/app/services/experiment-studio.service.ts`, `src/app/services/experiment-studio.service.spec.ts`, `e2e/experiment-studio-api.ts`, `e2e/experiment-studio.flows.spec.ts`, `README.md`, `docs/context/*.md`, `docs/llm-wiki/indexes/backend-endpoints.md`. + +Date: 2026-10-15. diff --git a/docs/context/module-index.md b/docs/context/module-index.md new file mode 100644 index 000000000..bdc4f92d6 --- /dev/null +++ b/docs/context/module-index.md @@ -0,0 +1,121 @@ +# Module Index + +## `src/app` +Purpose: Main Angular application source. +Key files: `app.routes.ts`, `app.config.ts`, `app.component.*`. +Used by: Angular bootstrap from `src/main.ts`. +Rules: Keep app-wide providers, route definitions, and shell wiring here; avoid feature logic in the root shell. +Tests: Root component and route behavior are covered indirectly by page/service specs. +Notes: Uses standalone Angular and zoneless change detection. + +## `src/app/guards` +Purpose: Route access control for authentication and terms/NDA. +Key files: `auth.guard.ts`, `terms.guard.ts`. +Used by: `app.routes.ts`. +Rules: Preserve auth and NDA boundaries; do not duplicate guard decisions in unrelated UI code. +Tests: Add focused guard tests when route access behavior changes. +Notes: `/terms` intentionally skips `TermsGuard`; `/notebook` also uses `TermsGuard` and runtime `NOTEBOOK_ENABLED` canMatch. + +## `src/app/services` +Purpose: Cross-feature state, backend calls, auth/session, exports, runtime env, rules, and errors. +Key files: `auth.service.ts`, `experiment-studio.service.ts`, `experiments-dashboard.service.ts`, `experiment-folders.service.ts`, `algorithm-rules.service.ts`, `runtime-env.service.ts`. +Used by: Pages, guards, shared components, and visualization/export flows. +Rules: Keep orchestration and API access here; prefer existing services before adding new shared state. +Tests: Service specs live beside services as `*.spec.ts`. +Notes: `ExperimentStudioService` is high-risk because it coordinates selection state, preview calls, run/edit flows, and sessionStorage persistence. `ExperimentFoldersService` owns analysis sets and nothing else: PostgreSQL is the store and `/services/experiment-folders` is the only door — nothing is kept in the browser. `setActiveUser(user)` points the `folders` signal at the signed-in user (username is only the cache key; the session cookie picks the rows server-side), and every mutation is an `Observable` that writes back the folder the server returned, never a locally edited copy. Routes and bodies are tabulated in `models/backend-experiment-folder.model.ts`; the service maps a `409` to the `null`/`false` the inline "that name is taken" messages speak and rethrows everything else. Folders hold member ids only, with each folder's members partitioned across its `sets` (`ExperimentSet = { id, name, experimentIds[] }`) and Ungrouped meaning simply "a member no set has taken". The partition is strict — `moveToSet` repoints the membership row so the set that had it gives the run up — because a run in two sections has no honest count; deleting a set returns its runs instead of deleting them. `pruneExperiment` is the one local-only mutation, and only because the experiment delete behind it has already cascade-dropped the membership rows. + +## `src/app/models` +Purpose: Backend DTOs, frontend models, and shared interfaces. +Key files: `backend-experiment.model.ts`, `backend-algorithms.model.ts`, `algorithm-definition.model.ts`, `data-model.interface.ts`, `filters.model.ts`, `experiment-folder.model.ts`. +Used by: Services, mappers, page components, and visualization logic. +Rules: Keep API shape changes explicit and compatibility-aware. +Tests: Model changes are usually validated through service/mapper/component tests. +Notes: Prefer extending existing interfaces over ad hoc `any` where practical. + +## `src/app/core` +Purpose: Algorithm/result mapping, constants, and utility logic. +Key files: `algorithm-mappers.ts`, `algorithm-result-enum-mapper.ts`, `algorithm-parameter.utils.ts`, `filter-display.utils.ts`, `constants/algorithm.constants.ts`, `outlier-rules.ts`, `share.utils.ts`, `route-path.utils.ts`, `experiment-drag.utils.ts`. +Used by: Experiment Studio, result rendering, services, and tests. +Rules: Treat algorithm key aliases and result schema mappings as compatibility-sensitive. +Tests: `algorithm-mappers.spec.ts`, `algorithm-result-enum-mapper.spec.ts`. +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/*`. +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. + +## `src/app/pages/experiment-studio/visualisations` +Purpose: Chart/table rendering for algorithm outputs, histograms, metadata browsers, and auto-rendered results. +Key files: `charts/chart-registry.ts` (builder + section title per algorithm), `charts/chart-builder.service.ts`, `charts/chart-palette.ts` (the one 8-slot series palette), `auto-renderer/algorithm-table-registry.ts`, renderer files under `charts/renderers`, `regression/regression-view.ts` + `regression-result.component` (coefficient view for linear/logistic regression, LMM, binary GLMM), `summary/summary-figures.ts` + `key-figures.component` (headline figures for tests and cross-validated models), `result-format.ts` (p-value, ratio and number formatting, χ² tail). +Used by: Algorithm result panels, dashboard detail/compare, and statistics views. +Rules: Register new algorithm outputs in the existing registries; preserve legacy aliases when changing algorithm keys. `AlgorithmResultComponent` picks one layout: the regression view when `buildRegressionView` returns one (linear/logistic regression, LMM, GLMMs, Cox; it then replaces charts and tables), otherwise key figures from `buildSummaryFigures`, then the registry charts, then the tables. A table is never repeated under a summary that already shows its values: `tablesShownAsFigures` (summary-figures) works out from the result which rows or columns the figures actually print, and each chart entry's `replaces` names the chart (`byChart`, its title text) that stands in for a table, applied through `tablesReplacedByCharts` only when that chart rendered. Both are `TableTrim`s — a title alone covers the whole table, `columns`/`rows` only those — and `trimTable` applies them on screen; the result title goes on the builder's first table before anything is trimmed. Before covering a table, move any value it has beyond the summary into the figures or chart. "Show all values as tables" under a result brings every covered table back whole (the accessible path to values a chart only draws); exports always keep every table, with exact values: a cell the screen rounds is a `DisplayCell` (`text` shown, `value` exported) and a table that merges values on screen carries an `export` with one exact value per column; CSV, ZIP and PDF all go through `exportTable`. The first column and name columns (groups, classes, categories) print as sent, never digit-grouped. View builders are pure functions with specs; components only render. The PDF export reads the table registry, so tables stay complete even where a summary view leads on screen. +Tests: Registry and renderer specs live in this subtree. +Notes: Registry keys and legacy aliases are compatibility-sensitive; check stored experiment data before removing an alias. + +## `src/app/pages/experiments-dashboard` +Purpose: Experiment listing, filtering, detail, compare, sharing, rename, deletion UI, plus user-curated folders ("analysis sets"). +Key files: `experiments-dashboard.component.*`, `experiments-dashboard.mapper.ts`, `experiment-list/*`, `experiment-detail/*`, `experiments-compare/*`, `experiment-folder/*`, `experiment-search/*`. +Used by: `/experiments-dashboard` route; folders are read and written through `ExperimentFoldersService` (`/services/experiment-folders`, scoped to the signed-in user by session). +Rules: Keep backend-to-frontend mapping in the mapper; preserve rollback behavior for optimistic deletion. Folders hold member ids only, and membership is pruned on a delete event or a 404 — never against the visible page, which is server-paginated. Compare handoff resolves members through `hydrateExperiments` so off-page runs are fetched before `compareIds` is set, and `experimentsForCompare` follows `compareIds` so the workspace shows the canvas' 1-2-3 order. The workspace records its origin in `compareOriginFolderId` to offer a way back to the canvas; turning compare on from the list clears the canvas and the origin. Members join from the row menu, by dropping a row onto the open canvas, or by dropping it on a chip; the payload is `EXPERIMENT_DRAG_MIME` from `core/experiment-drag.utils.ts`, targets gate on `dataTransfer.types` (the payload stays unreadable until the drop lands), and a drop adds rather than toggling. Drag is an affordance only — the row menu remains the keyboard path. Sets are a grouping *over* `folder.experimentIds`, never a second copy of membership: the canvas lists members grouped by set (folder order, Ungrouped last) with one number line across the canvas, and each row's `fa-object-group` menu is the only place a run changes set. The compare workspace reads that partition and never writes to it — it renders one section per set in folder order, then one section per algorithm label for everything ungrouped, each run a compact row that expands its result in place; with no origin folder (a hand-built selection) it groups by algorithm alone. A filter the backend contract cannot express (substring, author, variable, status, date) is answered from one capped snapshot of the ownership-scoped history: `historyTruncated` says the cap cut it short and the list summary names how many runs were searched, so a miss is never silent. +Tests: Add component/mapper tests when list, filter, compare, or detail rendering changes; folder specs cover the service, the chip strip, the canvas, the drag-to-add path, and the off-page handoff, and set specs cover move semantics, prune-on-delete/404, canvas grouping, and compare sections. +Notes: Sharing and deletion are human-review areas. Folders are per-owner server rows — no sharing of a folder, no deep link to one, and a failed write is shown rather than cached locally. + +## `src/app/pages/terms-page` +Purpose: Terms/NDA gate. +Key files: `terms-page.component.*`. +Used by: `/terms` route and `TermsGuard`. +Rules: Preserve acceptance flow and intended redirect behavior. +Tests: Add tests for markdown load, acceptance, and redirect changes. +Notes: Loads `src/assets/tos.md`. + +## `src/app/pages/account-page` +Purpose: User account/profile view. +Key files: `account-page.component.*`. +Used by: `/account` route. +Rules: Avoid logging or exposing sensitive user/session data. +Tests: Add component tests for profile/logout behavior changes. +Notes: Logout is implemented in `AuthService`. + +## `src/app/pages/notebook` +Purpose: Optional JupyterHub entry route with separate Hub OAuth session. +Key files: `notebook.component.*`, `hub-session.ts`, `hub-http.ts`, `jupyterhub-api.ts`. +Used by: `/notebook` route when runtime env enables it. +Rules: Respect `NOTEBOOK_ENABLED`, `JUPYTER_CONTEXT_PATH`; keep Hub login as top-level navigation; do not pass platform tokens into the iframe. +Tests: `notebook.component.spec.ts`, `hub-session.spec.ts`, `hub-http.spec.ts`, `jupyterhub-api.spec.ts`. +Notes: Platform auth/terms gate entry; Hub session is probed separately. Nginx returns top-level Hub/Lab URLs to `/notebook`. Footer sits below a full-viewport notebook area; scroll to reach it. + +## `src/app/pages/shared` +Purpose: Shared shell components and generic helpers. +Key files: `header/*`, `footer/*`, `spinner/*`, `utils/form-control.factory.ts`. +Used by: Root shell and feature pages. +Rules: Keep shared components generic; avoid feature-specific behavior here unless already established. +Tests: Footer has a spec; add focused tests for shared behavior changes. +Notes: Footer displays runtime MIP version. Notebook pill uses `NotebookNavService` (`mip.notebook.nav.seen`) for first-visit glow. + +## `src/assets` and `public` +Purpose: Runtime env, brand assets, icons, markdown, and static public files. +Key files: `env.js`, `tos.md`, logos/footer assets. +Used by: App runtime, Angular build assets, nginx container output. +Rules: Do not commit secrets into `env.js`; it is runtime-populated in containers. +Tests: Build verifies asset paths. +Notes: Brand/logo usage should follow `DESIGN.md`. + +## `Dockerfile`, `docker-entrypoint.sh`, `nginx.conf.template` +Purpose: Build and serve the Angular app in nginx with runtime environment injection. +Key files: all three root files. +Used by: Docker builds and release image publishing workflow. +Rules: Treat proxy, notebook, and env injection changes as deployment-sensitive. +Tests: Validate with `npm run build`; Docker image build is recommended for container changes. +Notes: Dockerfile uses `npm ci` when `package-lock.json` exists. + +## `.github/workflows` +Purpose: Release image publishing and EBRAINS mirroring. +Key files: `publish_images.yml`, `ebrains.yml`. +Used by: GitHub Actions on release, master pushes, and tags. +Rules: Never expose workflow secrets; avoid changing registry/mirror behavior without human review. +Tests: Use workflow syntax review and, if available, CI dry runs. +Notes: No pull-request test/build workflow is currently present. diff --git a/docs/context/risk-register.md b/docs/context/risk-register.md new file mode 100644 index 000000000..14fff3e2c --- /dev/null +++ b/docs/context/risk-register.md @@ -0,0 +1,22 @@ +# Risk Register + +| Area | Risk | Evidence | Agent guidance | Human review required? | +|---|---|---|---|---| +| Authentication/session | Login, logout, redirect, and session state can break access to the whole app. | `AuthService`, `AuthGuard`, `/services/activeUser`, Keycloak redirect URLs. | Keep redirects same-origin, preserve `redirect_url`, test login/logout manually. | Yes | +| Terms/NDA gate | Users may bypass or get stuck behind acceptance flow. | `TermsGuard`, `TermsService`, `/terms`, `agreeNDA`, `tos_redirect_url`. | Preserve route guard order and post-acceptance redirect behavior. | Yes | +| Same-origin credentials/XSRF | API calls can lose cookies or XSRF headers. | `auth.interceptor.ts`, `provideHttpClient`, XSRF config in `app.config.ts`. | Do not bypass the interceptor; validate authenticated API calls. | Yes | +| Experiment run/edit state | User selections and experiment payloads can become inconsistent. | `ExperimentStudioService` manages selections, configs, run/save, preview calls, and sessionStorage. | Make focused changes; add service/component tests and manual Studio validation. | Usually | +| Experiment deletion | Optimistic UI changes may hide failed backend deletion or delete wrong item. | `ExperimentsDashboardService.deleteExperiment` updates UI before backend response and rolls back on failure. | Preserve rollback and error reporting; manually test failure paths when possible. | Yes | +| Experiment sharing | Incorrect sharing state can expose or hide experiments. | Dashboard service patches `shared` and updates list state. | Treat `shared` changes as permission-sensitive; verify backend contract. | Yes | +| Algorithm availability | Algorithms can become incorrectly enabled/disabled. | `AlgorithmRulesService` and related specs. | Add tests for changed role/type/count rules. | Usually | +| Result mapping and visualization | Supported algorithm outputs may render blank or incorrectly. | `algorithm-mappers.ts`, table/chart registries, existing visualization audit docs. | Preserve legacy aliases; add registry/renderer tests for payload shape changes. | Usually | +| Runtime environment | Wrong env injection can break version display, notebook, or proxy context. | `docker-entrypoint.sh`, `RuntimeEnvService`, `nginx.conf.template`. | Do not read env values ad hoc; validate container runtime when changed. | Yes | +| Notebook proxy | Notebook route/proxy can expose unavailable or unintended upstreams. | `/notebook` route, `NOTEBOOK_ENABLED`, nginx notebook location. | Keep Angular gating aligned with nginx gating. | Yes | +| Docker image publishing | Release workflow uses registry credentials and pushes images. | `.github/workflows/publish_images.yml`. | Never print secrets; review registry/tag changes carefully. | Yes | +| EBRAINS mirroring | Workflow pushes code/tags to an external GitLab mirror. | `.github/workflows/ebrains.yml`. | Avoid changing mirror refs or credentials handling without owner review. | Yes | +| Dependencies | Updates can affect Angular build, bundle output, and chart/export libraries. | `package.json`, `package-lock.json`. | Explain need, review lockfile, run build/tests. | Yes | +| Cohort / category-rule filters (edit mode) | Stored filter trees can be re-sent in a shape the engine rejects, or silently rewritten, so an experiment that runs from the API fails from Studio. | `core/filter-logic.utils.ts`, `filter-config-modal.component.ts` (`backendToGroup`/`normalizeCondition`), `statistic-analysis-panel.component.ts` (`commitTransformationRuleFilters`). | Treat backend filter trees as untrusted input: hydrate every shape, never default an unknown operator to `equal`, never overwrite a loaded rule with an empty builder. Verify edit-mode runs against an experiment whose filters use `in`/`is_not_null`. | Yes | +| Derived-column stage (ungated) | Preview and Apply run on whatever the cards can express, so an unfinished card contributes nothing to the describe and to the payload, and a duplicate column name persists its first occurrence only. | `statistic-analysis-panel.component.ts` (`commitTransformationRuleFilters`, `buildTransformationConfigForDraft`, `transformationDraftStatus`). | Do not reintroduce a Preview/Apply gate over loaded data: it strands experiments whose category rules were authored outside this UI. Keep the commit's rule-safety check — never overwrite a rule its builder could not read — and let that rule's own note (`.transformation-saved-filter-note`, driven by `unloadedInput()`) plus the `Pending`/`Duplicate name` chips stay the only warning. `transformationCanApply` may dim Apply while no card forms a config, which must stay a statement about an empty payload and never a verdict on loaded rules; and Apply must keep the stage open while `transformationHasPendingChange` is true, or the chip that reports unfinished work folds away with it. Manual QA must cover Apply with a duplicate column name, and Apply on an experiment whose stored category rules this UI cannot author. | Yes | +| Ordered transformations / PCA columns | Reordering or keying creators by name can break "later step may only read earlier outputs"; sending `code` or `pca_with_transformation` fails at Exaflow. | `ExperimentStudioService.appliedTransformations`, `pca_column_creator` / `kmeans_cluster_creator` / `categorical_column_creator`, e2e PCA→K-means→logistic flow. | Keep creators in the ordered list with `new_column_names`; emit after singletons; never revive `pca_with_transformation`. Manual QA: two PCA cards, K-means on PCA outputs, logistic on outputs, edit/rerun. | Yes | +| Dashboard history cap | A filter the backend cannot express is answered from one ownership-scoped snapshot of the history, capped at `fullHistoryCap` runs; a search past it silently misses older runs. | `experiments-dashboard.service.ts` (`loadAllExperiments`, `historyTruncated`). | The cap is stated in the list summary now, not hidden; removing the signal without a backend substring/date filter brings the silent miss back. | Yes | +| Design and branding | UI changes can violate MIP brand guidelines. | `DESIGN.md`, logos/assets. | Follow approved colors, logo rules, and UI hierarchy. | For visible brand changes | diff --git a/docs/context/testing.md b/docs/context/testing.md new file mode 100644 index 000000000..3e6111599 --- /dev/null +++ b/docs/context/testing.md @@ -0,0 +1,113 @@ +# Testing + +## Frameworks Detected +- Unit/component tests: Vitest through the Angular CLI `unit-test` builder, in headless Chromium (Vitest browser mode with Playwright, 1280x720 viewport). +- Test configuration: `angular.json` test target and `tsconfig.spec.json`. +- Test setup file: `src/test-setup.ts`. It suppresses known noisy console messages and restores `vi.spyOn` spies after each test. +- `createSpyObj` in `src/app/testing/spy-obj.testing.ts` builds a typed object of `vi.fn()` methods. + +## Test Structure +- Specs live beside source files as `*.spec.ts`. +- Service specs are under `src/app/services`. +- Component specs are under their feature directories. +- Visualization registry/renderer specs are under `src/app/pages/experiment-studio/visualisations`. + +## Test Output Guardrails +`npm test` maps to `ng test`, which watches when run in a terminal. Do not start full interactive/watch test runs unless the user requested them or has confirmed after being told they may consume substantial runtime/tokens. Prefer focused specs or a verified non-watch command when available. + +## Unit Test Command +```bash +npm test +``` + +Agents should not start it autonomously unless the user requested tests or confirmed the token/runtime cost. A bounded run: + +```bash +npm test -- --watch=false # whole suite +npm test -- --watch=false --include src/app/services/x.spec.ts # one spec +``` + +The tests need Chromium: run `npx playwright install chromium` once, or set `CHROME_BIN` to an installed Chrome. CI runs the whole suite on pull requests (`.github/workflows/unit-tests.yml`). + +## Static Checks (no browser, no backend) +```bash +npm run typecheck # tsc over the app and spec projects, no emit +npm run check:dead-code # unused exports, orphan CSS/keyframes, unused :root tokens +npm run verify # typecheck + dead-code + build, in that order +``` + +## Full Local Validation +```bash +npm run verify +npm test +``` + +Run the full validation sequence only when warranted by the change and after applying the test output guardrails above. + +For authenticated flows, also run manual browser QA against a working backend and Keycloak-compatible auth setup. + +## Frontend Manual QA +There is no separate QA checklist file; the list below is it. Walk the affected surface in a browser against a working backend, especially when changing: +- Algorithm result mapping. +- Visualization registries/renderers. +- Histogram and descriptive statistics rendering. +- Dashboard detail or compare views. + +## Backend Tests +Not applicable in this frontend repository. Backend behavior must be validated in the backend repository or against a deployed/local backend. + +## E2E Tests +Playwright flows live in `e2e/`. They run headless and in parallel against `ng serve`, with every `/services` call mocked in the browser, so they need no backend and no secrets. CI runs them on pull requests (`.github/workflows/e2e-experiment-studio.yml`). + +```bash +npm run e2e # every spec in e2e/ +npm run e2e -- e2e/dashboard.spec.ts # one file +npm run e2e -- -g "save shows" # tests whose name matches +``` + +- `experiment-studio.flows.spec.ts`: studio flows, preprocessing and filter payloads, histogram and describe previews, run and save lifecycle, result rendering. Includes an ordered-transformation flow (PCA columns → K-means on those outputs → `logistic_regression`) that asserts preprocessing step order and `inputdata.variables`. +- `dashboard.spec.ts`: rename, share, delete (with rollback), compare, duplicate. +- `access.spec.ts`: Keycloak redirect, terms acceptance, notebook route on and off. +- `fixtures.ts`: the `test` every spec imports. It installs the mock (`api`), and fails the test if the app called a `/services` endpoint the mock does not answer. +- `experiment-studio-api.ts`: the mock and its data. Linear regression, `describe`, `histogram`, and `pca` answers follow the shapes Exaflow returns (PCA includes `variables`, `components`, and `reusable_preprocessing`). Preprocessing specs expose creators with the `outputs` shape (`pca_column_creator`, `categorical_column_creator`, `kmeans_cluster_creator`). `api.requests` holds every request body, so tests check the payload the studio sent, not only the labels it shows. A test overrides one endpoint with its own `page.route`, which runs before the mock. +- `studio.ts`: shared page steps (open the studio, add variables, apply preprocessing, add PCA/K-means columns, run, save). + +### Manual QA (PCA / ordered transformations) +Against a stack with Exaflow `ed9d6982b` or later and the aggregation server deployed: + +- Two PCA cards (fit and/or reuse a finished PCA run). +- One K-means card whose clustering variables are PCA output columns. +- Logistic regression on the derived outputs. +- Edit and rerun the saved experiment. +- Confirm creators send `new_column_names` (not `code`) and that there is no `pca_with_transformation` path. + +Failed tests keep a trace and a screenshot in `e2e/test-results/`; CI uploads that folder as the `e2e-test-results` artifact. Open a trace with `npx playwright show-trace <trace.zip>`. Locally the tests use the installed Chrome; CI uses Playwright's Chromium. + +## Fixtures and Mocks +- Existing unit tests define local fixtures inside specs. +- Component tests commonly provide service stubs and Angular Signals through `TestBed`. +- Shared unit-test helpers live in `src/app/testing/` (for example `createSpyObj`); e2e fixtures and the `/services` mock live in `e2e/`. + +## Critical Paths That Need Strong Coverage +- Auth and redirect handling. +- Terms/NDA gating and post-acceptance redirect behavior. +- Experiment creation/run/polling and edit hydration. +- Experiment sharing, rename, and deletion rollback behavior. +- Algorithm availability rules and result mapping. +- Visualization registry compatibility for current and legacy algorithm keys. +- Runtime env and notebook route gating. + +## Validation Matrix +| Change type | Required validation | +|---|---| +| UI-only component change | Relevant component spec if available, `npm run build`, and manual browser check for affected route. | +| Styling-only change | `npm run build`, responsive browser check, and `DESIGN.md` review. | +| Service/API integration change | Focused service/component specs, `npm run build`, and backend-backed manual check. | +| Auth/permission/NDA change | Guard/service specs, `npm run build`, manual login/logout/redirect/NDA check, human review. | +| Algorithm availability change | `algorithm-rules.service.spec.ts`, relevant component specs, `npm run build`, manual Studio check. | +| Visualization/result mapping change | Registry/renderer specs, `npm run build`, manual result rendering check using representative payloads. | +| Experiment deletion/sharing change | Service/component specs, rollback/error-path check, manual Dashboard check, human review. | +| Runtime env/nginx/Docker change | `npm run build`, Docker build when practical, runtime env smoke test, human review. | +| Dependency update | `npm ci`, `npm run build`, `npm test`, lockfile review, bundle/runtime smoke check. | +| Refactor without behavior change | Existing relevant specs, `npm run build`, focused manual smoke check for touched feature. | +| Documentation-only change | Path/link sanity check; build is optional unless repo instructions require it. | diff --git a/docs/exareme3-frontend-visualization-audit.md b/docs/exareme3-frontend-visualization-audit.md deleted file mode 100644 index 0a4511e46..000000000 --- a/docs/exareme3-frontend-visualization-audit.md +++ /dev/null @@ -1,120 +0,0 @@ -# Exareme3 Frontend Visualization Audit - -Date: 2026-02-11 - -## Scope - -This audit compares: -- Frontend visualization implementation under `src/app/pages/experiment-studio/visualisations/` and `algorithm-result` wiring. -- Exaflow Exareme3 algorithm outputs from `exaflow/algorithms/exareme3/*.py` and `*.json`. - -Primary frontend sources: -- `src/app/pages/experiment-studio/visualisations/charts/chart-registry.ts` -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` -- `src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.html` -- `src/app/pages/experiment-studio/variables-panel/variables-panel.component.ts` - -## 1. What The Frontend Visualizes Today - -### 1.1 Result page visualization registry - -The result page renders only what exists in: -- `AlgorithmTableRegistry` (tables) -- `AlgorithmChartRegistry` (charts) - -If an algorithm key is missing from both registries, result rendering is effectively empty. - -| Algorithm key (frontend) | Tables rendered | Charts rendered | -|---|---|---| -| `kmeans` | Cluster centers table (`centers`) | 2D/3D center scatter (`centers`) | -| `linear_regression` | Coefficients + model summary | None | -| `linear_regression_cv` | Fold sample sizes + error metrics | None | -| `logistic_regression` | Coefficients + model summary | None | -| `logistic_regression_cv` | CV metrics summary | Confusion matrix + ROC curves | -| `naive_bayes_gaussian` | Class summary + theta + variance | None | -| `naive_bayes_gaussian_cv` | Classification summary table | Confusion matrix | -| `naive_bayes_categorical` | Class summary + category counts | None | -| `naive_bayes_categorical_cv` | Classification summary table | Confusion matrix | -| `pearson_correlation` | `n_obs` table | Correlation + p-values + CI heatmaps | -| `anova_oneway` | ANOVA summary + Tukey table | Mean plot (bar chart renderer is currently empty) | -| `anova` (legacy key) | Two-way ANOVA table | None | -| `ttest_onesample` | Key/value table (all returned keys) | None | -| `ttest_independent` | Key/value table (all returned keys) | None | -| `ttest_paired` | Key/value table (all returned keys) | None | -| `pca` | None | Eigenvector heatmap | -| `pca_with_transformation` | None | Eigenvector heatmap | -| `describe` | None | Box plot over `variable_based` | -| `svm_scikit` (legacy key) | Summary/coefficients/support vector sample | SVM support vector distribution | -| `logistic_regression_cv_fedaverage` (legacy key) | CV metrics summary | Confusion matrix + ROC curves | - -### 1.2 Other visualization paths (outside result registry) - -- `histogram` is rendered as a transient D3 histogram in variables panel (`HistogramComponent`), not in `AlgorithmTableRegistry`/`AlgorithmChartRegistry`. -- Descriptive statistics also have a dedicated panel (`StatisticAnalysisPanel`) that renders: - - Variables/model tables from `describe.variable_based` and `describe.model_based` - - Numeric distributions (box plots) - - Nominal distributions (grouped bar charts) - -## 2. Exareme3 Algorithm Coverage Against Backend Outputs - -Backend Exareme3 algorithm names (from JSON `name` fields): -- `anova_oneway`, `anova_twoway`, `describe`, `histogram`, `kmeans`, `linear_regression`, `linear_regression_cv`, `linear_svm`, `logistic_regression`, `logistic_regression_cv`, `longitudinal_transformer`, `naive_bayes_categorical`, `naive_bayes_categorical_cv`, `naive_bayes_gaussian`, `naive_bayes_gaussian_cv`, `pca`, `pca_with_transformation`, `pearson_correlation`, `ttest_independent`, `ttest_onesample`, `ttest_paired`. - -### Coverage status - -| Exareme3 algorithm | Coverage status | Notes | -|---|---|---| -| `anova_oneway` | Partial | `anova_table` and `tuckey_test` shown; `ci_info` partly used (mean plot); `min_max_per_group` not shown. | -| `anova_twoway` | Missing in result registry | Frontend has `anova` key, not `anova_twoway`; name mismatch prevents rendering in result page. | -| `describe` | Partial | Dedicated descriptive panel is rich; result registry path only charts and no table builder. | -| `histogram` | Partial | Only transient variables-panel histogram is used; grouped histogram entries are not surfaced. | -| `kmeans` | Partial | `centers` shown; `n_obs`/`title` omitted. | -| `linear_regression` | Partial | Main regression stats shown; `dependent_var`, `ll`, `aic`, `bic` omitted. | -| `linear_regression_cv` | Partial (with mapping bug) | Missing `dependent_var`, `indep_vars`, `f_stat`; `mean` values not rendered due key mismatch (`avg` expected, backend uses `mean`). | -| `linear_svm` | Missing in result registry | Backend returns `weights`/`intercept`; frontend only has legacy `svm_scikit` renderers. | -| `logistic_regression` | Partial (with mapping bug) | Mostly shown, but `stderr` column is empty because frontend expects `std_err`. | -| `logistic_regression_cv` | Mostly complete | Summary + confusion matrix + ROC rendered; `dependent_var`/`indep_vars` not shown. | -| `longitudinal_transformer` | Not visualized (N/A in current UX) | Transformer/preprocessing utility, no dedicated visualization path. | -| `naive_bayes_categorical` | Partial | `category_log_prob` is not rendered. | -| `naive_bayes_categorical_cv` | Complete (for top-level output) | `classification_summary` table + confusion matrix chart. | -| `naive_bayes_gaussian` | Complete (for top-level output) | Class summary + theta + variance rendered. | -| `naive_bayes_gaussian_cv` | Complete (for top-level output) | `classification_summary` table + confusion matrix chart. | -| `pca` | Partial | Only `eigenvectors` charted; `eigenvalues`, `n_obs`, `title` omitted. | -| `pca_with_transformation` | Partial | Same as `pca`: only `eigenvectors` charted. | -| `pearson_correlation` | Complete (functional) | `n_obs` table + all matrix outputs (`correlations`, `p_values`, `ci_hi`, `ci_lo`) charted. | -| `ttest_independent` | Complete | All returned fields rendered as key/value rows. | -| `ttest_onesample` | Complete | All returned fields rendered as key/value rows. | -| `ttest_paired` | Complete | All returned fields rendered as key/value rows. | - -## 3. Discrepancies To Address - -### High-impact mismatches - -1. Algorithm key mismatch: `anova_twoway` backend vs `anova` frontend registry key. -2. Algorithm key mismatch: `linear_svm` backend vs `svm_scikit` frontend registry key. -3. `linear_regression_cv` metric mapping bug: frontend expects `avg`, backend returns `mean`. -4. `logistic_regression` stderr mapping bug: frontend reads `std_err`, backend returns `stderr`. - -### Missing output fields (partial rendering) - -1. `anova_oneway`: `min_max_per_group` not visualized; bar chart renderer is a stub. -2. `kmeans`: `n_obs` and `title` not visualized. -3. `linear_regression`: `dependent_var`, `ll`, `aic`, `bic` not visualized. -4. `linear_regression_cv`: `dependent_var`, `indep_vars`, `f_stat` not visualized. -5. `naive_bayes_categorical`: `category_log_prob` not visualized. -6. `pca` and `pca_with_transformation`: `eigenvalues`, `n_obs`, `title` not visualized. -7. `logistic_regression_cv`: `dependent_var` and `indep_vars` not visualized (metrics/charts are present). - -### Flow-specific gaps - -1. `histogram`: frontend consumes only the first histogram item in variables panel; grouped histogram entries (`grouping_var`, `grouping_enum`) are effectively ignored. -2. `describe`: rich visualization exists in dedicated descriptive panel, but the generic result renderer path has no table builder for `describe`. - -### Legacy/non-Exareme3 frontend keys found - -- `anova` -- `svm_scikit` -- `logistic_regression_cv_fedaverage` - -These keys are not present in current Exareme3 algorithm names and can cause confusion unless intentionally kept for backward compatibility with historical experiment payloads. - diff --git a/docs/exareme3-visualization-remediation-tasks.md b/docs/exareme3-visualization-remediation-tasks.md deleted file mode 100644 index 1c500546c..000000000 --- a/docs/exareme3-visualization-remediation-tasks.md +++ /dev/null @@ -1,395 +0,0 @@ -# Exareme3 Visualization Remediation Tasks - -Date: 2026-02-11 -Source audit: `docs/exareme3-frontend-visualization-audit.md` - -## Agent Intake - -This document is intended for execution by coding agents. - -Execution rules: -- Pick tasks by priority (`P0` before `P1` before `P2`) unless dependencies block. -- Respect dependencies in each task. -- For each completed task, update status and append a short implementation note. -- Keep backward compatibility for existing stored experiments unless task explicitly says otherwise. - -Task status values: -- `pending` -- `in_progress` -- `blocked` -- `done` - -## Machine-Readable Task Manifest - -```yaml -tasks: - - id: VIS-001 - priority: P0 - status: done - title: Harmonize algorithm keys (backend vs frontend registries) - depends_on: [] - - id: VIS-002 - priority: P0 - status: done - title: Fix schema field mapping bugs (stderr/std_err, mean/avg) - depends_on: [] - - id: VIS-003 - priority: P0 - status: blocked - title: Add/adjust automated tests for registry rendering contracts - depends_on: [VIS-001, VIS-002] - - id: VIS-004 - priority: P1 - status: done - title: Complete anova_oneway visualization gaps - depends_on: [VIS-001, VIS-002] - - id: VIS-005 - priority: P1 - status: done - title: Add missing linear_regression and linear_regression_cv fields - depends_on: [VIS-002] - - id: VIS-006 - priority: P1 - status: done - title: Add missing pca/pca_with_transformation summary tables - depends_on: [VIS-001] - - id: VIS-007 - priority: P1 - status: done - title: Add missing naive_bayes_categorical category_log_prob visualization - depends_on: [VIS-001] - - id: VIS-008 - priority: P1 - status: done - title: Add logistic_regression_cv metadata summary table - depends_on: [VIS-001] - - id: VIS-009 - priority: P1 - status: done - title: Improve histogram flow to support grouped histogram outputs - depends_on: [] - - id: VIS-010 - priority: P2 - status: done - title: Decide and implement describe result-page strategy - depends_on: [] - - id: VIS-011 - priority: P2 - status: done - title: Legacy key deprecation and compatibility policy - depends_on: [VIS-001] -``` - -## Detailed Tasks - -### VIS-001 (P0) Harmonize algorithm keys (backend vs frontend registries) - -Problem: -- Backend Exareme3 names: `anova_twoway`, `linear_svm`. -- Frontend registries currently use legacy keys: `anova`, `svm_scikit`. -- Result page misses rendering when keys do not match. - -Scope: -- Ensure `AlgorithmTableRegistry` and `AlgorithmChartRegistry` support backend keys. -- Keep legacy keys as aliases for backward compatibility. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` -- `src/app/pages/experiment-studio/visualisations/charts/chart-registry.ts` -- `src/app/core/algorithm-mappers.ts` - -Implementation checklist: -- Add `anova_twoway` key mapped to current two-way ANOVA table builder. -- Add `linear_svm` key mapped to SVM table/chart builders. -- Preserve `anova` and `svm_scikit` as aliases to avoid regressions on historical payloads. -- Verify output schema mapping in `algorithm-mappers.ts` is consistent with runtime keys. - -Acceptance criteria: -- A result with `algorithm="anova_twoway"` renders a non-empty table. -- A result with `algorithm="linear_svm"` renders non-empty table/chart. -- Existing historical keys (`anova`, `svm_scikit`) still render. - -Verification: -- `npm test` (or targeted tests for renderer components) -- `npm run build` - ---- - -### VIS-002 (P0) Fix schema field mapping bugs (stderr/std_err, mean/avg) - -Problem: -- Logistic regression output field is `stderr`, while frontend reads `std_err`. -- Linear regression CV returns `BasicStats(mean, std)`, while frontend expects `avg`. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` - -Implementation checklist: -- In logistic regression table builder, support both `stderr` and `std_err`. -- In linear regression CV builder, support both `mean` and `avg`. -- Add defensive handling for missing stats values to avoid blank rows. - -Acceptance criteria: -- Logistic regression "Std.Err." column is populated with Exareme3 payloads. -- Linear regression CV metric "Mean" values are populated with Exareme3 payloads. - -Verification: -- Unit tests for both payload shapes. -- `npm run build` - ---- - -### VIS-003 (P0) Add/adjust automated tests for registry rendering contracts - -Problem: -- Key-mapping and schema-field regressions can silently hide results. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.spec.ts` -- New spec files near registries: - - `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.spec.ts` - - `src/app/pages/experiment-studio/visualisations/charts/chart-registry.spec.ts` - -Implementation checklist: -- Add test fixtures for Exareme3 payloads (`anova_twoway`, `linear_svm`, `logistic_regression`, `linear_regression_cv`). -- Assert table builders produce rows/columns for canonical payloads. -- Assert chart builder returns chart options for supported algorithms. - -Acceptance criteria: -- Tests fail before fixes and pass after fixes for key mismatches/field mismatches. -- CI-local suite includes coverage for all `P0` discrepancies. - -Verification: -- `npm test` -- `npm run build` - ---- - -### VIS-004 (P1) Complete anova_oneway visualization gaps - -Problem: -- `buildBarChart` is currently a stub. -- `min_max_per_group` is not surfaced. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/charts/renderers/bar-chart.ts` -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` - -Implementation checklist: -- Implement `buildBarChart` using ANOVA one-way grouped summaries. -- Add table section for `min_max_per_group` (group, min, max). -- Keep existing mean plot. - -Acceptance criteria: -- ANOVA one-way renders at least one additional chart beyond mean plot. -- Min/max per group appears in a table. - -Verification: -- Screenshot/manual QA with real response payload. -- `npm run build` - ---- - -### VIS-005 (P1) Add missing linear_regression and linear_regression_cv fields - -Problem: -- Important backend outputs are omitted from tables. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` - -Implementation checklist: -- `linear_regression`: include `dependent_var`, `ll`, `aic`, `bic` in summary table. -- `linear_regression_cv`: include `dependent_var`, `indep_vars`, `f_stat` (mean/std) in summary. - -Acceptance criteria: -- All listed fields appear when present in payload. -- Existing rendering remains stable when fields are missing. - -Verification: -- Unit tests with complete and sparse payloads. -- `npm run build` - ---- - -### VIS-006 (P1) Add missing pca/pca_with_transformation summary tables - -Problem: -- PCA currently only visualizes `eigenvectors` heatmap. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` - -Implementation checklist: -- Add table builder entries for: - - `pca` - - `pca_with_transformation` -- Render: - - `title` - - `n_obs` - - `eigenvalues` (component/value) - -Acceptance criteria: -- PCA result page contains both chart and summary tables. - -Verification: -- Manual check with payload fixture. -- `npm run build` - ---- - -### VIS-007 (P1) Add missing naive_bayes_categorical category_log_prob visualization - -Problem: -- Backend returns `category_log_prob`, currently not visualized. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` - -Implementation checklist: -- Add per-feature table(s) for `category_log_prob`, aligned with category labels and class labels. -- Reuse existing enum/label mapping helpers. - -Acceptance criteria: -- Category log probabilities render for each feature in categorical NB. - -Verification: -- Unit tests with categorical NB payload. -- `npm run build` - ---- - -### VIS-008 (P1) Add logistic_regression_cv metadata summary table - -Problem: -- CV metrics/charts exist but `dependent_var` and `indep_vars` are hidden. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` - -Implementation checklist: -- Add compact metadata table above CV metrics for: - - `dependent_var` - - `indep_vars` - -Acceptance criteria: -- Metadata appears for logistic regression CV when provided. - -Verification: -- Unit tests for presence/absence of metadata fields. -- `npm run build` - ---- - -### VIS-009 (P1) Improve histogram flow to support grouped histogram outputs - -Problem: -- Variables panel consumes only the first histogram item, ignoring grouped outputs. - -Primary files: -- `src/app/pages/experiment-studio/variables-panel/variables-panel.component.ts` -- `src/app/pages/experiment-studio/variables-panel/variables-panel.component.html` -- `src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.ts` - -Implementation checklist: -- Parse all items in `result.histogram`. -- Add UI to switch between: - - Base histogram (no grouping) - - Grouped histograms (`grouping_var`, `grouping_enum`) -- Preserve enum label mapping for bins. - -Acceptance criteria: -- Grouped histogram responses are navigable and rendered. -- Non-grouped responses still render exactly as before. - -Verification: -- Manual UI test with grouped backend payload. -- `npm run build` - ---- - -### VIS-010 (P2) Decide and implement describe result-page strategy - -Problem: -- `describe` has rich dedicated panel, but generic result page path has no table builder. - -Decision options: -- Option A: Add `describe` table builder for result page parity. -- Option B: Keep result page minimal and route users to dedicated panel intentionally. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` -- `src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.html` -- Potentially docs/UX copy. - -Acceptance criteria: -- Chosen behavior is explicit in code and docs. -- No ambiguous empty state for describe results. - -Verification: -- Manual check for both studio and dashboard result contexts. -- `npm run build` - ---- - -### VIS-011 (P2) Legacy key deprecation and compatibility policy - -Problem: -- Legacy keys (`anova`, `svm_scikit`, `logistic_regression_cv_fedaverage`) still exist and can confuse maintenance. - -Primary files: -- `src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts` -- `src/app/pages/experiment-studio/visualisations/charts/chart-registry.ts` -- `docs/exareme3-frontend-visualization-audit.md` -- This task doc. - -Implementation checklist: -- Keep aliases for backward compatibility. -- Add inline comments documenting canonical keys and legacy aliases. -- Add doc section with deprecation timeline and removal criteria. - -Acceptance criteria: -- Canonical keys are clear and used by default. -- Legacy support behavior is documented and test-covered. - -Verification: -- `npm test` -- `npm run build` - -### Legacy Key Policy - -- Canonical keys to use in all new code and payloads: - - `anova_twoway` - - `linear_svm` - - `logistic_regression_cv` -- Backward-compatible aliases kept in registries: - - `anova` -> `anova_twoway` - - `svm_scikit` -> `linear_svm` - - `logistic_regression_cv_fedaverage` -> `logistic_regression_cv` rendering contract -- Deprecation review date: `2026-06-30`. -- Alias removal criteria: - - Zero usage in persisted experiments/export payloads for 30 consecutive days. - - Migration script or fallback mapper available for historical records. - - Explicit release note in frontend changelog before removal. - -## Suggested PR Split (for multiple agents) - -1. PR-A (P0): `VIS-001`, `VIS-002`, `VIS-003` -2. PR-B (P1 data tables): `VIS-005`, `VIS-006`, `VIS-007`, `VIS-008` -3. PR-C (P1 charts/UX): `VIS-004`, `VIS-009` -4. PR-D (policy/docs): `VIS-010`, `VIS-011` - -## Progress Log - -- 2026-02-11: Document created. All tasks `pending`. -- 2026-02-11: `VIS-001` completed (added canonical keys `anova_twoway` and `linear_svm` with legacy aliases preserved). -- 2026-02-11: `VIS-002` completed (added `mean|avg` and `stderr|std_err` compatibility in table builders). -- 2026-02-11: `VIS-003` implemented (added registry specs) but blocked on execution due local Chrome sandbox permissions in this environment. -- 2026-02-11: `VIS-005` completed (added missing linear regression and linear regression CV summary fields). -- 2026-02-11: `VIS-006` completed (added PCA/PCA-with-transformation summary and eigenvalue tables). -- 2026-02-11: `VIS-007` completed (added categorical Naive Bayes category log-probability tables). -- 2026-02-11: `VIS-008` completed (added logistic regression CV metadata table in both CV builders). -- 2026-02-11: `VIS-004` completed (implemented ANOVA one-way bar chart and added min/max per-group table output). -- 2026-02-11: `VIS-010` completed (adopted Option A by adding `describe` table builder for result-page parity). -- 2026-02-11: `VIS-011` completed (documented canonical/legacy key compatibility and deprecation policy). -- 2026-02-11: `VIS-009` completed (added grouped-histogram variant selector and rendering support in variables panel). diff --git a/docs/frontend-browser-qa-checklist.md b/docs/frontend-browser-qa-checklist.md deleted file mode 100644 index 919cbaef7..000000000 --- a/docs/frontend-browser-qa-checklist.md +++ /dev/null @@ -1,47 +0,0 @@ -## Frontend QA Template (Pass/Fail) - -Copy-paste this into your QA doc and fill the `Result` and `Notes` columns. - -| ID | Area | Check | Result (Pass/Fail) | Notes | -|---|---|---|---|---| -| PRE-01 | App | App loads without initial console errors | | | -| PRE-02 | Navigation | `Experiment Studio` and `Experiments Dashboard` both open normally | | | -| ALG-01 | `anova_twoway` | Non-empty “Two-Way ANOVA Results” table renders | | | -| ALG-02 | `linear_svm` | Table output renders for `linear_svm` | | | -| ALG-03 | `linear_svm` | SVM chart renders for `linear_svm` | | | -| ALG-04 | `logistic_regression` | `Std.Err.` column is populated (not blank) | | | -| ALG-05 | `linear_regression_cv` | Error metric “Mean” values are populated | | | -| ALG-06 | `logistic_regression_cv` | Metadata table includes dependent/independent variables | | | -| ALG-07 | `logistic_regression_cv` | Confusion matrix chart renders | | | -| ALG-08 | `logistic_regression_cv` | ROC chart renders | | | -| ALG-09 | `pca` | Summary table + eigenvalues table render | | | -| ALG-10 | `pca` | PCA heatmap chart renders | | | -| ALG-11 | `pca_with_transformation` | Summary table + eigenvalues table render | | | -| ALG-12 | `pca_with_transformation` | PCA heatmap chart renders | | | -| ALG-13 | `naive_bayes_categorical` | “Category Log Probabilities” table(s) render | | | -| ALG-14 | `describe` | Result page shows describe tables (not chart-only) | | | -| ANO-01 | `anova_oneway` | “ANOVA Sum of Squares” bar chart renders | | | -| ANO-02 | `anova_oneway` | “Group Min/Max” table appears when data exists | | | -| ANO-03 | `anova_oneway` | Mean plot still renders | | | -| HIS-01 | Histogram | Grouped histogram response shows `View` dropdown | | | -| HIS-02 | Histogram | Switching `View` updates histogram data | | | -| HIS-03 | Histogram | “Overall” option is present when backend returns it | | | -| HIS-04 | Histogram | Non-grouped histogram behavior remains unchanged | | | -| HIS-05 | Histogram Export | CSV export works for selected histogram view | | | -| HIS-06 | Histogram Export | PDF export works for selected histogram view | | | -| LEG-01 | Legacy compatibility | Old `anova` experiments still render | | | -| LEG-02 | Legacy compatibility | Old `svm_scikit` experiments still render | | | -| LEG-03 | Legacy compatibility | Old `logistic_regression_cv_fedaverage` experiments still render | | | -| DSH-01 | Dashboard detail | Updated algorithms render correctly in experiment detail page | | | -| DSH-02 | Dashboard compare | Updated algorithms render correctly in compare view | | | -| UX-01 | UI integrity | No blank result cards for supported algorithms | | | -| UX-02 | Responsive | No major layout breakage on desktop/mobile | | | -| UX-03 | Console | No new console errors during algorithm result rendering | | | - -### Optional run metadata -- QA date: -- Tester: -- Branch/commit: -- Backend version: -- Frontend version: -- Dataset(s) used: diff --git a/docs/llm-wiki/00-agent-entrypoint.md b/docs/llm-wiki/00-agent-entrypoint.md new file mode 100644 index 000000000..7f6631257 --- /dev/null +++ b/docs/llm-wiki/00-agent-entrypoint.md @@ -0,0 +1,60 @@ +# LLM Wiki Entrypoint + +Read this file first. Goal: spend the smallest useful context budget before +opening source files. + +## Load Protocol + +1. Read only this page. +2. Pick one topic page from the table below. +3. Use `indexes/files-by-feature.md` or `indexes/symbols-by-feature.md` to find + the exact path or symbol. +4. Use CodeGraph for structural questions when available; use `rg` for literal + text, labels, endpoints, selectors, and result keys. +5. Read source in a small range around the matched symbol or string. +6. Open whole files or folders only when the task is complex, cross-feature, or + the targeted lookup does not explain the behavior. + +## Topic Map + +| Task | Read next | +|---|---| +| bootstrap, providers, architecture | `01-architecture-map.md` | +| route, redirect, guard, notebook flag | `02-routing-map.md` | +| user-facing feature ownership | `03-feature-index.md` | +| backend call, service, endpoint | `04-services-and-api-index.md` | +| DTO, state, payload, data flow | `05-models-and-data-flow.md` | +| algorithm config, result schema, mapper | `06-algorithm-mapping-index.md` | +| component, template, CSS, chart, export | `07-ui-components-index.md` | +| login, logout, active user, terms | `08-auth-and-terms-flow.md` | +| build, tests, proxy, Docker, nginx | `09-build-test-runbook.md` | +| unsure or mixed task | `10-task-context-recipes.md` | + +## Repo Snapshot + +- App: Angular standalone frontend, package `fl-platform`. +- Backend boundary: relative `/services` requests. +- Bootstrap: `src/main.ts` -> `src/app/app.config.ts`. +- Routes: `src/app/app.routes.ts`. +- Main areas: Experiment Studio, Experiments Dashboard, Account, Terms, + optional Notebook. +- UI decisions: `DESIGN.md`, then the live tokens in `src/styles.css` `:root`. + +## Expensive Context + +Open only after a targeted lookup: + +- `src/app/core/algorithm-mappers.ts` +- `src/app/pages/experiment-studio/**` +- `src/app/pages/experiments-dashboard/**` +- component templates/styles unrelated to UI work +- `src/assets/**`, `public/**`, `package-lock.json`, generated output + +## Validation Defaults + +- Static gate: `npm run verify` (typecheck + dead-code scan + build) +- Narrowest static check: `npm run typecheck`, or `npm run check:dead-code` +- Unit tests: `npm test -- --watch=false` (Vitest, headless Chromium) +- Dev server: `npm start` + +Run the narrowest check that proves the change. diff --git a/docs/llm-wiki/01-architecture-map.md b/docs/llm-wiki/01-architecture-map.md new file mode 100644 index 000000000..33acaf5ec --- /dev/null +++ b/docs/llm-wiki/01-architecture-map.md @@ -0,0 +1,39 @@ +# Architecture Map + +Standalone Angular app; no root NgModule. + +```text +src/main.ts + -> bootstrapApplication(AppComponent, appConfig) +src/app/app.config.ts + -> global providers +src/app/app.routes.ts + -> lazy route map and guards +``` + +## Core Providers + +`app.config.ts` owns router setup, zoneless change detection, HTTP client, +credentials interceptor, XSRF config, and lazy ECharts provider. + +## Ownership + +| Concern | Start here | +|---|---| +| app shell | `src/app/app.component.*` | +| routes and guards | `src/app/app.routes.ts`, `src/app/guards/` | +| backend orchestration | `src/app/services/` | +| contracts and DTOs | `src/app/models/` | +| algorithm/result mapping | `src/app/core/` | +| feature UI | `src/app/pages/<feature>/` | +| global style/runtime assets | `src/styles.css`, `src/assets/`, `public/` | + +Use `indexes/files-by-feature.md` for exact paths. + +## Rules + +- Prefer standalone components and lazy route imports. +- Keep feature helpers in the owning feature unless reused. +- Put backend calls in services and data shapes in models. +- Do not duplicate algorithm result logic across components; prefer mappers and + registries. diff --git a/docs/llm-wiki/02-routing-map.md b/docs/llm-wiki/02-routing-map.md new file mode 100644 index 000000000..8a94caace --- /dev/null +++ b/docs/llm-wiki/02-routing-map.md @@ -0,0 +1,27 @@ +# Routing Map + +Routes live in `src/app/app.routes.ts`. + +| Route | Guards / matchers | Purpose | +|---|---|---| +| `/` | redirect | `/experiments-dashboard` | +| `/experiments-dashboard` | `AuthGuard`, `TermsGuard` | default app area | +| `/terms` | `AuthGuard` | terms acceptance | +| `/account` | `AuthGuard`, `TermsGuard` | account page | +| `/experiment-studio` | `AuthGuard`, `TermsGuard` | experiment creation/results | +| `/notebook` | `NOTEBOOK_ENABLED` canMatch, `AuthGuard`, `TermsGuard` | optional notebook | +| `**` | redirect | fallback to dashboard | + +## Lookup Order + +1. Search route path in `src/app/app.routes.ts`. +2. Inspect the target component `.ts`. +3. Inspect only the guards used by that route. +4. Open services only if guard/component state requires it. + +## Search Keys + +- route path, component class, guard name +- `NOTEBOOK_ENABLED`, `canMatch`, `frontend_redirect` + +For auth details read `08-auth-and-terms-flow.md`. diff --git a/docs/llm-wiki/03-feature-index.md b/docs/llm-wiki/03-feature-index.md new file mode 100644 index 000000000..b3fdbab03 --- /dev/null +++ b/docs/llm-wiki/03-feature-index.md @@ -0,0 +1,58 @@ +# Feature Index + +Use this page to choose the smallest feature context. + +## Experiment Studio + +Path: `src/app/pages/experiment-studio/` + +One step view at a time behind a sticky stepper (`activeSection`); the views stay +mounted and are hidden with CSS, so open editors and in-flight runs survive a step +change. + +Owns data model/dataset selection, the variable pool and its outcome/predictor +roles, filters, the Data review pipeline, algorithm config, runs, transient +previews, charts, tables, histograms, and metadata views. + +Start with: + +- `ExperimentStudioComponent` (step shell) and `StudioStepperComponent` (step rail) +- `ExperimentStudioService` (selection, algorithm roles, run state) +- `execution-panel/` for run status/results and + `algorithm-panel/algorithm-role-assignment/` for the y/x slots +- `shared/station-card`, `shared/station-action-bar`, `shared/station-list-row` for + the cards and footers every Data review step reuses +- relevant model under `src/app/models/` +- `06-algorithm-mapping-index.md` for algorithm output/config tasks + +Search keys: `activeSection`, `selectedDataModel`, `dataset`, `variable`, +`algorithmY`, `algorithmX`, `algorithmAssignableVariables`, `filter`, `transient`, +`histogram`, `describe`, `runResult`, algorithm name. + +## Experiments Dashboard + +Path: `src/app/pages/experiments-dashboard/` + +Owns experiment list/search/pagination, detail view, compare, metadata updates, +sharing, delete, and exports. + +Start with: + +- `ExperimentsDashboardComponent` +- `ExperimentsDashboardService` +- dashboard model/mapper + +Search keys: `compare`, `export`, `shared`, `description`, `delete`, `PATCH`, +experiment id, result key. + +## Smaller Pages + +| Area | Path | Start with | +|---|---|---| +| account | `src/app/pages/account-page/` | component + `AuthService` | +| terms | `src/app/pages/terms-page/` | component + `TermsGuard` | +| notebook | `src/app/pages/notebook/` | route + component + runtime env | +| shared layout | `src/app/pages/shared/` | specific component | + +Use `indexes/files-by-feature.md` for exact paths and avoid opening sibling +folders until a search points there. diff --git a/docs/llm-wiki/04-services-and-api-index.md b/docs/llm-wiki/04-services-and-api-index.md new file mode 100644 index 000000000..f7d498dda --- /dev/null +++ b/docs/llm-wiki/04-services-and-api-index.md @@ -0,0 +1,32 @@ +# Services and API Index + +Backend calls use relative `/services/...` URLs. + +## Lookup Order + +1. Check `indexes/backend-endpoints.md` for the endpoint. +2. Search the endpoint string or resource name. +3. Read the owning service. +4. Read request/response models only after the service is identified. +5. Read components only if UI behavior changes. + +## Service Ownership + +| Concern | Likely service | +|---|---| +| user/session/login/logout | `auth.service.ts` | +| studio data/runs/previews | `experiment-studio.service.ts` | +| dashboard list/detail/update/delete | `experiments-dashboard.service.ts` | +| folders, sets, membership | `experiment-folders.service.ts` | +| algorithm availability/rules | `algorithm-rules.service.ts` | +| PDF/CSV export | export services | +| error display | `error.service.ts` | +| runtime flags | `runtime-env.service.ts` | + +## HTTP Rule + +`auth.interceptor.ts` adds `withCredentials: true` to relative requests. Prefer +relative `/services/...` paths and avoid duplicating credential handling. + +Do not change API shapes from UI assumptions alone; confirm service payload use +and model contracts first. diff --git a/docs/llm-wiki/05-models-and-data-flow.md b/docs/llm-wiki/05-models-and-data-flow.md new file mode 100644 index 000000000..90cc9d606 --- /dev/null +++ b/docs/llm-wiki/05-models-and-data-flow.md @@ -0,0 +1,35 @@ +# Models and Data Flow + +Use before changing request/response shapes, feature state, algorithm payloads, +or result rendering. + +## Model Areas + +| Concern | Likely files | +|---|---| +| algorithm definitions | `algorithm-definition.model.ts`, `backend-algorithms.model.ts` | +| algorithm results | `algorithm-results.model.ts` | +| backend experiments | `backend-experiment.model.ts` | +| dashboard | `experiments-dashboard.model.ts` | +| filters | `filters.model.ts` | + +## Flows + +```text +/services/data-models -> ExperimentStudioService -> data model/dataset/variable UI +/services/specifications/algorithms -> algorithm mapper -> algorithm panel config +/services/experiments -> result model -> visualisations/tables/exports +/services/experiments/:id -> dashboard service -> detail/compare/export UI +backend result keys -> output schema -> renderer/registry +``` + +## Rules + +- Inspect the owning service before changing a model. +- Search model usage before renaming fields. +- Prefer additive optional fields if backend compatibility is uncertain. +- Update mappers when backend names differ from UI names. +- Update focused tests for mapper/model utilities where present. + +Search by endpoint, DTO/interface, backend field, UI label, algorithm name, or +result key before opening broad folders. diff --git a/docs/llm-wiki/06-algorithm-mapping-index.md b/docs/llm-wiki/06-algorithm-mapping-index.md new file mode 100644 index 000000000..857078ee9 --- /dev/null +++ b/docs/llm-wiki/06-algorithm-mapping-index.md @@ -0,0 +1,39 @@ +# Algorithm Mapping Index + +Primary file: `src/app/core/algorithm-mappers.ts`. + +Supporting files: input counts, parameter utilities, result enum mapper, +algorithm constants, and algorithm/result models. + +## Responsibilities + +- key configs by the backend algorithm name as delivered (no alias rewriting) +- assign categories +- build parameter/config schemas +- normalize enum/dictionary fields +- sanitize labels/docs +- apply fallback input counts +- attach output schemas for result rendering + +## Lookup Rules + +- For one algorithm, search by backend name or output key first. +- For output schema work, search `getOutputSchema` and edit only the matching + case. +- For category work, search `CATEGORY_MAPPING`. +- For input counts, inspect `algorithm-input-counts.ts`. +- Open renderer/registry files only when schema type/rendering behavior changes. + +## Search Keys + +Algorithm backend name, output key, `mapSpecificationsToAlgorithmConfigs`, +`getOutputSchema`, `prettifyLabel`, `buildConfigSchema`, `CATEGORY_MAPPING`, +`dynamic-table`, `section`, `number`, `table`. + +Do not read all of `algorithm-mappers.ts` unless the task crosses many +algorithms or the targeted search is inconclusive. + +Names are now used exactly as the backend sends them: the `anova` -> +`anova_twoway` rewrite and the `histogram_sql` / +`logistic_regression_cv_fedaverage` entries are gone, so check stored experiment +compatibility before removing or adding an alias. diff --git a/docs/llm-wiki/07-ui-components-index.md b/docs/llm-wiki/07-ui-components-index.md new file mode 100644 index 000000000..a491eab7e --- /dev/null +++ b/docs/llm-wiki/07-ui-components-index.md @@ -0,0 +1,36 @@ +# UI Components Index + +Use for component behavior, templates, CSS, charts, tables, and export UI. + +## Read Order + +| Task | Read first | +|---|---| +| TypeScript behavior | component `.ts`, service, model | +| template binding | component `.ts`, then `.html` | +| styling/layout | `DESIGN.md`, then `.html`, component `.css`, then `src/styles.css` `:root` | +| chart rendering | visualisation component, chart builder/registry | +| result table | algorithm schema, auto renderer/table registry | +| export/PDF | dashboard/detail component, export service | + +## Ownership + +| Area | Path | +|---|---| +| app shell | `src/app/app.component.*` | +| Experiment Studio | `src/app/pages/experiment-studio/` | +| Dashboard | `src/app/pages/experiments-dashboard/` | +| account/terms/notebook | corresponding page folder | +| shared layout | `src/app/pages/shared/` | + +Header notebook pill: discover glow (`header-nav-link--discover`) until first `/notebook` +visit; persisted in `localStorage` key `mip.notebook.nav.seen` via `NotebookNavService`. + +## Rules + +- Search by visible text, selector, CSS class, or component class before opening + folders. +- Prefer component-local CSS for feature layout. +- Use `src/styles.css` only for global behavior. +- Check output schema before changing result renderers. +- Do not edit unrelated CSS/templates in the same pass. diff --git a/docs/llm-wiki/08-auth-and-terms-flow.md b/docs/llm-wiki/08-auth-and-terms-flow.md new file mode 100644 index 000000000..c9b383ba9 --- /dev/null +++ b/docs/llm-wiki/08-auth-and-terms-flow.md @@ -0,0 +1,55 @@ +# Auth and Terms Flow + +Use for login, logout, active user, route protection, terms, and onboarding. + +## Main Files + +- `src/app/app.routes.ts` +- `src/app/guards/auth.guard.ts` +- `src/app/guards/terms.guard.ts` +- `src/app/services/auth.service.ts` +- `src/app/services/auth.interceptor.ts` + +## Endpoints + +- `GET /services/activeUser` +- `/services/oauth2/authorization/keycloak?frontend_redirect=<target>` +- `/services/logout` + +## Guard Matrix + +| Route | Guards | +|---|---| +| dashboard | auth, terms | +| terms | auth | +| account | auth, terms | +| studio | auth, terms | +| notebook | notebook flag, auth, terms | + +## Notebook auth (separate from platform session) + +Platform UI auth and JupyterHub auth are related but separate: + +1. `/notebook` requires platform login (`AuthGuard`) and terms acceptance (`TermsGuard`). +2. Angular does not pass the platform token into the Jupyter iframe. +3. On `/notebook`, Angular probes `/notebook/hub/api/user` with `credentials: 'include'`. +4. If Hub has no session, the top-level browser is redirected to `/notebook/hub/login?next=/notebook/hub/home` (not an in-app router navigation). With `JUPYTERHUB_AUTO_LOGIN` (default true), Hub immediately continues to Keycloak without the orange OAuth button page. +5. JupyterHub authenticates through Keycloak (`GenericOAuthenticator`) when `KEYCLOAK_CLIENT_ID` is set; otherwise Hub can use dummy auth in dev. +6. After OAuth, nginx sends top-level `/notebook/hub/home` and `/notebook/user/...` document navigations back to `/notebook` so Lab stays in the shell iframe. +7. Hub login redirects use a 15s cooldown and `sessionStorage` pending state to avoid redirect loops. +8. Hub sets its own session cookie (SameSite=Lax, Secure by default). Any valid Keycloak user for the Hub client is currently allowed (`allow_all = True`). +9. Before spawn, Hub injects the Keycloak access token into the notebook pod as `MIP_TOKEN`, refreshing via stored refresh tokens when needed. +10. Running notebooks can refresh `MIP_TOKEN` through Hub `/notebook/hub/api/platform-token` via the mip client transport. +11. Remote use (account page): shown when `GET /services/auth-config` answers (the backend has a public Keycloak client, `KEYCLOAK_CLI_CLIENT_ID`). "Create access token" runs an authorization-code sign-in with PKCE as that client (`scope=openid offline_access`, redirect back to `/account`, code exchanged by the browser, `cli-token.ts`) and shows the offline refresh token once; nothing stores it. Users paste it into `mip login --token` or `MIP_API_TOKEN`; the mip client renews access with Keycloak itself. The page also explains `mip login` (device flow). JupyterHub is not involved. + +Key files: `src/app/pages/notebook/hub-session.ts`, `hub-http.ts`, `jupyterhub-api.ts`, `nginx.conf.template`. + +## Lookup Order + +1. Confirm affected route in `app.routes.ts`. +2. Read the specific guard. +3. Read `AuthService` only for session/login/logout data. +4. Read UI page only for copy or button behavior. + +Avoid duplicating credential handling; relative requests already pass through the +credentials interceptor. diff --git a/docs/llm-wiki/09-build-test-runbook.md b/docs/llm-wiki/09-build-test-runbook.md new file mode 100644 index 000000000..30a363cbb --- /dev/null +++ b/docs/llm-wiki/09-build-test-runbook.md @@ -0,0 +1,48 @@ +# Build and Test Runbook + +Use for dependency, build, test, proxy, Docker, nginx, and runtime env tasks. + +## Requirements + +- Node 22 (`.nvmrc`; `package.json` engines `^22.22.3 || ^24.15.0 || >=26.0.0`) +- npm `>=10.0.0` + +`start`, `build`, `watch`, `test` and `typecheck` each carry a pre-hook running +`scripts/check-node-version.mjs`, so an unsupported Node fails immediately rather +than mid-build. + +## Commands + +| Command | Purpose | +|---|---| +| `npm start` | dev server with proxy | +| `npm run verify` | typecheck + dead-code scan + build, in order | +| `npm run typecheck` | `tsc --noEmit` over the app and spec projects | +| `npm run check:dead-code` | unused exports, orphan CSS/keyframes, unused `:root` tokens | +| `npm run build` | production build to `dist/fl-platform` | +| `npm run watch` | development build watch | +| `npm test` | Vitest unit tests in headless Chromium (ask before running) | + +## Key Files + +- build: `angular.json`, `tsconfig*.json`, `package.json` +- proxy: `src/proxy.conf.json` +- Docker/nginx: `Dockerfile`, `docker-entrypoint.sh`, `nginx.conf.template` +- runtime env: `src/assets/env.js`, runtime env service + +## Proxy + +- `/services` -> `http://localhost:8080` +- `/notebook/` -> `http://localhost:8000` with websockets + +## Validation + +- TypeScript/app change: `npm run verify` +- Unused code, orphan CSS, or a stale design token: `npm run check:dead-code` +- Tested utility/service change: focused spec if available, otherwise + `npm test` +- Dependency change: `npm ci`, `npm run verify`, tests +- Docker/nginx change: build app, then Docker/manual runtime validation + +Avoid `package-lock.json`, Docker, nginx, and broad test output unless the task +requires them. diff --git a/docs/llm-wiki/10-task-context-recipes.md b/docs/llm-wiki/10-task-context-recipes.md new file mode 100644 index 000000000..02fbc32d9 --- /dev/null +++ b/docs/llm-wiki/10-task-context-recipes.md @@ -0,0 +1,33 @@ +# Task Context Recipes + +Use when the task is mixed or the entrypoint topic map is not enough. + +## Compact Recipes + +| Task | Wiki pages | Source lookup | Avoid first | +|---|---|---|---| +| route change | routing, architecture | route path -> target component -> guard | unrelated pages | +| backend API | services, models | endpoint/resource -> service -> model | all services/components | +| auth/session | auth, routing | route -> guard -> `AuthService` | broad UI scan | +| algorithm output | algorithm mapping | algorithm/result key -> mapper case -> registry if needed | whole mapper | +| studio variable/filter | feature, models | selected field -> studio component/service -> model | dashboard/export | +| dashboard display/export | feature, UI, algorithm if result-related | visible label/result key -> detail/compare/export service | studio internals | +| styling | UI | visible text/class -> template -> component CSS | TypeScript services | +| build/deploy | build runbook | config name -> exact build/proxy/Docker file | feature code | + +## Escalation Rule + +Open broader context only when: + +- behavior crosses multiple feature areas; +- targeted search finds multiple plausible owners; +- a shared contract, auth boundary, or runtime/deploy behavior changes; +- the first exact file does not explain the flow. + +When escalating, prefer one index page plus one source file at a time. + +## Search Before Reading + +Use exact route paths, endpoint strings, component selectors, visible labels, +DTO names, algorithm names, result keys, CSS classes, or function names. Use +CodeGraph for callers/callees/signatures when available. diff --git a/docs/llm-wiki/indexes/backend-endpoints.md b/docs/llm-wiki/indexes/backend-endpoints.md new file mode 100644 index 000000000..12241bdd1 --- /dev/null +++ b/docs/llm-wiki/indexes/backend-endpoints.md @@ -0,0 +1,51 @@ +# Backend Endpoints Index + +Two relative upstreams: `/services` (platform backend; dev proxy -> `:8080`, +nginx -> `PLATFORM_BACKEND_SERVER`) and `/notebook` (JupyterHub; dev proxy -> +`:8000`). Both are same-origin, so `auth.interceptor.ts` covers the `/services` +side and `hub-http.ts` carries the Hub XSRF header. + +## Platform backend - base path `/services` + +| Area | Method | Endpoint | Notes | +|---|---|---|---| +| auth | GET | `/services/activeUser` | session check behind `AuthGuard` | +| auth | GET | `/services/oauth2/authorization/keycloak` | full-page login redirect, `frontend_redirect` | +| auth | GET | `/services/logout` | full-page logout redirect | +| terms | POST | `/services/activeUser/agreeNDA` | acceptance from `/terms`; the text itself is the static `assets/tos.md` | +| catalog | GET | `/services/data-models` | models, datasets, variables | +| specifications | GET | `/services/specifications/inputdata` | input DTO | +| specifications | GET | `/services/specifications/preprocessing` | preprocessing step DTOs | +| specifications | GET | `/services/specifications/algorithms` | algorithm catalog and config schema | +| experiments | GET | `/services/experiments` | server page; `page,size,mine,notMine,includeShared,orderBy,descending[,algorithm,shared]` | +| experiments | POST | `/services/experiments` | `{ runId, name }` Save experiment: persists the kept run, no re-run; 201 with `result: null`; 404 once expired (24h) | +| experiments | POST | `/services/experiments/preview` | preview without storing | +| experiments | POST | `/services/experiments/runs` | studio Run; the backend keeps a successful result, `uuid` is the run id | +| experiments | GET | `/services/experiments/:uuid` | detail/compare | +| experiments | PATCH | `/services/experiments/:uuid` | `{ name }` rename, `{ shared }` share | +| experiments | DELETE | `/services/experiments/:uuid` | delete; folder membership prunes on 404 | +| folders | GET, POST | `/services/experiment-folders` | list folders / create | +| folders | PATCH, DELETE | `/services/experiment-folders/:folderId` | rename / delete | +| folders | POST, DELETE | `/services/experiment-folders/:folderId/members[/:uuid]` | add / remove a run | +| folders | PATCH | `/services/experiment-folders/:folderId/members/:uuid/set` | `{ setId }`, or `null` to unassign | +| folders | POST | `/services/experiment-folders/:folderId/sets` | create a set | +| folders | PATCH, DELETE | `/services/experiment-folders/:folderId/sets/:setId` | rename / delete a set | + +No `/services/algorithms` exists: the catalog is read from +`/services/specifications/algorithms`. + +## JupyterHub - base path from `JUPYTER_CONTEXT_PATH`, default `/notebook` + +| Area | Method | Endpoint | Notes | +|---|---|---|---| +| hub | GET | `/notebook/hub/home` | XSRF prime, `pages/notebook/hub-http.ts` | +| hub | GET | `/notebook/hub/api/user` | session and server state | +| hub | POST | `/notebook/hub/api/users/:username/server` | start the server, retried once after an XSRF prime | +| hub | GET | `/notebook/hub/login?next=...` | full-page login redirect | +| lab | GET | `/notebook/user/:username/lab` | iframe target | + +Only `auth.service.ts`, `experiment-studio.service.ts`, +`experiments-dashboard.service.ts`, `experiment-folders.service.ts`, +`terms-page.component.ts` and `src/app/pages/notebook/hub-*.ts` hit the network. + +Search exact endpoint strings before opening services. diff --git a/docs/llm-wiki/indexes/files-by-feature.md b/docs/llm-wiki/indexes/files-by-feature.md new file mode 100644 index 000000000..df83984ff --- /dev/null +++ b/docs/llm-wiki/indexes/files-by-feature.md @@ -0,0 +1,15 @@ +# Files by Feature Index + +Use to choose exact source paths before reading. + +| Feature | Paths | +|---|---| +| bootstrap/routing | `src/main.ts`, `src/app/app.config.ts`, `src/app/app.routes.ts`, `src/app/app.component.*` | +| auth/session | `src/app/guards/*.ts`, `src/app/services/auth.service.ts`, `src/app/services/auth.interceptor.ts` | +| Experiment Studio | `src/app/pages/experiment-studio/`, `src/app/services/experiment-studio*.ts` | +| Dashboard | `src/app/pages/experiments-dashboard/`, `src/app/services/experiments-dashboard.service.ts` | +| algorithms/results | `src/app/core/algorithm-*.ts`, `src/app/core/constants/`, `src/app/models/*algorithm*.ts` | +| exports | `src/app/services/*export*.service.ts`, dashboard detail/compare components | +| shared frontend helpers | `src/app/core/route-path.utils.ts`, `src/app/core/share.utils.ts`, `src/app/core/pdf.utils.ts`, `src/app/core/result-label.utils.ts`, `src/app/core/data-model.utils.ts` | +| models | `src/app/models/` | +| runtime/build | `package.json`, `angular.json`, `src/proxy.conf.json`, Docker/nginx files, `src/assets/env.js` | diff --git a/docs/llm-wiki/indexes/high-churn-files.md b/docs/llm-wiki/indexes/high-churn-files.md new file mode 100644 index 000000000..8cd1da129 --- /dev/null +++ b/docs/llm-wiki/indexes/high-churn-files.md @@ -0,0 +1,15 @@ +# High-Churn Files + +Read these only after a targeted search identifies the relevant range: + +- `src/app/core/algorithm-mappers.ts` +- `src/app/pages/experiment-studio/experiment-studio.component.ts` +- `src/app/pages/experiment-studio/visualisations/**` +- `src/app/pages/experiments-dashboard/**` +- large component `.html` / `.css` +- `src/styles.css` +- `package-lock.json` +- generated build, coverage, and dependency output + +Prefer symbol, endpoint, selector, label, CSS class, or result-key searches over +whole-file reads. diff --git a/docs/llm-wiki/indexes/symbols-by-feature.md b/docs/llm-wiki/indexes/symbols-by-feature.md new file mode 100644 index 000000000..16b082f2e --- /dev/null +++ b/docs/llm-wiki/indexes/symbols-by-feature.md @@ -0,0 +1,14 @@ +# Symbols by Feature Index + +Search these before opening source files. + +| Area | Search keys | +|---|---| +| bootstrap/routing | `bootstrapApplication`, `appConfig`, `provideRouter`, route path | +| auth | `AuthGuard`, `TermsGuard`, `activeUser`, `frontend_redirect`, `logout`, `withCredentials` | +| runtime flags | `NOTEBOOK_ENABLED`, `window.__env`, `runtimeEnv` | +| studio | `ExperimentStudioComponent`, `ExperimentStudioService`, `StudioStepperComponent`, `ExecutionPanelComponent`, `AlgorithmRoleAssignmentComponent`, `selectedDataModel`, `dataset`, `activeSection`, `algorithmY`, `algorithmX`, `algorithmAssignableVariables`, `runResult`, `transient` | +| dashboard | `ExperimentsDashboardComponent`, `ExperimentsDashboardService`, `compare`, `shared`, `export`, `delete`, `PATCH` | +| algorithm mapping | `CATEGORY_MAPPING`, `mapSpecificationsToAlgorithmConfigs`, `getOutputSchema`, `prettifyLabel`, `buildConfigSchema`, algorithm backend name, result key | +| shared frontend helpers (`src/app/core/*.utils.ts`) | `isRoutePath`, `buildExperimentShareUrl`, `copyShareUrl`, `isExperimentOwner`, `captureHtmlToPng`, `renderMipVersion`, `withLabels`, `findDataModelByCodeVersion`, `buildEnumMapForVariables` | +| visualisations | `renderer`, `registry`, `auto-renderer`, `table`, `chart`, `histogram`, `metadata` | diff --git a/e2e/access.spec.ts b/e2e/access.spec.ts new file mode 100644 index 000000000..55e807ef9 --- /dev/null +++ b/e2e/access.spec.ts @@ -0,0 +1,78 @@ +import { Page } from '@playwright/test'; +import { STUDIO_USER } from './experiment-studio-api'; +import { expect, test } from './fixtures'; + +/** Sign-in, terms and notebook gating, against the mocked `/services` API. */ + +const ACTIVE_USER = /\/services\/activeUser(\?.*)?$/; + +/** Serve the real `assets/env.js`, then these runtime values on top of its defaults. */ +async function runtimeEnv(page: Page, values: Record<string, string>) { + await page.route('**/assets/env.js', async (route) => { + const defaults = await (await route.fetch()).text(); + await route.fulfill({ + contentType: 'application/javascript', + body: `${defaults}\nObject.assign(window.__env, ${JSON.stringify(values)});`, + }); + }); +} + +test('a signed-out user is sent to the Keycloak login with the page to return to', async ({ page }) => { + await page.route(ACTIVE_USER, (route) => route.fulfill({ status: 401, json: {} })); + await page.route('**/services/oauth2/authorization/keycloak**', (route) => + route.fulfill({ contentType: 'text/html', body: '<h1>Keycloak login</h1>' }), + ); + const login = page.waitForRequest(/\/services\/oauth2\/authorization\/keycloak/); + await page.goto('/experiment-studio'); + + const target = new URL((await login).url()).searchParams.get('frontend_redirect'); + expect(target).toBe(new URL('/experiment-studio', page.url()).toString()); + await expect(page.getByRole('heading', { name: 'Keycloak login' })).toBeVisible(); +}); + +test('terms must be accepted first, then the requested page opens', async ({ page, api }) => { + let agreed = false; + await page.route(ACTIVE_USER, (route) => route.fulfill({ json: { ...STUDIO_USER, agreeNDA: agreed } })); + page.on('request', (request) => { + if (request.method() === 'POST' && request.url().endsWith('/services/activeUser/agreeNDA')) agreed = true; + }); + + await page.goto('/experiment-studio'); + await expect(page).toHaveURL(/\/terms$/); + const proceed = page.getByRole('button', { name: 'Proceed' }); + await expect(proceed).toBeDisabled(); + await page.getByLabel('I accept the Terms of Use.').check(); + await proceed.click(); + + await expect(page).toHaveURL(/\/experiment-studio$/); + expect(api.requests.some((request) => request.method === 'POST' && request.path === '/services/activeUser/agreeNDA')).toBe(true); +}); + +test('the notebook route is closed when notebooks are disabled', async ({ page }) => { + await runtimeEnv(page, { NOTEBOOK_ENABLED: '0' }); + await page.goto('/notebook'); + await expect(page).toHaveURL(/\/experiments-dashboard$/); + await expect(page.getByRole('link', { name: 'Jupyter Notebook' })).toHaveCount(0); +}); + +test('the notebook route opens JupyterLab when notebooks are enabled', async ({ page }) => { + await runtimeEnv(page, { NOTEBOOK_ENABLED: '1' }); + // A Hub with a signed-in user whose server is already running. + await page.route('**/notebook/hub/home*', (route) => + route.fulfill({ contentType: 'text/html', body: '<script>xsrf_token: "e2e-token",</script>' }), + ); + // Hub API calls carry the XSRF token as a query parameter. + await page.route(/\/notebook\/hub\/api\/user(\?.*)?$/, (route) => + route.fulfill({ json: { name: 'ada', servers: { '': { ready: true, pending: null } } } }), + ); + await page.route('**/notebook/user/**', (route) => + route.fulfill({ contentType: 'text/html', body: '<h1>JupyterLab</h1>' }), + ); + + await page.goto('/experiments-dashboard'); + await page.getByRole('link', { name: 'Jupyter Notebook' }).click(); + await expect(page).toHaveURL(/\/notebook$/); + const lab = page.locator('iframe[title="Jupyter Notebook"]'); + await expect(lab).toHaveAttribute('src', /\/notebook\/user\/ada\/lab/); + await expect(page.frameLocator('iframe[title="Jupyter Notebook"]').getByRole('heading', { name: 'JupyterLab' })).toBeVisible(); +}); diff --git a/e2e/dashboard.spec.ts b/e2e/dashboard.spec.ts new file mode 100644 index 000000000..7fb0c6fd5 --- /dev/null +++ b/e2e/dashboard.spec.ts @@ -0,0 +1,103 @@ +import { Page } from '@playwright/test'; +import { seedLinearExperiment } from './experiment-studio-api'; +import { expect, lastRequest, test } from './fixtures'; +import { goToAlgorithms, goToDataHandling, runExperiment, saveExperiment } from './studio'; + +/** Dashboard actions on saved experiments, against the mocked `/services` API. */ + +async function openExperiment(page: Page, name: string) { + await page.goto('/experiments-dashboard'); + await page.getByRole('option', { name }).click(); + await expect(page.getByRole('heading', { name })).toBeVisible(); +} + +async function moreActions(page: Page, action: string) { + await page.getByLabel('More actions').click(); + await page.locator('.detail-more-menu').getByRole('button', { name: action }).click(); +} + +test.describe('experiments dashboard', () => { + test.beforeEach(({ api }) => { + api.experiments.push(seedLinearExperiment()); + }); + + test('rename an experiment', async ({ page, api }) => { + await openExperiment(page, 'Baseline age model'); + await moreActions(page, 'Rename'); + const editor = page.locator('.title-edit-group'); + await editor.locator('input').fill('Age model v2'); + await editor.getByRole('button', { name: 'Save' }).click(); + + await expect(page.getByRole('heading', { name: 'Age model v2' })).toBeVisible(); + expect(lastRequest(api, 'PATCH', '/services/experiments/exp-seed-1')).toEqual({ name: 'Age model v2' }); + }); + + test('share an experiment', async ({ page, api }) => { + await openExperiment(page, 'Baseline age model'); + await page.getByRole('button', { name: 'Share', exact: true }).click(); + + const share = page.locator('.share-btn'); + await expect(share).toHaveText('Shared'); + await expect(share).toHaveAttribute('title', 'Stop sharing'); + expect(lastRequest(api, 'PATCH', '/services/experiments/exp-seed-1')).toEqual({ shared: true }); + }); + + test('delete an experiment', async ({ page, api }) => { + await openExperiment(page, 'Baseline age model'); + await moreActions(page, 'Delete…'); + await expect(page.getByRole('heading', { name: 'Delete experiment?' })).toBeVisible(); + await page.locator('.dialog-actions').getByRole('button', { name: 'Delete' }).click(); + + await expect(page.getByRole('option', { name: /Baseline age model/ })).toHaveCount(0); + expect(api.requests.some((request) => request.method === 'DELETE' && request.path === '/services/experiments/exp-seed-1')).toBe(true); + expect(api.experiments).toHaveLength(0); + }); + + test('a failed delete puts the experiment back', async ({ page }) => { + await page.route('**/services/experiments/exp-seed-1', (route) => + route.request().method() === 'DELETE' ? route.fulfill({ status: 500, json: {} }) : route.fallback(), + ); + await openExperiment(page, 'Baseline age model'); + await moreActions(page, 'Delete…'); + await page.locator('.dialog-actions').getByRole('button', { name: 'Delete' }).click(); + + await expect(page.getByRole('alert').filter({ hasText: 'Failed to delete experiment.' })).toBeVisible(); + await expect(page.getByRole('option', { name: /Baseline age model/ })).toBeVisible(); + }); + + test('compare two experiments side by side', async ({ page, api }) => { + api.experiments.push(seedLinearExperiment({ uuid: 'exp-seed-2', name: 'Strict age model', analysis: { + ...seedLinearExperiment().analysis, + algorithm: { name: 'linear_regression', y: ['age'], x: ['sex'], parameters: { alpha: 0.01 } }, + } })); + await page.goto('/experiments-dashboard'); + for (const name of ['Baseline age model', 'Strict age model']) { + await page.getByRole('option', { name }).getByLabel('Select for compare').click(); + } + await page.getByRole('button', { name: 'Compare 2' }).click(); + + await expect(page.getByRole('heading', { name: '2 experiments side by side' })).toBeVisible(); + const heads = page.locator('.compare-diff-name'); + await expect(heads).toHaveText(['Baseline age model', 'Strict age model']); + }); + + test('duplicate a saved experiment into a new one', async ({ page, api }) => { + await page.goto('/experiment-studio?experimentId=exp-seed-1&mode=duplicate'); + await expect(page.locator('.pool-chip', { hasText: 'Age' })).toBeVisible(); + await goToDataHandling(page); + await goToAlgorithms(page); + await expect(page.locator('h2.algorithm-details__title')).toHaveText('Linear regression'); + await runExperiment(page); + await expect(page.getByText('age ~ sex').first()).toBeVisible(); + await saveExperiment(page, 'Copy of baseline'); + await expect(page.getByText('Experiment saved successfully!')).toBeVisible(); + + // A new experiment is created; the original is not touched. + const body = api.experiments[0] as any; + expect(body.analysis).toEqual(lastRequest(api, 'POST', '/services/experiments/runs', 'linear_regression').analysis); + expect(body.name).toBe('Copy of baseline'); + expect(body.analysis.algorithm).toMatchObject({ name: 'linear_regression', y: ['age'], x: ['sex'], parameters: { alpha: 0.05 } }); + expect(api.requests.some((request) => request.method === 'PATCH')).toBe(false); + expect(api.experiments.map((experiment) => experiment.name)).toEqual(['Copy of baseline', 'Baseline age model']); + }); +}); diff --git a/e2e/experiment-studio-api.ts b/e2e/experiment-studio-api.ts new file mode 100644 index 000000000..272dd019d --- /dev/null +++ b/e2e/experiment-studio-api.ts @@ -0,0 +1,674 @@ +import { Page, Route } from '@playwright/test'; + +/** Signed-in user the studio guards accept. agreeNDA skips the terms page. */ +export const STUDIO_USER = { + username: 'ada', + fullname: 'Ada Lovelace', + email: 'ada@mip.test', + subjectId: 'subject-ada', + agreeNDA: true, +}; + +export interface StoredExperiment { + uuid: string; + name: string; + created: string; + finished: string; + shared: boolean; + viewed: boolean; + status: string; + analysis: Record<string, unknown>; + result?: unknown; + createdBy: typeof STUDIO_USER; +} + +const inputField = (types: string[], extra: Record<string, unknown> = {}) => ({ + label: '', + desc: '', + types, + required: false, + ...extra, +}); + +/** Catalog entries the algorithm panel groups by CATEGORY_MAPPING. */ +function algorithmSpecs() { + const yReal = inputField(['real'], { required: true, max_count: 1 }); + const yNominal = inputField(['text'], { required: true, max_count: 1 }); + const xRealOrNominal = inputField(['real', 'text'], { required: true, min_count: 1, max_count: 1 }); + const xNominal = inputField(['text'], { required: true, min_count: 1, max_count: 1 }); + const yRealMany = inputField(['real'], { required: true, min_count: 1 }); + + const spec = ( + name: string, + label: string, + y: Record<string, unknown>, + x?: Record<string, unknown>, + parameters: Record<string, unknown> | null = null, + ) => ({ + name, + label, + desc: `${label} specification used by the mocked studio.`, + type: 'algorithm', + y, + ...(x ? { x } : {}), + requires_validation_datasets: false, + parameters, + required_preprocessing: [], + }); + + return [ + spec('linear_regression', 'Linear regression', yReal, inputField(['real', 'text'], { min_count: 0 }), { + alpha: { label: 'Alpha', desc: 'Regularisation strength', types: ['real'], required: false, default: 0.05 }, + }), + spec('logistic_regression', 'Logistic regression', yNominal, inputField(['real', 'text'], { required: true, min_count: 1 })), + spec('pearson_correlation', 'Pearson correlation', yReal, xRealOrNominal), + spec('ttest_independent', 'Independent t-test', yReal, xNominal), + spec('anova_oneway', 'One-way ANOVA', yReal, xNominal), + spec('chi_squared', 'Chi-squared', yNominal, xNominal), + spec('naive_bayes_gaussian', 'Naive Bayes gaussian', yNominal, inputField(['real'], { required: true, min_count: 1 })), + spec('naive_bayes_categorical', 'Naive Bayes categorical', yNominal, inputField(['text'], { required: true, min_count: 1 })), + spec('kmeans', 'K-means', yRealMany), + spec('pca', 'PCA', yRealMany), + spec('describe', 'Describe', inputField(['real', 'text'], { required: true, min_count: 1 })), + spec('histogram', 'Histogram', inputField(['real', 'text'], { required: true })), + ]; +} + +function strokeModel() { + return { + uuid: 'dm-stroke', + code: 'stroke', + version: '1', + label: 'Stroke', + longitudinal: true, + released: true, + variables: [ + { + code: 'dataset', + label: 'Dataset', + type: 'nominal', + enumerations: [ + { code: 'chuv', label: 'CHUV' }, + { code: 'adni', label: 'ADNI' }, + ], + }, + { + code: 'visitid', + label: 'Visit', + type: 'nominal', + enumerations: [ + { code: 'bl', label: 'Baseline' }, + { code: 'm12', label: 'Month 12' }, + ], + }, + { code: 'age', label: 'Age', type: 'continuous' }, + { + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [ + { code: 'M', label: 'Male' }, + { code: 'F', label: 'Female' }, + ], + }, + ], + groups: [], + }; +} + +function kmeansReport(variables: string[]) { + const codes = variables.filter(Boolean); + const used = codes.length ? codes : ['age']; + const center = (value: number) => Object.fromEntries(used.map((code) => [code, value])); + return { + status: 'success', + result: { + result_type: 'privacy_safe_cluster_report', + selected_k: 2, + k_selection: 'elbow', + elbow: { k_min: 2, k_max: 6 }, + n_obs_interval: '80–100', + clusters: [ + { cluster_id: '0', label: 'Cluster 0', size_interval: '40–50', interpretation: 'Higher scores' }, + { cluster_id: '1', label: 'Cluster 1', size_interval: '40–50', interpretation: 'Lower scores' }, + ], + reusable_preprocessing: { + enumeration_prefix: 'cluster_', + preprocessing_name: 'kmeans_cluster_creator', + cluster_variables: used, + centers: { '0': center(80), '1': center(60) }, + source_context: { + data_model: 'dementia:1', + datasets: ['chuv', 'adni'], + input_fingerprint: 'mock', + }, + available_outputs: [], + cluster_choices: [ + { cluster_id: '0', label: 'Cluster 0' }, + { cluster_id: '1', label: 'Cluster 1' }, + ], + }, + }, + }; +} + +function dataModel() { + return { + uuid: 'dm-dementia', + code: 'dementia', + version: '1', + label: 'Dementia', + longitudinal: false, + released: true, + variables: [ + { + code: 'dataset', + label: 'Dataset', + type: 'nominal', + enumerations: [ + { code: 'chuv', label: 'CHUV' }, + { code: 'adni', label: 'ADNI' }, + ], + }, + { code: 'age', label: 'Age', type: 'continuous' }, + { + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [ + { code: 'M', label: 'Male' }, + { code: 'F', label: 'Female' }, + ], + }, + { + code: 'apoe', + label: 'APOE', + type: 'nominal', + enumerations: [ + { code: 'e3', label: 'e3' }, + { code: 'e4', label: 'e4' }, + ], + }, + { code: 'mmse', label: 'MMSE', type: 'continuous' }, + ], + groups: [], + }; +} + +interface NumericalStats { + num_dtps: number; + num_na: number; + num_total: number; + mean: number; + std: number; + min: number; + q1: number; + q2: number; + q3: number; + max: number; +} + +/** + * Per-dataset summaries in the shape of Exaflow's `describe` (exaflow + * algorithms/exareme3/statistics/describe.py). Unknown numerical codes reuse `age`. + */ +const NUMERICAL_STATS: Record<string, Record<string, NumericalStats>> = { + age: { + chuv: { num_dtps: 48, num_na: 2, num_total: 50, mean: 71.6, std: 7.9, min: 52, q1: 66, q2: 72, q3: 77, max: 91 }, + adni: { num_dtps: 47, num_na: 3, num_total: 50, mean: 73.1, std: 8.4, min: 55, q1: 67, q2: 73, q3: 79, max: 94 }, + }, + mmse: { + chuv: { num_dtps: 45, num_na: 5, num_total: 50, mean: 24.6, std: 3.9, min: 13, q1: 22, q2: 25, q3: 28, max: 30 }, + adni: { num_dtps: 49, num_na: 1, num_total: 50, mean: 25.3, std: 3.4, min: 15, q1: 23, q2: 26, q3: 28, max: 30 }, + }, +}; + +/** Category counts per dataset, for nominal variables and the columns the studio derives. */ +const NOMINAL_COUNTS: Record<string, Record<string, Record<string, number>>> = { + sex: { chuv: { M: 22, F: 28 }, adni: { M: 24, F: 26 } }, + apoe: { chuv: { e3: 36, e4: 14 }, adni: { e3: 33, e4: 17 } }, + sex_group: { chuv: { female: 28, other: 22 }, adni: { female: 26, other: 24 } }, + kmeans_cluster: { chuv: { '0': 23, '1': 25 }, adni: { '0': 22, '1': 25 } }, +}; + +/** The `all datasets` row: summed counts, pooled mean and standard deviation, no quartiles. */ +function globalStats(rows: NumericalStats[]) { + const n = rows.reduce((sum, row) => sum + row.num_dtps, 0); + const sx = rows.reduce((sum, row) => sum + row.mean * row.num_dtps, 0); + const sxx = rows.reduce((sum, row) => sum + (row.num_dtps - 1) * row.std ** 2 + row.num_dtps * row.mean ** 2, 0); + const mean = sx / n; + return { + num_dtps: n, + num_na: rows.reduce((sum, row) => sum + row.num_na, 0), + num_total: rows.reduce((sum, row) => sum + row.num_total, 0), + mean, + std: Math.sqrt((sxx - n * mean ** 2) / (n - 1)), + min: Math.min(...rows.map((row) => row.min)), + max: Math.max(...rows.map((row) => row.max)), + }; +} + +function nominalTotals(rows: { num_dtps: number; num_na: number; num_total: number; counts: Record<string, number> }[]) { + const counts: Record<string, number> = {}; + for (const row of rows) for (const [level, count] of Object.entries(row.counts)) counts[level] = (counts[level] ?? 0) + count; + return { + num_dtps: rows.reduce((sum, row) => sum + row.num_dtps, 0), + num_na: rows.reduce((sum, row) => sum + row.num_na, 0), + num_total: rows.reduce((sum, row) => sum + row.num_total, 0), + counts, + }; +} + +/** Exaflow `describe`: one row per variable and dataset, then one `all datasets` row per variable. */ +function describePayload(body: { analysis?: { algorithm?: { y?: string[] }; inputdata?: { variables?: string[]; datasets?: string[] } } }) { + const codes = (body?.analysis?.algorithm?.y?.length + ? body.analysis.algorithm.y + : body?.analysis?.inputdata?.variables ?? [] + ).filter((code) => code && code !== 'dataset'); + const datasets = body?.analysis?.inputdata?.datasets?.length ? body.analysis.inputdata.datasets : ['chuv', 'adni']; + + const perDataset: { variable: string; dataset: string; data: object | null }[] = []; + const global: typeof perDataset = []; + for (const code of codes) { + const nominal = NOMINAL_COUNTS[code]; + if (nominal) { + const rows = datasets.flatMap((dataset) => { + const counts = nominal[dataset]; + if (!counts) return []; + const num_dtps = Object.values(counts).reduce((sum, count) => sum + count, 0); + return [{ dataset, data: { num_dtps, num_na: 0, num_total: num_dtps, counts } }]; + }); + rows.forEach((row) => perDataset.push({ variable: code, ...row })); + global.push({ variable: code, dataset: 'all datasets', data: rows.length ? nominalTotals(rows.map((row) => row.data)) : null }); + } else { + const table = NUMERICAL_STATS[code] ?? NUMERICAL_STATS['age']; + const rows = datasets.flatMap((dataset) => (table[dataset] ? [{ dataset, data: table[dataset] }] : [])); + rows.forEach((row) => perDataset.push({ variable: code, ...row })); + global.push({ variable: code, dataset: 'all datasets', data: rows.length ? globalStats(rows.map((row) => row.data)) : null }); + } + } + return { featurewise: [...perDataset, ...global] }; +} + +/** + * Exaflow `histogram` (exaflow tests/prod_env_tests/expected/histogram_expected.json): + * bin edges for numerical variables, levels for nominal ones, and `null` where a count + * is too small to share. + */ +const HISTOGRAMS: Record<string, { bins: (number | string)[]; counts: (number | null)[] }> = { + age: { bins: [50, 55, 60, 65, 70, 75, 80, 85, 90, 95], counts: [null, 11, 14, 18, 20, 14, 10, null, null] }, + sex: { bins: ['M', 'F'], counts: [46, 54] }, +}; + +function histogramPayload(body: { analysis?: { algorithm?: { y?: string[] } } }) { + const code = body?.analysis?.algorithm?.y?.[0] ?? 'age'; + const histogram = HISTOGRAMS[code] ?? HISTOGRAMS['age']; + return { histogram: [{ var: code, grouping_var: null, grouping_enum: null, ...histogram }] }; +} + +/** + * Age ~ sex, in the shape Exaflow returns for linear_regression + * (exaflow tests/prod_env_tests/expected/linear_regression_expected.json). + */ +export const LINEAR_REGRESSION_RESULT = { + dependent_var: 'age', + n_obs: 100, + df_resid: 98.0, + df_model: 1.0, + rse: 7.912, + r_squared: 0.0421, + r_squared_adjusted: 0.0323, + f_stat: 4.3082, + f_pvalue: 0.04055, + indep_vars: ['Intercept', 'sex[M]'], + coefficients: [71.2041, 2.0512], + std_err: [1.0213, 0.9882], + t_stats: [69.7191, 2.0757], + pvalues: [1.2e-80, 0.04055], + lower_ci: [69.1773, 0.0901], + upper_ci: [73.2309, 4.0123], +}; + +/** + * PCA result shaped like Exaflow after PR #623 (changes.md §1): additive + * `variables`, `components`, and `reusable_preprocessing` for `pca_column_creator`. + * Numbers are illustrative; variable codes match the dementia mock model. + */ +function pcaResult() { + return { + title: 'Eigenvalues and Eigenvectors', + n_obs: 95, + eigenvalues: [1.71, 0.29], + eigenvectors: [ + [0.707, 0.707], + [-0.707, 0.707], + ], + variables: ['age', 'mmse'], + components: [ + { + component_id: 'PC1', + explained_variance: 1.71, + explained_variance_ratio: 0.855, + cumulative_explained_variance: 0.855, + loadings: { age: 0.707, mmse: 0.707 }, + }, + { + component_id: 'PC2', + explained_variance: 0.29, + explained_variance_ratio: 0.145, + cumulative_explained_variance: 1.0, + loadings: { age: -0.707, mmse: 0.707 }, + }, + ], + reusable_preprocessing: { + schema_version: '1', + preprocessing_name: 'pca_column_creator', + base_parameters: { + variables: ['age', 'mmse'], + pca_variant: 'pca', + }, + component_choices: [ + { component_id: 'PC1', explained_variance_ratio: 0.855 }, + { component_id: 'PC2', explained_variance_ratio: 0.145 }, + ], + }, + }; +} + +function resultFor(name: string) { + if (name === 'linear_regression') return LINEAR_REGRESSION_RESULT; + if (name === 'pca') return pcaResult(); + return { title: `Result ${name}`, n_obs: 100 }; +} + +/** A saved run the dashboard can open straight into the studio. */ +export function seedLinearExperiment(overrides: Partial<StoredExperiment> = {}): StoredExperiment { + const now = new Date().toISOString(); + return { + uuid: 'exp-seed-1', + name: 'Baseline age model', + created: now, + finished: now, + shared: false, + viewed: true, + status: 'success', + createdBy: STUDIO_USER, + analysis: { + inputdata: { + data_model: 'dementia:1', + datasets: ['chuv', 'adni'], + filters: null, + variables: ['age', 'sex'], + }, + preprocessing: [ + { name: 'missing_values_handler', parameters: { strategies: { age: 'drop', sex: 'drop' } } }, + ], + algorithm: { + name: 'linear_regression', + y: ['age'], + x: ['sex'], + parameters: { alpha: 0.05 }, + }, + }, + result: resultFor('linear_regression'), + ...overrides, + }; +} + +function json(route: Route, body: unknown, status = 200) { + return route.fulfill({ + status, + contentType: 'application/json', + body: JSON.stringify(body), + }); +} + +export interface RecordedRequest { + method: string; + path: string; + body: Record<string, any>; +} + +export interface StudioApi { + /** The stored experiments. Push seeds here before the page loads them. */ + experiments: StoredExperiment[]; + /** Every `/services` call, in order, with its parsed JSON body. */ + requests: RecordedRequest[]; + /** Calls no branch below answers. The fixture fails the test when this is not empty. */ + unhandled: string[]; +} + +/** + * Intercept every `/services/*` call the app makes. + * One in-memory list stands in for PostgreSQL so save and re-open share a store. + * Tests override single endpoints with their own `page.route`, which runs first. + */ +export async function installExperimentStudioApi(page: Page): Promise<StudioApi> { + const experiments: StoredExperiment[] = []; + const requests: RecordedRequest[] = []; + const unhandled: string[] = []; + let sequence = 0; + const runs = new Map<string, { analysis: StoredExperiment['analysis']; result: StoredExperiment['result'] }>(); + await page.route('**/services/**', async (route) => { + const request = route.request(); + const url = new URL(request.url()); + const path = url.pathname.replace(/\/$/, '') || '/'; + const method = request.method(); + + const readBody = () => { + const raw = request.postData(); + if (!raw) return {}; + try { + return JSON.parse(raw) as Record<string, any>; + } catch { + return {}; + } + }; + + requests.push({ method, path, body: readBody() }); + + if (path === '/services/activeUser' && method === 'GET') { + return json(route, STUDIO_USER); + } + + if (path === '/services/activeUser/agreeNDA' && method === 'POST') { + return json(route, { ...STUDIO_USER, agreeNDA: true }); + } + + if (path === '/services/experiment-folders' && method === 'GET') { + return json(route, { folders: [] }); + } + + if (path === '/services/data-models' && method === 'GET') { + return json(route, [dataModel(), strokeModel()]); + } + + if (path === '/services/specifications/inputdata' && method === 'GET') { + return json(route, { + data_model: inputField(['text'], { required: true }), + datasets: inputField(['text'], { required: true }), + filters: inputField(['jsonObject']), + variables: inputField(['real', 'int', 'text'], { required: true, min_count: 1 }), + }); + } + + if (path === '/services/specifications/preprocessing' && method === 'GET') { + return json(route, [ + { + name: 'missing_values_handler', + label: 'Missing values', + desc: 'Decide what happens to missing values.', + order: 1, + parameters: {}, + }, + { + name: 'outlier_winsorizer', + label: 'Outlier winsorizer', + desc: 'Cap extreme values.', + order: 2, + parameters: {}, + }, + { + name: 'pca_column_creator', + label: 'PCA column creator', + desc: 'Create principal component columns.', + order: 3, + parameters: {}, + outputs: { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'real', + stattype: 'numerical', + column_count_param: 'n_components', + enumerations: null, + }, + ], + }, + }, + { + name: 'categorical_column_creator', + label: 'Categorical column creator', + desc: 'Create categorical columns from rules.', + order: 4, + parameters: {}, + outputs: { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'text', + stattype: 'nominal', + column_count_param: null, + enumerations: { + source_parameter_name: 'rules', + source_type: 'dict_keys', + additional_value_parameter_name: 'default_enumeration', + prefix_parameter_name: null, + }, + }, + ], + }, + }, + { + name: 'kmeans_cluster_creator', + label: 'K-means cluster creator', + desc: 'Create cluster columns.', + order: 5, + parameters: {}, + outputs: { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'int', + stattype: 'nominal', + column_count_param: null, + enumerations: { + source_parameter_name: 'k', + source_type: 'prefixed_range', + additional_value_parameter_name: null, + prefix_parameter_name: 'enumeration_prefix', + }, + }, + ], + }, + }, + ]); + } + + if (path === '/services/specifications/algorithms' && method === 'GET') { + return json(route, algorithmSpecs()); + } + + if (path === '/services/experiments/preview' && method === 'POST') { + const body = readBody(); + const name = body?.analysis?.algorithm?.name; + if (name === 'histogram') { + return json(route, histogramPayload(body)); + } + // Describe and outlier previews need featurewise rows. A real run needs status plus result. + if (!name || name === 'describe' || name === 'outlier_report') { + return json(route, describePayload(body)); + } + if (name === 'kmeans') { + const variables = body?.analysis?.algorithm?.y; + return json(route, kmeansReport(Array.isArray(variables) ? variables : [])); + } + return json(route, { status: 'success', result: resultFor(name) }); + } + + // Studio Run: like the backend, keep the successful result so Save can persist it as is. + if (path === '/services/experiments/runs' && method === 'POST') { + const body = readBody(); + const name = String(body?.analysis?.algorithm?.name ?? 'unknown'); + sequence += 1; + const uuid = `run-${sequence}`; + const result = name === 'describe' || name === 'outlier_report' ? describePayload(body) : resultFor(name); + runs.set(uuid, { analysis: body?.analysis ?? {}, result }); + return json(route, { uuid, status: 'success', result }); + } + + if (path === '/services/experiments' && method === 'GET') { + return json(route, { + experiments, + totalExperiments: experiments.length, + totalPages: experiments.length ? 1 : 0, + currentPage: 0, + }); + } + + // Save: persist a kept run by id. Like the backend, nothing runs again and the + // response carries no result. + if (path === '/services/experiments' && method === 'POST') { + const body = readBody(); + if (!body?.runId || !String(body?.name ?? '').trim()) { + return json(route, { message: 'runId and name are required.' }, 400); + } + const run = runs.get(String(body.runId)); + if (!run) return json(route, { message: 'This run is no longer available to save.' }, 404); + sequence += 1; + const stored: StoredExperiment = { + uuid: `exp-created-${sequence}`, + name: String(body.name), + created: new Date().toISOString(), + finished: new Date().toISOString(), + shared: false, + viewed: false, + status: 'success', + analysis: run.analysis, + result: run.result, + createdBy: STUDIO_USER, + }; + experiments.unshift(stored); + return json(route, { ...stored, result: null }, 201); + } + + const experimentMatch = path.match(/^\/services\/experiments\/([^/]+)$/); + if (experimentMatch && method === 'GET') { + const found = experiments.find((experiment) => experiment.uuid === experimentMatch[1]); + if (!found) return json(route, { message: 'not found' }, 404); + return json(route, found); + } + + if (experimentMatch && method === 'DELETE') { + const index = experiments.findIndex((experiment) => experiment.uuid === experimentMatch[1]); + if (index < 0) return json(route, { message: 'not found' }, 404); + experiments.splice(index, 1); + return route.fulfill({ status: 204 }); + } + + if (experimentMatch && method === 'PATCH') { + const found = experiments.find((experiment) => experiment.uuid === experimentMatch[1]); + if (!found) return json(route, { message: 'not found' }, 404); + const body = readBody(); + if (typeof body.name === 'string') found.name = body.name; + if (typeof body.shared === 'boolean') found.shared = body.shared; + return json(route, found); + } + + // The app called an endpoint this mock does not know. Answer with an error, not an empty success. + unhandled.push(`${method} ${path}`); + return json(route, { message: `No mock for ${method} ${path}` }, 501); + }); + + return { experiments, requests, unhandled }; +} diff --git a/e2e/experiment-studio.flows.spec.ts b/e2e/experiment-studio.flows.spec.ts new file mode 100644 index 000000000..d30814ada --- /dev/null +++ b/e2e/experiment-studio.flows.spec.ts @@ -0,0 +1,531 @@ +import { Page } from '@playwright/test'; +import { seedLinearExperiment, StudioApi } from './experiment-studio-api'; +import { expect, lastRequest, test } from './fixtures'; +import { + addKMeansColumn, + addKMeansOnColumns, + addNominalColumn, + addPcaColumns, + addVariable, + alphaInput, + applyFilters, + applyLongitudinalStrategy, + applyMeanImputationToAll, + applyOutlierChoice, + assignRole, + clearRole, + collapsePreprocessing, + expectAlgorithm, + expectAlgorithmBlocked, + goToAlgorithms, + goToDataHandling, + openAgeAndSex, + openLongitudinalAge, + openMissingValues, + openNewStudio, + prepareLinearRun, + runExperiment, + saveExperiment, + setAlpha, +} from './studio'; + +/** + * End-to-end experiment studio flows against a mocked `/services` API. + * Each test is named for the flow it covers so a failure says which one broke. + * Payload checks read the request the studio sent, since that is what Exaflow runs. + */ + +const PREVIEW = '/services/experiments/preview'; +const RUNS = '/services/experiments/runs'; + +/** Save must persist the kept run by id, never post the analysis to be run a second time. */ +function expectNoRerun(api: StudioApi) { + const saves = api.requests.filter((request) => request.method === 'POST' && request.path === '/services/experiments'); + expect(saves.length).toBeGreaterThan(0); + expect(saves.every((request) => request.body?.runId && !request.body?.analysis)).toBe(true); +} + +/** One preprocessing step from the latest Data review preview (`describe`). */ +function previewStep(api: StudioApi, name: string) { + const steps: { name: string; parameters: Record<string, any> }[] = + lastRequest(api, 'POST', PREVIEW, 'describe').analysis.preprocessing ?? []; + const step = steps.find((candidate) => candidate.name === name); + expect(step, `preview sends ${name}`).toBeTruthy(); + return step!.parameters; +} + +test.describe('experiment studio', () => { + test('new experiment starts empty and lists the variables it adds', async ({ page }) => { + await openNewStudio(page); + await expect(page).not.toHaveURL(/experimentId/); + await addVariable(page, 'Age'); + await expect(page.locator('.pool-card')).toContainText('Age'); + await expect(page.locator('.sub-header-cta')).toHaveText(/Continue with 1 variable/); + }); + + test('open a saved experiment from the dashboard', async ({ page, api }) => { + api.experiments.push(seedLinearExperiment()); + await page.goto('/experiments-dashboard'); + await page.getByRole('option', { name: /Baseline age model/ }).click(); + await expect(page.getByRole('heading', { name: 'Baseline age model' })).toBeVisible(); + await page.getByRole('button', { name: /Open in Studio/ }).click(); + await expect(page).toHaveURL(/mode=edit/); + await expect(page).toHaveURL(/experimentId=exp-seed-1/); + await expect(page.locator('.pool-chip', { hasText: 'Age' })).toBeVisible(); + await expect(page.locator('.pool-chip', { hasText: 'Sex' })).toBeVisible(); + await goToDataHandling(page); + await goToAlgorithms(page); + await expect(page.locator('h2.algorithm-details__title')).toHaveText('Linear regression'); + await expect(page.getByRole('button', { name: 'Remove Age from outcome' })).toBeVisible(); + await expect(page.getByRole('button', { name: 'Remove Sex from predictors' })).toBeVisible(); + }); + + test('edit a saved experiment and save the changed parameter', async ({ page, api }) => { + api.experiments.push(seedLinearExperiment()); + await page.goto('/experiment-studio?experimentId=exp-seed-1&mode=edit'); + await expect(page.locator('.pool-chip', { hasText: 'Age' })).toBeVisible(); + await goToDataHandling(page); + await goToAlgorithms(page); + await expect(alphaInput(page)).toHaveValue('0.05'); + await setAlpha(page, '0.2'); + await runExperiment(page); + await expect(page.getByText('age ~ sex').first()).toBeVisible(); + await saveExperiment(page, 'Edited age model'); + await expect(page.getByText('Experiment saved successfully!')).toBeVisible(); + + expectNoRerun(api); + const body = api.experiments[0] as any; + expect(body.name).toBe('Edited age model'); + expect(body.analysis).toEqual(lastRequest(api, 'POST', RUNS, 'linear_regression').analysis); + expect(body.analysis.algorithm).toMatchObject({ name: 'linear_regression', y: ['age'], x: ['sex'], parameters: { alpha: 0.2 } }); + // The saved preprocessing comes back unchanged. + expect(body.analysis.preprocessing).toContainEqual({ + name: 'missing_values_handler', + parameters: { strategies: { age: 'drop', sex: 'drop' } }, + }); + }); + + test('edit when specifications answer after the experiment', async ({ page, api }) => { + api.experiments.push(seedLinearExperiment()); + // A slow Exaflow catalog: the saved experiment arrives first, the specifications 2 s later. + await page.route('**/services/specifications/**', async (route) => { + await new Promise((resolve) => setTimeout(resolve, 2_000)); + await route.fallback(); + }); + + await page.goto('/experiment-studio?experimentId=exp-seed-1&mode=edit'); + await expect(page.locator('.pool-chip', { hasText: 'Age' })).toBeVisible(); + await goToDataHandling(page); + await goToAlgorithms(page); + await expect(page.locator('h2.algorithm-details__title')).toHaveText('Linear regression'); + await expect(alphaInput(page)).toHaveValue('0.05'); + await expect(page.getByRole('button', { name: 'Remove Age from outcome' })).toBeVisible(); + }); + + test('save sends the whole analysis', async ({ page, api }) => { + await prepareLinearRun(page); + await setAlpha(page, '0.2'); + await runExperiment(page); + await expect(page.getByText('age ~ sex').first()).toBeVisible(); + await saveExperiment(page, 'Age by sex'); + await expect(page.getByText('Experiment saved successfully!')).toBeVisible(); + await expect(page.getByText('Saved as “Age by sex”')).toBeVisible(); + + // Save sends only the run id and name; the kept run supplies the analysis and result. + expectNoRerun(api); + const save = api.requests.find((request) => request.method === 'POST' && request.path === '/services/experiments'); + expect(save?.body).toEqual({ runId: expect.stringMatching(/^run-/), name: 'Age by sex' }); + const body = api.experiments[0] as any; + expect(body.analysis).toEqual(lastRequest(api, 'POST', RUNS, 'linear_regression').analysis); + expect(body.name).toBe('Age by sex'); + expect(body.analysis.algorithm).toMatchObject({ name: 'linear_regression', y: ['age'], x: ['sex'], parameters: { alpha: 0.2 } }); + expect(body.analysis.inputdata).toMatchObject({ data_model: 'dementia:1', datasets: ['chuv', 'adni'], filters: null }); + expect(body.analysis.inputdata.variables).toEqual(expect.arrayContaining(['age', 'sex'])); + expect(body.analysis.preprocessing).toContainEqual({ + name: 'missing_values_handler', + parameters: { strategies: { age: 'drop', sex: 'drop' } }, + }); + }); + + test('re-open restores preprocessing and parameters', async ({ page }) => { + await prepareLinearRun(page); + await page.getByRole('button', { name: 'Back to Data Handling' }).click(); + await expect(page.getByRole('heading', { name: 'Data Handling Pipeline' })).toBeVisible(); + await applyMeanImputationToAll(page); + await page.getByRole('button', { name: 'Continue to Algorithm Selection' }).last().click(); + await expect(page.locator('h2.algorithm-details__title')).toHaveText('Linear regression'); + await setAlpha(page, '0.2'); + await runExperiment(page); + await expect(page.getByText('age ~ sex').first()).toBeVisible(); + await saveExperiment(page, 'Reopened age model'); + await expect(page.getByText('Experiment saved successfully!')).toBeVisible(); + + await page.getByRole('link', { name: 'My experiments' }).click(); + await expect(page).toHaveURL(/experiments-dashboard/); + await page.getByRole('option', { name: /Reopened age model/ }).click(); + await page.getByRole('button', { name: /Open in Studio/ }).click(); + await expect(page).toHaveURL(/mode=edit/); + await expect(page.locator('.pool-chip', { hasText: 'Age' })).toBeVisible(); + await goToDataHandling(page); + await expect(page.getByText(/Mean imputation|Imputation configured/).first()).toBeVisible(); + await goToAlgorithms(page); + await expect(page.locator('h2.algorithm-details__title')).toHaveText('Linear regression'); + await expect(alphaInput(page)).toHaveValue('0.2'); + }); + + test('algorithms follow the assigned roles', async ({ page }) => { + await openNewStudio(page); + await addVariable(page, 'Age'); + await addVariable(page, 'Sex'); + await addVariable(page, 'APOE'); + await goToDataHandling(page); + await goToAlgorithms(page); + + await assignRole(page, 'Age', 'Outcome'); + await assignRole(page, 'Sex', 'Predictor'); + await expectAlgorithm(page, 'Linear regression', 'Regression'); + await expectAlgorithm(page, 'Independent t-test', 'Statistical Tests'); + await expectAlgorithm(page, 'One-way ANOVA', 'Statistical Tests'); + await expectAlgorithm(page, 'Pearson correlation', 'Correlation'); + await expectAlgorithmBlocked(page, 'K-means'); + await expectAlgorithmBlocked(page, 'PCA'); + await expectAlgorithmBlocked(page, 'Logistic regression'); + + await clearRole(page, 'Sex', 'predictors'); + await expectAlgorithm(page, 'K-means', 'Clustering'); + await expectAlgorithm(page, 'PCA', 'Dimensionality Reduction'); + + await clearRole(page, 'Age', 'outcome'); + await assignRole(page, 'Sex', 'Outcome'); + await assignRole(page, 'Age', 'Predictor'); + await expectAlgorithm(page, 'Logistic regression', 'Regression'); + await expectAlgorithm(page, 'Naive Bayes gaussian', 'Classification'); + + await clearRole(page, 'Age', 'predictors'); + await assignRole(page, 'APOE', 'Predictor'); + await expectAlgorithm(page, 'Chi-squared', 'Statistical Tests'); + await expectAlgorithm(page, 'Naive Bayes categorical', 'Classification'); + }); +}); + +test.describe('preprocessing', () => { + const missingStrategies = [ + { radio: /Mean imputation/, label: 'Mean imputation', strategy: 'mean' }, + { radio: /Median imputation/, label: 'Median imputation', strategy: 'median' }, + { radio: /Constant value/, label: 'Constant value', strategy: 'constant', value: '70' }, + ]; + for (const { radio, label, strategy, value } of missingStrategies) { + test(`missing values: ${label}`, async ({ page, api }) => { + await openAgeAndSex(page); + const missing = await openMissingValues(page); + await missing.getByRole('radio', { name: radio }).click(); + if (value) await missing.getByPlaceholder('Value').fill(value); + await missing.getByRole('button', { name: 'Apply', exact: true }).click(); + await collapsePreprocessing(page); + await expect(page.getByText(`Age: ${label}`)).toBeVisible(); + + const parameters = previewStep(api, 'missing_values_handler'); + expect(parameters.strategies).toEqual({ age: strategy, sex: 'drop' }); + if (value) expect(JSON.stringify(parameters)).toContain(value); + }); + } + + test('missing values: switch a strategy back to removing rows', async ({ page, api }) => { + await openAgeAndSex(page); + const missing = await openMissingValues(page); + await missing.getByRole('radio', { name: /Mean imputation/ }).click(); + await missing.getByRole('button', { name: 'Apply', exact: true }).click(); + await collapsePreprocessing(page); + await expect(page.getByText('Age: Mean imputation')).toBeVisible(); + await page.getByRole('button', { name: /Missing Values Strategy/ }).click(); + const reopened = page.locator('app-station-card', { hasText: 'Missing Values' }); + await reopened.getByRole('radio', { name: /Remove rows/ }).click(); + await reopened.getByRole('button', { name: 'Apply', exact: true }).click(); + await collapsePreprocessing(page); + await expect(page.getByText(/Rows with missing values removed/)).toBeVisible(); + expect(previewStep(api, 'missing_values_handler').strategies).toEqual({ age: 'drop', sex: 'drop' }); + }); + + 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(); + 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(); + await outlier.getByRole('button', { name: 'Apply', exact: true }).click(); + await collapsePreprocessing(page); + await expect(page.getByText(/Extreme values capped/).first()).toBeVisible(); + + expect(previewStep(api, 'missing_values_handler').strategies).toEqual({ age: 'mean', sex: 'drop' }); + expect(Object.keys(previewStep(api, 'outlier_winsorizer').strategies)).toEqual(['age']); + }); + + const outlierMethods = [ + { method: 'Gaussian', strategy: 'gaussian' }, + { method: 'IQR', strategy: 'iqr' }, + { method: 'MAD', strategy: 'mad' }, + { method: 'Quantile', strategy: 'quantile' }, + ]; + for (const { method, strategy } of outlierMethods) { + test(`outliers: ${method} method`, async ({ page, api }) => { + await openAgeAndSex(page); + await applyOutlierChoice(page, method); + await expect(page.getByText(`(${method})`)).toBeVisible(); + expect(previewStep(api, 'outlier_winsorizer').strategies).toEqual({ age: strategy }); + }); + } + + const outlierTails = [ + { clip: 'Low only', tail: 'left' }, + { clip: 'High only', tail: 'right' }, + { clip: 'Both', tail: 'both' }, + ]; + for (const { clip, tail } of outlierTails) { + test(`outliers: clip ${clip.toLowerCase()}`, async ({ page, api }) => { + await openAgeAndSex(page); + await applyOutlierChoice(page, undefined, clip); + expect(previewStep(api, 'outlier_winsorizer').tails).toEqual({ age: tail }); + await page.getByRole('button', { name: /Outlier Winsorizer/ }).click(); + const card = page.locator('app-station-card', { hasText: 'Outlier Handling' }); + await expect(card.getByRole('radio', { name: clip, exact: true })).toHaveAttribute('aria-checked', 'true'); + }); + } + + const longitudinalStrategies = [ + { label: 'Diff (Visit 2 - Visit 1)', strategy: 'diff' }, + { label: 'Use visit 1', strategy: 'first' }, + { label: 'Use visit 2', strategy: 'second' }, + ]; + for (const { label, strategy } of longitudinalStrategies) { + test(`longitudinal: ${label}`, async ({ page, api }) => { + await openLongitudinalAge(page); + await applyLongitudinalStrategy(page, label); + expect(previewStep(api, 'longitudinal_transformer')).toEqual({ visit1: 'bl', visit2: 'm12', strategies: { age: strategy } }); + await page.getByRole('button', { name: /Longitudinal Comparison/ }).click(); + const card = page.locator('app-station-card', { hasText: 'Longitudinal Transformation' }); + await expect(card.locator('select').last().locator('option:checked')).toHaveText(label); + }); + } +}); + +test.describe('filters and transformations', () => { + test('filters on a numeric and a nominal variable', async ({ page, api }) => { + await openAgeAndSex(page); + await applyFilters(page, { female: true }); + await expect(page.getByText('Age is at least 60')).toBeVisible(); + await expect(page.getByText('Sex is Female')).toBeVisible(); + await expect(page.getByRole('button', { name: 'Continue to Algorithm Selection' }).last()).toBeEnabled(); + + expect(lastRequest(api, 'POST', PREVIEW, 'describe').analysis.inputdata.filters).toEqual({ + condition: 'AND', + rules: [ + expect.objectContaining({ field: 'age', operator: 'greater_or_equal', value: 60 }), + expect.objectContaining({ field: 'sex', operator: 'equal', value: 'F' }), + ], + valid: true, + }); + }); + + test('filter, preprocessing and algorithm reach the run', async ({ page, api }) => { + await openAgeAndSex(page); + await applyFilters(page); + await expect(page.getByText('Age is at least 60')).toBeVisible(); + const missing = await openMissingValues(page); + await missing.getByRole('radio', { name: /Median imputation/ }).click(); + await missing.getByRole('button', { name: 'Apply', exact: true }).click(); + await collapsePreprocessing(page); + await expect(page.getByText('Age: Median imputation')).toBeVisible(); + await goToAlgorithms(page); + await assignRole(page, 'Age', 'Outcome'); + await assignRole(page, 'Sex', 'Predictor'); + await expectAlgorithm(page, 'Linear regression', 'Regression'); + await runExperiment(page); + await expect(page.getByText('age ~ sex').first()).toBeVisible(); + await expect(page.getByText('Age >= 60 · 1 rule')).toBeVisible(); + await expect(page.getByText('Age: median imputation, Sex: remove rows')).toBeVisible(); + + const run = lastRequest(api, 'POST', RUNS, 'linear_regression').analysis; + expect(run.algorithm).toMatchObject({ y: ['age'], x: ['sex'] }); + expect(run.inputdata.filters.rules).toEqual([expect.objectContaining({ field: 'age', operator: 'greater_or_equal', value: 60 })]); + expect(run.preprocessing).toContainEqual({ + name: 'missing_values_handler', + parameters: { strategies: { age: 'median', sex: 'drop' } }, + }); + }); + + test('k-means cluster column', async ({ page, api }) => { + await openAgeAndSex(page); + await addKMeansColumn(page); + await expect(page.getByText('Column kmeans_cluster')).toBeVisible(); + expect(lastRequest(api, 'POST', PREVIEW, 'kmeans').analysis.algorithm.y).toEqual(['age']); + }); + + test('nominal column and k-means cluster column together', async ({ page, api }) => { + await openAgeAndSex(page); + await addNominalColumn(page, 'sex_group', 'female', 'other'); + await addKMeansColumn(page, { stepAdded: true }); + await expect(page.getByText('Nominal column · sex_group')).toBeVisible(); + await expect(page.locator('app-station-card', { hasText: 'Nominal column · sex_group' })).toContainText('Applied'); + await expect(page.getByText('Column kmeans_cluster')).toBeVisible(); + await expect(page.getByText('Sex is Female')).toBeVisible(); + + const steps = lastRequest(api, 'POST', PREVIEW, 'describe').analysis.preprocessing; + expect(steps).toContainEqual({ + name: 'categorical_column_creator', + parameters: { + new_column_names: ['sex_group'], + strategy: 'filter_rules', + rules: { female: { condition: 'AND', rules: [expect.objectContaining({ field: 'sex', operator: 'equal', value: 'F' })], valid: true } }, + default_enumeration: 'other', + }, + }); + expect(steps).toContainEqual({ + name: 'kmeans_cluster_creator', + parameters: expect.objectContaining({ + new_column_names: ['kmeans_cluster'], + k: 2, + enumeration_prefix: 'cluster_', + }), + }); + }); + + test('PCA columns then K-means on those outputs for logistic regression', async ({ page, api }) => { + await openNewStudio(page); + await addVariable(page, 'Age'); + await addVariable(page, 'MMSE'); + await addVariable(page, 'Sex'); + await goToDataHandling(page); + await addPcaColumns(page); + await addKMeansOnColumns(page, ['pca_pc1', 'pca_pc2'], { stepAdded: true }); + await expect(page.locator('app-pca-column-source .pca-column-source-applied-code')).toHaveText('pca_pc1, pca_pc2'); + await expect(page.locator('app-kmeans-cluster-source .kmeans-cluster-source-applied-code')).toHaveText('Column kmeans_cluster'); + + await goToAlgorithms(page); + await assignRole(page, 'Sex', 'Outcome'); + await assignRole(page, 'pca_pc1', 'Predictor'); + await assignRole(page, 'kmeans_cluster', 'Predictor'); + await expectAlgorithm(page, 'Logistic regression', 'Regression'); + await runExperiment(page); + + const run = lastRequest(api, 'POST', RUNS, 'logistic_regression').analysis; + const names = (run.preprocessing ?? []).map((step: { name: string }) => step.name); + const pcaIndex = names.indexOf('pca_column_creator'); + const kmeansIndex = names.indexOf('kmeans_cluster_creator'); + expect(pcaIndex).toBeGreaterThanOrEqual(0); + expect(kmeansIndex).toBeGreaterThan(pcaIndex); + expect(run.preprocessing[pcaIndex]).toEqual({ + name: 'pca_column_creator', + parameters: { + variables: ['age', 'mmse'], + selected_components: ['PC1', 'PC2'], + new_column_names: ['pca_pc1', 'pca_pc2'], + n_components: 2, + }, + }); + expect(run.preprocessing[kmeansIndex]).toEqual({ + name: 'kmeans_cluster_creator', + parameters: expect.objectContaining({ + new_column_names: ['kmeans_cluster'], + variables: ['pca_pc1', 'pca_pc2'], + k: 2, + enumeration_prefix: 'cluster_', + }), + }); + expect(run.inputdata.variables).toEqual(expect.arrayContaining(['age', 'mmse', 'sex'])); + for (const code of ['pca_pc1', 'pca_pc2', 'kmeans_cluster']) { + expect(run.inputdata.variables).not.toContain(code); + } + expect(run.algorithm).toMatchObject({ + name: 'logistic_regression', + y: ['sex'], + x: expect.arrayContaining(['pca_pc1', 'kmeans_cluster']), + }); + }); +}); + +test.describe('statistics previews', () => { + test('variable details show the Exaflow histogram', async ({ page, api }) => { + await openNewStudio(page); + const histogram = page.locator('#histogram-tab-panel'); + for (const [label, ticks] of [['Age', ['55', '80', 'Count']], ['Sex', ['Male', 'Female']]] as const) { + await page.getByRole('button', { name: 'Search variables or groups' }).click(); + await page.locator('#search-bar').fill(label); + await page.locator('.search-result-item', { hasText: label }).first().click(); + for (const tick of ticks) await expect(histogram).toContainText(tick); + expect(lastRequest(api, 'POST', PREVIEW, 'histogram').analysis.algorithm.y).toEqual([label.toLowerCase()]); + } + }); + + test('raw data preview shows the Exaflow describe statistics', async ({ page }) => { + await openAgeAndSex(page); + await page.locator('[data-guide="analysis-source-data"] .pipeline-node-toggle').click(); + const raw = page.locator('section.pipeline-node').first(); + const cells = (metric: RegExp) => raw.getByRole('row', { name: metric }).locator('th, td'); + // Two datasets, then the federated "all datasets" column Exaflow adds. + await expect(raw.getByRole('row').first()).toContainText(/CHUV.*ADNI.*All datasets/s); + await expect(cells(/^Datapoints/)).toHaveText(['Datapoints', '48', '47', '95']); + await expect(cells(/^Mean/)).toHaveText(['Mean', '71.6', '73.1', '72.34']); + await expect(cells(/^Standard Deviation/)).toHaveText(['Standard Deviation', '7.9', '8.4', '8.14']); + // Exaflow reports quartiles per dataset only. + await expect(cells(/^Q1/)).toHaveText(['Q1', '66', '67', 'N/A']); + }); +}); + +test.describe('run and save lifecycle', () => { + async function runLinearModel(page: Page) { + await prepareLinearRun(page); + await runExperiment(page); + await expect(page.getByText('age ~ sex').first()).toBeVisible(); + } + + /** Answer the save of the kept run with `status`, as the backend does when it fails or the run expired. */ + async function failSave(page: Page, status: number) { + await page.route('**/services/experiments', (route) => + route.request().method() === 'POST' ? route.fulfill({ status, json: {} }) : route.fallback(), + ); + } + + test('save asks for a fresh run once the kept run has expired', async ({ page }) => { + await failSave(page, 404); + await runLinearModel(page); + await saveExperiment(page, 'Expired save'); + await expect( + page.getByRole('alert').filter({ hasText: 'Saving failed: This result is no longer available to save. Run the experiment again.' }), + ).toBeVisible(); + await expect(page.getByText('Experiment saved successfully!')).toHaveCount(0); + }); + + test('save shows a failed request', async ({ page }) => { + await failSave(page, 500); + await runLinearModel(page); + await saveExperiment(page, 'Rejected save'); + await expect(page.getByRole('alert').filter({ hasText: 'Saving failed: Failed to save the experiment. Please try again.' })).toBeVisible(); + // The reviewed result stays on screen, so the user can retry without running again. + await expect(page.getByText('age ~ sex').first()).toBeVisible(); + }); + + test('run shows the engine error', async ({ page }) => { + await page.route('**/services/experiments/runs', (route) => + route.request().postDataJSON()?.analysis?.algorithm?.name === 'linear_regression' + ? route.fulfill({ json: { status: 'error', result: { message: 'Singular matrix' } } }) + : route.fallback(), + ); + await prepareLinearRun(page); + await runExperiment(page); + await expect(page.getByText('The experiment did not finish')).toBeVisible(); + await expect(page.getByText('Singular matrix')).toBeVisible(); + }); + + test('linear regression result shows the Exaflow fields', async ({ page }) => { + await runLinearModel(page); + const result = page.locator('app-execution-panel'); + await expect(result).toContainText('R²'); + await expect(result).toContainText('0.042'); + await expect(result).toContainText('F(1, 98) = 4.31'); + const sexRow = result.getByRole('row', { name: /Sex · Male/ }); + await expect(sexRow).toContainText('2.05'); + await expect(sexRow).toContainText('0.090 to 4.01'); + await expect(result.getByRole('row', { name: /Intercept/ })).toContainText('71.20'); + }); +}); diff --git a/e2e/fixtures.ts b/e2e/fixtures.ts new file mode 100644 index 000000000..2d70c19ba --- /dev/null +++ b/e2e/fixtures.ts @@ -0,0 +1,31 @@ +import { expect, test as base } from '@playwright/test'; +import { installExperimentStudioApi, RecordedRequest, StudioApi } from './experiment-studio-api'; + +/** + * Every test gets a page whose `/services` calls go to the in-browser mock (`api`). + * Guided tours are marked as seen, and the test fails if the app called an endpoint + * the mock does not answer. + */ +export const test = base.extend<{ api: StudioApi }>({ + api: [async ({ page }, use) => { + await page.addInitScript(() => { + localStorage.setItem('mip.guide.experiments-dashboard.autostarted', 'true'); + localStorage.setItem('mip.guide.experiment-studio.seen', 'true'); + localStorage.setItem('mip.guide.experiment-studio.autostarted', 'true'); + }); + const api = await installExperimentStudioApi(page); + await use(api); + expect(api.unhandled, 'calls to /services the mock does not answer').toEqual([]); + }, { auto: true }], +}); + +export { expect }; + +/** The body of the latest request to `path` (and algorithm `name`, for runs and previews). */ +export function lastRequest(api: StudioApi, method: string, path: string, name?: string): RecordedRequest['body'] { + const match = api.requests.filter( + (request) => request.method === method && request.path === path && (!name || request.body?.analysis?.algorithm?.name === name), + ); + expect(match.length, `${method} ${path}${name ? ` (${name})` : ''} was sent`).toBeGreaterThan(0); + return match[match.length - 1].body; +} diff --git a/e2e/studio.ts b/e2e/studio.ts new file mode 100644 index 000000000..146d6b767 --- /dev/null +++ b/e2e/studio.ts @@ -0,0 +1,274 @@ +import { expect, Locator, Page } from '@playwright/test'; + +/** Page steps shared by the experiment studio and dashboard specs. */ + +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(); +} + +export async function addVariable(page: Page, label: string) { + await page.getByRole('button', { name: 'Search variables or groups' }).click(); + await page.locator('#search-bar').fill(label); + await page.locator('.search-result-item', { hasText: label }).first().click(); + await page.locator('[data-guide="guide-add-variable"]').click(); + await expect(page.locator('.pool-chip', { hasText: label })).toBeVisible(); +} + +async function continueFromHeader(page: Page, name: RegExp) { + const cta = page.locator('.sub-header-cta'); + await expect(cta).toBeEnabled(); + await expect(cta).toHaveText(name); + await cta.click(); +} + +export async function goToDataHandling(page: Page) { + await continueFromHeader(page, /Continue with/); + await expect(page.getByRole('heading', { name: 'Data Handling Pipeline' })).toBeVisible(); +} + +export async function goToAlgorithms(page: Page) { + await continueFromHeader(page, /Continue to Algorithm Selection/); + await expect(page.getByRole('heading', { name: 'Algorithm', exact: true })).toBeVisible(); +} + +export async function openAgeAndSex(page: Page) { + await openNewStudio(page); + await addVariable(page, 'Age'); + await addVariable(page, 'Sex'); + await goToDataHandling(page); +} + +export async function assignRole(page: Page, variable: string, role: 'Outcome' | 'Predictor') { + // The role band is either open ("Done") or folded ("+ Add"). Wait for one, then open it. + const band = page.locator('app-algorithm-role-assignment'); + const add = band.getByRole('button', { name: '+ Add' }); + await expect(add.or(band.getByRole('button', { name: 'Done' }))).toBeVisible(); + if (await add.isVisible()) await add.click(); + await band.locator('li.row', { hasText: variable }).getByRole('radio', { name: role }).click(); + const slot = role === 'Outcome' ? 'outcome' : 'predictors'; + await expect(page.getByRole('button', { name: `Remove ${variable} from ${slot}` })).toBeVisible(); +} + +export async function clearRole(page: Page, variable: string, slot: 'outcome' | 'predictors') { + await page.getByRole('button', { name: `Remove ${variable} from ${slot}` }).click(); +} + +export async function chooseAlgorithm(page: Page, label: string) { + await page.locator('#algo-search').fill(label); + const tile = page.locator('.algo-tile', { hasText: label }); + await expect(tile).toBeVisible(); + await tile.click(); + await expect(page.locator('h2.algorithm-details__title')).toHaveText(label); +} + +export async function expectAlgorithm(page: Page, label: string, category: string) { + await chooseAlgorithm(page, label); + await expect(page.locator('.inspector-tag')).toHaveText(category); + await expect(page.locator('.algo-tile.selected', { hasText: label })).not.toHaveClass(/disabled-algo/); +} + +export async function expectAlgorithmBlocked(page: Page, label: string) { + // The catalog hides methods that cannot run until "All" is selected. + const allMethods = page.getByRole('button', { name: /^All\b/ }); + if ((await allMethods.getAttribute('aria-pressed')) !== 'true') { + await allMethods.click(); + } + await page.locator('#algo-search').fill(label); + const tile = page.locator('.algo-tile', { hasText: label }); + await expect(tile).toBeVisible(); + await expect(tile).toHaveClass(/disabled-algo/); + await expect(tile.locator('.algo-why')).not.toHaveText(''); +} + +export function alphaInput(page: Page): Locator { + return page.locator('.config-field', { hasText: 'Alpha' }).locator('input'); +} + +/** Alpha is an advanced parameter: it sits in the closed "Advanced (n)" disclosure. */ +export async function setAlpha(page: Page, value: string) { + const advanced = page.locator('details.config-advanced'); + if ((await advanced.getAttribute('open')) === null) { + await advanced.locator('summary').click(); + } + await alphaInput(page).fill(value); + await expect(alphaInput(page)).toHaveValue(value); +} + +/** Age as outcome, Sex as predictor, linear regression selected. */ +export async function prepareLinearRun(page: Page) { + await openAgeAndSex(page); + await goToAlgorithms(page); + await assignRole(page, 'Age', 'Outcome'); + await assignRole(page, 'Sex', 'Predictor'); + await expectAlgorithm(page, 'Linear regression', 'Regression'); +} + +export async function runExperiment(page: Page) { + await page.locator('.sub-header-cta', { hasText: 'Run experiment' }).click(); +} + +/** Save the run under `name`. Resolves once the save request has been sent. */ +export async function saveExperiment(page: Page, name: string) { + await page.getByRole('button', { name: 'Save experiment' }).click(); + await page.locator('#save-as-name-input').fill(name); + await page.locator('.result-save-form').getByRole('button', { name: 'Save', exact: true }).click(); +} + +export async function collapsePreprocessing(page: Page) { + // After Apply the step is either still open or folded into its step list; its title folds it. + const section = page.locator('[data-guide="analysis-preprocessing"]'); + const open = section.locator('.pipeline-node-toggle[aria-expanded="true"]'); + await expect(open.or(section.getByRole('list', { name: 'Preprocessing 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' }); + await missing.getByRole('button', { name: /^Age/ }).click(); + return missing; +} + +/** Set every numerical variable to mean imputation with the batch menu. */ +export async function applyMeanImputationToAll(page: Page) { + await page.getByRole('button', { name: 'Customize rules' }).click(); + const missing = page.locator('app-station-card', { hasText: 'Missing Values' }); + await missing.getByRole('button', { name: 'Set all' }).click(); + await page.getByRole('menuitem', { name: 'Set all numerical to Mean imputation' }).click(); + await missing.getByRole('button', { name: 'Apply', exact: true }).click(); + await expect(missing).toContainText(/Mean imputation|Applied/); +} + +/** 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(); + 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(); + if (method) await outlier.getByRole('radio', { name: method, exact: true }).click(); + if (clip) await outlier.getByRole('radio', { name: clip, exact: true }).click(); + await outlier.getByRole('button', { name: 'Apply', exact: true }).click(); + await collapsePreprocessing(page); + await expect(page.getByText(/Extreme values capped/)).toBeVisible(); +} + +async function selectStroke(page: Page) { + const chip = page.locator('.study-context-chip'); + await chip.click(); + await page.locator('.data-model-chip', { hasText: 'Stroke' }).click(); + await expect(chip).toContainText('Stroke'); + if ((await chip.getAttribute('aria-expanded')) === 'true') await chip.click(); +} + +export async function openLongitudinalAge(page: Page) { + await openNewStudio(page); + await selectStroke(page); + await addVariable(page, 'Age'); + await goToDataHandling(page); +} + +export async function applyLongitudinalStrategy(page: Page, label: string) { + await page.getByRole('button', { name: 'Customize rules' }).click(); + 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 expect(page.getByText('Baseline vs Month 12')).toBeVisible(); +} + +async function addCondition(scope: Locator, variable: string) { + // An empty builder offers "Add condition"; once a rule exists it offers "Condition" (and "Group"). + await scope.getByRole('button', { name: 'Add condition' }).or(scope.getByRole('button', { name: 'Condition', exact: true })).click(); + const fields = scope.locator('.condition-fields').last(); + await fields.getByPlaceholder('Type to find a variable').fill(variable); + return fields; +} + +/** Filter rows to Age >= 60 (and Sex = Female when `female` is set). */ +export async function applyFilters(page: Page, { female = false } = {}) { + await page.getByTitle('Open the rule builder').click(); + const modal = page.locator('app-filter-config-modal:visible'); + const age = await addCondition(modal, 'Age'); + const operator = age.locator('select.filter-field-input').first(); + await expect(operator).toContainText('is at least'); + await operator.selectOption({ label: 'is at least' }); + await age.getByPlaceholder('Value').fill('60'); + if (female) { + const sex = await addCondition(modal, 'Sex'); + await sex.locator('select.filter-field-input').last().selectOption({ label: 'Female' }); + } + await page.locator('.filter-workflow-section').getByRole('button', { name: 'Apply filters' }).click(); +} + +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 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); + const editor = page.locator('.transformation-rule-editor.is-open'); + const fields = editor.locator('.condition-fields').first(); + await fields.getByPlaceholder('Type to find a variable').fill('Sex'); + await fields.locator('select.filter-field-input').last().selectOption({ label: 'Female' }); + await editor.getByRole('button', { name: 'Done' }).click(); + await page.getByPlaceholder('unknown').fill(fallback); + await expect(page.getByText('Sex is Female')).toBeVisible(); +} + +/** `stepAdded`: the transformation step is already open (another column was added first). */ +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(); + 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(); + await source.getByRole('button', { name: 'Use as cluster column' }).click(); + await page.getByRole('button', { name: 'Back to column' }).click(); + await expect(source.getByText('Column kmeans_cluster')).toBeVisible(); + await expect(source.getByText(/2 clusters from age/)).toBeVisible(); +} + +/** + * Fit-mode PCA card on the selected numerical CDEs (defaults PC1+PC2 → pca_pc1, pca_pc2). + * `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(); + 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(); + await expect(source.getByText('pca_pc1, pca_pc2')).toBeVisible(); +} + +/** + * Fit-mode K-means card that clusters only on the given earlier columns (e.g. PCA outputs). + * 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(); + const source = page.locator('app-kmeans-cluster-source').last(); + await source.getByRole('button', { name: 'Fit on selected variables' }).click(); + const keep = new Set(columns); + for (const button of await source.locator('.kmeans-cluster-source-var').all()) { + const label = (await button.innerText()).replace(/\s+/g, ' ').trim(); + const code = columns.find((candidate) => label.includes(candidate)) + ?? (label.includes('Age') ? 'age' : label.includes('MMSE') ? 'mmse' : label); + const selected = (await button.getAttribute('aria-pressed')) === 'true'; + const wanted = keep.has(code); + if (selected !== wanted) await button.click(); + } + await source.locator('#kmeans-fit-k').fill('2'); + await source.getByRole('button', { name: 'Add cluster column' }).click(); + // applyFit emits runRequested → preview hides the source; leave preview to assert the applied row. + const card = page.locator('app-station-card', { hasText: 'K-means cluster column · kmeans_cluster' }); + await expect(card).toContainText('Applied'); + await card.getByRole('button', { name: 'Back to column' }).click(); + await expect(source.getByText('Column kmeans_cluster')).toBeVisible(); +} diff --git a/nginx-websocket-map.conf b/nginx-websocket-map.conf new file mode 100644 index 000000000..32af4c3ce --- /dev/null +++ b/nginx-websocket-map.conf @@ -0,0 +1,4 @@ +map $http_upgrade $connection_upgrade { + default upgrade; + '' close; +} diff --git a/nginx.conf.template b/nginx.conf.template index e6c16b470..5da14453c 100644 --- a/nginx.conf.template +++ b/nginx.conf.template @@ -13,20 +13,97 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } - # Route notebook traffic to Jupyter + # Angular shell route for the notebook page (must not proxy to JupyterHub). + location = /${JUPYTER_CONTEXT} { + set $notebook_enabled "${NOTEBOOK_ENABLED}"; + if ($notebook_enabled != "1") { + return 404; + } + if ($uri != "/notebook") { + return 302 /notebook; + } + try_files /index.html =404; + } + + # Without this, /notebook 301s to /notebook/ and the prefix location proxies to + # JupyterHub, which redirects the top window to /notebook/user/.../lab (no shell). + location = /${JUPYTER_CONTEXT}/ { + set $notebook_enabled "${NOTEBOOK_ENABLED}"; + if ($notebook_enabled != "1") { + return 404; + } + if ($uri != "/notebook/") { + return 302 /notebook; + } + try_files /index.html =404; + } + + # Keep single-user Lab URLs inside the iframe. Top-level navigations (refresh, + # Hub redirects, iframe breakout) should return to the platform shell route. + location ~ ^/${JUPYTER_CONTEXT}/user/ { + set $notebook_enabled "${NOTEBOOK_ENABLED}"; + if ($notebook_enabled != "1") { + return 404; + } + + # Top-level browser navigations (refresh, Hub redirect, address-bar URL) must + # use the Angular shell at /notebook. Iframe loads use Sec-Fetch-Dest: iframe. + if ($http_sec_fetch_dest = "document") { + return 302 /notebook; + } + + proxy_pass http://${JUPYTER_SERVER}; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + } + + # After Hub OAuth, JupyterHub redirects to /hub/home. Send top-level navigations + # back to the Angular shell while keeping Hub API paths proxied to JupyterHub. + location = /${JUPYTER_CONTEXT}/hub/home { + set $notebook_enabled "${NOTEBOOK_ENABLED}"; + if ($notebook_enabled != "1") { + return 404; + } + + if ($http_sec_fetch_dest = "document") { + return 302 /notebook; + } + + proxy_pass http://${JUPYTER_SERVER}; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + } + + # Route remaining notebook traffic to JupyterHub (hub API, websockets, etc.). location /${JUPYTER_CONTEXT}/ { set $notebook_enabled "${NOTEBOOK_ENABLED}"; if ($notebook_enabled != "1") { return 404; } - set $jupyter_upstream "${JUPYTER_SERVER}"; - proxy_pass http://$jupyter_upstream; + proxy_pass http://${JUPYTER_SERVER}; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "upgrade"; + proxy_set_header Connection $connection_upgrade; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; } # Fallback to index.html for Angular routing diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..cc93f2eec --- /dev/null +++ b/package-lock.json @@ -0,0 +1,7849 @@ +{ + "name": "fl-platform", + "version": "0.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "fl-platform", + "version": "0.0.0", + "dependencies": { + "@angular/cdk": "^22.2.4", + "@angular/common": "^22.2.3", + "@angular/compiler": "^22.2.3", + "@angular/core": "^22.2.3", + "@angular/forms": "^22.2.3", + "@angular/material": "^22.2.4", + "@angular/platform-browser": "^22.2.3", + "@angular/router": "^22.2.3", + "@fortawesome/fontawesome-free": "^7.3.1", + "d3": "^7.9.0", + "echarts": "6.1.0", + "html2canvas": "^1.4.1", + "jspdf": "^4.2.1", + "jspdf-autotable": "^5.0.9", + "jszip": "^3.10.2", + "ngx-echarts": "^22.0.0", + "rxjs": "~7.8.2", + "tslib": "^2.8.1" + }, + "devDependencies": { + "@angular/build": "22.2.4", + "@angular/cli": "22.2.4", + "@angular/compiler-cli": "^22.2.3", + "@playwright/test": "^1.64.1", + "@types/d3": "^7.4.3", + "@vitest/browser-playwright": "5.0.5", + "typescript": "6.0.3", + "vitest": "5.0.5" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": ">=10.0.0" + } + }, + "node_modules/@ampproject/remapping": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz", + "integrity": "sha512-30iZtAPgz+LTIYoeivqYo853f02jBYSd5uGnGpkFV0M3xOt9aN73erkgYAmZU43x4VfqcnLxW9Kpg3R5LC4YYw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@jridgewell/gen-mapping": "^0.3.5", + "@jridgewell/trace-mapping": "^0.3.24" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@angular-devkit/architect": { + "version": "0.2202.4", + "resolved": "https://registry.npmjs.org/@angular-devkit/architect/-/architect-0.2202.4.tgz", + "integrity": "sha512-hafew1tft3+bnv+DjAtLDEq/Yx5/UyAz/+M4VC5EWQQhcFCN81X47szTX3F9dNeiZIQb8d/WmCnmlbHz6CyupA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.2.4", + "rxjs": "7.8.2" + }, + "bin": { + "architect": "bin/cli.js" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + } + }, + "node_modules/@angular-devkit/core": { + "version": "22.2.4", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-22.2.4.tgz", + "integrity": "sha512-HDlsVK/qN7p5ttKgPvVLo87yrN0UcHR7OyQo1n/lvvMQcYrA0n01E+ix4t+q4C04ifpMbNQtrK0I1222OKi99w==", + "dev": true, + "license": "MIT", + "dependencies": { + "ajv": "8.20.0", + "ajv-formats": "3.0.1", + "jsonc-parser": "3.3.1", + "picomatch": "4.0.7", + "rxjs": "7.8.2", + "source-map": "0.8.0" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + }, + "peerDependencies": { + "chokidar": "^5.0.0" + }, + "peerDependenciesMeta": { + "chokidar": { + "optional": true + } + } + }, + "node_modules/@angular-devkit/schematics": { + "version": "22.2.4", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-22.2.4.tgz", + "integrity": "sha512-9pRNtcZN87yM7KQX+HTxGatrm86y30JxVwkF2WNG1pR0e9yeiYWc1KQ5hRS1Nq1LULoP95HApc+TBXqUXzF4Ag==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.2.4", + "jsonc-parser": "3.3.1", + "magic-string": "1.4.1", + "ora": "9.4.1", + "rxjs": "7.8.2" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + } + }, + "node_modules/@angular/build": { + "version": "22.2.4", + "resolved": "https://registry.npmjs.org/@angular/build/-/build-22.2.4.tgz", + "integrity": "sha512-Emb3pxRkvf749NQ5cz4ux2h9Cnbxx3FmcupdXIaoRxIyNXM8JxjFDCLZTe0yGGOWv5+hSaoR7XESOcN134vBPw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@ampproject/remapping": "2.3.0", + "@angular-devkit/architect": "0.2202.4", + "@babel/core": "8.0.5", + "@inquirer/confirm": "6.3.2", + "@parcel/watcher": "2.6.0", + "@vitejs/plugin-basic-ssl": "2.3.0", + "beasties": "0.5.4", + "browserslist": "^4.26.0", + "chokidar": "5.0.0", + "esbuild": "0.28.2", + "https-proxy-agent": "9.1.0", + "jsonc-parser": "3.3.1", + "listr2": "11.1.0", + "magic-string": "1.4.1", + "mrmime": "2.0.1", + "oxc-parser": "0.150.0", + "parse5-html-rewriting-stream": "8.0.1", + "picomatch": "4.0.7", + "piscina": "5.3.2", + "rolldown": "1.2.8", + "sass": "1.104.1", + "sass-embedded": "1.104.1", + "semver": "7.8.5", + "source-map-support": "0.5.21", + "tinyglobby": "0.2.17", + "vite": "8.3.0", + "xxhash-wasm": "1.1.0" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + }, + "optionalDependencies": { + "lmdb": "3.5.6" + }, + "peerDependencies": { + "@angular/compiler": "^22.0.0", + "@angular/compiler-cli": "^22.0.0", + "@angular/core": "^22.0.0", + "@angular/localize": "^22.0.0", + "@angular/platform-browser": "^22.0.0", + "@angular/platform-server": "^22.0.0", + "@angular/service-worker": "^22.0.0", + "@angular/ssr": "^22.2.4", + "istanbul-lib-instrument": "^6.0.0", + "karma": "^6.4.0", + "less": "^4.2.0", + "ng-packagr": "^22.0.0", + "postcss": "^8.4.0", + "rollup": "^4.0.0", + "tailwindcss": "^2.0.0 || ^3.0.0 || ^4.0.0", + "tslib": "^2.3.0", + "typescript": ">=6.0 <6.1", + "vitest": "^4.0.8 || ^5.0.0" + }, + "peerDependenciesMeta": { + "@angular/core": { + "optional": true + }, + "@angular/localize": { + "optional": true + }, + "@angular/platform-browser": { + "optional": true + }, + "@angular/platform-server": { + "optional": true + }, + "@angular/service-worker": { + "optional": true + }, + "@angular/ssr": { + "optional": true + }, + "istanbul-lib-instrument": { + "optional": true + }, + "karma": { + "optional": true + }, + "less": { + "optional": true + }, + "ng-packagr": { + "optional": true + }, + "postcss": { + "optional": true + }, + "rollup": { + "optional": true + }, + "tailwindcss": { + "optional": true + }, + "vitest": { + "optional": true + } + } + }, + "node_modules/@angular/cdk": { + "version": "22.2.4", + "resolved": "https://registry.npmjs.org/@angular/cdk/-/cdk-22.2.4.tgz", + "integrity": "sha512-c/DSWyCnN17f/EIl9Y1zVmSEWCAEdp4yeS53WWiHghIPg3kDX32Qbe/9b0/hlYfxcBSdPxtcgZMLSvqtu6hvkA==", + "license": "MIT", + "dependencies": { + "parse5": "^8.0.0", + "tslib": "^2.3.0" + }, + "peerDependencies": { + "@angular/common": "^22.0.0 || ^23.0.0", + "@angular/core": "^22.0.0 || ^23.0.0", + "@angular/forms": "^22.0.0 || ^23.0.0", + "@angular/platform-browser": "^22.0.0 || ^23.0.0", + "rxjs": "^6.5.3 || ^7.4.0" + } + }, + "node_modules/@angular/cli": { + "version": "22.2.4", + "resolved": "https://registry.npmjs.org/@angular/cli/-/cli-22.2.4.tgz", + "integrity": "sha512-luD9gNbMV7ME7mv9cUatVE+WF8Ecqa8v9Zo47YZK/6EPQUKdLxeosGWlHtrvBXElK1YehHItrUbawtZQmQwwlg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/architect": "0.2202.4", + "@angular-devkit/core": "22.2.4", + "@angular-devkit/schematics": "22.2.4", + "@inquirer/prompts": "8.7.2", + "@listr2/prompt-adapter-inquirer": "4.2.7", + "@modelcontextprotocol/server": "2.3.1", + "@schematics/angular": "22.2.4", + "jsonc-parser": "3.3.1", + "listr2": "11.1.0", + "npm-package-arg": "14.0.0", + "parse5-html-rewriting-stream": "8.0.1", + "semver": "7.8.5", + "yargs": "18.1.0", + "zod": "4.6.6" + }, + "bin": { + "ng": "bin/ng.js" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + } + }, + "node_modules/@angular/common": { + "version": "22.2.3", + "resolved": "https://registry.npmjs.org/@angular/common/-/common-22.2.3.tgz", + "integrity": "sha512-sRzcvxzurbkCDVFJMguNVgsHds3NvDj5qbZ44NHjH7J9MBk5oZYXHppdHVm8AolBhCXAWvMk0vx+NO8Bt4rS7A==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0" + }, + "peerDependencies": { + "@angular/core": "22.2.3", + "rxjs": "^6.5.3 || ^7.4.0" + } + }, + "node_modules/@angular/compiler": { + "version": "22.2.3", + "resolved": "https://registry.npmjs.org/@angular/compiler/-/compiler-22.2.3.tgz", + "integrity": "sha512-GjDkmOBqW+6RJTeW5mlLRivf6Zy7Ed0auVh+DYD67d4UF4Sk17F1vIt8puF4CQ2PIezlHh7oOnermCBNHAOGkQ==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0" + } + }, + "node_modules/@angular/compiler-cli": { + "version": "22.2.3", + "resolved": "https://registry.npmjs.org/@angular/compiler-cli/-/compiler-cli-22.2.3.tgz", + "integrity": "sha512-em7Ht0Dv7cN7vZgvl2fl1yscAs+zUG2Vr2YKpVZ/rOWuTL2JZyT1qIfKhC4wQoo/3lN8fiUIwQMpflSGzIEPng==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/core": "8.0.1", + "@jridgewell/sourcemap-codec": "^1.4.14", + "chokidar": "^5.0.0", + "convert-source-map": "^1.5.1", + "reflect-metadata": "^0.2.0", + "semver": "^7.0.0", + "tslib": "^2.3.0", + "yargs": "^18.0.0" + }, + "bin": { + "ng-xi18n": "bundles/src/bin/ng_xi18n.js", + "ngc": "bundles/src/bin/ngc.js" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0" + }, + "peerDependencies": { + "@angular/compiler": "22.2.3", + "typescript": ">=6.0 <6.1" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/@angular/compiler-cli/node_modules/@babel/core": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-8.0.1.tgz", + "integrity": "sha512-5FgxM4dLQpMJHSiVATk8foW263dVHQHBVpXYiimNECVWG01f4nFyEbQixeT6Mwvg7TayREJ2gpKl3o2RoMdnqw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/code-frame": "^8.0.0", + "@babel/generator": "^8.0.0", + "@babel/helper-compilation-targets": "^8.0.0", + "@babel/helpers": "^8.0.0", + "@babel/parser": "^8.0.0", + "@babel/template": "^8.0.0", + "@babel/traverse": "^8.0.0", + "@babel/types": "^8.0.0", + "@types/gensync": "^1.0.5", + "convert-source-map": "^2.0.0", + "empathic": "^2.0.1", + "gensync": "^1.0.0-beta.2", + "import-meta-resolve": "^4.2.0", + "json5": "^2.2.3", + "obug": "^2.1.1", + "semver": "^7.7.3" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/babel" + } + }, + "node_modules/@angular/compiler-cli/node_modules/@babel/core/node_modules/convert-source-map": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", + "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@angular/core": { + "version": "22.2.3", + "resolved": "https://registry.npmjs.org/@angular/core/-/core-22.2.3.tgz", + "integrity": "sha512-ponarFrAZsPutT+htRt3PEmkt2/IB7DyrBy2f48sxsIx3ZA6xw09i2HOEBo+VBphlN6B/ynK9q+o04HPhdJ+dQ==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0" + }, + "peerDependencies": { + "@angular/compiler": "22.2.3", + "rxjs": "^6.5.3 || ^7.4.0", + "zone.js": "~0.15.0 || ~0.16.0" + }, + "peerDependenciesMeta": { + "@angular/compiler": { + "optional": true + }, + "zone.js": { + "optional": true + } + } + }, + "node_modules/@angular/forms": { + "version": "22.2.3", + "resolved": "https://registry.npmjs.org/@angular/forms/-/forms-22.2.3.tgz", + "integrity": "sha512-TKP8FpeRCY/++vKWsY8GC1J2sNtEkdNNBPdgnMAMLTwheW5JzjwVatuQlbuZEx3WaAjbll1Ux3d+eHeqAVrtQg==", + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.0.0", + "tslib": "^2.3.0", + "zod": "^4.0.10" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0" + }, + "peerDependencies": { + "@angular/common": "22.2.3", + "@angular/core": "22.2.3", + "@angular/platform-browser": "22.2.3", + "rxjs": "^6.5.3 || ^7.4.0" + } + }, + "node_modules/@angular/material": { + "version": "22.2.4", + "resolved": "https://registry.npmjs.org/@angular/material/-/material-22.2.4.tgz", + "integrity": "sha512-3CFxsj+U4fGQFwY1ZBOAltazAQucNupbzRFIx2d4aW4k94R+C6kQ9EoRi5evRNUtXa2boeF/DuVjY6fBtNfLig==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "peerDependencies": { + "@angular/cdk": "22.2.4", + "@angular/common": "^22.0.0 || ^23.0.0", + "@angular/core": "^22.0.0 || ^23.0.0", + "@angular/forms": "^22.0.0 || ^23.0.0", + "@angular/platform-browser": "^22.0.0 || ^23.0.0", + "rxjs": "^6.5.3 || ^7.4.0" + } + }, + "node_modules/@angular/platform-browser": { + "version": "22.2.3", + "resolved": "https://registry.npmjs.org/@angular/platform-browser/-/platform-browser-22.2.3.tgz", + "integrity": "sha512-eWZGsbG4bSrSg9UB/NE8wORGVrh1u+x6rirPGpMWqq/mTi6q+nS+pnPxAxmiEs8wmPH5nCz5b2pRKQeYUkhAGA==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0" + }, + "peerDependencies": { + "@angular/animations": "22.2.3", + "@angular/common": "22.2.3", + "@angular/core": "22.2.3" + }, + "peerDependenciesMeta": { + "@angular/animations": { + "optional": true + } + } + }, + "node_modules/@angular/router": { + "version": "22.2.3", + "resolved": "https://registry.npmjs.org/@angular/router/-/router-22.2.3.tgz", + "integrity": "sha512-h4C3iJ3OXM9HItErmTo9fFrFRDj1DSqbvweHAWl8eYJvyuhDD80gBNgdpN8/5gpt9K1HV+PXk0YbyjyQ0zMfPA==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0" + }, + "peerDependencies": { + "@angular/common": "22.2.3", + "@angular/core": "22.2.3", + "@angular/platform-browser": "22.2.3", + "rxjs": "^6.5.3 || ^7.4.0" + } + }, + "node_modules/@babel/code-frame": { + "version": "8.0.6", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-8.0.6.tgz", + "integrity": "sha512-uMHhvmDmwFiLuiozaoRYTZHqMFuu3fem/X9JY78lohAuLoh8LSGGC8JqUzo2cVuBsBxkjMZNk56CE7ock8Xxkw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-validator-identifier": "^8.0.6", + "js-tokens": "^10.0.0" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/compat-data": { + "version": "8.0.5", + "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-8.0.5.tgz", + "integrity": "sha512-YLsYoQMvL8l8WrGpN3Zj7O1wK5LEBN+cQtux7BcuHyxIXve724XG+zuJ1n3U1cUweRtTzQOA4IHbuQw3N34SZw==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/core": { + "version": "8.0.5", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-8.0.5.tgz", + "integrity": "sha512-2/oWkgTbBYoqioCWAE4XJobOrzwxTDa5/XjDP3tJ1BhDr/owcd9qnXBp4xc3/2G5X4bvXM04nrxbRJxFcXAxFQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/code-frame": "^8.0.0", + "@babel/generator": "^8.0.5", + "@babel/helper-compilation-targets": "^8.0.5", + "@babel/helpers": "^8.0.5", + "@babel/parser": "^8.0.5", + "@babel/template": "^8.0.0", + "@babel/traverse": "^8.0.5", + "@babel/types": "^8.0.5", + "@types/gensync": "^1.0.5", + "convert-source-map": "^2.0.0", + "empathic": "^2.0.1", + "gensync": "^1.0.0-beta.2", + "import-meta-resolve": "^4.2.0", + "json5": "^2.2.3", + "obug": "^2.1.1", + "verkit": "^0.3.2" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/babel" + } + }, + "node_modules/@babel/core/node_modules/convert-source-map": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", + "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@babel/generator": { + "version": "8.0.6", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-8.0.6.tgz", + "integrity": "sha512-/pcanjdRsonCexCuxgGD8fKnBLLzk2i9uM4kmcmGyyev2R5YDs+rafMYZ89RvaGY1mW8T7C5au0TyYaEeHBGng==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/parser": "^8.0.6", + "@babel/types": "^8.0.6", + "@jridgewell/gen-mapping": "0.4.0-beta.0", + "@jridgewell/trace-mapping": "^0.3.31", + "@types/jsesc": "^2.5.0", + "jsesc": "^3.0.2" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/generator/node_modules/@jridgewell/gen-mapping": { + "version": "0.4.0-beta.0", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.4.0-beta.0.tgz", + "integrity": "sha512-JdGNkbE4GlNPYQhM0L95fBQr7ctLZJ276QXQLTad4t1oSdnnCI3fDq9DW3BqYAWv8Wc3+HS+4Gsii1oPMCfz1w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0-beta.0", + "@jridgewell/trace-mapping": "^0.3.24" + } + }, + "node_modules/@babel/helper-compilation-targets": { + "version": "8.0.7", + "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-8.0.7.tgz", + "integrity": "sha512-ilAZCdOOlOVA7dQbaFNw2f/59p6GW33NxTopc3DvEE3efp3ZXTrGX/yZScaKw8/vd9bq31Ag0EcXiLw32bAgJg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/compat-data": "^8.0.5", + "@babel/helper-validator-option": "^8.0.0", + "browserslist": "^4.24.0", + "flru": "1.0.2", + "verkit": "^0.5.0" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/helper-compilation-targets/node_modules/verkit": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/verkit/-/verkit-0.5.0.tgz", + "integrity": "sha512-u4J1hr6Y+C/hit34GwL3eIcUf3wngT8TlYUjK7tk0jxydKLAq3sYvifjdTDA6U3KCKRcQCQDGf/srjxfZF3l7g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18.12.0" + }, + "funding": { + "url": "https://github.com/sponsors/sxzz" + } + }, + "node_modules/@babel/helper-globals": { + "version": "8.0.6", + "resolved": "https://registry.npmjs.org/@babel/helper-globals/-/helper-globals-8.0.6.tgz", + "integrity": "sha512-OdnA/N52LhzFpeUuVz5YGuSWjc7TPHiFmVYZbPlSgExocO2GwbxVpSni9MVs5wwW+wZ4kPuwBEBN9pIPDYz1mA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/helper-string-parser": { + "version": "8.0.6", + "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-8.0.6.tgz", + "integrity": "sha512-+6HYEBnsDRZHJtUTAEfMQd0zMDqdFHS7C+5vAsEZqBUr3vEMWOWlHJ0Jz1gS8jJIlCvLsuA6ivmeoqzH936YcA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/helper-validator-identifier": { + "version": "8.0.6", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-8.0.6.tgz", + "integrity": "sha512-HYx0YmuchRxRSpnjvcNffnPS8DCauKNmnumIuHG3jnQAVewRA7kfJGwF13ayEpSZs7s4R/oBH4eppuXcAQd5JQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/helper-validator-option": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-8.0.0.tgz", + "integrity": "sha512-U4Dybxh4WESWHt5XhBeExi4DrY0/DNK1aHpQbsrQXCUbFHuMweT0TpLEWKvaraV2Y6fS+ZXunsZ8zIuZIgvF2Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/helpers": { + "version": "8.0.7", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-8.0.7.tgz", + "integrity": "sha512-5dMj5kFwS2AY7Dt4PZp4px3c5ThMbbEa7tZfuHTncCGSFYq8uoA1ufDE1IS/lYpQedE+3+GQ3cPZUiplIu195w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/template": "^8.0.0", + "@babel/types": "^8.0.6" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/parser": { + "version": "8.0.7", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-8.0.7.tgz", + "integrity": "sha512-UkDwEbPO1px2JQSrdh4qIgaH1JgG7GO/pFeJkpxhDxIETkT/QvGpnigXIw75ViHYPtIgKITmVUJqLjiO+JuWjA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/types": "^8.0.6" + }, + "bin": { + "parser": "bin/babel-parser.js" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/runtime": { + "version": "7.29.10", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.10.tgz", + "integrity": "sha512-NpugTzbKgGrNe45Ui32cN8vCFFMxVtP/9rtfwv0nP5FRDAjvYBxJcLVpYA9mr1jxP6cnIT7x9LUzuDTbWYYNRA==", + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/@babel/template": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-8.0.0.tgz", + "integrity": "sha512-eAD0QW/AlbamBbw0FeGiwasbCVPq5ncW0HNVyLP3B9czqLyh4gvw+5JTSNt6le9+ziAU7mqDZsKTHf3jTb4chQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/code-frame": "^8.0.0", + "@babel/parser": "^8.0.0", + "@babel/types": "^8.0.0" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/traverse": { + "version": "8.0.7", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-8.0.7.tgz", + "integrity": "sha512-bVV0brinsTWgc6ptSIoQo3GbITtl0VZAqEUJbHHcyfDPEu+q/fEvlYEa+6H85HoK8IdksKBFtKJbJFZ6Kff+zg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/code-frame": "^8.0.6", + "@babel/generator": "^8.0.6", + "@babel/helper-globals": "^8.0.6", + "@babel/parser": "^8.0.7", + "@babel/template": "^8.0.0", + "@babel/types": "^8.0.6", + "obug": "^3.0.0" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@babel/traverse/node_modules/obug": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", + "integrity": "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT", + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/@babel/types": { + "version": "8.0.6", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-8.0.6.tgz", + "integrity": "sha512-c+xgSWboV2pdwXP67BUWDVRHi2BO5Q3KwxyFVs7taVTY+fsK47/L51ZXQoV9rozHJDNHvJ4v4WEsI/IlxV2l2A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-string-parser": "^8.0.6", + "@babel/helper-validator-identifier": "^8.0.6" + }, + "engines": { + "node": "^22.18.0 || >=24.11.0" + } + }, + "node_modules/@blazediff/core": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/@blazediff/core/-/core-1.10.0.tgz", + "integrity": "sha512-AOQff0zgR7cGsZL+4E7hVkmujoPUpm0J9xzWGWZj5wCjd3gmxESXAPfKyuzs93VdpQNFhHlBhfOjrcZ+XTERtQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@bufbuild/protobuf": { + "version": "2.16.0", + "resolved": "https://registry.npmjs.org/@bufbuild/protobuf/-/protobuf-2.16.0.tgz", + "integrity": "sha512-FWa0sPlqYGJgpTs6OxBRcL/AW4JT0OIO+W1ajerluoTVh8CV0B7XM1qsNCRgnkSCkRCgOICKHRQlY17jrsm0+Q==", + "dev": true, + "license": "(Apache-2.0 AND BSD-3-Clause)" + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz", + "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz", + "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz", + "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz", + "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz", + "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz", + "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz", + "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz", + "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz", + "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz", + "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz", + "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz", + "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz", + "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz", + "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz", + "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz", + "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz", + "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz", + "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz", + "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz", + "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz", + "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz", + "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz", + "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz", + "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz", + "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz", + "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@fortawesome/fontawesome-free": { + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/@fortawesome/fontawesome-free/-/fontawesome-free-7.3.1.tgz", + "integrity": "sha512-wmglKKPDIkgV3aWlZzWECCPoGIkYCulzBwxG9+w7rc5BGapZ6cPMpoPOT8k36J0Ni7PPX6c/rsoMWfS4d1MUMg==", + "license": "(CC-BY-4.0 AND OFL-1.1 AND MIT)", + "engines": { + "node": ">=6" + } + }, + "node_modules/@harperfast/extended-iterable": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@harperfast/extended-iterable/-/extended-iterable-1.0.5.tgz", + "integrity": "sha512-4ScXlphaKEa49RpHmqwATq9QI9aUTdWVYDlJkzxhsD7ShJP+pHCYLznMO9n/wREY8jPjRZ59wLJY6HM5yB5Gkw==", + "dev": true, + "license": "Apache-2.0", + "optional": true + }, + "node_modules/@inquirer/ansi": { + "version": "2.0.9", + "resolved": "https://registry.npmjs.org/@inquirer/ansi/-/ansi-2.0.9.tgz", + "integrity": "sha512-0Ywt3S8tqM6TWUcsRlBFQ1x+X3/o8kktX0ZiKAnByYb9GFDknnOhse1PVtfiD2BXPI8ar+BWx9PV9R0lQIcygw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + } + }, + "node_modules/@inquirer/checkbox": { + "version": "5.2.7", + "resolved": "https://registry.npmjs.org/@inquirer/checkbox/-/checkbox-5.2.7.tgz", + "integrity": "sha512-/+QmGE/9bvE/EobaZDnzYe+T+QWoJrnmxwBQNtoi2OVSUR9d1znYsNyEoUrOZvFSxDYttrhzRIpXpaLkN+iIsg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.9", + "@inquirer/core": "^12.0.5", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/checkbox/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/confirm": { + "version": "6.3.2", + "resolved": "https://registry.npmjs.org/@inquirer/confirm/-/confirm-6.3.2.tgz", + "integrity": "sha512-Xvr/0HggjddPtGppuqVmxhTw+Hr8PvsZ/k0HmOEaAqQEt80OITNkFWnsdNmyT0/eM4Ab+iJLx2R8rctlEyfSVg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.3", + "@inquirer/type": "4.1.1" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/core": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/@inquirer/core/-/core-12.0.5.tgz", + "integrity": "sha512-c0ZSnO9oVk/IlEGoZK3u8b3IZQeu9I6jUkXJSQnQEVRXsjKvtAq3AGqFL2LgfJhlzMZne08pZHWRWasIcI3Rew==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.9", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.2.0", + "cli-width": "^4.1.0", + "fast-wrap-ansi": "^0.2.0", + "mute-stream": "^3.0.0", + "signal-exit": "^4.1.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/core/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/editor": { + "version": "5.3.5", + "resolved": "https://registry.npmjs.org/@inquirer/editor/-/editor-5.3.5.tgz", + "integrity": "sha512-6lU3/pPx5z3lHWejFW061MV4qZooBQYv/+EeGhNOPiP/i+hrYsHfxxBnnDGsAfg8YdLUd69YckoeOjtr3QuAUg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.5", + "@inquirer/external-editor": "^3.0.6", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/editor/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/expand": { + "version": "5.1.7", + "resolved": "https://registry.npmjs.org/@inquirer/expand/-/expand-5.1.7.tgz", + "integrity": "sha512-eiJ31YFCaB6fLoJCqLIn8S6eqUyME6vwr8KcP5h8cxmA+bV9RRVPO+kywoxb7G63HduDXV6sN/SM7o2R+ahEcQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.5", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/expand/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/external-editor": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/@inquirer/external-editor/-/external-editor-3.0.6.tgz", + "integrity": "sha512-rWmw6WG8JZ7LMn/srXEylYGhL6EXXJeROE7/4x4GREh6NgzqJBDbIgpNGsYoLPfobeb4VZtEYzPGnT5EFNYhiQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "chardet": "^2.1.1", + "iconv-lite": "^0.7.2" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/figures": { + "version": "2.0.9", + "resolved": "https://registry.npmjs.org/@inquirer/figures/-/figures-2.0.9.tgz", + "integrity": "sha512-EAWgUTGQ/Umgga51dE3B2PUHbufuXarDfg86uVgoSgNHNNQnyFKcOrQLWVqYMghuSyHh8+2HUH0Js9cTC1WAdg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + } + }, + "node_modules/@inquirer/input": { + "version": "5.1.8", + "resolved": "https://registry.npmjs.org/@inquirer/input/-/input-5.1.8.tgz", + "integrity": "sha512-psOSTgkL8y/OtgJJYbqJ0oISr2QBE0U1Ro/qQdA/EKjeJiwiUTQgQmq8o4ESNzVOWthIeh3YLD7BCUR9dKnR5g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.5", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/input/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/number": { + "version": "4.2.5", + "resolved": "https://registry.npmjs.org/@inquirer/number/-/number-4.2.5.tgz", + "integrity": "sha512-lEy7LWsdF5qHx5QjHZqabUqM9P7i5uTMCt8GRzqxmwXXKbapKJDA+ucVflVVPAyj4snD5DTAD4L+sct85XOlOQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.5", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/number/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/password": { + "version": "5.2.4", + "resolved": "https://registry.npmjs.org/@inquirer/password/-/password-5.2.4.tgz", + "integrity": "sha512-T7ir3fflSSVCYEVqNyMBq+pIMsilQRtbZcD5gxn6Ewsin5TJWBxrGBdXveWtmeXVAeoInfhS+BhBUZaL4sFSDg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.9", + "@inquirer/core": "^12.0.5", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/password/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/prompts": { + "version": "8.7.2", + "resolved": "https://registry.npmjs.org/@inquirer/prompts/-/prompts-8.7.2.tgz", + "integrity": "sha512-QoRB4wFIjgH5iOhSjoIKMkTvSHDuV+O3OITlIqAYO0oK5x364GJILXiMBvlPiE+klg7Xx9tq5XVqQHcGUDYYPA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/checkbox": "^5.2.5", + "@inquirer/confirm": "^6.3.2", + "@inquirer/editor": "^5.3.3", + "@inquirer/expand": "^5.1.5", + "@inquirer/input": "^5.1.6", + "@inquirer/number": "^4.2.3", + "@inquirer/password": "^5.2.2", + "@inquirer/rawlist": "^5.3.5", + "@inquirer/search": "^4.3.3", + "@inquirer/select": "^5.2.5" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/rawlist": { + "version": "5.3.7", + "resolved": "https://registry.npmjs.org/@inquirer/rawlist/-/rawlist-5.3.7.tgz", + "integrity": "sha512-XveEe8v3BHj9ec00n+9i8FHjWhXjS6+jCTizYvXCqKUKtgkrq9xid8S5j3mYGW96qfxG3mwNoud3m3v8GDk9Pw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.5", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/rawlist/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/search": { + "version": "4.3.5", + "resolved": "https://registry.npmjs.org/@inquirer/search/-/search-4.3.5.tgz", + "integrity": "sha512-kIUecMugEN2YTvTmff+uYz0nAE0f9RFNDVZknfrh264vAQM9P8tnsXiUp7z8XbGuNIBEtFMJhvc9QquawpC46A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/core": "^12.0.5", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/search/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/select": { + "version": "5.2.7", + "resolved": "https://registry.npmjs.org/@inquirer/select/-/select-5.2.7.tgz", + "integrity": "sha512-pRDWO4dTqSAvFQdO8yclKMtXNDm9K9HmvWB5UZmT3clw2OlOBNXveHzhyFmO/0d0yJMqlPcNITus1Jtpm6t8VQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/ansi": "^2.0.9", + "@inquirer/core": "^12.0.5", + "@inquirer/figures": "^2.0.9", + "@inquirer/type": "4.2.0" + }, + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/select/node_modules/@inquirer/type": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.2.0.tgz", + "integrity": "sha512-2rnHX8RPo4glJYqnFc/v3nQeIhDcjyRRyaT7MGcfL/8xv/8K03SXYoA2tX06WVSKywWOE/ncbG3dtuzLjR5TCQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@inquirer/type": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/@inquirer/type/-/type-4.1.1.tgz", + "integrity": "sha512-yJoHYrMnxIsJZCY+0Vb66Dy3he3kL3e2wOBKhoSwWWAzZAY82emlxwgprCtp6yRixvNRNq9ztfRWQYPNr3Go7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=23.5.0 || ^22.13.0 || ^20.17.0" + }, + "peerDependencies": { + "@types/node": ">=18" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + } + } + }, + "node_modules/@jridgewell/gen-mapping": { + "version": "0.3.13", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz", + "integrity": "sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.0", + "@jridgewell/trace-mapping": "^0.3.24" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz", + "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, + "node_modules/@listr2/prompt-adapter-inquirer": { + "version": "4.2.7", + "resolved": "https://registry.npmjs.org/@listr2/prompt-adapter-inquirer/-/prompt-adapter-inquirer-4.2.7.tgz", + "integrity": "sha512-k180vxSv2KqmnDxoato6pCqEJsov4U5C0EVmldZkAdNeVBxY48A/LM6TCNPGIzDK/iZSfsZP0Mw+gbWc5hXLyA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@inquirer/type": "^4.0.7" + }, + "engines": { + "node": ">=22.13.0" + }, + "peerDependencies": { + "@inquirer/prompts": ">= 3 < 9", + "listr2": "11.1.0" + } + }, + "node_modules/@lmdb/lmdb-darwin-arm64": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/@lmdb/lmdb-darwin-arm64/-/lmdb-darwin-arm64-3.5.6.tgz", + "integrity": "sha512-mY5FG4TjPAkY4P0w+OhHaUka5mDh2TX2WKYIwuKzJ1zeW3VvRgxdam/lGJTquI+bthTx5CSHDW+BAQCnNAzkEA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@lmdb/lmdb-darwin-x64": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/@lmdb/lmdb-darwin-x64/-/lmdb-darwin-x64-3.5.6.tgz", + "integrity": "sha512-foa+pwitysO8k+xhs7psBFfTKnVgR69NlZRRTHaFVDqphh7AdGpLeyRzKw/ofatr/sN6TiHRRW6mmop0ZrrppQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@lmdb/lmdb-linux-arm": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/@lmdb/lmdb-linux-arm/-/lmdb-linux-arm-3.5.6.tgz", + "integrity": "sha512-QR4YRyR5h5Z8eGXrNQjiyo2NNDfqi3tCc9dQG5Is1blCt+qWw1ZoBWhlWAr5d+jshkifMIJjVHzHGKbkKzF8Tw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@lmdb/lmdb-linux-arm64": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/@lmdb/lmdb-linux-arm64/-/lmdb-linux-arm64-3.5.6.tgz", + "integrity": "sha512-HmiyFFdJa38s1heCMSooSPaBSFTHJ3C+ERPp28xAPlDX1YiALJVOgbry065nXd8Y7KISWjnw05zpG1RX8IfftA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@lmdb/lmdb-linux-x64": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/@lmdb/lmdb-linux-x64/-/lmdb-linux-x64-3.5.6.tgz", + "integrity": "sha512-ADzCuCF2cTNiX9kDScqcz1fjnAkxPpQNneV3KFTdV3wWtVlI2sTGzySoMTgDpinkMMFj1NTJlxA6XR8fwc4hlA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@lmdb/lmdb-win32-arm64": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/@lmdb/lmdb-win32-arm64/-/lmdb-win32-arm64-3.5.6.tgz", + "integrity": "sha512-J7A9aEQsQiv0TYtBGL7NDIPp2lOS8nnl+zm4sWZm1xlsTTaQ4PgD096Adzdrk27rw3UxCkDXdCUa4ax41oztBQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@lmdb/lmdb-win32-x64": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/@lmdb/lmdb-win32-x64/-/lmdb-win32-x64-3.5.6.tgz", + "integrity": "sha512-1g7G0knRX2iV/voDu54yxrGqw5Dk0w2oIYb7dgJq8IkOi+m7wbD8Q3QpPFjh0C01G58S88dqGn03len6UPCXsg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@modelcontextprotocol/core": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/@modelcontextprotocol/core/-/core-2.3.1.tgz", + "integrity": "sha512-laVmIhPGpWi7rG9q+0UigblRN0Hws/Yjlb6qLFtVqryXVLkc4h+yN8z731POD2LnAGQlFe7/tXhUm7Bi0YB91g==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "zod": "^4.2.0" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/@modelcontextprotocol/server": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/@modelcontextprotocol/server/-/server-2.3.1.tgz", + "integrity": "sha512-e59MfWuSssj6DoQ96JCTqdW1AXT5/XtnTl0WWd6MU1Ka434fFUGGosCLJbGNySDhHGvxQWoRWK137FXYFkYNWw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@modelcontextprotocol/core": "2.3.1", + "zod": "^4.2.0" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/@msgpackr-extract/msgpackr-extract-darwin-arm64": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@msgpackr-extract/msgpackr-extract-darwin-arm64/-/msgpackr-extract-darwin-arm64-3.0.4.tgz", + "integrity": "sha512-LCkGo6JDfaBhgST7UpPWgNgLINpcpabaHfyz5OBx75nUYxBsaEPxjnyNjWpeb/xBup/682QnBfRBy2/LvPutZQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@msgpackr-extract/msgpackr-extract-darwin-x64": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@msgpackr-extract/msgpackr-extract-darwin-x64/-/msgpackr-extract-darwin-x64-3.0.4.tgz", + "integrity": "sha512-zExlW9zUJKZH/tOtVMttwjKa4Xm/3KcNjnE3dPN92uCktwavMxpgCA3MoJK/DOnTWsQgo224OaST27/mPNAf+w==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@msgpackr-extract/msgpackr-extract-linux-arm": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@msgpackr-extract/msgpackr-extract-linux-arm/-/msgpackr-extract-linux-arm-3.0.4.tgz", + "integrity": "sha512-Tg3yX65f5GbtXLkrYEHE5oibZG9epyYWas7FogTTEJeDEF9JlXJzKgXaNhT3UXlTOeA+AfZpYZYZ0uPj7Cfquw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@msgpackr-extract/msgpackr-extract-linux-arm64": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@msgpackr-extract/msgpackr-extract-linux-arm64/-/msgpackr-extract-linux-arm64-3.0.4.tgz", + "integrity": "sha512-dgX0P/9wGPJeHFBG+ZmhgE6bmtMt7NP5CRBGyyktpopdk/mW4POnrpQsSLtKI1dwpc+pPLuXHDh6vvskyQE/sw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@msgpackr-extract/msgpackr-extract-linux-x64": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@msgpackr-extract/msgpackr-extract-linux-x64/-/msgpackr-extract-linux-x64-3.0.4.tgz", + "integrity": "sha512-8TNXMEjJc3QEy7R/x1INhgiU+XakDAFUzBhaz7+Rbrs8NH5UQeHQxxmzsSBJGyV6I1jW79undiQm8tOI+D+8FQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@msgpackr-extract/msgpackr-extract-win32-x64": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@msgpackr-extract/msgpackr-extract-win32-x64/-/msgpackr-extract-win32-x64-3.0.4.tgz", + "integrity": "sha512-CmCXPQrkbwExx3j946/PtHWHbYJiCRBRDl4BlkRQcJB/YOwQxJRTpoo7aTsortjgoJ1x7opzTSxn7C+ASSLVjQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/@napi-rs/nice": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice/-/nice-1.1.1.tgz", + "integrity": "sha512-xJIPs+bYuc9ASBl+cvGsKbGrJmS6fAKaSZCnT0lhahT5rhA2VVy9/EcIgd2JhtEuFOJNx7UHNn/qiTPTY4nrQw==", + "dev": true, + "license": "MIT", + "optional": true, + "engines": { + "node": ">= 10" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + }, + "optionalDependencies": { + "@napi-rs/nice-android-arm-eabi": "1.1.1", + "@napi-rs/nice-android-arm64": "1.1.1", + "@napi-rs/nice-darwin-arm64": "1.1.1", + "@napi-rs/nice-darwin-x64": "1.1.1", + "@napi-rs/nice-freebsd-x64": "1.1.1", + "@napi-rs/nice-linux-arm-gnueabihf": "1.1.1", + "@napi-rs/nice-linux-arm64-gnu": "1.1.1", + "@napi-rs/nice-linux-arm64-musl": "1.1.1", + "@napi-rs/nice-linux-ppc64-gnu": "1.1.1", + "@napi-rs/nice-linux-riscv64-gnu": "1.1.1", + "@napi-rs/nice-linux-s390x-gnu": "1.1.1", + "@napi-rs/nice-linux-x64-gnu": "1.1.1", + "@napi-rs/nice-linux-x64-musl": "1.1.1", + "@napi-rs/nice-openharmony-arm64": "1.1.1", + "@napi-rs/nice-win32-arm64-msvc": "1.1.1", + "@napi-rs/nice-win32-ia32-msvc": "1.1.1", + "@napi-rs/nice-win32-x64-msvc": "1.1.1" + } + }, + "node_modules/@napi-rs/nice-android-arm-eabi": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-android-arm-eabi/-/nice-android-arm-eabi-1.1.1.tgz", + "integrity": "sha512-kjirL3N6TnRPv5iuHw36wnucNqXAO46dzK9oPb0wj076R5Xm8PfUVA9nAFB5ZNMmfJQJVKACAPd/Z2KYMppthw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-android-arm64": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-android-arm64/-/nice-android-arm64-1.1.1.tgz", + "integrity": "sha512-blG0i7dXgbInN5urONoUCNf+DUEAavRffrO7fZSeoRMJc5qD+BJeNcpr54msPF6qfDD6kzs9AQJogZvT2KD5nw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-darwin-arm64": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-darwin-arm64/-/nice-darwin-arm64-1.1.1.tgz", + "integrity": "sha512-s/E7w45NaLqTGuOjC2p96pct4jRfo61xb9bU1unM/MJ/RFkKlJyJDx7OJI/O0ll/hrfpqKopuAFDV8yo0hfT7A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-darwin-x64": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-darwin-x64/-/nice-darwin-x64-1.1.1.tgz", + "integrity": "sha512-dGoEBnVpsdcC+oHHmW1LRK5eiyzLwdgNQq3BmZIav+9/5WTZwBYX7r5ZkQC07Nxd3KHOCkgbHSh4wPkH1N1LiQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-freebsd-x64": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-freebsd-x64/-/nice-freebsd-x64-1.1.1.tgz", + "integrity": "sha512-kHv4kEHAylMYmlNwcQcDtXjklYp4FCf0b05E+0h6nDHsZ+F0bDe04U/tXNOqrx5CmIAth4vwfkjjUmp4c4JktQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-arm-gnueabihf": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-arm-gnueabihf/-/nice-linux-arm-gnueabihf-1.1.1.tgz", + "integrity": "sha512-E1t7K0efyKXZDoZg1LzCOLxgolxV58HCkaEkEvIYQx12ht2pa8hoBo+4OB3qh7e+QiBlp1SRf+voWUZFxyhyqg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-arm64-gnu": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-arm64-gnu/-/nice-linux-arm64-gnu-1.1.1.tgz", + "integrity": "sha512-CIKLA12DTIZlmTaaKhQP88R3Xao+gyJxNWEn04wZwC2wmRapNnxCUZkVwggInMJvtVElA+D4ZzOU5sX4jV+SmQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-arm64-musl": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-arm64-musl/-/nice-linux-arm64-musl-1.1.1.tgz", + "integrity": "sha512-+2Rzdb3nTIYZ0YJF43qf2twhqOCkiSrHx2Pg6DJaCPYhhaxbLcdlV8hCRMHghQ+EtZQWGNcS2xF4KxBhSGeutg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-ppc64-gnu": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-ppc64-gnu/-/nice-linux-ppc64-gnu-1.1.1.tgz", + "integrity": "sha512-4FS8oc0GeHpwvv4tKciKkw3Y4jKsL7FRhaOeiPei0X9T4Jd619wHNe4xCLmN2EMgZoeGg+Q7GY7BsvwKpL22Tg==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-riscv64-gnu": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-riscv64-gnu/-/nice-linux-riscv64-gnu-1.1.1.tgz", + "integrity": "sha512-HU0nw9uD4FO/oGCCk409tCi5IzIZpH2agE6nN4fqpwVlCn5BOq0MS1dXGjXaG17JaAvrlpV5ZeyZwSon10XOXw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-s390x-gnu": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-s390x-gnu/-/nice-linux-s390x-gnu-1.1.1.tgz", + "integrity": "sha512-2YqKJWWl24EwrX0DzCQgPLKQBxYDdBxOHot1KWEq7aY2uYeX+Uvtv4I8xFVVygJDgf6/92h9N3Y43WPx8+PAgQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-x64-gnu": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-x64-gnu/-/nice-linux-x64-gnu-1.1.1.tgz", + "integrity": "sha512-/gaNz3R92t+dcrfCw/96pDopcmec7oCcAQ3l/M+Zxr82KT4DljD37CpgrnXV+pJC263JkW572pdbP3hP+KjcIg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-linux-x64-musl": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-linux-x64-musl/-/nice-linux-x64-musl-1.1.1.tgz", + "integrity": "sha512-xScCGnyj/oppsNPMnevsBe3pvNaoK7FGvMjT35riz9YdhB2WtTG47ZlbxtOLpjeO9SqqQ2J2igCmz6IJOD5JYw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-openharmony-arm64": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-openharmony-arm64/-/nice-openharmony-arm64-1.1.1.tgz", + "integrity": "sha512-6uJPRVwVCLDeoOaNyeiW0gp2kFIM4r7PL2MczdZQHkFi9gVlgm+Vn+V6nTWRcu856mJ2WjYJiumEajfSm7arPQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-win32-arm64-msvc": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-win32-arm64-msvc/-/nice-win32-arm64-msvc-1.1.1.tgz", + "integrity": "sha512-uoTb4eAvM5B2aj/z8j+Nv8OttPf2m+HVx3UjA5jcFxASvNhQriyCQF1OB1lHL43ZhW+VwZlgvjmP5qF3+59atA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-win32-ia32-msvc": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-win32-ia32-msvc/-/nice-win32-ia32-msvc-1.1.1.tgz", + "integrity": "sha512-CNQqlQT9MwuCsg1Vd/oKXiuH+TcsSPJmlAFc5frFyX/KkOh0UpBLEj7aoY656d5UKZQMQFP7vJNa1DNUNORvug==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@napi-rs/nice-win32-x64-msvc": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@napi-rs/nice-win32-x64-msvc/-/nice-win32-x64-msvc-1.1.1.tgz", + "integrity": "sha512-vB+4G/jBQCAh0jelMTY3+kgFy00Hlx2f2/1zjMoH821IbplbWZOkLiTYXQkygNTzQJTq5cvwBDgn2ppHD+bglQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@oxc-parser/binding-android-arm-eabi": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-android-arm-eabi/-/binding-android-arm-eabi-0.150.0.tgz", + "integrity": "sha512-oQef2Zu4Prz1KLKznz3HqZzU9uVoA5PMoDZuuLmqms7hKmKSAPzlaMnLllJq3t+rgKmfJJ2siPrpZfFrW06btw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-android-arm64": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-android-arm64/-/binding-android-arm64-0.150.0.tgz", + "integrity": "sha512-B6ofpoFiAUwIZ0MJ2IgHPvZK8FAtL4qzSZRwOkAKIfxEobE1mQC8nDdiFZj7pUaJiVUVCsfkMsBJKedzO8rZvA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-darwin-arm64": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-darwin-arm64/-/binding-darwin-arm64-0.150.0.tgz", + "integrity": "sha512-J+9IHKzx/bSz1JetOfD4zKXSK9sOm4/a7+0qomJODcTL6JsRXGeV/ZdkAPSIDydfFmWzYCraMlQEosSZEyBYkQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-darwin-x64": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-darwin-x64/-/binding-darwin-x64-0.150.0.tgz", + "integrity": "sha512-v6IPfcAcSYrBWXV5Tce1DmxDMXLhEYpIIWiRFPJopgCVscZdLaV4MRQOUxvL3usQlw+yrwYOjBFB9IwBx+jUiQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-freebsd-x64": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-freebsd-x64/-/binding-freebsd-x64-0.150.0.tgz", + "integrity": "sha512-AoR/4jD02HET0KO0yNT4nbTE+XJUxiO9jB1X/ycEjUE3WrIJ3IjV/Vf+gskhWLcqqeXfvNnkDtBR7epllAm88A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-arm-gnueabihf": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-0.150.0.tgz", + "integrity": "sha512-A/hycCFjLUrCmLoL5O/vBp40ohlaXO1Ta3v5hqYZxicYFs129wZmgZJggcW4mYGYbZebQLhup+N8JjeQl8qwyw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-arm-musleabihf": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm-musleabihf/-/binding-linux-arm-musleabihf-0.150.0.tgz", + "integrity": "sha512-pbqahg1Pkz7J4RKmXm30s/iQu99hv64ayXCu8P4p75HcEH5azOdR4eGqiB6lFGkFR3mMpzaYsSKLkS8oJbjmeQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-arm64-gnu": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-0.150.0.tgz", + "integrity": "sha512-HV11aRbBQGwqv8bEo+K/6qr88uB4fEe1mhXncMhlVCrj+WpBSraxrUzHaPVmeQRU6x6x8v/3DuCbbo93rpp+fw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-arm64-musl": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-arm64-musl/-/binding-linux-arm64-musl-0.150.0.tgz", + "integrity": "sha512-k6pVkJqALwtEuP4zukVhGRhdIy4+ofChUIUUsAHHyqDZlNsknmel3JjbggrJiWGaes6h/dIcWmEXsKW4SmK9oQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-ppc64-gnu": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-0.150.0.tgz", + "integrity": "sha512-tEFg39mw/rHO5n8GK2DR4ZMFfsPQZtnQIPbsIpjoQRomJc/2tRfsCSmCT6gNit+NZGoeJG5aH9ATDH5bf0WnoQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-riscv64-gnu": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-riscv64-gnu/-/binding-linux-riscv64-gnu-0.150.0.tgz", + "integrity": "sha512-0JO7IFkoek6HqV589Menx3hJySNXi6quRhO4tq2P7kpEHKEXoDATnoPVUpn5B7gDH2KLkdo751N0uIrGJFCTCw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-riscv64-musl": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-riscv64-musl/-/binding-linux-riscv64-musl-0.150.0.tgz", + "integrity": "sha512-7XPzREnyAS5wHU9aB+49Uaqgoo1gVCklHc3DRlc3fJy2tXD/eAJFN1QFz/crj+Ulup5P1+axNREF+/RGaB/IRA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-s390x-gnu": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-0.150.0.tgz", + "integrity": "sha512-7n7ZxcbRWDFaTdyj8M8p0O9OfzoMKm8O6syBAIgcSutbOALq0Bb9G52Me+XH0anMLZV+NgXc726gqgG2q39vcQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-x64-gnu": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-x64-gnu/-/binding-linux-x64-gnu-0.150.0.tgz", + "integrity": "sha512-Vx0GSA9ZCRTUiczoEQnIIyXkMvtEY8uc6IiwLQtMe5ci2sXPqjrry0Ek5fsPP1k2kCzkML3ow9UOOEgnEDi7Bw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-linux-x64-musl": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-linux-x64-musl/-/binding-linux-x64-musl-0.150.0.tgz", + "integrity": "sha512-ii4/9m3viDLssMnfXU7/Pni/3nYplApuGayceX4qsVGaqqQJCx3tHIH9M/HWIeSty5i8LjS21zPYT6lVIkwLxw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-openharmony-arm64": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-openharmony-arm64/-/binding-openharmony-arm64-0.150.0.tgz", + "integrity": "sha512-ZtoxX5rez36ZWkwtiEhjkCPnlTPoQ2I7lIL0IYZ1yjxMYohbvoOjBmUIZzrf9W/HouONq0omIfTeCWEY+R380w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-win32-arm64-msvc": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-0.150.0.tgz", + "integrity": "sha512-VqeRb5JX/bKYBrff7TUDvJyKY0914UzuCkuXIGxJS4FUmvMNfqkCbc7Sq90iMz9DlmAtlggwaBYQ9eg3h3ltiw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-win32-ia32-msvc": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-win32-ia32-msvc/-/binding-win32-ia32-msvc-0.150.0.tgz", + "integrity": "sha512-EPqJfeZ4Pgg2BJSsCV8GozxV0FDltRzpVtGVa1r2noJu1iu+opOw3gtBWXwIo9odCT/MdFfyHxdy0/CRqs3OqA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-parser/binding-win32-x64-msvc": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-parser/binding-win32-x64-msvc/-/binding-win32-x64-msvc-0.150.0.tgz", + "integrity": "sha512-n5YMzbqwPQqozbkrexi0lNZrUz5cnPHalYpFWe6Dau7AmKIWNo+y24IwzDuZEQtEdUHuKhXmMMih/MPjOI+CMw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@oxc-project/types": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.150.0.tgz", + "integrity": "sha512-rDS5/31E9HfPl/CIzGrn0DOlvBbXFseQ5URJ9sYMfstbKLD/c6Gm9vmRzRGDdAXyOIL4zmO37lc9RIwYqVruZw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/oxc-project" + } + }, + "node_modules/@parcel/watcher": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher/-/watcher-2.6.0.tgz", + "integrity": "sha512-7FNeNl8NCE7aINx7WXiKQrPYZWC/hvrTsmk6zmxbI7LTXE7hVek/n8AfVgpe2y82zl3w0HvCHN0bVKMBoJcC0w==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "dependencies": { + "detect-libc": "^2.0.3", + "is-glob": "^4.0.3", + "node-addon-api": "^7.0.0", + "picomatch": "^4.0.4" + }, + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "@parcel/watcher-android-arm64": "2.6.0", + "@parcel/watcher-darwin-arm64": "2.6.0", + "@parcel/watcher-darwin-x64": "2.6.0", + "@parcel/watcher-freebsd-x64": "2.6.0", + "@parcel/watcher-linux-arm-glibc": "2.6.0", + "@parcel/watcher-linux-arm-musl": "2.6.0", + "@parcel/watcher-linux-arm64-glibc": "2.6.0", + "@parcel/watcher-linux-arm64-musl": "2.6.0", + "@parcel/watcher-linux-x64-glibc": "2.6.0", + "@parcel/watcher-linux-x64-musl": "2.6.0", + "@parcel/watcher-win32-arm64": "2.6.0", + "@parcel/watcher-win32-x64": "2.6.0" + } + }, + "node_modules/@parcel/watcher-android-arm64": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-android-arm64/-/watcher-android-arm64-2.6.0.tgz", + "integrity": "sha512-trgpLSCKRC/huFjXX/Smh+0sWe4+YtKfktIToiMl59ghz7z+qkH6kMvNnUbLyRs9N11t8l4svSCs1+5B3rOAhA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-darwin-arm64": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-darwin-arm64/-/watcher-darwin-arm64-2.6.0.tgz", + "integrity": "sha512-Y3QV0gl7Q1zbfueunkWIERICbEojQFCgpyG7YqOGNFLsckXyI1xu9mAIUpKY9QBYzBtSkN8dBPwd3yiAO9ovMw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-darwin-x64": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-darwin-x64/-/watcher-darwin-x64-2.6.0.tgz", + "integrity": "sha512-Ohv6OpzhUfKYD7Beb8kDvG0jbIxORCYY1JRdZnaBtnjjkJxgD7ZVL0nw2sCYd0yTMKTvz3nnTnOF3cDifK+kvw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-freebsd-x64": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-freebsd-x64/-/watcher-freebsd-x64-2.6.0.tgz", + "integrity": "sha512-5HmXvDgs8VK+74jF9y9/2FE3/OnlcKmc56tjmSrEuZjpSZOGL+fvAu+HKJBdPs9uwoP2hE6TlSUpXZ/C5jUFmQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm-glibc": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm-glibc/-/watcher-linux-arm-glibc-2.6.0.tgz", + "integrity": "sha512-Ps/hui3A+vMbjdqlqAowK2ZL8+BO8dBjxeWXj6npTBs3jx4wWmbPpaLuqwrQrSqIVMCnpWo238bJ1U37GhQOYg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm-musl": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm-musl/-/watcher-linux-arm-musl-2.6.0.tgz", + "integrity": "sha512-9c6AUHgHoG+IY88MRIHupztQiQnrbqHYQjkM2btA+Bf/wQnQMuiD0Wfk1EVv3TlNT3x41uU71rn6E4xh/+zvkw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm64-glibc": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm64-glibc/-/watcher-linux-arm64-glibc-2.6.0.tgz", + "integrity": "sha512-yHRqS2owEXe6Hic9z6Mh1ECsCd+ODVOGvZDyciqRd21+v+o+DnXMOrw50DSpIG2sb8GPEaPPmfeCAWKPJdq46g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm64-musl": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm64-musl/-/watcher-linux-arm64-musl-2.6.0.tgz", + "integrity": "sha512-WhB2e/V7rqdHHWZusBSPuy5Ei8S6lSz6FE5TKKQz5h3a0O+C+mhY7vxU9b/stqvMb8beLnPY82ZrFTLKs+SrKA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-x64-glibc": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-x64-glibc/-/watcher-linux-x64-glibc-2.6.0.tgz", + "integrity": "sha512-ulGE6x6Oz6iAwg75T8YQSoguBWasniIbX+QWpaYPcCnDOpdWX3k+4xbEYPZVLxOuoJI+svJJPD3sEj8G7lrQ3A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-x64-musl": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-x64-musl/-/watcher-linux-x64-musl-2.6.0.tgz", + "integrity": "sha512-tkBYKt7YQrjIJWYDnto2YgO8MRkjlMTSNoRHzsXinBqbLdeOM3L32wPZJvIZxqaLMfSlS/4sUjH/6STVP/XDLw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-arm64": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-arm64/-/watcher-win32-arm64-2.6.0.tgz", + "integrity": "sha512-gIZAP23jaHjGWasY/TY6yL7NHFClf0Ga7FN+iINvk+KN94rhm94lYZhFsbYFNcA04/onvGD9kKmiJLJB2HbNwQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-x64": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-x64/-/watcher-win32-x64-2.6.0.tgz", + "integrity": "sha512-cA+/pXV2YkfxlIcXOQ5fSWqAzzPyD78/x5qbK/I0vUkrlYHA8TIz+MXjAbGouguKVSI4bOmkTSJ1/poVSsgt+A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@playwright/test": { + "version": "1.64.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.64.1.tgz", + "integrity": "sha512-IaYGml/nSOiZWROfPtuIL7neuu9qDOZ3TNvNljUIrhDMxiGTgPjzj/g9p9RxpS2tSepPDRY+UwAaEeiKtUvIAg==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.64.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/@polka/url": { + "version": "1.0.0-next.29", + "resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.29.tgz", + "integrity": "sha512-wwQAWhWSuHaag8c4q/KN/vCoeOJYshAIvMQwD4GpSb3OiZklFfvAgmj0VCBBImRpuF/aFgIRzllXlVX93Jevww==", + "dev": true, + "license": "MIT" + }, + "node_modules/@rolldown/binding-android-arm-eabi": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.8.tgz", + "integrity": "sha512-tN5aztYkKCte4i5SIrrz5yK/HMjEuCqCSCJa418jOV8tZ1cBY3YF2otxB1ktPxzsLA1BeTqwapK0bfjxNvHJVw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-android-arm64": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.2.8.tgz", + "integrity": "sha512-dIYTWl9XprMUiQFoc55KUyk/oS8SKYH3zFl0LTR7RT0Xj4hgSVyuJcroH8JUu8RcpF8fTB6E0aOwCkZoYPcDSQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.2.8.tgz", + "integrity": "sha512-PCSDQGXD2IyTEFrcgPyBM8jJuGmrbCMuoIOXdbEGVemruKACXoLQJrb+A45Z0L5t1RQkdfJprAYPkikbh7dzdA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-x64": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.2.8.tgz", + "integrity": "sha512-Uk7lRsGhPFHVX/sAUC6D5H9Ol30dFHd6iquokll2th3LpdJ3F5CzQB+7DHn0Ri2mG+U7k2zXiPHDrwZenXhwSA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.2.8.tgz", + "integrity": "sha512-DjszaTEVogPqA5bYzsEeqDCQxbcp2fexQwKcRspYji2yzR68fCf+e4fx6kBSRDwX5/brZaHw/hWS9+A/+/w9sQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.2.8.tgz", + "integrity": "sha512-zmwa7FTmdzB6aaEEuuls18H6Ap5JmJPSoPTuXixeJZV6tG40SyLkApQtz1g8ptZtiEKqj9OM0oNLPh1AgvE31Q==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.2.8.tgz", + "integrity": "sha512-KdYQDPHwJVnbFwdTGMgxsI9SqblBlz6STGM+w1We/d5B8OWWidYH0MwkU/uA1wM5fIpO2MkOVxXrNzzuZhw9ew==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.2.8.tgz", + "integrity": "sha512-jFJTifHnNPY+yzOoNZQfSIysrVyXzEQPhPnOUjmD1bcQGHH6s7c8cViKWar8YplQImE5N9JRqMCLrM2CdxOrZA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.2.8.tgz", + "integrity": "sha512-FhiOziBDWPBjbcmRzfLyIJnaP7AVMFXT7YCXPjXxj7wKU3vx24RjrCNN/zjvVa+N2vVoHJwCoUBvsrN/DG3zIA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.2.8.tgz", + "integrity": "sha512-WnHfADMzOV2Y55wlx1hzzQnar/wDt/VdvWSD99r18Mz9ylNieIGOkRx3UV21h7m/eJvjySYJkO26VvGNFkwsIQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.2.8.tgz", + "integrity": "sha512-H9tRr5ibfXFVLxbPOseVewewFpl28zcEdjRDt2FTUZU7odxP0gEv1ki4/kGmcGOh78oRwZuuQllGLZ9zTJp84g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.2.8.tgz", + "integrity": "sha512-UefiqfM3D6IVNlZ8tSGs9+Ejjud2T+oxO0IHADU45Y+lyEjD2dVFyZHbkfX0LUb5Zugo/oIv1eCO/KVYhgYJYA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.2.8.tgz", + "integrity": "sha512-637Ke4kWSy6rp9cxQ9gMOXlxPgIw/c1beASV4M//3+9I4uwBVOOl74G+e3zyU3u19U7RkRl/HuewixZ/Z6+Rjg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.2.8.tgz", + "integrity": "sha512-xWBkPOF1Q9k/Gv1nQXnVdLxKu74jXppuOM4Z3mnypVUJJJwLsMl7hNJGRAUJoG8A5MgOI1ACKM+wBFxSJzKy4A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.2.8.tgz", + "integrity": "sha512-uz2ZvfgXbxqNwijjjbxrnvALwpyODDcgc1T1N8N3rf/DXKQmaFwmB4LX4yyjggpwN2obdQLb2rgirX5ffCWYng==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/pluginutils": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.1.tgz", + "integrity": "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@schematics/angular": { + "version": "22.2.4", + "resolved": "https://registry.npmjs.org/@schematics/angular/-/angular-22.2.4.tgz", + "integrity": "sha512-3yXpeDsn780rk/Pm+2QjoGpvS03doyuB2i8jUt/tNtDatIIVoXUJak3kjPOK3TVorOJX/fTNMjMxDwQzvec2+w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@angular-devkit/core": "22.2.4", + "@angular-devkit/schematics": "22.2.4", + "jsonc-parser": "3.3.1", + "oxc-parser": "0.150.0", + "typescript": "6.0.3" + }, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", + "yarn": ">= 1.13.0" + } + }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "license": "MIT" + }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/d3": { + "version": "7.4.3", + "resolved": "https://registry.npmjs.org/@types/d3/-/d3-7.4.3.tgz", + "integrity": "sha512-lZXZ9ckh5R8uiFVt8ogUNf+pIrK4EsWrx2Np75WvF/eTpJ0FMHNhjXk8CKEx/+gpHbNQyJWehbFaTvqmHWB3ww==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-array": "*", + "@types/d3-axis": "*", + "@types/d3-brush": "*", + "@types/d3-chord": "*", + "@types/d3-color": "*", + "@types/d3-contour": "*", + "@types/d3-delaunay": "*", + "@types/d3-dispatch": "*", + "@types/d3-drag": "*", + "@types/d3-dsv": "*", + "@types/d3-ease": "*", + "@types/d3-fetch": "*", + "@types/d3-force": "*", + "@types/d3-format": "*", + "@types/d3-geo": "*", + "@types/d3-hierarchy": "*", + "@types/d3-interpolate": "*", + "@types/d3-path": "*", + "@types/d3-polygon": "*", + "@types/d3-quadtree": "*", + "@types/d3-random": "*", + "@types/d3-scale": "*", + "@types/d3-scale-chromatic": "*", + "@types/d3-selection": "*", + "@types/d3-shape": "*", + "@types/d3-time": "*", + "@types/d3-time-format": "*", + "@types/d3-timer": "*", + "@types/d3-transition": "*", + "@types/d3-zoom": "*" + } + }, + "node_modules/@types/d3-array": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.2.2.tgz", + "integrity": "sha512-hOLWVbm7uRza0BYXpIIW5pxfrKe0W+D5lrFiAEYR+pb6w3N2SwSMaJbXdUfSEv+dT4MfHBLtn5js0LAWaO6otw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-axis": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/@types/d3-axis/-/d3-axis-3.0.6.tgz", + "integrity": "sha512-pYeijfZuBd87T0hGn0FO1vQ/cgLk6E1ALJjfkC0oJ8cbwkZl3TpgS8bVBLZN+2jjGgg38epgxb2zmoGtSfvgMw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-selection": "*" + } + }, + "node_modules/@types/d3-brush": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/@types/d3-brush/-/d3-brush-3.0.6.tgz", + "integrity": "sha512-nH60IZNNxEcrh6L1ZSMNA28rj27ut/2ZmI3r96Zd+1jrZD++zD3LsMIjWlvg4AYrHn/Pqz4CF3veCxGjtbqt7A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-selection": "*" + } + }, + "node_modules/@types/d3-chord": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/@types/d3-chord/-/d3-chord-3.0.6.tgz", + "integrity": "sha512-LFYWWd8nwfwEmTZG9PfQxd17HbNPksHBiJHaKuY1XeqscXacsS2tyoo6OdRsjf+NQYeB6XrNL3a25E3gH69lcg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-color": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/@types/d3-color/-/d3-color-3.1.3.tgz", + "integrity": "sha512-iO90scth9WAbmgv7ogoq57O9YpKmFBbmoEoCHDB2xMBY0+/KVrqAaCDyCE16dUspeOvIxFFRI+0sEtqDqy2b4A==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-contour": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/@types/d3-contour/-/d3-contour-3.0.6.tgz", + "integrity": "sha512-BjzLgXGnCWjUSYGfH1cpdo41/hgdWETu4YxpezoztawmqsvCeep+8QGfiY6YbDvfgHz/DkjeIkkZVJavB4a3rg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-array": "*", + "@types/geojson": "*" + } + }, + "node_modules/@types/d3-delaunay": { + "version": "6.0.4", + "resolved": "https://registry.npmjs.org/@types/d3-delaunay/-/d3-delaunay-6.0.4.tgz", + "integrity": "sha512-ZMaSKu4THYCU6sV64Lhg6qjf1orxBthaC161plr5KuPHo3CNm8DTHiLw/5Eq2b6TsNP0W0iJrUOFscY6Q450Hw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-dispatch": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/@types/d3-dispatch/-/d3-dispatch-3.0.7.tgz", + "integrity": "sha512-5o9OIAdKkhN1QItV2oqaE5KMIiXAvDWBDPrD85e58Qlz1c1kI/J0NcqbEG88CoTwJrYe7ntUCVfeUl2UJKbWgA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-drag": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/@types/d3-drag/-/d3-drag-3.0.7.tgz", + "integrity": "sha512-HE3jVKlzU9AaMazNufooRJ5ZpWmLIoc90A37WU2JMmeq28w1FQqCZswHZ3xR+SuxYftzHq6WU6KJHvqxKzTxxQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-selection": "*" + } + }, + "node_modules/@types/d3-dsv": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/@types/d3-dsv/-/d3-dsv-3.0.7.tgz", + "integrity": "sha512-n6QBF9/+XASqcKK6waudgL0pf/S5XHPPI8APyMLLUHd8NqouBGLsU8MgtO7NINGtPBtk9Kko/W4ea0oAspwh9g==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-ease": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/d3-ease/-/d3-ease-3.0.2.tgz", + "integrity": "sha512-NcV1JjO5oDzoK26oMzbILE6HW7uVXOHLQvHshBUW4UMdZGfiY6v5BeQwh9a9tCzv+CeefZQHJt5SRgK154RtiA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-fetch": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/@types/d3-fetch/-/d3-fetch-3.0.7.tgz", + "integrity": "sha512-fTAfNmxSb9SOWNB9IoG5c8Hg6R+AzUHDRlsXsDZsNp6sxAEOP0tkP3gKkNSO/qmHPoBFTxNrjDprVHDQDvo5aA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-dsv": "*" + } + }, + "node_modules/@types/d3-force": { + "version": "3.0.10", + "resolved": "https://registry.npmjs.org/@types/d3-force/-/d3-force-3.0.10.tgz", + "integrity": "sha512-ZYeSaCF3p73RdOKcjj+swRlZfnYpK1EbaDiYICEEp5Q6sUiqFaFQ9qgoshp5CzIyyb/yD09kD9o2zEltCexlgw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-format": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/d3-format/-/d3-format-3.0.4.tgz", + "integrity": "sha512-fALi2aI6shfg7vM5KiR1wNJnZ7r6UuggVqtDA+xiEdPZQwy/trcQaHnwShLuLdta2rTymCNpxYTiMZX/e09F4g==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-geo": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@types/d3-geo/-/d3-geo-3.1.1.tgz", + "integrity": "sha512-65Emv9fQiQQqphLlRkuQ5ypPsOmWPhtBGCMv61JDPEPMvsx+gzhGf74yw1a78xFKPj6zw4AgQICJoQv0vK9M2w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/geojson": "*" + } + }, + "node_modules/@types/d3-hierarchy": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/@types/d3-hierarchy/-/d3-hierarchy-3.1.7.tgz", + "integrity": "sha512-tJFtNoYBtRtkNysX1Xq4sxtjK8YgoWUNpIiUee0/jHGRwqvzYxkq0hGVbbOGSz+JgFxxRu4K8nb3YpG3CMARtg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-interpolate": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/d3-interpolate/-/d3-interpolate-3.0.4.tgz", + "integrity": "sha512-mgLPETlrpVV1YRJIglr4Ez47g7Yxjl1lj7YKsiMCb27VJH9W8NVM6Bb9d8kkpG/uAQS5AmbA48q2IAolKKo1MA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-color": "*" + } + }, + "node_modules/@types/d3-path": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@types/d3-path/-/d3-path-3.1.1.tgz", + "integrity": "sha512-VMZBYyQvbGmWyWVea0EHs/BwLgxc+MKi1zLDCONksozI4YJMcTt8ZEuIR4Sb1MMTE8MMW49v0IwI5+b7RmfWlg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-polygon": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/d3-polygon/-/d3-polygon-3.0.2.tgz", + "integrity": "sha512-ZuWOtMaHCkN9xoeEMr1ubW2nGWsp4nIql+OPQRstu4ypeZ+zk3YKqQT0CXVe/PYqrKpZAi+J9mTs05TKwjXSRA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-quadtree": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/@types/d3-quadtree/-/d3-quadtree-3.0.6.tgz", + "integrity": "sha512-oUzyO1/Zm6rsxKRHA1vH0NEDG58HrT5icx/azi9MF1TWdtttWl0UIUsjEQBBh+SIkrpd21ZjEv7ptxWys1ncsg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-random": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/d3-random/-/d3-random-3.0.4.tgz", + "integrity": "sha512-UHYId5WTCx4L4YNel7NU00XUXXgvgpgZOvp10PuvsQENjMDXhh2RyFc0KBjO7B45ne4Ha1yVH7ii0vnzKkuzWA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-scale": { + "version": "4.0.9", + "resolved": "https://registry.npmjs.org/@types/d3-scale/-/d3-scale-4.0.9.tgz", + "integrity": "sha512-dLmtwB8zkAeO/juAMfnV+sItKjlsw2lKdZVVy6LRr0cBmegxSABiLEpGVmSJJ8O08i4+sGR6qQtb6WtuwJdvVw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-time": "*" + } + }, + "node_modules/@types/d3-scale-chromatic": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@types/d3-scale-chromatic/-/d3-scale-chromatic-3.1.0.tgz", + "integrity": "sha512-iWMJgwkK7yTRmWqRB5plb1kadXyQ5Sj8V/zYlFGMUBbIPKQScw+Dku9cAAMgJG+z5GYDoMjWGLVOvjghDEFnKQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-selection": { + "version": "3.0.12", + "resolved": "https://registry.npmjs.org/@types/d3-selection/-/d3-selection-3.0.12.tgz", + "integrity": "sha512-Qe/KWYhEiIIxGs7HrAAjMfShxKldx19SJtr5zu53f3afPsdZNz7HHtdTLXo/kqeiWNXVycI24kSnfzBYkTzpgw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-shape": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@types/d3-shape/-/d3-shape-3.2.0.tgz", + "integrity": "sha512-kVd74ta9eof3eJOvbNd1vGKS/XERRyQbT26Og63hIsvDO84cjD5gEOhsXf26w3FSoNlPVz84DOFcKv/oou+fMw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-path": "*" + } + }, + "node_modules/@types/d3-time": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/d3-time/-/d3-time-3.0.4.tgz", + "integrity": "sha512-yuzZug1nkAAaBlBBikKZTgzCeA+k1uy4ZFwWANOfKw5z5LRhV0gNA7gNkKm7HoK+HRN0wX3EkxGk0fpbWhmB7g==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-time-format": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/d3-time-format/-/d3-time-format-4.0.3.tgz", + "integrity": "sha512-5xg9rC+wWL8kdDj153qZcsJ0FWiFt0J5RB6LYUNZjwSnesfblqrI/bJ1wBdJ8OQfncgbJG5+2F+qfqnqyzYxyg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-timer": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/d3-timer/-/d3-timer-3.0.2.tgz", + "integrity": "sha512-Ps3T8E8dZDam6fUyNiMkekK3XUsaUEik+idO9/YjPtfj2qruF8tFBXS7XhtE4iIXBLxhmLjP3SXpLhVf21I9Lw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/d3-transition": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@types/d3-transition/-/d3-transition-3.0.9.tgz", + "integrity": "sha512-uZS5shfxzO3rGlu0cC3bjmMFKsXv+SmZZcgp0KD22ts4uGXp5EVYGzu/0YdwZeKmddhcAccYtREJKkPfXkZuCg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-selection": "*" + } + }, + "node_modules/@types/d3-zoom": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@types/d3-zoom/-/d3-zoom-3.0.9.tgz", + "integrity": "sha512-0sE1406XBYJGiqD3AusTl9ZqC//2mIXix51tbom25gDCA8ri4xnSZg28CaSE8Srl6FClqABUKfsn0qghgdepMA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/d3-interpolate": "*", + "@types/d3-selection": "*" + } + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/estree": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz", + "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/gensync": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@types/gensync/-/gensync-1.0.5.tgz", + "integrity": "sha512-MbsRCT7mTikHwKZ0X+LVUTLRrZZRLipTuXEO9qOYO+zmjMVk81axyClMROf6uoPD9MRVu46bx8zoR0Ad9q3NAg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/geojson": { + "version": "7946.0.16", + "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.16.tgz", + "integrity": "sha512-6C8nqWur3j98U6+lXDfTUWIfgvZU+EumvpHKcYjujKH7woYyLj2sUmff0tRhrqM7BohUw7Pz3ZB1jj2gW9Fvmg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/jsesc": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@types/jsesc/-/jsesc-2.5.1.tgz", + "integrity": "sha512-9VN+6yxLOPLOav+7PwjZbxiID2bVaeq0ED4qSQmdQTdjnXJSaCVKTR58t15oqH1H5t8Ng2ZX1SabJVoN9Q34bw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/pako": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/pako/-/pako-2.0.4.tgz", + "integrity": "sha512-VWDCbrLeVXJM9fihYodcLiIv0ku+AlOa/TQ1SvYOaBuyrSKgEcro95LJyIsJ4vSo6BXIxOKxiJAat04CmST9Fw==", + "license": "MIT" + }, + "node_modules/@types/raf": { + "version": "3.4.3", + "resolved": "https://registry.npmjs.org/@types/raf/-/raf-3.4.3.tgz", + "integrity": "sha512-c4YAvMedbPZ5tEyxzQdMoOhhJ4RD3rngZIdwC2/qDN3d7JpEhB6fiBRKVY1lg5B7Wk+uPBjn5f39j1/2MY1oOw==", + "license": "MIT", + "optional": true + }, + "node_modules/@types/trusted-types": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz", + "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==", + "license": "MIT", + "optional": true + }, + "node_modules/@vitejs/plugin-basic-ssl": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/@vitejs/plugin-basic-ssl/-/plugin-basic-ssl-2.3.0.tgz", + "integrity": "sha512-bdyo8rB3NnQbikdMpHaML9Z1OZPBu6fFOBo+OtxsBlvMJtysWskmBcnbIDhUqgC8tcxNv/a+BcV5U+2nQMm1OQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "peerDependencies": { + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + } + }, + "node_modules/@vitest/browser": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/browser/-/browser-5.0.5.tgz", + "integrity": "sha512-IDMoZX/iZ8rkilw7weaSgN8tJ7iZ9hUv0wYoKXyiyjWjWFwLSWAY707DcVTgxTYOudWC1E3HA9LYn7kVa9a7uQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@blazediff/core": "1.10.0", + "@vitest/mocker": "5.0.5", + "@vitest/ui": "5.0.5", + "@vitest/utils": "5.0.5", + "magic-string": "^1.4.3", + "pngjs": "^7.0.0", + "sirv": "^3.0.2", + "tinyrainbow": "^3.2.0", + "ws": "^8.22.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "vitest": "5.0.5" + } + }, + "node_modules/@vitest/browser-playwright": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/browser-playwright/-/browser-playwright-5.0.5.tgz", + "integrity": "sha512-lcdThFUUcMV9mGGV1SUiZXPxfjgW4CssA7o3STxzh8WK0tT2r526kc12tZIBRZfoLMwkhxjLmMXkWXONzdsDQw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/browser": "5.0.5", + "@vitest/mocker": "5.0.5", + "tinyrainbow": "^3.2.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "playwright": "*", + "vitest": "5.0.5" + }, + "peerDependenciesMeta": { + "playwright": { + "optional": false + } + } + }, + "node_modules/@vitest/browser/node_modules/magic-string": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.3.tgz", + "integrity": "sha512-z12OxmaPGE0F4xlpGdjuAUckipLpQlTAuAmyGhNoD7ODpYUzvDfvtxUbjM/jwznyP178oju/KDdyi220wNJ0RQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, + "node_modules/@vitest/mocker": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-5.0.5.tgz", + "integrity": "sha512-xLL3lkDegJZw+MGoGaCCsFayDZthGgpAtr3Vziyfc4p7xH89K7KRPcRBBh/kEYm0Aqkt5DtTmkYpBtRV/FrHZg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.31", + "@vitest/spy": "5.0.5", + "estree-walker": "^3.0.3", + "magic-string": "^1.4.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9 || ^3.0.0", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/mocker/node_modules/magic-string": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.3.tgz", + "integrity": "sha512-z12OxmaPGE0F4xlpGdjuAUckipLpQlTAuAmyGhNoD7ODpYUzvDfvtxUbjM/jwznyP178oju/KDdyi220wNJ0RQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, + "node_modules/@vitest/pretty-format": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-5.0.5.tgz", + "integrity": "sha512-24sSdIlMA9DVPXnYiB9siwU0bTd887Y9jX3+vabztCSsvHwo7ipvJ/a2L3Rljw8+2rK1f5fCj3h5U9I/Jsqgbw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyrainbow": "^3.2.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/spy": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-5.0.5.tgz", + "integrity": "sha512-lwJRExuH417257Q5wv9QYugN9IMJkoxcrKrYxGPQkBzl7wlNpTUxD/5yNX4ESKVUIShKPgkGchYPFMWOOk0wzw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/ui": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/ui/-/ui-5.0.5.tgz", + "integrity": "sha512-dCoGUOdgmCaSB2g+WQ/spPCBkytAWdNUkFSXY4z+eYp2d8JEOY8v0uDRaJTG9qf/kTlTVZ4CuYMYMz/f1HS/qQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "5.0.5", + "fflate": "^0.8.3", + "flatted": "^3.4.4", + "pathe": "^2.0.3", + "sirv": "^3.0.2", + "tinyrainbow": "^3.2.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "vitest": "5.0.5" + } + }, + "node_modules/@vitest/utils": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-5.0.5.tgz", + "integrity": "sha512-/rBu7gzLDQI/SC79Tn98b5DwOgaHU/Gt4I9OVeP3YofzNaYtbImRCOJQmT1S6vOJg9gEIlWC+r3QxKkjuSa1nQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "5.0.5", + "convert-source-map": "^2.0.0", + "tinyrainbow": "^3.2.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/utils/node_modules/convert-source-map": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", + "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", + "dev": true, + "license": "MIT" + }, + "node_modules/agent-base": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-9.0.0.tgz", + "integrity": "sha512-TQf59BsZnytt8GdJKLPfUZ54g/iaUL2OWDSFCCvMOhsHduDQxO8xC4PNeyIkVcA5KwL2phPSv0douC0fgWzmnA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 20" + } + }, + "node_modules/ajv": { + "version": "8.20.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.20.0.tgz", + "integrity": "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-deep-equal": "^3.1.3", + "fast-uri": "^3.0.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/ajv-formats": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz", + "integrity": "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "ajv": "^8.0.0" + }, + "peerDependencies": { + "ajv": "^8.0.0" + }, + "peerDependenciesMeta": { + "ajv": { + "optional": true + } + } + }, + "node_modules/ansi-escapes": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-7.3.0.tgz", + "integrity": "sha512-BvU8nYgGQBxcmMuEeUEmNTvrMVjJNSH7RgW24vXexN4Ven6qCvy4TntnvlnwnMLTVlcRQQdbRY8NKnaIoeWDNg==", + "dev": true, + "license": "MIT", + "dependencies": { + "environment": "^1.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ansi-regex": { + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.4.0.tgz", + "integrity": "sha512-KzTVk2tCWAHtYrvvvaP8bJKJq2pVinhLcGEQdtLIYPbmNGNyYe8QwNaTUYQp2J7/vIsUKt5QCqAfUkYyG9DkOw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-regex?sponsor=1" + } + }, + "node_modules/ansi-styles": { + "version": "6.2.3", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.3.tgz", + "integrity": "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, + "node_modules/base64-arraybuffer": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz", + "integrity": "sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6.0" + } + }, + "node_modules/baseline-browser-mapping": { + "version": "2.11.31", + "resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.11.31.tgz", + "integrity": "sha512-b0k7wXlWjpuL/Uxb/pYlpjY0ej5BjAmurtPUk3eRGuloI4yjK32m8TiKUQ81l1ptcLjKS/KH2+uqIolZti1x4Q==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "baseline-browser-mapping": "dist/cli.cjs" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/beasties": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/beasties/-/beasties-0.5.4.tgz", + "integrity": "sha512-8/lOOjT+PElYCnaUbmhgd3Uad5R1VTQ4Wg0k4rK8bX04m5WEqHNmnlLy3Q/lJ7dk25XA+72gayzc/skTfKKpcA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "css-select": "^7.0.0", + "css-what": "^8.0.0", + "dom-serializer": "^3.0.0", + "domhandler": "^6.0.0", + "htmlparser2": "^12.0.0", + "picocolors": "^1.1.1", + "postcss": "^8.4.49", + "postcss-media-query-parser": "^0.2.3", + "postcss-safe-parser": "^7.0.1" + }, + "engines": { + "node": ">=20.19.0" + } + }, + "node_modules/boolbase": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-2.0.0.tgz", + "integrity": "sha512-DkVaaQHymRhpYEYo9x1oo7Q7B0Y6KJUsjm3c9eTyFDby4MHLBTwZ6ZDWBel5zrYxj1WsZgC5oLpiz+93MluXeA==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + }, + "node_modules/browserslist": { + "version": "4.29.4", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.29.4.tgz", + "integrity": "sha512-2TcbbiwXEq+N0kD36tykpCfyyQigUXmuwNQbnPp4Xk4PX9JOqVIKjJlIDjpH7uY6tmRGoAd+apeT1p60T8wsdA==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/browserslist" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "baseline-browser-mapping": "^2.11.29", + "caniuse-lite": "^1.0.30001816", + "electron-to-chromium": "^1.5.459", + "node-releases": "^2.0.59", + "update-browserslist-db": "^1.3.4" + }, + "bin": { + "browserslist": "cli.js" + }, + "engines": { + "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" + } + }, + "node_modules/buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/caniuse-lite": { + "version": "1.0.30001819", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001819.tgz", + "integrity": "sha512-LFUqIIAwpzsIuevnKnsR9qnStBcgzPShTX61lBwN9uFkkf8o8zLiebEjbW8AYtZiF+1YKDWfdt/pWDtMuG8Xwg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/caniuse-lite" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "CC-BY-4.0" + }, + "node_modules/canvg": { + "version": "3.0.11", + "resolved": "https://registry.npmjs.org/canvg/-/canvg-3.0.11.tgz", + "integrity": "sha512-5ON+q7jCTgMp9cjpu4Jo6XbvfYwSB2Ow3kzHKfIyJfaCAOHLbdKPQqGKgfED/R5B+3TFFfe8pegYA+b423SRyA==", + "license": "MIT", + "optional": true, + "dependencies": { + "@babel/runtime": "^7.12.5", + "@types/raf": "^3.4.0", + "core-js": "^3.8.3", + "raf": "^3.4.1", + "regenerator-runtime": "^0.13.7", + "rgbcolor": "^1.0.1", + "stackblur-canvas": "^2.0.0", + "svg-pathdata": "^6.0.3" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/chai": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-6.3.0.tgz", + "integrity": "sha512-XWAtwJ6OHO+tj0EKCs0Y2UamnyOxseZWltU4x2U2wh8g4AigdjwvtUjvLP2tqkA/avxHEtzxNaqGq/YGNwckKg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/chalk": { + "version": "5.6.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz", + "integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/chardet": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/chardet/-/chardet-2.2.0.tgz", + "integrity": "sha512-rddelWYNPRrXq6PtNEN2S3f6t9ILzvqaN5pVgi4kqt9jHQaXIial9PznB5iSPVlQSLNaaH22ItWz3EJtQ10+OA==", + "dev": true, + "license": "MIT" + }, + "node_modules/chokidar": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-5.0.0.tgz", + "integrity": "sha512-TQMmc3w+5AxjpL8iIiwebF73dRDF4fBIieAqGn9RGCWaEVwQ6Fb2cGe31Yns0RRIzii5goJ1Y7xbMwo1TxMplw==", + "dev": true, + "license": "MIT", + "dependencies": { + "readdirp": "^5.0.0" + }, + "engines": { + "node": ">= 20.19.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/cli-cursor": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-5.0.0.tgz", + "integrity": "sha512-aCj4O5wKyszjMmDT4tZj93kxyydN/K5zPWSCe6/0AV/AA1pqe5ZBIw0a2ZfPQV7lL5/yb5HsUreJ6UFAF1tEQw==", + "dev": true, + "license": "MIT", + "dependencies": { + "restore-cursor": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cli-spinners": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-3.4.0.tgz", + "integrity": "sha512-bXfOC4QcT1tKXGorxL3wbJm6XJPDqEnij2gQ2m7ESQuE+/z9YFIWnl/5RpTiKWbMq3EVKR4fRLJGn6DVfu0mpw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18.20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cli-truncate": { + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/cli-truncate/-/cli-truncate-6.1.1.tgz", + "integrity": "sha512-06p9vyLahLa4zkGcgsGxU6iEkSOiuI4fhCH6Emhe2lPAcoUv73n72DnODsnHA+5wwXGnV0n9M9/qOQJSjYhFhw==", + "dev": true, + "license": "MIT", + "dependencies": { + "slice-ansi": "^9.0.0", + "string-width": "^8.2.0" + }, + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cli-width": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-4.1.0.tgz", + "integrity": "sha512-ouuZd4/dm2Sw5Gmqy6bGyNNNe1qt9RpmxveLSO7KcgsTnU7RXfsw+/bukWGo1abgBiMAic068rclZsO4IWmmxQ==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">= 12" + } + }, + "node_modules/cliui": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-9.0.1.tgz", + "integrity": "sha512-k7ndgKhwoQveBL+/1tqGJYNz097I7WOvwbmmU2AR5+magtbjPWQTS1C5vzGkBC8Ym8UWRzfKUzUUqFLypY4Q+w==", + "dev": true, + "license": "ISC", + "dependencies": { + "string-width": "^7.2.0", + "strip-ansi": "^7.1.0", + "wrap-ansi": "^9.0.0" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/cliui/node_modules/string-width": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-7.2.0.tgz", + "integrity": "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "emoji-regex": "^10.3.0", + "get-east-asian-width": "^1.0.0", + "strip-ansi": "^7.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cliui/node_modules/wrap-ansi": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-9.0.2.tgz", + "integrity": "sha512-42AtmgqjV+X1VpdOfyTGOYRi0/zsoLqtXQckTmqTeybT+BDIbM/Guxo7x3pE2vtpr1ok6xRqM9OpBe+Jyoqyww==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^6.2.1", + "string-width": "^7.0.0", + "strip-ansi": "^7.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, + "node_modules/colorjs.io": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/colorjs.io/-/colorjs.io-0.7.1.tgz", + "integrity": "sha512-LY7OHnJZxHwT5UlzNa9bbhHHDbzB6yE5+3MIPwJEQKRvSCt/T4G7epsj+9j2BExUIIfXFIJGsIXUKdfrK9Q5tA==", + "dev": true, + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/color" + } + }, + "node_modules/commander": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz", + "integrity": "sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==", + "license": "MIT", + "engines": { + "node": ">= 10" + } + }, + "node_modules/convert-source-map": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.9.0.tgz", + "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==", + "dev": true, + "license": "MIT" + }, + "node_modules/core-js": { + "version": "3.51.0", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.51.0.tgz", + "integrity": "sha512-7bDKx/5jm5MxzsXxc93g6Z2+4ZsuT/MddrfRmTDJNAkWsYWHU8EVNEsBiIEWnMAMCHMmRfUEjkuo3jeNBOIHRw==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "engines": { + "node": "*" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/core-js" + } + }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", + "license": "MIT" + }, + "node_modules/css-line-break": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-2.1.0.tgz", + "integrity": "sha512-FHcKFCZcAha3LwfVBhCQbW2nCNbkZXn7KVUJcsT5/P8YmfsVja0FMPJr0B903j/E69HUphKiV9iQArX8SDYA4w==", + "license": "MIT", + "dependencies": { + "utrie": "^1.0.2" + } + }, + "node_modules/css-select": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/css-select/-/css-select-7.0.0.tgz", + "integrity": "sha512-snmjEVXy+1LnwXdxhYvTMj1d9tOh4HxkA1YmoayVBeeyR2C14Pum7fcxJIm4SswYspVy866eYNwlH6xC3/VH5g==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "boolbase": "^2.0.0", + "css-what": "^8.0.0", + "domhandler": "^6.0.1", + "domutils": "^4.0.2", + "nth-check": "^3.0.1" + }, + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + }, + "node_modules/css-what": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/css-what/-/css-what-8.0.0.tgz", + "integrity": "sha512-DH0Bqq3DNp5tdOReuNyAA+Ev4Y2GS5FMbZpeTLP6C4CDi0h5nL0BmUPChXw3o/qbHLDWHl49sbNqQVY7bMSDdw==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + }, + "node_modules/d3": { + "version": "7.9.0", + "resolved": "https://registry.npmjs.org/d3/-/d3-7.9.0.tgz", + "integrity": "sha512-e1U46jVP+w7Iut8Jt8ri1YsPOvFpg46k+K8TpCb0P+zjCkjkPnV7WzfDJzMHy1LnA+wj5pLT1wjO901gLXeEhA==", + "license": "ISC", + "dependencies": { + "d3-array": "3", + "d3-axis": "3", + "d3-brush": "3", + "d3-chord": "3", + "d3-color": "3", + "d3-contour": "4", + "d3-delaunay": "6", + "d3-dispatch": "3", + "d3-drag": "3", + "d3-dsv": "3", + "d3-ease": "3", + "d3-fetch": "3", + "d3-force": "3", + "d3-format": "3", + "d3-geo": "3", + "d3-hierarchy": "3", + "d3-interpolate": "3", + "d3-path": "3", + "d3-polygon": "3", + "d3-quadtree": "3", + "d3-random": "3", + "d3-scale": "4", + "d3-scale-chromatic": "3", + "d3-selection": "3", + "d3-shape": "3", + "d3-time": "3", + "d3-time-format": "4", + "d3-timer": "3", + "d3-transition": "3", + "d3-zoom": "3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-array": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/d3-array/-/d3-array-3.2.4.tgz", + "integrity": "sha512-tdQAmyA18i4J7wprpYq8ClcxZy3SC31QMeByyCFyRt7BVHdREQZ5lpzoe5mFEYZUWe+oq8HBvk9JjpibyEV4Jg==", + "license": "ISC", + "dependencies": { + "internmap": "1 - 2" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-axis": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/d3-axis/-/d3-axis-3.0.0.tgz", + "integrity": "sha512-IH5tgjV4jE/GhHkRV0HiVYPDtvfjHQlQfJHs0usq7M30XcSBvOotpmH1IgkcXsO/5gEQZD43B//fc7SRT5S+xw==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-brush": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/d3-brush/-/d3-brush-3.0.0.tgz", + "integrity": "sha512-ALnjWlVYkXsVIGlOsuWH1+3udkYFI48Ljihfnh8FZPF2QS9o+PzGLBslO0PjzVoHLZ2KCVgAM8NVkXPJB2aNnQ==", + "license": "ISC", + "dependencies": { + "d3-dispatch": "1 - 3", + "d3-drag": "2 - 3", + "d3-interpolate": "1 - 3", + "d3-selection": "3", + "d3-transition": "3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-chord": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-chord/-/d3-chord-3.0.1.tgz", + "integrity": "sha512-VE5S6TNa+j8msksl7HwjxMHDM2yNK3XCkusIlpX5kwauBfXuyLAtNg9jCp/iHH61tgI4sb6R/EIMWCqEIdjT/g==", + "license": "ISC", + "dependencies": { + "d3-path": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-color": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/d3-color/-/d3-color-3.1.0.tgz", + "integrity": "sha512-zg/chbXyeBtMQ1LbD/WSoW2DpC3I0mpmPdW+ynRTj/x2DAWYrIY7qeZIHidozwV24m4iavr15lNwIwLxRmOxhA==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-contour": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/d3-contour/-/d3-contour-4.0.2.tgz", + "integrity": "sha512-4EzFTRIikzs47RGmdxbeUvLWtGedDUNkTcmzoeyg4sP/dvCexO47AaQL7VKy/gul85TOxw+IBgA8US2xwbToNA==", + "license": "ISC", + "dependencies": { + "d3-array": "^3.2.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-delaunay": { + "version": "6.0.4", + "resolved": "https://registry.npmjs.org/d3-delaunay/-/d3-delaunay-6.0.4.tgz", + "integrity": "sha512-mdjtIZ1XLAM8bm/hx3WwjfHt6Sggek7qH043O8KEjDXN40xi3vx/6pYSVTwLjEgiXQTbvaouWKynLBiUZ6SK6A==", + "license": "ISC", + "dependencies": { + "delaunator": "5" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-dispatch": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-dispatch/-/d3-dispatch-3.0.1.tgz", + "integrity": "sha512-rzUyPU/S7rwUflMyLc1ETDeBj0NRuHKKAcvukozwhshr6g6c5d8zh4c2gQjY2bZ0dXeGLWc1PF174P2tVvKhfg==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-drag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/d3-drag/-/d3-drag-3.0.0.tgz", + "integrity": "sha512-pWbUJLdETVA8lQNJecMxoXfH6x+mO2UQo8rSmZ+QqxcbyA3hfeprFgIT//HW2nlHChWeIIMwS2Fq+gEARkhTkg==", + "license": "ISC", + "dependencies": { + "d3-dispatch": "1 - 3", + "d3-selection": "3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-dsv": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-dsv/-/d3-dsv-3.0.1.tgz", + "integrity": "sha512-UG6OvdI5afDIFP9w4G0mNq50dSOsXHJaRE8arAS5o9ApWnIElp8GZw1Dun8vP8OyHOZ/QJUKUJwxiiCCnUwm+Q==", + "license": "ISC", + "dependencies": { + "commander": "7", + "iconv-lite": "0.6", + "rw": "1" + }, + "bin": { + "csv2json": "bin/dsv2json.js", + "csv2tsv": "bin/dsv2dsv.js", + "dsv2dsv": "bin/dsv2dsv.js", + "dsv2json": "bin/dsv2json.js", + "json2csv": "bin/json2dsv.js", + "json2dsv": "bin/json2dsv.js", + "json2tsv": "bin/json2dsv.js", + "tsv2csv": "bin/dsv2dsv.js", + "tsv2json": "bin/dsv2json.js" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-dsv/node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/d3-ease": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-ease/-/d3-ease-3.0.1.tgz", + "integrity": "sha512-wR/XK3D3XcLIZwpbvQwQ5fK+8Ykds1ip7A2Txe0yxncXSdq1L9skcG7blcedkOX+ZcgxGAmLX1FrRGbADwzi0w==", + "license": "BSD-3-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-fetch": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-fetch/-/d3-fetch-3.0.1.tgz", + "integrity": "sha512-kpkQIM20n3oLVBKGg6oHrUchHM3xODkTzjMoj7aWQFq5QEM+R6E4WkzT5+tojDY7yjez8KgCBRoj4aEr99Fdqw==", + "license": "ISC", + "dependencies": { + "d3-dsv": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-force": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/d3-force/-/d3-force-3.0.0.tgz", + "integrity": "sha512-zxV/SsA+U4yte8051P4ECydjD/S+qeYtnaIyAs9tgHCqfguma/aAQDjo85A9Z6EKhBirHRJHXIgJUlffT4wdLg==", + "license": "ISC", + "dependencies": { + "d3-dispatch": "1 - 3", + "d3-quadtree": "1 - 3", + "d3-timer": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-format": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/d3-format/-/d3-format-3.1.2.tgz", + "integrity": "sha512-AJDdYOdnyRDV5b6ArilzCPPwc1ejkHcoyFarqlPqT7zRYjhavcT3uSrqcMvsgh2CgoPbK3RCwyHaVyxYcP2Arg==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-geo": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/d3-geo/-/d3-geo-3.1.1.tgz", + "integrity": "sha512-637ln3gXKXOwhalDzinUgY83KzNWZRKbYubaG+fGVuc/dxO64RRljtCTnf5ecMyE1RIdtqpkVcq0IbtU2S8j2Q==", + "license": "ISC", + "dependencies": { + "d3-array": "2.5.0 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-hierarchy": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/d3-hierarchy/-/d3-hierarchy-3.1.2.tgz", + "integrity": "sha512-FX/9frcub54beBdugHjDCdikxThEqjnR93Qt7PvQTOHxyiNCAlvMrHhclk3cD5VeAaq9fxmfRp+CnWw9rEMBuA==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-interpolate": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-interpolate/-/d3-interpolate-3.0.1.tgz", + "integrity": "sha512-3bYs1rOD33uo8aqJfKP3JWPAibgw8Zm2+L9vBKEHJ2Rg+viTR7o5Mmv5mZcieN+FRYaAOWX5SJATX6k1PWz72g==", + "license": "ISC", + "dependencies": { + "d3-color": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-path": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/d3-path/-/d3-path-3.1.0.tgz", + "integrity": "sha512-p3KP5HCf/bvjBSSKuXid6Zqijx7wIfNW+J/maPs+iwR35at5JCbLUT0LzF1cnjbCHWhqzQTIN2Jpe8pRebIEFQ==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-polygon": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-polygon/-/d3-polygon-3.0.1.tgz", + "integrity": "sha512-3vbA7vXYwfe1SYhED++fPUQlWSYTTGmFmQiany/gdbiWgU/iEyQzyymwL9SkJjFFuCS4902BSzewVGsHHmHtXg==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-quadtree": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-quadtree/-/d3-quadtree-3.0.1.tgz", + "integrity": "sha512-04xDrxQTDTCFwP5H6hRhsRcb9xxv2RzkcsygFzmkSIOJy3PeRJP7sNk3VRIbKXcog561P9oU0/rVH6vDROAgUw==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-random": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-random/-/d3-random-3.0.1.tgz", + "integrity": "sha512-FXMe9GfxTxqd5D6jFsQ+DJ8BJS4E/fT5mqqdjovykEB2oFbTMDVdg1MGFxfQW+FBOGoB++k8swBrgwSHT1cUXQ==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-scale": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/d3-scale/-/d3-scale-4.0.2.tgz", + "integrity": "sha512-GZW464g1SH7ag3Y7hXjf8RoUuAFIqklOAq3MRl4OaWabTFJY9PN/E1YklhXLh+OQ3fM9yS2nOkCoS+WLZ6kvxQ==", + "license": "ISC", + "dependencies": { + "d3-array": "2.10.0 - 3", + "d3-format": "1 - 3", + "d3-interpolate": "1.2.0 - 3", + "d3-time": "2.1.1 - 3", + "d3-time-format": "2 - 4" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-scale-chromatic": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/d3-scale-chromatic/-/d3-scale-chromatic-3.1.0.tgz", + "integrity": "sha512-A3s5PWiZ9YCXFye1o246KoscMWqf8BsD9eRiJ3He7C9OBaxKhAd5TFCdEx/7VbKtxxTsu//1mMJFrEt572cEyQ==", + "license": "ISC", + "dependencies": { + "d3-color": "1 - 3", + "d3-interpolate": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-selection": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/d3-selection/-/d3-selection-3.0.0.tgz", + "integrity": "sha512-fmTRWbNMmsmWq6xJV8D19U/gw/bwrHfNXxrIN+HfZgnzqTHp9jOmKMhsTUjXOJnZOdZY9Q28y4yebKzqDKlxlQ==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-shape": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/d3-shape/-/d3-shape-3.2.0.tgz", + "integrity": "sha512-SaLBuwGm3MOViRq2ABk3eLoxwZELpH6zhl3FbAoJ7Vm1gofKx6El1Ib5z23NUEhF9AsGl7y+dzLe5Cw2AArGTA==", + "license": "ISC", + "dependencies": { + "d3-path": "^3.1.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-time": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/d3-time/-/d3-time-3.1.0.tgz", + "integrity": "sha512-VqKjzBLejbSMT4IgbmVgDjpkYrNWUYJnbCGo874u7MMKIWsILRX+OpX/gTk8MqjpT1A/c6HY2dCA77ZN0lkQ2Q==", + "license": "ISC", + "dependencies": { + "d3-array": "2 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-time-format": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/d3-time-format/-/d3-time-format-4.1.0.tgz", + "integrity": "sha512-dJxPBlzC7NugB2PDLwo9Q8JiTR3M3e4/XANkreKSUxF8vvXKqm1Yfq4Q5dl8budlunRVlUUaDUgFt7eA8D6NLg==", + "license": "ISC", + "dependencies": { + "d3-time": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-timer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-timer/-/d3-timer-3.0.1.tgz", + "integrity": "sha512-ndfJ/JxxMd3nw31uyKoY2naivF+r29V+Lc0svZxe1JvvIRmi8hUsrMvdOwgS1o6uBHmiz91geQ0ylPP0aj1VUA==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-transition": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-transition/-/d3-transition-3.0.1.tgz", + "integrity": "sha512-ApKvfjsSR6tg06xrL434C0WydLr7JewBB3V+/39RMHsaXTOG0zmt/OAXeng5M5LBm0ojmxJrpomQVZ1aPvBL4w==", + "license": "ISC", + "dependencies": { + "d3-color": "1 - 3", + "d3-dispatch": "1 - 3", + "d3-ease": "1 - 3", + "d3-interpolate": "1 - 3", + "d3-timer": "1 - 3" + }, + "engines": { + "node": ">=12" + }, + "peerDependencies": { + "d3-selection": "2 - 3" + } + }, + "node_modules/d3-zoom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/d3-zoom/-/d3-zoom-3.0.0.tgz", + "integrity": "sha512-b8AmV3kfQaqWAuacbPuNbL6vahnOJflOhexLzMMNLga62+/nh0JzvJ0aO/5a5MVgUFGS7Hu1P9P03o3fJkDCyw==", + "license": "ISC", + "dependencies": { + "d3-dispatch": "1 - 3", + "d3-drag": "2 - 3", + "d3-interpolate": "1 - 3", + "d3-selection": "2 - 3", + "d3-transition": "2 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "dev": true, + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/delaunator": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/delaunator/-/delaunator-5.1.0.tgz", + "integrity": "sha512-AGrQ4QSgssa1NGmWmLPqN5NY2KajF5MqxetNEO+o0n3ZwZZeTmt7bBnvzHWrmkZFxGgr4HdyFgelzgi06otLuQ==", + "license": "ISC", + "dependencies": { + "robust-predicates": "^3.0.2" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/dom-serializer": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-3.1.1.tgz", + "integrity": "sha512-4MEa38/QexBob6gFNwu+EGdWvhJ1OKuNwdYY3Y3NyeWDQfnGeDYQUDfIRzWu5B5gsv03so2Uxd28YC6zrsx3Lw==", + "dev": true, + "license": "MIT", + "dependencies": { + "domelementtype": "^3.0.0", + "domhandler": "^6.0.0", + "entities": "^8.0.0" + }, + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1" + } + }, + "node_modules/domelementtype": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-3.0.0.tgz", + "integrity": "sha512-umCQid3jKbDmVjx8jGaW7uUykm4DEUeyV21hPxNMo2nV955DhUThwqyOIDtreepP31hl84X7G5U9ZfsWvIB3Pg==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + ], + "license": "BSD-2-Clause", + "engines": { + "node": ">=20.19.0" + } + }, + "node_modules/domhandler": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-6.0.1.tgz", + "integrity": "sha512-gYzvtM72ZtxQO0T048kd6HWSbbGCNOUwcnfQ01cqIJ4X2IYKFFHZ5mKvrQETcFXxsRObZulDaKmy//R7TPtsBg==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "domelementtype": "^3.0.0" + }, + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/fb55/domhandler?sponsor=1" + } + }, + "node_modules/dompurify": { + "version": "3.4.17", + "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.4.17.tgz", + "integrity": "sha512-B/pSlYVHQ58kqISD/yDjuJ1yfzI34m1gF4lAod2bwSRhiN4K5Cl3H8Af7hqB2PmTL911tdEudnnXn5FnhRoz0w==", + "license": "(MPL-2.0 OR Apache-2.0)", + "optional": true, + "optionalDependencies": { + "@types/trusted-types": "^2.0.7" + } + }, + "node_modules/domutils": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-4.0.2.tgz", + "integrity": "sha512-qI4JLRKnSzqFqr7hAlS5xQDusBCjKSEG4t4+7aNrIQMHBcsC2TGEhuyABJdYkgSewL57PNLYEiibY2iPKhKpaA==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "dom-serializer": "^3.0.0", + "domelementtype": "^3.0.0", + "domhandler": "^6.0.0" + }, + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/fb55/domutils?sponsor=1" + } + }, + "node_modules/echarts": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/echarts/-/echarts-6.1.0.tgz", + "integrity": "sha512-q0yaFPggC9FUdsWH4blavRWFmxdrIodbkoKNAjJudAI6CA9gNPxHtV2RcZNEepZVlk4yvBYkOkbk6HIVpIyHZA==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "2.3.0", + "zrender": "6.1.0" + } + }, + "node_modules/echarts/node_modules/tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==", + "license": "0BSD" + }, + "node_modules/electron-to-chromium": { + "version": "1.5.463", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.463.tgz", + "integrity": "sha512-nVWNU6ltFjopvOdD44J4zoBkgO0M+WZiFKKFV7EjebOH9z0KNphk6bCEjplXtJofL21ARIuuwWOCsFkBVklm6w==", + "dev": true, + "license": "ISC" + }, + "node_modules/emoji-regex": { + "version": "10.6.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-10.6.0.tgz", + "integrity": "sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A==", + "dev": true, + "license": "MIT" + }, + "node_modules/empathic": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/empathic/-/empathic-2.1.0.tgz", + "integrity": "sha512-AnfC1ATldl49/cvZdLPDjBfrRNwbDO05aibiOtzQu3qtlbJtomNLhF30HEtn/7iBz50dlMECqATo3fG0LrdEgw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14" + } + }, + "node_modules/entities": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-8.1.0.tgz", + "integrity": "sha512-kxL7msIffSuh9aaFAMD7rxAIuTRMAHMeBtgHW2yUdWw732ZNh4MehkF2gdjvtdmikkaIP9bFDDJOPlsvm7avrA==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/environment": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/environment/-/environment-1.1.0.tgz", + "integrity": "sha512-xUtoPkMggbz0MPyPiIWr1Kp4aeWJjDZ6SMvURhimjdZgsRuDplF5/s9hcgGhyXMhs+6vpnuoiZ2kFiu3FMnS8Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/es-module-lexer": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.3.2.tgz", + "integrity": "sha512-poHGpORABojJJucnV9KbOavETW8lBVnphkW77ER5/BQ5Fz7oXSoCNek7IH3vR5nRjdsEz926ibFYX8KtLQmdyw==", + "dev": true, + "license": "MIT" + }, + "node_modules/esbuild": { + "version": "0.28.2", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", + "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.28.2", + "@esbuild/android-arm": "0.28.2", + "@esbuild/android-arm64": "0.28.2", + "@esbuild/android-x64": "0.28.2", + "@esbuild/darwin-arm64": "0.28.2", + "@esbuild/darwin-x64": "0.28.2", + "@esbuild/freebsd-arm64": "0.28.2", + "@esbuild/freebsd-x64": "0.28.2", + "@esbuild/linux-arm": "0.28.2", + "@esbuild/linux-arm64": "0.28.2", + "@esbuild/linux-ia32": "0.28.2", + "@esbuild/linux-loong64": "0.28.2", + "@esbuild/linux-mips64el": "0.28.2", + "@esbuild/linux-ppc64": "0.28.2", + "@esbuild/linux-riscv64": "0.28.2", + "@esbuild/linux-s390x": "0.28.2", + "@esbuild/linux-x64": "0.28.2", + "@esbuild/netbsd-arm64": "0.28.2", + "@esbuild/netbsd-x64": "0.28.2", + "@esbuild/openbsd-arm64": "0.28.2", + "@esbuild/openbsd-x64": "0.28.2", + "@esbuild/openharmony-arm64": "0.28.2", + "@esbuild/sunos-x64": "0.28.2", + "@esbuild/win32-arm64": "0.28.2", + "@esbuild/win32-ia32": "0.28.2", + "@esbuild/win32-x64": "0.28.2" + } + }, + "node_modules/escalade": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", + "integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, + "node_modules/expect-type": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz", + "integrity": "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/fast-png": { + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/fast-png/-/fast-png-6.4.0.tgz", + "integrity": "sha512-kAqZq1TlgBjZcLr5mcN6NP5Rv4V2f22z00c3g8vRrwkcqjerx7BEhPbOnWCPqaHUl2XWQBJQvOT/FQhdMT7X/Q==", + "license": "MIT", + "dependencies": { + "@types/pako": "^2.0.3", + "iobuffer": "^5.3.2", + "pako": "^2.1.0" + } + }, + "node_modules/fast-string-truncated-width": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/fast-string-truncated-width/-/fast-string-truncated-width-3.0.3.tgz", + "integrity": "sha512-0jjjIEL6+0jag3l2XWWizO64/aZVtpiGE3t0Zgqxv0DPuxiMjvB3M24fCyhZUO4KomJQPj3LTSUnDP3GpdwC0g==", + "dev": true, + "license": "MIT" + }, + "node_modules/fast-string-width": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/fast-string-width/-/fast-string-width-3.0.2.tgz", + "integrity": "sha512-gX8LrtNEI5hq8DVUfRQMbr5lpaS4nMIWV+7XEbXk2b8kiQIizgnlr12B4dA3ZEx3308ze0O4Q1R+cHts8kyUJg==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-string-truncated-width": "^3.0.2" + } + }, + "node_modules/fast-uri": { + "version": "3.1.9", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.9.tgz", + "integrity": "sha512-hS/0Wt3Ya043oKXs0sIxrMpKQITXhMLIrVAKKXi4gozGC9v7YRWVnXIZzjNPTO02M9BdxLCWqK1yRXgNziVyJQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/fastify" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/fastify" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/fast-wrap-ansi": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/fast-wrap-ansi/-/fast-wrap-ansi-0.2.2.tgz", + "integrity": "sha512-7F2Fl+TjRSenLqlU3UjSH0iyqopqoZIu7eZVpEirP2g1GtWa2G/ecEmBdgz31+Mxr+ELclgg6sokpSFIQiZ02Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "fast-string-width": "^3.0.2" + } + }, + "node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/fflate": { + "version": "0.8.3", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.3.tgz", + "integrity": "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA==", + "license": "MIT" + }, + "node_modules/flatted": { + "version": "3.4.4", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.4.4.tgz", + "integrity": "sha512-5+ybhBZANEJxaH3X5evAFatUxLfEHSr7n6kYJ+1Qd0mUqr4eu9gIf6GDbWHf8RJijHrjjO8G+la14SlL2SeS1Q==", + "dev": true, + "license": "ISC" + }, + "node_modules/flru": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/flru/-/flru-1.0.2.tgz", + "integrity": "sha512-kWyh8ADvHBFz6ua5xYOPnUroZTT/bwWfrCeL0Wj1dzG4/YOmOcfJ99W8dOVyyynJN35rZ9aCOtHChqQovV7yog==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/gensync": { + "version": "1.0.0-beta.2", + "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", + "integrity": "sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "dev": true, + "license": "ISC", + "engines": { + "node": "6.* || 8.* || >= 10.*" + } + }, + "node_modules/get-east-asian-width": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.7.0.tgz", + "integrity": "sha512-XjH1AECxf0giL2V1aU8vKyRR2ppRUb5c0EvT7zuJTokQ74bNo52zOtghqdWIqrhUD79fo3x0WfKZdOqxF6LG1Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/hosted-git-info": { + "version": "10.1.1", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-10.1.1.tgz", + "integrity": "sha512-DeOnSPAvOndYKfw075gt8yZzQ7S2hNztw34zBTfhIzLhmBTswIBg5/y+pqu/VD5cYWm5goAFTusDmUEmKZ0PEQ==", + "dev": true, + "license": "ISC", + "dependencies": { + "lru-cache": "^11.1.0" + }, + "engines": { + "node": "^22.22.2 || ^24.15.0 || >=26.0.0" + } + }, + "node_modules/html2canvas": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.4.1.tgz", + "integrity": "sha512-fPU6BHNpsyIhr8yyMpTLLxAbkaK8ArIBcmZIRiBLiDhjeqvXolaEmDGmELFuX9I4xDcaKKcJl+TKZLqruBbmWA==", + "license": "MIT", + "dependencies": { + "css-line-break": "^2.1.0", + "text-segmentation": "^1.0.3" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/htmlparser2": { + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-12.0.0.tgz", + "integrity": "sha512-Tz7u1i95/g2x2jz81+x0FBVhBhY5aRTvD3tXXdFaljuNdzDLJ8UGNRrTcj2cgQvAg3iW/h77Fz15nLW0L0CrZw==", + "dev": true, + "funding": [ + "https://github.com/fb55/htmlparser2?sponsor=1", + { + "type": "github", + "url": "https://github.com/sponsors/fb55" + } + ], + "license": "MIT", + "dependencies": { + "domelementtype": "^3.0.0", + "domhandler": "^6.0.0", + "domutils": "^4.0.2", + "entities": "^8.0.0" + }, + "engines": { + "node": ">=20.19.0" + } + }, + "node_modules/https-proxy-agent": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-9.1.0.tgz", + "integrity": "sha512-ag87y7cJJ9/3+GxFr8Oy4O5faDsGRGnBGsJj/YjOSsSx/5eadKLYTMPlzuR6obgoCDDm0abAAZitXXQkMOPSpA==", + "dev": true, + "license": "MIT", + "dependencies": { + "agent-base": "9.0.0", + "debug": "^4.3.4", + "proxy-agent-negotiate": "1.1.0" + }, + "engines": { + "node": ">= 20" + } + }, + "node_modules/iconv-lite": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.3.tgz", + "integrity": "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==", + "license": "MIT" + }, + "node_modules/immutable": { + "version": "5.1.9", + "resolved": "https://registry.npmjs.org/immutable/-/immutable-5.1.9.tgz", + "integrity": "sha512-m8nVez3rwrgmWxtLMt1ZYXB2Lv7OKYn/disyxAlSDYAlKSlFoPPfIAmAM/M5xqL4m4C/wAPw7S2/CNaUii1Hxg==", + "dev": true, + "license": "MIT" + }, + "node_modules/import-meta-resolve": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/import-meta-resolve/-/import-meta-resolve-4.2.0.tgz", + "integrity": "sha512-Iqv2fzaTQN28s/FwZAoFq0ZSs/7hMAHJVX+w8PZl3cY19Pxk6jFFalxQoIfW2826i/fDLXv8IiEZRIT0lDuWcg==", + "dev": true, + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/internmap": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/internmap/-/internmap-2.0.3.tgz", + "integrity": "sha512-5Hh7Y1wQbvY5ooGgPbDaL5iYLAPzMTUrjMulskHLH6wnv/A+1q5rgEaiuqEjB+oxGXIVZs1FF+R/KPN3ZSQYYg==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/iobuffer": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/iobuffer/-/iobuffer-5.4.0.tgz", + "integrity": "sha512-DRebOWuqDvxunfkNJAlc3IzWIPD5xVxwUNbHr7xKB8E6aLJxIPfNX3CoMJghcFjpv6RWQsrcJbghtEwSPoJqMA==", + "license": "MIT" + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-fullwidth-code-point": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-5.1.0.tgz", + "integrity": "sha512-5XHYaSyiqADb4RnZ1Bdad6cPp8Toise4TzEjcOYDHZkTCbKgiUl7WTUCpNWHuxmDt91wnsZBc9xinNzopv3JMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "get-east-asian-width": "^1.3.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-interactive": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-2.0.0.tgz", + "integrity": "sha512-qP1vozQRI+BMOPcjFzrjXuQvdak2pHNUMZoeG2eRbiSqyvbEf/wQtEOTOX1guk6E3t36RkaqiSt8A/6YElNxLQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/is-unicode-supported": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-2.1.0.tgz", + "integrity": "sha512-mE00Gnza5EEB3Ds0HfMyllZzbBrmLOX3vfWoj9A9PEnTfratQ/BcaJOuMhnkhjXvb2+FkY3VuHqtAGpTPmglFQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", + "license": "MIT" + }, + "node_modules/js-tokens": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-10.0.0.tgz", + "integrity": "sha512-lM/UBzQmfJRo9ABXbPWemivdCW8V2G8FHaHdypQaIy523snUjog0W71ayWXTjiR+ixeMyVHN2XcpnTd/liPg/Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/jsesc": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz", + "integrity": "sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA==", + "dev": true, + "license": "MIT", + "bin": { + "jsesc": "bin/jsesc" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "dev": true, + "license": "MIT" + }, + "node_modules/json5": { + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", + "dev": true, + "license": "MIT", + "bin": { + "json5": "lib/cli.js" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/jsonc-parser": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.3.1.tgz", + "integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/jspdf": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/jspdf/-/jspdf-4.2.1.tgz", + "integrity": "sha512-YyAXyvnmjTbR4bHQRLzex3CuINCDlQnBqoSYyjJwTP2x9jDLuKDzy7aKUl0hgx3uhcl7xzg32agn5vlie6HIlQ==", + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.28.6", + "fast-png": "^6.2.0", + "fflate": "^0.8.1" + }, + "optionalDependencies": { + "canvg": "^3.0.11", + "core-js": "^3.6.0", + "dompurify": "^3.3.1", + "html2canvas": "^1.0.0-rc.5" + } + }, + "node_modules/jspdf-autotable": { + "version": "5.0.9", + "resolved": "https://registry.npmjs.org/jspdf-autotable/-/jspdf-autotable-5.0.9.tgz", + "integrity": "sha512-wIOG2ggjG6Y7qvqjgFACZZeu+oHxoxrhwCn1lTMxjPhojLVVvu2U2yS2srVCYl0CXe6pqiG/jQMIIS0CFn1ssw==", + "license": "MIT", + "peerDependencies": { + "jspdf": "^2 || ^3 || ^4" + } + }, + "node_modules/jszip": { + "version": "3.10.2", + "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.2.tgz", + "integrity": "sha512-3l+rb15IOWtUhU0H5MFqES/T6Kh7abYwjosBey/vD6hDt8zoEffkSC5Ws5SGtgVw3gBx2NEbhTeSW1+kWkpyTQ==", + "license": "(MIT OR GPL-3.0-or-later)", + "dependencies": { + "lie": "~3.3.0", + "pako": "~1.0.2", + "readable-stream": "~2.3.6", + "setimmediate": "^1.0.5" + } + }, + "node_modules/jszip/node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==", + "license": "(MIT AND Zlib)" + }, + "node_modules/lie": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz", + "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==", + "license": "MIT", + "dependencies": { + "immediate": "~3.0.5" + } + }, + "node_modules/lightningcss": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.33.0.tgz", + "integrity": "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==", + "dev": true, + "license": "MPL-2.0", + "dependencies": { + "detect-libc": "^2.0.3" + }, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "lightningcss-android-arm64": "1.33.0", + "lightningcss-darwin-arm64": "1.33.0", + "lightningcss-darwin-x64": "1.33.0", + "lightningcss-freebsd-x64": "1.33.0", + "lightningcss-linux-arm-gnueabihf": "1.33.0", + "lightningcss-linux-arm64-gnu": "1.33.0", + "lightningcss-linux-arm64-musl": "1.33.0", + "lightningcss-linux-x64-gnu": "1.33.0", + "lightningcss-linux-x64-musl": "1.33.0", + "lightningcss-win32-arm64-msvc": "1.33.0", + "lightningcss-win32-x64-msvc": "1.33.0" + } + }, + "node_modules/lightningcss-android-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.33.0.tgz", + "integrity": "sha512-gEpRTalKdosp4Bb8qWtc2iOgE5SeIHlpS1up9bFq2wAyYhl1UdTObYiHe98zEM9SQvSoqQZ1IQD0JNpg3Ml5pg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.33.0.tgz", + "integrity": "sha512-Sciaz8eenNTKn9b3t7+xr0ipTp9YxKQY4npwQ3mrRuL0BAVHBLyZxofhaKBAVtzmtRZ/zTyo0/to4B1uWG/Djg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.33.0.tgz", + "integrity": "sha512-Z5UPAxzrjlWNNyGy6i65cJzzvgJ5D3T6wMvs+gWpY9d7qRhANrxqAp6LhxIgZhWEw18RfJTGcRxjuLIBr+m8XQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-freebsd-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.33.0.tgz", + "integrity": "sha512-QQM/Ti/hQajJwCY+RiWuCZ9sdtI/XQk7nDK5vC8kkdwixezOlDgvDx7+RT+QjK6FcFT4MpsuoBnHIo/O3StRRg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm-gnueabihf": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.33.0.tgz", + "integrity": "sha512-N7FVBe6iS24MlM6R/4RBTxGhQheZGs7tiQ9U32UtF75NzP5Q7xWPRqLBCKxlRQRk3rY1jCIPLzx7WzOhuUIRLQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.33.0.tgz", + "integrity": "sha512-j2v/itmy4HlNxlc6voKXYgBqNi0Ng2LShg4z7GufpEgs05P+2suBVyi9I6YHq5uoVFx9ETin3eCEhLVyXGQnKg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.33.0.tgz", + "integrity": "sha512-yiO5ROMuYQgXbC60yjZU5CYSFZGKXL0HFATXt9mHJn1+zW55oCtMI9NfcVhYLMFDL7gV7oBPon/EmMMGg2OvtQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.33.0.tgz", + "integrity": "sha512-ar+Ju7LmcN0Jo4FpL4hpFybwNG9/3A/Br5KW2n2jyODg3MEZXaDYADdemoNS+BDNfMgKvylJLj4S5tyRActuAg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.33.0.tgz", + "integrity": "sha512-RYiYbkokw0trfKqqzfF55lginwEPrD3OJDfTuJzFs1MK6iFnDenaz1fqLLtX4ITG3OktJQXOeTaw1awrBAlZPw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-arm64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.33.0.tgz", + "integrity": "sha512-1K+MPfLSFVpphzpdbfkhlWk6wBrTObBzS2T6db10PNOZgR9GoVsAWzwNyuhUYYbTp23j+4RrncfujZ4uAzXvwA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-x64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.33.0.tgz", + "integrity": "sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/listr2": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/listr2/-/listr2-11.1.0.tgz", + "integrity": "sha512-xsuczF6mWkEo/E80QHaEokQzKmTy7m80KcLRnkUt9wrcePres2w7dbQi5zOsWtDUg8Dac4gPFyu4EJ/IvKwTOg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cli-truncate": "^6.1.1", + "log-update": "^8.0.0", + "wrap-ansi": "^10.0.1" + }, + "engines": { + "node": ">=22.13.0" + } + }, + "node_modules/lmdb": { + "version": "3.5.6", + "resolved": "https://registry.npmjs.org/lmdb/-/lmdb-3.5.6.tgz", + "integrity": "sha512-j3uE8ReKNyUWDjhfEFSJqE/1DLtfTR5Z8yFzVHvBjAk37wNg7HdScjcv8ttPHRvrdgPQMPWxFFI0SsdBzI5lBw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@harperfast/extended-iterable": "^1.0.3", + "msgpackr": "^1.11.2", + "node-addon-api": "^6.1.0", + "node-gyp-build-optional-packages": "5.2.2", + "ordered-binary": "^1.5.3", + "weak-lru-cache": "^1.2.2" + }, + "bin": { + "download-lmdb-prebuilds": "bin/download-prebuilds.js" + }, + "optionalDependencies": { + "@lmdb/lmdb-darwin-arm64": "3.5.6", + "@lmdb/lmdb-darwin-x64": "3.5.6", + "@lmdb/lmdb-linux-arm": "3.5.6", + "@lmdb/lmdb-linux-arm64": "3.5.6", + "@lmdb/lmdb-linux-x64": "3.5.6", + "@lmdb/lmdb-win32-arm64": "3.5.6", + "@lmdb/lmdb-win32-x64": "3.5.6" + } + }, + "node_modules/lmdb/node_modules/node-addon-api": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-6.1.0.tgz", + "integrity": "sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==", + "dev": true, + "license": "MIT", + "optional": true + }, + "node_modules/log-symbols": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-7.0.1.tgz", + "integrity": "sha512-ja1E3yCr9i/0hmBVaM0bfwDjnGy8I/s6PP4DFp+yP+a+mrHO4Rm7DtmnqROTUkHIkqffC84YY7AeqX6oFk0WFg==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-unicode-supported": "^2.0.0", + "yoctocolors": "^2.1.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/log-update/-/log-update-8.0.0.tgz", + "integrity": "sha512-lddSgOt3bPASrylL54ZSpy8nBHns+vBVSoILlVOx+dei300pnLRN958rj/EdlVLKuWlSESU3qdnDZdAI7FXYGg==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-escapes": "^7.3.0", + "cli-cursor": "^5.0.0", + "slice-ansi": "^9.0.0", + "string-width": "^8.2.0", + "strip-ansi": "^7.2.0", + "wrap-ansi": "^10.0.0" + }, + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/lru-cache": { + "version": "11.5.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.5.3.tgz", + "integrity": "sha512-U4N8FgzmWxc8k1VH8Kr6lQg18U7Fjvby6wXHVRX/ZZ7IwWbRMgrRbP0Wrb5q5NVinryp4SQampHKdvtecItxUg==", + "dev": true, + "license": "BlueOak-1.0.0", + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/magic-string": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.1.tgz", + "integrity": "sha512-8lyCu36ErXR0J9uaGKlKQoiLZKmtI63YGLE8G2o9jyRPdr4X47LusSOwgOJOzcVtp81fTAAjxR7BwKz682Jhow==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, + "node_modules/mimic-function": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/mimic-function/-/mimic-function-5.0.1.tgz", + "integrity": "sha512-VP79XUPxV2CigYP3jWwAUFSku2aKqBH7uTAapFWCBqutsbmDo96KY5o8uh6U+/YSIn5OxJnXp73beVkpqMIGhA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/mrmime": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/mrmime/-/mrmime-2.0.1.tgz", + "integrity": "sha512-Y3wQdFg2Va6etvQ5I82yUhGdsKrcYox6p7FfL1LbK2J4V01F9TGlepTIhnK24t7koZibmg82KGglhA1XK5IsLQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true, + "license": "MIT" + }, + "node_modules/msgpackr": { + "version": "1.12.1", + "resolved": "https://registry.npmjs.org/msgpackr/-/msgpackr-1.12.1.tgz", + "integrity": "sha512-4EUH9tQHnMmEgzW/MdAP0KIfa1T9AF+htl0ffe2n5vb2EKn9y2co8ccpgWko6S52Jy1PQZKwRnx5/KkYjtd9MQ==", + "dev": true, + "license": "MIT", + "optional": true, + "optionalDependencies": { + "msgpackr-extract": "^3.0.2" + } + }, + "node_modules/msgpackr-extract": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/msgpackr-extract/-/msgpackr-extract-3.0.4.tgz", + "integrity": "sha512-4kmO/MdyUIkLIvTPr8VHLil4AtoKIoniWPIEk5+CDy0xnWC84azhSFmuJ7PxZdsYtiP5kEeQsORAVIeMgxT+Hw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "dependencies": { + "node-gyp-build-optional-packages": "5.2.2" + }, + "bin": { + "download-msgpackr-prebuilds": "bin/download-prebuilds.js" + }, + "optionalDependencies": { + "@msgpackr-extract/msgpackr-extract-darwin-arm64": "3.0.4", + "@msgpackr-extract/msgpackr-extract-darwin-x64": "3.0.4", + "@msgpackr-extract/msgpackr-extract-linux-arm": "3.0.4", + "@msgpackr-extract/msgpackr-extract-linux-arm64": "3.0.4", + "@msgpackr-extract/msgpackr-extract-linux-x64": "3.0.4", + "@msgpackr-extract/msgpackr-extract-win32-x64": "3.0.4" + } + }, + "node_modules/mute-stream": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-3.0.0.tgz", + "integrity": "sha512-dkEJPVvun4FryqBmZ5KhDo0K9iDXAwn08tMLDinNdRBNPcYEDiWYysLcc6k3mjTMlbP9KyylvRpd4wFtwrT9rw==", + "dev": true, + "license": "ISC", + "engines": { + "node": "^20.17.0 || >=22.9.0" + } + }, + "node_modules/nanoid": { + "version": "3.3.20", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.20.tgz", + "integrity": "sha512-uKdg2G3GNCKQn9byYOpxbGqrT2fGO5KRt5J/8b3pok8rT6qxGWF6hxMyJiEYtAf+FVyYuD9hRaDqX5uPFYJ4ZQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/ngx-echarts": { + "version": "22.0.0", + "resolved": "https://registry.npmjs.org/ngx-echarts/-/ngx-echarts-22.0.0.tgz", + "integrity": "sha512-evh5+CgEg3f/jmDLKwzUdih8unuhtwrpPzLTtoFwF/jwr6BM5QEb46QKdqXrwBTmSDZCL//iK9jlQJGIzM9LkQ==", + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "peerDependencies": { + "@angular/core": ">=22.0.0", + "echarts": ">=5.0.0" + } + }, + "node_modules/node-addon-api": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.1.1.tgz", + "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/node-gyp-build-optional-packages": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/node-gyp-build-optional-packages/-/node-gyp-build-optional-packages-5.2.2.tgz", + "integrity": "sha512-s+w+rBWnpTMwSFbaE0UXsRlg7hU4FjekKU4eyAih5T8nJuNZT1nNsskXpxmeqSK9UzkBl6UgRlnKc8hz8IEqOw==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "detect-libc": "^2.0.1" + }, + "bin": { + "node-gyp-build-optional-packages": "bin.js", + "node-gyp-build-optional-packages-optional": "optional.js", + "node-gyp-build-optional-packages-test": "build-test.js" + } + }, + "node_modules/node-releases": { + "version": "2.0.60", + "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.60.tgz", + "integrity": "sha512-OmO8+GM1JUZqXET720VrJCwkjUCXmCMQm0Z3UDDRbfNLHS4NEAvY60j5jiFv0tkKw2PNNlqOLhahVmwJIOLDIQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/npm-package-arg": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/npm-package-arg/-/npm-package-arg-14.0.0.tgz", + "integrity": "sha512-69XQh3k+dtGa1p+7RaR57IuG3rCko96xr/nUfN4yDYBXbTYICiWcOpsFKLN2GtGE9cyIljE+f1exnaYt9MvM+Q==", + "dev": true, + "license": "ISC", + "dependencies": { + "hosted-git-info": "^10.1.0", + "proc-log": "^7.0.0", + "semver": "^7.3.5", + "validate-npm-package-name": "^8.0.0" + }, + "engines": { + "node": "^22.22.2 || ^24.15.0 || >=26.0.0" + } + }, + "node_modules/nth-check": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-3.0.1.tgz", + "integrity": "sha512-GX0gsdbGVCgnRgbeGaubfjpBXyYRWOOCVeYh08bSQvDZqxz5ndXs1OTfAt/h36G1xvI94YIspsI0sVFqAV9+RQ==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "boolbase": "^2.0.0" + }, + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/fb55/nth-check?sponsor=1" + } + }, + "node_modules/obug": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/obug/-/obug-2.2.1.tgz", + "integrity": "sha512-XrsrhT5sybtKI6wakr2SPOlGZWWYbUXZ7a0jT8/QOeAPau+1X/bSegNe5YR75oJmEZQbKningirmGOEJCIk61Q==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT", + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/onetime": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-7.0.0.tgz", + "integrity": "sha512-VXJjc87FScF88uafS3JllDgvAm+c/Slfz06lorj2uAY34rlUu0Nt+v8wreiImcrgAjjIHp1rXpTDlLOGw29WwQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "mimic-function": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora": { + "version": "9.4.1", + "resolved": "https://registry.npmjs.org/ora/-/ora-9.4.1.tgz", + "integrity": "sha512-6VlU9MLXbjVQD04AZCMX28hVtA5bUoadvUqO76MUCVA0ilwJbMiHsITRPfyVm6p/BC0Av/BXMujx39WCe1LEqw==", + "dev": true, + "license": "MIT", + "dependencies": { + "chalk": "^5.6.2", + "cli-cursor": "^5.0.0", + "cli-spinners": "^3.2.0", + "is-interactive": "^2.0.0", + "is-unicode-supported": "^2.1.0", + "log-symbols": "^7.0.1", + "stdin-discarder": "^0.3.2", + "string-width": "^8.1.0" + }, + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ordered-binary": { + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/ordered-binary/-/ordered-binary-1.6.3.tgz", + "integrity": "sha512-Bc8LnA2x4ZHRQ9LjCm/NOdD3+CTQPhGNu8m7/6+ERtiB6Bb/fn7nCySwcmAC+5WCqwelpSAQgo1VX9BGpsC0tQ==", + "dev": true, + "license": "MIT", + "optional": true + }, + "node_modules/oxc-parser": { + "version": "0.150.0", + "resolved": "https://registry.npmjs.org/oxc-parser/-/oxc-parser-0.150.0.tgz", + "integrity": "sha512-zwajKw1GUa57IOdafIS7/yOObGF2NihwIfZfs5yJTRprMSkiwhWOc/k+n8bwnUPbHr2/XsjfBO8KyTPaW3VpWg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@oxc-project/types": "^0.150.0" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/sponsors/oxc-project" + }, + "optionalDependencies": { + "@oxc-parser/binding-android-arm-eabi": "0.150.0", + "@oxc-parser/binding-android-arm64": "0.150.0", + "@oxc-parser/binding-darwin-arm64": "0.150.0", + "@oxc-parser/binding-darwin-x64": "0.150.0", + "@oxc-parser/binding-freebsd-x64": "0.150.0", + "@oxc-parser/binding-linux-arm-gnueabihf": "0.150.0", + "@oxc-parser/binding-linux-arm-musleabihf": "0.150.0", + "@oxc-parser/binding-linux-arm64-gnu": "0.150.0", + "@oxc-parser/binding-linux-arm64-musl": "0.150.0", + "@oxc-parser/binding-linux-ppc64-gnu": "0.150.0", + "@oxc-parser/binding-linux-riscv64-gnu": "0.150.0", + "@oxc-parser/binding-linux-riscv64-musl": "0.150.0", + "@oxc-parser/binding-linux-s390x-gnu": "0.150.0", + "@oxc-parser/binding-linux-x64-gnu": "0.150.0", + "@oxc-parser/binding-linux-x64-musl": "0.150.0", + "@oxc-parser/binding-openharmony-arm64": "0.150.0", + "@oxc-parser/binding-win32-arm64-msvc": "0.150.0", + "@oxc-parser/binding-win32-ia32-msvc": "0.150.0", + "@oxc-parser/binding-win32-x64-msvc": "0.150.0" + } + }, + "node_modules/pako": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pako/-/pako-2.2.0.tgz", + "integrity": "sha512-zJq6RP/5q+TO2OpFV3FHzlPnFjmkb7Nc99a5SNjJE+uu/PkpChs+NIZSSzbBoD+6kjiISXjfYdwj1ZRQ81dz/w==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/puzrin" + }, + { + "type": "github", + "url": "https://github.com/sponsors/nodeca" + } + ], + "license": "(MIT AND Zlib)" + }, + "node_modules/parse5": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/parse5/-/parse5-8.0.1.tgz", + "integrity": "sha512-z1e/HMG90obSGeidlli3hj7cbocou0/wa5HacvI3ASx34PecNjNQeaHNo5WIZpWofN9kgkqV1q5YvXe3F0FoPw==", + "license": "MIT", + "dependencies": { + "entities": "^8.0.0" + }, + "funding": { + "url": "https://github.com/inikulin/parse5?sponsor=1" + } + }, + "node_modules/parse5-html-rewriting-stream": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/parse5-html-rewriting-stream/-/parse5-html-rewriting-stream-8.0.1.tgz", + "integrity": "sha512-NaRku2aMpUN1Sh1Gyk1KWUh2A7EJx2c6qYzvwsPtqhoHoaURshdrceYK3LunVCm3WHhm6FS7Vcczbvdh3/UIVw==", + "dev": true, + "license": "MIT", + "dependencies": { + "entities": "^8.0.0", + "parse5": "^8.0.0", + "parse5-sax-parser": "^8.0.0" + }, + "funding": { + "url": "https://github.com/inikulin/parse5?sponsor=1" + } + }, + "node_modules/parse5-sax-parser": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/parse5-sax-parser/-/parse5-sax-parser-8.0.0.tgz", + "integrity": "sha512-/dQ8UzHZwnrzs3EvDj6IkKrD/jIZyTlB+8XrHJvcjNgRdmWruNdN9i9RK/JtxakmlUdPwKubKPTCqvbTgzGhrw==", + "dev": true, + "license": "MIT", + "dependencies": { + "parse5": "^8.0.0" + }, + "funding": { + "url": "https://github.com/inikulin/parse5?sponsor=1" + } + }, + "node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, + "node_modules/performance-now": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", + "integrity": "sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==", + "license": "MIT", + "optional": true + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.7.tgz", + "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/piscina": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/piscina/-/piscina-5.3.2.tgz", + "integrity": "sha512-vv7l/mM7B9WgrhDaudIqv1x6v4LOBRYYmpWGdWushhpbCmVHp11sUB2v4hj1CAeTGXVxuZFz6xOU1RalAiPKfQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.x" + }, + "optionalDependencies": { + "@napi-rs/nice": "^1.0.4" + } + }, + "node_modules/playwright": { + "version": "1.64.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.64.1.tgz", + "integrity": "sha512-HCrVhHr5QQuwYoswEUGTb7gGp522yflbv/ST8nF4sFWGuLjQCt17Q/EubpGGSNKYn1tsTCPZxqHhHCfVURu7UA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.64.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.64.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.64.1.tgz", + "integrity": "sha512-BC4s8y6SEVA6Q1rSxZTto8hI1KUI3xXlHGZgvGqP4IqPdI/a6FX9H29IDNz7vKo9l21b6LfYosScPL6M3xxyDQ==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/pngjs": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/pngjs/-/pngjs-7.0.0.tgz", + "integrity": "sha512-LKWqWJRhstyYo9pGvgor/ivk2w94eSjE3RGVuzLGlr3NmD8bf7RcYGze1mNdEHRP6TRP6rMuDHk5t44hnTRyow==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.19.0" + } + }, + "node_modules/postcss": { + "version": "8.5.31", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.31.tgz", + "integrity": "sha512-YUbWYCgErwnMAVkjUnYa+KlMBzGpHwiIt/DE7f0G33AuX2LH9UcTk6KgWxd3zRU+pN3u/C+Xl3EnMLM4Hea7Sg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.20", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.2" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/postcss-media-query-parser": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/postcss-media-query-parser/-/postcss-media-query-parser-0.2.3.tgz", + "integrity": "sha512-3sOlxmbKcSHMjlUXQZKQ06jOswE7oVkXPxmZdoB1r5l0q6gTFTQSHxNxOrCccElbW7dxNytifNEo8qidX2Vsig==", + "dev": true, + "license": "MIT" + }, + "node_modules/postcss-safe-parser": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/postcss-safe-parser/-/postcss-safe-parser-7.1.0.tgz", + "integrity": "sha512-1WzZxRLaAFwEh6Do+zyGpjWV3nGJNxxhuh7Ubu/q1ICImMgZJnLwhoaEpRbG4pJppp2Y1ncL9ffA2f+LhrefQg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss-safe-parser" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "engines": { + "node": ">=18.0" + }, + "peerDependencies": { + "postcss": "^8.4.31" + } + }, + "node_modules/proc-log": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/proc-log/-/proc-log-7.0.0.tgz", + "integrity": "sha512-FYgfaA69XZ93zaXLoMNQ+ViDXGGBgR8aLh03txzcFhV+9xOXx7+8DLCULrKKpR9+GsH9ZfHm82aSUPpozX0Ztg==", + "dev": true, + "license": "ISC", + "engines": { + "node": "^22.22.2 || ^24.15.0 || >=26.0.0" + } + }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", + "license": "MIT" + }, + "node_modules/proxy-agent-negotiate": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-agent-negotiate/-/proxy-agent-negotiate-1.1.0.tgz", + "integrity": "sha512-N8IBcM3UgCVzz2L2Lqv8DVntDnnC8/hiV4nEDUPkqq72TPUgYWjQc+bdZlBPZK9LzPAvOY//gAt0S0DApoOXWQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 20" + }, + "peerDependencies": { + "kerberos": "^2.0.0" + }, + "peerDependenciesMeta": { + "kerberos": { + "optional": true + } + } + }, + "node_modules/raf": { + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/raf/-/raf-3.4.1.tgz", + "integrity": "sha512-Sq4CW4QhwOHE8ucn6J34MqtZCeWFP2aQSmrlroYgqAV1PjStIhJXxYuTgUIfkEk7zTLjmIjLmU5q+fbD1NnOJA==", + "license": "MIT", + "optional": true, + "dependencies": { + "performance-now": "^2.1.0" + } + }, + "node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/readdirp": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-5.1.1.tgz", + "integrity": "sha512-Kko+Y5XQ6fM+Ce3dq3m9YGxnacYZYl9cA1wZjaF3Vbry2L3i1qVg8+CAgNPsXRArPMUMCaOR7oa9Nqntc43JKA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 20.19.0" + }, + "funding": { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/reflect-metadata": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.2.tgz", + "integrity": "sha512-urBwgfrvVP/eAyXx4hluJivBKzuEbSQs9rKWCrCkbSxNv8mxPcUZKeuoF3Uy4mJl3Lwprp6yy5/39VWigZ4K6Q==", + "dev": true, + "license": "Apache-2.0" + }, + "node_modules/regenerator-runtime": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz", + "integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==", + "license": "MIT", + "optional": true + }, + "node_modules/require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/restore-cursor": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-5.1.0.tgz", + "integrity": "sha512-oMA2dcrw6u0YfxJQXm342bFKX/E4sG9rbTzO9ptUcR/e8A33cHuvStiYOwH7fszkZlZ1z/ta9AAoPk2F4qIOHA==", + "dev": true, + "license": "MIT", + "dependencies": { + "onetime": "^7.0.0", + "signal-exit": "^4.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/rgbcolor": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/rgbcolor/-/rgbcolor-1.0.1.tgz", + "integrity": "sha512-9aZLIrhRaD97sgVhtJOW6ckOEh6/GnvQtdVNfdZ6s67+3/XwLS9lBcQYzEEhYVeUowN7pRzMLsyGhK2i/xvWbw==", + "license": "MIT OR SEE LICENSE IN FEEL-FREE.md", + "optional": true, + "engines": { + "node": ">= 0.8.15" + } + }, + "node_modules/robust-predicates": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/robust-predicates/-/robust-predicates-3.0.3.tgz", + "integrity": "sha512-NS3levdsRIUOmiJ8FZWCP7LG3QpJyrs/TE0Zpf1yvZu8cAJJ6QMW92H1c7kWpdIHo8RvmLxN/o2JXTKHp74lUA==", + "license": "Unlicense" + }, + "node_modules/rolldown": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.8.tgz", + "integrity": "sha512-Z67nTmhZe7anqnM/EjI392w5i/ANUinjip7QYsOyN37oayduxt3ksdX0hf5OOamkAd53BiIHfbfSzfUmzKFQqQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@oxc-project/types": "=0.149.0", + "@rolldown/pluginutils": "^1.0.0" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm-eabi": "1.2.8", + "@rolldown/binding-android-arm64": "1.2.8", + "@rolldown/binding-darwin-arm64": "1.2.8", + "@rolldown/binding-darwin-x64": "1.2.8", + "@rolldown/binding-freebsd-x64": "1.2.8", + "@rolldown/binding-linux-arm-gnueabihf": "1.2.8", + "@rolldown/binding-linux-arm64-gnu": "1.2.8", + "@rolldown/binding-linux-arm64-musl": "1.2.8", + "@rolldown/binding-linux-ppc64-gnu": "1.2.8", + "@rolldown/binding-linux-s390x-gnu": "1.2.8", + "@rolldown/binding-linux-x64-gnu": "1.2.8", + "@rolldown/binding-linux-x64-musl": "1.2.8", + "@rolldown/binding-openharmony-arm64": "1.2.8", + "@rolldown/binding-win32-arm64-msvc": "1.2.8", + "@rolldown/binding-win32-x64-msvc": "1.2.8" + } + }, + "node_modules/rolldown/node_modules/@oxc-project/types": { + "version": "0.149.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.149.0.tgz", + "integrity": "sha512-Efcc+iF0j3Bf67YjEqIqWXbX5XddXoK/Mw4K1/JuXwRCZ8N16VR7iT23nlCc9XrveFVh/E5Rqs2StT0V8v9LdA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/oxc-project" + } + }, + "node_modules/rw": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz", + "integrity": "sha512-PdhdWy89SiZogBLaw42zdeqtRJ//zFd2PgQavcICDUgJT5oW10QCRKbJ6bg4r0/UY2M6BWd5tkxuGFRvCkgfHQ==", + "license": "BSD-3-Clause" + }, + "node_modules/rxjs": { + "version": "7.8.2", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.2.tgz", + "integrity": "sha512-dhKf903U/PQZY6boNNtAGdWbG85WAbjT/1xYoZIC7FAY0yWapOBQVsVrDl58W86//e1VpMNBtRV4MaXfdMySFA==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "^2.1.0" + } + }, + "node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/sass": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass/-/sass-1.104.1.tgz", + "integrity": "sha512-yDA+1aIG3EHgN4V/BvuhCvu61FF6hEd4e+9DxikUm9U0CAGuvdIZ/UYy7qbOxjhbbWQraoyLlMuzdRGOSV5Bmw==", + "dev": true, + "license": "MIT", + "dependencies": { + "chokidar": "^5.0.0", + "immutable": "^5.1.5", + "source-map-js": ">=0.6.2 <2.0.0" + }, + "bin": { + "sass": "sass.js" + }, + "engines": { + "node": ">=20.19.0" + }, + "optionalDependencies": { + "@parcel/watcher": "^2.4.1" + } + }, + "node_modules/sass-embedded": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded/-/sass-embedded-1.104.1.tgz", + "integrity": "sha512-7LxbtPJ1MWKVbhzOnGx1sHTkHTcHVJ9hbgVsU29p/8gY1VeXt19fcJFr5xsx2f6zfrrR3FeOcYMt0uYDLAxpgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@bufbuild/protobuf": "^2.5.0", + "colorjs.io": "^0.7.0", + "immutable": "^5.1.5", + "rxjs": "^7.4.0", + "supports-color": "^8.1.1", + "sync-child-process": "^1.0.2", + "varint": "^6.0.0" + }, + "bin": { + "sass": "dist/bin/sass.js" + }, + "engines": { + "node": ">=20.19.0" + }, + "optionalDependencies": { + "sass-embedded-all-unknown": "1.104.1", + "sass-embedded-android-arm": "1.104.1", + "sass-embedded-android-arm64": "1.104.1", + "sass-embedded-android-riscv64": "1.104.1", + "sass-embedded-android-x64": "1.104.1", + "sass-embedded-darwin-arm64": "1.104.1", + "sass-embedded-darwin-x64": "1.104.1", + "sass-embedded-linux-arm": "1.104.1", + "sass-embedded-linux-arm64": "1.104.1", + "sass-embedded-linux-musl-arm": "1.104.1", + "sass-embedded-linux-musl-arm64": "1.104.1", + "sass-embedded-linux-musl-riscv64": "1.104.1", + "sass-embedded-linux-musl-x64": "1.104.1", + "sass-embedded-linux-riscv64": "1.104.1", + "sass-embedded-linux-x64": "1.104.1", + "sass-embedded-unknown-all": "1.104.1", + "sass-embedded-win32-arm64": "1.104.1", + "sass-embedded-win32-x64": "1.104.1" + } + }, + "node_modules/sass-embedded-all-unknown": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-all-unknown/-/sass-embedded-all-unknown-1.104.1.tgz", + "integrity": "sha512-toyMCyUKkT6DsYlHuKZGjd8T6WltaW4TGX07F7HnF4tNJM5m/8VaVsd74zrq3QSPqGE9JjxIDK4NrNc/QY9pkA==", + "cpu": [ + "!arm", + "!arm64", + "!riscv64", + "!x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "sass": "1.104.1" + } + }, + "node_modules/sass-embedded-android-arm": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-android-arm/-/sass-embedded-android-arm-1.104.1.tgz", + "integrity": "sha512-g/XPAJYAzSSQZFP4xCdGD0HLKB4mf24cJ/+5Nls8rMool3jNkBv9+AFO4EV0b+RxN8rLcEsMccH1CR64yIFYcQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-android-arm64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-android-arm64/-/sass-embedded-android-arm64-1.104.1.tgz", + "integrity": "sha512-yW3BVW4YFpN2vu2cVQ+Pz5JrRTvoIz36aCdo+J/Mo1A9gEN8eVPV/AL7aeZ803YdKgIitd3qw3pY4wpmT6TXNg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-android-riscv64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-android-riscv64/-/sass-embedded-android-riscv64-1.104.1.tgz", + "integrity": "sha512-ZYY8oMKQS7Q/6FBoC8w3T16O3MucDe3xv8iKym41Nht5uvLCGGpm4fC4OF5nlv7wQkJv8oactYddGG3Wd0Rqkg==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-android-x64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-android-x64/-/sass-embedded-android-x64-1.104.1.tgz", + "integrity": "sha512-5WZKC4lSfjMIjzZdQUlYQUAV+UiatBhD8w28U7cYLLj4/dMgEPOBd4g3VMv0BwpgH2JOaycMS42dXq29W2bcDg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-darwin-arm64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-darwin-arm64/-/sass-embedded-darwin-arm64-1.104.1.tgz", + "integrity": "sha512-oatuHzn0NsEg0d9Dcze6CuWUPvuETtMxRK4cv0eRAg7p6R0vHnv5GZRyWG6gEy3nc/ha/L+GtTasWVU82f4jrQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-darwin-x64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-darwin-x64/-/sass-embedded-darwin-x64-1.104.1.tgz", + "integrity": "sha512-gql/5sHGzUTVwuHfM3QLCDeb6NYwen+LLj22gz81C2NKHF/4nUtDyh4N+fURWdT7J0/EhtMOd0+FLML3sGNP7g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-arm": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-arm/-/sass-embedded-linux-arm-1.104.1.tgz", + "integrity": "sha512-WfmshPJOp9vUKxgp8wZ2ncaKBbyrlW9J51M2aUQHWECjMNxTULnGOHWSmD7FViXv4zwn1Co5FIFQUipW7RCxtA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-arm64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-arm64/-/sass-embedded-linux-arm64-1.104.1.tgz", + "integrity": "sha512-sVWUKXNTu8sP3X95Df0xo/t49Ce5IseTIMqXHxhFBKVmx7gf7kBWNLK0wH5ym+4khc+B5nNJPgq4IxpelG8tMQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-musl-arm": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-arm/-/sass-embedded-linux-musl-arm-1.104.1.tgz", + "integrity": "sha512-k0cQ/CxBzFVYiQEObg6CpF6CUglkdEhi/DZgZXQbwC27kbGGY+fatmIs/cFPJL2z1ulACwoAr86RQUFzYvw5eg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-musl-arm64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-arm64/-/sass-embedded-linux-musl-arm64-1.104.1.tgz", + "integrity": "sha512-H7b+LzZ/w8pwNExInM8HDHraqvxCMdUc5ZRGU/aSJJhZ9HjeRKBjF3GxaisVHnx7o18j1l4VQ5cLBL9tTmNzZw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-musl-riscv64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-riscv64/-/sass-embedded-linux-musl-riscv64-1.104.1.tgz", + "integrity": "sha512-9DYdn4wfrkidjxJVtEd2iCx8HKdZe+uF82GDmrYEZhgu4NjVE6a4fQyW7OLwzn3erZMHKBaObBOH1d37DnXI1A==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-musl-x64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-x64/-/sass-embedded-linux-musl-x64-1.104.1.tgz", + "integrity": "sha512-5xz+nAr01b6yaRdrGVgE7lNvm1lGFjwNdUvnz+teidvHCbD5oTcbhrRVrnDoLHHvq5n3Jve9TzWaaHhezaEfHA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-riscv64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-riscv64/-/sass-embedded-linux-riscv64-1.104.1.tgz", + "integrity": "sha512-oWryPJWTXK5adtlz/FD7wd2kS4I0CEgp6mWLUVb4oMFURMLz9bA9fO87D7lc4SV++lds7T51PU1kykCbOvNHMg==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-linux-x64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-linux-x64/-/sass-embedded-linux-x64-1.104.1.tgz", + "integrity": "sha512-j/sX/yKTx/wJKepC5/ISlR6kPeSuHKThf76D2za5i2VnbHg0MN19jQq3WMorowR3kRghqt6azs3awogG+e7IRA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-unknown-all": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-unknown-all/-/sass-embedded-unknown-all-1.104.1.tgz", + "integrity": "sha512-RaMsYZB5mRioL4qhoI7T5mmNb2OunakbPTJW2cuu3MeIULnGoxo7Wd5QKFxeQvF7sSQL57RuaQ+q9pH+/pQ9Fg==", + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "!android", + "!darwin", + "!linux", + "!win32" + ], + "dependencies": { + "sass": "1.104.1" + } + }, + "node_modules/sass-embedded-win32-arm64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-win32-arm64/-/sass-embedded-win32-arm64-1.104.1.tgz", + "integrity": "sha512-fS6DL7lcnTPclqGLF4xRt8egZlFY9cU23d57ufbWqaUbCFu9JTu0+8qB4jMxfx1la6byi3GoSTfbnNOLfWv8sw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/sass-embedded-win32-x64": { + "version": "1.104.1", + "resolved": "https://registry.npmjs.org/sass-embedded-win32-x64/-/sass-embedded-win32-x64-1.104.1.tgz", + "integrity": "sha512-/c4N2A93Ckms6DuRFVftKYISuI1tyC8gmlVJ8WaWFzgCBN/Swq2rARJIBUNeuxlmxjmYXfd3+2f/u887K63wbA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/semver": { + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "dev": true, + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==", + "license": "MIT" + }, + "node_modules/signal-exit": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/sirv": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/sirv/-/sirv-3.0.2.tgz", + "integrity": "sha512-2wcC/oGxHis/BoHkkPwldgiPSYcpZK3JU28WoMVv55yHJgcZ8rlXvuG9iZggz+sU1d4bRgIGASwyWqjxu3FM0g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@polka/url": "^1.0.0-next.24", + "mrmime": "^2.0.0", + "totalist": "^3.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/slice-ansi": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-9.0.1.tgz", + "integrity": "sha512-aBY19bn/XA+hKOKX0qL7Z0UfdBsbQvc9hn993U8ALUjzxCvDcuZqLoRXjGJrUARWAlwMnsRVD9sw2AFNOYvalA==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^6.2.3", + "is-fullwidth-code-point": "^5.1.0" + }, + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/chalk/slice-ansi?sponsor=1" + } + }, + "node_modules/source-map": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.8.0.tgz", + "integrity": "sha512-d8EqvL+k/SOXCreS/SUzg2ciyHqBBLcN/yuRjFsbvVhHTE2pgei7oAhmPM7kWFbkX6OSMQfUq4KbkF3au9lhYQ==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">= 12" + } + }, + "node_modules/source-map-js": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.2.tgz", + "integrity": "sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/source-map-support": { + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "dev": true, + "license": "MIT", + "dependencies": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, + "node_modules/source-map-support/node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/stackblur-canvas": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/stackblur-canvas/-/stackblur-canvas-2.7.0.tgz", + "integrity": "sha512-yf7OENo23AGJhBriGx0QivY5JP6Y1HbrrDI6WLt6C5auYZXlQrheoY8hD4ibekFKz1HOfE48Ww8kMWMnJD/zcQ==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=0.1.14" + } + }, + "node_modules/std-env": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-4.3.0.tgz", + "integrity": "sha512-OtU/EgQ1kIm5KwqQpBC6ZEMXrZRui11w8zgfTWp8cdO9B8OaPsbA8bTHO2P+HNo1VlUTGMVBwPhydu6poeXiag==", + "dev": true, + "license": "MIT" + }, + "node_modules/stdin-discarder": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/stdin-discarder/-/stdin-discarder-0.3.2.tgz", + "integrity": "sha512-eCPu1qRxPVkl5605OTWF8Wz40b4Mf45NY5LQmVPQ599knfs5QhASUm9GbJ5BDMDOXgrnh0wyEdvzmL//YMlw0A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/string-width": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-8.3.0.tgz", + "integrity": "sha512-ZbmZM0JCihQN91dWnxoipT2KOEyHqEyfRXUyjuRhW8b/xnqPDoq4gWEVApTVa9db2wN8mmoikgFBbjh71+cGeQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "get-east-asian-width": "^1.5.0", + "strip-ansi": "^7.1.2" + }, + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/strip-ansi": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.2.0.tgz", + "integrity": "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-regex": "^6.2.2" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/strip-ansi?sponsor=1" + } + }, + "node_modules/supports-color": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/svg-pathdata": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/svg-pathdata/-/svg-pathdata-6.0.3.tgz", + "integrity": "sha512-qsjeeq5YjBZ5eMdFuUa4ZosMLxgr5RZ+F+Y1OrDhuOCEInRMA3x74XdBtggJcj9kOeInz0WE+LgCPDkZFlBYJw==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/sync-child-process": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/sync-child-process/-/sync-child-process-1.1.0.tgz", + "integrity": "sha512-1LMf/x0kU0CKFSesh5DfYktvj1KhiqYi5LAh5HU2G4SkKk1TGDhsMFBOL47g/E+mNfYpgs365pVNlIYOIHPvlQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "sync-message-port": "^1.0.0" + }, + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/sync-message-port": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/sync-message-port/-/sync-message-port-1.2.0.tgz", + "integrity": "sha512-gAQ9qrUN/UCypHtGFbbe7Rc/f9bzO88IwrG8TDo/aMKAApKyD6E3W4Cm0EfhfBb6Z6SKt59tTCTfD+n1xmAvMg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=16.0.0" + } + }, + "node_modules/text-segmentation": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/text-segmentation/-/text-segmentation-1.0.3.tgz", + "integrity": "sha512-iOiPUo/BGnZ6+54OsWxZidGCsdU8YbE4PSpdPinp7DeMtUJNJBoJ/ouUSTJjHkh1KntHaltHl/gDs2FC4i5+Nw==", + "license": "MIT", + "dependencies": { + "utrie": "^1.0.2" + } + }, + "node_modules/tinybench": { + "version": "6.2.2", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-6.2.2.tgz", + "integrity": "sha512-nIZ2N2dFpjBsg2hgitKu7mA+FvsIJuGJOwysRngmVRRmWgR/UyPjwJj8c/fT/dhm8EkHnF5EOogRf+0Z2qrimA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/tinyexec": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.3.1.tgz", + "integrity": "sha512-GCvB3aoys96IuDFBMcTB46JOR6mdMtAToqwiW8JlWhsoh1mhHi/xn9ss/Dg7N555GiJyEt2qzoG/NHCwM6h1EA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/tinyglobby": { + "version": "0.2.17", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", + "integrity": "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==", + "dev": true, + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.4" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/tinyrainbow": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.2.0.tgz", + "integrity": "sha512-LgO3D9yZJjApUiuUfl9iFAwrtaX4+lok3wJIqttGoKCHlWUqHqbQpnxCf82L8FjgKsh4iGo78hqJwgL8F6To2A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/totalist": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/totalist/-/totalist-3.0.1.tgz", + "integrity": "sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "license": "0BSD" + }, + "node_modules/typescript": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/update-browserslist-db": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.3.4.tgz", + "integrity": "sha512-t8wonIOdQyOjtPd2VktlBu6QjTVTks6v8bSi7kd0r2Vll8TK1JYDZtRRdbmCHOAMphkZgrzK+L+vXK4pyZ8t4A==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/browserslist" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "escalade": "^3.2.0", + "picocolors": "^1.1.1" + }, + "bin": { + "update-browserslist-db": "cli.js" + }, + "peerDependencies": { + "browserslist": ">= 4.21.0" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT" + }, + "node_modules/utrie": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/utrie/-/utrie-1.0.2.tgz", + "integrity": "sha512-1MLa5ouZiOmQzUbjbu9VmjLzn1QLXBhwpUa7kdLUQK+KQ5KA9I1vk5U4YHe/X2Ch7PYnJfWuWT+VbuxbGwljhw==", + "license": "MIT", + "dependencies": { + "base64-arraybuffer": "^1.0.2" + } + }, + "node_modules/validate-npm-package-name": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/validate-npm-package-name/-/validate-npm-package-name-8.0.0.tgz", + "integrity": "sha512-SCv6OOV6Xj2/3cXy3dGmADluJTNcL3o7hZAglNPTe+WYuEuvxgJzxPrSDLZhF+CwyQOubqgecjMmTJGMVLWjYQ==", + "dev": true, + "license": "ISC", + "engines": { + "node": "^22.22.2 || ^24.15.0 || >=26.0.0" + } + }, + "node_modules/varint": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/varint/-/varint-6.0.0.tgz", + "integrity": "sha512-cXEIW6cfr15lFv563k4GuVuW/fiwjknytD37jIOLSdSWuOI6WnO/oKwmP2FQTU2l01LP8/M5TSAJpzUaGe3uWg==", + "dev": true, + "license": "MIT" + }, + "node_modules/verkit": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/verkit/-/verkit-0.3.2.tgz", + "integrity": "sha512-zj/ob3UsvJGN0whEAKFp53REA5X66hvffVqoCtVQAakJKnKlH+/PcOfMoFwIG/o4rElqLv/ycAFlx8ZlXUorCg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18.12.0" + }, + "funding": { + "url": "https://github.com/sponsors/sxzz" + } + }, + "node_modules/vite": { + "version": "8.3.5", + "resolved": "https://registry.npmjs.org/vite/-/vite-8.3.5.tgz", + "integrity": "sha512-z1YpjieJ9g0ebTmQpLBv/oq9E3F9EIlOe+7LahW8SrE3LTiG3udBRUXQOlYXP9b+IC2q2phHeVxj9xGlw34OEQ==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "lightningcss": "^1.33.0", + "picomatch": "^4.0.7", + "postcss": "^8.5.30", + "rolldown": "~1.2.14", + "tinyglobby": "^0.2.17" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "@vitejs/devtools": "^0.7.1 || ^0.8.0", + "esbuild": "^0.27.0 || ^0.28.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "@vitejs/devtools": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vite/node_modules/@oxc-project/types": { + "version": "0.155.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.155.0.tgz", + "integrity": "sha512-SeE8JPwW1xs1tHeNycjUtUBdhorRa8o4jlvjwk49R7/Z291i+4DQa7JJIgOm/TaYQZx+QrPQbVJ/9c8dMIZN8Q==", + "dev": true, + "license": "MIT", + "peer": true, + "funding": { + "url": "https://github.com/sponsors/oxc-project" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-android-arm-eabi": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.15.tgz", + "integrity": "sha512-aLcgK9gIJ3YRvrrtfSBbRa27F5WrpiTaPulnFpsHPxpUHgFlSgnU4854MHwoRGEBhfRJDWCS2WQ7lIjskE9Olw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-android-arm64": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.2.15.tgz", + "integrity": "sha512-xD7DdZCrbThKuxNg7ooQmvp07ys4J/pnSEMTG3/OdOWl6daC67m/Q4fL78sRHMxTH/N4fGUbYhcPM0MF5WFHkg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.2.15.tgz", + "integrity": "sha512-m2WuQ6r0B4OTYxyPpNLtXaMRbRDUbG8YQV2Q3KTdSSK8Th6KHCnC8jVCJt8bvYeSB5WivkkNbc85mKadaSdOxA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-darwin-x64": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.2.15.tgz", + "integrity": "sha512-5RtnsHM36nodM9WbMy+zip0LGS/G+y5wro5RHrpOlr4ac24+1zSi9kiV2MP1myZeZ/ifEHfulQcWjnAXzc2C2A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.2.15.tgz", + "integrity": "sha512-g3l09pyFqhu+135JKkbkwiKGQmwU5hUMUPJ4VdBADvuqR8d33L6H578rh8m7rtxbdp+VmX/+nzI7nvhSZlzpUQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.2.15.tgz", + "integrity": "sha512-nIe3VugI97nGQtspPxDqA4sXMUpeiocKIP/CqpY/u/vgbT+alX+cVxu1C/oVBPKg4feNTmYovE91v4DgXvXNwQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.2.15.tgz", + "integrity": "sha512-iTBjXx+B5QSvgI89lJfmqh4wTh8NabR1YZIIN3hNx4zSG0EVfVqkOvLulNpG4251pbd6/IAmZ/LefVk1Y4rPhw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.2.15.tgz", + "integrity": "sha512-uN26XOnVR7rqQupG3V9oAG2EGGatWjcJHOeDOHUFCh4TBWmP2tHTFoVN13aYbwfm0aHqJAEmcwhAu6sooIjfqQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.2.15.tgz", + "integrity": "sha512-9nWkcdd+H+XhaA3fQIwpHnKIrfx/PQ6rO0pivr78Og07tXlUlctBBJoEYLPfhTmCR/QxEVdn6uPjHV5SymPhdw==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.2.15.tgz", + "integrity": "sha512-Ggh0ciFjLwIusLSLz7LZYEaGdxOAUJXI+Qu5JCbhPJJHEt8vP+IjRATTJheiJsdzzTYgUiHUiw3X1Ea3ZpMBdA==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.2.15.tgz", + "integrity": "sha512-FHg60rS6DBePAYZ9X9F67x+JObFdmgn6tupA1C/a3B+CufFmTD4mX+e8M75tCiAO3foR/6xwVBI0KQiI7sn/rw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.2.15.tgz", + "integrity": "sha512-/cvV/7v5VGz/moiz0XKRj2dE8DzsKyhIu1KpOvJGPjRywg3yscON889UGvAe8zfiJLHup7VLjBduoGWi3HusWQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.2.15.tgz", + "integrity": "sha512-MyzC5Hrv/sm+Z7TSGKP/j8001RO2xQm1zOpCNVL4j0cY/VhBZviludA2YFQ7i4lH6ZAXxQm0lcMdBzM+99mE7w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.2.15.tgz", + "integrity": "sha512-+PgYiS+AH5omy6L45ZLxybasauJaj3M8buoT83D+nzPmCayXlxUV9hydhciCKVyIg0B/v0BtXPDE1uGqNwCPpg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.2.15.tgz", + "integrity": "sha512-HFZd2QNQYn1JbIi+Hpd7H+r/lkWxcfIu28nirZ6eCn2LafuvmExMlb/P96Sfx5tf7pPArw39IdhmCF8vmV+NhQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "peer": true, + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/vite/node_modules/rolldown": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.15.tgz", + "integrity": "sha512-6odND+fel5zVw3I4Yc2e69gvguDnX0CAAGiNDZSq4ik5WUcaLrkD2GLuog0XcODbor31hZ5ayEMgVDno+KPV5g==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "@oxc-project/types": "=0.155.0", + "@rolldown/pluginutils": "^1.0.0" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm-eabi": "1.2.15", + "@rolldown/binding-android-arm64": "1.2.15", + "@rolldown/binding-darwin-arm64": "1.2.15", + "@rolldown/binding-darwin-x64": "1.2.15", + "@rolldown/binding-freebsd-x64": "1.2.15", + "@rolldown/binding-linux-arm-gnueabihf": "1.2.15", + "@rolldown/binding-linux-arm64-gnu": "1.2.15", + "@rolldown/binding-linux-arm64-musl": "1.2.15", + "@rolldown/binding-linux-ppc64-gnu": "1.2.15", + "@rolldown/binding-linux-s390x-gnu": "1.2.15", + "@rolldown/binding-linux-x64-gnu": "1.2.15", + "@rolldown/binding-linux-x64-musl": "1.2.15", + "@rolldown/binding-openharmony-arm64": "1.2.15", + "@rolldown/binding-win32-arm64-msvc": "1.2.15", + "@rolldown/binding-win32-x64-msvc": "1.2.15" + } + }, + "node_modules/vitest": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-5.0.5.tgz", + "integrity": "sha512-cPODOMF8YnkiiYtDdHo6n4wWmBrOBBbOIYH2d33M4l3CdNn72h+qMmO6uXffUN6A5AZFXNND/d+krS9mgkXRfg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/mocker": "5.0.5", + "chai": "^6.3.0", + "es-module-lexer": "^2.3.2", + "expect-type": "^1.4.0", + "magic-string": "^1.4.3", + "obug": "^3.0.0", + "picomatch": "^4.0.7", + "std-env": "^4.3.0", + "tinybench": "^6.2.2", + "tinyexec": "^1.3.1", + "tinyglobby": "^0.2.17", + "why-is-node-running": "3.2.1" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^22.12.0 || ^24.0.0 || >=26.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@opentelemetry/api": "^1.9.0", + "@types/node": "^22.0.0 || >=24.0.0", + "@vitest/browser-playwright": "5.0.5", + "@vitest/browser-preview": "5.0.5", + "@vitest/browser-webdriverio": "^5.0.0-beta.5 || >=5.0.0", + "@vitest/coverage-istanbul": "5.0.5", + "@vitest/coverage-v8": "5.0.5", + "@vitest/ui": "5.0.5", + "happy-dom": "*", + "jsdom": "*", + "vite": "^6.4.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser-playwright": { + "optional": true + }, + "@vitest/browser-preview": { + "optional": true + }, + "@vitest/browser-webdriverio": { + "optional": true + }, + "@vitest/coverage-istanbul": { + "optional": true + }, + "@vitest/coverage-v8": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + }, + "vite": { + "optional": false + } + } + }, + "node_modules/vitest/node_modules/magic-string": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-1.4.3.tgz", + "integrity": "sha512-z12OxmaPGE0F4xlpGdjuAUckipLpQlTAuAmyGhNoD7ODpYUzvDfvtxUbjM/jwznyP178oju/KDdyi220wNJ0RQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.6.0" + } + }, + "node_modules/vitest/node_modules/obug": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/obug/-/obug-3.0.0.tgz", + "integrity": "sha512-5vvB5+W7ePv+p3uqxi+RcW1XAzLW0/hxt3/4X4Lc4qHudzOhmBiBwOY6DRob4WnanAEGvNcLjF+KNOufrUoEQw==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT", + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/weak-lru-cache": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/weak-lru-cache/-/weak-lru-cache-1.2.2.tgz", + "integrity": "sha512-DEAoo25RfSYMuTGc9vPJzZcZullwIqRDSI9LOy+fkCJPi6hykCnfKaXTuPBDuXAUcqHXyOgFtHNp/kB2FjYHbw==", + "dev": true, + "license": "MIT", + "optional": true + }, + "node_modules/why-is-node-running": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-3.2.1.tgz", + "integrity": "sha512-Tb2FUhB4vUsGQlfSquQLYkApkuPAFQXGFzxWKHHumVz2dK+X1RUm/HnID4+TfIGYJ1kTcwOaCk/buYCEJr6YjQ==", + "dev": true, + "license": "MIT", + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=20.11" + } + }, + "node_modules/wrap-ansi": { + "version": "10.0.2", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-10.0.2.tgz", + "integrity": "sha512-6OLRNZqRntVGBm26ghZ5eZhCuWWyOkRCSn8XsOIuq5stpCE/tjLbdozd93naILZig0lmD8ANfRwnhn0DLeXGlg==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^6.2.3", + "string-width": "^8.2.0" + }, + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, + "node_modules/ws": { + "version": "8.22.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.22.0.tgz", + "integrity": "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/xxhash-wasm": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/xxhash-wasm/-/xxhash-wasm-1.1.0.tgz", + "integrity": "sha512-147y/6YNh+tlp6nd/2pWq38i9h6mz/EuQ6njIrmW8D1BS5nCqs0P6DG+m6zTGnNz5I+uhZ0SHxBs9BsPrwcKDA==", + "dev": true, + "license": "MIT" + }, + "node_modules/y18n": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", + "dev": true, + "license": "ISC", + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs": { + "version": "18.1.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-18.1.0.tgz", + "integrity": "sha512-2rAgRKu54VsHkqI0/tYkmluGXHD4KW7yZoycuqDQ15QOTnc2VVfy0nN/1eMhnQLO00A+dwtK20xuCnc1YGeUyg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cliui": "^9.0.1", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "string-width": "^8.2.1", + "y18n": "^5.0.5", + "yargs-parser": "^22.0.0" + }, + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=23" + } + }, + "node_modules/yargs-parser": { + "version": "22.0.0", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-22.0.0.tgz", + "integrity": "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw==", + "dev": true, + "license": "ISC", + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=23" + } + }, + "node_modules/yoctocolors": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/yoctocolors/-/yoctocolors-2.2.0.tgz", + "integrity": "sha512-xYqdZFUK/VYazNl/oCDYN+3WloWQwMfZxBoiNt6qNyk+xfOdi598muWE42rNZFp1kNOiqW936q5RhUdnpqElSg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/zod": { + "version": "4.6.6", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.6.6.tgz", + "integrity": "sha512-4UmzQUF5fxvfcYHlbjBmejU+w+hbDnWFx4aS8kBx7nPBHcO2SqPbpAG8snbdg+Xse4pRefR98hgjFgIMCI/UhA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + }, + "node_modules/zrender": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/zrender/-/zrender-6.1.0.tgz", + "integrity": "sha512-oEGMDB6pOP2S6OwRR4PdVv610zrjnA3Bh+JnSG12fYJlBKjtNAoEb5fSUoCOOINlH96I2fU38/A2UpRKs67xYQ==", + "license": "BSD-3-Clause", + "dependencies": { + "tslib": "2.3.0" + } + }, + "node_modules/zrender/node_modules/tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==", + "license": "0BSD" + } + } +} diff --git a/package.json b/package.json index 639f7559b..e76fa06da 100644 --- a/package.json +++ b/package.json @@ -3,61 +3,56 @@ "version": "0.0.0", "scripts": { "ng": "ng", + "prestart": "node scripts/check-node-version.mjs", "start": "ng serve", + "prebuild": "node scripts/check-node-version.mjs", "build": "ng build", + "prewatch": "node scripts/check-node-version.mjs", "watch": "ng build --watch --configuration development", - "test": "ng test" + "pretest": "node scripts/check-node-version.mjs", + "pretypecheck": "node scripts/check-node-version.mjs", + "typecheck": "tsc -p tsconfig.app.json --noEmit && tsc -p tsconfig.spec.json --noEmit", + "check:dead-code": "node scripts/check-dead-code.mjs", + "verify": "npm run typecheck && npm run check:dead-code && npm run build", + "test": "ng test", + "e2e": "playwright test" }, "private": true, + "engines": { + "node": "^22.22.3 || ^24.15.0 || >=26.0.0", + "npm": ">=10.0.0" + }, "dependencies": { - "@angular/animations": "^21.1.3", - "@angular/cdk": "^21.1.3", - "@angular/common": "^21.1.3", - "@angular/compiler": "^21.1.3", - "@angular/core": "^21.1.3", - "@angular/forms": "^21.1.3", - "@angular/material": "^21.1.3", - "@angular/platform-browser": "^21.1.3", - "@angular/platform-browser-dynamic": "^21.1.3", - "@angular/router": "^21.1.3", - "@fortawesome/fontawesome-free": "^6.6.0", - "@kerwin612/ngx-query-builder": "^0.6.4", + "@angular/cdk": "^22.2.4", + "@angular/common": "^22.2.3", + "@angular/compiler": "^22.2.3", + "@angular/core": "^22.2.3", + "@angular/forms": "^22.2.3", + "@angular/material": "^22.2.4", + "@angular/platform-browser": "^22.2.3", + "@angular/router": "^22.2.3", + "@fortawesome/fontawesome-free": "^7.3.1", "d3": "^7.9.0", - "echarts": "5.4.3", - "echarts-for-angular": "^0.4.0", - "echarts-gl": "2.0.9", + "echarts": "6.1.0", "html2canvas": "^1.4.1", - "jquery": "^3.7.1", - "jQuery-QueryBuilder": "^3.0.0", - "jquery-ui": "^1.14.1", - "jquery-ui-dist": "^1.13.3", - "jspdf": "^3.0.4", - "jspdf-autotable": "^5.0.7", - "ngx-echarts": "^19.0.0", - "query-builder": "^0.3.37", - "rxjs": "~7.8.0", - "tslib": "^2.3.0", - "uuid": "^11.0.3", - "zone.js": "^0.15.1", - "zrender": "5.4.3" + "jspdf": "^4.2.1", + "jspdf-autotable": "^5.0.9", + "jszip": "^3.10.2", + "ngx-echarts": "^22.0.0", + "rxjs": "~7.8.2", + "tslib": "^2.8.1" }, "devDependencies": { - "@angular-devkit/build-angular": "^21.1.3", - "@angular-devkit/core": "^21.1.3", - "@angular-devkit/schematics": "^21.1.3", - "@angular/cli": "^21.1.3", - "@angular/compiler-cli": "^21.1.3", + "@angular/build": "22.2.4", + "@angular/cli": "22.2.4", + "@angular/compiler-cli": "^22.2.3", + "@playwright/test": "^1.64.1", "@types/d3": "^7.4.3", - "@types/jasmine": "~5.1.0", - "@types/jquery": "^3.5.32", - "@types/jspdf": "^1.3.3", - "baseline-browser-mapping": "^2.9.19", - "jasmine-core": "~5.2.0", - "karma": "~6.4.0", - "karma-chrome-launcher": "~3.2.0", - "karma-coverage": "~2.2.0", - "karma-jasmine": "~5.1.0", - "karma-jasmine-html-reporter": "~2.1.0", - "typescript": "~5.9.3" + "@vitest/browser-playwright": "5.0.5", + "typescript": "6.0.3", + "vitest": "5.0.5" + }, + "overrides": { + "vite": "8.3.5" } } diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 000000000..aac791683 --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,33 @@ +import { defineConfig } from '@playwright/test'; + +/** + * Headless e2e flows. The Angular dev server is the only process; + * backend calls are intercepted in the browser (see e2e/experiment-studio-api.ts). + * Every test owns its page and its mock store, so tests run in parallel. + */ +export default defineConfig({ + testDir: './e2e', + fullyParallel: true, + timeout: 60_000, + expect: { timeout: 20_000 }, + outputDir: 'e2e/test-results', + reporter: 'list', + use: { + baseURL: 'http://127.0.0.1:4200', + headless: true, + // Local machines use installed Chrome. CI uses Playwright's Chromium. + ...(process.env.CI ? {} : { channel: 'chrome' as const }), + viewport: { width: 1440, height: 900 }, + actionTimeout: 15_000, + // Kept only for failed tests; CI uploads e2e/test-results as an artifact. + trace: 'retain-on-failure', + screenshot: 'only-on-failure', + }, + webServer: { + // No hot reload: under parallel first loads its component endpoint can fail and leave a blank page. + command: 'node node_modules/@angular/cli/bin/ng.js serve --host 127.0.0.1 --port 4200 --no-hmr --no-live-reload', + url: 'http://127.0.0.1:4200', + reuseExistingServer: !process.env.CI, + timeout: 180_000, + }, +}); diff --git a/public/favicon.ico b/public/favicon.ico index 57614f9c9..74b5c218e 100644 Binary files a/public/favicon.ico and b/public/favicon.ico differ diff --git a/renovate.json b/renovate.json new file mode 100644 index 000000000..5a33d4e19 --- /dev/null +++ b/renovate.json @@ -0,0 +1,71 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "customManagers": [ + { + "customType": "regex", + "description": "Bump tool versions and their pinned release-asset SHA256 in CI setup scripts", + "managerFilePatterns": [ + "/(^|/)setup-tools\\.sh$/" + ], + "matchStrings": [ + "# renovate: datasource=(?<datasource>[a-z-]+) depName=(?<depName>[^\\s]+)\\n[A-Z_]+_VERSION=\"\\$\\{[A-Z_]+_VERSION:-(?<currentValue>v[0-9][0-9.]*)\\}\"\\n[A-Z_]+_SHA256=\"\\$\\{[A-Z_]+_SHA256:-(?<currentDigest>[a-f0-9]{64})\\}\"", + "# renovate: datasource=(?<datasource>npm) depName=(?<depName>[^\\s]+)\\n[A-Z_]+_VERSION=\"\\$\\{[A-Z_]+_VERSION:-(?<currentValue>[0-9][0-9.]*)\\}\"" + ] + }, + { + "customType": "regex", + "description": "Bump python-version pinned in workflow files", + "managerFilePatterns": [ + "/^\\.github/workflows/.+\\.ya?ml$/" + ], + "matchStrings": [ + "# renovate: datasource=(?<datasource>[a-z-]+) depName=(?<depName>[^\\s]+)\\n\\s*python-version:\\s*'(?<currentValue>[0-9][0-9.]*)'" + ] + }, + { + "currentValueTemplate": "develop", + "customType": "regex", + "description": "Track a git branch head pinned as a commit SHA in CI setup scripts", + "managerFilePatterns": [ + "/(^|/)setup-tools\\.sh$/" + ], + "matchStrings": [ + "# renovate: datasource=(?<datasource>git-refs) depName=(?<depName>[^\\s]+)\\n[A-Z_]+_REF=\"\\$\\{[A-Z_]+_REF:-(?<currentDigest>[a-f0-9]{40})\\}\"" + ] + } + ], + "enabledManagers": [ + "custom.regex", + "pre-commit" + ], + "extends": [ + "config:recommended" + ], + "labels": [ + "dependencies" + ], + "packageRules": [ + { + "description": "Never update a checksum for an existing version: a re-uploaded release asset must fail CI, not be auto-repinned", + "enabled": false, + "matchDatasources": [ + "github-release-attachments" + ], + "matchUpdateTypes": [ + "digest" + ] + }, + { + "description": "Cooldown: only propose releases at least 7 days old, so yanked or compromised releases never reach a PR", + "matchDatasources": [ + "github-release-attachments", + "github-tags", + "github-releases", + "npm", + "python-version", + "git-refs" + ], + "minimumReleaseAge": "7 days" + } + ] +} diff --git a/scripts/check-dead-code.mjs b/scripts/check-dead-code.mjs new file mode 100644 index 000000000..23b6723d8 --- /dev/null +++ b/scripts/check-dead-code.mjs @@ -0,0 +1,218 @@ +#!/usr/bin/env node +/** + * Dead-code check for fl-platform. Four questions, all answered statically: + * + * 1. dead exports — a module-level `export` nothing outside its own file uses. + * 2. orphan CSS — a class selector no template can put on an element. + * 3. orphan keyframes — an `@keyframes` block no `animation` declaration names. + * 4. orphan tokens — a `:root` custom property nothing reads with `var()`. + * + * Component stylesheets are checked against their OWN template, because Angular's + * default emulated encapsulation means that is the only markup they can match; a class + * another component uses is not evidence this copy is live. Two exceptions: `:host` / + * `:host-context` selectors (their classes come from ancestors) and sheets pulled in + * with `@import`, which ship inside the importer and match the importer's template. + * `@keyframes` get the same scoping, because Angular renames them per component. + * + * Run: npm run check:dead-code (exit 1 on findings) + * + * A finding is a bug in the code or in the allowlist below, never a reason to delete a + * class Angular applies at runtime. Runtime classes are recognized by prefix; when one + * shows up that the prefix list misses, add the prefix here. + */ +import { readFileSync, readdirSync, statSync, existsSync } from 'node:fs'; +import { join, relative, basename, dirname, extname, resolve } from 'node:path'; + +const ROOT = process.cwd(); +const RUNTIME_CLASS_PREFIXES = ['ng-', 'cdk-', 'mat-', 'mdc-', 'ql-', 'wq-', 'ngx-', 'fa-', 'fa', 'swiper-']; + +const walk = (dir, out = []) => { + for (const entry of readdirSync(dir)) { + const p = join(dir, entry); + if (statSync(p).isDirectory()) walk(p, out); + else out.push(p); + } + return out; +}; +const files = walk(join(ROOT, 'src')); +const textCache = new Map(); +const read = (p) => { + if (!textCache.has(p)) textCache.set(p, readFileSync(p, 'utf8')); + return textCache.get(p); +}; +const rel = (p) => relative(ROOT, p); +const ts = files.filter((p) => extname(p) === '.ts'); +const html = files.filter((p) => extname(p) === '.html'); +const css = files.filter((p) => extname(p) === '.css'); +const appMarkup = [...ts, ...html].map(read); +const globalHay = appMarkup.join('\n'); + +const isWordChar = (value) => value !== undefined && /[A-Za-z0-9_$]/.test(value); +const countIn = (haystack, name) => { + if (!name) return 0; + let count = 0; + let from = 0; + while ((from = haystack.indexOf(name, from)) !== -1) { + const before = haystack[from - 1]; + const after = haystack[from + name.length]; + if (!isWordChar(before) && !isWordChar(after)) count += 1; + from += name.length; + } + return count; +}; + +/* 1 ─ dead exports ─────────────────────────────────────────────────────────── */ +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) { + const text = read(p); + for (const line of text.split('\n')) { + const m = DECL.exec(line); + if (!m) continue; + const [, kind, name] = m; + const used = appSources.some((q) => q !== p && countIn(read(q), name)) + || html.some((q) => countIn(read(q), name)); + const selfUses = countIn(text, name) - 1; + if (!used && selfUses <= 0) deadExports.push(`${rel(p)} ${kind} ${name} (no reference anywhere)`); + } +} + +/* 2 ─ orphan CSS ───────────────────────────────────────────────────────────── */ +/** + * Class names a sheet depends on, split by who can put them on an element: the component + * itself (own template) or an ancestor (`:host` / `:host-context`, matched against the app). + */ +const classDecls = (sheet) => { + const text = sheet + .replace(/\/\*[\s\S]*?\*\//g, '') + .replace(/url\([^)]*\)/g, ' url() ') + .replace(/"[^"]*"|'[^']*'/g, ' '); + const own = new Set(); + const hostScoped = new Set(); + for (const m of text.matchAll(/([^{}]+)\{/g)) { + const selector = m[1]; + const names = [...selector.matchAll(/\.([A-Za-z_][\w-]*)/g)].map((x) => x[1]); + const target = /:host\b/.test(selector) ? hostScoped : own; + names.forEach((n) => target.add(n)); + } + return { own, hostScoped }; +}; +const templateFor = (p) => { + const t = join(dirname(p), basename(p, '.ts') + '.html'); + return files.includes(t) ? read(t) : ''; +}; +const globalSheets = css.filter((p) => !p.endsWith('.component.css')); + +/** Markup one sheet can style on its own; a global sheet reaches everywhere, so `null`. */ +const ownMarkup = (p) => p.endsWith('.component.css') + ? [templateFor(p.replace('.component.css', '.component.ts')), ...walk(join(ROOT, dirname(rel(p)))).filter((q) => q.endsWith('.ts')).map(read)].join('\n') + : null; + +/** `@import` edges: an imported sheet also matches the importing component's markup. */ +const importers = new Map(); +for (const p of css) { + for (const m of read(p).matchAll(/@import\s+['"]([^'"]+)['"]/g)) { + const base = resolve(dirname(p), m[1]); + const target = existsSync(base) ? base : existsSync(`${base}.css`) ? `${base}.css` : null; + if (!target || target === p) continue; + if (!importers.has(target)) importers.set(target, new Set()); + importers.get(target).add(p); + } +} +/** + * Markup a sheet can style: its own, plus the markup of everything that imports it, as far + * up the chain as it goes — an imported sheet ships inside its importer, so it matches that + * importer's template too. A global sheet anywhere on the way reaches the whole app, which + * makes everything reachable, so the answer is `null` there as well. + */ +const reachableMarkup = (p, seen = new Set()) => { + if (seen.has(p)) return []; + seen.add(p); + const own = ownMarkup(p); + if (own === null) return null; + const parts = [own]; + for (const importer of importers.get(p) ?? []) { + const upstream = reachableMarkup(importer, seen); + if (upstream === null) return null; + parts.push(...upstream); + } + return parts; +}; + +const orphanCss = []; +for (const p of css) { + const reach = reachableMarkup(p); + const own = reach ? reach.join('\n') : null; + const { own: ownClasses, hostScoped } = classDecls(read(p)); + const isDead = (c, hay) => + !RUNTIME_CLASS_PREFIXES.some((pre) => c.startsWith(pre)) && !hay.includes(c); + const dead = [...ownClasses].filter((c) => isDead(c, own ?? globalHay)) + .concat([...hostScoped].filter((c) => isDead(c, globalHay))); + if (dead.length) orphanCss.push(`${rel(p)} ${dead.length} orphan class(es): ${dead.slice(0, 12).join(', ')}${dead.length > 12 ? ', …' : ''}`); +} + +/* 3 ─ orphan keyframes ─────────────────────────────────────────────────────── */ +/** Names an `animation` / `animation-name` declaration could refer to, per sheet. */ +const ANIMATION_KEYWORDS = new Set(['none', 'infinite', 'alternate', 'alternate-reverse', 'reverse', 'normal', + 'backwards', 'forwards', 'both', 'running', 'paused', 'ease', 'ease-in', 'ease-out', 'ease-in-out', 'linear', + 'step-start', 'step-end', 'initial', 'inherit', 'unset', 'revert', 'auto']); +const animationRefs = (sheet) => { + const names = new Set(); + let unverifiable = false; + for (const m of sheet.replace(/\/\*[\s\S]*?\*\//g, '').matchAll(/animation(?:-name)?\s*:\s*([^;}]+)/g)) { + for (const part of m[1].split(',')) { + if (part.includes('var(')) unverifiable = true; // name comes from a custom property — cannot resolve statically + part.split(/\s+/).forEach((tok) => { + if (/^[A-Za-z_][\w-]*$/.test(tok) && !ANIMATION_KEYWORDS.has(tok)) names.add(tok); + }); + } + } + return { names, unverifiable }; +}; +const orphanKeyframes = []; +for (const p of css) { + const sheet = read(p); + const { names, unverifiable } = animationRefs(sheet); + if (unverifiable) continue; + const own = ownMarkup(p) ?? globalHay; + const defs = [...sheet.replace(/\/\*[\s\S]*?\*\//g, '').matchAll(/@keyframes\s+([A-Za-z_][\w-]*)/g)].map((m) => m[1]); + const dead = defs.filter((n) => !names.has(n) && !own.includes(n)); + if (dead.length) orphanKeyframes.push(`${rel(p)} ${dead.length} unreferenced @keyframes: ${dead.join(', ')}`); +} + +/* 4 ─ orphan design tokens ─────────────────────────────────────────────────── */ +/** + * Global `:root` custom properties, matched against every `var(…)` in the app. Skipped + * entirely if any code composes a property name at runtime (`style.setProperty` / a + * template literal inside `var(`), since then no static answer is honest. + */ +const rootBlock = (sheet) => + [...sheet.replace(/\/\*[\s\S]*?\*\//g, '').matchAll(/:root\s*\{([^}]*)\}/g)].map((m) => m[1]); +const orphanTokens = []; +if (![...ts, ...html, ...css].some((p) => /setProperty\(|var\(\s*--[^)\n]*\$\{/.test(read(p)))) { + const varSite = [...appMarkup, ...css.map(read)].join('\n'); + for (const p of globalSheets) { + for (const block of rootBlock(read(p))) { + for (const d of block.matchAll(/(^|[\s;{])(--[a-z][\w-]*)\s*:/g)) { + const name = d[2]; + if (!new RegExp(`var\\(\\s*${name}(?![\\w-])`).test(varSite)) { + orphanTokens.push(`${rel(p)} ${name} declared in :root but never read with var()`); + } + } + } + } +} + +/* report ───────────────────────────────────────────────────────────────────── */ +const show = (title, rows) => { + console.log(`\n${title}: ${rows.length}`); + rows.forEach((r) => console.log(' ' + r)); +}; +show('dead exports', deadExports); +show('orphan CSS', orphanCss); +show('orphan keyframes', orphanKeyframes); +show('orphan design tokens', orphanTokens); +const total = deadExports.length + orphanCss.length + orphanKeyframes.length + orphanTokens.length; +console.log(`\n${total === 0 ? 'OK — no dead code found' : total + ' finding(s)'}`); +process.exit(total ? 1 : 0); diff --git a/scripts/check-node-version.mjs b/scripts/check-node-version.mjs new file mode 100644 index 000000000..f7c65ff2a --- /dev/null +++ b/scripts/check-node-version.mjs @@ -0,0 +1,19 @@ +const major = Number.parseInt(process.versions.node.split('.')[0] ?? '0', 10); +const required = 22; + +if (major < required) { + console.error(''); + console.error(`platform-ui requires Node.js >= ${required}. You are running ${process.version}.`); + console.error(''); + console.error('Angular 22 requires Node.js 22.22.3+, 24.15.0+, or 26+.'); + console.error(''); + console.error('Fix:'); + console.error(' nvm install 22'); + console.error(' nvm use 22'); + console.error(' node -v'); + console.error(' npm start'); + console.error(''); + console.error('Use npm start (local CLI), not a global ng binary on an unsupported Node.'); + console.error(''); + process.exit(1); +} diff --git a/src/app/app.component.css b/src/app/app.component.css index 4c00175bb..862c3b663 100644 --- a/src/app/app.component.css +++ b/src/app/app.component.css @@ -4,6 +4,33 @@ min-height: 100vh; } +.skip-link { + position: fixed; + top: 0; + left: 50%; + transform: translate(-50%, -120%); + z-index: 10001; + padding: 8px 16px; + border-radius: 0 0 var(--radius-md) var(--radius-md); + background: var(--primary-color); + color: #fff; + font-size: 0.85rem; + font-weight: 600; + text-decoration: none; + transition: transform 0.15s ease; +} + +.skip-link:focus-visible { + transform: translate(-50%, 0); + outline: 2px solid var(--text-main); + outline-offset: 2px; +} + +/* + * The shell is the only place that knows the chrome exists: app-header is sticky and + * takes its own height out of this column, so `main` starts below it and no page adds a + * header offset. Pages fill the leftover with flex, not with `100vh - <magic px>`. + */ main { flex: 1 1 auto; padding: 0; @@ -12,13 +39,13 @@ main { min-height: 0; } -/* app-header and app-footer are components and will handle their own styles */ -app-header { - position: sticky; - top: 0; - z-index: 1000; +/* Notebook wants its own scroll height instead of the leftover, because the iframe + manages the rest. No padding-top: the sticky bar is already above this box. */ +main.main--notebook { + flex: 0 0 auto; } +/* app-header and app-footer are components and will handle their own styles. */ app-footer { margin-top: auto; flex-shrink: 0; diff --git a/src/app/app.component.html b/src/app/app.component.html index 4b12bfe12..a2712e227 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,7 +1,9 @@ +<a class="skip-link" href="#main-content">Skip to content</a> + <app-header></app-header> <!-- Route-specific content loaded here --> -<main> +<main id="main-content" [class.main--notebook]="notebookRouteActive()"> <router-outlet></router-outlet> </main> diff --git a/src/app/app.component.ts b/src/app/app.component.ts index e6a26e969..8e7991a28 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,21 +1,38 @@ import { FooterComponent } from './pages/shared/footer/footer.component'; -import { Component, inject } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core'; +import { toSignal } from '@angular/core/rxjs-interop'; import { HeaderComponent } from './pages/shared/header/header.component'; -import { RouterModule } from '@angular/router'; +import { NavigationEnd, Router, RouterModule } from '@angular/router'; import { AuthService } from './services/auth.service'; +import { isRoutePath } from './core/route-path.utils'; +import { filter, map, startWith } from 'rxjs'; @Component({ selector: 'app-root', imports: [HeaderComponent, FooterComponent, RouterModule], templateUrl: './app.component.html', - styleUrls: ['./app.component.css'] + styleUrl: './app.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class AppComponent { +export class AppComponent implements OnInit { authService = inject(AuthService); - title = 'fl-platform'; + private router = inject(Router); + + readonly notebookRouteActive = toSignal( + this.router.events.pipe( + filter((event): event is NavigationEnd => event instanceof NavigationEnd), + map(() => this.isNotebookRoute()), + startWith(this.isNotebookRoute()), + ), + { initialValue: this.isNotebookRoute() }, + ); ngOnInit() { this.authService.initialize(); } + private isNotebookRoute(): boolean { + return isRoutePath(this.router.url, '/notebook'); + } + } diff --git a/src/app/app.config.ts b/src/app/app.config.ts index d7a353a41..114725ec5 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,4 +1,4 @@ -import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; +import { ApplicationConfig, provideZonelessChangeDetection } from '@angular/core'; import { provideRouter, withInMemoryScrolling } from '@angular/router'; import { provideHttpClient, withInterceptors, withXsrfConfiguration } from '@angular/common/http'; @@ -8,7 +8,7 @@ import { provideEchartsCore } from 'ngx-echarts'; export const appConfig: ApplicationConfig = { providers: [ - provideZoneChangeDetection({ eventCoalescing: true }), + provideZonelessChangeDetection(), provideRouter( appRoutes, withInMemoryScrolling({ diff --git a/src/app/app.routes.spec.ts b/src/app/app.routes.spec.ts new file mode 100644 index 000000000..f962de1b0 --- /dev/null +++ b/src/app/app.routes.spec.ts @@ -0,0 +1,11 @@ +import { AuthGuard } from './guards/auth.guard'; +import { TermsGuard } from './guards/terms.guard'; +import { appRoutes } from './app.routes'; + +describe('appRoutes', () => { + it('lets authenticated users open the dashboard without a studio-guide gate', () => { + const dashboard = appRoutes.find((route) => route.path === 'experiments-dashboard'); + + expect(dashboard?.canActivate).toEqual([AuthGuard, TermsGuard]); + }); +}); diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index c62a683cd..816a2e79f 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,27 +1,11 @@ +import { inject } from '@angular/core'; import { CanMatchFn, Routes } from '@angular/router'; import { AuthGuard } from './guards/auth.guard'; import { TermsGuard } from './guards/terms.guard'; - -const parseBool = (value: unknown, defaultValue: boolean): boolean => { - if (value === undefined || value === null) { - return defaultValue; - } - if (typeof value === 'boolean') { - return value; - } - const normalized = String(value).trim().toLowerCase(); - if (normalized === '1' || normalized === 'true' || normalized === 'yes' || normalized === 'on') { - return true; - } - if (normalized === '0' || normalized === 'false' || normalized === 'no' || normalized === 'off') { - return false; - } - return defaultValue; -}; +import { RuntimeEnvService } from './services/runtime-env.service'; const notebookEnabledMatch: CanMatchFn = () => { - const runtimeEnv = (window as any).__env || {}; - return parseBool(runtimeEnv.NOTEBOOK_ENABLED, false); + return inject(RuntimeEnvService).notebookEnabled; }; export const appRoutes: Routes = [ @@ -35,11 +19,6 @@ export const appRoutes: Routes = [ redirectTo: 'experiments-dashboard', pathMatch: 'full' }, - { - path: 'home', - redirectTo: 'experiments-dashboard', - pathMatch: 'full' - }, { path: 'terms', loadComponent: () => import('./pages/terms-page/terms-page.component').then(m => m.TermsPageComponent), @@ -59,7 +38,7 @@ export const appRoutes: Routes = [ path: 'notebook', loadComponent: () => import('./pages/notebook/notebook.component').then(m => m.NotebookComponent), canMatch: [notebookEnabledMatch], - canActivate: [AuthGuard], + canActivate: [AuthGuard, TermsGuard], }, { path: '**', redirectTo: 'experiments-dashboard' } ]; diff --git a/src/app/core/algorithm-input-counts.spec.ts b/src/app/core/algorithm-input-counts.spec.ts new file mode 100644 index 000000000..dcf037c8e --- /dev/null +++ b/src/app/core/algorithm-input-counts.spec.ts @@ -0,0 +1,36 @@ +import { + applyFallbackInputCounts, + formatInputCountRange, + readInputCount, + resolveInputMaxCount, + resolveInputMinCount, +} from './algorithm-input-counts'; + +describe('algorithm input counts', () => { + it('reads snake_case and camelCase count fields', () => { + expect(readInputCount({ min_count: 2 }, 'min_count')).toBe(2); + expect(readInputCount({ minCount: 3 }, 'min_count')).toBe(3); + expect(readInputCount({ max_count: '1' }, 'max_count')).toBe(1); + }); + + it('applies Binary GLMM fallback bounds when counts are missing', () => { + const inputdata = applyFallbackInputCounts({ + y: { label: 'Y', desc: '', types: ['int'], required: true }, + x: { label: 'X', desc: '', types: ['real'], required: true }, + }, 'glmm_binary'); + + expect(inputdata['y']).toEqual(expect.objectContaining({ max_count: 1 })); + expect(inputdata['x']).toEqual(expect.objectContaining({ min_count: 2 })); + }); + + it('resolves Binary GLMM availability counts', () => { + const y = { required: true }; + const x = { required: true }; + + expect(resolveInputMaxCount(y, 'y', 'glmm_binary')).toBe(1); + expect(resolveInputMinCount(x, 'x', 'glmm_binary')).toBe(2); + expect(formatInputCountRange(1, 1)).toBe('exactly 1'); + expect(formatInputCountRange(2, null)).toBe('2+'); + expect(formatInputCountRange(2, 2)).toBe('exactly 2'); + }); +}); diff --git a/src/app/core/algorithm-input-counts.ts b/src/app/core/algorithm-input-counts.ts new file mode 100644 index 000000000..eb9836346 --- /dev/null +++ b/src/app/core/algorithm-input-counts.ts @@ -0,0 +1,102 @@ +type AlgorithmInputCountRole = 'y' | 'x'; + +type CountBounds = { min?: number; max?: number }; + +/** Fallback bounds when the catalog omits min_count/max_count (kept in sync with Exaflow specs). */ +const FALLBACK_INPUT_COUNT_BOUNDS: Record<string, Partial<Record<AlgorithmInputCountRole, CountBounds>>> = { + linear_regression: { y: { max: 1 } }, + linear_regression_cv: { y: { max: 1 } }, + cox_regression_classical: { y: { max: 1 }, x: { min: 2 } }, + cox_regression_stacked: { y: { max: 1 }, x: { min: 2 } }, + lmm: { y: { max: 1 }, x: { min: 2 } }, + glmm_binary: { y: { max: 1 }, x: { min: 2 } }, + glmm_ordinal: { y: { max: 1 }, x: { min: 2 } }, + anova_twoway: { y: { max: 1 }, x: { min: 2, max: 2 } }, +}; + +function normalizeInputCount(value: number | string | undefined | null): number | null { + if (value === undefined || value === null || value === '') return null; + const parsed = Number(value); + return Number.isFinite(parsed) ? parsed : null; +} + +export function readInputCount(field: unknown, key: 'min_count' | 'max_count'): number | null { + if (!field || typeof field !== 'object') return null; + const record = field as Record<string, unknown>; + const camelKey = key === 'min_count' ? 'minCount' : 'maxCount'; + return normalizeInputCount(record[key] as number | string | undefined | null ?? record[camelKey] as number | string | undefined | null); +} + +function isInputFieldRequired(field: unknown): boolean { + if (!field || typeof field !== 'object') return false; + const required = (field as Record<string, unknown>)['required']; + if (typeof required === 'boolean') return required; + if (required === undefined || required === null) return false; + const normalized = String(required).trim().toLowerCase(); + return normalized === 'true' || normalized === '1'; +} + +export function resolveInputMinCount( + field: unknown, + role: AlgorithmInputCountRole, + algorithmName?: string | null +): number { + const explicit = readInputCount(field, 'min_count'); + if (explicit !== null) return explicit; + + const fallback = algorithmName ? FALLBACK_INPUT_COUNT_BOUNDS[algorithmName]?.[role]?.min : undefined; + if (fallback !== undefined) return fallback; + + return isInputFieldRequired(field) ? 1 : 0; +} + +export function resolveInputMaxCount( + field: unknown, + role: AlgorithmInputCountRole, + algorithmName?: string | null +): number | null { + const explicit = readInputCount(field, 'max_count'); + if (explicit !== null) return explicit; + + const fallback = algorithmName ? FALLBACK_INPUT_COUNT_BOUNDS[algorithmName]?.[role]?.max : undefined; + if (fallback !== undefined) return fallback; + + return null; +} + + +export function formatInputCountRange(minCount: number, maxCount: number | null): string { + if (minCount === 1 && maxCount === 1) return 'exactly 1'; + if (minCount === 0 && maxCount === 1) return '0-1'; + if (minCount > 0 && maxCount !== null && minCount === maxCount) return `exactly ${minCount}`; + if (minCount > 0 && maxCount !== null) return `${minCount}-${maxCount}`; + if (minCount > 0) return `${minCount}+`; + if (maxCount !== null) return `0-${maxCount}`; + return 'optional'; +} + +export function applyFallbackInputCounts(inputdata: unknown, algorithmName: string): Record<string, unknown> { + if (!inputdata || typeof inputdata !== 'object') return {}; + const next: Record<string, unknown> = { ...(inputdata as Record<string, unknown>) }; + const bounds = FALLBACK_INPUT_COUNT_BOUNDS[algorithmName]; + if (!bounds) return next; + + (['y', 'x'] as AlgorithmInputCountRole[]).forEach((role) => { + const field = next[role]; + const roleBounds = bounds[role]; + if (!field || typeof field !== 'object' || !roleBounds) return; + + const current = field as Record<string, unknown>; + next[role] = { + ...current, + ...(readInputCount(current, 'min_count') === null && roleBounds.min !== undefined + ? { min_count: roleBounds.min } + : {}), + ...(readInputCount(current, 'max_count') === null && roleBounds.max !== undefined + ? { max_count: roleBounds.max } + : {}), + }; + }); + + return next; +} diff --git a/src/app/core/algorithm-mappers.spec.ts b/src/app/core/algorithm-mappers.spec.ts new file mode 100644 index 000000000..cbdf11658 --- /dev/null +++ b/src/app/core/algorithm-mappers.spec.ts @@ -0,0 +1,10 @@ +import { getOutputSchema } from './algorithm-mappers'; +describe('algorithm mappers', () => { + it('provides output schemas for new Exaflow algorithms', () => { + ['lmm', 'glmm_binary', 'glmm_ordinal', 'chi_squared', 'fisher_exact', 'outlier_report', 'quartiles', 'binned_mann_whitney_u_test'].forEach((name) => { + const schema = getOutputSchema(name); + expect(schema).toBeTruthy(); + expect(schema?.length).toBeGreaterThan(0); + }); + }); +}); diff --git a/src/app/core/algorithm-mappers.ts b/src/app/core/algorithm-mappers.ts index 93bc6006a..060d4010f 100644 --- a/src/app/core/algorithm-mappers.ts +++ b/src/app/core/algorithm-mappers.ts @@ -1,67 +1,143 @@ -import { RawAlgorithmDefinition, RawParameter } from '../models/backend-algorithms.model'; +import { + AlgorithmSpecification, + AnalysisInputDataSpecification, + PreprocessingStepSpecification, + RawInputData, + RawParameter, +} from '../models/backend-algorithms.model'; import { AlgorithmConfig } from '../models/algorithm-definition.model'; +import { applyFallbackInputCounts, readInputCount } from './algorithm-input-counts'; // Lookup for categories const CATEGORY_MAPPING: Record<string, string> = { "pearson_correlation": "Correlation", "anova_oneway": "Statistical Tests", + "binned_mann_whitney_u_test": "Statistical Tests", + "standardized_mean_difference": "Statistical Tests", "ttest_independent": "Statistical Tests", "ttest_onesample": "Statistical Tests", "ttest_paired": "Statistical Tests", "linear_regression": "Regression", "logistic_regression": "Regression", + "cox_regression_classical": "Regression", + "cox_regression_stacked": "Regression", + "lmm": "Mixed Models", + "glmm_binary": "Mixed Models", + "glmm_ordinal": "Mixed Models", "naive_bayes_categorical": "Classification", "naive_bayes_gaussian": "Classification", "kmeans": "Clustering", "pca": "Dimensionality Reduction", - "pca_with_transformation": "Dimensionality Reduction", "linear_regression_cv": "Regression", "logistic_regression_cv": "Regression", - "logistic_regression_cv_fedaverage": "Regression", "naive_bayes_gaussian_cv": "Classification", "naive_bayes_categorical_cv": "Classification", "anova_twoway": "Statistical Tests", + "chi_squared": "Statistical Tests", + "fisher_exact": "Statistical Tests", "describe": "Descriptive Statistics", "histogram": "Descriptive Statistics", + "quartiles": "Descriptive Statistics", + "outlier_report": "Descriptive Statistics", "linear_svm": "Classification", "longitudinal_transformer": "Transformers", }; -function guessVariableType(ioField?: { types?: string[] }): string { - if (!ioField) return "None"; - if (ioField?.types?.includes('int')) return "Numerical"; - if (ioField?.types?.includes('real')) return "Numerical"; - if (ioField?.types?.includes('text')) return "Nominal"; - return "Any"; +function normalizeBool(value: boolean | string | undefined | null): boolean { + if (typeof value === 'boolean') return value; + return String(value ?? '').trim().toLowerCase() === 'true'; } -function buildConfigSchema(parameters: Record<string, RawParameter>): Array<any> { - const schema = []; +function enumSourceToArray(source: string[] | string | undefined): string[] { + if (Array.isArray(source)) return source; + if (source === undefined || source === null) return []; + return [source]; +} + +function normalizeCount(value: number | string | undefined | null): number | undefined { + if (value === undefined || value === null || value === '') return undefined; + const parsed = Number(value); + return Number.isFinite(parsed) ? parsed : undefined; +} + +function normalizeInputField(field: any): any { + if (!field) return field; + const minCount = readInputCount(field, 'min_count'); + const maxCount = readInputCount(field, 'max_count'); + + return { + ...field, + ...(minCount !== null ? { min_count: minCount } : {}), + ...(maxCount !== null ? { max_count: maxCount } : {}), + }; +} + +function normalizeInputData(inputdata: any): any { + if (!inputdata) return {}; + return Object.fromEntries( + Object.entries(inputdata).map(([key, field]) => [key, normalizeInputField(field)]) + ); +} + +function buildConfigSchema(parameters?: Record<string, RawParameter> | null): Array<any> { + const schema: any[] = []; + if (!parameters) return schema; for (const [key, param] of Object.entries(parameters)) { + const enumType = param.enums?.type; + const enumSource = enumSourceToArray(param.enums?.source); + const dictKeyEnumType = param.dict_keys_enums?.type; + const dictKeyEnumSource = enumSourceToArray(param.dict_keys_enums?.source); + const dictValueEnumType = param.dict_values_enums?.type; + const dictValueOptions = enumSourceToArray(param.dict_values_enums?.source); + const isMultiple = normalizeBool(param.multiple); + const min = normalizeCount(param.min); + const max = normalizeCount(param.max); const baseField: any = { key, label: param.label ?? key, desc: param.desc ?? '', - required: param.required ?? false, - multiple: param.multiple ?? false, + required: normalizeBool(param.required), + multiple: isMultiple, types: param.types ?? [], stattypes: param.stattypes ?? [], - ...(param.min !== undefined ? { min: +param.min } : {}), - ...(param.max !== undefined ? { max: +param.max } : {}), + ...(enumType ? { enumType } : {}), + ...(enumSource.length ? { enumSource } : {}), + ...(min !== undefined ? { min } : {}), + ...(max !== undefined ? { max } : {}), ...(param.default !== undefined ? { default: param.default } : {}), + ...(param.default === undefined && param.default_value !== undefined ? { default: param.default_value } : {}), }; + // --- Dictionary fields --- + if (param.types?.includes('dict')) { + schema.push({ + ...baseField, + type: 'dict', + ...(dictKeyEnumType ? { dictKeyEnumType } : {}), + ...(dictKeyEnumSource.length ? { dictKeyEnumSource } : {}), + ...(dictValueEnumType ? { dictValueEnumType } : {}), + ...(dictValueOptions.length ? { dictValueOptions } : {}), + ...(param.dict_values_type ? { dictValueType: param.dict_values_type } : {}), + }); + continue; + } + // --- Select fields --- if (param.enums) { schema.push({ ...baseField, - type: 'select', - options: Array.isArray(param.enums?.source) - ? param.enums.source - : Array.isArray(param.enums) - ? param.enums - : [], + type: isMultiple ? 'multi-select' : 'select', + options: enumType === 'list' ? enumSource : [], + }); + continue; + } + + if (isMultiple && (param.types?.includes('text') || param.types?.includes('int'))) { + schema.push({ + ...baseField, + type: 'multi-select', + options: [], }); continue; } @@ -94,23 +170,167 @@ function buildConfigSchema(parameters: Record<string, RawParameter>): Array<any> return schema; } -export function mapRawAlgorithmToAlgorithmConfig(raw: RawAlgorithmDefinition): AlgorithmConfig { - const normalizedName = raw.name === 'anova' ? 'anova_twoway' : raw.name; - return { - name: normalizedName, - label: raw.label, - description: raw.desc ?? '', - inputdata: raw.inputdata ?? {}, - requiredVariable: raw.inputdata?.y?.types || [], - covariate: raw.inputdata?.x?.types || [], - category: CATEGORY_MAPPING[normalizedName] ?? 'Uncategorized', - configSchema: buildConfigSchema(raw.parameters ?? {}), - isDisabled: false, - ...(getOutputSchema(normalizedName) ? { outputSchema: getOutputSchema(normalizedName) } : {}), - }; +type DocumentationParameter = Pick<RawParameter, 'dict_values_enums' | 'enums' | 'label'>; + +const quotedCodeTokenPattern = /(['"`])([^'"`\n]+)\1/g; + +const specialDocumentationLabels: Record<string, string> = { + aic: 'AIC', + bic: 'BIC', + glmm: 'GLMM', + iqr: 'IQR', + mad: 'MAD', + ols: 'OLS', + pca: 'PCA', + svm: 'SVM', +}; + +function normalizeDocumentationToken(token: string): string { + return token.trim(); } +function humanizeDocumentationToken(token: string): string { + const normalized = normalizeDocumentationToken(token); + const lower = normalized.toLowerCase(); + const specialLabel = specialDocumentationLabels[lower]; + + if (specialLabel) { + return specialLabel; + } + + const spaced = normalized.replace(/_+/g, ' ').toLowerCase(); + + return spaced.charAt(0).toUpperCase() + spaced.slice(1); +} + +function isFallbackCodeToken(token: string): boolean { + const normalized = normalizeDocumentationToken(token); + + return /^[A-Za-z][A-Za-z0-9_-]*$/.test(normalized) + && ( + normalized.includes('_') + || normalized.includes('-') + || /\d/.test(normalized) + || /[a-z][A-Z]/.test(normalized) + ); +} + +function addDocumentationEnumLabels(labelMap: Record<string, string>, enumDefinition?: DocumentationParameter['enums']): void { + if (!enumDefinition || enumDefinition.type !== 'list') { + return; + } + + enumSourceToArray(enumDefinition.source).forEach((value) => { + const token = normalizeDocumentationToken(value); + + if (token) { + labelMap[token] = humanizeDocumentationToken(token); + } + }); +} + +function buildDocumentationLabelMap( + parameters?: Record<string, DocumentationParameter | null | undefined> | null, +): Record<string, string> { + const labelMap: Record<string, string> = {}; + + Object.entries(parameters ?? {}).forEach(([key, parameter]) => { + const token = normalizeDocumentationToken(key); + + if (token) { + labelMap[token] = parameter?.label?.trim() || humanizeDocumentationToken(token); + } + + addDocumentationEnumLabels(labelMap, parameter?.enums); + addDocumentationEnumLabels(labelMap, parameter?.dict_values_enums); + }); + + return labelMap; +} + +function sanitizeDocumentation(documentation: string, labelMap: Record<string, string>): string { + return documentation.replace(quotedCodeTokenPattern, (_match, quote: string, rawToken: string) => { + const token = normalizeDocumentationToken(rawToken); + const mappedLabel = labelMap[token]; + + if (mappedLabel) { + return mappedLabel; + } + + if (isFallbackCodeToken(token)) { + return humanizeDocumentationToken(token); + } + + return `${quote}${rawToken}${quote}`; + }); +} + +export function mapSpecificationsToAlgorithmConfigs( + inputdataSpec: AnalysisInputDataSpecification, + preprocessingSpecs: PreprocessingStepSpecification[], + algorithmSpecs: AlgorithmSpecification[], +): Record<string, AlgorithmConfig> { + const preprocessing = preprocessingSpecs.map((step) => ({ + name: step.name, + label: step.label, + desc: step.desc, + documentation: sanitizeDocumentation( + step.documentation ?? '', + buildDocumentationLabelMap(step.parameters ?? {}), + ), + parameters: step.parameters ?? {}, + outputs: step.outputs ?? null, + })); + + const sharedInputdata = normalizeInputData({ + data_model: inputdataSpec.data_model, + datasets: inputdataSpec.datasets, + filters: inputdataSpec.filters, + variables: inputdataSpec.variables, + ...(inputdataSpec.validation_datasets + ? { validation_datasets: inputdataSpec.validation_datasets } + : {}), + }); + + const mapped: Record<string, AlgorithmConfig> = {}; + for (const raw of algorithmSpecs) { + const normalizedName = raw.name; + const algorithmDocumentationLabels = buildDocumentationLabelMap(raw.parameters ?? {}); + const inputdata = applyFallbackInputCounts( + { + ...sharedInputdata, + ...(raw.y ? { y: normalizeInputField(raw.y) } : {}), + ...(raw.x ? { x: normalizeInputField(raw.x) } : {}), + }, + normalizedName, + ) as RawInputData; + const outputSchema = getOutputSchema(normalizedName); + + mapped[normalizedName] = { + name: normalizedName, + label: raw.label, + description: raw.desc ?? '', + documentation: sanitizeDocumentation(raw.documentation ?? '', algorithmDocumentationLabels), + type: raw.type, + flags: raw.flags ?? [], + inputdata, + requiredVariable: inputdata?.y?.types || [], + covariate: inputdata?.x?.types || [], + category: CATEGORY_MAPPING[normalizedName] ?? 'Uncategorized', + configSchema: buildConfigSchema(raw.parameters ?? {}), + preprocessing, + requires_validation_datasets: raw.requires_validation_datasets, + required_preprocessing: raw.required_preprocessing ?? [], + isDisabled: false, + ...(outputSchema ? { outputSchema } : {}), + }; + } + + return mapped; +} + + export function getOutputSchema(algorithmName: string): any[] | undefined { switch (algorithmName) { @@ -181,25 +401,46 @@ export function getOutputSchema(algorithmName: string): any[] | undefined { } ]; case 'kmeans': + return [ + { key: 'selected_k', label: 'Selected k', type: 'number' }, + { key: 'n_obs_interval', label: 'Observations', type: 'text' }, + { key: 'clusters', label: 'Clusters', type: 'table' }, + ]; + case 'quartiles': return [ { - key: 'centers', - label: 'Cluster Centers', - type: 'dynamic-table', - getColumnsFrom: 'y', - rowLabelPrefix: 'Cluster' + key: 'quantiles', + label: 'Quartiles', + type: 'table', + columns: [ + { key: 'q', label: 'Quantile', format: 'float' }, + { key: 'value', label: 'Value', format: 'float' }, + { key: 'actual_q', label: 'Actual quantile', format: 'float' }, + ] } ]; + case 'binned_mann_whitney_u_test': + return [ + { key: 'u_stat', label: 'U statistic', type: 'number', format: 'float' }, + { key: 'p_value', label: 'p-value', type: 'number', format: 'pval' }, + { key: 'z_score', label: 'z-score', type: 'number', format: 'float' }, + { key: 'n1', label: 'Group A sample size', type: 'number' }, + { key: 'n2', label: 'Group B sample size', type: 'number' }, + ]; + case 'standardized_mean_difference': + return [ + { key: 'comparisons', label: 'Pairwise Comparisons', type: 'table' }, + ]; case 'linear_regression_cv': return [ { type: 'table', label: 'Cross-Validation Metrics', key: 'cv_metrics', - constructFrom: ['mean_sq_error', 'r_squared', 'mean_abs_error'], + constructFrom: ['root_mean_sq_error', 'r_squared', 'mean_abs_error'], columns: [ { key: 'fold', label: 'Fold' }, - { key: 'mean_sq_error', label: 'MSE' }, + { key: 'root_mean_sq_error', label: 'RMSE' }, { key: 'r_squared', label: 'R²' }, { key: 'mean_abs_error', label: 'MAE' } ] @@ -236,32 +477,91 @@ export function getOutputSchema(algorithmName: string): any[] | undefined { ] } ]; - case 'logistic_regression_cv_fedaverage': + case 'lmm': return [ + { key: 'dependent_var', label: 'Dependent Variable', type: 'string' }, + { key: 'grouping_var', label: 'Grouping Variable', type: 'string' }, + { key: 'n_obs', label: 'Observations', type: 'number' }, + { key: 'n_groups', label: 'Groups', type: 'number' }, + { key: 'df_model', label: 'Degrees of Freedom (Model)', type: 'number' }, + { key: 'df_resid', label: 'Degrees of Freedom (Residual)', type: 'number' }, + { key: 'sigma2', label: 'Residual Variance', type: 'number' }, + { key: 'sigma_u2', label: 'Random Intercept Variance', type: 'number' }, + { key: 'll_reml', label: 'REML Log-Likelihood', type: 'number' }, + { key: 'aic', label: 'AIC', type: 'number' }, + { key: 'bic', label: 'BIC', type: 'number' }, + { key: 'converged', label: 'Converged', type: 'boolean' }, + { key: 'n_iter', label: 'Iterations', type: 'number' }, { - key: 'accuracy', - label: 'Accuracy (per fold)', - type: 'array', - }, - { - key: 'recall', - label: 'Recall (per fold)', - type: 'array', - }, + key: 'coefficients', + label: 'Fixed Effects', + type: 'table', + constructFrom: [ + 'indep_vars', + 'coefficients', + 'std_err', + 't_stats', + 'pvalues', + 'pvalues_display', + 'lower_ci', + 'upper_ci', + ], + columns: [ + { key: 'variable', label: 'Variable' }, + { key: 'coefficient', label: 'Coefficient', format: 'float' }, + { key: 'std_err', label: 'Std. Error', format: 'float' }, + { key: 't_stat', label: 't-statistic', format: 'float' }, + { key: 'pvalue', label: 'P(>|t|)', format: 'string' }, + { key: 'lower_ci', label: 'Lower 95% CI', format: 'float' }, + { key: 'upper_ci', label: 'Upper 95% CI', format: 'float' }, + ] + } + ]; + case 'glmm_binary': + return [ + { key: 'dependent_var', label: 'Dependent Variable', type: 'string' }, + { key: 'grouping_var', label: 'Grouping Variable', type: 'string' }, + { key: 'n_obs', label: 'Observations', type: 'number' }, + { key: 'n_groups', label: 'Groups', type: 'number' }, + { key: 'sigma_u2', label: 'Random Intercept Variance', type: 'number' }, + { key: 'converged', label: 'Converged', type: 'boolean' }, + { key: 'n_iter', label: 'Iterations', type: 'number' }, { - key: 'precision', - label: 'Precision (per fold)', - type: 'array', - }, + key: 'coefficients', + label: 'Fixed Effects', + type: 'table', + constructFrom: ['indep_vars', 'coefficients'], + columns: [ + { key: 'variable', label: 'Variable' }, + { key: 'coefficient', label: 'Coefficient', format: 'float' }, + ] + } + ]; + case 'glmm_ordinal': + return [ + { key: 'dependent_var', label: 'Dependent Variable', type: 'string' }, + { key: 'grouping_var', label: 'Grouping Variable', type: 'string' }, + { key: 'category_order', label: 'Category Order', type: 'array', elementType: 'string' }, + { key: 'n_obs', label: 'Observations', type: 'number' }, + { key: 'n_groups', label: 'Groups', type: 'number' }, + { key: 'sigma_u2', label: 'Random Intercept Variance', type: 'number' }, + { key: 'converged', label: 'Converged', type: 'boolean' }, + { key: 'n_iter', label: 'Iterations', type: 'number' }, { - key: 'fscore', - label: 'F1 Score (per fold)', - type: 'array', + key: 'coefficients', + label: 'Fixed Effects', + type: 'table', + constructFrom: ['indep_vars', 'coefficients'], + columns: [ + { key: 'variable', label: 'Variable' }, + { key: 'coefficient', label: 'Coefficient', format: 'float' }, + ] }, { - key: 'auc', - label: 'AUC (per fold)', + key: 'cutpoints', + label: 'Cutpoints', type: 'array', + elementType: 'number' } ]; case 'logistic_regression_cv': @@ -335,7 +635,6 @@ export function getOutputSchema(algorithmName: string): any[] | undefined { } ]; case 'pca': - case 'pca_with_transformation': return [ { key: 'n_obs', label: 'Observations', type: 'number' }, { @@ -349,6 +648,22 @@ export function getOutputSchema(algorithmName: string): any[] | undefined { label: 'Eigenvectors', type: 'matrix', elementType: 'number' + }, + { + key: 'variables', + label: 'Variables', + type: 'list', + elementType: 'string' + }, + { + key: 'components', + label: 'Components', + type: 'table' + }, + { + key: 'reusable_preprocessing', + label: 'Reusable preprocessing', + type: 'dict' } ]; case 'pearson_correlation': @@ -394,6 +709,50 @@ export function getOutputSchema(algorithmName: string): any[] | undefined { }, { key: 'intercept', label: 'Intercept', type: 'number' } ]; + case 'chi_squared': + return [ + { key: 'chi2', label: 'Chi-Squared Statistic', type: 'number', format: 'float' }, + { key: 'p_value', label: 'p-value', type: 'number', format: 'pval' }, + { key: 'dof', label: 'Degrees of Freedom', type: 'number' }, + { key: 'x_labels', label: 'Factor Categories', type: 'array', elementType: 'string' }, + { key: 'y_labels', label: 'Outcome Categories', type: 'array', elementType: 'string' }, + { + key: 'expected', + label: 'Expected Frequencies', + type: 'matrix', + elementType: 'number', + rowLabelsKey: 'x_labels', + columnLabelsKey: 'y_labels' + } + ]; + case 'fisher_exact': + return [ + { key: 'odds_ratio', label: 'Odds Ratio', type: 'number', format: 'float' }, + { key: 'p_value', label: 'p-value', type: 'number', format: 'pval' }, + { key: 'x_labels', label: 'Factor Categories', type: 'array', elementType: 'string' }, + { key: 'y_labels', label: 'Outcome Categories', type: 'array', elementType: 'string' } + ]; + case 'outlier_report': + return [ + { + key: 'featurewise', + label: 'Outlier Report', + type: 'table', + columns: [ + { key: 'variable', label: 'Variable' }, + { key: 'dataset', label: 'Dataset' }, + { key: 'data.strategy', label: 'Strategy' }, + { key: 'data.tail', label: 'Tail' }, + { key: 'data.fold', label: 'Fold', format: 'float' }, + { key: 'data.lower_bound', label: 'Lower bound', format: 'float' }, + { key: 'data.upper_bound', label: 'Upper bound', format: 'float' }, + { key: 'data.lower_outlier_count', label: 'Lower outliers' }, + { key: 'data.upper_outlier_count', label: 'Upper outliers' }, + { key: 'data.total_outlier_count', label: 'Total outliers' }, + { key: 'data.total_outlier_percentage', label: 'Outlier %', format: 'percent' }, + ] + } + ]; case 'ttest_independent': return [ { key: 'statistic', label: 'Test Statistic (t)', type: 'number' }, @@ -513,3 +872,9 @@ export function getOutputSchema(algorithmName: string): any[] | undefined { return undefined; } } + +/** Turns a snake_case algorithm/field key into a human label ("logistic_regression" → "Logistic Regression"). */ +export function prettifyLabel(label: string): string { + if (!label) return ''; + return label.replace(/_/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase()); +} diff --git a/src/app/core/algorithm-parameter.utils.spec.ts b/src/app/core/algorithm-parameter.utils.spec.ts new file mode 100644 index 000000000..3f7e6b94a --- /dev/null +++ b/src/app/core/algorithm-parameter.utils.spec.ts @@ -0,0 +1,100 @@ +import { + formatAlgorithmParameterValue, + omitEmptyOptionalParameters, + optionBindingValue, + serializeAlgorithmParameterValue, +} from './algorithm-parameter.utils'; + +describe('algorithm-parameter.utils', () => { + it('binds option values as string codes', () => { + expect(optionBindingValue({ code: 1, label: 'yes' })).toBe('1'); + expect(optionBindingValue({ code: 'yes', label: 'Yes' })).toBe('yes'); + }); + + it('serializes select values to string codes using option metadata', () => { + const field = { + key: 'positive_class', + type: 'select', + options: [ + { code: '0', label: 'no' }, + { code: '1', label: 'yes' }, + { code: '9', label: 'unknown' }, + ], + }; + + expect(serializeAlgorithmParameterValue('yes', field)).toBe('1'); + expect(serializeAlgorithmParameterValue(1, field)).toBe('1'); + expect(serializeAlgorithmParameterValue('unknown', field)).toBe('9'); + }); + + it('keeps positive_class as a string code without catalog options', () => { + expect( + serializeAlgorithmParameterValue(1, { key: 'positive_class', type: 'select' }) + ).toBe('1'); + }); + + it('omits empty optional parameters from the request payload', () => { + const result = omitEmptyOptionalParameters( + { + event_var: 'procedure', + positive_class: null, + alpha: 0.05, + }, + [ + { key: 'event_var', required: true }, + { key: 'positive_class', required: false }, + { key: 'alpha', required: false }, + ] + ); + + expect(result['positive_class']).toBeUndefined(); + expect(result['event_var']).toBe('procedure'); + expect(result['alpha']).toBe(0.05); + }); + + it('keeps empty required positive_class in the request payload', () => { + const result = omitEmptyOptionalParameters( + { + event_var: 'procedure', + positive_class: null, + }, + [ + { key: 'event_var', required: true }, + { key: 'positive_class', required: true }, + ] + ); + + expect(result['positive_class']).toBeNull(); + expect(result['event_var']).toBe('procedure'); + }); +}); + +describe('formatAlgorithmParameterValue', () => { + const selectField = { + key: 'groupA', + type: 'select', + options: [ + { code: '1', label: 'female' }, + { code: '0', label: 'male' }, + ], + }; + + it('reads option labels instead of codes', () => { + expect(formatAlgorithmParameterValue('1', selectField)).toBe('female'); + expect(formatAlgorithmParameterValue({ code: '0' }, selectField)).toBe('male'); + expect(formatAlgorithmParameterValue(['1', '0'], { ...selectField, type: 'multi-select' })).toBe('female, male'); + }); + + it('reads booleans and leaves plain scalars alone', () => { + expect(formatAlgorithmParameterValue(true)).toBe('Yes'); + expect(formatAlgorithmParameterValue(false)).toBe('No'); + expect(formatAlgorithmParameterValue(0.95)).toBe('0.95'); + expect(formatAlgorithmParameterValue('spearman')).toBe('spearman'); + }); + + it('flattens a nested value and drops the unset ones', () => { + expect(formatAlgorithmParameterValue({ min_value: 1, tail: 'both' })).toBe('min value: 1; tail: both'); + expect(formatAlgorithmParameterValue(null)).toBe(''); + expect(formatAlgorithmParameterValue(' ')).toBe(''); + }); +}); diff --git a/src/app/core/algorithm-parameter.utils.ts b/src/app/core/algorithm-parameter.utils.ts new file mode 100644 index 000000000..1701d45c2 --- /dev/null +++ b/src/app/core/algorithm-parameter.utils.ts @@ -0,0 +1,188 @@ +type ParameterField = { + type?: string; + options?: unknown[]; + types?: string[]; + key?: string; +}; + +function normalizeOption(entry: unknown): { code: string; label: string } | null { + if (entry === null || entry === undefined) return null; + + if (typeof entry === 'object') { + const record = entry as Record<string, unknown>; + const code = record['code'] ?? record['value'] ?? record['name'] ?? record['label']; + if (code === null || code === undefined || String(code).trim() === '') { + return null; + } + const normalizedCode = String(code); + return { + code: normalizedCode, + label: String(record['label'] ?? record['name'] ?? normalizedCode), + }; + } + + const primitive = String(entry); + return { code: primitive, label: primitive }; +} + + +function selectOptions(field: ParameterField | null | undefined): Array<{ code: string; label: string }> { + return (field?.options ?? []) + .map(normalizeOption) + .filter((option): option is { code: string; label: string } => !!option); +} + +function serializeSelectValue(value: unknown, options: Array<{ code: string; label: string }>): string { + if (typeof value === 'object' && value !== null) { + const record = value as Record<string, unknown>; + const embedded = record['code'] ?? record['value']; + if (embedded !== null && embedded !== undefined && String(embedded).trim() !== '') { + return String(embedded); + } + } + + const raw = String(value); + const byCode = options.find((option) => option.code === raw); + if (byCode) return byCode.code; + + const byLabel = options.find((option) => option.label === raw); + if (byLabel) return byLabel.code; + + return raw; +} + +/** Ensures enum-backed parameter values are sent as string codes (not labels or numbers). */ +export function serializeAlgorithmParameterValue( + value: unknown, + field: ParameterField | null | undefined +): unknown { + if (value === null || value === undefined || value === '') { + return value; + } + + if (!field) { + return typeof value === 'number' ? String(value) : value; + } + + if (field.type === 'multi-select') { + const values = Array.isArray(value) ? value : [value]; + const options = selectOptions(field); + return values.map((entry) => + options.length ? serializeSelectValue(entry, options) : String(entry) + ); + } + + if (field.type === 'select') { + const options = selectOptions(field); + if (options.length) { + return serializeSelectValue(value, options); + } + } + + if (field.key === 'positive_class' || field.key === 'event_var') { + return String( + typeof value === 'object' && value !== null + ? (value as Record<string, unknown>)['code'] ?? + (value as Record<string, unknown>)['value'] ?? + value + : value + ); + } + + return value; +} + +export function optionBindingValue(option: unknown): string { + const normalized = normalizeOption(option); + return normalized?.code ?? String(option); +} + +function isEmptyParameterValue(value: unknown): boolean { + if (value === null || value === undefined) return true; + if (typeof value === 'string' && value.trim() === '') return true; + return false; +} + +/** Schema `default` values, keyed by field. Used when a request has no stored parameters. */ +export function parameterDefaultsFromSchema( + schema: Array<{ key?: string; default?: unknown }> = [] +): Record<string, unknown> { + const defaults: Record<string, unknown> = {}; + for (const field of schema) { + const key = field?.key; + if (!key || field.default === undefined) continue; + defaults[key] = field.default; + } + return defaults; +} + +/** Drops unset optional parameters so they are not sent to Exaflow. */ +export function omitEmptyOptionalParameters( + config: Record<string, unknown>, + schema: Array<{ key?: string; required?: boolean }> = [] +): Record<string, unknown> { + const next = { ...config }; + + schema.forEach((field) => { + const key = field?.key; + if (!key || field.required) return; + if (isEmptyParameterValue(next[key])) { + delete next[key]; + } + }); + + const positiveClassField = schema.find((field) => field?.key === 'positive_class'); + if ( + !positiveClassField?.required && + isEmptyParameterValue(next['positive_class']) + ) { + delete next['positive_class']; + } + + return next; +} + +/** + * Human-readable value for a parameter as it was configured: option labels instead of codes, + * Yes/No instead of booleans, lists joined, and nested objects flattened to `key: value` pairs. + * Returns '' for an unset value so callers drop the row instead of showing an empty cell. + * Display only — `serializeAlgorithmParameterValue` stays the request-side counterpart. + */ +export function formatAlgorithmParameterValue( + value: unknown, + field?: ParameterField | null +): string { + if (value === null || value === undefined) return ''; + if (typeof value === 'string') return value.trim() ? resolveOptionLabel(value, field) || value : ''; + if (typeof value === 'boolean') return value ? 'Yes' : 'No'; + if (typeof value === 'number') return String(value); + + if (Array.isArray(value)) { + return value + .map((entry) => formatAlgorithmParameterValue(entry, field)) + .filter(Boolean) + .join(', '); + } + + // Enum-backed parameters can arrive as the option object rather than its code. + const record = value as Record<string, unknown>; + const embedded = record['code'] ?? record['value']; + if (embedded !== undefined && embedded !== null) { + return resolveOptionLabel(embedded, field) || String(embedded); + } + + return Object.entries(record) + .filter(([, nested]) => nested !== null && nested !== undefined && nested !== '') + .map(([key, nested]) => `${key.replace(/_/g, ' ')}: ${formatAlgorithmParameterValue(nested)}`) + .join('; '); +} + +/** Option label for a select / multi-select value, matched on its code. */ +function resolveOptionLabel(value: unknown, field?: ParameterField | null): string { + if (!field?.options?.length) return ''; + + const options = selectOptions(field); + if (!options.length) return ''; + + return options.find((option) => option.code === String(value))?.label ?? ''; +} diff --git a/src/app/core/algorithm-result-enum-mapper.md b/src/app/core/algorithm-result-enum-mapper.md index f62d57fe2..0f4e90407 100644 --- a/src/app/core/algorithm-result-enum-mapper.md +++ b/src/app/core/algorithm-result-enum-mapper.md @@ -11,7 +11,7 @@ metadata enumerations and variable labels. The implementation lives in: - **Enum maps** (category codes → labels) come from data model enumerations. - **Label maps** (variable codes → labels) come from data model variable labels. - **Y/X variables** are derived from: - - Studio: selected variables/covariates. + - Studio: algorithm y / x roles and the assignable pool. - Dashboard detail: experiment algorithm input `y` / `x`. - Compare view: experiment variables / covariates. diff --git a/src/app/core/algorithm-result-enum-mapper.spec.ts b/src/app/core/algorithm-result-enum-mapper.spec.ts index af6a4e651..d0ae5ecee 100644 --- a/src/app/core/algorithm-result-enum-mapper.spec.ts +++ b/src/app/core/algorithm-result-enum-mapper.spec.ts @@ -21,4 +21,126 @@ describe('mapAlgorithmResultEnums', () => { expect(mapped.classes).toEqual(['Control', 'Case']); }); + + it('maps mixed-effects variable labels and ordinal categories', () => { + const mapped = mapAlgorithmResultEnums( + 'glmm_ordinal', + { + dependent_var: 'agegroup', + grouping_var: 'dataset', + indep_vars: ['Intercept', 'gender[M]'], + category_order: ['young', 'old'], + }, + { + agegroup: { young: 'Young', old: 'Old' }, + gender: { M: 'Male' }, + }, + { y: 'agegroup' }, + { + agegroup: 'Age group', + dataset: 'Dataset', + gender: 'Gender', + } + ); + + expect(mapped.dependent_var).toBe('Age group'); + expect(mapped.grouping_var).toBe('Dataset'); + expect(mapped.indep_vars).toEqual(['Intercept', 'Gender[Male]']); + expect(mapped.category_order).toEqual(['Young', 'Old']); + }); + + it('maps Cox regression variable and covariate labels', () => { + const mapped = mapAlgorithmResultEnums( + 'cox_regression_classical', + { + dependent_var: 'timesincebaseline', + event_var: 'alzheimerbroadcategory', + indep_vars: ['Intercept', 'gender[M]'], + summary: { n_obs: 10, n_events: 4 }, + }, + { + alzheimerbroadcategory: { AD: 'Alzheimer disease', Other: 'Other' }, + gender: { M: 'Male' }, + }, + { y: 'timesincebaseline', x: 'alzheimerbroadcategory' }, + { + timesincebaseline: 'Time since baseline', + alzheimerbroadcategory: 'Diagnosis', + gender: 'Gender', + } + ); + + expect(mapped.dependent_var).toBe('Time since baseline'); + expect(mapped.event_var).toBe('Diagnosis'); + expect(mapped.indep_vars).toEqual(['Intercept', 'Gender[Male]']); + }); + + it('maps categorical table labels using y and x enum maps', () => { + const mapped = mapAlgorithmResultEnums( + 'chi_squared', + { + x_labels: ['AD', 'Other'], + y_labels: ['F', 'M'], + }, + { + diagnosis: { AD: 'Alzheimer disease', Other: 'Other diagnosis' }, + gender: { F: 'Female', M: 'Male' }, + }, + { x: 'diagnosis', y: 'gender' } + ); + + expect(mapped.x_labels).toEqual(['Alzheimer disease', 'Other diagnosis']); + expect(mapped.y_labels).toEqual(['Female', 'Male']); + }); + + it('maps standardized mean difference groups using the x-variable enum map', () => { + const mapped = mapAlgorithmResultEnums( + 'standardized_mean_difference', + { comparisons: [{ group1: '1', group2: '2', smd: -0.277 }] }, + { biol_sex: { '1': 'male', '2': 'female' } }, + { y: 'age', x: 'biol_sex' } + ); + + expect(mapped.comparisons).toEqual([{ group1: 'male', group2: 'female', smd: -0.277 }]); + }); + + it('labels k-means variables and the matching centre keys', () => { + const mapped = mapAlgorithmResultEnums( + 'kmeans', + { variables: ['age', 'nihss_24h'], clusters: [{ label: 'Cluster 0', center: { age: 79.5, nihss_24h: 2 } }] }, + {}, + {}, + { age: 'Age', nihss_24h: '24h score' } + ); + + expect(mapped.variables).toEqual(['Age', '24h score']); + expect(mapped.clusters[0].center).toEqual({ Age: 79.5, '24h score': 2 }); + }); + + it('labels the variable that opens each k-means profile line', () => { + const mapped = mapAlgorithmResultEnums( + 'kmeans', + { + variables: ['nihss', 'nihss_24h'], + clusters: [{ label: 'Cluster 0', center: {}, profile: ['nihss_24h is above the overall mean', 'nihss is below the overall mean'] }], + }, + {}, + {}, + { nihss: 'Admission score', nihss_24h: '24h score' } + ); + + expect(mapped.clusters[0].profile).toEqual(['24h score is above the overall mean', 'Admission score is below the overall mean']); + }); + + it('labels a mixed model grouping variable sent as a list', () => { + const mapped = mapAlgorithmResultEnums( + 'lmm', + { grouping_var: ['hospit_unit'], indep_vars: ['Intercept'] }, + {}, + {}, + { hospit_unit: 'Hospitalization unit' } + ); + + expect(mapped.grouping_var).toEqual(['Hospitalization unit']); + }); }); diff --git a/src/app/core/algorithm-result-enum-mapper.ts b/src/app/core/algorithm-result-enum-mapper.ts index 3a08b369f..9a06f4b59 100644 --- a/src/app/core/algorithm-result-enum-mapper.ts +++ b/src/app/core/algorithm-result-enum-mapper.ts @@ -232,6 +232,58 @@ function mapRegressionVars(result: any, enumMaps: EnumMaps, labelMap: LabelMap | }; } +function mapMixedEffectsResult( + result: any, + enumMaps: EnumMaps, + vars: { y?: string | null; x?: string | null } | undefined, + labelMap: LabelMap | null | undefined +): any { + const mapped = mapRegressionVars(result, enumMaps, labelMap); + const next = { ...mapped }; + + if (typeof mapped?.grouping_var === 'string') { + next.grouping_var = labelMap?.[mapped.grouping_var] ?? mapped.grouping_var; + } else if (Array.isArray(mapped?.grouping_var)) { + next.grouping_var = mapped.grouping_var.map((code: string) => labelMap?.[code] ?? code); + } + + if (Array.isArray(mapped?.category_order)) { + const directMap = getEnumMapForVar(enumMaps, vars?.y); + const map = directMap ?? resolveEnumMapForValues(mapped.category_order, enumMaps); + if (map) { + next.category_order = mapArrayValues(mapped.category_order, map); + } + } + + return next; +} + +function mapTableTestLabels( + result: any, + enumMaps: EnumMaps, + vars: { y?: string | null; x?: string | null } | undefined +): any { + const next = { ...result }; + + if (Array.isArray(result?.x_labels)) { + const directMap = getEnumMapForVar(enumMaps, vars?.x); + const map = directMap ?? resolveEnumMapForValues(result.x_labels, enumMaps); + if (map) { + next.x_labels = mapArrayValues(result.x_labels, map); + } + } + + if (Array.isArray(result?.y_labels)) { + const directMap = getEnumMapForVar(enumMaps, vars?.y); + const map = directMap ?? resolveEnumMapForValues(result.y_labels, enumMaps); + if (map) { + next.y_labels = mapArrayValues(result.y_labels, map); + } + } + + return next; +} + function mapTwoWayAnovaKeys(obj: Record<string, any>, labelMap: LabelMap | null | undefined): Record<string, any> { if (!labelMap) return obj; const next: Record<string, any> = {}; @@ -313,6 +365,21 @@ function mapNaiveBayesClasses( }; } +function mapCoxResult( + result: any, + enumMaps: EnumMaps, + labelMap: LabelMap | null | undefined +): any { + const mapped = mapRegressionVars(result, enumMaps, labelMap); + const next = { ...mapped }; + + if (typeof mapped?.event_var === 'string') { + next.event_var = labelMap?.[mapped.event_var] ?? mapped.event_var; + } + + return next; +} + export function mapAlgorithmResultEnums( algorithm: string | null | undefined, result: any, @@ -338,6 +405,13 @@ export function mapAlgorithmResultEnums( const confusionMapped = mapConfusionMatrix(classesMapped.result, safeEnumMaps, yVar); const effectiveMap = confusionMapped.map ?? classesMapped.map; + // Gaussian only: the categorical table looks category_count up by feature code. + if (labelMap && Array.isArray(confusionMapped.result?.feature_names) && !confusionMapped.result?.category_count) { + confusionMapped.result = { + ...confusionMapped.result, + feature_names: confusionMapped.result.feature_names.map((code: string) => labelMap[code] ?? code), + }; + } if (effectiveMap && confusionMapped.result?.classification_summary) { return { ...confusionMapped.result, @@ -364,40 +438,70 @@ export function mapAlgorithmResultEnums( } case 'histogram': return mapMultipleHistograms(result, safeEnumMaps, yVar); + case 'outlier_report': { + if (!labelMap || !Array.isArray(result?.featurewise)) return result; + return { + ...result, + featurewise: result.featurewise.map((row: any) => ({ ...row, variable: labelMap[row?.variable] ?? row?.variable })), + }; + } + case 'kmeans': { + if (!labelMap || !Array.isArray(result?.variables)) return result; + // Each profile line starts with a variable code: "nihss_adm_score is below the overall mean". + const profileLine = (line: unknown) => { + const text = String(line ?? ''); + const code = result.variables.find((v: string) => text.startsWith(`${v} `)); + return code && labelMap[code] ? labelMap[code] + text.slice(code.length) : text; + }; + return { + ...result, + variables: result.variables.map((code: string) => labelMap[code] ?? code), + clusters: Array.isArray(result.clusters) + ? result.clusters.map((cluster: any) => ({ + ...cluster, + center: mapObjectKeys(cluster?.center ?? {}, labelMap), + ...(Array.isArray(cluster?.profile) ? { profile: cluster.profile.map(profileLine) } : {}), + })) + : result.clusters, + }; + } + case 'standardized_mean_difference': { + const map = getEnumMapForVar(safeEnumMaps, xVar); + if (!map || !Array.isArray(result?.comparisons)) return result; + return { + ...result, + comparisons: result.comparisons.map((row: any) => ({ + ...row, + group1: map[normalizeKey(row.group1)] ?? row.group1, + group2: map[normalizeKey(row.group2)] ?? row.group2, + })), + }; + } case 'linear_regression': case 'linear_regression_cv': case 'logistic_regression': case 'logistic_regression_cv': - return mapRegressionVars(result, safeEnumMaps, labelMap); + case 'cox_regression_classical': + case 'cox_regression_stacked': + return mapCoxResult(result, safeEnumMaps, labelMap); + case 'lmm': + case 'glmm_binary': + case 'glmm_ordinal': + return mapMixedEffectsResult(result, safeEnumMaps, vars, labelMap); + case 'chi_squared': + case 'fisher_exact': + return mapTableTestLabels(result, safeEnumMaps, vars); case 'anova_twoway': { if (!labelMap) return result; const mapped: any = { ...result }; if (Array.isArray(mapped.terms)) { mapped.terms = mapAnovaTerms(mapped.terms, labelMap); } - if (mapped.sum_sq && typeof mapped.sum_sq === 'object' && !Array.isArray(mapped.sum_sq)) { - mapped.sum_sq = mapTwoWayAnovaKeys(mapped.sum_sq, labelMap); - } - if (mapped.df && typeof mapped.df === 'object' && !Array.isArray(mapped.df)) { - mapped.df = mapTwoWayAnovaKeys(mapped.df, labelMap); - } - if (mapped.ms && typeof mapped.ms === 'object' && !Array.isArray(mapped.ms)) { - mapped.ms = mapTwoWayAnovaKeys(mapped.ms, labelMap); - } - if (mapped.f_stat && typeof mapped.f_stat === 'object' && !Array.isArray(mapped.f_stat)) { - mapped.f_stat = mapTwoWayAnovaKeys(mapped.f_stat, labelMap); - } - if (mapped.f_value && typeof mapped.f_value === 'object' && !Array.isArray(mapped.f_value)) { - mapped.f_value = mapTwoWayAnovaKeys(mapped.f_value, labelMap); - } - if (mapped.p_value && typeof mapped.p_value === 'object' && !Array.isArray(mapped.p_value)) { - mapped.p_value = mapTwoWayAnovaKeys(mapped.p_value, labelMap); - } - if (mapped.pvalue && typeof mapped.pvalue === 'object' && !Array.isArray(mapped.pvalue)) { - mapped.pvalue = mapTwoWayAnovaKeys(mapped.pvalue, labelMap); - } - if (mapped.f_pvalue && typeof mapped.f_pvalue === 'object' && !Array.isArray(mapped.f_pvalue)) { - mapped.f_pvalue = mapTwoWayAnovaKeys(mapped.f_pvalue, labelMap); + const twoWayKeys = ['sum_sq', 'df', 'ms', 'f_stat', 'f_value', 'p_value', 'pvalue', 'f_pvalue']; + for (const key of twoWayKeys) { + if (mapped[key] && typeof mapped[key] === 'object' && !Array.isArray(mapped[key])) { + mapped[key] = mapTwoWayAnovaKeys(mapped[key], labelMap); + } } return mapped; } @@ -411,6 +515,7 @@ export function mapAlgorithmResultEnums( mapped.correlations = mapMatrixVariables(correlations, labelMap); } + // Backwards compatibility: keep historical Pearson matrix key aliases when remapping labels. const matrixKeys = [ 'p-values', 'p_values', diff --git a/src/app/core/constants/algorithm.constants.ts b/src/app/core/constants/algorithm.constants.ts index 522dc62c6..62981468f 100644 --- a/src/app/core/constants/algorithm.constants.ts +++ b/src/app/core/constants/algorithm.constants.ts @@ -4,10 +4,17 @@ export const AlgorithmNames = { ANOVA_TWOWAY: 'anova_twoway', TTEST_ONESAMPLE: 'ttest_onesample', PCA: 'pca', - PCA_WITH_TRANSFORMATION: 'pca_with_transformation', HISTOGRAM: 'histogram', DESCRIBE: 'describe', - LOGISTIC_REGRESSION_FEDAVERAGE_FLOWER: 'logistic_regression_fedaverage_flower', + QUARTILES: 'quartiles', + BINNED_MANN_WHITNEY_U_TEST: 'binned_mann_whitney_u_test', + LMM: 'lmm', + GLMM_BINARY: 'glmm_binary', + GLMM_ORDINAL: 'glmm_ordinal', + CHI_SQUARED: 'chi_squared', + FISHER_EXACT: 'fisher_exact', + OUTLIER_REPORT: 'outlier_report', + LINEAR_SVM: 'linear_svm', }; export const VariableTypes = { @@ -18,6 +25,10 @@ export const VariableTypes = { TEXT: 'text', }; +export const HistogramBinningType = { + WILKINSON: 'wilkinson', +} as const; + export const AlgorithmRoles = { Y: 'y', X: 'x', diff --git a/src/app/core/data-model.utils.spec.ts b/src/app/core/data-model.utils.spec.ts new file mode 100644 index 000000000..cc2a00b72 --- /dev/null +++ b/src/app/core/data-model.utils.spec.ts @@ -0,0 +1,36 @@ +import { + isCategoricalVariableType, + isFreeTextCde, + isNumericalVariableType, + specificationAcceptsVariableType, +} from './data-model.utils'; + +describe('CDE variable types', () => { + it('classifies the five CDE types and rejects legacy ones', () => { + expect(['continuous', 'discrete'].every(isNumericalVariableType)).toBe(true); + expect(['nominal', 'ordinal', 'binary'].every(isCategoricalVariableType)).toBe(true); + for (const legacy of ['real', 'integer', 'int', 'text']) { + expect(isNumericalVariableType(legacy)).toBe(false); + expect(isCategoricalVariableType(legacy)).toBe(false); + } + }); + + it('maps CDE types onto algorithm specification input types', () => { + expect(specificationAcceptsVariableType(['real'], 'continuous')).toBe(true); + expect(specificationAcceptsVariableType(['real'], 'discrete')).toBe(true); + expect(specificationAcceptsVariableType(['int'], 'discrete')).toBe(true); + expect(specificationAcceptsVariableType(['int'], 'continuous')).toBe(false); + expect(specificationAcceptsVariableType(['text'], 'ordinal')).toBe(true); + expect(specificationAcceptsVariableType(['text'], 'binary')).toBe(true); + expect(specificationAcceptsVariableType(['real', 'int'], 'nominal')).toBe(false); + expect(specificationAcceptsVariableType(['real'], 'real')).toBe(false); + }); + + it('treats a categorical CDE without enumerations as free text Exaflow cannot analyse', () => { + expect(isFreeTextCde({ type: 'nominal' })).toBe(true); + expect(isFreeTextCde({ type: 'ordinal', enumerations: [] })).toBe(true); + expect(isFreeTextCde({ type: 'nominal', enumerations: [{ code: 'M' }] })).toBe(false); + expect(isFreeTextCde({ type: 'continuous' })).toBe(false); + expect(isFreeTextCde(null)).toBe(false); + }); +}); diff --git a/src/app/core/data-model.utils.ts b/src/app/core/data-model.utils.ts new file mode 100644 index 000000000..f2696d7f7 --- /dev/null +++ b/src/app/core/data-model.utils.ts @@ -0,0 +1,106 @@ +import { DataModel } from '../models/data-model.interface'; +import { EnumMaps } from './algorithm-result-enum-mapper'; + +/** + * Find a data model by its "code:version" string (e.g. "Stroke:3.7"). + * Returns null when no model matches or the input is empty. + */ +export function findDataModelByCodeVersion( + codeVersion: string, + models: DataModel[] +): DataModel | null { + if (!codeVersion) return null; + const [code, version] = codeVersion.split(':'); + return ( + models.find((m) => m.code === code && String(m.version) === String(version)) ?? null + ); +} + +/** + * Build an enum-map (variable code -> { enumCode -> label }) from a converted + * D3 hierarchy's variables. Variables without enumerations or without a code + * are skipped; empty enum maps are dropped. + */ +export function buildEnumMapForVariables( + allVariables: Array<{ code?: unknown; enumerations?: Array<{ code?: unknown; label?: unknown; name?: unknown }> }> +): EnumMaps { + const maps: EnumMaps = {}; + for (const v of allVariables) { + const enums = Array.isArray(v.enumerations) ? v.enumerations : []; + if (!enums.length) continue; + + const code = String(v.code ?? ''); + if (!code) continue; + + const enumMap: Record<string, string> = {}; + for (const e of enums) { + const raw = e?.code ?? e?.label ?? e?.name; + if (raw === null || raw === undefined) continue; + const key = String(raw); + enumMap[key] = String(e?.label ?? e?.name ?? raw); + } + + if (Object.keys(enumMap).length > 0) { + maps[code] = enumMap; + } + } + return maps; +} + +/** + * Count the leaf nodes in a (possibly nested) tree. A node without children + * counts as one leaf. + */ +export function countLeafNodes(node: unknown): number { + const children = (node as { children?: unknown[] } | null | undefined)?.children; + if (!children || children.length === 0) { + return 1; + } + return children.reduce((total: number, child) => total + countLeafNodes(child), 0); +} + +/** + * CDE variable types. `type` is the only typing field a CDE carries: numerical + * `continuous`/`discrete`, categorical `nominal`/`ordinal`/`binary`. + */ +export const NUMERICAL_VARIABLE_TYPES: readonly string[] = ['continuous', 'discrete']; +export const CATEGORICAL_VARIABLE_TYPES: readonly string[] = ['nominal', 'ordinal', 'binary']; + +/** Algorithm specifications still declare input `types` as `real`, `int`, or `text`. */ +const SPECIFICATION_TYPE_BY_VARIABLE_TYPE: Readonly<Record<string, string>> = { + continuous: 'real', + discrete: 'int', + nominal: 'text', + ordinal: 'text', + binary: 'text', +}; + +function normalizeVariableType(type: unknown): string { + return String(type ?? '').trim().toLowerCase(); +} + +export function isNumericalVariableType(type: unknown): boolean { + return NUMERICAL_VARIABLE_TYPES.includes(normalizeVariableType(type)); +} + +export function isCategoricalVariableType(type: unknown): boolean { + return CATEGORICAL_VARIABLE_TYPES.includes(normalizeVariableType(type)); +} + +/** + * A categorical CDE without enumerations holds free text (e.g. a subject id). + * Exaflow's request validator rejects it as an algorithm input. + */ +export function isFreeTextCde(variable: { type?: unknown; enumerations?: unknown[] | null } | null | undefined): boolean { + return isCategoricalVariableType(variable?.type) && !variable?.enumerations?.length; +} + +/** + * Whether an algorithm input declaring `specTypes` accepts a CDE of `type`: + * continuous → real, discrete → int (also accepted by real slots), categorical → text. + */ +export function specificationAcceptsVariableType(specTypes: readonly string[], type: unknown): boolean { + const specType = SPECIFICATION_TYPE_BY_VARIABLE_TYPE[normalizeVariableType(type)]; + if (!specType) return false; + return specTypes.includes(specType) || (specType === 'int' && specTypes.includes('real')); +} diff --git a/src/app/core/describe-result.utils.ts b/src/app/core/describe-result.utils.ts new file mode 100644 index 000000000..746a53e31 --- /dev/null +++ b/src/app/core/describe-result.utils.ts @@ -0,0 +1,43 @@ +import { VariableStats } from '../models/algorithm-results.model'; + +interface DescribePayload { + featurewise?: unknown; +} + +export function getFeaturewiseDescribeRows(value: unknown): VariableStats[] { + const payload = unwrapDescribePayload(value); + const rows = payload?.featurewise ?? []; + return Array.isArray(rows) ? rows as VariableStats[] : []; +} + +function readDatasetLabels(source: unknown): Record<string, string> { + if (!source || typeof source !== 'object' || Array.isArray(source)) return {}; + const labels = (source as { dataset_labels?: unknown }).dataset_labels; + if (!labels || typeof labels !== 'object' || Array.isArray(labels)) return {}; + return labels as Record<string, string>; +} + +export function getDescribeDatasetLabels(value: unknown): Record<string, string> { + if (!value || typeof value !== 'object') return {}; + const topLevel = readDatasetLabels(value); + if (Object.keys(topLevel).length) return topLevel; + return readDatasetLabels(unwrapDescribePayload(value)); +} + +export function resolveDatasetDisplayLabel( + code: string, + labels: Record<string, string> +): string { + if (!code) return code; + if (String(code).toLowerCase() === 'all datasets') return 'All datasets'; + return labels[code] ?? code; +} + +function unwrapDescribePayload(value: unknown): DescribePayload | null { + if (!value || typeof value !== 'object') return null; + const maybeResponse = value as { result?: unknown }; + if (maybeResponse.result && typeof maybeResponse.result === 'object') { + return maybeResponse.result as DescribePayload; + } + return value as DescribePayload; +} diff --git a/src/app/core/experiment-drag.utils.ts b/src/app/core/experiment-drag.utils.ts new file mode 100644 index 000000000..1ba2ce8d7 --- /dev/null +++ b/src/app/core/experiment-drag.utils.ts @@ -0,0 +1,41 @@ +/** + * The one contract between an experiment row being dragged and whatever can receive it — the + * folder canvas and the folder chips. A private MIME keeps the payload from being mistaken for + * dropped text, and every target reads it through these helpers so the two surfaces cannot + * drift apart on the type string. + * + * Drop targets may only inspect `types` while the drag is in flight: the payload itself stays + * unreadable until the drop lands. Hence the split `isExperimentDrag` / `droppedExperimentId`. + */ + +export const EXPERIMENT_DRAG_MIME = 'application/x-mip-experiment'; + +/** Arm the drag. Only `dragstart` has a writable data transfer, so this is the single writer. */ +export function beginExperimentDrag(dataTransfer: DataTransfer | null, experimentId: string): void { + if (!dataTransfer || !experimentId) return; + dataTransfer.effectAllowed = 'copy'; + dataTransfer.setData(EXPERIMENT_DRAG_MIME, experimentId); +} + +/** Cheap enough for `dragover`, which fires continuously and must never touch the payload. */ +export function isExperimentDrag(dataTransfer: DataTransfer | null): boolean { + if (!dataTransfer) return false; + return Array.from(dataTransfer.types || []).includes(EXPERIMENT_DRAG_MIME); +} + +/** The dragged run id, or null when the drag carries something else (a file, selected text). */ +export function droppedExperimentId(dataTransfer: DataTransfer | null): string | null { + if (!dataTransfer || !isExperimentDrag(dataTransfer)) return null; + const id = dataTransfer.getData(EXPERIMENT_DRAG_MIME)?.trim(); + return id ? id : null; +} + +/** + * Whether a `dragleave` really leaves the drop zone. It also fires when the pointer crosses a + * child, so the naive handler makes a highlight flicker over every row and icon inside it. + */ +export function leavesDragZone(event: DragEvent, zone: EventTarget | null): boolean { + if (!zone) return true; + const next = event.relatedTarget as Node | null; + return !(zone as HTMLElement).contains(next); +} diff --git a/src/app/core/export.utils.ts b/src/app/core/export.utils.ts new file mode 100644 index 000000000..7beace7b4 --- /dev/null +++ b/src/app/core/export.utils.ts @@ -0,0 +1,190 @@ +import html2canvas from 'html2canvas'; +import { prettifyLabel } from './algorithm-mappers'; + +export interface ChartImage { + /** JPEG data-URL on a white background. */ + dataUrl: string; + width: number; + height: number; + /** The chart's own title, when it has one. */ + title: string; +} + +/** + * JPEG keeps exported charts small: a lossless PNG screenshot at 2-3x was the + * main reason a report weighed several MB. 2x pixel ratio stays sharp in print. + */ +const PIXEL_RATIO = 2; +const JPEG_QUALITY = 0.85; + +/** + * Every ECharts chart inside `container`, in document order. Nothing else is + * captured, so a results section without charts never becomes a screenshot. + */ +export async function captureCharts(container: HTMLElement): Promise<ChartImage[]> { + const hosts = Array.from(container.querySelectorAll<HTMLElement>('[_echarts_instance_]')); + const images: ChartImage[] = []; + for (const host of hosts) { + const image = await captureEChart(host); + if (image) images.push(image); + } + return images; +} + +/** + * Render the chart inside `element` to a compressed image. Uses, in order: + * the ECharts instance's own export, the first SVG (D3 charts), then a + * screenshot of the element as a last resort. + */ +export async function captureChart(element: HTMLElement): Promise<ChartImage | null> { + return ( + (await captureEChart(element)) ?? + (await captureSvg(element)) ?? + (await captureScreenshot(element)) + ); +} + +async function captureEChart(element: HTMLElement): Promise<ChartImage | null> { + const host = element.hasAttribute('_echarts_instance_') + ? element + : element.querySelector<HTMLElement>('[_echarts_instance_]'); + if (!host) return null; + + // Same lazy module ngx-echarts loads, so the instance registry is shared. + const echarts = await import('echarts'); + const chart = echarts.getInstanceByDom(host); + if (!chart || !chart.getWidth() || !chart.getHeight()) return null; + + const option = chart.getOption() as { title?: Array<{ text?: string }> }; + return { + dataUrl: chart.getDataURL({ type: 'jpeg', pixelRatio: PIXEL_RATIO, backgroundColor: '#ffffff' }), + width: chart.getWidth() * PIXEL_RATIO, + height: chart.getHeight() * PIXEL_RATIO, + // Multi-line chart titles ("Group means\np-value: …") read as one line in a list. + title: String(option.title?.[0]?.text ?? '').trim().replace(/\s*\n\s*/g, ' · '), + }; +} + +async function captureSvg(element: HTMLElement): Promise<ChartImage | null> { + const svg = element.querySelector('svg'); + if (!svg) return null; + const box = svg.getBoundingClientRect(); + const width = parseFloat(svg.getAttribute('width') ?? '') || box.width; + const height = parseFloat(svg.getAttribute('height') ?? '') || box.height; + if (!width || !height) return null; + + const clone = svg.cloneNode(true) as SVGSVGElement; + clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); + const url = URL.createObjectURL( + new Blob([new XMLSerializer().serializeToString(clone)], { type: 'image/svg+xml;charset=utf-8' }) + ); + try { + const img = await new Promise<HTMLImageElement | null>((resolve) => { + const image = new Image(); + image.onload = () => resolve(image); + image.onerror = () => resolve(null); + image.src = url; + }); + if (!img) return null; + const canvas = whiteCanvas(width * PIXEL_RATIO, height * PIXEL_RATIO); + canvas.getContext('2d')?.drawImage(img, 0, 0, canvas.width, canvas.height); + return { dataUrl: canvas.toDataURL('image/jpeg', JPEG_QUALITY), width: canvas.width, height: canvas.height, title: '' }; + } finally { + URL.revokeObjectURL(url); + } +} + +async function captureScreenshot(element: HTMLElement): Promise<ChartImage | null> { + const canvas = await html2canvas(element, { + backgroundColor: '#ffffff', + scale: PIXEL_RATIO, + useCORS: true, + logging: false, + }); + if (!canvas.width || !canvas.height) return null; + return { dataUrl: canvas.toDataURL('image/jpeg', JPEG_QUALITY), width: canvas.width, height: canvas.height, title: '' }; +} + +function whiteCanvas(width: number, height: number): HTMLCanvasElement { + const canvas = document.createElement('canvas'); + canvas.width = Math.round(width); + canvas.height = Math.round(height); + const ctx = canvas.getContext('2d'); + if (ctx) { + ctx.fillStyle = '#ffffff'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + } + return canvas; +} + +/** File-name-safe slug: letters, digits, dash and underscore only. */ +export function fileSlug(value: string, fallback: string): string { + const slug = String(value ?? '').trim().replace(/\s+/g, '_').replace(/[^\w-]/g, ''); + return slug || fallback; +} + +/** Label + value row of an export's "About this export" block; arrays render as bullets. */ +export type ContextRow = [label: string, value: string | string[] | null | undefined]; + +/** What an experiment export describes: the setup the result came from. */ +export interface ExperimentExportDetails { + experimentName: string; + createdBy?: string | null; + createdAt?: Date | string | null; + algorithm?: string | null; + params?: Record<string, unknown> | null; + preprocessing?: string | null; + domain?: string | null; + datasets?: string[] | null; + variables?: string[] | null; + covariates?: string[] | null; + filters?: string[] | null; + interactions?: string | string[] | null; + transformations?: string | string[] | null; + mipVersion?: string | null; +} + +/** The experiment's setup as context rows, shared by the PDF report and the ZIP README. */ +export function experimentContextRows(details: ExperimentExportDetails): ContextRow[] { + const created = [details.createdBy, formatExportDate(details.createdAt)].filter(Boolean).join(', '); + return [ + ['Algorithm', details.algorithm], + ['Parameters', Object.entries(details.params ?? {}).map(([key, value]) => `${prettifyLabel(key)}: ${formatExportValue(value)}`)], + ['Pathology', details.domain], + ['Datasets', details.datasets], + ['Outcome (y)', details.variables], + ['Covariates (x)', details.covariates], + ['Filters', details.filters], + ['Preprocessing', details.preprocessing ? details.preprocessing.split('\n') : null], + ['Interactions', details.interactions], + ['Transformations', details.transformations], + ['Created', created], + ]; +} + +/** Drop rows with no value, so an export never lists "none" placeholders. */ +export function filledContextRows(rows: ContextRow[]): Array<[string, string[]]> { + return rows + .map(([label, value]): [string, string[]] => [ + label, + (Array.isArray(value) ? value : [value ?? '']).map((item) => String(item).trim()).filter((item) => item && item !== 'none'), + ]) + .filter(([, values]) => values.length > 0); +} + +export function formatExportDate(value?: Date | string | null): string { + if (!value) return ''; + const date = value instanceof Date ? value : new Date(value); + return Number.isNaN(date.getTime()) ? '' : date.toLocaleString(); +} + +export function formatExportValue(value: unknown): string { + if (value === null || value === undefined) return ''; + if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') return String(value); + if (Array.isArray(value)) return value.map(formatExportValue).join(', '); + try { + return JSON.stringify(value); + } catch { + return String(value); + } +} diff --git a/src/app/core/filter-display.utils.spec.ts b/src/app/core/filter-display.utils.spec.ts new file mode 100644 index 000000000..45e14a26a --- /dev/null +++ b/src/app/core/filter-display.utils.spec.ts @@ -0,0 +1,54 @@ +import { BackendFilter } from '../models/filters.model'; +import { countFilterRules, formatFilterExpression } from './filter-display.utils'; + +const sources = { + labelMap: { age: 'Age at onset', sex: 'Sex' }, + enumMaps: { sex: { '1': 'female', '0': 'male' } }, +}; + +function rule(field: string, operator: string, value: unknown): any { + return { id: `${field}-${operator}`, field, type: 'string', input: 'text', operator, value }; +} + +function group(condition: 'AND' | 'OR', rules: unknown[]): BackendFilter { + return { condition, rules: rules as BackendFilter['rules'], valid: true }; +} + +describe('filter-display.utils', () => { + it('reads a single rule with its label, operator and enum value', () => { + expect(formatFilterExpression(group('AND', [rule('sex', 'equal', '1')]), sources)).toBe('Sex = female'); + }); + + it('joins a group with its own condition and parenthesises a nested group', () => { + const filters = group('AND', [ + rule('age', 'greater_or_equal', 18), + group('OR', [rule('sex', 'equal', '1'), rule('sex', 'not_equal', '0')]), + ]); + + expect(formatFilterExpression(filters, sources)).toBe( + 'Age at onset >= 18 AND (Sex = female OR Sex != male)' + ); + }); + + it('renders null checks and unknown operators without a value', () => { + expect(formatFilterExpression(group('AND', [rule('age', 'is_null', null)]), sources)) + .toBe('Age at onset IS NULL'); + expect(formatFilterExpression(group('AND', [rule('sex', 'between', 'x')]), sources)) + .toBe('Sex between x'); + }); + + it('falls back to the raw code when nothing labels it', () => { + expect(formatFilterExpression(group('AND', [rule('unknown_cde', 'equal', '3')]), sources)) + .toBe('unknown_cde = 3'); + }); + + it('returns nothing for an empty tree and counts only leaf rules', () => { + const empty = group('AND', []); + expect(formatFilterExpression(empty, sources)).toBe(''); + expect(countFilterRules(empty)).toBe(0); + expect(countFilterRules(null)).toBe(0); + + const nested = group('AND', [rule('age', 'equal', 1), group('OR', [rule('sex', 'equal', '1'), rule('sex', 'equal', '0')])]); + expect(countFilterRules(nested)).toBe(3); + }); +}); diff --git a/src/app/core/filter-display.utils.ts b/src/app/core/filter-display.utils.ts new file mode 100644 index 000000000..8eeb73376 --- /dev/null +++ b/src/app/core/filter-display.utils.ts @@ -0,0 +1,73 @@ +import { BackendFilter, BackendRule } from '../models/filters.model'; +import { EnumMaps, LabelMap } from './algorithm-result-enum-mapper'; +import { filterOperatorSymbolFor, isUnaryFilterOperator } from './filter-logic.utils'; + +/** + * Reads a backend filter tree as a user reads it. The tree is the payload Exaflow + * receives, so this is the one place that knows how a rule group reads as a sentence. + */ +interface FilterDisplaySources { + /** Variable code → data-model label. */ + labelMap: LabelMap; + /** Variable code → enum code → label, for the value side of a rule. */ + enumMaps?: EnumMaps; +} + +/* The operator symbols come from core/filter-logic.utils — the same table the filter builder + writes with, so a tree never prints a raw backend token here and a real operator there. */ + +/** Leaf-rule count of a filter tree; 0 when nothing was filtered. */ +export function countFilterRules(node: BackendFilter | null | undefined): number { + if (!node || !Array.isArray(node.rules)) return 0; + return node.rules.reduce<number>( + (total, rule) => total + ((rule as BackendFilter)?.rules ? countFilterRules(rule as BackendFilter) : 1), + 0 + ); +} + +/** + * One-line cohort description, e.g. `Age >= 18 AND (Sex = female OR Sex = male)`. + * Returns '' for a tree without rules so callers can show their own empty state. + */ +export function formatFilterExpression( + node: BackendFilter | null | undefined, + sources: FilterDisplaySources +): string { + return formatNode(node, sources, true); +} + +/** Only a nested group is parenthesised: the outermost group has nothing to bind tighter than. */ +function formatNode( + node: BackendRule | BackendFilter | null | undefined, + sources: FilterDisplaySources, + isRoot = false +): string { + if (!node) return ''; + + if ('rules' in node) { + const parts = node.rules.map((child) => formatNode(child, sources)).filter(Boolean); + if (!parts.length) return ''; + const condition = String(node.condition).toUpperCase() === 'OR' ? 'OR' : 'AND'; + const expression = parts.join(` ${condition} `); + return parts.length > 1 && !isRoot ? `(${expression})` : expression; + } + + const field = String(node.field ?? node.id ?? ''); + const label = sources.labelMap[field] || field || 'Variable'; + const operator = filterOperatorSymbolFor(String(node.operator ?? '')); + + if (isUnaryFilterOperator(String(node.operator ?? ''))) { + return `${label} ${operator}`; + } + return `${label} ${operator} ${formatValue(field, node.value, sources)}`; +} + +function formatValue(field: string, value: unknown, sources: FilterDisplaySources): string { + if (value === null || value === undefined || value === '') return 'value'; + if (Array.isArray(value)) { + const parts = value.map((entry) => formatValue(field, entry, sources)).filter(Boolean); + return parts.length ? parts.join(', ') : 'value'; + } + const code = String(value); + return sources.enumMaps?.[field]?.[code] ?? code; +} diff --git a/src/app/core/filter-logic.utils.ts b/src/app/core/filter-logic.utils.ts new file mode 100644 index 000000000..26afa04ca --- /dev/null +++ b/src/app/core/filter-logic.utils.ts @@ -0,0 +1,82 @@ +import { isCategoricalVariableType } from './data-model.utils'; +/** + * The one operator vocabulary the app shares between the filter builder, the filter + * display, and anything that sends a filter tree to the backend. + * + * A builder-side `symbol` is what a condition block holds and what a `<select>` binds; + * `backend` is the operator name exaflow expects. Every lookup accepts either form and + * returns the input untouched when it is not in the table, so an operator the builder + * cannot author is preserved verbatim instead of silently becoming `equal`. + */ +export type FilterOperatorValueKind = + /** One value: a category, a number, a string. */ + | 'single' + /** Any number of values, sent as an array: `in`, `not_in`. */ + | 'values' + /** No value at all: `is null`, `is not null`. */ + | 'none'; + +export interface FilterOperatorSpec { + symbol: string; + backend: string; + /** How the builder names the operator to the user. */ + label: string; + /** How a rule summary reads it: `Age is at least 40`. */ + phrase: string; + valueKind: FilterOperatorValueKind; + /** Offered only for variables the data model types as real/integer. */ + numericOnly?: boolean; +} + +export const FILTER_OPERATORS: readonly FilterOperatorSpec[] = [ + { symbol: '=', backend: 'equal', label: 'Equals', phrase: 'is', valueKind: 'single' }, + { symbol: '!=', backend: 'not_equal', label: 'Does not equal', phrase: 'is not', valueKind: 'single' }, + { symbol: 'IN', backend: 'in', label: 'In (any of)', phrase: 'is any of', valueKind: 'values' }, + { symbol: 'NOT IN', backend: 'not_in', label: 'Not in (none of)', phrase: 'is none of', valueKind: 'values' }, + { symbol: '>', backend: 'greater', label: 'Greater than', phrase: 'is greater than', valueKind: 'single', numericOnly: true }, + { symbol: '>=', backend: 'greater_or_equal', label: 'Greater than or equal to', phrase: 'is at least', valueKind: 'single', numericOnly: true }, + { symbol: '<', backend: 'less', label: 'Less than', phrase: 'is less than', valueKind: 'single', numericOnly: true }, + { symbol: '<=', backend: 'less_or_equal', label: 'Less than or equal to', phrase: 'is at most', valueKind: 'single', numericOnly: true }, + { symbol: 'IS NULL', backend: 'is_null', label: 'Is null', phrase: 'is empty', valueKind: 'none' }, + { symbol: 'IS NOT NULL', backend: 'is_not_null', label: 'Is not null', phrase: 'is not empty', valueKind: 'none' }, +]; + +/** Looks an operator up by builder symbol or by backend name — callers never convert first. */ +export function filterOperatorSpec(operator: string | null | undefined): FilterOperatorSpec | null { + return FILTER_OPERATORS.find((entry) => entry.symbol === operator || entry.backend === operator) ?? null; +} + +/** Backend name → builder symbol. Anything outside the table is returned unchanged. */ +export function filterOperatorSymbolFor(operator: string): string { + return filterOperatorSpec(operator)?.symbol ?? operator; +} + +/** Rule-summary verb; `between` has no builder symbol but is still worded. */ +export function filterOperatorPhrase(operator: string): string { + return filterOperatorSpec(operator)?.phrase ?? (operator.toLowerCase() === 'between' ? 'is between' : operator); +} + +/** Builder symbol → backend name. Anything outside the table is returned unchanged. */ +export function filterOperatorBackendFor(operator: string): string { + return filterOperatorSpec(operator)?.backend ?? operator; +} + +export function filterOperatorValueKind(operator: string): FilterOperatorValueKind { + return filterOperatorSpec(operator)?.valueKind ?? 'single'; +} + +export function isUnaryFilterOperator(operator: string): boolean { + return filterOperatorValueKind(operator) === 'none'; +} + +/** `in` / `not_in`: the value is a set, so any number of categories can be picked. */ +export function isMultiValueFilterOperator(operator: string): boolean { + return filterOperatorValueKind(operator) === 'values'; +} + +/** Operator symbols offered for a data-model variable type. */ +export function filterOperatorSymbolsForType(type: string | null | undefined): string[] { + const numeric = !isCategoricalVariableType(type); + return FILTER_OPERATORS.filter((entry) => numeric || !entry.numericOnly).map((entry) => entry.symbol); +} + diff --git a/src/app/core/html.utils.spec.ts b/src/app/core/html.utils.spec.ts new file mode 100644 index 000000000..2308e5d6b --- /dev/null +++ b/src/app/core/html.utils.spec.ts @@ -0,0 +1,7 @@ +import { escapeHtml } from './html.utils'; + +describe('escapeHtml', () => { + it('escapes every HTML-significant character', () => { + expect(escapeHtml(`Sex <b>&"'x</b>`)).toBe('Sex <b>&"'x</b>'); + }); +}); diff --git a/src/app/core/html.utils.ts b/src/app/core/html.utils.ts new file mode 100644 index 000000000..b78ffe572 --- /dev/null +++ b/src/app/core/html.utils.ts @@ -0,0 +1,10 @@ +/** Escapes a value for safe interpolation into strings bound with [innerHTML] or returned by an + * ECharts tooltip formatter (also HTML). */ +export function escapeHtml(value: unknown): string { + return String(value ?? '') + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} diff --git a/src/app/core/http-error.utils.ts b/src/app/core/http-error.utils.ts new file mode 100644 index 000000000..f7516c705 --- /dev/null +++ b/src/app/core/http-error.utils.ts @@ -0,0 +1,8 @@ +/** + * True when an HTTP failure means "this resource is gone", not "the request failed". + * The distinction matters for folder-member hydration: a 404 is safe to prune, while a + * transient network/server error must leave folder membership intact for a retry. + */ +export function isNotFoundError(error: unknown): boolean { + return (error as { status?: unknown } | null)?.status === 404; +} diff --git a/src/app/core/inflight-writes.ts b/src/app/core/inflight-writes.ts new file mode 100644 index 000000000..6fb56c25f --- /dev/null +++ b/src/app/core/inflight-writes.ts @@ -0,0 +1,38 @@ +import { computed, signal } from '@angular/core'; +import { Observable } from 'rxjs'; + +/** + * Bookend for server writes: one request per key on the wire (a doubled click is one write, while + * unrelated keys still run in parallel), a `busy` signal to disarm controls with, and the failure + * line routed to whichever signal the owner of the control shows it in. + */ +export class InflightWrites { + readonly busy = computed(() => this.pending() > 0); + private readonly keys = new Set<string>(); + private readonly pending = signal(0); + + constructor(private readonly report: (message: string) => void) {} + + run<T>(key: string, request: Observable<T>, onDone: (value: T) => void, failure: string): void { + if (this.keys.has(key)) return; + + this.keys.add(key); + this.pending.update((count) => count + 1); + request.subscribe({ + next: (value) => { + this.finish(key); + onDone(value); + }, + error: (err) => { + this.finish(key); + console.error('[InflightWrites] write failed', key, err); + this.report(failure); + }, + }); + } + + private finish(key: string): void { + this.keys.delete(key); + this.pending.update((count) => Math.max(0, count - 1)); + } +} diff --git a/src/app/core/keyboard.utils.ts b/src/app/core/keyboard.utils.ts new file mode 100644 index 000000000..f662a9a6f --- /dev/null +++ b/src/app/core/keyboard.utils.ts @@ -0,0 +1,10 @@ +/** True when the target owns its own arrow keys (a field being typed in), so global + * arrow-key handlers must leave the event alone. */ +export function isTypingTarget(target: EventTarget | null): boolean { + if (!(target instanceof HTMLElement)) { + return false; + } + + const tag = target.tagName; + return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || target.isContentEditable; +} diff --git a/src/app/core/kmeans-cluster-source.utils.spec.ts b/src/app/core/kmeans-cluster-source.utils.spec.ts new file mode 100644 index 000000000..c0f7cbd1d --- /dev/null +++ b/src/app/core/kmeans-cluster-source.utils.spec.ts @@ -0,0 +1,253 @@ +import { BackendExperiment } from '../models/backend-experiment.model'; +import { BackendFilter } from '../models/filters.model'; +import { + KMEANS_CLUSTER_CREATOR, + KMEANS_RESULT_TYPE, + KMeansSourceContext, + canonicalFilterKey, + extractReusablePreprocessing, + findKMeansReport, + findReusablePreprocessing, + kmeansCreatorClusterChoices, + kmeansCreatorVariables, + analysisSourceIsReusable, +} from './kmeans-cluster-source.utils'; + +function rule(field: string, operator: string, value: unknown): any { + return { id: `${field}-${operator}`, field, type: 'string', input: 'text', operator, value }; +} + +function group(condition: 'AND' | 'OR', rules: unknown[]): BackendFilter { + return { condition, rules: rules as BackendFilter['rules'] }; +} + +function experiment(overrides: { + algorithm?: string; + status?: string; + dataModel?: string; + datasets?: string[]; + filters?: BackendFilter | null; +}): BackendExperiment { + return { + uuid: 'uuid-1', + name: 'K-means run', + created: '2024-01-01T00:00:00Z', + finished: '2024-01-01T00:05:00Z', + shared: false, + viewed: false, + status: overrides.status ?? 'success', + analysis: { + inputdata: { + data_model: overrides.dataModel ?? 'Stroke:3.7', + datasets: overrides.datasets ?? ['diabetes', 'hypertension'], + filters: overrides.filters ?? null, + variables: ['age'], + }, + algorithm: { name: overrides.algorithm ?? 'kmeans' }, + }, + createdBy: { + username: 'u', + fullname: 'U', + email: 'u@example.org', + subjectId: 's', + agreeNDA: false, + }, + } as BackendExperiment; +} + +const current: KMeansSourceContext = { + dataModel: 'Stroke:3.7', + datasets: ['hypertension', 'diabetes'], + filters: group('AND', [rule('age', 'greater_or_equal', 18)]), +} satisfies KMeansSourceContext; + +describe('kmeans-cluster-source.utils', () => { + describe('canonicalFilterKey', () => { + it('ignores object key order', () => { + expect(canonicalFilterKey({ b: 1, a: { d: 2, c: 3 } })) + .toBe(canonicalFilterKey({ a: { c: 3, d: 2 }, b: 1 })); + }); + + it('keeps array order significant', () => { + expect(canonicalFilterKey([1, 2])).not.toBe(canonicalFilterKey([2, 1])); + }); + + it('treats undefined like null', () => { + expect(canonicalFilterKey(undefined)).toBe('null'); + expect(canonicalFilterKey(undefined)).toBe(canonicalFilterKey(null)); + }); + + it('drops undefined object values like request serialization does', () => { + expect(canonicalFilterKey({ a: 1, b: undefined })).toBe(canonicalFilterKey({ a: 1 })); + }); + }); + + describe('analysisSourceIsReusable', () => { + it('accepts a matching experiment regardless of dataset and filter key order', () => { + const source = experiment({ + datasets: ['diabetes', 'hypertension'], + filters: { rules: [rule('age', 'greater_or_equal', 18)], condition: 'AND' } as BackendFilter, + }); + + expect(analysisSourceIsReusable(source, 'kmeans', current)).toBe(true); + }); + + it('accepts a matching PCA experiment when algorithmName is pca', () => { + expect(analysisSourceIsReusable( + experiment({ algorithm: 'pca' }), + 'pca', + { ...current, filters: null }, + )).toBe(true); + expect(analysisSourceIsReusable(experiment({ algorithm: 'pca' }), 'kmeans', { ...current, filters: null })) + .toBe(false); + }); + + it('rejects a non-matching algorithm', () => { + expect(analysisSourceIsReusable(experiment({ algorithm: 'pca' }), 'kmeans', { ...current, filters: null })) + .toBe(false); + }); + + it('rejects a run that did not finish successfully', () => { + expect(analysisSourceIsReusable(experiment({ status: 'error' }), 'kmeans', { ...current, filters: null })) + .toBe(false); + }); + + it('rejects a different data model', () => { + expect(analysisSourceIsReusable(experiment({ dataModel: 'MIP:1.0' }), 'kmeans', { ...current, filters: null })) + .toBe(false); + }); + + it('rejects different datasets', () => { + expect(analysisSourceIsReusable(experiment({ datasets: ['diabetes'] }), 'kmeans', { ...current, filters: null })) + .toBe(false); + }); + + it('rejects different filters, including no source filters at all', () => { + expect(analysisSourceIsReusable(experiment({ filters: null }), 'kmeans', current)).toBe(false); + expect(analysisSourceIsReusable( + experiment({ filters: group('OR', [rule('sex', 'equal', '1')]) }), + 'kmeans', + current, + )).toBe(false); + }); + + it('does not throw when the analysis is incomplete', () => { + expect(analysisSourceIsReusable({ status: 'success' } as BackendExperiment, 'kmeans', current)).toBe(false); + expect(analysisSourceIsReusable( + { status: 'success', analysis: { algorithm: { name: 'kmeans' } } } as BackendExperiment, + 'kmeans', + current, + )).toBe(false); + }); + }); + + describe('extractReusablePreprocessing', () => { + it('returns the preprocessing of a privacy_safe_cluster_report result', () => { + const reusable = { + enumeration_prefix: 'cluster_', + preprocessing_name: KMEANS_CLUSTER_CREATOR, + cluster_variables: ['age', 'sex'], + centers: { '0': { age: 61.5, sex: 1 } }, + source_context: { + data_model: 'Stroke:3.7', + datasets: ['diabetes'], + input_fingerprint: 'abc', + }, + available_outputs: [{ code: 'cluster_id' }], + cluster_choices: [{ cluster_id: '0', label: 'Cluster 1' }], + }; + + expect(extractReusablePreprocessing({ + result_type: KMEANS_RESULT_TYPE, + reusable_preprocessing: reusable, + })).toBe(reusable); + // Legacy stored results predate the result_type marker. + expect(extractReusablePreprocessing({ reusable_preprocessing: reusable })).toBe(reusable); + }); + + it('rejects a different report type even when the preprocessing block looks reusable', () => { + const reusable = { + enumeration_prefix: 'cluster_', + preprocessing_name: KMEANS_CLUSTER_CREATOR, + cluster_variables: ['age'], + centers: { '0': { age: 61.5 } }, + source_context: { data_model: 'Stroke:3.7', datasets: ['diabetes'], input_fingerprint: 'abc' }, + available_outputs: [], + cluster_choices: [{ cluster_id: '0', label: 'Cluster 1' }], + }; + + expect(extractReusablePreprocessing({ + result_type: 'privacy_safe_outlier_report', + reusable_preprocessing: reusable, + })).toBeNull(); + }); + + it('returns null for missing or malformed preprocessing', () => { + expect(extractReusablePreprocessing({ centers: [[1, 2]] })).toBeNull(); + expect(extractReusablePreprocessing(null)).toBeNull(); + expect(extractReusablePreprocessing({ reusable_preprocessing: null })).toBeNull(); + }); + + it('finds the reusable block in nested transient envelopes', () => { + const reusable = { + enumeration_prefix: 'cluster_', + preprocessing_name: KMEANS_CLUSTER_CREATOR, + cluster_variables: ['age'], + centers: { c1: { age: 40 } }, + source_context: { data_model: 'Stroke:3.7', datasets: ['diabetes'], input_fingerprint: 'fp' }, + available_outputs: [], + cluster_choices: [{ cluster_id: 'c1', label: 'Cluster 1' }], + }; + const report = { result_type: KMEANS_RESULT_TYPE, reusable_preprocessing: reusable, clusters: [] }; + + expect(findReusablePreprocessing({ result: report })).toBe(reusable); + expect(findReusablePreprocessing({ result: { result: report } })).toBe(reusable); + }); + + it('finds a nested K-means report for display', () => { + const report = { selected_k: 2, clusters: [{ cluster_id: 'c1', label: 'Cluster 1' }] }; + expect(findKMeansReport({ result: { result: report } })).toBe(report); + expect(findKMeansReport({ result: { message: 'nope' } })).toBeNull(); + }); + }); + + describe('kmeansCreatorVariables / kmeansCreatorClusterChoices', () => { + it('reads recipe cluster_variables and cluster_choices', () => { + const creator = { + reusable_preprocessing: { + cluster_variables: ['age', 'bmi'], + cluster_choices: [ + { cluster_id: 'cluster_0', label: 'Cluster 0' }, + { cluster_id: 'cluster_1', label: 'Cluster 1' }, + ], + }, + }; + expect(kmeansCreatorVariables(creator)).toEqual(['age', 'bmi']); + expect(kmeansCreatorClusterChoices(creator)).toEqual([ + { cluster_id: 'cluster_0', label: 'Cluster 0' }, + { cluster_id: 'cluster_1', label: 'Cluster 1' }, + ]); + }); + + it('falls back to fit-mode variables and synthetic cluster_1..k', () => { + const creator = { + variables: ['age', 'bmi'], + k: 3, + enumeration_prefix: 'cluster_', + }; + expect(kmeansCreatorVariables(creator)).toEqual(['age', 'bmi']); + expect(kmeansCreatorClusterChoices(creator)).toEqual([ + { cluster_id: 'cluster_1', label: 'cluster_1' }, + { cluster_id: 'cluster_2', label: 'cluster_2' }, + { cluster_id: 'cluster_3', label: 'cluster_3' }, + ]); + }); + + it('defaults enumeration_prefix to cluster_ when synthesizing', () => { + expect(kmeansCreatorClusterChoices({ variables: ['age'], k: 2 })).toEqual([ + { cluster_id: 'cluster_1', label: 'cluster_1' }, + { cluster_id: 'cluster_2', label: 'cluster_2' }, + ]); + }); + }); +}); diff --git a/src/app/core/kmeans-cluster-source.utils.ts b/src/app/core/kmeans-cluster-source.utils.ts new file mode 100644 index 000000000..893ac38d3 --- /dev/null +++ b/src/app/core/kmeans-cluster-source.utils.ts @@ -0,0 +1,175 @@ +import { BackendExperiment } from '../models/backend-experiment.model'; +import { BackendFilter } from '../models/filters.model'; + +/** Name the backend puts in reusable_preprocessing.preprocessing_name for K-means clusters. */ +export const KMEANS_CLUSTER_CREATOR = 'kmeans_cluster_creator'; + +/** Result type of the K-means cluster report. Its reusable_preprocessing is the creator input. */ +export const KMEANS_RESULT_TYPE = 'privacy_safe_cluster_report'; + +/** Preprocessing block stored in a K-means result that can be replayed as a cluster column. + * Matches exaflow `KMeansReusablePreprocessing` (no schema_version, has enumeration_prefix). */ +export interface KMeansReusablePreprocessing { + enumeration_prefix: string; + preprocessing_name: string; + cluster_variables: string[]; + centers: Record<string, Record<string, number>>; + source_context: { data_model: string; datasets: string[]; input_fingerprint: string }; + available_outputs: Record<string, unknown>[]; + cluster_choices: { cluster_id: string; label: string }[]; +} + +/** Clustering variable codes from a stored creator: recipe `cluster_variables` or fit `variables`. */ +export function kmeansCreatorVariables( + creator: Record<string, unknown> | null | undefined, +): string[] { + const reusable = creator?.['reusable_preprocessing']; + if (isPlainObject(reusable) && Array.isArray(reusable['cluster_variables'])) { + return reusable['cluster_variables'].map((code) => String(code).trim()).filter((code) => !!code); + } + const variables = creator?.['variables']; + return Array.isArray(variables) + ? variables.map((code) => String(code).trim()).filter((code) => !!code) + : []; +} + +/** + * Cluster category ids/labels: recipe `cluster_choices`, or synthetic + * `${enumeration_prefix || 'cluster_'}${1..k}` for fit mode. + */ +export function kmeansCreatorClusterChoices( + creator: Record<string, unknown> | null | undefined, +): Array<{ cluster_id: string; label: string }> { + const reusable = creator?.['reusable_preprocessing']; + if (isPlainObject(reusable) && Array.isArray(reusable['cluster_choices'])) { + return reusable['cluster_choices'] + .filter(isPlainObject) + .map((choice) => ({ + cluster_id: String(choice['cluster_id'] ?? '').trim(), + label: String(choice['label'] ?? '').trim(), + })) + .filter((choice) => !!choice.cluster_id); + } + const k = typeof creator?.['k'] === 'number' ? creator['k'] : 0; + if (!Number.isFinite(k) || k < 1) return []; + const prefix = typeof creator?.['enumeration_prefix'] === 'string' && creator['enumeration_prefix'] + ? creator['enumeration_prefix'] + : 'cluster_'; + return Array.from({ length: Math.floor(k) }, (_, index) => { + const id = `${prefix}${index + 1}`; + return { cluster_id: id, label: id }; + }); +} + +/** Source of the data the current draft experiment runs on. */ +export interface KMeansSourceContext { + dataModel: string | null; + datasets: string[]; + filters: BackendFilter | null | undefined; +} + +/** + * Stable JSON stringification: object keys sorted recursively, array order kept. + * Mirrors Exaflow's fingerprint (json.dumps(..., sort_keys=True)), which ignores + * key order but not list order. Undefined object values are dropped, as they are + * when the request is serialized; a top-level `undefined` counts as `null`. + */ +export function canonicalFilterKey(filters: unknown): string { + return JSON.stringify(sortKeys(filters)); +} + +function sortKeys(value: unknown): unknown { + if (Array.isArray(value)) return value.map(sortKeys); + if (value && typeof value === 'object') { + return Object.fromEntries( + Object.entries(value as Record<string, unknown>) + .filter(([, item]) => item !== undefined) + .sort(([a], [b]) => (a < b ? -1 : 1)) + .map(([key, item]) => [key, sortKeys(item)]) + ); + } + return value ?? null; +} + +/** + * Whether a finished analysis run can still provide a recipe for the current draft: + * matching algorithm, same data model, same datasets, same cohort filters. + */ +export function analysisSourceIsReusable( + experiment: BackendExperiment, + algorithmName: string, + current: KMeansSourceContext, +): boolean { + const analysis = experiment?.analysis as unknown as + | { algorithm?: { name?: unknown }; inputdata?: Record<string, unknown> } + | undefined; + const inputdata = analysis?.inputdata; + if (analysis?.algorithm?.name !== algorithmName || experiment?.status !== 'success') return false; + if (!inputdata || inputdata['data_model'] !== current.dataModel) return false; + + const sourceDatasets = (Array.isArray(inputdata['datasets']) ? inputdata['datasets'] : []) + .map(String) + .sort(); + return sourceDatasets.join('\u0000') === [...current.datasets].map(String).sort().join('\u0000') + && canonicalFilterKey(inputdata['filters']) === canonicalFilterKey(current.filters); +} + +/** + * Read the replayable preprocessing out of a stored K-means result. Returns null + * for results saved before the feature existed or with an unexpected shape. + */ +export function extractReusablePreprocessing(result: unknown): KMeansReusablePreprocessing | null { + const record = result as { result_type?: unknown; reusable_preprocessing?: unknown } | null | undefined; + // The report contract is privacy_safe_cluster_report. Legacy results without the + // marker are still accepted; a different declared type is never reusable. + if (record?.result_type !== undefined && record.result_type !== KMEANS_RESULT_TYPE) { + return null; + } + const candidate = record?.reusable_preprocessing; + + if (!isPlainObject(candidate)) { + return null; + } + // The creator contract names the block; tolerate legacy blocks that lack the name. + if (candidate['preprocessing_name'] !== undefined + && candidate['preprocessing_name'] !== KMEANS_CLUSTER_CREATOR) { + return null; + } + if (!Array.isArray(candidate['cluster_variables']) || !Array.isArray(candidate['cluster_choices']) + || !isPlainObject(candidate['centers'])) { + return null; + } + // The creator names the cluster categories with this prefix; a block without it cannot replay. + if (typeof candidate['enumeration_prefix'] !== 'string' || !candidate['enumeration_prefix']) { + return null; + } + + return candidate as unknown as KMeansReusablePreprocessing; +} + +/** Where a transient response can carry the report: the response itself, the backend's + * `result`, or a stored `{ result }` nested inside it. */ +function reportCandidates(value: unknown): unknown[] { + const outer = (value as { result?: unknown } | null | undefined)?.result; + return [value, outer, (outer as { result?: unknown } | null | undefined)?.result]; +} + +/** The reusable block of a transient K-means response, or null. */ +export function findReusablePreprocessing(value: unknown): KMeansReusablePreprocessing | null { + for (const candidate of reportCandidates(value)) { + const found = extractReusablePreprocessing(candidate); + if (found) return found; + } + return null; +} + +/** The cluster report of a transient K-means response, for display. */ +export function findKMeansReport(value: unknown): Record<string, unknown> | null { + const found = reportCandidates(value).find((candidate) => + isPlainObject(candidate) && Array.isArray(candidate['clusters']) && candidate['selected_k'] !== undefined); + return (found as Record<string, unknown> | undefined) ?? null; +} + +export function isPlainObject(value: unknown): value is Record<string, unknown> { + return !!value && typeof value === 'object' && !Array.isArray(value); +} diff --git a/src/app/core/observe-resize.util.ts b/src/app/core/observe-resize.util.ts new file mode 100644 index 000000000..683264fb2 --- /dev/null +++ b/src/app/core/observe-resize.util.ts @@ -0,0 +1,38 @@ +import { NgZone } from '@angular/core'; + +/** + * Debounced ResizeObserver: `onSize` runs once the box stops changing, back inside the + * Angular zone. A zero side means the element is hidden (a Studio step folded with + * `display: none`), which each caller treats as "not a size worth drawing for". + * Returns the teardown. + */ +export function observeSize( + element: Element, + ngZone: NgZone, + debounceMs: number, + onSize: (width: number, height: number) => void +): () => void { + if (typeof ResizeObserver === 'undefined') { + return () => {}; + } + + let timer: ReturnType<typeof setTimeout> | undefined; + let observer: ResizeObserver | undefined; + + ngZone.runOutsideAngular(() => { + observer = new ResizeObserver((entries) => { + const box = entries[0]?.contentRect; + const width = Math.floor(box?.width ?? 0); + const height = Math.floor(box?.height ?? 0); + + clearTimeout(timer); + timer = setTimeout(() => ngZone.run(() => onSize(width, height)), debounceMs); + }); + observer.observe(element); + }); + + return () => { + clearTimeout(timer); + observer?.disconnect(); + }; +} diff --git a/src/app/core/outlier-rules.ts b/src/app/core/outlier-rules.ts new file mode 100644 index 000000000..cc41934e3 --- /dev/null +++ b/src/app/core/outlier-rules.ts @@ -0,0 +1,169 @@ +import { isNumericalVariableType } from './data-model.utils'; +export type OutlierStrategy = 'gaussian' | 'iqr' | 'mad' | 'quantile'; +export type OutlierTail = 'left' | 'right' | 'both'; + +export interface OutlierRule { + variableCode: string; + enabled: boolean; + strategy: OutlierStrategy; + tail: OutlierTail; + fold: number | null; +} + +interface OutlierVariable { + code: string; + label?: string; + name?: string; + type?: string; + enumerations?: unknown[]; +} + +interface SerializedOutlierRules { + strategies: Record<string, OutlierStrategy>; + tails: Record<string, OutlierTail>; + folds: Record<string, number>; +} + +export const OUTLIER_STRATEGIES: Array<{ value: OutlierStrategy; label: string }> = [ + { value: 'gaussian', label: 'Gaussian' }, + { value: 'iqr', label: 'IQR' }, + { value: 'mad', label: 'MAD' }, + { value: 'quantile', label: 'Quantile' }, +]; + +export const OUTLIER_TAILS: Array<{ value: OutlierTail; label: string }> = [ + { value: 'left', label: 'Left' }, + { value: 'right', label: 'Right' }, + { value: 'both', label: 'Both' }, +]; + +const OUTLIER_DEFAULT_FOLDS: Record<OutlierStrategy, number> = { + gaussian: 3.0, + iqr: 1.5, + mad: 3.0, + quantile: 0.05, +}; + +function isOutlierStrategy(value: unknown): value is OutlierStrategy { + return value === 'gaussian' || value === 'iqr' || value === 'mad' || value === 'quantile'; +} + +function isOutlierTail(value: unknown): value is OutlierTail { + return value === 'left' || value === 'right' || value === 'both'; +} + +export function isOutlierEligibleVariable(variable: OutlierVariable): boolean { + if (!variable?.code) return false; + if (Array.isArray(variable.enumerations) && variable.enumerations.length > 0) return false; + return isNumericalVariableType(variable.type); +} + +export function createDefaultOutlierRule(variableCode: string, enabled = false): OutlierRule { + const strategy: OutlierStrategy = 'iqr'; + return { + variableCode, + enabled, + strategy, + tail: 'both', + fold: OUTLIER_DEFAULT_FOLDS[strategy], + }; +} + +export function defaultFoldForStrategy(strategy: OutlierStrategy): number { + return OUTLIER_DEFAULT_FOLDS[strategy]; +} + +export function validateOutlierRule(rule: OutlierRule): string | null { + if (!rule.enabled) return null; + const fold = rule.fold; + if (fold === null || fold === undefined || !Number.isFinite(Number(fold))) { + return 'A fold value is required.'; + } + if (rule.strategy === 'quantile') { + return fold > 0 && fold < 0.5 ? null : 'Quantile fold must be greater than 0 and less than 0.5.'; + } + return fold > 0 ? null : 'Fold must be greater than 0.'; +} + +export function serializeOutlierRules( + rules: Record<string, OutlierRule>, + allowedCodes?: Set<string> +): SerializedOutlierRules | null { + const strategies: Record<string, OutlierStrategy> = {}; + const tails: Record<string, OutlierTail> = {}; + const folds: Record<string, number> = {}; + + Object.values(rules).forEach((rule) => { + if (allowedCodes && !allowedCodes.has(rule.variableCode)) return; + if (!rule.enabled) return; + const error = validateOutlierRule(rule); + if (error) return; + strategies[rule.variableCode] = rule.strategy; + tails[rule.variableCode] = rule.tail; + folds[rule.variableCode] = Number(rule.fold); + }); + + return Object.keys(strategies).length ? { strategies, tails, folds } : null; +} + +export function cloneOutlierRules(rules: Record<string, OutlierRule>): Record<string, OutlierRule> { + return Object.fromEntries( + Object.entries(rules).map(([code, rule]) => [code, { ...rule }]) + ); +} + +export function serializeOutlierRule(rule: OutlierRule | undefined): string { + const normalized = rule ?? createDefaultOutlierRule(''); + return JSON.stringify({ + enabled: normalized.enabled, + strategy: normalized.strategy, + tail: normalized.tail, + fold: normalized.fold, + }); +} + +export function hydrateOutlierRules( + config: unknown, + allowedCodes: Set<string> +): Record<string, OutlierRule> { + if (!config || typeof config !== 'object' || Array.isArray(config)) return {}; + + const payload = config as { + strategies?: Record<string, unknown>; + tails?: Record<string, unknown>; + folds?: Record<string, unknown>; + }; + const strategies = payload.strategies ?? {}; + const tails = payload.tails ?? {}; + const folds = payload.folds ?? {}; + const hydrated: Record<string, OutlierRule> = {}; + + allowedCodes.forEach((code) => { + const strategyValue = strategies[code]; + if (!isOutlierStrategy(strategyValue)) return; + const tailValue = tails[code]; + const rawFold = folds[code]; + const parsedFold = rawFold === null || rawFold === undefined || rawFold === '' + ? null + : Number(rawFold); + hydrated[code] = { + variableCode: code, + enabled: true, + strategy: strategyValue, + tail: isOutlierTail(tailValue) ? tailValue : 'both', + fold: typeof parsedFold === 'number' && Number.isFinite(parsedFold) + ? parsedFold + : defaultFoldForStrategy(strategyValue), + }; + }); + + return hydrated; +} + +export function outlierStrategyLabel(strategy: OutlierStrategy | string): string { + return OUTLIER_STRATEGIES.find((item) => item.value === strategy)?.label ?? String(strategy).replace(/_/g, ' '); +} + +export function outlierTailLabel(tail: OutlierTail | string): string { + return OUTLIER_TAILS.find((item) => item.value === tail)?.label.toLowerCase() ?? String(tail).replace(/_/g, ' '); +} diff --git a/src/app/core/pca-column-source.utils.spec.ts b/src/app/core/pca-column-source.utils.spec.ts new file mode 100644 index 000000000..fefc8c577 --- /dev/null +++ b/src/app/core/pca-column-source.utils.spec.ts @@ -0,0 +1,67 @@ +import { + PCAReusablePreprocessing, + buildPcaCreatorParameters, + defaultPcaColumnNames, + extractPcaRecipe, +} from './pca-column-source.utils'; + +const R: PCAReusablePreprocessing = { + schema_version: '1', + preprocessing_name: 'pca_column_creator', + base_parameters: { + variables: ['lefthippocampus', 'righthippocampus'], + pca_variant: 'pca', + }, + component_choices: [ + { component_id: 'PC1', explained_variance_ratio: 0.855 }, + { component_id: 'PC2', explained_variance_ratio: 0.145 }, + ], +}; + +describe('pca-column-source.utils', () => { + describe('buildPcaCreatorParameters', () => { + it('builds recipe-mode parameters with n_components from the selection', () => { + expect(buildPcaCreatorParameters({ + recipe: R, + selected: ['PC1', 'PC3'], + names: ['pca_pc1', 'pca_pc3'], + })).toEqual({ + reusable_preprocessing: R, + selected_components: ['PC1', 'PC3'], + new_column_names: ['pca_pc1', 'pca_pc3'], + n_components: 2, + }); + }); + + it('builds fit-mode parameters without reusable_preprocessing', () => { + expect(buildPcaCreatorParameters({ + variables: ['a', 'b'], + selected: ['PC1'], + names: ['pca_pc1'], + })).not.toHaveProperty('reusable_preprocessing'); + }); + }); + + describe('extractPcaRecipe', () => { + it('returns null when schema_version is not 1', () => { + expect(extractPcaRecipe({ reusable_preprocessing: { ...R, schema_version: '2' } })).toBeNull(); + }); + + it('returns the recipe when schema_version is 1 and the name matches', () => { + expect(extractPcaRecipe({ reusable_preprocessing: R })).toBe(R); + }); + + it('returns null when preprocessing_name is wrong', () => { + expect(extractPcaRecipe({ + reusable_preprocessing: { ...R, preprocessing_name: 'kmeans_cluster_creator' }, + })).toBeNull(); + }); + }); + + describe('defaultPcaColumnNames', () => { + it('joins prefix and lowercased component ids', () => { + expect(defaultPcaColumnNames('pca', ['PC1', 'PC2'])).toEqual(['pca_pc1', 'pca_pc2']); + expect(defaultPcaColumnNames('pca2', ['PC1'])).toEqual(['pca2_pc1']); + }); + }); +}); diff --git a/src/app/core/pca-column-source.utils.ts b/src/app/core/pca-column-source.utils.ts new file mode 100644 index 000000000..b9b38cca0 --- /dev/null +++ b/src/app/core/pca-column-source.utils.ts @@ -0,0 +1,90 @@ +/** Name the backend puts in reusable_preprocessing.preprocessing_name for PCA columns. */ +export const PCA_COLUMN_CREATOR = 'pca_column_creator'; + +/** Preprocessing block stored in a PCA result that can be replayed as component columns. + * Matches exaflow `PCAReusablePreprocessing` (schema_version "1"). */ +export interface PCAReusablePreprocessing { + schema_version: string; + preprocessing_name: string; + base_parameters: { variables: string[]; pca_variant: string }; + component_choices: { component_id: string; explained_variance_ratio: number }[]; +} + +/** + * Read the replayable PCA preprocessing out of a stored result. Returns null unless + * `preprocessing_name === 'pca_column_creator'` and `schema_version === '1'`. + */ +export function extractPcaRecipe(result: unknown): PCAReusablePreprocessing | null { + const record = result as { reusable_preprocessing?: unknown } | null | undefined; + const candidate = record?.reusable_preprocessing; + if (!candidate || typeof candidate !== 'object' || Array.isArray(candidate)) return null; + const recipe = candidate as Record<string, unknown>; + if (recipe['preprocessing_name'] !== PCA_COLUMN_CREATOR) return null; + if (recipe['schema_version'] !== '1') return null; + if (!isPlainObject(recipe['base_parameters'])) return null; + if (!Array.isArray(recipe['component_choices'])) return null; + return candidate as PCAReusablePreprocessing; +} + +/** Default column codes: `${prefix}_${componentId.toLowerCase()}` (prefix defaults to `pca` in the UI). */ +export function defaultPcaColumnNames(prefix: string, componentIds: string[]): string[] { + return componentIds.map((id) => `${prefix}_${id.toLowerCase()}`); +} + +/** + * Parameters for one `pca_column_creator` step. Always sets `n_components = selected.length` + * and exactly one of `reusable_preprocessing` / `variables`. + */ +export function buildPcaCreatorParameters(input: { + recipe?: PCAReusablePreprocessing; + variables?: string[]; + selected: string[]; + names: string[]; +}): Record<string, unknown> { + const base: Record<string, unknown> = { + selected_components: input.selected, + new_column_names: input.names, + n_components: input.selected.length, + }; + if (input.recipe) { + return { ...base, reusable_preprocessing: input.recipe }; + } + return { ...base, variables: input.variables ?? [] }; +} + +/** Where a transient response can carry the recipe: the response itself, `result`, or nested `{ result }`. */ +function recipeCandidates(value: unknown): unknown[] { + const outer = (value as { result?: unknown } | null | undefined)?.result; + return [value, outer, (outer as { result?: unknown } | null | undefined)?.result]; +} + +/** The reusable PCA block of a transient response, or null. */ +export function findPcaRecipe(value: unknown): PCAReusablePreprocessing | null { + for (const candidate of recipeCandidates(value)) { + const found = extractPcaRecipe(candidate); + if (found) return found; + } + return null; +} + +/** + * Next column-name prefix for a PCA card (`pca`, `pca2`, `pca3`, …), skipping prefixes + * already used by other applied `pca_column_creator` steps. + */ +export function nextPcaColumnPrefix(usedNames: string[]): string { + const taken = new Set<string>(); + for (const name of usedNames) { + const match = /^(.+)_pc\d+$/i.exec(String(name).trim()); + if (match) taken.add(match[1].toLowerCase()); + } + let n = 1; + while (true) { + const prefix = n === 1 ? 'pca' : `pca${n}`; + if (!taken.has(prefix)) return prefix; + n += 1; + } +} + +function isPlainObject(value: unknown): value is Record<string, unknown> { + return !!value && typeof value === 'object' && !Array.isArray(value); +} diff --git a/src/app/core/preprocessing-outputs.utils.spec.ts b/src/app/core/preprocessing-outputs.utils.spec.ts new file mode 100644 index 000000000..5dc7a1d57 --- /dev/null +++ b/src/app/core/preprocessing-outputs.utils.spec.ts @@ -0,0 +1,209 @@ +import { describe, it, expect } from 'vitest'; +import { + PreprocessingOutputs, + resolvePreprocessingOutputs, +} from './preprocessing-outputs.utils'; + +const PCA_OUTPUTS: PreprocessingOutputs = { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'real', + stattype: 'numerical', + column_count_param: 'n_components', + enumerations: null, + }, + ], +}; + +const CATEGORICAL_OUTPUTS: PreprocessingOutputs = { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'text', + stattype: 'nominal', + column_count_param: null, + enumerations: { + source_parameter_name: 'rules', + source_type: 'dict_keys', + additional_value_parameter_name: 'default_enumeration', + prefix_parameter_name: null, + }, + }, + ], +}; + +const KMEANS_OUTPUTS: PreprocessingOutputs = { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'int', + stattype: 'nominal', + column_count_param: null, + enumerations: { + source_parameter_name: 'k', + source_type: 'prefixed_range', + additional_value_parameter_name: null, + prefix_parameter_name: 'enumeration_prefix', + }, + }, + ], +}; + +describe('resolvePreprocessingOutputs', () => { + it('expands a PCA block by n_components', () => { + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + n_components: 2, + new_column_names: ['a', 'b'], + }), + ).toEqual([ + { code: 'a', type: 'real', stattype: 'numerical', enumerations: [] }, + { code: 'b', type: 'real', stattype: 'numerical', enumerations: [] }, + ]); + }); + + it('builds nominal categories from rules plus default', () => { + const cols = resolvePreprocessingOutputs(CATEGORICAL_OUTPUTS, { + new_column_names: ['grp'], + rules: { lo: {}, hi: {} }, + default_enumeration: 'other', + })!; + expect(cols[0].enumerations.map((e) => e.code)).toEqual(['lo', 'hi', 'other']); + expect(cols[0].enumerations.map((e) => e.label)).toEqual(['lo', 'hi', 'other']); + }); + + it('builds prefixed K-means categories', () => { + const cols = resolvePreprocessingOutputs(KMEANS_OUTPUTS, { + new_column_names: ['km'], + k: 3, + enumeration_prefix: 'cluster_', + })!; + expect(cols[0].enumerations.map((e) => e.code)).toEqual([ + 'cluster_1', + 'cluster_2', + 'cluster_3', + ]); + }); + + it('returns null on a name-count mismatch', () => { + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + n_components: 2, + new_column_names: ['a'], + }), + ).toBeNull(); + }); + + it('returns null when a repeat count is missing or not a positive integer', () => { + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + new_column_names: ['a', 'b'], + }), + ).toBeNull(); + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + n_components: 0, + new_column_names: ['a', 'b'], + }), + ).toBeNull(); + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + n_components: true, + new_column_names: ['a', 'b'], + }), + ).toBeNull(); + }); + + it('returns null on a blank column name', () => { + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + n_components: 2, + new_column_names: ['a', ' '], + }), + ).toBeNull(); + }); + + it('returns null on duplicate column names', () => { + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + n_components: 2, + new_column_names: ['a', 'a'], + }), + ).toBeNull(); + }); + + it('returns null when the names are not an array', () => { + expect( + resolvePreprocessingOutputs(PCA_OUTPUTS, { + n_components: 2, + new_column_names: 'ab', + }), + ).toBeNull(); + }); + + it('returns null when a dict_keys enumeration source is empty', () => { + expect( + resolvePreprocessingOutputs(CATEGORICAL_OUTPUTS, { + new_column_names: ['grp'], + rules: {}, + }), + ).toBeNull(); + }); + + it('omits the additional value when it is null', () => { + const cols = resolvePreprocessingOutputs(CATEGORICAL_OUTPUTS, { + new_column_names: ['grp'], + rules: { lo: {} }, + default_enumeration: null, + })!; + expect(cols[0].enumerations.map((e) => e.code)).toEqual(['lo']); + }); + + it('returns null when a prefixed_range source or prefix is missing', () => { + expect( + resolvePreprocessingOutputs(KMEANS_OUTPUTS, { + new_column_names: ['km'], + enumeration_prefix: 'cluster_', + }), + ).toBeNull(); + expect( + resolvePreprocessingOutputs(KMEANS_OUTPUTS, { + new_column_names: ['km'], + k: 2, + }), + ).toBeNull(); + }); + + it('expands multiple columns with mixed repeat counts in order', () => { + const multi: PreprocessingOutputs = { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'real', + stattype: 'numerical', + column_count_param: 'n_components', + enumerations: null, + }, + { + type: 'text', + stattype: 'nominal', + column_count_param: null, + enumerations: { + source_parameter_name: 'rules', + source_type: 'dict_keys', + additional_value_parameter_name: null, + prefix_parameter_name: null, + }, + }, + ], + }; + const cols = resolvePreprocessingOutputs(multi, { + n_components: 2, + new_column_names: ['a', 'b', 'grp'], + rules: { lo: {}, hi: {} }, + })!; + expect(cols.map((c) => c.code)).toEqual(['a', 'b', 'grp']); + expect(cols[2].enumerations.map((e) => e.code)).toEqual(['lo', 'hi']); + }); +}); diff --git a/src/app/core/preprocessing-outputs.utils.ts b/src/app/core/preprocessing-outputs.utils.ts new file mode 100644 index 000000000..58717bbc7 --- /dev/null +++ b/src/app/core/preprocessing-outputs.utils.ts @@ -0,0 +1,149 @@ +/** + * Preprocessing `outputs` contract and the generic column resolver. + * + * Ports `resolve_output_metadata` from exaflow + * `exaflow/algorithms/utils/output_specifications.py`. Unlike the Python + * worker, this returns `null` for incomplete parameters instead of raising. + */ + +export interface PreprocessingOutputEnumerations { + source_parameter_name: string; + source_type: 'dict_keys' | 'prefixed_range'; + additional_value_parameter_name: string | null; + prefix_parameter_name: string | null; +} + +export interface PreprocessingOutputColumn { + type: 'text' | 'int' | 'real'; + stattype: 'nominal' | 'numerical'; + column_count_param: string | null; + enumerations: PreprocessingOutputEnumerations | null; +} + +export interface PreprocessingOutputs { + new_column_names_param: string; + columns: PreprocessingOutputColumn[]; +} + +/** Resolved view of a single derived output column. */ +export interface DerivedColumn { + code: string; + type: string; + stattype: 'nominal' | 'numerical'; + enumerations: { code: string; label: string }[]; +} + +/** Positive integer count; accepts integer-valued floats, rejects bools/0/negatives. */ +function positiveCount(source: unknown): number | null { + if (typeof source !== 'number' || !Number.isInteger(source) || source < 1) { + return null; + } + return source; +} + +export function resolvePreprocessingOutputs( + outputs: PreprocessingOutputs, + parameters: Record<string, unknown>, +): DerivedColumn[] | null { + if (outputs === null || outputs === undefined) { + return null; + } + + // Expand each column by its repeat count, validating counts in order. + const widths: number[] = []; + for (const column of outputs.columns) { + if (column.column_count_param === null || column.column_count_param === undefined) { + widths.push(1); + } else { + const count = positiveCount(parameters[column.column_count_param]); + if (count === null) { + return null; + } + widths.push(count); + } + } + const outputCount = widths.reduce((sum, width) => sum + width, 0); + + const names = parameters[outputs.new_column_names_param]; + if ( + !Array.isArray(names) || + names.length !== outputCount || + names.some((name) => typeof name !== 'string' || !name.trim()) + ) { + return null; + } + + // Ported from Exaflow: new column names must be unique. + if (new Set(names).size !== names.length) { + return null; + } + + const expanded: PreprocessingOutputColumn[] = []; + for (let i = 0; i < outputs.columns.length; i++) { + for (let w = 0; w < widths[i]; w++) { + expanded.push(outputs.columns[i]); + } + } + + const resolved: DerivedColumn[] = []; + for (let i = 0; i < names.length; i++) { + const name = names[i] as string; + const column = expanded[i]; + const derived: DerivedColumn = { + code: name, + type: column.type, + stattype: column.stattype, + enumerations: [], + }; + + if (column.stattype === 'nominal') { + const enums = column.enumerations; + if (enums === null || enums === undefined) { + return null; + } + const source = parameters[enums.source_parameter_name]; + let categories: unknown[]; + + if (enums.source_type === 'dict_keys') { + if ( + typeof source !== 'object' || + source === null || + Array.isArray(source) || + Object.keys(source as Record<string, unknown>).length === 0 + ) { + return null; + } + categories = Object.keys(source as Record<string, unknown>); + if (enums.additional_value_parameter_name !== null && enums.additional_value_parameter_name !== undefined) { + const additional = parameters[enums.additional_value_parameter_name]; + if (additional !== null && additional !== undefined) { + categories.push(additional); + } + } + } else { + const prefixParam = enums.prefix_parameter_name; + const prefix = prefixParam === null ? null : parameters[prefixParam]; + const count = positiveCount(source); + if (count === null || typeof prefix !== 'string') { + return null; + } + categories = []; + for (let index = 1; index <= count; index++) { + categories.push(`${prefix}${index}`); + } + } + + if (categories.some((category) => typeof category !== 'string' || !category.trim())) { + return null; + } + derived.enumerations = categories.map((category) => { + const code = String(category); + return { code, label: code }; + }); + } + + resolved.push(derived); + } + + return resolved; +} diff --git a/src/app/core/result-label.utils.spec.ts b/src/app/core/result-label.utils.spec.ts new file mode 100644 index 000000000..8de8f8861 --- /dev/null +++ b/src/app/core/result-label.utils.spec.ts @@ -0,0 +1,26 @@ +import { enrichPcaResult } from './result-label.utils'; + +describe('enrichPcaResult', () => { + const labelled = [ + { code: 'age', label: 'Age' }, + { code: 'bmi', label: 'BMI' }, + ]; + + it('labels the result variables in the order the result lists them', () => { + const result = enrichPcaResult({ variables: ['bmi', 'age'] }, 'pca', labelled); + + expect((result as any).variable_names).toEqual(['BMI', 'Age']); + }); + + it('leaves a result without variables unchanged', () => { + const result = { eigenvalues: [1, 0.5] }; + + expect(enrichPcaResult(result, 'pca', labelled)).toBe(result); + }); + + it('leaves other algorithms unchanged', () => { + const result = { variables: ['age'] }; + + expect(enrichPcaResult(result, 'kmeans', labelled)).toBe(result); + }); +}); diff --git a/src/app/core/result-label.utils.ts b/src/app/core/result-label.utils.ts new file mode 100644 index 000000000..c471086a2 --- /dev/null +++ b/src/app/core/result-label.utils.ts @@ -0,0 +1,57 @@ +/** Shared helpers for labeling and enriching experiment results in the dashboard. */ + +export interface LabeledItem { + code: string; + label: string; +} + +/** + * Map a list of codes to `{ code, label }` pairs, falling back to the raw code + * when the label map has no entry for it. + */ +export function withLabels( + codes: string[] | undefined | null, + labelMap: Record<string, string> +): LabeledItem[] { + return (codes ?? []).map((code) => ({ code, label: labelMap[code] ?? code })); +} + +/** + * Inject human-readable variable names into a saved PCA result so the heatmap and the weights + * table do not show raw codes. Labels the result's own `variables` (request order). + */ +export function enrichPcaResult<T>(result: T, algorithmName: string, variables: LabeledItem[]): T { + if (algorithmName !== 'pca' || !result || typeof result !== 'object') return result; + const codes = (result as { variables?: unknown }).variables; + if (!Array.isArray(codes) || !codes.length) return result; + const names = codes.map((code) => variables.find((item) => item.code === String(code))?.label ?? String(code)); + return { ...result, variable_names: names }; +} + +/** `3 datasets`, `1 dataset`. */ +export function pluralize(count: number, singular: string): string { + return `${count} ${singular}${count === 1 ? '' : 's'}`; +} + +/** `y ~ x1, x2`, or the plain list when either side is empty. */ +export function formulaLine(y: string[], x: string[], xSeparator = ', '): string { + if (y.length && x.length) return `${y.join(', ')} ~ ${x.join(xSeparator)}`; + return [...y, ...x].join(', '); +} + +/** A result's `n_obs` as a count grouped like the result figures (27 308, narrow no-break space), + * or '' when absent or not a positive number. */ +export function rowsUsedLabel(nObs: unknown): string { + if (typeof nObs !== 'number' || !Number.isFinite(nObs) || nObs <= 0) return ''; + return nObs.toLocaleString('en-US').replace(/,/g, '\u202f'); +} + +/** Top-level `n_obs`, or the first nested one (one-way ANOVA keeps it on `anova_table`). */ +export function observationCount(result: unknown): string { + if (!result || typeof result !== 'object') return ''; + for (const value of [result, ...Object.values(result)]) { + const label = value && typeof value === 'object' ? rowsUsedLabel((value as { n_obs?: unknown }).n_obs) : ''; + if (label) return label; + } + return ''; +} diff --git a/src/app/core/route-path.utils.spec.ts b/src/app/core/route-path.utils.spec.ts new file mode 100644 index 000000000..fb25719a3 --- /dev/null +++ b/src/app/core/route-path.utils.spec.ts @@ -0,0 +1,13 @@ +import { isRoutePath } from './route-path.utils'; + +describe('isRoutePath', () => { + it('matches the route and its nested paths, ignoring query and fragment, without matching siblings', () => { + expect(isRoutePath('/notebook', '/notebook')).toBe(true); + expect(isRoutePath('/notebook/launch', '/notebook')).toBe(true); + expect(isRoutePath('/notebook?session=1#top', '/notebook')).toBe(true); + expect(isRoutePath('/experiments?experiment=7', '/notebook')).toBe(false); + expect(isRoutePath('/notebookish', '/notebook')).toBe(false); + expect(isRoutePath('/experiments-dashboard', '/experiments')).toBe(false); + expect(isRoutePath('/experiments-dashboard', '/experiments-dashboard')).toBe(true); + }); +}); diff --git a/src/app/core/route-path.utils.ts b/src/app/core/route-path.utils.ts new file mode 100644 index 000000000..b2f34e83f --- /dev/null +++ b/src/app/core/route-path.utils.ts @@ -0,0 +1,14 @@ +/** + * The route-path test the chrome and the app shell both need: "is the current + * URL under this route, nested paths included?". + * + * It used to be written twice — once in AppComponent (notebook only) and once in + * HeaderComponent (`onPath`, used for notebook, studio and dashboard). Two copies + * of a URL parse drift: one of them had to remember to strip `#` as well as `?`, + * and a route whose prefix is a prefix of another (`/notebook`, `/notebook/...`) + * must not match a sibling. One implementation, one behaviour. + */ +export function isRoutePath(url: string, prefix: string): boolean { + const path = url.split('?')[0].split('#')[0]; + return path === prefix || path.startsWith(`${prefix}/`); +} diff --git a/src/app/core/share.utils.ts b/src/app/core/share.utils.ts new file mode 100644 index 000000000..f9fd0f30a --- /dev/null +++ b/src/app/core/share.utils.ts @@ -0,0 +1,59 @@ +import { Router } from '@angular/router'; + +/** + * Build an absolute share URL for an experiment, pointing back at the + * experiments dashboard with the experiment id in the query string. + */ +export function buildExperimentShareUrl(router: Router, experimentId: string): string { + const tree = router.createUrlTree(['/experiments-dashboard'], { + queryParams: { experiment: experimentId }, + }); + const relative = router.serializeUrl(tree); + return window.location.origin + relative; +} + +/** + * Whether the given user owns the experiment (matching the author's email). + * Returns false when either email is unavailable. + */ +export function isExperimentOwner( + currentUserEmail: string | null | undefined, + authorEmail: string | null | undefined +): boolean { + if (!currentUserEmail || !authorEmail) return false; + return currentUserEmail === authorEmail; +} + +/** + * Toast copy for the share flow, in one place. Both the dashboard list (which + * anchors the toast on the row that was acted on) and the detail view show these + * same strings; keeping them here is what stops the two surfaces from worded-splitting. + */ +export const SHARE_TOAST = { + copied: 'Link copied to clipboard', + copyFailed: 'Could not copy link — check console.', + clipboardUnavailable: 'Clipboard not available — check console log.', + nowShared: 'Experiment is now shared', + noLongerShared: 'Experiment is no longer shared', + toggleFailed: 'Failed to update share state', +} as const; + +/** Copy `text` and resolve to the toast string for the outcome (the reason is logged, not shown). */ +export async function copyShareUrl(text: string): Promise<string> { + if (!navigator.clipboard?.writeText) { + console.warn('Clipboard API not available, share URL:', text); + return SHARE_TOAST.clipboardUnavailable; + } + try { + await navigator.clipboard.writeText(text); + return SHARE_TOAST.copied; + } catch (err) { + console.warn('Failed to copy share URL:', err); + return SHARE_TOAST.copyFailed; + } +} + +/** Toast for a completed share toggle. */ +export function shareToggleToast(shared: boolean): string { + return shared ? SHARE_TOAST.nowShared : SHARE_TOAST.noLongerShared; +} diff --git a/src/app/core/transformation-steps.utils.spec.ts b/src/app/core/transformation-steps.utils.spec.ts new file mode 100644 index 000000000..42111867e --- /dev/null +++ b/src/app/core/transformation-steps.utils.spec.ts @@ -0,0 +1,203 @@ +import { describe, it, expect } from 'vitest'; +import { + CREATOR_NAMES, + fitSelectedCodes, + fitVariablePool, + newStepId, + stepOutputNames, + stepSourceVariables, + splitPreprocessingSteps, + TransformationStep, +} from './transformation-steps.utils'; + +const categoricalStep: TransformationStep = { + id: 'c1', + name: 'categorical_column_creator', + parameters: { + new_column_names: ['grp'], + strategy: 'filter_rules', + rules: { a: {}, b: {} }, + default_enumeration: 'other', + }, +}; + +const kmeansStep: TransformationStep = { + id: 'k1', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km'] as [string], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { cluster_variables: ['a', 'b'] }, + }, +}; + +const kmeansStepFallback: TransformationStep = { + id: 'k2', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km2'] as [string], + k: 2, + enumeration_prefix: 'cluster_', + variables: ['c', 'd'], + }, +}; + +const pcaStep: TransformationStep = { + id: 'p1', + name: 'pca_column_creator', + parameters: { + new_column_names: ['p1', 'p2'], + n_components: 2, + reusable_preprocessing: { base_parameters: { variables: ['x', 'y'] } }, + }, +}; + +const pcaStepFallback: TransformationStep = { + id: 'p2', + name: 'pca_column_creator', + parameters: { new_column_names: ['p3'], n_components: 1, variables: ['z'] }, +}; + +describe('CREATOR_NAMES', () => { + it('covers the three column-creating steps', () => { + expect([...CREATOR_NAMES].sort()).toEqual([ + 'categorical_column_creator', + 'kmeans_cluster_creator', + 'pca_column_creator', + ]); + }); +}); + +describe('stepSourceVariables', () => { + it('returns the field codes referenced inside a categorical step rules, not the rule keys', () => { + expect( + stepSourceVariables({ + id: 'c', + name: 'categorical_column_creator', + parameters: { + new_column_names: ['grp'], + rules: { + a: { condition: 'AND', rules: [{ id: 'age', field: 'age', type: 'integer', input: 'number', operator: 'less', value: 60 }] }, + b: { condition: 'AND', rules: [{ field: 'sex', operator: 'in', value: ['1', '2'] }] }, + }, + }, + }), + ).toEqual(['age', 'sex']); + }); + + it('returns an empty list for a categorical step with no rules', () => { + expect( + stepSourceVariables({ + id: 'c', + name: 'categorical_column_creator', + parameters: { new_column_names: ['g'] }, + }), + ).toEqual([]); + }); + + it('returns an empty list for a categorical step whose rule filters reference no field', () => { + expect(stepSourceVariables(categoricalStep)).toEqual([]); + }); + + it('reads cluster_variables for a kmeans step, falling back to variables', () => { + expect(stepSourceVariables(kmeansStep)).toEqual(['a', 'b']); + expect(stepSourceVariables(kmeansStepFallback)).toEqual(['c', 'd']); + }); + + it('reads base_parameters.variables for a pca step, falling back to variables', () => { + expect(stepSourceVariables(pcaStep)).toEqual(['x', 'y']); + expect(stepSourceVariables(pcaStepFallback)).toEqual(['z']); + }); +}); + +describe('stepOutputNames', () => { + it('reads new_column_names', () => { + expect(stepOutputNames(categoricalStep)).toEqual(['grp']); + expect(stepOutputNames(pcaStep)).toEqual(['p1', 'p2']); + }); + + it('returns an empty list when new_column_names is absent', () => { + expect( + stepOutputNames({ + id: 'x', + name: 'categorical_column_creator', + parameters: {}, + }), + ).toEqual([]); + }); +}); + +describe('splitPreprocessingSteps', () => { + it('keeps the transformation order and gives each step a unique id', () => { + const { singletons, transformations } = splitPreprocessingSteps([ + { name: 'missing_values_handler', parameters: { strategies: { a: 'drop' } } }, + { name: 'pca_column_creator', parameters: { new_column_names: ['a'] } }, + { name: 'categorical_column_creator', parameters: { new_column_names: ['b'] } }, + { name: 'pca_column_creator', parameters: { new_column_names: ['c'] } }, + ]); + + expect(transformations.map((t) => t.name)).toEqual([ + 'pca_column_creator', + 'categorical_column_creator', + 'pca_column_creator', + ]); + expect(singletons).toEqual({ missing_values_handler: { strategies: { a: 'drop' } } }); + + const ids = transformations.map((t) => t.id); + expect(ids.every((id) => typeof id === 'string' && id.length > 0)).toBe(true); + expect(new Set(ids).size).toBe(ids.length); + }); + + it('keeps every singleton step out of the transformation list', () => { + const { transformations, singletons } = splitPreprocessingSteps([ + { name: 'outlier_winsorizer', parameters: { strategies: { age: 'iqr' } } }, + { name: 'longitudinal_transformer', parameters: { strategies: { age: 'diff' } } }, + ]); + expect(transformations).toEqual([]); + expect(Object.keys(singletons).sort()).toEqual([ + 'longitudinal_transformer', + 'outlier_winsorizer', + ]); + }); + + it('returns empty results for an empty step list', () => { + const { singletons, transformations } = splitPreprocessingSteps([]); + expect(singletons).toEqual({}); + expect(transformations).toEqual([]); + }); +}); + +describe('newStepId', () => { + it('returns distinct 32-character hex ids', () => { + const ids = new Set(Array.from({ length: 50 }, () => newStepId())); + expect(ids.size).toBe(50); + ids.forEach((id) => expect(id).toMatch(/^[0-9a-f]{32}$/)); + }); +}); + +describe('fitVariablePool / fitSelectedCodes', () => { + const steps: TransformationStep[] = [ + { id: 'pca', name: 'pca_column_creator', parameters: { new_column_names: ['pca_pc1'] } }, + { id: 'km', name: 'kmeans_cluster_creator', parameters: { new_column_names: ['km_score'] } }, + { id: 'late', name: 'pca_column_creator', parameters: { new_column_names: ['late_pc1'] } }, + ]; + const candidates = [ + { code: 'age', type: 'real' }, + { code: 'sex', type: 'nominal' }, + { code: 'pca_pc1', type: 'real', isCreatedColumn: true }, + { code: 'km_score', type: 'real', isCreatedColumn: true }, + { code: 'late_pc1', type: 'real', isCreatedColumn: true }, + { code: 'age', type: 'real' }, + ]; + + it('keeps numerical CDEs and earlier outputs, not own, later, nominal or repeated codes', () => { + expect(fitVariablePool(steps, 'km', candidates).map((variable) => variable.code)).toEqual(['age', 'pca_pc1']); + }); + + it('starts CDEs selected and created columns unselected; a toggle flips each', () => { + const pool = fitVariablePool(steps, 'km', candidates); + expect(fitSelectedCodes(pool, new Set())).toEqual(['age']); + expect(fitSelectedCodes(pool, new Set(['age', 'pca_pc1']))).toEqual(['pca_pc1']); + }); +}); diff --git a/src/app/core/transformation-steps.utils.ts b/src/app/core/transformation-steps.utils.ts new file mode 100644 index 000000000..3e9c03c06 --- /dev/null +++ b/src/app/core/transformation-steps.utils.ts @@ -0,0 +1,151 @@ +/** + * One ordered list of column-creating preprocessing steps (categorical, K-means, + * and the future PCA), replacing the per-name map the applied-preprocessing store + * used to keep. Ports the Exaflow contract where every column-creating step is a + * `*_column_creator` / `kmeans_cluster_creator` step in request order. + */ + +import { AnalysisPreprocessingStep } from '../models/backend-algorithms.model'; +import { BackendFilter } from '../models/filters.model'; +import { isOutlierEligibleVariable } from './outlier-rules'; + +/** Applied descriptive preprocessing config: a map of step name → parameters. */ +export type PreprocessingConfig = Record<string, unknown>; + +export type CreatorName = + | 'categorical_column_creator' + | 'kmeans_cluster_creator' + | 'pca_column_creator'; + +export interface TransformationStep { + id: string; + name: CreatorName; + parameters: Record<string, unknown>; +} + +/** Exaflow column codes: an identifier, not free text. */ +export const COLUMN_CODE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/; + +/** A random step id. `crypto.getRandomValues` works on plain-HTTP origins, unlike `randomUUID`. */ +export function newStepId(): string { + return Array.from(crypto.getRandomValues(new Uint8Array(16)), (byte) => byte.toString(16).padStart(2, '0')).join(''); +} + +export const CREATOR_NAMES: ReadonlySet<CreatorName> = new Set<CreatorName>([ + 'categorical_column_creator', + 'kmeans_cluster_creator', + 'pca_column_creator', +]); + +/** + * Field codes a step reads from: for a categorical step the CDEs referenced inside its rule + * filters (not the rule keys, which are enumeration labels); for K-means/PCA the clustering + * variables. + */ +export function stepSourceVariables(step: TransformationStep): string[] { + if (step.name === 'categorical_column_creator') { + const rules = step.parameters['rules']; + if (rules && typeof rules === 'object' && !Array.isArray(rules)) { + const codes = new Set<string>(); + for (const filter of Object.values(rules as Record<string, BackendFilter>)) { + collectFilterFieldCodes(filter, codes); + } + return [...codes]; + } + return []; + } + if (step.name === 'kmeans_cluster_creator') { + const reusable = step.parameters['reusable_preprocessing'] as + | { cluster_variables?: unknown } + | undefined; + const variables = reusable?.cluster_variables; + if (Array.isArray(variables)) return variables.map((v) => String(v)); + return toArray(step.parameters['variables']); + } + // pca_column_creator + const base = step.parameters['reusable_preprocessing'] as + | { base_parameters?: { variables?: unknown } } + | undefined; + const variables = base?.base_parameters?.variables; + if (Array.isArray(variables)) return variables.map((v) => String(v)); + return toArray(step.parameters['variables']); +} + +/** The names the step writes, from `new_column_names`. */ +export function stepOutputNames(step: TransformationStep): string[] { + const names = step.parameters['new_column_names']; + return Array.isArray(names) ? names.map((name) => String(name)) : []; +} + +/** + * Split backend preprocessing steps into the singleton steps (kept as a config map) + * and the ordered column-creating transformations. Each transformation step gets a + * fresh id so the store can reference it; the order is preserved exactly. + */ +export function splitPreprocessingSteps( + steps: AnalysisPreprocessingStep[], +): { singletons: PreprocessingConfig; transformations: TransformationStep[] } { + const singletons: PreprocessingConfig = {}; + const transformations: TransformationStep[] = []; + + for (const step of steps) { + if (!CREATOR_NAMES.has(step.name as CreatorName)) { + singletons[step.name] = step.parameters; + continue; + } + transformations.push({ + id: newStepId(), + name: step.name as CreatorName, + parameters: step.parameters, + }); + } + + return { singletons, transformations }; +} + +/** + * Fit-mode variable pool of a K-means/PCA card: numerical variables, where a created column + * counts only when an earlier step writes it. The card's own outputs are left out. + */ +export function fitVariablePool(steps: TransformationStep[], stepId: string, candidates: any[]): any[] { + const index = steps.findIndex((step) => step.id === stepId); + const earlier = new Set((index === -1 ? steps : steps.slice(0, index)).flatMap((step) => stepOutputNames(step))); + const own = new Set(index === -1 ? [] : stepOutputNames(steps[index])); + const seen = new Set<string>(); + return candidates.filter((variable) => { + const code = String(variable?.code ?? ''); + if (!code || seen.has(code) || own.has(code) || !isOutlierEligibleVariable(variable)) return false; + if (variable?.isCreatedColumn && !earlier.has(code)) return false; + seen.add(code); + return true; + }); +} + +/** + * Codes picked from a fit pool: CDEs start selected and created columns start unselected + * (clustering raw variables with components built from them is opt-in). A toggle flips the default. + */ +export function fitSelectedCodes(pool: any[], toggled: ReadonlySet<string>): string[] { + return pool + .filter((variable) => !!variable?.isCreatedColumn === toggled.has(String(variable.code))) + .map((variable) => String(variable.code)); +} + +function toArray(value: unknown): string[] { + return Array.isArray(value) ? value.map((v) => String(v)) : []; +} + +/** Collect the field codes referenced by a filter tree into the given set. */ +function collectFilterFieldCodes(logic: unknown, codes: Set<string>): void { + if (!logic || typeof logic !== 'object') return; + const node = logic as { field?: unknown; id?: unknown; rules?: unknown }; + if (Array.isArray(node.rules)) { + node.rules.forEach((child) => collectFilterFieldCodes(child, codes)); + return; + } + if (typeof node.field === 'string') { + codes.add(node.field); + } else if (typeof node.id === 'string') { + codes.add(node.id); + } +} diff --git a/src/app/core/variable-availability.utils.spec.ts b/src/app/core/variable-availability.utils.spec.ts new file mode 100644 index 000000000..a40b0f908 --- /dev/null +++ b/src/app/core/variable-availability.utils.spec.ts @@ -0,0 +1,114 @@ +import { + collectVariableCodes, + computeUnavailableVariables, + isVariableUnavailable, + unavailableDatasetsFor, + unavailableTooltipFor, + unavailableVariableTooltip, +} from './variable-availability.utils'; + +describe('computeUnavailableVariables', () => { + const datasetsVariables = { + ds_a: ['dataset', 'age', 'sex'], + ds_b: ['dataset', 'age'], + ds_c: ['dataset', 'sex'], + }; + const labels = [ + { code: 'ds_a', label: 'Dataset A' }, + { code: 'ds_b', label: 'Dataset B' }, + { code: 'ds_c', label: 'Dataset C' }, + ]; + const variables = ['dataset', 'age', 'sex', 'mmse']; + + it('marks a variable unavailable in every selected dataset that lacks it, by label', () => { + const result = computeUnavailableVariables(datasetsVariables, ['ds_a', 'ds_b', 'ds_c'], variables, labels); + expect(result.get('sex')).toEqual(['Dataset B']); + expect(result.get('age')).toEqual(['Dataset C']); + expect(result.get('mmse')).toEqual(['Dataset A', 'Dataset B', 'Dataset C']); + }); + + it('only considers the selected datasets', () => { + const result = computeUnavailableVariables(datasetsVariables, ['ds_a'], variables, labels); + expect(result.has('sex')).toBe(false); + expect(result.has('age')).toBe(false); + expect(result.get('mmse')).toEqual(['Dataset A']); + }); + + it('never marks the dataset variable unavailable', () => { + const result = computeUnavailableVariables({ ds_a: ['age'] }, ['ds_a'], ['dataset', 'Dataset', 'age'], labels); + expect(result.size).toBe(0); + }); + + it('falls back to the dataset code when no label is known', () => { + const result = computeUnavailableVariables({ ds_x: ['age'] }, ['ds_x'], ['sex'], labels); + expect(result.get('sex')).toEqual(['ds_x']); + }); + + it('treats missing datasetsVariables, no selection and unknown datasets as no information', () => { + expect(computeUnavailableVariables(undefined, ['ds_a'], variables, labels).size).toBe(0); + expect(computeUnavailableVariables(null, ['ds_a'], variables, labels).size).toBe(0); + expect(computeUnavailableVariables({}, ['ds_a'], variables, labels).size).toBe(0); + expect(computeUnavailableVariables(datasetsVariables, [], variables, labels).size).toBe(0); + expect(computeUnavailableVariables(datasetsVariables, ['ds_unknown'], variables, labels).size).toBe(0); + }); + + it('ignores a selected dataset without an entry but still applies the others', () => { + const result = computeUnavailableVariables(datasetsVariables, ['ds_unknown', 'ds_b'], variables, labels); + expect(result.get('sex')).toEqual(['Dataset B']); + }); + + it('treats an empty entry as a dataset without variables', () => { + const result = computeUnavailableVariables({ ds_a: [], ds_b: ['age'] }, ['ds_a', 'ds_b'], ['dataset', 'age', 'sex'], labels); + expect(result.get('age')).toEqual(['Dataset A']); + expect(result.get('sex')).toEqual(['Dataset A', 'Dataset B']); + expect(result.has('dataset')).toBe(false); + }); + + it('ignores an empty entry for a dataset that is not selected', () => { + const result = computeUnavailableVariables({ ds_a: [], ds_b: ['age'] }, ['ds_b'], ['age'], labels); + expect(result.size).toBe(0); + }); + + it('lists each dataset once even when selected twice', () => { + const result = computeUnavailableVariables(datasetsVariables, ['ds_b', 'ds_b'], ['sex'], labels); + expect(result.get('sex')).toEqual(['Dataset B']); + }); +}); + +describe('variable availability helpers', () => { + const map = new Map([['sex', ['Dataset B', 'Dataset C']]]); + + it('formats the tooltip', () => { + expect(unavailableVariableTooltip(['Dataset A', 'Dataset B'])).toBe('Not available in: Dataset A, Dataset B'); + expect(unavailableVariableTooltip([])).toBe(''); + expect(unavailableVariableTooltip(undefined)).toBe(''); + expect(unavailableVariableTooltip(['Dataset A'], 'Remove variables not available in')) + .toBe('Remove variables not available in: Dataset A'); + }); + + it('formats the tooltip for a node in one call', () => { + expect(unavailableTooltipFor(map, { code: 'sex' })).toBe('Not available in: Dataset B, Dataset C'); + expect(unavailableTooltipFor(map, { code: 'sex', children: [{}] })).toBe(''); + expect(unavailableTooltipFor(map, { code: 'age' })).toBe(''); + expect(unavailableTooltipFor(null, { code: 'sex' })).toBe(''); + }); + + it('looks up leaf variables and leaves groups alone', () => { + expect(unavailableDatasetsFor(map, { code: 'sex' })).toEqual(['Dataset B', 'Dataset C']); + expect(isVariableUnavailable(map, { code: 'sex' })).toBe(true); + expect(isVariableUnavailable(map, { code: 'sex', children: [{}] })).toBe(false); + expect(isVariableUnavailable(map, { code: 'age' })).toBe(false); + expect(isVariableUnavailable(map, null)).toBe(false); + expect(isVariableUnavailable(undefined, { code: 'sex' })).toBe(false); + }); + + it('collects variable codes from nested groups', () => { + expect(collectVariableCodes({ + variables: [{ label: 'Dataset', code: 'dataset' }], + groups: [ + { label: 'Demo', variables: [{ label: 'Age', code: 'age' }], groups: [{ label: 'Sub', variables: [{ label: 'Sex', code: 'sex' }] }] }, + ], + })).toEqual(['dataset', 'age', 'sex']); + expect(collectVariableCodes(null)).toEqual([]); + }); +}); diff --git a/src/app/core/variable-availability.utils.ts b/src/app/core/variable-availability.utils.ts new file mode 100644 index 000000000..fd398ce34 --- /dev/null +++ b/src/app/core/variable-availability.utils.ts @@ -0,0 +1,121 @@ +import { DataModel, Group, Variable } from '../models/data-model.interface'; + +/** Code → label for the datasets the user can pick, e.g. ExperimentStudioService.availableDatasets(). */ +export type DatasetLabelSource = ReadonlyArray<{ code: string; label?: string | null }>; + +/** Variable code → human labels of the selected datasets that do not contain it. */ +export type UnavailableVariablesMap = ReadonlyMap<string, readonly string[]>; + +export const EMPTY_UNAVAILABLE_VARIABLES: UnavailableVariablesMap = new Map<string, string[]>(); + +/** The dataset pseudo-variable exists in every dataset by construction and is never unavailable. */ +const DATASET_VARIABLE_CODE = 'dataset'; + +/** + * Which variables of the data model cannot be used with the current dataset selection. + * + * A variable is unavailable when at least one selected dataset has an entry in + * `datasetsVariables` and that entry does not list the variable. A selected dataset with + * no entry (or a model with no `datasetsVariables` at all) carries no information, so it + * never makes a variable unavailable. An empty entry is strict: the dataset has no + * variables, so every variable is unavailable there. The `dataset` variable is never + * unavailable. + * + * @param variableCodes every variable code of the data model (leaves of the hierarchy) + * @returns variable code → labels (falling back to codes) of the datasets lacking it, + * in the order the datasets were selected; only unavailable variables are present + */ +export function computeUnavailableVariables( + datasetsVariables: DataModel['datasetsVariables'] | null | undefined, + selectedDatasets: readonly string[] | null | undefined, + variableCodes: Iterable<string>, + datasetLabels: DatasetLabelSource = [] +): UnavailableVariablesMap { + if (!datasetsVariables || !selectedDatasets?.length) { + return EMPTY_UNAVAILABLE_VARIABLES; + } + + const labelByCode = new Map<string, string>(); + for (const dataset of datasetLabels) { + const label = (dataset?.label ?? '').trim(); + if (dataset?.code && label) labelByCode.set(dataset.code, label); + } + + const informative: Array<{ label: string; present: Set<string> }> = []; + for (const code of new Set(selectedDatasets)) { + const listed = datasetsVariables[code]; + if (!Array.isArray(listed)) continue; + informative.push({ + label: labelByCode.get(code) ?? code, + present: new Set(listed), + }); + } + if (!informative.length) { + return EMPTY_UNAVAILABLE_VARIABLES; + } + + const result = new Map<string, string[]>(); + for (const code of variableCodes) { + if (!code || code.toLowerCase() === DATASET_VARIABLE_CODE || result.has(code)) continue; + const missingIn = informative + .filter((dataset) => !dataset.present.has(code)) + .map((dataset) => dataset.label); + if (missingIn.length) result.set(code, missingIn); + } + return result; +} + +/** + * Tooltip text for an unavailable variable, e.g. "Not available in: Dataset A, Dataset B". + * `lead` replaces "Not available in" for longer messages; '' when no dataset lacks it. + */ +export function unavailableVariableTooltip( + datasetLabels: readonly string[] | null | undefined, + lead = 'Not available in' +): string { + return datasetLabels?.length ? `${lead}: ${datasetLabels.join(', ')}` : ''; +} + +/** Datasets lacking the node's variable; empty for groups, unknown codes and available variables. */ +export function unavailableDatasetsFor( + map: UnavailableVariablesMap | null | undefined, + node: { code?: unknown; children?: unknown[] } | null | undefined +): readonly string[] { + if (!map?.size || !node || (Array.isArray(node.children) && node.children.length)) return []; + const code = node.code == null ? '' : String(node.code); + return (code && map.get(code)) || []; +} + +/** "Not available in: …" for the node's variable, '' for groups and available variables. */ +export function unavailableTooltipFor( + map: UnavailableVariablesMap | null | undefined, + node: { code?: unknown; children?: unknown[] } | null | undefined +): string { + return unavailableVariableTooltip(unavailableDatasetsFor(map, node)); +} + +export function isVariableUnavailable( + map: UnavailableVariablesMap | null | undefined, + node: { code?: unknown; children?: unknown[] } | null | undefined +): boolean { + return unavailableDatasetsFor(map, node).length > 0; +} + +/** Every variable code of the data model, top-level and nested in groups. */ +export function collectVariableCodes(model: Pick<DataModel, 'variables' | 'groups'> | null | undefined): string[] { + const codes: string[] = []; + const addVariables = (variables: Variable[] | undefined): void => { + (variables ?? []).forEach((variable) => { + if (variable?.code) codes.push(variable.code); + }); + }; + const visit = (groups: Group[] | undefined): void => { + (groups ?? []).forEach((group) => { + addVariables(group?.variables); + visit(group?.groups); + }); + }; + addVariables(model?.variables); + visit(model?.groups); + return codes; +} diff --git a/src/app/guards/auth.guard.ts b/src/app/guards/auth.guard.ts index e0867d522..8ea9cb447 100644 --- a/src/app/guards/auth.guard.ts +++ b/src/app/guards/auth.guard.ts @@ -10,7 +10,7 @@ export class AuthGuard implements CanActivate { constructor() { } - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { + canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { // wait for intitialisationt to finish return this.authService.authState$.pipe( filter((authState) => authState.status !== 'checking'), // wait to stop checking diff --git a/src/app/guards/terms.guard.ts b/src/app/guards/terms.guard.ts index ac8746b6a..683246791 100644 --- a/src/app/guards/terms.guard.ts +++ b/src/app/guards/terms.guard.ts @@ -1,6 +1,6 @@ import { inject, Injectable } from '@angular/core'; import { CanActivate, Router, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { map, take } from 'rxjs/operators'; import { AuthService } from '../services/auth.service'; import { TermsService } from '../services/terms.service'; @@ -13,7 +13,12 @@ export class TermsGuard implements CanActivate { constructor() { } - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { + canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { + const currentAuthState = this.authService.authState(); + if (currentAuthState.status === 'authenticated' && this.termsService.hasAgreed(currentAuthState.user)) { + return of(true); + } + return this.authService.onAuthResolved().pipe( take(1), map((authState) => { diff --git a/src/app/models/algorithm-definition.model.ts b/src/app/models/algorithm-definition.model.ts index aefebd35d..392984697 100644 --- a/src/app/models/algorithm-definition.model.ts +++ b/src/app/models/algorithm-definition.model.ts @@ -1,34 +1,42 @@ -import { RawInputData } from "./backend-algorithms.model"; +import { RawInputData, RawPreprocessingStep } from "./backend-algorithms.model"; -export interface AlgorithmParameter { - type: 'number' | 'string' | 'boolean' | 'select'; + +export type AlgorithmAvailabilityRole = 'y' | 'x'; + +export interface AlgorithmAvailabilityDetail { + role: AlgorithmAvailabilityRole; label: string; - default?: number | string | boolean; - required?: boolean; - options?: string[]; - min?: number; - max?: number; + selectedCount: number; + minCount: number; + maxCount: number | null; + required: boolean; + types: string[]; + stattypes: string[]; + messages: string[]; + satisfied: boolean; } -export interface AlgorithmDefinition { - label: string; - description?: string; - category: string; - requiresY: boolean; - requiresX: boolean; - supportsWeights: boolean; - supportsFilters: boolean; - configSchema: Record<string, AlgorithmParameter>; +export interface AlgorithmAvailability { + available: boolean; + summary: string | null; + details: AlgorithmAvailabilityDetail[]; } export interface AlgorithmConfig { name: string; label: string; description: string; + documentation?: string; + type?: string; + flags?: string[]; requiredVariable: string[]; covariate: string[]; category: string; configSchema: Array<any>; inputdata?: RawInputData; + preprocessing?: RawPreprocessingStep[]; + requires_validation_datasets?: boolean; + required_preprocessing?: string[]; isDisabled: boolean; + availability?: AlgorithmAvailability; } diff --git a/src/app/models/algorithm-results.model.ts b/src/app/models/algorithm-results.model.ts index 16590b6b5..ab8479e7e 100644 --- a/src/app/models/algorithm-results.model.ts +++ b/src/app/models/algorithm-results.model.ts @@ -1,3 +1,5 @@ +import type { PCAReusablePreprocessing } from '../core/pca-column-source.utils'; + export interface AnovaResult { anova_table: AnovaTable; tuckey_test: TuckeyTestItem[]; @@ -5,7 +7,7 @@ export interface AnovaResult { ci_info: CIInfo; } -export interface AnovaTable { +interface AnovaTable { n_obs: number; y_label: string; x_label: string; @@ -19,7 +21,7 @@ export interface AnovaTable { f_stat: number; } -export interface TuckeyTestItem { +interface TuckeyTestItem { groupA: string; groupB: string; meanA: number; @@ -30,13 +32,13 @@ export interface TuckeyTestItem { p_tuckey: number; } -export interface MinMaxPerGroup { +interface MinMaxPerGroup { categories: string[]; min: number[]; max: number[]; } -export interface CIInfo { +interface CIInfo { means: Record<string, number>; sample_stds: Record<string, number>; 'm-s': Record<string, number>; @@ -52,8 +54,7 @@ export interface AnovaTwoWayResult { } export interface DescriptiveStatsResult { - variable_based: VariableStats[]; - model_based: VariableStats[]; + featurewise?: VariableStats[]; } export interface VariableStats { @@ -79,14 +80,43 @@ export interface NumericalDescriptiveStats { q1: number | null; q2: number | null; q3: number | null; + median?: number | null; max: number | null; } +interface QuartileResult { + q: number; + value: number | null; + actual_q: number | null; +} + +export interface QuartilesResult { + quantiles: QuartileResult[]; +} + +export interface BinnedMannWhitneyUTestResult { + u_stat: number; + p_value: number; + z_score: number; + n1: number; + n2: number; +} + +export interface StandardizedMeanDifferenceComparison { + group1: string | number; + group2: string | number; + smd: number; +} + +export interface StandardizedMeanDifferenceResult { + comparisons: StandardizedMeanDifferenceComparison[]; +} + export interface HistogramResult { histogram: HistogramResultItem[]; } -export interface HistogramResultItem { +interface HistogramResultItem { var: string; grouping_var: string | null; grouping_enum: string | null; @@ -94,10 +124,41 @@ export interface HistogramResultItem { counts: (number | null)[]; } +export interface KMeansClusterReport { + cluster_id: string; + label: string; + size_interval: string; + center: Record<string, number>; + profile: string[]; + interpretation: string; + quality: { compactness: string | null }; +} + +export interface KMeansElbowReport { + k_min: number; + k_max: number; + selected_k: number; + inertia_by_k: Record<string, number>; + warning: string | null; +} + export interface KMeansResult { title: string; - n_obs: number; - centers: number[][]; + variables: string[]; + k_selection: 'manual' | 'elbow'; + selected_k: number; + initialization_method: string; + n_init: number; + n_obs_interval: string; + center_definition: string; + intended_use: string[]; + privacy_note: string; + clusters: KMeansClusterReport[]; + elbow: KMeansElbowReport | null; + converged: boolean; + n_iter: number; + warnings: string[]; + limitations: string[]; } export interface LinearRegressionResult { @@ -110,6 +171,10 @@ export interface LinearRegressionResult { r_squared_adjusted: number; f_stat: number; f_pvalue: number; + f_stat_status?: string; + f_stat_display?: string; + f_pvalue_display?: string; + f_stat_note?: string; ll: number; aic: number; bic: number; @@ -122,35 +187,105 @@ export interface LinearRegressionResult { upper_ci: number[]; } +interface CoxRegressionSummaryBase { + n_obs: number; + n_events: number; + coefficients: number[]; + hazard_ratios: number[]; + std_err: number[]; + lower_ci: number[]; + upper_ci: number[]; + hr_lower_ci: number[]; + hr_upper_ci: number[]; + z_scores: number[]; + pvalues: number[]; + df_model: number; + df_resid: number; + ll: number; +} + +export interface ClassicalCoxRegressionSummary extends CoxRegressionSummaryBase { + n_covariates: number; + n_unique_event_times: number; + ties: string; + n_iter: number; + converged: boolean; + score_norm: number; + step_norm: number; + method: string; +} + +export interface StackedCoxRegressionSummary extends CoxRegressionSummaryBase { + n_stacked_rows: number; + n_covariates: number; + r_squared_cs: number; + r_squared_mcf: number; + ll0: number; + aic: number; + bic: number; + time_grid_strategy: string; + n_time_bins_used: number; + method: string; +} + +export interface CoxRegressionResult { + dependent_var: string; + event_var: string; + indep_vars: string[]; + summary: ClassicalCoxRegressionSummary | StackedCoxRegressionSummary; +} + export interface LinearRegressionCVResult { dependent_var: string; indep_vars: string[]; n_obs: number[]; - mean_sq_error: BasicStats; + root_mean_sq_error: BasicStats; + /** Pre-rename name of the same RMSE value; still present in stored experiments. */ + mean_sq_error?: BasicStats; r_squared: BasicStats; mean_abs_error: BasicStats; f_stat: BasicStats; } -export interface BasicStats { - mean: number; - std: number; -} - -export interface SVMResult { - title: string; +export interface LMMResult { + dependent_var: string; + grouping_var: string; + indep_vars: string[]; n_obs: number; - weights: number[]; - intercept: number; + n_groups: number; + df_model: number; + df_resid: number; + coefficients: number[]; + std_err: number[]; + t_stats: number[]; + pvalue_label: string; + pvalues: number[]; + pvalues_display: string[]; + lower_ci: number[]; + upper_ci: number[]; + sigma2: number; + sigma_u2: number; + ll_reml: number; + aic: number; + bic: number; + converged: boolean; + n_iter: number; } +// Exaflow sends [null, null] when any fold value is undefined or infinite. +type BasicStats = { + mean: number | null; + std: number | null; +} | (number | null)[]; + + export interface LogisticRegressionResult { dependent_var: string; indep_vars: string[]; summary: LogisticRegressionSummary; } -export interface LogisticRegressionSummary { +interface LogisticRegressionSummary { n_obs: number; coefficients: number[]; stderr: number[]; @@ -176,7 +311,7 @@ export interface CVLogisticRegressionResult { roc_curves: ROCCurve[]; } -export interface CVClassificationSummary { +interface CVClassificationSummary { row_names: string[]; n_obs: (number | null)[]; accuracy: number[]; @@ -185,14 +320,14 @@ export interface CVClassificationSummary { fscore: number[]; } -export interface ConfusionMatrix { +interface ConfusionMatrix { tp: number; fp: number; tn: number; fn: number; } -export interface ROCCurve { +interface ROCCurve { name: string; tpr: number[]; fpr: number[]; @@ -214,12 +349,12 @@ export interface NaiveBayesCVResult { classification_summary: MulticlassClassificationSummary; } -export interface SimpleConfusionMatrix { +interface SimpleConfusionMatrix { data: number[][]; labels: string[]; } -export interface MulticlassClassificationSummary { +interface MulticlassClassificationSummary { accuracy: Record<string, Record<string, number>>; precision: Record<string, Record<string, number>>; recall: Record<string, Record<string, number>>; @@ -236,11 +371,22 @@ export interface NaiveBayesGaussianResult { feature_names: string[]; } +export interface PCAComponent { + component_id: string; + explained_variance: number; + explained_variance_ratio: number; + cumulative_explained_variance: number; + loadings: Record<string, number>; +} + export interface PCAResult { title: string; n_obs: number; eigenvalues: number[]; eigenvectors: number[][]; + variables: string[]; + components: PCAComponent[]; + reusable_preprocessing: PCAReusablePreprocessing | null; } export interface PearsonResult { @@ -252,7 +398,7 @@ export interface PearsonResult { ci_lo: MatrixDict; } -export interface MatrixDict { +interface MatrixDict { variables: string[]; [key: string]: string[] | number[]; // dynamic keys for variables } diff --git a/src/app/models/algorithm-visualization-guide.md b/src/app/models/algorithm-visualization-guide.md index 9561e2e7b..c1d99d07a 100644 --- a/src/app/models/algorithm-visualization-guide.md +++ b/src/app/models/algorithm-visualization-guide.md @@ -13,7 +13,6 @@ This document describes what the frontend currently renders for each algorithm a ### ANOVA One-Way (`anova_oneway`) - Charts: - - ANOVA Sum of Squares bar chart. - Mean plot with confidence intervals. - Tables: - ANOVA summary table. @@ -22,24 +21,47 @@ This document describes what the frontend currently renders for each algorithm a ### ANOVA Two-Way (`anova_twoway`) - Charts: - - Sum of Squares bar chart across terms. + - None (table only). - Tables: - Two-way ANOVA table (term, sum sq, df, F, p-value). -Notes: -- Legacy `anova` payload names are normalized to `anova_twoway` in mapping logic. ### T-Tests (`ttest_independent`, `ttest_paired`, `ttest_onesample`) - Charts: - - Mean difference with 95% CI error-bar style chart. + - Independent t-test: mean difference with its CI as an error-bar style chart, two-sided runs only + (a one-sided CI has an open side to draw). +- Key figures: + - Mean difference with what it is the difference of (group A − group B; paired: measurement 1 (x) + − measurement 2 (y)) and the CI at the run's level, (1 − alpha). One-sided runs show their + one-sided bound: `≥ lower` for `greater`, `≤ upper` for `less`. +- Tables: + - Compact test summary table with t, df, p, mean difference, CI, Cohen's d. The chart and the table + label the CI without a level (they do not know the run's alpha); the key figure states it. On screen the open + side of a one-sided CI (`"Infinity"` / `"-Infinity"` from Exaflow) shows as `∞` / `−∞`; CSV + keeps the engine's text. + +### Cox Regression Classical (`cox_regression_classical`) + +- Charts: + - Hazard ratio plot with 95% CI on a log scale (reference line at HR = 1). + - Intercept is omitted; factors use display labels when available. +- Tables: + - Clinician coefficients table (factor, hazard ratio, 95% CI, p-value). + - Study and model summary (participants, events, convergence, etc.). + +### Cox Regression Stacked (`cox_regression_stacked`) + +- Charts: + - Same hazard ratio plot as classical Cox. - Tables: - - Compact test summary table with t, df, p, mean difference, CI, Cohen's d. + - Clinician coefficients table. + - Study and model summary (includes pseudo R², AIC/BIC where applicable). ### Linear Regression (`linear_regression`) - Charts: - - Forest plot for coefficients with 95% CI. + - Regression line chart (single-predictor models only). - Tables: - Coefficients table. - Model summary table (R², adjusted R², AIC/BIC, etc.). @@ -48,20 +70,20 @@ Notes: - Charts: - CV metrics chart. - - Supports both per-fold arrays and summary `{ mean, std }` objects. + - Supports summary `[mean, std]` arrays and `{ mean, std }` objects; `[null, null]` metrics are skipped. - Tables: - Training set sample sizes. - - Error metrics mean/std table. + - Error metrics mean/std table (RMSE from `root_mean_sq_error`, falling back to legacy `mean_sq_error`; undefined metrics show `Undefined`). ### Logistic Regression (`logistic_regression`) - Charts: - - Forest plot for coefficients with 95% CI. + - Logistic regression curve (single-predictor models only). - Tables: - Coefficients table. - Model summary table. -### Logistic Regression CV (`logistic_regression_cv`, `logistic_regression_cv_fedaverage`) +### Logistic Regression CV (`logistic_regression_cv`) - Charts: - Confusion matrix heatmap. @@ -92,13 +114,16 @@ Notes: ### K-Means (`kmeans`) - Charts: - - 2D scatter for 2-dimensional centers. - - 3D scatter for 3-dimensional centers. - - Parallel coordinates fallback for dimensions greater than 3. + - Bubble chart of the centres when the result has 2 variables (the Cluster Centers table stays). + - Heatmap of the centres, values in the cells, for 3 or more variables (it replaces the Cluster Centers table on screen). + - Elbow curve when `elbow` is present. - Tables: - - Cluster centers table. + - K-Means summary (k selection, selected k, observations, initialization, convergence). + - Cluster centers table (cluster label, size interval, center per variable). + - Cluster profiles table (compactness, profile, interpretation). + - Notes table (privacy, center definition, warnings, limitations, intended use). -### PCA (`pca`, `pca_with_transformation`) +### PCA (`pca`) - Charts: - Eigenvector heatmap. diff --git a/src/app/models/backend-algorithms.model.ts b/src/app/models/backend-algorithms.model.ts index 0f77870dd..4a040290b 100644 --- a/src/app/models/backend-algorithms.model.ts +++ b/src/app/models/backend-algorithms.model.ts @@ -1,28 +1,14 @@ -export interface RawAlgorithmDefinition { - name: string; - label: string; - desc: string; - enabled: boolean; - inputdata: RawInputData; - parameters: Record<string, RawParameter>; - preprocessing?: RawPreprocessingStep[]; -} - -export interface RawInputData { - y?: RawIOField; - x?: RawIOField; - data_model: RawIOField; - datasets: RawIOField; - filter?: RawIOField; -} +import { BackendFilter } from './filters.model'; +import { PreprocessingOutputs } from '../core/preprocessing-outputs.utils'; -export interface RawIOField { +interface RawIOField { label: string; desc: string; types: string[]; stattypes?: string[]; - notblank?: boolean | string; - multiple?: boolean | string; + required?: boolean | string; + min_count?: number | string; + max_count?: number | string; } export interface RawParameter { @@ -32,26 +18,113 @@ export interface RawParameter { stattypes?: number; required?: boolean | string; multiple?: boolean | string; - default_value?: string | number; + default_value?: string | number | boolean; min?: string | number; max?: string | number; - default?: string | number; + default?: string | number | boolean; enums?: RawEnumsDefinition; + dict_keys_enums?: RawEnumsDefinition; + dict_values_enums?: RawEnumsDefinition; + dict_values_type?: 'real' | 'int' | 'text' | 'boolean'; } -export interface RawEnumsDefinition { - type: 'list' | 'input_var_CDE_enums' | 'fixed_var_CDE_enums'; +interface RawEnumsDefinition { + type: 'list' | 'input_var_names' | 'input_var_CDE_enums' | 'fixed_var_CDE_enums' | 'variables'; source: string[] | string; } +interface RawDictParameter extends RawParameter { + dict_keys_enums?: RawEnumsDefinition; + dict_values_enums?: RawEnumsDefinition; +} + +/** Shared inputdata specification from GET /services/specifications/inputdata */ +export interface AnalysisInputDataSpecification { + data_model: RawIOField; + datasets: RawIOField; + filters: RawIOField; + variables: RawIOField; + validation_datasets?: RawIOField; +} + +/** Preprocessing step specification from GET /services/specifications/preprocessing */ +export interface PreprocessingStepSpecification { + name: string; + label: string; + desc: string; + documentation?: string; + order?: number; + parameters: Record<string, RawParameter | RawDictParameter> | null; + outputs?: PreprocessingOutputs | null; +} + +/** Algorithm specification from GET /services/specifications/algorithms */ +export interface AlgorithmSpecification { + name: string; + label: string; + desc: string; + documentation?: string; + type?: string; + flags?: string[]; + y: RawIOField; + x?: RawIOField | null; + requires_validation_datasets: boolean; + parameters: Record<string, RawParameter> | null; + required_preprocessing: string[]; +} + +/** Merged inputdata view used by AlgorithmConfig (shared slots + algorithm y/x). */ +export interface RawInputData { + data_model?: RawIOField; + datasets?: RawIOField; + filters?: RawIOField; + filter?: RawIOField; + variables?: RawIOField; + validation_datasets?: RawIOField; + y?: RawIOField; + x?: RawIOField; +} + export interface RawPreprocessingStep { name: string; label: string; desc: string; + documentation?: string; + order?: number; parameters: Record<string, RawParameter | RawDictParameter>; + outputs?: PreprocessingOutputs | null; } -export interface RawDictParameter extends RawParameter { - dict_keys_enums?: RawEnumsDefinition; - dict_values_enums?: RawEnumsDefinition; + +export interface AnalysisInputData { + data_model: string; + datasets: string[]; + validation_datasets?: string[] | null; + filters?: BackendFilter | null; + variables: string[]; +} + +export interface AnalysisPreprocessingStep { + name: string; + parameters: Record<string, unknown>; +} + +interface AnalysisAlgorithm { + name: string; + x?: string[] | null; + y?: string[] | null; + parameters?: Record<string, unknown>; +} + +export interface AnalysisRequest { + request_id?: string | null; + inputdata: AnalysisInputData; + preprocessing?: AnalysisPreprocessingStep[] | null; + algorithm: AnalysisAlgorithm; + flags?: Record<string, unknown> | null; +} + +export interface ExperimentCreateRequest { + name: string; + analysis: AnalysisRequest; } diff --git a/src/app/models/backend-experiment-folder.model.ts b/src/app/models/backend-experiment-folder.model.ts new file mode 100644 index 000000000..66606dff3 --- /dev/null +++ b/src/app/models/backend-experiment-folder.model.ts @@ -0,0 +1,30 @@ +import { ExperimentFolder } from './experiment-folder.model'; + +/** + * Wire shapes of `/services/experiment-folders`. + * + * Identity is never sent: the session cookie picks the owner server-side, so every route below is + * scoped to the signed-in user and a different session simply gets a different set of rows. + * + * The contract the service speaks: + * + * | Call | Body | Result | + * |---------------------------------------------------------|-----------------------------|----------------------------| + * | `GET /experiment-folders` | | `{ folders: Folder[] }` | + * | `POST /experiment-folders` | `{ name, experimentUuid? }` | the created folder | + * | `PATCH /experiment-folders/{folderId}` | `{ name }` | the renamed folder | + * | `DELETE /experiment-folders/{folderId}` | | no content | + * | `POST /experiment-folders/{folderId}/members` | `{ experimentUuid }` | the updated folder | + * | `DELETE /experiment-folders/{folderId}/members/{id}` | | the updated folder | + * | `POST /experiment-folders/{folderId}/sets` | `{ name, experimentUuid? }` | the updated folder | + * | `PATCH /experiment-folders/{folderId}/sets/{setId}` | `{ name }` | the updated folder | + * | `DELETE /experiment-folders/{folderId}/sets/{setId}` | | the updated folder | + * | `PATCH /experiment-folders/{folderId}/members/{id}/set` | `{ setId }` (null = ungroup) | the updated folder | + * + * A name a sibling already carries comes back as `409`, which the service maps to the `null`/`false` + * the inline "that name is taken" messages already speak. Every other route that changes a folder + * returns the whole folder, so the client takes its state from the server instead of guessing. + */ +export interface BackendExperimentFoldersResponse { + folders: ExperimentFolder[]; +} diff --git a/src/app/models/backend-experiment.model.ts b/src/app/models/backend-experiment.model.ts index e871ff5b5..312db10ee 100644 --- a/src/app/models/backend-experiment.model.ts +++ b/src/app/models/backend-experiment.model.ts @@ -1,4 +1,4 @@ -import { BackendFilter } from './filters.model'; +import { AnalysisRequest } from './backend-algorithms.model'; export interface BackendExperiment { uuid: string; @@ -10,18 +10,7 @@ export interface BackendExperiment { status: string; description?: string; mipVersion?: string; - algorithm: { - name: string; - inputdata: { - data_model: string; - datasets: string[] | string | null; - y: string[] | string | null; - x: string[] | string | null; - filters: BackendFilter | null; - }; - parameters: Record<string, unknown>; - status: string; - }; + analysis: AnalysisRequest; createdBy: { username: string; fullname: string; diff --git a/src/app/models/data-model.interface.ts b/src/app/models/data-model.interface.ts index ab805dae3..c0f65ca00 100644 --- a/src/app/models/data-model.interface.ts +++ b/src/app/models/data-model.interface.ts @@ -3,8 +3,6 @@ export interface Variable { label: string; code?: string; description?: string; - sql_type?: string; - isCategorical?: boolean; enumerations?: EnumValue[]; type?: string; methodology?: string; @@ -13,7 +11,7 @@ export interface Variable { maxValue?: number; } -export interface EnumValue { +interface EnumValue { label: string; // UI label code: any; // backend value } @@ -37,6 +35,8 @@ export interface DataModel { groups?: Group[]; released:boolean; datasets?: string[]; + /** Dataset code → codes of the variables present in that dataset (GET /services/data-models). */ + datasetsVariables?: Record<string, string[]>; } // D3 hierarchy format interface @@ -46,8 +46,6 @@ export interface D3HierarchyNode { value?: number; // Optional, as this may only be used for leaf nodes code?: string; description?: string; - sql_type?: string; - isCategorical?: boolean; enumerations?: any[]; type?: string; methodology?: string; diff --git a/src/app/models/experiment-folder.model.ts b/src/app/models/experiment-folder.model.ts new file mode 100644 index 000000000..499d21c10 --- /dev/null +++ b/src/app/models/experiment-folder.model.ts @@ -0,0 +1,28 @@ +/** + * A named group of runs inside a folder: the unit the compare workspace turns into a section. + * + * Strict partition — a run sits in at most one set per folder, because sets drive section + * grouping and a run in two sections breaks numbering, counts, and the mental model. Moving a + * run into a set removes it from its previous one; deleting a set returns its runs to Ungrouped. + */ +export interface ExperimentSet { + id: string; + name: string; + experimentIds: string[]; +} + +/** + * A user-curated set of experiments ("analysis set"). + * + * The rows live in PostgreSQL behind `/services/experiment-folders`; this is the shape the client + * keeps them in. Both the folder and its sets hold member ids only, never experiment copies, so the + * only drift risk is an id the experiment API no longer resolves. Ids are plain UUIDs the backend + * minted — nothing here is generated in the browser. + */ +export interface ExperimentFolder { + id: string; + name: string; + experimentIds: string[]; + /** Named subsets of `experimentIds`. Members in no set render under an implicit Ungrouped group. */ + sets: ExperimentSet[]; +} diff --git a/src/app/models/experiment-run-setup.model.ts b/src/app/models/experiment-run-setup.model.ts new file mode 100644 index 000000000..d139a7101 --- /dev/null +++ b/src/app/models/experiment-run-setup.model.ts @@ -0,0 +1,32 @@ +import { BackendFilter } from './filters.model'; + +/** One `Label: value` row of the run's effective preprocessing. */ +export interface RunSetupSummaryRow { + label: string; + value: string; + lines?: string[]; +} + +/** + * Frozen copy of the inputs of the run that produced `ExperimentStudioService.runResult`. + * Editing a parameter on the Algorithm step does not invalidate a result that is already + * shown, so the Execution step reads this snapshot rather than live state; otherwise the + * summary would describe a run that never happened. Dataset and variable labels stay out — + * they are stored as codes and labelled at render time from the label maps. + */ +export interface ExperimentRunSetup { + /** Algorithm the run went out as, including any CV / transformation variant. */ + algorithmKey: string | null; + /** Selected data model, labelled (a pathology) the way the studio calls it. */ + dataModel: string | null; + datasets: string[]; + /** Codes assigned to the algorithm's `y` role. */ + outcome: string[]; + /** Codes assigned to the algorithm's `x` role. */ + covariates: string[]; + filterLogic: BackendFilter | null; + /** Effective preprocessing of the request, including defaults Exaflow applies anyway. */ + preprocessing: RunSetupSummaryRow[]; + /** Parameter values as configured for `algorithmKey`. */ + parameters: Record<string, unknown>; +} diff --git a/src/app/models/experiment-studio.model.ts b/src/app/models/experiment-studio.model.ts deleted file mode 100644 index b143013a7..000000000 --- a/src/app/models/experiment-studio.model.ts +++ /dev/null @@ -1,16 +0,0 @@ -export interface BubbleData { - code: string; - name: string; - label?: string; - type?: string; - description?: string; - value: number; - group?: string; - children?: BubbleData[]; -} - -export interface HierarchyData { - label: string; - children?: BubbleData[]; - value?: number; // Optional, depending on hierarchy -} diff --git a/src/app/models/experiments-dashboard.model.ts b/src/app/models/experiments-dashboard.model.ts index 1766252df..380d12834 100644 --- a/src/app/models/experiments-dashboard.model.ts +++ b/src/app/models/experiments-dashboard.model.ts @@ -1,3 +1,5 @@ +import { BackendFilter } from './filters.model'; + export interface Experiment { id: string; name: string; @@ -14,18 +16,8 @@ export interface Experiment { variables?: string[]; covariates?: string[]; filters?: string[]; + filterLogic?: BackendFilter | null; + preprocessing?: Record<string, unknown> | null; mipVersion?: string; } -export interface AlgorithmDetails { - name: string; - datasets: string[]; - parameters: Record<string, unknown>; - dataModel: string; -} - -export interface UserDetails { - username: string; - fullname: string; - email: string; -} diff --git a/src/app/models/filters.model.ts b/src/app/models/filters.model.ts index 8aca68214..ead3a923b 100644 --- a/src/app/models/filters.model.ts +++ b/src/app/models/filters.model.ts @@ -7,8 +7,10 @@ export type BackendRule = { value: any; }; +type BackendFilterNode = BackendRule | BackendFilter; + export type BackendFilter = { condition: 'AND' | 'OR'; - rules: BackendRule[]; + rules: BackendFilterNode[]; valid?: boolean; }; diff --git a/src/app/models/visualization-guide.md b/src/app/models/visualization-guide.md index 5511efff8..4e969fb0e 100644 --- a/src/app/models/visualization-guide.md +++ b/src/app/models/visualization-guide.md @@ -52,6 +52,9 @@ Proposed visualization: Data includes: - `t_stat`, `df`, `p`, `mean_diff`, `se_diff`, `ci_lower`, `ci_upper`, `cohens_d` +- One-sided runs (`alt_hypothesis` `less` / `greater`) send the open CI side as the string + `"-Infinity"` / `"Infinity"` and the other side as the one-sided (1 − alpha) bound. +- The paired t-test's `mean_diff` is measurement 1 (x) minus measurement 2 (y). Proposed visualization: - Primary: Single-point effect chart (mean difference with 95% CI). @@ -76,16 +79,16 @@ Proposed visualization: Data includes: - `n_obs` (per fold) -- Metric summaries as `BasicStats`: - - `mean_sq_error`, `r_squared`, `mean_abs_error`, `f_stat` +- Metric summaries as `BasicStats` (`[mean, std]`): + - `root_mean_sq_error` (RMSE), `r_squared`, `mean_abs_error`, `f_stat` (F diagnostic, not a significance test) + - Stored results from before the Exaflow rename carry the same RMSE value as `mean_sq_error`. + - A summary is `[null, null]` when any fold value is undefined or infinite. Proposed visualization: -- Primary: Grouped bar chart of mean vs std for each metric. +- Primary: Grouped bar chart of mean vs std for each metric; undefined metrics are left out. - Supporting tables: - Fold sample sizes. - - Metric mean/std summary table. -- If future payload includes per-fold arrays: - - Add fold trend line charts for each metric. + - Metric mean/std summary table; undefined metrics show `Undefined`. ## `LogisticRegressionResult` @@ -162,14 +165,19 @@ Note: ## `KMeansResult` Data includes: -- `centers` (2D, 3D, or higher-dimensional) +- `variables`, `k_selection`, `selected_k`, `initialization_method`, `n_init`, `converged`, `n_iter` +- `clusters` (label, `size_interval`, `center` per variable, `profile`, `interpretation`, `quality.compactness`) +- `elbow` (null, or `k_min`/`k_max`, `selected_k`, `inertia_by_k`, `warning`) +- `n_obs_interval`, `center_definition`, `privacy_note`, `intended_use`, `warnings`, `limitations` Proposed visualization: -- For 2D centers: Scatter plot. -- For 3D centers: 3D scatter plot. -- For >3 dimensions: Parallel coordinates plot. -- Supporting table: - - Cluster center coordinates. +- For 2 variables: Bubble plot of cluster centers, bubble area by cluster size. +- For 3 or more variables: Heatmap of cluster centers (clusters × variables), each cell printing the centre's value. +- When `elbow` is present: Elbow curve of `inertia_by_k` with the selected k marked. +- Supporting tables: + - K-Means summary (k selection, k, observations, initialization, convergence). + - Cluster centers and cluster profiles per cluster. + - Notes (privacy, center definition, warnings, limitations, intended use). ## `PCAResult` @@ -211,7 +219,7 @@ Proposed visualization: ## `DescriptiveStatsResult` Data includes: -- `variable_based`, `model_based` +- `featurewise` - each item has `NominalDescriptiveStats` or `NumericalDescriptiveStats` Proposed visualization: diff --git a/src/app/pages/account-page/account-page.component.css b/src/app/pages/account-page/account-page.component.css index 2f54124cd..8ae22fcc6 100644 --- a/src/app/pages/account-page/account-page.component.css +++ b/src/app/pages/account-page/account-page.component.css @@ -1,24 +1,21 @@ /* Layout wrapper */ .account-page { - min-height: calc(100vh - 80px); display: flex; justify-content: center; align-items: flex-start; padding: 40px 16px; - background: #f3f5f9; - font-family: 'Roboto', sans-serif; + background: transparent; } /* Main card */ .account-card { width: 100%; max-width: 900px; - background: #ffffff; - border-radius: 16px; - box-shadow: - 0 10px 30px rgba(15, 23, 42, 0.08), - 0 1px 2px rgba(15, 23, 42, 0.04); - padding: 24px 28px 28px; + background: var(--card-bg); + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-lg); + box-shadow: var(--studio-card-shadow); + padding: 32px; box-sizing: border-box; } @@ -42,29 +39,23 @@ align-items: center; gap: 8px; background: #ffffff; - color: #0f172a; - border: 1px solid #d1d5db; + color: var(--text-main); + border: 1px solid var(--border-color); padding: 8px 12px; - border-radius: 999px; + border-radius: var(--radius-md); cursor: pointer; font-size: 14px; font-weight: 500; - box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08); - transition: - background-color 0.15s ease-in-out, - transform 0.1s ease-in-out, - box-shadow 0.15s ease-in-out; + box-shadow: var(--shadow-sm); + transition: background-color 0.15s ease-in-out; } .back-btn:hover { - background: #f8fafc; - transform: translateY(-1px); - box-shadow: 0 4px 12px rgba(15, 23, 42, 0.12); + background: var(--bg-neutral); } .back-btn:active { - transform: translateY(0); - box-shadow: 0 2px 6px rgba(15, 23, 42, 0.1); + box-shadow: var(--shadow-sm); } .title-group h1 { @@ -85,19 +76,16 @@ display: inline-flex; align-items: center; gap: 8px; - background-color: #18a2b8; + background-color: var(--primary-color); color: #ffffff; border: none; - padding: 8px 14px; - border-radius: 999px; + padding: 10px 18px; + border-radius: var(--radius-md); cursor: pointer; font-size: 14px; - font-weight: 500; - box-shadow: 0 4px 10px rgba(24, 162, 184, 0.35); - transition: - background-color 0.15s ease-in-out, - transform 0.1s ease-in-out, - box-shadow 0.15s ease-in-out; + font-weight: 600; + box-shadow: var(--shadow-sm); + transition: background-color 0.15s ease; } .sign-out-btn i { @@ -105,22 +93,20 @@ } .sign-out-btn:hover { - background-color: #14889a; - transform: translateY(-1px); - box-shadow: 0 6px 14px rgba(24, 162, 184, 0.45); + background-color: var(--primary-dark); } .sign-out-btn:active { - transform: translateY(0); - box-shadow: 0 3px 8px rgba(24, 162, 184, 0.3); + background-color: var(--primary-dark); } /* Main content inside card */ .account-main { - border-radius: 12px; - border: 1px solid #e5e7eb; - padding: 20px 20px 18px; - background: linear-gradient(to bottom, #f9fafb, #ffffff); + border-radius: var(--radius-lg); + border: 1px solid var(--studio-card-border); + padding: 24px; + background: var(--card-bg); + box-shadow: none; } /* Top profile row */ @@ -212,15 +198,22 @@ } /* ===== Layout wrapper ===== */ +/* Fills the leftover the shell's column offers, instead of guessing chrome heights. */ +:host { + display: flex; + flex-direction: column; + flex: 1 1 auto; +} + .account-page { - min-height: calc(100vh - 80px); + flex: 1 1 auto; display: flex; justify-content: center; align-items: flex-start; padding: clamp(20px, 4vw, 40px) clamp(10px, 3vw, 16px); background: #f3f5f9; - font-family: 'Roboto', sans-serif; + font-family: var(--font-sans); } /* ===== Main card ===== */ @@ -494,3 +487,150 @@ min-width: 0; } } + +/* ===== Use MIP from your own computer ===== */ +.remote-access { + font-size: 14px; + line-height: 1.5; + color: #111827; +} + +.remote-access pre { + margin: 0; + padding: 10px 12px; + border-radius: 10px; + background: #f9fafb; + border: 1px solid #e5e7eb; + font-family: var(--font-mono); + font-size: 13px; + overflow-x: auto; +} + +.remote-access code { + font-family: var(--font-mono); +} + +/* Inline code only; blocks are styled by pre above. */ +.remote-access :not(pre) > code { + padding: 1px 5px; + border-radius: 4px; + background: #f3f4f6; + font-size: 0.92em; +} + +.remote-text { + margin: 14px 0 8px; +} + +.remote-note { + margin: 6px 0 0; + font-size: 13px; + color: #6b7280; +} + +.account-link { + color: var(--primary-color); +} + +.remote-options { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr)); + gap: 12px 16px; +} + +.remote-option { + padding: 14px 16px; + border-radius: 10px; + background: #f9fafb; + border: 1px solid #e5e7eb; + min-width: 0; +} + +.remote-option pre { + margin-top: 10px; + background: #ffffff; +} + +.remote-option :not(pre) > code { + background: #e5e7eb; +} + +.remote-option .remote-text { + margin: 10px 0 0; +} + +.remote-access h4 { + margin: 0 0 10px; + font-size: 14px; + font-weight: 600; + color: #111827; +} + +.snippet { + position: relative; +} + +.snippet pre { + padding-right: 88px; +} + +.copy-btn { + position: absolute; + top: 6px; + right: 6px; + display: inline-flex; + align-items: center; + gap: 6px; + background: #ffffff; + color: #0f172a; + border: 1px solid #d1d5db; + padding: 4px 10px; + border-radius: 999px; + cursor: pointer; + font-size: 13px; +} + +.copy-btn:hover { + background: #f8fafc; +} + +.copy-status { + display: block; + min-height: 1em; + margin-top: 4px; + font-size: 12px; + color: #6b7280; +} + +.create-token-btn { + display: inline-flex; + align-items: center; + gap: 8px; + background-color: var(--primary-color); + color: #ffffff; + border: none; + padding: 8px 14px; + border-radius: var(--radius-md); + font-size: 14px; + font-weight: 500; + cursor: pointer; +} + +.create-token-btn:hover:not(:disabled) { + background-color: var(--primary-dark); +} + +.create-token-btn:disabled { + opacity: 0.6; + cursor: default; +} + +.token-value code { + white-space: pre-wrap; + word-break: break-all; +} + +.token-error { + margin: 8px 0 0; + color: #991b1b; +} diff --git a/src/app/pages/account-page/account-page.component.html b/src/app/pages/account-page/account-page.component.html index b797c0721..b20a6a364 100644 --- a/src/app/pages/account-page/account-page.component.html +++ b/src/app/pages/account-page/account-page.component.html @@ -43,6 +43,79 @@ <h3>Profile Information</h3> </div> </div> </section> + + @if (authConfig()) { + <section class="info-section remote-access"> + <h3>Use MIP from your own computer</h3> + + <p class="remote-text">Install the client and point it at this portal:</p> + <div class="snippet"> + <pre><code>{{ setupSnippet }}</code></pre> + <button class="copy-btn snippet-copy-btn" type="button" (click)="copy(setupSnippet, copyStatus)" + aria-label="Copy setup commands"> + <i class="fas fa-copy"></i> + <span>Copy</span> + </button> + </div> + <span class="copy-status" aria-live="polite">{{ copyStatus() }}</span> + <p class="remote-note">In Windows PowerShell, write <code>$env:MIP_URL = "…"</code> instead of <code>export</code>.</p> + + <p class="remote-text">Then sign in, one of two ways:</p> + <div class="remote-options"> + <div class="remote-option"> + <h4>No browser on that machine</h4> + <button class="create-token-btn" type="button" (click)="createToken()" [disabled]="creatingToken()"> + <i class="fas fa-key"></i> + <span>{{ creatingToken() ? 'Creating…' : 'Create access token' }}</span> + </button> + <p class="remote-note">You confirm with your MIP sign-in. The token is shown only once.</p> + @if (tokenError()) { + <p class="token-error" role="alert">{{ tokenError() }}</p> + } + @if (cliToken()) { + <div class="snippet"> + <pre class="token-value"><code>{{ cliToken() }}</code></pre> + <button class="copy-btn token-copy-btn" type="button" (click)="copy(cliToken(), tokenCopyStatus)" + aria-label="Copy access token"> + <i class="fas fa-copy"></i> + <span>Copy</span> + </button> + </div> + <span class="copy-status" aria-live="polite">{{ tokenCopyStatus() }}</span> + <p class="remote-note"> + Treat it like a password: it gives your full MIP data access for up to 6 months, until + it goes unused for 30 days, or until you run <code>mip logout</code>. + </p> + } + <p class="remote-text"> + On that machine, run <code>mip login --token</code> and paste it. Scripts can set + <code>MIP_API_TOKEN</code> instead, but every program started from there, AI assistants + included, can read it. + </p> + </div> + + <div class="remote-option"> + <h4>A browser on any device</h4> + <p class="remote-text"> + Run <code>mip login</code>. It shows a link and a code: open the link, sign in, check + that the page shows the same code, and approve. + </p> + <p class="remote-note">Never approve a code that someone else sent you.</p> + </div> + </div> + + <p class="remote-text">Then, in Python:</p> + <pre><code>import mip +client = mip.Client.from_env()</code></pre> + <p class="remote-note"> + Each sign-in lasts at most 6 months, and ends after 30 days without use. To end it sooner, + run <code>mip logout</code> on that computer. Without that computer, remove + "MIP command line" under Applications in your + <a class="account-link" [href]="authConfig()!.issuer + '/account/applications'" target="_blank" + rel="noopener">sign-in account</a>: that ends every computer's sign-in. + </p> + </section> + } </section> </div> </div> \ No newline at end of file diff --git a/src/app/pages/account-page/account-page.component.spec.ts b/src/app/pages/account-page/account-page.component.spec.ts new file mode 100644 index 000000000..7e82d8cfc --- /dev/null +++ b/src/app/pages/account-page/account-page.component.spec.ts @@ -0,0 +1,182 @@ +import { provideHttpClient } from '@angular/common/http'; +import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; +import { Location } from '@angular/common'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { provideRouter } from '@angular/router'; +import { AccountPageComponent } from './account-page.component'; +import { AuthService } from '../../services/auth.service'; +import { createSpyObj } from '../../testing/spy-obj.testing'; +import { cliTokenSignInUrl } from './cli-token'; + +describe('AccountPageComponent', () => { + const startUrl = window.location.pathname + window.location.search; + afterEach(() => { + vi.useRealTimers(); + vi.restoreAllMocks(); + sessionStorage.clear(); + history.replaceState(null, '', startUrl); + }); + + // offered: whether the backend has a Keycloak client for `mip login` (GET /services/auth-config). + function render(offered = true) { + const authService = createSpyObj<AuthService>('AuthService', ['logout'], { + authState: signal({ status: 'authenticated', user: { fullname: 'Marie Curie', email: 'm@c.eu' } }), + }); + TestBed.configureTestingModule({ + imports: [AccountPageComponent], + providers: [ + provideZonelessChangeDetection(), + provideRouter([]), + provideHttpClient(), + provideHttpClientTesting(), + { provide: AuthService, useValue: authService }, + ], + }); + const fixture = TestBed.createComponent(AccountPageComponent); + const authConfig = TestBed.inject(HttpTestingController).expectOne('/services/auth-config'); + if (offered) { + authConfig.flush({ issuer: 'https://idp.example/realms/MIP', clientId: 'mip-cli' }); + } else { + authConfig.flush(null, { status: 404, statusText: 'Not Found' }); + } + fixture.detectChanges(); + return fixture; + } + + it('hides the remote access section when the platform does not offer it', () => { + const el: HTMLElement = render(false).nativeElement; + + expect(el.querySelector('.remote-access')).toBeNull(); + }); + + it('explains both ways to sign in and never sends users to the JupyterHub token page', () => { + const el: HTMLElement = render().nativeElement; + + const text = el.querySelector('.remote-access')!.textContent!; + expect(text).toContain('mip login --token'); + expect(text).toContain('MIP_API_TOKEN'); + expect(text).toContain('mip logout'); + expect(text).not.toContain('custom:mip-platform-token'); + expect(el.querySelector('a[href*="/hub/token"]')).toBeNull(); + expect(el.querySelector('.token-value')).toBeNull(); + }); + + it('links to the Keycloak account page to end every computer\'s sign-in', () => { + const link = (render().nativeElement as HTMLElement).querySelector<HTMLAnchorElement>('.account-link')!; + + expect(link.getAttribute('href')).toBe('https://idp.example/realms/MIP/account/applications'); + expect(link.target).toBe('_blank'); + expect(link.rel).toBe('noopener'); + }); + + it('starts the token sign-in at Keycloak as the public client', async () => { + const fixture = render(); + const redirect = vi.spyOn(fixture.componentInstance as unknown as { redirect: (url: string) => void }, 'redirect') + .mockImplementation(() => {}); + + (fixture.nativeElement.querySelector('.create-token-btn') as HTMLButtonElement).click(); + await fixture.whenStable(); + + const url = new URL(redirect.mock.calls[0][0]); + expect(url.origin + url.pathname).toBe('https://idp.example/realms/MIP/protocol/openid-connect/auth'); + expect(url.searchParams.get('client_id')).toBe('mip-cli'); + expect(url.searchParams.get('redirect_uri')).toBe(`${window.location.origin}/account`); + }); + + it('shows the token once when Keycloak sends the browser back, and drops the code from the address', async () => { + const config = { issuer: 'https://idp.example/realms/MIP', clientId: 'mip-cli' }; + const state = new URL(await cliTokenSignInUrl(config, `${window.location.origin}/account`)).searchParams.get('state')!; + history.replaceState(null, '', `/account?code=one-time-code&state=${state}`); + vi.spyOn(window, 'fetch').mockResolvedValue( + new Response(JSON.stringify({ access_token: 'a', refresh_token: 'offline-token' }), { status: 200 }), + ); + + const fixture = render(); + await vi.waitFor(() => { + fixture.detectChanges(); + expect(fixture.nativeElement.querySelector('.token-value').textContent).toContain('offline-token'); + }); + + expect(TestBed.inject(Location).path()).toBe('/account'); + expect(sessionStorage.length).toBe(0); + }); + + it('drops a code it did not ask for from the address and says so, without exchanging it', async () => { + history.replaceState(null, '', '/account?code=one-time-code&state=elsewhere'); + const fetchSpy = vi.spyOn(window, 'fetch'); + + const fixture = render(); + await vi.waitFor(() => { + fixture.detectChanges(); + expect(fixture.nativeElement.querySelector('.token-error').textContent).toContain('not started on this page'); + }); + + expect(TestBed.inject(Location).path()).toBe('/account'); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it('shows why the sign-in could not start and lets the user try again', async () => { + vi.spyOn(crypto.subtle, 'digest').mockRejectedValue(new Error('no subtle crypto')); + const fixture = render(); + const button = fixture.nativeElement.querySelector('.create-token-btn') as HTMLButtonElement; + + button.click(); + await vi.waitFor(() => { + fixture.detectChanges(); + expect(fixture.nativeElement.querySelector('.token-error').textContent).toContain('no subtle crypto'); + }); + expect(button.disabled).toBe(false); + }); + + it('enables the button again when the browser comes back from Keycloak through its back/forward cache', async () => { + const fixture = render(); + const redirect = vi.spyOn(fixture.componentInstance as unknown as { redirect: (url: string) => void }, 'redirect') + .mockImplementation(() => {}); + const button = fixture.nativeElement.querySelector('.create-token-btn') as HTMLButtonElement; + + button.click(); + await vi.waitFor(() => expect(redirect).toHaveBeenCalled()); + fixture.detectChanges(); + expect(button.disabled).toBe(true); + + window.dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true })); + fixture.detectChanges(); + expect(button.disabled).toBe(false); + }); + + it('copies a setup snippet built from the current origin, with no real token', async () => { + const writeText = vi.spyOn(navigator.clipboard, 'writeText').mockResolvedValue(); + const fixture = render(); + + (fixture.nativeElement.querySelector('.snippet-copy-btn') as HTMLButtonElement).click(); + await fixture.whenStable(); + + expect(writeText).toHaveBeenCalledWith( + `pip install mip-client\nexport MIP_URL=${window.location.origin}`, + ); + expect(fixture.nativeElement.querySelector('.copy-status').textContent).toContain('Copied'); + }); + + it('clears the copy status so the next copy is announced again', async () => { + vi.useFakeTimers(); + vi.spyOn(navigator.clipboard, 'writeText').mockResolvedValue(); + const fixture = render(); + + const page = fixture.componentInstance; + await page.copy(page.setupSnippet, page.copyStatus); + expect(page.copyStatus()).toBe('Copied'); + vi.advanceTimersByTime(2400); + expect(page.copyStatus()).toBe(''); + }); + + it('tells the user to copy by hand when the clipboard refuses', async () => { + vi.spyOn(navigator.clipboard, 'writeText').mockRejectedValue(new Error('denied')); + const fixture = render(); + + (fixture.nativeElement.querySelector('.snippet-copy-btn') as HTMLButtonElement).click(); + await fixture.whenStable(); + + expect(fixture.nativeElement.querySelector('.copy-status').textContent).toContain('Could not copy'); + }); +}); diff --git a/src/app/pages/account-page/account-page.component.ts b/src/app/pages/account-page/account-page.component.ts index cd89d5a86..c4507d124 100644 --- a/src/app/pages/account-page/account-page.component.ts +++ b/src/app/pages/account-page/account-page.component.ts @@ -1,23 +1,75 @@ -import { ChangeDetectionStrategy, Component, computed, inject, OnDestroy, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, Component, computed, inject, signal, WritableSignal } from '@angular/core'; +import { toSignal } from '@angular/core/rxjs-interop'; import { CommonModule, Location } from '@angular/common'; +import { HttpClient } from '@angular/common/http'; import { RouterModule } from '@angular/router'; +import { catchError, of } from 'rxjs'; import { AuthService } from "../../services/auth.service"; +import { AuthConfig, cliTokenSignInUrl, finishCliTokenSignIn, isSignInReturn } from './cli-token'; @Component({ selector: 'app-account-page', imports: [CommonModule, RouterModule], templateUrl: './account-page.component.html', - styleUrls: ['./account-page.component.css'], + styleUrl: './account-page.component.css', changeDetection: ChangeDetectionStrategy.OnPush }) export class AccountPageComponent { public authService = inject(AuthService); private location = inject(Location); + private http = inject(HttpClient); userName = computed(() => this.authService.authState().user?.fullname ?? ''); userEmail = computed(() => this.authService.authState().user?.email ?? ''); - constructor() { } + // The platform's public Keycloak client for the mip client; the backend answers 404 + // when there is none, and the remote access guide stays hidden. + readonly authConfig = toSignal( + this.http.get<AuthConfig>('/services/auth-config').pipe(catchError(() => of(null))), + { initialValue: null }, + ); + // Keycloak sends the browser back here after the sign-in for a new token. + private readonly tokenRedirectUri = `${window.location.origin}/account`; + // Shown once; never stored, and gone when the page is left. + readonly cliToken = signal(''); + readonly tokenError = signal(''); + readonly creatingToken = signal(false); + readonly tokenCopyStatus = signal(''); + readonly setupSnippet = ['pip install mip-client', `export MIP_URL=${window.location.origin}`].join('\n'); + readonly copyStatus = signal(''); + + constructor() { + const query = new URLSearchParams(window.location.search); + if (isSignInReturn(query)) { + // Take the one-time code out of the address bar and history right away, even when it is not ours. + this.location.replaceState('/account'); + this.creatingToken.set(true); + finishCliTokenSignIn(query, this.tokenRedirectUri) + .then((token) => this.cliToken.set(token)) + .catch((err: Error) => this.tokenError.set(err.message)) + .finally(() => this.creatingToken.set(false)); + } + } + + async createToken(): Promise<void> { + const config = this.authConfig(); + if (!config) return; + this.tokenError.set(''); + this.creatingToken.set(true); + // Back from Keycloak can restore this page from the back/forward cache with the button still disabled. + window.addEventListener('pageshow', () => this.creatingToken.set(false), { once: true }); + try { + this.redirect(await cliTokenSignInUrl(config, this.tokenRedirectUri)); + } catch (err) { + // e.g. no crypto.subtle when the portal is not served over HTTPS + this.tokenError.set(`Could not start the sign-in (${(err as Error).message}).`); + this.creatingToken.set(false); + } + } + + protected redirect(url: string): void { + window.location.assign(url); + } signOut(): void { this.authService.logout(); @@ -26,4 +78,15 @@ export class AccountPageComponent { goBack(): void { this.location.back(); } + + async copy(text: string, status: WritableSignal<string>): Promise<void> { + try { + await navigator.clipboard.writeText(text); + status.set('Copied'); + } catch { + status.set('Could not copy, select the text instead'); + } + // Cleared after a while so the live region changes, and is announced, on every click. + setTimeout(() => status.set(''), 2400); + } } diff --git a/src/app/pages/account-page/cli-token.spec.ts b/src/app/pages/account-page/cli-token.spec.ts new file mode 100644 index 000000000..7082a7ef0 --- /dev/null +++ b/src/app/pages/account-page/cli-token.spec.ts @@ -0,0 +1,84 @@ +import { cliTokenSignInUrl, codeChallenge, finishCliTokenSignIn, isSignInReturn } from './cli-token'; + +const config = { issuer: 'https://idp.example/realms/MIP', clientId: 'mip-cli-handson' }; +const redirectUri = 'https://portal.example/account'; + +function jsonResponse(status: number, body: unknown): Response { + return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }); +} + +describe('cli-token', () => { + beforeEach(() => sessionStorage.clear()); + afterEach(() => vi.restoreAllMocks()); + afterAll(() => sessionStorage.clear()); + + it('derives the PKCE challenge as base64url(SHA-256(verifier)) without padding', async () => { + // Expected value computed independently with Python's hashlib. + expect(await codeChallenge('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFXmFDjwM')).toBe( + '4CLSnxkUKEUvyrSkXOVODl4ymFB08uOtK9zimkO4PAA', + ); + }); + + it('starts an offline sign-in as the public client with PKCE', async () => { + const url = new URL(await cliTokenSignInUrl(config, redirectUri)); + + expect(url.origin + url.pathname).toBe('https://idp.example/realms/MIP/protocol/openid-connect/auth'); + expect(url.searchParams.get('client_id')).toBe('mip-cli-handson'); + expect(url.searchParams.get('response_type')).toBe('code'); + expect(url.searchParams.get('scope')).toBe('openid offline_access'); + expect(url.searchParams.get('redirect_uri')).toBe(redirectUri); + expect(url.searchParams.get('code_challenge_method')).toBe('S256'); + expect(url.searchParams.get('code_challenge')).toMatch(/^[A-Za-z0-9_-]{43}$/); + expect(url.searchParams.get('state')).toBeTruthy(); + expect(isSignInReturn(new URLSearchParams({ code: 'c', state: url.searchParams.get('state')! }))).toBe(true); + }); + + it('exchanges the code with the verifier and returns the refresh token', async () => { + const state = new URL(await cliTokenSignInUrl(config, redirectUri)).searchParams.get('state')!; + const fetchFn = vi.spyOn(window, 'fetch').mockResolvedValue( + jsonResponse(200, { access_token: 'a', refresh_token: 'offline-token' }), + ); + + const token = await finishCliTokenSignIn(new URLSearchParams({ code: 'the-code', state }), redirectUri); + + expect(token).toBe('offline-token'); + const [url, init] = fetchFn.mock.calls[0]; + expect(url).toBe('https://idp.example/realms/MIP/protocol/openid-connect/token'); + const body = new URLSearchParams(init!.body as URLSearchParams); + expect(body.get('grant_type')).toBe('authorization_code'); + expect(body.get('code')).toBe('the-code'); + expect(body.get('client_id')).toBe('mip-cli-handson'); + expect(body.get('redirect_uri')).toBe(redirectUri); + expect(body.get('code_verifier')).toMatch(/^[A-Za-z0-9_-]{43}$/); + expect(sessionStorage.length).toBe(0); + }); + + it('refuses a return it did not start, and never sends the code', async () => { + await cliTokenSignInUrl(config, redirectUri); + const fetchFn = vi.spyOn(window, 'fetch'); + + await expect( + finishCliTokenSignIn(new URLSearchParams({ code: 'the-code', state: 'forged' }), redirectUri), + ).rejects.toThrow('not started on this page'); + expect(fetchFn).not.toHaveBeenCalled(); + expect(sessionStorage.length).toBe(0); + }); + + it('reports Keycloak refusals without the code', async () => { + const state = new URL(await cliTokenSignInUrl(config, redirectUri)).searchParams.get('state')!; + vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse(400, { error: 'invalid_grant' })); + + const failure = finishCliTokenSignIn(new URLSearchParams({ code: 'the-code', state }), redirectUri); + + await expect(failure).rejects.toThrow('invalid_grant'); + await expect(failure).rejects.not.toThrow('the-code'); + }); + + it('reports a sign-in the user refused', async () => { + const state = new URL(await cliTokenSignInUrl(config, redirectUri)).searchParams.get('state')!; + + await expect( + finishCliTokenSignIn(new URLSearchParams({ error: 'access_denied', state }), redirectUri), + ).rejects.toThrow('access_denied'); + }); +}); diff --git a/src/app/pages/account-page/cli-token.ts b/src/app/pages/account-page/cli-token.ts new file mode 100644 index 000000000..6c37444d1 --- /dev/null +++ b/src/app/pages/account-page/cli-token.ts @@ -0,0 +1,85 @@ +// The token for the mip Python client, created on the account page: an OAuth 2.0 +// authorization-code sign-in with PKCE as the platform's public Keycloak client (from +// GET /services/auth-config), asking for offline_access. Keycloak hands the long-lived +// refresh token to the browser; the page shows it once and nothing stores it. + +export interface AuthConfig { + issuer: string; + clientId: string; +} + +interface PendingSignIn extends AuthConfig { + state: string; + verifier: string; +} + +const PENDING_KEY = 'mip-cli-token-sign-in'; + +function base64Url(bytes: Uint8Array): string { + return btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); +} + +export async function codeChallenge(verifier: string): Promise<string> { + const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier)); + return base64Url(new Uint8Array(digest)); +} + +function randomValue(): string { + return base64Url(crypto.getRandomValues(new Uint8Array(32))); +} + +// Keycloak's endpoints under the issuer; its discovery document is not needed in the browser. +function endpoint(issuer: string, name: 'auth' | 'token'): string { + return `${issuer.replace(/\/+$/, '')}/protocol/openid-connect/${name}`; +} + +/** Where to send the browser to start the sign-in; remembers state and verifier for the return. */ +export async function cliTokenSignInUrl(config: AuthConfig, redirectUri: string): Promise<string> { + const pending: PendingSignIn = { ...config, state: randomValue(), verifier: randomValue() }; + sessionStorage.setItem(PENDING_KEY, JSON.stringify(pending)); + const params = new URLSearchParams({ + client_id: config.clientId, + response_type: 'code', + scope: 'openid offline_access', + redirect_uri: redirectUri, + state: pending.state, + code_challenge: await codeChallenge(pending.verifier), + code_challenge_method: 'S256', + }); + return `${endpoint(config.issuer, 'auth')}?${params}`; +} + +/** True when this page load is an authorization response; finishCliTokenSignIn checks it is ours. */ +export function isSignInReturn(query: URLSearchParams): boolean { + return query.has('state') && (query.has('code') || query.has('error')); +} + +/** Exchanges the returned code for the refresh token. Errors never include the code or token. */ +export async function finishCliTokenSignIn(query: URLSearchParams, redirectUri: string): Promise<string> { + const raw = sessionStorage.getItem(PENDING_KEY); + sessionStorage.removeItem(PENDING_KEY); + const pending: PendingSignIn | null = raw ? JSON.parse(raw) : null; + if (!pending || query.get('state') !== pending.state) { + throw new Error('This sign-in was not started on this page. Create the token again.'); + } + const error = query.get('error'); + if (error) { + throw new Error(`Keycloak refused the sign-in (${error}).`); + } + const response = await fetch(endpoint(pending.issuer, 'token'), { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: new URLSearchParams({ + grant_type: 'authorization_code', + code: query.get('code') ?? '', + redirect_uri: redirectUri, + client_id: pending.clientId, + code_verifier: pending.verifier, + }), + }); + const body = await response.json().catch(() => ({})); + if (!response.ok || typeof body.refresh_token !== 'string') { + throw new Error(`Could not create the token (${body.error ?? response.status}).`); + } + return body.refresh_token; +} diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.css b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.css index 0d9ad4775..8332c345c 100644 --- a/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.css +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.css @@ -1,1571 +1,1523 @@ -/* ROOT LAYOUT - CONTAINER */ +/* Unified Algorithm Selection studio card. + One frame: the Experiment pool spans the full width as the top band, then a + master–detail row below it — Algorithm catalog on the left, Algorithm configuration + (documentation + parameters) scrolling in the column on the right. + Tokens per DESIGN.md — typography, borders, subtle tints; no icon fonts. */ -.panel-header { - display: flex; - justify-content: space-between; - align-items: center; - padding: 12px 16px; - background: #f8fafc; - border-bottom: 1px solid var(--border-color); - border-radius: 0px; - margin: 2px 0 10px; -} - -.panel-header p { - margin: 0; - font-size: 0.82rem; - font-weight: 800; - text-transform: uppercase; - letter-spacing: 0.08em; - color: var(--text-muted); -} +/* ROOT LAYOUT - CONTAINER */ .experiment-container { display: grid; - grid-template-columns: minmax(220px, 260px) minmax(0, 1fr); - gap: 18px; - padding: 8px 6px 6px; - font-family: "Roboto", system-ui, -apple-system, BlinkMacSystemFont, - "Segoe UI", sans-serif; + grid-template-columns: minmax(0, 1fr); + gap: 16px; + padding: 12px 0 0; + font-family: inherit; position: relative; - min-height: 60vh; align-items: flex-start; box-sizing: border-box; + width: 100%; } -/* LEFT: ALGORITHM PANEL */ +/* The unified studio card is the only child; the host paints no frame itself. */ +.algorithm-studio-card { + min-width: 0; +} -.algorithm-panel { - color: #0f172a; - background: var(--studio-card-bg, rgba(255, 253, 253, 0.85)); - backdrop-filter: blur(10px); - -webkit-backdrop-filter: blur(10px); - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.3)); - border-radius: 16px; - padding: 16px 0; - /* Vertical padding moved to children if needed, or kept at 0 to match legend */ - box-shadow: var(--studio-card-shadow, 0 8px 32px rgba(15, 23, 42, 0.06)); - box-sizing: border-box; - position: relative; - z-index: 1; +.algorithm-roles-board { display: flex; flex-direction: column; - align-self: stretch; + width: 100%; + min-width: 0; + min-height: 0; + height: auto; + box-sizing: border-box; + border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.3)); + border-radius: var(--radius-lg, 12px); + background: var(--studio-card-bg); + box-shadow: var(--studio-card-shadow, 0 8px 32px rgba(15, 23, 42, 0.08)); + color: var(--text-main); } -.algorithm-panel h5 { - font-size: 0.9rem; - color: var(--primary-color); - margin: 0 0 8px; +.algorithm-roles-board__header { + display: flex; + flex-wrap: wrap; + align-items: center; + justify-content: space-between; + gap: 12px 16px; + padding: 12px 14px; + border-bottom: 1px solid var(--studio-card-header-border, rgba(43, 51, 233, 0.08)); + background: var(--studio-card-header-bg, #f8fbff); +} + +.algorithm-roles-board__title { + margin: 0 0 2px; font-weight: 700; - padding: 0 16px 6px; - border-bottom: 1px solid var(--studio-card-header-border, rgba(226, 232, 240, 0.9)); + line-height: 1.25; + letter-spacing: 0.01em; + color: var(--text-main); } -.algo-toolbar { - padding: 0 16px; +/* Live runnable/total counter — tabular so filtering never reflows the header. */ +.algorithm-count-badge { + gap: 6px; + flex: 0 0 auto; } -.category { - padding: 0 16px; +.algorithm-count-badge strong { + color: var(--primary-color); + font-size: 1rem; + font-weight: 800; + font-variant-numeric: tabular-nums; + letter-spacing: -0.01em; } -.no-algos-empty { - padding: 0 16px; +.algorithm-count-badge span { + font-variant-numeric: tabular-nums; } -/* Category card */ +/* BOTTOM ROW: Algorithm catalog (left) · Algorithm configuration (right). The row + grows with its content (24rem floor) instead of being pinned to a short fixed + height, so the parameter selection expands and every parameter field is readable + in the normal flow — the window is the scroller rather than a nested inner scroll. + The assignment rails sit above the row in flow, so switching methods never moves + them out from under the pointer; only space below the row changes. */ -.category { - border: 1px solid rgba(226, 232, 240, 0.6); - border-radius: 12px; - margin-bottom: 12px; - background: rgba(255, 255, 255, 0.5); - padding: 4px; +.algorithm-workspace { + display: grid; + grid-template-columns: minmax(300px, 20%) minmax(0, 80%); /* 20% catalog · 80% configuration */ + min-width: 0; + min-height: 24rem; + height: auto; overflow: hidden; } -/* Category header */ - -.category h6 { - font-size: 0.82rem; - font-weight: 600; - cursor: pointer; - padding: 8px 10px; - margin: 0; - color: var(--primary-color); - border-radius: 0px; - display: flex; - align-items: center; - gap: 6px; - transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease; +/* TOP BAND: Experiment pool across the full card width — the variable list on the left, + the stacked outcome and predictor rails beside it on the right — so the band only has + to be tall enough for the scrolling list. It is one shrinking grid track rather than a + stretched flex cross-size: that is what lets the band stop at its max-height and hand + the overflow to the list's own scroll, instead of clipping the hint out of the card. */ +.algorithm-pool { + display: grid; + grid-template-columns: minmax(0, 1fr); + grid-template-rows: minmax(0, 1fr); + flex: 0 0 auto; + min-width: 0; + min-height: 0; + border-bottom: 1px solid var(--studio-card-header-border, rgba(43, 51, 233, 0.08)); } -.category h6:hover { - background: rgba(43, 51, 233, 0.08); - transform: translateY(-1px); +.algorithm-pool > app-algorithm-role-assignment { + min-width: 0; + min-height: 0; } -/* Expand icon */ +/* LEFT OF THE BOTTOM ROW: ALGORITHM CATALOG */ -.expand-icon { - display: inline-block; - font-size: 14px; - font-weight: bold; - transition: transform 0.2s ease-in-out; +/* The catalog title mirrors the pool's pane head so the two panes read as peers. */ +.catalog-title { + margin: 0; + font-size: 0.68rem; + font-weight: 700; + letter-spacing: 0.08em; + text-transform: uppercase; + color: var(--primary-color); } -.expand-icon.expanded { - transform: rotate(90deg); +.algorithm-catalog { + display: flex; + flex-direction: column; + gap: 10px; + padding: 12px 14px 14px; + min-width: 0; + min-height: 0; + overflow: hidden; + color: var(--text-main); + box-sizing: border-box; + border-right: 1px solid var(--studio-card-header-border, rgba(43, 51, 233, 0.08)); } -/* Algorithm list */ - -.category ul { - list-style: none; - padding-left: 10px; - margin: 6px 0 2px; +.algorithm-match-empty { + margin: 0; + padding: 14px 12px; + font-size: 0.78rem; + color: var(--text-muted); + line-height: 1.45; + border: 1px dashed rgba(148, 163, 184, 0.4); + border-radius: var(--radius-md, 8px); } -.category li { - font-size: 0.82rem; - padding: 8px 10px; - cursor: pointer; - border-radius: 8px; - margin-bottom: 2px; - background: transparent; - transition: all 0.2s ease; - position: relative; - z-index: 1; -} +/* Search */ -.category li:hover { - background: rgba(43, 51, 233, 0.05); - transform: translateX(4px); +.catalog-search { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px; } -.category li.selected { - background: var(--studio-card-bg, rgba(255, 255, 255, 0.92)); - color: var(--primary-color); - font-weight: 700; - border: 2px solid var(--primary-color); - box-shadow: 0 4px 12px rgba(43, 51, 233, 0.15); +/* The clear button overlays the field, so the field — not the row — is its containing + block; the filter chip sits beside the field instead of under it. */ +.catalog-search-field { + position: relative; + display: block; + flex: 1 1 auto; + /* Below this the chip drops to its own line rather than crushing the field: the + row wraps on this minimum plus the chip, so the field never shrinks under a + readable width when the catalog is narrow. */ + min-width: 8rem; } -.algo-item-content { - display: flex; - justify-content: space-between; - align-items: center; +.catalog-search-input { + display: block; width: 100%; + min-width: 0; + box-sizing: border-box; + padding: 8px 34px 8px 12px; + border-radius: var(--radius-md, 8px); + border: 1px solid rgba(148, 163, 184, 0.34); + background: var(--studio-card-bg); + color: var(--text-main); + font: inherit; + font-size: 0.85rem; + line-height: 1.4; } -.algo-badges { - display: flex; - gap: 4px; +.catalog-search-input:focus { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px rgba(127, 156, 232, 0.16); } -.algo-badge { +.catalog-search-input::placeholder { + color: #94a3b8; +} + +.catalog-search-clear { + position: absolute; + right: 6px; display: inline-flex; align-items: center; justify-content: center; - width: 18px; - height: 18px; - border-radius: 4px; - font-size: 0.65rem; -} - -.algo-badge--req { - background: rgba(255, 186, 8, 0.1); - /* MIP orange */ - color: var(--accent-color, #ffba08); - border: 1px solid rgba(255, 186, 8, 0.2); + width: 22px; + height: 22px; + padding: 0; + border: 0; + border-radius: var(--radius-sm, 4px); + background: transparent; + color: var(--text-muted); + font: inherit; + font-size: 1rem; + line-height: 1; + cursor: pointer; } -.algo-badge--active { - background: rgba(16, 185, 129, 0.1); - color: var(--success, #10b981); - border: 1px solid rgba(16, 185, 129, 0.2); +.catalog-search-clear:hover, +.catalog-search-clear:focus-visible { + background: color-mix(in srgb, var(--primary-color) 8%, white); + color: var(--text-main); } -.selected .algo-badge { - background: rgba(255, 255, 255, 0.2); - color: white; - border-color: rgba(255, 255, 255, 0.4); -} +/* One status chip (All / Runnable) sits on the search row — see .pill-toggle below. */ -/* Disabled algorithms */ -.algorithm-panel li.disabled-algo { - opacity: 0.5; - cursor: not-allowed; - pointer-events: auto; -} +/* Method list. Category kicker: chevron first, then the group name (per the mock). */ -.algorithm-panel li.disabled-algo:hover { - opacity: 0.8; +.algorithm-match-rows { + display: flex; + flex-direction: column; + gap: 10px; + flex: 1 1 auto; + min-height: 0; + overflow: auto; + padding-right: 2px; } -/* Legend */ -.algo-legend { - margin-top: auto; - padding: 12px 16px; - background: rgba(248, 250, 252, 0.4); - border-top: 1px solid rgba(226, 232, 240, 0.6); +.algorithm-match-row { display: flex; flex-direction: column; - gap: 8px; + gap: 6px; } -.legend-item { +/* Category kicker doubles as the disclosure control for its tiles. */ +.algorithm-match-cat { + appearance: none; display: flex; align-items: center; - gap: 10px; + gap: 8px; + width: 100%; + margin: 0; + padding: 2px 0; + border: 0; + background: none; + font-family: inherit; + font-size: 0.8rem; + font-weight: 700; + letter-spacing: 0.01em; + color: var(--primary-color); + line-height: 1.3; + text-align: left; + cursor: pointer; } -.legend-icon { - width: 20px; - height: 20px; - display: flex; +.algorithm-match-cat-name { + min-width: 0; +} + +.algorithm-match-cat-meta { + display: inline-flex; align-items: center; justify-content: center; - font-size: 0.75rem; - background: #fff; - border: 1px solid rgba(226, 232, 240, 1); + flex: 0 0 auto; + order: -1; /* chevron sits before the label */ + color: var(--primary-color); } -.legend-label { - font-size: 0.72rem; - font-weight: 600; - color: #64748b; - text-transform: uppercase; - letter-spacing: 0.04em; +/* Collapsed points right (›), expanded points down (⌄). */ +.algorithm-match-cat-meta::after { + content: ""; + width: 0.4rem; + height: 0.4rem; + border-right: 1.5px solid currentColor; + border-bottom: 1.5px solid currentColor; + transform: rotate(-45deg); + transition: transform 0.2s ease; } -/* Tooltip */ -.tooltip { - position: fixed !important; - background: #ffffff; - box-shadow: 0 12px 32px rgba(15, 23, 42, 0.22); - padding: 16px; - border-radius: 12px; - width: 320px; - max-width: calc(100vw - 40px); - max-height: 70vh; - overflow-y: auto; - z-index: 99999; - pointer-events: none; - border: 1px solid rgba(226, 232, 240, 0.8); - opacity: 1; - transition: opacity 0.15s ease-out; +.algorithm-match-cat[aria-expanded="true"] .algorithm-match-cat-meta::after { + transform: rotate(45deg); } -.tooltip-title { - margin: 0 0 8px; - font-size: 1rem; - font-weight: 700; +.algorithm-match-cat:hover .algorithm-match-cat-meta, +.algorithm-match-cat:focus-visible .algorithm-match-cat-meta { color: var(--primary-color); - line-height: 1.3; } -.tooltip-description { - font-size: 0.82rem; - line-height: 1.5; - color: #475569; - margin-bottom: 0; -} - -.tooltip-requirements { - margin-top: 12px; - padding-top: 12px; - border-top: 1px solid rgba(226, 232, 240, 0.6); +.algorithm-match-tiles { + display: flex; + flex-direction: column; + gap: 6px; } -.tooltip-requirements-header { - font-size: 0.72rem; - font-weight: 700; - text-transform: uppercase; - letter-spacing: 0.06em; - color: #94a3b8; - margin-bottom: 8px; -} +/* Algorithm row: plain label with trailing role chips; no selected highlight — + the chosen method is named in the details and parameters bands below. */ -.req-item { +.algo-tile { + appearance: none; display: flex; - align-items: flex-start; - gap: 10px; - margin-bottom: 8px; + flex-direction: column; + align-items: stretch; + gap: 2px; + width: 100%; + max-width: 100%; + border-radius: var(--radius-md, 8px); + border: 1px solid transparent; + background: #ffffff; + color: var(--text-main); + font: inherit; + font-size: 0.8rem; + font-weight: 500; + line-height: 1.45; + text-align: left; + cursor: pointer; + transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease; } -.req-icon { - font-size: 0.85rem; - color: var(--accent-color, #ffba08); - margin-top: 2px; - flex-shrink: 0; +.algo-tile:hover:not(.disabled-algo), +.algo-tile:focus-visible:not(.disabled-algo) { + border-color: rgba(43, 51, 233, 0.22); + background: color-mix(in srgb, var(--primary-light) 10%, white); } -.req-text { - font-size: 0.78rem; - color: #334155; - line-height: 1.4; +/* Selected method: accent bar, primary border, and a subtle tint so the tile the + configuration column on the right refers to is unmistakable. */ +.algo-tile.selected:not(.disabled-algo) { + border-color: var(--primary-color); + background: color-mix(in srgb, var(--primary-color) 8%, white); + box-shadow: inset 3px 0 0 var(--primary-color); } -/* Unavailable warning */ -::ng-deep .unavailable-warning { - display: flex !important; - align-items: flex-start !important; - gap: 10px !important; - margin: 12px 0 0 !important; - padding: 10px 12px !important; - background: #fefce8 !important; - border-left: 4px solid #facc15 !important; - border-radius: 4px !important; - color: #854d0e !important; - font-weight: 500 !important; - font-size: 0.78rem !important; - line-height: 1.5 !important; - box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05) !important; -} - -::ng-deep .unavailable-warning::before { - content: "⚠️"; - flex-shrink: 0; - font-size: 0.9rem; +/* The chevron points from the chosen tile into the configuration column, so the eye + lands on the details without scanning. Hidden until the tile is the selection. */ +.algo-tile-cue { + display: none; + flex: 0 0 auto; + width: 6px; + height: 6px; + margin-left: auto; + border-top: 2px solid var(--primary-color); + border-right: 2px solid var(--primary-color); + transform: rotate(45deg); } -/* RIGHT: EXPERIMENT PANEL */ +.algo-tile.selected:not(.disabled-algo) .algo-tile-cue { + display: block; +} -.experiment-panel { - flex: 1; - min-width: 0; - padding: 10px 12px 12px; - box-sizing: border-box; - transition: all 0.3s ease; - position: relative; - background: var(--studio-card-bg, rgba(255, 255, 255, 0.92)); - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.45)); - border-radius: 0px; - box-shadow: var(--studio-card-shadow, 0 10px 22px rgba(15, 23, 42, 0.08)); +.algo-tile.selected:not(.disabled-algo) .algo-label { + color: var(--primary-color); + font-weight: 700; } -.panel-loader { +.algo-tile-top { display: flex; - flex-direction: column; align-items: center; - justify-content: center; - min-height: 480px; - background: rgba(255, 255, 255, 0.5); - border-radius: 20px; - gap: 1.5rem; - margin-top: 20px; - border: 1px dashed rgba(148, 163, 184, 0.3); + gap: 8px; + width: 100%; + min-width: 0; } -.panel-loader p { - color: var(--text-muted); +.algo-tile .algo-label { + min-width: 0; + color: var(--text-main); font-weight: 500; - font-size: 1.1rem; - margin: 0; + overflow-wrap: anywhere; } -/* Header (name + run + info) */ +/* Required-input chips: amber y (outcome), lavender x (predictor) */ -.experiment-panel>header { - margin: -10px -12px 12px; - padding: 12px 16px; - background: #f8fafc; - border-bottom: 1px solid var(--border-color); - border-top-left-radius: 0px; - border-top-right-radius: 0px; +.algo-why { + display: block; + font-weight: 500; + line-height: 1.4; } -.experiment-header { - display: flex; - align-items: center; - justify-content: space-between; - gap: 12px; - margin-bottom: 0; - width: 100%; +.algo-tile.disabled-algo { + cursor: pointer; + background: color-mix(in srgb, var(--bg-neutral, #f8fafc) 55%, white); + border-color: transparent; + box-shadow: none; + color: var(--text-muted); } -.header-title-group { - display: flex; - align-items: center; - gap: 12px; +.algo-tile.disabled-algo .algo-label { + color: var(--text-muted); } -.header-actions-group { - display: flex; - align-items: center; - gap: 8px; +.algo-tile.disabled-algo:hover, +.algo-tile.disabled-algo:focus-visible { + background: color-mix(in srgb, var(--text-muted) 8%, white); } -.icon-pill { - width: 42px; - height: 42px; - border-radius: 12px; - background: white; - border: 1px solid rgba(43, 51, 233, 0.1); - display: flex; - align-items: center; - justify-content: center; - color: var(--primary-color); - font-size: 1.25rem; - box-shadow: 0 4px 12px rgba(15, 23, 42, 0.08); +/* A read-only preview selection of an unavailable method: same accent bar, muted. */ +.algo-tile.disabled-algo.selected { + border-color: rgba(100, 116, 139, 0.55); + background: color-mix(in srgb, var(--text-muted) 8%, white); + box-shadow: inset 3px 0 0 rgba(100, 116, 139, 0.55); } -.icon-pill i { - color: var(--primary-color); - font-size: 1.1rem; -} +/* RIGHT OF THE BOTTOM ROW: ALGORITHM CONFIGURATION — documentation and parameter + fields share one column that grows with its content, so a parameter-rich method + reveals its whole form instead of clipping it under a fixed-height inner scroll. + The board paints the frame: this column adds no border, tint, or shadow of its + own. */ -.header-text { +.algorithm-config-column { display: flex; flex-direction: column; + min-width: 0; + min-height: 0; + overflow: visible; } -.experiment-title { - font-size: 1rem; - font-weight: 700; +/* PARAMETERS — continue the configuration flow directly under the documentation; + the unified card paints the frame. */ + +.algorithm-params { + display: flex; + flex-direction: column; + flex: 1 1 auto; + gap: 10px; + padding: 0 16px 16px; + min-width: 0; + box-sizing: border-box; color: var(--text-main); - margin: 0; - line-height: 1.2; } -.experiment-subtitle { - font-size: 0.75rem; +.inspector-tag { + display: inline-flex; + align-items: center; + padding: 1px 8px; + border-radius: 999px; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.14)); + background: var(--bg-neutral); color: var(--text-muted); - margin: 0; + font-size: 0.64rem; + font-weight: 700; } -.back-btn { - margin-right: 4px; +.algorithm-params__body { display: flex; - align-items: center; - gap: 8px; - background: transparent; - color: var(--text-muted); - border: 1px solid transparent; + flex-direction: column; + gap: 10px; + min-width: 0; } -.back-btn:hover { - color: var(--primary-color); - background: rgba(43, 51, 233, 0.05); - border-color: rgba(43, 51, 233, 0.1); +.algorithm-params__empty { + display: flex; + flex-direction: column; + flex: 1 1 auto; + border-radius: var(--radius-md, 8px); } -.icon-pill i { - color: #fff; - font-size: 14px; +.algorithm-params__empty-title { + margin: 0; + font-size: 0.92rem; + font-weight: 700; + color: var(--text-main); + line-height: 1.35; } -/* icon-pill is purely decorative, no hover effects */ +.algorithm-params__empty-text { + margin: 0; + max-width: 30rem; + font-size: 0.8rem; + line-height: 1.6; + color: var(--text-muted); +} -/* Name box */ +/* TOP OF THE CONFIGURATION COLUMN — one continuous Algorithm configuration section: + header, description, and documentation. It no longer pins: the column expands to + its content and the window scrolls, so a sticky head would hang over the scrolling + page and hide the parameter fields below it instead of helping them. */ -.experiment-name-box { +.algorithm-details { display: flex; flex-direction: column; - min-width: 260px; + gap: 8px; + position: static; + background: var(--studio-card-bg); + color: var(--text-main); } -.experiment-name-box label { - font-size: 0.72rem; - color: #475569; - margin-bottom: 4px; - font-weight: 700; - letter-spacing: 0.04em; - text-transform: uppercase; +.algorithm-details__header { + flex-direction: column; } -.experiment-name-input-row { - display: flex; - align-items: center; - gap: 6px; +.algorithm-details__heading { + flex-wrap: wrap; } -.experiment-name-input-row input { - flex: 1; -} +/* Documentation */ -.experiment-name-box input { - padding: 6px 10px; - border-radius: 0px; - border: 1px solid #cbd5e1; - font-size: 0.85rem; - outline: none; - transition: border-color 0.15s ease, box-shadow 0.15s ease, - background-color 0.15s ease; - background: #ffffff; +.documentation-panel { + margin: 0; } -.experiment-name-box input:focus { - border-color: var(--primary-color); - box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.25); +/* The native disclosure triangle is hidden, so the summary states its own affordance: + a chevron that turns with the state, driven by [open] on the <details> itself. */ +.documentation-summary { + gap: 8px; + cursor: pointer; + list-style: none; } -/* Tooltip icon next to name */ - -.name-tooltip-icon { - font-size: 11px; - color: var(--primary-color); - cursor: help; - display: inline-flex; - align-items: center; - justify-content: center; - width: 18px; - height: 18px; - border-radius: var(--radius-sm); - border: 1px solid rgba(43, 51, 233, 0.4); - background: rgba(43, 51, 233, 0.05); +.documentation-summary::-webkit-details-marker { + display: none; } -.name-tooltip-icon:hover { - background: rgba(43, 51, 233, 0.08); +/* Same chevron geometry as the catalog groups: right when collapsed, down when open. */ +.documentation-chevron { + flex: 0 0 auto; + width: 0.45rem; + height: 0.45rem; + border-right: 1.5px solid currentColor; + border-bottom: 1.5px solid currentColor; + transform: rotate(-45deg); + transition: transform 0.2s ease; } -/* Save name */ -.save-name-btn { - background: var(--primary-color); - color: white; - padding: 4px 10px; - border: none; - border-radius: var(--radius-sm); - font-size: 0.7rem; - cursor: pointer; - transition: 0.15s ease-in-out; - white-space: nowrap; +.documentation-panel[open] > .documentation-summary .documentation-chevron { + transform: rotate(45deg); } -.save-name-btn:hover:not(:disabled) { - background: var(--primary-dark, #1b21a3); - transform: translateY(-1px); - box-shadow: 0 6px 16px rgba(43, 51, 233, 0.25); +.documentation-content { + font-size: 0.82rem; + line-height: 1.45; + color: rgba(20, 22, 31, 0.72); + white-space: pre-wrap; + overflow-wrap: anywhere; } -.save-name-btn:disabled { - opacity: 0.4; - cursor: not-allowed; +.config-description { + margin: 0; + font-size: 0.86rem; + line-height: 1.55; + color: var(--text-muted); } -/* DESCRIPTION BUTTON */ -.description-button { - background: #ffffff; - border: 1px solid #cbd5e1; - width: 36px; - height: 36px; - border-radius: var(--radius-sm); - font-size: 16px; - cursor: pointer; -} +/* Run requirements */ -.description-button:hover { - background: #f1f5f9; +.config-form { + display: flex; + flex-direction: column; + gap: 10px; } -/* RUN BUTTON */ -.run-button { - background: var(--primary-color); - color: white; - border: none; - padding: 10px 22px; - border-radius: var(--radius-md); - font-size: 0.82rem; - font-weight: 700; - cursor: pointer; - box-shadow: var(--shadow-md); - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); +.config-field { + flex-direction: column; } -.run-button:hover:not(:disabled):not(.loading) { - background: linear-gradient(135deg, var(--primary-dark, #1b21a3), var(--primary-light, #7f9ce8)); - transform: translateY(-1px); - box-shadow: 0 16px 30px rgba(15, 23, 42, 0.3); +/* Fields are divided by a hairline rather than framed: the parameters read as one + form, and the card already paints the only container. */ +.config-field + .config-field { + border-top: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + padding-top: 10px; } -.run-button:disabled { - background: linear-gradient(135deg, rgba(43, 51, 233, 0.4), rgba(127, 156, 232, 0.5)) !important; - color: #f9f9f9 !important; - cursor: not-allowed !important; - box-shadow: none !important; - transform: none !important; - opacity: 0.7; -} +/* Label + "Required" */ -/* SAVE AS UI */ -.save-as-container { +.config-field>label { + font-weight: 700; + font-size: 0.72rem; + color: var(--text-muted); + margin-bottom: 5px; display: flex; align-items: center; gap: 8px; - padding: 4px 8px; - background: white; - border: 1px solid var(--border-color); - border-radius: var(--radius-md); - box-shadow: var(--shadow-lg); - z-index: 100; + letter-spacing: 0.04em; + text-transform: uppercase; } -.save-as-input { - padding: 8px 12px; - border-radius: var(--radius-sm); - border: 1px solid #cbd5e1; - font-size: 0.85rem; - outline: none; - min-width: 200px; +.config-field-desc { + margin: 0 0 8px; + font-size: 0.8rem; + line-height: 1.45; + color: #64748b; } -.save-as-input:focus { - border-color: var(--primary-color); - box-shadow: 0 0 0 2px rgba(43, 51, 233, 0.1); +.config-field-hint { + margin: 0 0 8px; + font-size: 0.78rem; + line-height: 1.4; + color: var(--text-muted); } -.cancel-btn { - background: transparent; - border: none; - color: #64748b; - cursor: pointer; - padding: 4px 8px; - font-size: 1.1rem; - border-radius: var(--radius-sm); -} +/* Inputs / selects */ -.cancel-btn:hover { - background: #f1f5f9; - color: #0f172a; +.config-input, +.config-select { + display: block; + width: 100%; + min-width: 0; + box-sizing: border-box; + padding: 9px 12px; + background: rgba(255, 255, 255, 0.98); + border-radius: var(--radius-md, 8px); + border: 1px solid rgba(148, 163, 184, 0.34); + font-size: 0.85rem; + line-height: 1.4; + transition: border-color 0.18s ease, box-shadow 0.18s ease, + background-color 0.18s ease; + box-shadow: 0 1px 0 rgba(255, 255, 255, 0.85) inset; } -/* cog button */ -.icon-pill { - width: 34px; - height: 34px; - border-radius: var(--radius-sm); - border: 1px solid #cbd5e1; - background: #ffffff; - display: inline-flex; - align-items: center; - justify-content: center; - cursor: pointer; - box-shadow: 0 10px 22px rgba(15, 23, 42, 0.12); - transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease; +.config-select { + padding-right: 30px; } -.icon-pill i { - color: var(--primary-color); - font-size: 13px; +.config-select[multiple] { + min-height: 7rem; + padding-right: 12px; } -/* FLOATING INFO BUTTON + SLIDING PANEL */ - -.floating-group { - z-index: 10; - display: flex; - align-items: center; +.config-input:focus, +.config-select:focus { + outline: none; + border-color: var(--primary-color); + background-color: var(--studio-card-bg); + box-shadow: + 0 0 0 3px rgba(127, 156, 232, 0.16), + 0 4px 12px rgba(43, 51, 233, 0.08); } -.floating-info-button { - background: linear-gradient(135deg, var(--primary-color), #7f9ce8); - color: white; - border: none; - padding: 0; - width: 34px; - height: 34px; - border-radius: var(--radius-sm); - cursor: pointer; - font-size: 16px; - font-weight: 600; - box-shadow: 0 10px 22px rgba(43, 51, 233, 0.25); +/* Checkbox wrapper */ + +.checkbox-wrapper { display: inline-flex; align-items: center; - justify-content: center; - transition: background 0.2s ease-in-out, transform 0.15s ease-in-out, - box-shadow 0.15s ease-in-out; + gap: 8px; + font-size: 0.85rem; + color: var(--text-main); } -.floating-info-button:hover { - background: linear-gradient(135deg, var(--primary-color), #7f9ce8); - transform: scale(1.08) translateY(-1px); - box-shadow: 0 14px 30px rgba(43, 51, 233, 0.3); +.checkbox-wrapper input[type="checkbox"] { + width: 18px; + height: 18px; + border-radius: 5px; } -/* Info side-panel */ +/* Validation states */ -.experiment-info-panel { - position: absolute; - top: 40px; - right: -420px; - width: 380px; - color: #0f172a; - background: var(--studio-card-bg, #f9fdfd); - border: 1px solid var(--studio-card-border, #cdeaea); - border-radius: 0px; - padding: 10px 12px 12px; - box-shadow: var(--studio-card-shadow, 0 18px 40px rgba(15, 23, 42, 0.18)); - transition: right 0.24s ease-in-out, opacity 0.18s ease-out, - transform 0.18s ease-out; - z-index: 20; - opacity: 0; - transform: translateX(8px); +.config-error { + color: #b42318; + font-size: 0.74rem; + margin-top: 5px; } -.experiment-info-panel.open { - right: 0; - opacity: 1; - transform: translateX(0); -} +/* Validation states: valid stays neutral; only the invalid edge and message signal a problem. */ -/* Header of info panel */ +/* Placeholders */ -.info-panel-header { +.config-placeholder { + width: 100%; display: flex; - align-items: flex-start; - justify-content: space-between; - gap: 8px; - padding-bottom: 8px; - border-bottom: 1px solid var(--studio-card-header-border, rgba(148, 163, 184, 0.5)); + align-items: center; + gap: 16px; + padding: 14px 16px; + border-radius: var(--radius-md, 8px); + border: 1px solid rgba(226, 232, 240, 0.95); + background: rgba(248, 250, 252, 0.65); + box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.85); } -.info-title-block h3 { - margin: 0; - font-size: 0.9rem; - color: var(--primary-color); - font-weight: 700; +.config-placeholder--minimal { + width: auto; + max-width: 100%; + min-height: 0; } -.info-subtitle { - margin: 2px 0 0; - font-size: 0.75rem; - color: #6b7280; +.config-placeholder-content { + display: flex; + flex-direction: column; + gap: 6px; + max-width: 560px; } -.info-close-btn { - background: none; - border: none; - font-size: 0.9rem; - cursor: pointer; - padding: 3px; - border-radius: var(--radius-sm); - color: #6b7280; - line-height: 1; - transition: background 0.12s ease, color 0.12s ease, transform 0.08s ease; +.config-placeholder-content h5, +.config-placeholder-content h6 { + margin: 0; + color: var(--text-main); + font-size: 0.92rem; + font-weight: 700; } -.info-close-btn:hover { - background: rgba(15, 23, 42, 0.06); - color: #111827; - transform: translateY(-1px); +.config-placeholder-content p { + margin: 0; + color: var(--text-muted); + font-size: 0.82rem; + line-height: 1.6; } -/* info panel body */ +/* Outlier report table */ -.info-panel-body { - margin-top: 8px; - padding-top: 2px; +.outlier-report-editor { display: flex; flex-direction: column; gap: 8px; - max-height: 360px; - overflow-y: auto; + padding: 4px 0; } -/* Rows */ - -.info-row { - display: grid; - grid-template-columns: 90px minmax(0, 1fr); - column-gap: 8px; - row-gap: 2px; - align-items: flex-start; +.outlier-editor-header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; } -.info-row--stacked { - grid-template-columns: minmax(0, 1fr); +.outlier-editor-header h6 { + margin: 0; + font-size: 0.9rem; + font-weight: 700; + color: var(--text-main); } -.info-label { +.outlier-count { font-size: 0.72rem; font-weight: 600; - color: #4b5563; - text-transform: uppercase; - letter-spacing: 0.06em; - padding-top: 2px; + color: var(--text-muted); + font-variant-numeric: tabular-nums; } -.info-row--stacked .info-label { - margin-bottom: 4px; +.outlier-table-wrapper { + overflow-x: auto; } -.info-value { - font-size: 0.82rem; - color: #111827; +.outlier-table { + width: 100%; + border-collapse: collapse; + font-size: 0.8rem; } -/* Chips */ +.outlier-table th { + padding: 6px 8px; + text-align: left; + font-size: 0.68rem; + font-weight: 700; + letter-spacing: 0.05em; + text-transform: uppercase; + color: var(--text-muted); + border-bottom: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); +} -.info-chips { - display: flex; - flex-wrap: wrap; - gap: 4px; +.outlier-table td { + padding: 6px 8px; + vertical-align: top; + border-bottom: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); } -.info-chip { - display: inline-flex; - align-items: center; - max-width: 100%; - padding: 3px 8px; - border-radius: 0px; - background: #eefdfb; - border: 1px solid rgba(148, 227, 213, 0.9); - font-size: 0.74rem; - color: #035b5b; - white-space: nowrap; - text-overflow: ellipsis; - overflow: hidden; +.outlier-table tr:last-child td { + border-bottom: 0; } -.info-chip--soft { - background: #f1f5f9; - border-color: rgba(148, 163, 184, 0.9); - color: #0f172a; +.outlier-variable-label { + display: block; + font-weight: 600; + color: var(--text-main); } -.info-chip--outline { - background: #ffffff; - border-style: dashed; - border-color: rgba(148, 163, 184, 0.9); - color: #0f172a; +.outlier-table small { + color: var(--text-muted); + font-size: 0.68rem; } -/* Empty pill */ +.outlier-table .compact-select, +.outlier-table .compact-input { + min-width: 6.5rem; + padding: 5px 8px; + font-size: 0.78rem; +} -.info-empty-pill { - display: inline-flex; - align-items: center; - padding: 3px 10px; - border-radius: 0px; - font-size: 0.74rem; - background: #f8fafc; - color: #94a3b8; - border: 1px dashed rgba(148, 163, 184, 0.7); +.outlier-enable { + min-height: 24px; } -/* Algorithm configs list */ +/* Panel-level empty state (no variables selected yet) */ -.info-config-list { - margin: 0; - padding: 0; +.algorithm-panel-empty { + margin: 14px 0 18px; + min-height: min(280px, calc(100vh - 320px)); display: flex; flex-direction: column; - gap: 4px; -} - -.info-config-item { - display: grid; - grid-template-columns: 120px minmax(0, 1fr); - gap: 4px; - align-items: baseline; - font-size: 0.78rem; + align-items: center; + justify-content: center; + gap: 12px; + padding: 36px 28px; + text-align: center; + border-radius: var(--radius-lg, 12px); + background: rgba(255, 255, 255, 0.4); + border: 1px solid rgba(255, 255, 255, 0.5); + box-shadow: inset 0 2px 10px rgba(43, 51, 233, 0.02); } -.info-config-item dt { - font-weight: 600; - color: #036062; +.algorithm-panel-empty__title { + margin: 4px 0 0; + font-size: 1rem; + font-weight: 700; + color: var(--text-main); + line-height: 1.3; } -.info-config-item dd { +.algorithm-panel-empty__text { margin: 0; - font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, - "Liberation Mono", "Courier New", monospace; - font-size: 0.78rem; - color: #0f172a; - word-break: break-all; + max-width: 420px; + font-size: 0.84rem; + line-height: 1.55; + color: #64748b; } -/* CONFIG FORM / EXPERIMENT SETTINGS */ +/* Tooltip (hover affordance for catalog cards) */ -.experiment-settings { - background: var(--studio-card-bg, rgba(255, 255, 255, 0.92)); - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.45)); - border-radius: 0px; - padding: 14px 16px; - margin-top: 12px; - box-shadow: var(--studio-card-shadow, 0 10px 22px rgba(15, 23, 42, 0.08)); - transition: box-shadow 0.2s ease, transform 0.15s ease; +.tooltip { + position: absolute !important; + background: var(--studio-card-bg); + box-shadow: 0 12px 32px rgba(15, 23, 42, 0.22); + padding: 16px; + border-radius: var(--radius-lg, 12px); + width: 320px; + max-width: calc(100vw - 40px); + max-height: 70vh; + overflow-y: auto; + z-index: 99999; + pointer-events: none; + border: 1px solid rgba(226, 232, 240, 0.8); + opacity: 1; + transition: opacity 0.15s ease-out; } -.experiment-settings:hover { - box-shadow: 0 14px 30px rgba(15, 23, 42, 0.12); +.tooltip-title { + margin: 0 0 8px; + font-size: 1rem; + font-weight: 700; + color: var(--primary-color); + line-height: 1.3; } -.experiment-settings h5 { - font-size: 0.92rem; - color: #026d6d; - margin-bottom: 4px; - letter-spacing: 0.01em; +.tooltip-description { + font-size: 0.82rem; + line-height: 1.5; + color: var(--text-muted); + margin-bottom: 0; } -.experiment-settings p { - font-size: 0.8rem; - color: #555; - margin-bottom: 10px; +.tooltip-requirements { + margin-top: 12px; + padding-top: 12px; + border-top: 1px solid rgba(226, 232, 240, 0.6); } -.config-title { - display: flex; - flex-direction: column; - gap: 4px; +.tooltip-requirements-header { + font-size: 0.72rem; + font-weight: 700; + text-transform: uppercase; + letter-spacing: 0.06em; + color: #94a3b8; margin-bottom: 8px; - font-size: 0.95rem; - font-weight: 600; - color: #015b5b; } -.config-description { - font-size: 0.8rem; - color: #4b5563; - margin-bottom: 10px; +.req-item { + display: flex; + align-items: flex-start; + gap: 10px; + margin-bottom: 8px; } -.cross-validation-toggle { +.req-copy { display: flex; flex-direction: column; - gap: 4px; - margin-bottom: 10px; - padding: 8px 10px; - border-radius: 0px; - background: rgba(2, 128, 128, 0.06); - border: 1px solid rgba(2, 128, 128, 0.18); + gap: 2px; } -.cross-validation-hint { - font-size: 0.74rem; - color: #4b5563; +.req-text { + font-size: 0.78rem; + color: var(--chart-label-color); line-height: 1.4; } -.transformation-toggle { - display: flex; - flex-direction: column; - gap: 6px; - margin-bottom: 10px; - padding: 10px 12px; - border-radius: 0px; - background: rgba(8, 59, 94, 0.06); - border: 1px solid rgba(8, 59, 94, 0.18); +.req-error { + font-size: 0.72rem; + color: #b45309; + line-height: 1.35; } -.transformation-hint { - font-size: 0.74rem; - color: #4b5563; - line-height: 1.4; -} +/* Shared fieldset host: the unified card paints the frame, not the fieldset. */ -.transformation-empty { - font-size: 0.74rem; - color: #6b7280; +.algorithm-readonly-fieldset { + border: 0; + padding: 0; + margin: 0; + min-width: 0; } -.transformation-grid { - display: grid; - grid-template-columns: minmax(140px, 1.5fr) repeat(4, minmax(70px, 0.6fr)); - gap: 6px 10px; - align-items: center; +.sr-only { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); } -.transformation-grid.disabled { - opacity: 0.6; - pointer-events: none; -} +/* RESPONSIVE LAYOUT */ -.transformation-header { - font-size: 0.72rem; - font-weight: 600; - color: #1f2937; - text-transform: uppercase; - letter-spacing: 0.04em; -} +@media (max-width: 960px) { + .algorithm-workspace { + grid-template-columns: minmax(0, 1fr); + height: auto; + min-height: 0; + } + + /* Stacked: head, rails, list and hint sit in one column, so the band hugs that + taller content and the window scrolls rather than the band scrolling inside it. */ + .algorithm-pool { + border-bottom: 1px solid var(--studio-card-header-border, rgba(43, 51, 233, 0.08)); + } + + .algorithm-catalog { + border-right: 0; + border-bottom: 1px solid var(--studio-card-header-border, rgba(43, 51, 233, 0.08)); + max-height: 24rem; + overflow: auto; + } -.transformation-cell { - font-size: 0.8rem; - color: #111827; } -.transformation-label { - font-weight: 500; +@media (max-width: 640px) { + .algo-why { + max-width: 100%; + } } -.transformation-cell input[type="radio"] { - width: 14px; - height: 14px; +@media (prefers-reduced-motion: reduce) { + .algo-tile, + .algorithm-match-cat-meta::after, + .documentation-chevron { + transition: none; + } } -.config-form { - display: flex; - flex-direction: column; - gap: 12px; +/* ========================================================================= + Algorithm panel review pass: step kickers, compact role band, catalog, + needs checklist, parameter layout, advanced grouping, option switches, + and the readiness bar. + ========================================================================= */ + +.algorithm-step-kicker { + padding: 10px 14px 0; } -.config-field { +.step-kicker { display: flex; - flex-direction: column; - padding: 10px 12px; - border-radius: 0px; - background: rgba(255, 255, 255, 0.95); - border: 1px solid rgba(203, 213, 225, 0.6); - box-shadow: 0 6px 16px rgba(15, 23, 42, 0.04); + align-items: center; + gap: 8px; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; + color: var(--text-muted); } -/* Label + "Required" */ - -.config-label, -.config-field>label { - font-weight: 600; - font-size: 0.86rem; - color: #026a6a; - margin-bottom: 6px; - display: flex; +.step-kicker-n { + display: inline-flex; align-items: center; - gap: 6px; + justify-content: center; + width: 20px; + height: 20px; + border-radius: 50%; + box-shadow: inset 0 0 0 1.5px var(--studio-card-border); + font-size: 0.68rem; } -/* Required tag */ +.algorithm-roles-board__title { + font-size: 0.96rem; +} -.required-tag { - background: rgba(210, 232, 255, 0.9); - color: #4866c9; - font-size: 0.7rem; +.algorithm-count-badge { + display: inline-flex; + align-items: center; + padding: 2px 10px; + border: 0; + border-radius: 999px; + background: color-mix(in srgb, var(--primary-color) 8%, white); + color: var(--primary-color); + font-size: 0.72rem; font-weight: 600; - padding: 2px 6px; - border-radius: 0px; - border: 1px solid rgba(157, 178, 248, 0.9); - text-transform: uppercase; - letter-spacing: 0.05em; + white-space: nowrap; } -/* Inputs / selects – pill style */ +/* The role band owns its own height now: the expanded roster caps itself and + scrolls; the pool no longer forces the whole assignment band into 22rem. */ +.algorithm-roles-board { + /* visible keeps the run bar sticking to the page scroller; the bar rounds its + own bottom corners so the card frame stays intact. */ + overflow: visible; +} -.config-input, -.config-select { - padding: 8px 12px; - background: rgba(255, 255, 255, 0.96); - border-radius: 0px; - border: 1px solid #c4e3e3; - font-size: 0.85rem; - line-height: 1.4; - transition: border-color 0.18s ease, box-shadow 0.18s ease, - background-color 0.18s ease, transform 0.12s ease; - box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06), - 0 0 0 1px rgba(255, 255, 255, 0.9) inset; +.algorithm-pool { + height: auto; + max-height: none; + overflow: visible; } -.config-select { - padding-right: 30px; +.algorithm-pool > app-algorithm-role-assignment { + overflow: visible; } -.config-input:focus, -.config-select:focus { - outline: none; - border-color: #03a0a0; - background-color: #f4fdfd; - box-shadow: 0 0 0 1px rgba(1, 148, 148, 0.2), - 0 8px 18px rgba(15, 23, 42, 0.15); - transform: translateY(-0.5px); +.catalog-mode-tabs { + flex: 0 0 auto; } -/* Checkbox wrapper */ +.catalog-mode-tabs button { + gap: 4px; + min-height: 26px; + padding: 0 10px; + font-size: 0.74rem; +} -.checkbox-wrapper { - display: inline-flex; - align-items: center; - gap: 8px; - font-size: 0.85rem; - color: #035757; +.catalog-title.sr-only { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); } -.checkbox-wrapper input[type="checkbox"] { - width: 15px; - height: 15px; - border-radius: 0px; +.algorithm-match-cat-count { + margin-left: auto; + padding-left: 10px; + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 600; } -/* Field meta info (desc / range / default) */ - -.field-meta { - margin-top: 6px; - font-size: 0.74rem; - color: #64748b; - line-height: 1.45; +.algo-tile { + min-height: 36px; + padding: 8px 10px; } -.desc-text { - color: #4b5563; - margin-bottom: 2px; +.algo-tile-top::before { + content: ''; + flex: 0 0 auto; + width: 10px; + height: 10px; + border-radius: 50%; + box-shadow: inset 0 0 0 1.5px rgba(71, 85, 105, 0.45); } -.range-hint { - color: #6b7280; - font-style: italic; +.algo-tile.selected .algo-tile-top::before { + box-shadow: inset 0 0 0 3.5px var(--primary-color); } -.default-hint { - color: #028080; - font-weight: 500; +.algo-tile.disabled-algo .algo-tile-top::before { + box-shadow: inset 0 0 0 1.5px rgba(71, 85, 105, 0.25); } -/* Validation states */ - -.config-error { - color: #c53030; +.algo-why { font-size: 0.74rem; - margin-top: 4px; + color: #78350f; } -/* Valid field */ - -.config-input.ng-valid.ng-touched, -.config-select.ng-valid.ng-touched { - border: 1.6px solid #22c55e !important; - background-color: #f3fdf7; - box-shadow: 0 0 0 1px rgba(34, 197, 94, 0.15), - 0 8px 16px rgba(22, 163, 74, 0.15); - padding-right: 32px; - background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%2322c55e' viewBox='0 0 24 24'%3E%3Cpath d='M9.00065 16.1701L4.83065 12.0001L3.41065 13.4101L9.00065 19.0001L21.0007 7.00012L19.5907 5.58012L9.00065 16.1701Z'/%3E%3C/svg%3E"); - background-repeat: no-repeat; - background-position: right 8px center; - background-size: 18px; +/* Configuration head: number ring, real method label, category tag. */ +.algorithm-details { + padding: 14px 16px 8px; } -/* Invalid field */ +.algorithm-details__header { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 12px; +} -.config-input.ng-invalid.ng-touched, -.config-select.ng-invalid.ng-touched { - border: 1.6px solid #f97373 !important; - background-color: #fff7f7 !important; - box-shadow: 0 0 0 1px rgba(248, 113, 113, 0.15), - 0 8px 16px rgba(220, 38, 38, 0.16); - padding-right: 32px; - background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23dc2626' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='11' stroke='%23dc2626' stroke-width='2' fill='none'/%3E%3Cline x1='12' y1='7' x2='12' y2='13' stroke='%23dc2626' stroke-width='2.4'/%3E%3Ccircle cx='12' cy='17' r='1.5' fill='%23dc2626'/%3E%3C/svg%3E"); - background-repeat: no-repeat; - background-position: right 8px center; - background-size: 18px; +.algorithm-details__heading { + display: flex; + align-items: center; + gap: 10px; + min-width: 0; } -.config-input.ng-invalid.ng-touched:focus, -.config-select.ng-invalid.ng-touched:focus { - border-color: #e11d48 !important; - box-shadow: 0 0 0 1px rgba(225, 29, 72, 0.2), - 0 8px 18px rgba(190, 18, 60, 0.22); +.algorithm-details__title { + margin: 0; + min-width: 0; + font-size: 1.1rem; + font-weight: 700; + line-height: 1.3; + color: var(--text-main); + overflow-wrap: anywhere; } -/* Backend error banner */ +.inspector-tag { + flex: 0 0 auto; + text-transform: none; + letter-spacing: 0; +} -.backend-error-banner { - margin: 10px 0 14px; - padding: 10px 12px; - border: 1px solid #f3b1b1; - background: #ffecec; - color: #8a1f1f; - border-radius: 0px; - line-height: 1.4; - font-size: 0.8rem; +.config-description { + max-width: 60ch; + margin-top: 6px; } -.algo-toolbar { - display: flex; - align-items: center; - justify-content: flex-start; - margin: 6px 0 10px; +/* Documentation is a quiet text link by default. */ +.documentation-panel { + margin-top: 2px; } -.pill-toggle { +.documentation-summary { display: inline-flex; align-items: center; - gap: 8px; - padding: 6px 10px; - border-radius: 0px; - border: 1px solid rgba(2, 109, 109, 0.25); - background: #ffffff; - color: #026d6d; - font-size: 0.75rem; + padding: 2px 0; + margin: 0; + border-radius: 0; + background: none; + font-size: 0.8rem; font-weight: 600; - cursor: pointer; - transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease; - box-shadow: 0 6px 16px rgba(15, 23, 42, 0.06); + color: var(--primary-color); } -.pill-toggle:hover { - transform: translateY(-1px); - background: rgba(2, 109, 109, 0.06); - box-shadow: 0 10px 22px rgba(15, 23, 42, 0.09); +.documentation-summary:hover { + background: none; + text-decoration: underline; } -.pill-toggle.on { - background: linear-gradient(135deg, #028080, #2dcccc); - color: #ffffff; - border-color: rgba(2, 109, 109, 0.15); +.documentation-chevron { + display: none; } -.pill-dot { - width: 8px; - height: 8px; - border-radius: 0px; - background: rgba(2, 109, 109, 0.35); +.documentation-panel[open] > .documentation-summary { + border-bottom: 0; + border-radius: 0; } -.pill-toggle.on .pill-dot { - background: rgba(255, 255, 255, 0.9); +.documentation-content { + margin-top: 6px; + padding-left: 0; + border-left: 0; } -.pill-count { - font-size: 0.72rem; - font-weight: 700; - opacity: 0.85; +/* Needs checklist */ +.algo-needs { + display: grid; + gap: 4px; + margin: 0 0 14px; + padding: 10px 12px; + list-style: none; + border-radius: var(--radius-md); + background: var(--bg-neutral); + font-size: 0.8rem; } -.no-algos-empty { - margin-top: 10px; - padding: 12px 12px; - border-radius: 0px; - border: 1px dashed rgba(148, 163, 184, 0.75); - background: linear-gradient(135deg, #ffffff, #f7fbfb); - color: #475569; +.algo-needs li { + display: flex; + flex-wrap: wrap; + align-items: baseline; + gap: 4px 8px; } -.no-algos-title { - font-size: 0.82rem; - font-weight: 700; - color: #0f172a; - margin-bottom: 2px; +.algo-needs-mark { + width: 12px; + height: 12px; + border-radius: 50%; + box-shadow: inset 0 0 0 1.5px #a06b10; + transform: translateY(2px); } -.no-algos-subtitle { - font-size: 0.78rem; - color: #64748b; - line-height: 1.35; +.algo-needs li.is-met .algo-needs-mark { + background: var(--success); + box-shadow: none; } -/* RESULT SECTION & EXPORT BUTTON */ - -.result-section { - margin-top: 1rem; +.algo-needs small { + flex-basis: 100%; + padding-left: 20px; + color: #78350f; } -.result-section h5 { - font-size: 1rem; - color: #026d6d; - margin-bottom: 8px; +.algo-needs-all-met { + margin: 0 0 14px; + padding: 8px 12px; + border-radius: var(--radius-md); + background: color-mix(in srgb, var(--success) 10%, white); + color: #065f46; + font-size: 0.8rem; font-weight: 600; } -.result-header { +/* Parameters */ +.algorithm-params__title { + margin: 0 0 2px; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; + color: var(--text-muted); +} + +/* Parameter rows: label beside control, sentence case, optional tag. */ +.config-field { display: grid; - grid-template-columns: 1fr auto 1fr; + grid-template-columns: 220px minmax(0, 360px); + gap: 2px 16px; + align-items: start; + padding: 10px 0; +} + +.config-field > label { + grid-column: 1; + display: flex; align-items: center; gap: 8px; - margin-top: 0.5rem; - margin-bottom: 0.5rem; + margin: 0; + padding-top: 8px; + font-size: 0.84rem; + font-weight: 600; + color: var(--text-main); + text-transform: none; + letter-spacing: 0; +} + +.config-field-desc, +.config-field-hint { + grid-column: 1; + margin: 0; + font-size: 0.76rem; } -.result-header h5 { +.config-field > .config-input, +.config-field > .config-select, +.config-field > .checkbox-wrapper, +.config-field > .chip-picker, +.config-field > .validation-message { grid-column: 2; - font-size: 1rem; - color: #026d6d; + grid-row: 1 / span 3; +} + +.optional-tag { + margin-left: 4px; + font-size: 0.66rem; font-weight: 600; - margin: 0; - text-align: center; - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; - max-width: 100%; + color: var(--text-muted); } -.header-spacer { - grid-column: 1; +.chip-picker { + display: flex; + flex-wrap: wrap; + gap: 6px; + min-width: 0; } -.export-button { - grid-column: 3; - justify-self: end; +.chip-picker-option { + min-height: 28px; + padding: 0 10px; + border: 1px solid rgba(148, 163, 184, 0.34); + border-radius: 999px; + background: #fff; + color: var(--text-main); + font: inherit; + font-size: 0.76rem; + cursor: pointer; } -/* Generic icon button */ +.chip-picker-option.active { + border-color: var(--primary-color); + background: color-mix(in srgb, var(--primary-color) 8%, white); + color: var(--primary-color); + font-weight: 600; +} -.icon-button { - display: inline-flex; +/* Advanced fields stay behind a disclosure, closed by default. */ +.config-advanced { + border-top: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + margin-top: 4px; +} + +.config-advanced__summary { + display: flex; align-items: center; - gap: 6px; - padding: 6px 10px; - border-radius: var(--radius-sm); - border: 1px solid #cbd5e1; - background: #ffffff; - font-size: 0.75rem; - font-weight: 500; - color: #0f172a; + gap: 8px; + padding: 10px 0; + list-style: none; cursor: pointer; - transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.12s ease, - border-color 0.15s ease; + font-size: 0.84rem; + font-weight: 600; + color: var(--text-main); } -.icon-button svg { - display: block; - fill: currentColor; +.config-advanced__summary::-webkit-details-marker { + display: none; } -.icon-button.export-button { - background: #f8fafc; +.config-advanced__chevron { + width: 0.4rem; + height: 0.4rem; + border-right: 1.5px solid currentColor; + border-bottom: 1.5px solid currentColor; + transform: rotate(-45deg); + transition: transform 0.2s ease; } -.icon-button.export-button:hover { - background: #e0f2fe; - border-color: #0284c7; - box-shadow: 0 4px 10px rgba(15, 23, 42, 0.12); - transform: translateY(-1px); +.config-advanced[open] > .config-advanced__summary .config-advanced__chevron { + transform: rotate(45deg); } -.icon-button.export-button:active { - transform: translateY(0); - box-shadow: none; +.config-advanced__body { + padding-bottom: 6px; } -/* FULLSCREEN OVERLAY LOADER */ - -.overlay-loader { - position: fixed; - inset: 0; - background: rgba(240, 255, 255, 0.72); - backdrop-filter: blur(2px); +/* Option rows: cross-validation uses the studio switch. */ +.config-option-row { display: flex; align-items: center; - justify-content: center; - z-index: 9999; - pointer-events: all; - animation: fadeIn 0.25s ease-in; + justify-content: space-between; + gap: 16px; + padding: 12px 0; + border-top: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); } -@keyframes fadeIn { - from { - opacity: 0; - } +/* Settings that belong to the switch above sit under it, inset to read as its children. */ +.config-option-detail { + margin: -4px 0 8px; + padding-left: 14px; + border-left: 2px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); +} - to { - opacity: 1; - } +.config-option-detail .config-field { + grid-template-columns: 206px minmax(0, 360px); + padding: 4px 0; } -/* MIP VERSION BAR */ +.config-option-row.is-locked { + opacity: 0.86; +} -.mip-version-bar { +.config-option-copy { display: flex; - align-items: center; - gap: 8px; - padding: 10px 16px; - margin-top: 12px; - background: rgba(248, 250, 252, 0.8); - border: 1px solid rgba(148, 163, 184, 0.3); - border-radius: 8px; - font-size: 0.75rem; - color: #64748b; + flex-direction: column; + gap: 2px; + min-width: 0; } -.mip-version-label { +.config-option-label { + font-size: 0.84rem; font-weight: 600; - text-transform: uppercase; - letter-spacing: 0.08em; - color: #94a3b8; + color: var(--text-main); } -.mip-version-value { - font-family: 'JetBrains Mono', 'Roboto Mono', monospace; - color: #475569; +.config-option-hint { + font-size: 0.76rem; + line-height: 1.4; + color: var(--text-muted); } -/* RESPONSIVE LAYOUT */ - -@media (max-width: 1200px) { - .experiment-container { - grid-template-columns: 240px minmax(0, 1fr); - gap: 12px; - } - - .experiment-panel { - padding: 4px 0 0; - } - - .experiment-info-panel { - right: -360px; - width: 340px; - } +.toggle-switch { + position: relative; + display: inline-block; + flex: 0 0 auto; + width: 44px; + height: 24px; } -/* --- Tablet landscape --- */ -@media (max-width: 992px) { - .experiment-container { - grid-template-columns: minmax(0, 1fr); - } - - .algorithm-panel { - order: 1; - } - - .experiment-panel { - order: 2; - margin-top: 4px; - } - - .experiment-header { - padding-right: 140px; - } - - .run-button { - right: 64px; - } - - .floating-group { - right: 8px; - } - - .experiment-info-panel { - top: 40px; - right: -340px; - width: 320px; - } - - .experiment-info-panel.open { - right: 0; - } +.toggle-switch.is-locked { + cursor: not-allowed; } -/* --- Tablet portrait --- */ -@media (max-width: 768px) { - .experiment-container { - padding: 8px 0; - } - - .algorithm-panel { - border-radius: 0px; - box-shadow: 0 6px 18px rgba(15, 23, 42, 0.06); - } +.toggle-switch input { + opacity: 0; + width: 0; + height: 0; +} - .experiment-header { - flex-direction: column; - align-items: stretch; - gap: 10px; - padding-right: 0; - } +.toggle-switch input:focus-visible + .toggle-slider { + outline: 2px solid var(--primary-color); + outline-offset: 2px; +} - .experiment-name-box { - min-width: 0; - } +.toggle-slider { + position: absolute; + cursor: pointer; + inset: 0; + border-radius: 999px; + border: 1px solid color-mix(in srgb, var(--text-muted) 75%, transparent); + background: color-mix(in srgb, var(--text-muted) 22%, white); + transition: background 0.22s ease, border-color 0.22s ease, box-shadow 0.22s ease; +} - .run-button { - position: static; - align-self: flex-start; - margin-top: 2px; - } +.toggle-slider::before { + content: ''; + position: absolute; + height: 18px; + width: 18px; + left: 3px; + top: 2.5px; + border-radius: 50%; + background: var(--card-bg); + box-shadow: 0 2px 6px rgba(15, 23, 42, 0.18), inset 0 0 0 1px rgba(15, 23, 42, 0.12); + transition: transform 0.22s var(--ease-standard, ease), box-shadow 0.22s ease; +} - .floating-group { - top: 6px; - right: 10px; - } +.toggle-switch input:checked + .toggle-slider { + background: var(--primary-color); + border-color: rgba(43, 51, 233, 0.24); + box-shadow: 0 5px 14px color-mix(in srgb, var(--primary-color) 22%, transparent); +} - .experiment-info-panel { - top: 46px; - right: 12px; - width: min(360px, calc(100vw - 24px)); - } +.toggle-switch input:checked + .toggle-slider::before { + transform: translateX(20px); +} - .experiment-info-panel.open { - right: 12px; - } +.toggle-switch input:disabled + .toggle-slider { + cursor: not-allowed; } -/* --- Small Devices --- */ -@media (max-width: 520px) { - .algorithm-panel { - padding: 12px 12px 10px; - } +/* Muted validation: invalid keeps a danger edge; valid is neutral. */ +.config-input.ng-valid.ng-touched, +.config-select.ng-valid.ng-touched { + border: 1px solid rgba(148, 163, 184, 0.34); + background: #fff; + background-image: none; + box-shadow: none; + padding-right: 12px; +} - .experiment-settings { - padding: 12px 12px; - border-radius: 0px; - } +.config-input.ng-invalid.ng-touched, +.config-select.ng-invalid.ng-touched { + border: 1px solid var(--danger); + background-color: #fff; + background-image: none; + box-shadow: none; + padding-right: 12px; +} - .experiment-info-panel { - position: fixed; - top: 66px; - right: 8px; - left: 8px; - width: auto; - max-height: calc(100vh - 90px); - border-radius: 0px; - z-index: 999; - } +.config-input.ng-invalid.ng-touched:focus, +.config-select.ng-invalid.ng-touched:focus { + border-color: var(--danger); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 16%, transparent); +} - .experiment-info-panel.open { - right: 8px; - } +/* Readiness bar: Run lives in the step bar, so this closes the panel with state only. */ +.algorithm-run-bar { + position: sticky; + bottom: 0; + z-index: 5; + border-radius: 0 0 var(--radius-lg, 12px) var(--radius-lg, 12px); + display: flex; + align-items: center; + justify-content: space-between; + flex-wrap: wrap; + gap: 8px 16px; + padding: 12px 18px; + border-top: 1px solid var(--studio-card-header-border); + background: var(--studio-card-header-bg); + font-size: 0.8rem; + color: var(--text-muted); +} - .info-panel-body { - max-height: calc(100vh - 130px); - } +.algorithm-run-status { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +} - .floating-info-button { - width: 32px; - height: 32px; - font-size: 16px; - } +.algorithm-run-dot { + flex: 0 0 auto; + width: 8px; + height: 8px; + border-radius: 50%; + background: #b45309; +} - .run-button { - width: 100%; - justify-content: center; - text-align: center; - display: inline-flex; - } +.algorithm-run-bar.is-ready .algorithm-run-dot { + background: var(--success); } -/* Success notification toast */ -.success-notification { - position: fixed; - bottom: 24px; - right: 24px; - z-index: 9999; - animation: slideInUp 0.3s ease-out, fadeOut 0.3s ease-in 3.5s forwards; +.algorithm-run-text { + min-width: 0; } -.notification-content { - display: flex; - align-items: center; - gap: 10px; - padding: 14px 20px; - background: white; - border: 1px solid rgba(16, 185, 129, 0.2); - border-radius: 12px; - box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1); - font-size: 0.9rem; - font-weight: 500; +.algorithm-run-bar.is-ready { color: #065f46; + font-weight: 600; } -.notification-content i { - color: #10b981; - font-size: 1.1rem; +.algorithm-run-error { + flex-basis: 100%; + color: #8a1f1f; +} + +/* Empty state under the ③ kicker, without the dashed frame. */ +.algorithm-params__empty { + align-items: flex-start; + justify-content: flex-start; + gap: 6px; + margin: 0; + padding: 16px; + border: 0; + background: transparent; } -@keyframes slideInUp { - from { - opacity: 0; - transform: translateY(20px); +@media (max-width: 900px) { + .algorithm-details__header { + flex-wrap: wrap; } - to { - opacity: 1; - transform: translateY(0); + .config-field, + .config-option-detail .config-field { + grid-template-columns: 1fr; } -} -@keyframes fadeOut { - from { - opacity: 1; + .config-field > * { + grid-column: 1 !important; + grid-row: auto !important; } - to { - opacity: 0; + .algorithm-workspace { + grid-template-columns: 1fr; } -} \ No newline at end of file +} diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.html b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.html index 272c118d6..9ef1e43a5 100644 --- a/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.html +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.html @@ -1,477 +1,462 @@ -<div class="panel-header"> - <p>Algorithm Selection and Configuration</p> -</div> - -<div class="experiment-container"> - <aside class="algorithm-panel"> - <h5>Available Algorithms</h5> +<div class="experiment-container experiment-container--setup" data-guide="experiment-workspace"> - <div class="algo-toolbar"> - <button type="button" class="pill-toggle" [class.on]="showOnlyActive()" (click)="toggleActiveOnly()" [title]=" - showOnlyActive() - ? 'Showing only active algorithms' - : 'Showing all algorithms' - "> - <span class="pill-dot" aria-hidden="true"></span> - {{ showOnlyActive() ? "Active only" : "All" }} - <span class="pill-count"> - {{ algoCounts().active }}/{{ algoCounts().all }} - </span> - </button> - </div> - @if (hasAnyVisibleAlgorithms()) { - <div class="category"> - @for (category of filteredAlgorithmCategories(); track category.name) { - <h6 (click)="toggleCategory(category.name)"> - <span class="expand-icon" [class.expanded]="isCategoryOpen(category.name)">›</span> - {{ category.name }} - </h6> - @if (isCategoryOpen(category.name)) { - <ul> - @for (algorithm of category.algorithms; track algorithm) { - <li (click)="onAlgorithmClick(algorithm)" [class.disabled-algo]="algorithm.isDisabled" - (mouseenter)="showTooltip(algorithm, $event)" (mouseleave)="hideTooltip()" - [class.selected]="algorithm.name === selectedAlgorithm()?.name"> - <div class="algo-item-content"> - <span class="algo-label">{{ algorithm.label }}</span> - <div class="algo-badges"> - @if (algorithm.isDisabled) { - @if (getVariableRequirement(algorithm)) { - <span class="algo-badge algo-badge--req" title="Variable Requirement"> - <i class="fas fa-database"></i> - </span> - } - @if (getCovariateRequirement(algorithm)) { - <span class="algo-badge algo-badge--req" title="Covariate Requirement"> - <i class="fas fa-sliders-h"></i> - </span> - } - } @else { - <span class="algo-badge algo-badge--active"> - <i class="fas fa-check"></i> - </span> - } - </div> - </div> - </li> - } - </ul> - } } +@if (assignablePool.length > 0) { +<section class="algorithm-roles-board algorithm-studio-card" aria-label="Algorithm selection workspace"> + <header class="algorithm-roles-board__header"> + <div> + <h5 class="algorithm-roles-board__title">Algorithm</h5> </div> - } @else { - <div class="no-algos-empty"> - <div class="no-algos-title">No available algorithms.</div> - <div class="no-algos-subtitle">Select variables to get started.</div> - </div> - } + <span class="algorithm-count-badge">{{ algoCounts().active }} of {{ algoCounts().all }} runnable</span> + </header> - <!-- Legend for requirement icons --> - <div class="algo-legend"> - <div class="legend-item" title="Data requirements (e.g. types, count)"> - <span class="algo-badge algo-badge--req legend-icon"> - <i class="fas fa-database"></i> - </span> - <span class="legend-label">Variable Req.</span> - </div> - <div class="legend-item" title="Covariate requirements"> - <span class="algo-badge algo-badge--req legend-icon"> - <i class="fas fa-sliders-h"></i> - </span> - <span class="legend-label">Covariate Req.</span> - </div> - </div> - </aside> - - <!-- Tooltip for Algorithm Details (Moved outside to avoid backdrop-filter coordinate issues) --> - @if (tooltipVisible) { - <div class="tooltip" [style.top.px]="tooltipPosition.y" [style.left.px]="tooltipPosition.x"> - <h5 class="tooltip-title">{{ tooltipData?.label }}</h5> - <div class="tooltip-description" [innerHTML]="tooltipData?.description"></div> - - @if (getVariableRequirement() || getCovariateRequirement()) { - <div class="tooltip-requirements"> - <div class="tooltip-requirements-header">Requirements</div> - @if (getVariableRequirement()) { - <div class="req-item"> - <i class="fas fa-database req-icon"></i> - <span class="req-text">{{ getVariableRequirement() }}</span> - </div> - } - @if (getCovariateRequirement()) { - <div class="req-item"> - <i class="fas fa-sliders-h req-icon"></i> - <span class="req-text">{{ getCovariateRequirement() }}</span> - </div> - } - </div> - } + <div class="algorithm-pool"> + <app-algorithm-role-assignment></app-algorithm-role-assignment> </div> - } - <main class="experiment-panel"> - <header> - <div class="experiment-header"> - <div class="header-title-group"> - <span class="icon-pill" aria-hidden="true"> - <i class="fas fa-microchip"></i> - </span> - <div class="header-text"> - <h4 class="experiment-title"> - {{ result() ? "Experiment Result" : (selectedAlgorithm()?.label || "Algorithm Configuration") }} - </h4> - <p class="experiment-subtitle"> - {{ result() ? "Review analysis output" : "Configure parameters and run" }} - </p> - </div> + <div class="algorithm-workspace"> + <aside class="algorithm-catalog" data-guide="algorithm-selection" aria-labelledby="catalog-title"> + <div class="step-kicker algorithm-step-kicker"><span class="step-kicker-n">2</span> Method</div> + <h2 class="catalog-title sr-only" id="catalog-title">Algorithm catalog</h2> + + <div class="catalog-search"> + <div class="catalog-search-field"> + <label class="sr-only" for="algo-search">Search algorithms</label> + <input + id="algo-search" + class="catalog-search-input" + type="search" + placeholder="Search methods…" + autocomplete="off" + [value]="algoSearchQuery()" + (input)="onSearchInput($event)" + /> + @if (searchActive()) { + <button + type="button" + class="catalog-search-clear" + (click)="clearSearch()" + aria-label="Clear algorithm search" + >×</button> + } </div> - - <div class="header-actions-group"> - @if (result() && !saveAsMode()) { - <button class="secondary-action-btn back-btn" (click)="backToParameters()" title="Edit Parameters"> - <i class="fas fa-chevron-left"></i> - <span>Edit Parameters</span> + <div class="modal-tabs catalog-mode-tabs" role="group" aria-label="Method filter"> + <button type="button" [class.active]="showOnlyActive()" [attr.aria-pressed]="showOnlyActive()" + (click)="showOnlyActive() || toggleActiveOnly()"> + Runnable {{ algoCounts().active }} </button> - <button class="primary-action-btn run-button save-as" (click)="toggleSaveAsMode()"> - Save As + <button type="button" [class.active]="!showOnlyActive()" [attr.aria-pressed]="!showOnlyActive()" + (click)="!showOnlyActive() || toggleActiveOnly()"> + All {{ algoCounts().all }} </button> - } @else if (saveAsMode()) { - <div class="save-as-container"> - <input type="text" [ngModel]="saveAsName()" (ngModelChange)="saveAsName.set($event)" - placeholder="Experiment name" class="save-as-input" /> - <button class="primary-action-btn run-button" (click)="onSaveAs()" - [disabled]="!saveAsName().trim() || isRunning()"> - {{ isRunning() ? "Saving..." : "Save" }} - </button> - <button class="secondary-action-btn cancel-btn" (click)="cancelSaveAs()"> - ✕ + </div> + </div> + + @if (hasAnyVisibleAlgorithms()) { + <div class="algorithm-match-rows"> + @for (category of filteredAlgorithmCategories(); track category.name) { + <div class="algorithm-match-row"> + <button + type="button" + class="algorithm-match-cat" + [attr.aria-expanded]="isMatchCategoryOpen(category.name) || searchActive()" + (click)="toggleMatchCategory(category.name)" + > + <span class="algorithm-match-cat-name">{{ category.name }}</span> + <span class="algorithm-match-cat-count">{{ categoryRunnableCount(category) }} runnable</span> + <span class="algorithm-match-cat-meta" aria-hidden="true"></span> + </button> + @if (isMatchCategoryOpen(category.name) || searchActive()) { + <div class="algorithm-match-tiles"> + @for (algorithm of category.algorithms; track algorithm.name) { + <button + type="button" + class="algo-tile" + [class.disabled-algo]="algorithm.isDisabled" + [class.selected]="algorithm.name === selectedAlgorithm()?.name" + [attr.aria-disabled]="algorithm.isDisabled" + (click)="onAlgorithmClick(algorithm)" + [title]="algorithm.description || algorithm.label" + > + <span class="algo-tile-top"> + <span class="algo-label">{{ algorithm.label }}</span> + <span class="algo-tile-cue" aria-hidden="true"></span> + </span> + @if (algorithm.isDisabled) { + <span class="algo-why">{{ algorithmUnavailableReason(algorithm) }}</span> + } </button> + } </div> - } @else { - <button class="primary-action-btn run-button" [disabled]="isRunButtonDisabled()" (click)="onClickRunExp()" - [title]="isRunButtonDisabled() ? 'Complete variable and algorithm selection to proceed' : 'Execute experiment'"> - {{ isRunning() ? "Running..." : "Run Experiment" }} - </button> } + </div> + } + </div> + } @else if (searchActive()) { + <p class="algorithm-match-empty">No methods match “{{ algoSearchQuery() }}”.</p> + } @else if (showOnlyActive()) { + <p class="algorithm-match-empty">No runnable methods for this assignment. Switch to All to see why.</p> + } @else { + <p class="algorithm-match-empty">No methods in the catalog.</p> + } + </aside> + + <div class="algorithm-config-column"> + @if (selectedAlgorithm(); as algorithm) { + <section class="algorithm-details" aria-label="Algorithm configuration"> + <div class="algorithm-details__header"> + <div class="algorithm-details__heading"> + <div class="step-kicker"><span class="step-kicker-n">3</span></div> + <h2 class="algorithm-details__title">{{ algorithm.label }}</h2> + </div> + @if (selectedAlgorithmCategory()) { + <span class="inspector-tag">{{ selectedAlgorithmCategory() }}</span> + } + </div> + @if (algorithm.description) { + <p class="config-description">{{ algorithm.description }}</p> + } + @if (selectedAlgorithmDocumentation()) { + <details class="documentation-panel"> + <summary class="documentation-summary"> + <span class="documentation-chevron" aria-hidden="true"></span> + <span class="documentation-label">How this method works</span> + </summary> + <div class="documentation-content">{{ selectedAlgorithmDocumentation() }}</div> + </details> + } + </section> + } - <div class="floating-group"> - <button class="floating-info-button" type="button" (click)="toggleInfoPanel()"> - i - </button> - - <aside id="info-panel" class="experiment-info-panel" [class.open]="infoPanelOpen"> - <header class="info-panel-header"> - <div class="info-title-block"> - <h3>Experiment summary</h3> - <p class="info-subtitle"> - Quick overview of your inputs & configuration. - </p> - </div> - - <button class="info-close-btn" type="button" (click)="toggleInfoPanel()" aria-label="Close"> - ✕ + <section class="algorithm-params" data-guide="algorithm-settings" aria-label="Parameter selection"> + @if (selectedAlgorithm(); as algorithm) { + <div class="algorithm-params__body"> + @if (algorithmNeeds().length) { + <ul class="algo-needs algorithm-requirements-panel" aria-label="Requirements before running"> + <li class="algorithm-requirements-heading sr-only">Complete before running</li> + @for (need of algorithmNeeds(); track need.key) { + <li class="algorithm-requirement-item" [class.is-met]="need.met"> + <span class="algo-needs-mark" aria-hidden="true"></span> + <span>{{ need.title }}</span> + @for (message of need.messages; track message) { + <small>{{ message }}</small> + } + @if (need.action) { + <button type="button" class="algorithm-requirement-action" (click)="resolveRunRequirement(need.action)"> + {{ need.action.actionLabel }} </button> - </header> + } + </li> + } + </ul> + } @else if (selectedAlgorithm()) { + <p class="algo-needs-all-met">✓ All inputs match</p> + } - <div class="info-panel-body"> + <fieldset class="algorithm-readonly-fieldset" [disabled]="selectedAlgorithmUnavailable()" [style.pointer-events]="selectedAlgorithmUnavailable() ? 'none' : null"> - <!-- DATASETS --> - <div class="info-row"> - <div class="info-label">Datasets</div> - <div class="info-value"> - @if (experimentInfo().datasets.length) { - <div class="info-chips"> - @for (d of experimentInfo().datasets; track d) { - <span class="info-chip">{{ d }}</span> - } - </div> - } @else { - <span class="info-empty-pill">None selected</span> - } - </div> - </div> - - <!-- VARIABLES --> - <div class="info-row"> - <div class="info-label">Variables</div> - <div class="info-value"> - @if (experimentInfo().variables.length) { - <div class="info-chips"> - @for (v of experimentInfo().variables; track v.code) { - <span class="info-chip"> - {{ v.label || v.name || v.code }} - </span> - } - </div> - } @else { - <span class="info-empty-pill">None selected</span> - } - </div> - </div> - - <!-- COVARIATES --> - <div class="info-row"> - <div class="info-label">Covariates</div> - <div class="info-value"> - @if (experimentInfo().covariates.length) { - <div class="info-chips"> - @for (c of experimentInfo().covariates; track c.code) { - <span class="info-chip info-chip--soft"> - {{ c.label || c.name || c.code }} - </span> - } - </div> - } @else { - <span class="info-empty-pill">None selected</span> - } - </div> - </div> + @if (hasAlgorithmParameterControls()) { + <h3 class="algorithm-params__title">Parameters</h3> + } - <!-- FILTERS --> - <div class="info-row"> - <div class="info-label">Filters</div> - <div class="info-value"> - @if (experimentInfo().filters.length) { - <div class="info-chips"> - @for (f of experimentInfo().filters; track f.code) { - <span class="info-chip info-chip--outline"> - {{ f.label || f.name || f.code }} - </span> - } - </div> - } @else { - <span class="info-empty-pill">No filters</span> - } - </div> + @if (isOutlierReportSelected()) { + <section class="outlier-report-editor" aria-label="Outlier report configuration"> + <div class="outlier-editor-header"> + <div> + <h6>Per-variable Outlier Rules</h6> </div> + <span class="outlier-count">{{ outlierReportVariables().length }} numerical</span> + </div> - <!-- ALGORITHM CONFIGS --> - <div class="info-row info-row--stacked"> - <div class="info-label">Algorithm configs</div> - <div class="info-value"> - @if (objectKeys(experimentInfo().algorithmConfigs).length) { - <dl class="info-config-list"> - @for (k of objectKeys(experimentInfo().algorithmConfigs); - track k) { - <div class="info-config-item"> - <dt>{{ prettifyKey(k) }}</dt> - <dd>{{ experimentInfo().algorithmConfigs[k] }}</dd> - </div> - } - </dl> - } @else { - <span class="info-empty-pill">No custom configs</span> + @if (outlierReportVariables().length > 0) { + <div class="outlier-table-wrapper"> + <table class="outlier-table"> + <thead> + <tr> + <th>Variable</th> + <th>Enabled</th> + <th>Strategy</th> + <th>Tail</th> + <th>Fold</th> + </tr> + </thead> + <tbody> + @for (variable of outlierReportVariables(); track variable.code) { + <tr> + <td> + <span class="outlier-variable-label">{{ outlierVariableLabel(variable) }}</span> + <small>{{ variable.code }}</small> + </td> + <td> + <label class="checkbox-wrapper outlier-enable"> + <input type="checkbox" [checked]="outlierReportRuleFor(variable).enabled" + (change)="onOutlierReportEnabledChange(variable, $any($event.target).checked)" + [attr.aria-label]="'Enable ' + outlierVariableLabel(variable)" /> + </label> + </td> + <td> + <select class="config-select compact-select" [ngModel]="outlierReportRuleFor(variable).strategy" + (ngModelChange)="onOutlierReportStrategyChange(variable, $event)"> + @for (strategy of outlierStrategies; track strategy.value) { + <option [ngValue]="strategy.value">{{ strategy.label }}</option> + } + </select> + </td> + <td> + <select class="config-select compact-select" [ngModel]="outlierReportRuleFor(variable).tail" + (ngModelChange)="onOutlierReportTailChange(variable, $event)"> + @for (tail of outlierTails; track tail.value) { + <option [ngValue]="tail.value">{{ tail.label }}</option> + } + </select> + </td> + <td> + <input class="config-input compact-input" type="number" step="any" + [ngModel]="outlierReportRuleFor(variable).fold" + (ngModelChange)="onOutlierReportFoldChange(variable, $event)" + [class.input-error]="outlierReportRuleError(variable)" /> + @if (outlierReportRuleError(variable)) { + <div class="validation-message config-error">{{ outlierReportRuleError(variable) }}</div> + } + </td> + </tr> } - </div> + </tbody> + </table> + </div> + } @else { + <div class="config-placeholder config-placeholder--minimal"> + <div class="config-placeholder-content"> + <h6>No numerical variables selected</h6> </div> </div> - </aside> - </div> - </div> - </div> - </header> - - @if (isRunning()) { - <div class="panel-loader"> - <app-spinner [overlay]="false"></app-spinner> - <p>{{ loadingText() }}</p> - </div> - } @else if (result()) { - <section class="result-section" #resultSection> - <div class="result-header"> - <div class="header-spacer"></div> - <h5>{{ resultDisplayTitle() }}</h5> + } + </section> + } - <!-- Export as PDF button --> - <button class="icon-button export-button" type="button" (click)="onExportResult(resultSection)" - title="Export as PDF"> - <svg width="16" height="16" viewBox="0 0 24 24" aria-hidden="true"> - <path d="M5 20h14v-2H5v2zm7-16v9.17l3.59-3.58L17 11l-5 5-5-5 1.41-1.41L11 13.17V4h2z" /> - </svg> - <span>Export PDF</span> - </button> - </div> - <app-algorithm-result [result]="result()" [schema]="lastUsedSchema()" [algorithm]="lastUsedAlgorithm" - [enumMaps]="enumMaps()" [yVar]="yVar()" [xVar]="xVar()" [labelMap]="labelMap()" - [algorithmLabel]="resultAlgorithmLabel()" /> + @if (primaryConfigSchema().length || advancedConfigSchema().length) { + <form [formGroup]="configForm()" class="config-form flex flex-col gap-4"> + @for (field of primaryConfigSchema(); track field.key) { + <ng-container *ngTemplateOutlet="configField; context: { $implicit: field }"></ng-container> + } - <div class="mip-version-bar"> - <span class="mip-version-label">MIP Version:</span> - <span class="mip-version-value">{{ mipVersion }}</span> - </div> - </section> - } @else if (selectedAlgorithm()) { - <section class="experiment-settings"> - <h5 class="config-title"> - {{ selectedAlgorithm()?.label }} Configuration - </h5> - <p class="config-description">{{ selectedAlgorithm()?.description }}</p> + @if (advancedConfigSchema().length) { + <details class="config-advanced"> + <summary class="config-advanced__summary"> + <span class="config-advanced__chevron" aria-hidden="true"></span> + Advanced ({{ advancedConfigSchema().length }}) + </summary> + <div class="config-advanced__body"> + @for (field of advancedConfigSchema(); track field.key) { + <ng-container *ngTemplateOutlet="configField; context: { $implicit: field }"></ng-container> + } + </div> + </details> + } + </form> + } - @if (canToggleCrossValidation()) { - <div class="cross-validation-toggle"> - <label class="checkbox-wrapper"> - <input type="checkbox" [checked]="crossValidationEnabled()" (change)="toggleCrossValidation($event)" /> - <span>Enable cross-validation</span> - </label> - <div class="cross-validation-hint"> - Optional. Enable to run the cross-validation variant (default: 5 folds). - </div> - </div> - } + @if (canToggleCrossValidation() || isCrossValidationOnly()) { + <div class="config-option-row" [class.is-locked]="isCrossValidationOnly()"> + <div class="config-option-copy"> + <span class="config-option-label">Cross-validation</span> + <span class="config-option-hint"> + {{ isCrossValidationOnly() ? 'Always on for this method' : 'Runs the cross-validation variant and reports fold metrics' }} + </span> + </div> + <label class="toggle-switch" [class.is-locked]="isCrossValidationOnly()"> + <input type="checkbox" [checked]="crossValidationEnabled()" [disabled]="isCrossValidationOnly()" + (change)="toggleCrossValidation($event)" /> + <span class="toggle-slider" aria-hidden="true"></span> + <span class="sr-only">Cross-validation</span> + </label> + </div> - @if (isCrossValidationOnly()) { - <div class="cross-validation-toggle"> - <div class="cross-validation-hint"> - Cross-validation is enabled for this algorithm. - </div> - </div> - } + @if (crossValidationConfigSchema().length) { + <div class="config-option-detail"> + @for (field of crossValidationConfigSchema(); track field.key) { + <ng-container *ngTemplateOutlet="configField; context: { $implicit: field }"></ng-container> + } + </div> + } + } - @if (canToggleTransformation()) { - <div class="transformation-toggle"> - <label class="checkbox-wrapper"> - <input type="checkbox" [checked]="transformationEnabled()" (change)="toggleTransformation($event)" /> - <span>Enable data transformations</span> - </label> - <div class="transformation-hint"> - Optional. Pick a single transformation per variable. - </div> + @if (!hasAlgorithmParameterControls()) { + <div class="config-placeholder config-placeholder--minimal"> + <div class="config-placeholder-content"> + <h6>No additional parameters for this algorithm</h6> + <p> + This algorithm does not expose extra configuration fields. Review the + documentation and run the experiment when you are ready. + </p> + </div> + </div> + } - @if (!selectedVariables.length) { - <div class="transformation-empty"> - Select variables to configure transformations. + </fieldset> + + <ng-template #configField let-field> + @if (configForm().get(field.key)) { + <div class="config-field" [formGroup]="configForm()"> + <label> + {{ prettyFieldLabel(field) }} + @if (!field.required && !isCrossValidationField(field)) { + <span class="optional-tag">optional</span> + } + </label> + + @if (field.desc) { + <p class="config-field-desc">{{ field.desc }}</p> + } + + @if (field.enumType === 'input_var_names' && field.type === 'select' && (!field.options || !field.options.length)) { + <p class="config-field-hint"> + @if (field.enumSource?.includes('x')) { + Select covariates first. This parameter must be one of the variables in x. + } @else if (field.enumSource?.includes('y')) { + Select a variable first. This parameter must be one of the variables in y. + } @else { + Select the required variables before choosing this parameter. + } + </p> + } + + @if (field.key === 'positive_class' && !selectedEventVarCode()) { + <p class="config-field-hint">Select an event indicator variable first.</p> + } + + @if (field.key === 'positive_class' && selectedEventVarCode() && positiveClassOptions().length) { + <p class="config-field-hint"> + Select the event level mapped to 1; all other levels are mapped to 0. + </p> + } + + @if (field.key === 'positive_class' && selectedEventVarCode() && !positiveClassOptions().length) { + <p class="config-field-hint"> + This event variable has no catalog categories. Enter the value treated as event=1 (for example 0 or 1). + </p> + } + + @if (field.type === 'number') { + <input type="number" [formControlName]="field.key" class="config-input" [min]="field.min" [max]="field.max" + [step]=" + field.types?.includes('real') || + field.key === 'alpha' || + field.key === 'tol' || + field.key === 'mu' + ? 'any' + : 1 + " [placeholder]=" + field.default !== undefined ? field.default : 'Enter value...' + " /> + + } @else if (field.type === 'select') { + <select [formControlName]="field.key" class="config-select"> + <option [ngValue]="null" [disabled]="field.required"> + {{ field.required ? '-- Select --' : 'None (optional)' }} + </option> + + @for (opt of field.options; track opt) { + <option [ngValue]="bindOptionValue(opt)"> + {{ opt.label ?? opt.name ?? opt }} + </option> + } + </select> + } @else if (field.type === 'multi-select') { + <div class="chip-picker" role="group" [attr.aria-label]="prettyFieldLabel(field)"> + @for (opt of field.options; track opt) { + <button type="button" class="chip-picker-option" + [class.active]="isMultiSelectOptionSelected(field.key, opt)" + (click)="toggleMultiSelectOption(field.key, opt)"> + {{ opt.label ?? opt.name ?? opt }} + </button> + } + </div> + } @else if (field.type === 'checkbox') { + <label class="checkbox-wrapper"> + <input type="checkbox" [formControlName]="field.key" /> + <span>Enable</span> + </label> + } @else { + <input type="text" [formControlName]="field.key" class="config-input" placeholder="Enter value..." /> + } + + @if (configForm().get(field.key)?.invalid && + configForm().get(field.key)?.touched) { + <div class="validation-message config-error"> + @if (configForm().get(field.key)?.hasError('required')) { + <small>This field is required.</small> + } @if (configForm().get(field.key)?.hasError('min')) { + <small>Value too small.</small> + } @if (configForm().get(field.key)?.hasError('max')) { + <small>Value too large.</small> + } @if (configForm().get(field.key)?.hasError('pattern')) { + <small>Invalid value.</small> + } @if (configForm().get(field.key)?.hasError('invalidOption')) { + <small>Select a value from the list.</small> + } + </div> + } + </div> + } + </ng-template> </div> } @else { - <div class="transformation-grid" [class.disabled]="!transformationEnabled()"> - <div class="transformation-header">Variable</div> - <div class="transformation-header">None</div> - <div class="transformation-header">Standardize</div> - <div class="transformation-header">Center</div> - <div class="transformation-header">Exp</div> - - @for (v of selectedVariables; track v.code) { - <div class="transformation-cell transformation-label"> - {{ v.label || v.name || v.code }} - </div> - <div class="transformation-cell"> - <input type="radio" [name]="'transform-' + v.code" [value]="'none'" - [checked]="getTransformationAssignment(v.code) === 'none'" - (change)="setTransformationAssignment(v.code, 'none')" [disabled]="!transformationEnabled()" /> - </div> - <div class="transformation-cell"> - <input type="radio" [name]="'transform-' + v.code" [value]="'standardize'" - [checked]="getTransformationAssignment(v.code) === 'standardize'" - (change)="setTransformationAssignment(v.code, 'standardize')" [disabled]="!transformationEnabled()" /> - </div> - <div class="transformation-cell"> - <input type="radio" [name]="'transform-' + v.code" [value]="'center'" - [checked]="getTransformationAssignment(v.code) === 'center'" - (change)="setTransformationAssignment(v.code, 'center')" [disabled]="!transformationEnabled()" /> - </div> - <div class="transformation-cell"> - <input type="radio" [name]="'transform-' + v.code" [value]="'exp'" - [checked]="getTransformationAssignment(v.code) === 'exp'" - (change)="setTransformationAssignment(v.code, 'exp')" [disabled]="!transformationEnabled()" /> - </div> - } + <div class="algorithm-params__empty"> + <div class="step-kicker"><span class="step-kicker-n">3</span></div> + <h3 class="algorithm-params__empty-title">No algorithm selected</h3> + <p class="algorithm-params__empty-text"> + Select a runnable algorithm from the catalog to configure parameters and run the + experiment. + </p> </div> } - </div> - } + </section> + </div> + </div> - @if (errorMsg()) { - <div class="backend-error-banner"> - <strong>Backend error: </strong> - <span>{{ errorMsg() }}</span> + <div class="algorithm-run-bar" [class.is-ready]="canRun()" role="status"> + <span class="algorithm-run-status"> + <span class="algorithm-run-dot" aria-hidden="true"></span> + <span class="algorithm-run-text">{{ readinessText() }}</span> + </span> + @if (!canRun()) { + <span class="algorithm-run-hint">Finish the needs above to enable the run.</span> + } + @if (errorMsg()) { + <span class="algorithm-run-error">Backend error: {{ errorMsg() }}</span> + } + </div> +</section> + +@if (tooltipVisible()) { +<div class="tooltip" [style.top.px]="tooltipPosition().y" [style.left.px]="tooltipPosition().x"> + <h5 class="tooltip-title">{{ tooltipData()?.label }}</h5> + <div class="tooltip-description" [innerHTML]="tooltipData()?.description"></div> + + @if (availabilityDetails().length) { + <div class="tooltip-requirements"> + <div class="tooltip-requirements-header">Availability</div> + @for (detail of availabilityDetails(); track detail.role) { + <div class="req-item" [class.req-item--failed]="detail.messages.length > 0"> + <div class="req-copy"> + <span class="req-text">{{ availabilityRequirementText(detail) }}</span> + @for (message of detail.messages; track message) { + <span class="req-error">{{ message }}</span> + } </div> - - } @if (enrichedConfigSchema().length) { - <form [formGroup]="configForm()" class="config-form flex flex-col gap-4"> - @for (field of enrichedConfigSchema(); track field.key) { @if - (configForm().get(field.key)) { - <div class="config-field"> - <label class="font-semibold mb-1 text-[var(--primary-color)] flex items-center gap-2"> - {{ prettyFieldLabel(field) }} - @if (field.required) { - <span class="required-tag">Required</span> - } - </label> - - @if (field.type === 'number') { - <input type="number" [formControlName]="field.key" class="config-input" [min]="field.min" [max]="field.max" - [step]=" - field.types?.includes('real') || - field.key === 'alpha' || - field.key === 'tol' || - field.key === 'mu' - ? 'any' - : 1 - " [placeholder]=" - field.default !== undefined ? field.default : 'Enter value...' - " /> - - } @else if (field.type === 'select') { - <select [formControlName]="field.key" class="config-select"> - <option [ngValue]="null" [selected]="!configForm().get(field.key)?.value" disabled value=""> - -- Select -- - </option> - - @for (opt of field.options; track opt) { - <option [ngValue]="opt.code ?? opt"> - {{ opt.label ?? opt.name ?? opt }} - </option> - } - </select> - } @else if (field.type === 'checkbox') { - <label class="checkbox-wrapper"> - <input type="checkbox" [formControlName]="field.key" /> - <span>Enable</span> - </label> - } @else { - <input type="text" [formControlName]="field.key" class="config-input" placeholder="Enter value..." /> - } - <!-- Extra info section under each field --> - <div class="field-meta"> - @if (field.desc) { - <div class="desc-text">{{ field.desc }}</div> - } @if ( field.min !== null && field.min !== undefined && field.max - !== null && field.max !== undefined) { - <div class="range-hint"> - Range: {{ field.min }} to {{ field.max }} - </div> - } @if (field.default !== null && field.default !== undefined) { - <div class="default-hint">Default: {{ field.default }}</div> - } - </div> - - @if (configForm().get(field.key)?.invalid && - configForm().get(field.key)?.touched) { - <div class="validation-message config-error"> - @if (configForm().get(field.key)?.hasError('required')) { - <small>This field is required.</small> - } @if (configForm().get(field.key)?.hasError('min')) { - <small>Value too small.</small> - } @if (configForm().get(field.key)?.hasError('max')) { - <small>Value too large.</small> - } @if (configForm().get(field.key)?.hasError('pattern')) { - <small>Invalid value.</small> - } - </div> - } - </div> - } } - </form> - } - </section> + </div> } - </main> -</div> - -@if (showSuccessNotification()) { -<div class="success-notification" role="status" aria-live="polite"> - <div class="notification-content"> - <i class="fas fa-check-circle"></i> - <span>Experiment saved successfully!</span> </div> + } +</div> +} +} @else { +<section class="algorithm-panel-empty" aria-label="Algorithm configuration placeholder"> + <h5 class="algorithm-panel-empty__title">Select variables first</h5> + <p class="algorithm-panel-empty__text"> + Add variables on Datasets & Variables, then assign an outcome (y) and predictors (x) here. + </p> +</section> +} </div> -} \ No newline at end of file diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.spec.ts b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.spec.ts new file mode 100644 index 000000000..e44f5a89d --- /dev/null +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.spec.ts @@ -0,0 +1,1240 @@ +import type { MockedObject, Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideRouter } from '@angular/router'; +import { AuthService } from '../../../services/auth.service'; +import { ErrorService } from '../../../services/error.service'; +import { PdfExportService } from '../../../services/pdf-export.service'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; +import { SessionStorageService } from '../../../services/session-storage.service'; +import { AlgorithmConfig } from '../../../models/algorithm-definition.model'; +import { AlgorithmPanelComponent } from './algorithm-panel.component'; +import { of, throwError } from 'rxjs'; +import { createSpyObj } from '../../../testing/spy-obj.testing'; + +describe('AlgorithmPanelComponent', () => { + let fixture: ComponentFixture<AlgorithmPanelComponent>; + let studioNavigation: MockedObject<ExperimentStudioNavigationService>; + let experimentStudioService: { + selectedAlgorithm: ReturnType<typeof signal<AlgorithmConfig | null>>; + selectedVariables: ReturnType<typeof signal<any[]>>; + algorithmY: ReturnType<typeof signal<any[]>>; + algorithmX: ReturnType<typeof signal<any[]>>; + algorithmAssignableVariables: ReturnType<typeof signal<any[]>>; + unavailableVariables: ReturnType<typeof signal<ReadonlyMap<string, readonly string[]>>>; + selectedFilters: ReturnType<typeof signal<any[]>>; + selectedDatasets: ReturnType<typeof signal<string[]>>; + selectedDataModel: ReturnType<typeof signal<any>>; + algorithmConfigurations: ReturnType<typeof signal<Record<string, any>>>; + availableGroupedAlgorithms: ReturnType<typeof signal<Record<string, AlgorithmConfig[]>>>; + backendAlgorithms: ReturnType<typeof signal<Record<string, AlgorithmConfig>>>; + isRunning: ReturnType<typeof signal<boolean>>; + currentExperimentUUID: ReturnType<typeof signal<string | null>>; + lastUsedAlgorithm: ReturnType<typeof signal<string>>; + runResult: ReturnType<typeof signal<any>>; + runId: ReturnType<typeof signal<string | null>>; + runError: ReturnType<typeof signal<string | null>>; + lastRunSchema: ReturnType<typeof signal<any[]>>; + hasRunStarted: ReturnType<typeof signal<boolean>>; + runStatusText: ReturnType<typeof signal<string>>; + variableLabelMap: ReturnType<typeof signal<Record<string, string>>>; + notifySaveSucceeded: Mock; + availableDatasets: ReturnType<typeof signal<any[]>>; + getCategoricalEnumMaps: Mock; + isCrossValidationOnly: Mock; + getCrossValidationBase: Mock; + getCrossValidationVariant: Mock; + hasAppliedDescriptivePreprocessing: Mock; + hasRequestPreprocessingForRun: Mock; + isAlgorithmAvailable: Mock; + getAlgorithmAvailability: Mock; + setAlgorithm: Mock; + setRunning: Mock; + runSelectedAlgorithm: Mock; + captureRunSetup: Mock; + getEffectivePreprocessingSummary: Mock; + getDatasetLabelMap: Mock; + getAppliedDescriptivePreprocessing: Mock; + formatPreprocessingEntries: Mock; + filterLogic: ReturnType<typeof signal<any>>; + isCrossValidationAlgorithm: Mock; + }; + + const algorithm: AlgorithmConfig = { + name: 'flat_config_algorithm', + label: 'Flat Config Algorithm', + description: 'Algorithm with more than three configuration fields.', + documentation: 'Line one.\nLine two.', + category: 'Test', + requiredVariable: [], + covariate: [], + configSchema: [ + { key: 'first', label: 'First', type: 'number', default: 1 }, + { key: 'second', label: 'Second', type: 'number', default: 2 }, + { key: 'third', label: 'Third', type: 'number', default: 3 }, + { key: 'fourth', label: 'Fourth', type: 'number', default: 4 }, + ], + isDisabled: false, + }; + + beforeEach(async () => { + studioNavigation = createSpyObj<ExperimentStudioNavigationService>( + 'ExperimentStudioNavigationService', + ['navigateToSection', 'goToPreprocessing', 'publishState'], + ); + + experimentStudioService = { + selectedAlgorithm: signal<AlgorithmConfig | null>(algorithm), + selectedVariables: signal<any[]>([{ code: 'y', label: 'Y variable' }]), + algorithmY: signal<any[]>([]), + algorithmX: signal<any[]>([]), + algorithmAssignableVariables: signal<any[]>([{ code: 'y', label: 'Y variable', type: 'continuous' }]), + unavailableVariables: signal<ReadonlyMap<string, readonly string[]>>(new Map()), + selectedFilters: signal<any[]>([]), + selectedDatasets: signal<string[]>([]), + selectedDataModel: signal<any>(null), + algorithmConfigurations: signal<Record<string, any>>({}), + availableGroupedAlgorithms: signal<Record<string, AlgorithmConfig[]>>({ Test: [algorithm] }), + backendAlgorithms: signal<Record<string, AlgorithmConfig>>({ flat_config_algorithm: algorithm }), + isRunning: signal(false), + currentExperimentUUID: signal<string | null>(null), + lastUsedAlgorithm: signal(''), + runResult: signal<any>(null), + runId: signal<string | null>(null), + runError: signal<string | null>(null), + lastRunSchema: signal<any[]>([]), + hasRunStarted: signal(false), + runStatusText: signal('Processing experiment...'), + variableLabelMap: signal<Record<string, string>>({}), + notifySaveSucceeded: vi.fn(), + availableDatasets: signal<any[]>([]), + getCategoricalEnumMaps: vi.fn().mockReturnValue({}), + isCrossValidationOnly: vi.fn().mockReturnValue(false), + getCrossValidationBase: vi.fn().mockReturnValue(null), + getCrossValidationVariant: vi.fn().mockReturnValue(null), + hasAppliedDescriptivePreprocessing: vi.fn().mockReturnValue(true), + hasRequestPreprocessingForRun: vi.fn().mockReturnValue(false), + isAlgorithmAvailable: vi.fn().mockReturnValue(true), + getAlgorithmAvailability: vi.fn().mockReturnValue({ + available: true, + summary: '', + details: [], + }), + setAlgorithm: vi.fn().mockImplementation((next: AlgorithmConfig) => { + experimentStudioService.selectedAlgorithm.set(next); + }), + setRunning: vi.fn(), + runSelectedAlgorithm: vi.fn(), + captureRunSetup: vi.fn(), + getEffectivePreprocessingSummary: vi.fn().mockReturnValue(null), + getDatasetLabelMap: vi.fn().mockReturnValue({}), + getAppliedDescriptivePreprocessing: vi.fn().mockReturnValue(null), + formatPreprocessingEntries: vi.fn().mockReturnValue([]), + filterLogic: signal(null), + isCrossValidationAlgorithm: vi.fn().mockReturnValue(false), + }; + + await TestBed.configureTestingModule({ + imports: [AlgorithmPanelComponent], + providers: [ + provideZonelessChangeDetection(), + provideRouter([]), + { provide: ExperimentStudioService, useValue: experimentStudioService }, + { provide: ErrorService, useValue: { clearError: vi.fn() } }, + { provide: AuthService, useValue: { currentUser: null } }, + { provide: PdfExportService, useValue: { exportExperimentPdf: vi.fn() } }, + { provide: RuntimeEnvService, useValue: { mipVersion: 'test' } }, + { provide: SessionStorageService, useValue: {} }, + { provide: ExperimentStudioNavigationService, useValue: studioNavigation }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(AlgorithmPanelComponent); + fixture.detectChanges(); + }); + + it('groups default optional parameter fields under a closed Advanced disclosure', async () => { + fixture.componentInstance.setStudioSubstep('parameters'); + await fixture.whenStable(); + fixture.detectChanges(); + + const nativeElement = fixture.nativeElement as HTMLElement; + const details = nativeElement.querySelector('.config-advanced') as HTMLDetailsElement | null; + expect(details).toBeTruthy(); + expect(details?.open).toBe(false); + expect(details?.textContent).toContain('Advanced (4)'); + expect(nativeElement.querySelectorAll('.config-field').length).toBe(4); + expect(nativeElement.textContent).toContain('Fourth'); + expect((nativeElement.querySelector('.algorithm-readonly-fieldset') as HTMLFieldSetElement)?.disabled).toBe(false); + expect(fixture.componentInstance.canRun()).toBe(true); + }); + + describe('variables missing from a selected dataset', () => { + const unavailable = new Map<string, readonly string[]>([ + ['age', ['Dataset B']], + ['sex', ['Dataset B', 'Dataset C']], + ]); + + it('blocks the run while an outcome, predictor or filter is unavailable', () => { + const component = fixture.componentInstance; + experimentStudioService.unavailableVariables.set(unavailable); + experimentStudioService.algorithmY.set([{ code: 'age', label: 'Age' }]); + experimentStudioService.algorithmX.set([{ code: 'mmse', label: 'MMSE' }]); + experimentStudioService.selectedFilters.set([{ code: 'sex', label: 'Sex' }]); + fixture.detectChanges(); + + expect(component.canRun()).toBe(false); + expect(component.runDisabledReason()).toBe('Remove variables not available in: Dataset B, Dataset C'); + expect(component.algorithmNeeds()).toContainEqual(expect.objectContaining({ + key: 'unavailable-variables', + met: false, + title: 'Remove variables not available in: Dataset B, Dataset C', + })); + expect((fixture.nativeElement as HTMLElement).textContent) + .toContain('Remove variables not available in: Dataset B, Dataset C'); + + component.onClickRunExp(); + expect(experimentStudioService.runSelectedAlgorithm).not.toHaveBeenCalled(); + expect(experimentStudioService.setRunning).not.toHaveBeenCalled(); + }); + + it('checks predictors on their own', () => { + experimentStudioService.unavailableVariables.set(unavailable); + experimentStudioService.algorithmX.set([{ code: 'age', label: 'Age' }]); + + expect(fixture.componentInstance.canRun()).toBe(false); + expect(fixture.componentInstance.runDisabledReason()).toBe('Remove variables not available in: Dataset B'); + }); + + it('runs again once the unavailable variables are removed', () => { + const component = fixture.componentInstance; + experimentStudioService.unavailableVariables.set(unavailable); + experimentStudioService.algorithmY.set([{ code: 'age', label: 'Age' }]); + expect(component.canRun()).toBe(false); + + experimentStudioService.algorithmY.set([{ code: 'mmse', label: 'MMSE' }]); + expect(component.canRun()).toBe(true); + expect(component.runDisabledReason()).toBe('Execute experiment'); + expect(component.algorithmNeeds().some((need) => need.key === 'unavailable-variables')).toBe(false); + }); + + it('ignores unavailable variables the run does not use', () => { + experimentStudioService.unavailableVariables.set(unavailable); + experimentStudioService.selectedVariables.set([{ code: 'age', label: 'Age' }]); + + expect(fixture.componentInstance.canRun()).toBe(true); + }); + }); + + it('keeps required parameters in the primary list and knows the friendly needs copy', async () => { + const xDetail = { + role: 'x' as const, + label: 'Predictor', + selectedCount: 2, + minCount: 1, + maxCount: null, + required: false, + types: ['real'], + stattypes: [], + messages: [], + satisfied: true, + }; + const kmeansLike: AlgorithmConfig = { + ...algorithm, + name: 'kmeans_like', + label: 'K-means', + inputdata: { x: { types: ['real'], required: true, min_count: 1 } } as any, + availability: { available: true, summary: '', details: [xDetail] }, + configSchema: [ + { key: 'k', label: 'Number of clusters (k)', type: 'number', default: 4, required: true }, + { key: 'max_iter', label: 'Max iterations', type: 'number', default: 100 }, + ], + }; + + experimentStudioService.availableGroupedAlgorithms.set({ Clustering: [kmeansLike] }); + experimentStudioService.backendAlgorithms.set({ kmeans_like: kmeansLike }); + experimentStudioService.selectedAlgorithm.set(kmeansLike); + experimentStudioService.algorithmY.set([{ code: 'mmse', label: 'MMSE', type: 'discrete' }]); + experimentStudioService.algorithmX.set([{ code: 'age', label: 'Age', type: 'continuous' }, { code: 'bmi', label: 'BMI', type: 'continuous' }]); + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + + const root = fixture.nativeElement as HTMLElement; + expect(root.querySelector('.documentation-panel')?.textContent).toContain('How this method works'); + const needsText = root.querySelector('.algo-needs')?.textContent ?? ''; + expect(needsText).toContain('No outcome needed'); + expect(needsText).toContain('Your outcome is ignored'); + expect(needsText).toContain('At least 1 numerical predictor'); + + const primaryLabels = Array.from(root.querySelectorAll('.config-form > .config-field > label')) + .map((label) => label.textContent ?? '').join(' '); + const advancedText = root.querySelector('.config-advanced')?.textContent ?? ''; + expect(primaryLabels).toContain('Number of clusters'); + expect(advancedText).toContain('Max iterations'); + expect((root.querySelector('.config-advanced') as HTMLDetailsElement)?.open).toBe(false); + }); + + it('renders selected algorithm documentation separately from the short description', async () => { + fixture.componentInstance.setStudioSubstep('parameters'); + await fixture.whenStable(); + fixture.detectChanges(); + + const nativeElement = fixture.nativeElement as HTMLElement; + const details = nativeElement.querySelector('.documentation-panel') as HTMLDetailsElement; + + expect(nativeElement.querySelector('.config-description')?.textContent).toContain('Algorithm with more than three configuration fields.'); + expect(details?.textContent).toContain('How this method works'); + expect(details?.textContent).toContain('Line one.'); + expect(details?.textContent).toContain('Line two.'); + expect(details?.open).toBe(false); + expect(details?.querySelector('.documentation-content')?.textContent).toContain('Line one.'); + }); + + it('shows disabled algorithm availability reasons on the card and in the tooltip', async () => { + const disabledAlgorithm: AlgorithmConfig = { + ...algorithm, + name: 'needs_two_variables', + label: 'Needs Two Variables', + isDisabled: true, + availability: { + available: false, + summary: 'Outcome needs at least 2, selected 1.', + details: [ + { + role: 'y', + label: 'Outcome', + selectedCount: 1, + minCount: 2, + maxCount: 3, + required: true, + types: ['real'], + stattypes: ['nominal'], + messages: [ + 'Outcome needs at least 2, selected 1.', + 'Outcome type must be one of real.', + ], + satisfied: false, + }, + ], + }, + }; + experimentStudioService.availableGroupedAlgorithms.set({ Test: [disabledAlgorithm] }); + fixture.componentInstance.showOnlyActive.set(false); + fixture.detectChanges(); + await fixture.whenStable(); + + // Groups without runnable methods start collapsed: reveal the Test group. + ((fixture.nativeElement as HTMLElement).querySelector('.algorithm-match-cat') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const component = fixture.componentInstance; + component.tooltipVisible.set(true); + component.tooltipData.set(disabledAlgorithm); + fixture.detectChanges(); + + const nativeElement = fixture.nativeElement as HTMLElement; + const algorithmItem = nativeElement.querySelector('.algo-tile') as HTMLElement; + expect(algorithmItem.textContent).toContain('Needs Two Variables'); + expect(algorithmItem.querySelector('.algo-why')?.textContent).toContain('Outcome needs at least 2, selected 1.'); + expect(nativeElement.querySelector('.tooltip')?.textContent).toContain('Outcome needs at least 2, selected 1.'); + expect(nativeElement.textContent).toContain('Availability'); + expect(nativeElement.textContent).toContain('Outcome: 2-3, selected 1'); + expect(nativeElement.textContent).toContain('type: real'); + expect(nativeElement.querySelector('.tooltip')?.textContent).toContain('Outcome type must be one of real.'); + expect(nativeElement.textContent).not.toContain('type: nominal'); + expect(nativeElement.textContent).not.toContain('stattypes: nominal'); + }); + + it('displays text availability types as nominal in requirement tips', () => { + const text = fixture.componentInstance.availabilityRequirementText({ + role: 'x', + label: 'Predictor', + selectedCount: 1, + minCount: 1, + maxCount: null, + required: true, + types: ['real', 'int', 'text'], + stattypes: [], + messages: [], + satisfied: true, + }); + + expect(text).toContain('type: real, int, nominal'); + expect(text).not.toContain('text'); + }); + + it('shows type availability reasons on algorithm cards', async () => { + const disabledAlgorithm: AlgorithmConfig = { + ...algorithm, + name: 'type_only_algorithm', + label: 'Type Only Algorithm', + isDisabled: true, + availability: { + available: false, + summary: 'Outcome type must be one of real.', + details: [ + { + role: 'y', + label: 'Outcome', + selectedCount: 1, + minCount: 1, + maxCount: 1, + required: true, + types: ['real'], + stattypes: [], + messages: ['Outcome type must be one of real.'], + satisfied: false, + }, + ], + }, + }; + experimentStudioService.availableGroupedAlgorithms.set({ Test: [disabledAlgorithm] }); + fixture.componentInstance.showOnlyActive.set(false); + fixture.detectChanges(); + await fixture.whenStable(); + + // Groups without runnable methods start collapsed: reveal the Test group. + ((fixture.nativeElement as HTMLElement).querySelector('.algorithm-match-cat') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const algorithmItem = fixture.nativeElement.querySelector('.algo-tile') as HTMLElement; + expect(algorithmItem.textContent).toContain('Type Only Algorithm'); + expect(algorithmItem.querySelector('.algo-why')?.textContent).toContain('Outcome type must be one of real.'); + }); + + it('selects unavailable algorithms as a read-only preview with expandable documentation', async () => { + const unavailableAlgorithm: AlgorithmConfig = { + ...algorithm, + name: 'unavailable_algorithm', + label: 'Unavailable Algorithm', + documentation: 'Unavailable docs.\nSecond line.', + isDisabled: true, + configSchema: [ + { key: 'alpha', label: 'Alpha', type: 'number', default: 0.05 }, + ], + availability: { + available: false, + summary: 'Outcome type must be one of text.', + details: [], + }, + }; + experimentStudioService.availableGroupedAlgorithms.set({ Test: [unavailableAlgorithm] }); + experimentStudioService.backendAlgorithms.set({ unavailable_algorithm: unavailableAlgorithm }); + experimentStudioService.isAlgorithmAvailable.mockImplementation((name: string) => name !== 'unavailable_algorithm'); + experimentStudioService.getAlgorithmAvailability.mockImplementation((name: string) => name === 'unavailable_algorithm' + ? unavailableAlgorithm.availability + : { available: true, summary: '', details: [] }); + + fixture.componentInstance.showOnlyActive.set(false); + fixture.detectChanges(); + await fixture.whenStable(); + + // Groups without runnable methods start collapsed: reveal the Test group. + ((fixture.nativeElement as HTMLElement).querySelector('.algorithm-match-cat') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const unavailableListItem = (fixture.nativeElement as HTMLElement).querySelector('.algo-tile.disabled-algo') as HTMLElement; + expect(getComputedStyle(unavailableListItem).cursor).toBe('pointer'); + + fixture.componentInstance.onAlgorithmClick(unavailableAlgorithm); + fixture.detectChanges(); + await fixture.whenStable(); + + const nativeElement = fixture.nativeElement as HTMLElement; + const details = nativeElement.querySelector('.documentation-panel') as HTMLDetailsElement; + + expect(experimentStudioService.selectedAlgorithm()?.name).toBe('unavailable_algorithm'); + expect(details?.textContent).toContain('Unavailable docs.'); + expect(details?.open).toBe(false); + details?.querySelector('summary')?.dispatchEvent(new MouseEvent('click', { bubbles: true })); + + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + + const runRoot = fixture.nativeElement as HTMLElement; + const fieldset = runRoot.querySelector('.algorithm-readonly-fieldset') as HTMLFieldSetElement; + const requirementsPanel = runRoot.querySelector('.algorithm-requirements-panel') as HTMLElement; + expect(requirementsPanel?.textContent).toContain('Complete before running'); + expect(requirementsPanel?.textContent).toContain('Outcome type must be one of nominal.'); + expect(requirementsPanel?.textContent).not.toContain('text'); + expect(fieldset).toBeTruthy(); + expect(fieldset.disabled).toBe(true); + expect(getComputedStyle(fieldset).pointerEvents).toBe('none'); + expect(fixture.componentInstance.canRun()).toBe(false); + + fixture.componentInstance.onClickRunExp(); + expect(experimentStudioService.runSelectedAlgorithm).not.toHaveBeenCalled(); + }); + + it('keeps fields after the old advanced cutoff in the persisted form config', async () => { + await fixture.whenStable(); + + const component = fixture.componentInstance; + component.configForm().get('fourth')?.setValue('42'); + + expect(experimentStudioService.algorithmConfigurations()['flat_config_algorithm']['fourth']).toBe(42); + }); + + it('shows event_var description and covariate options for Cox regression', async () => { + const coxAlgorithm: AlgorithmConfig = { + name: 'cox_regression_classical', + label: 'Cox Regression Classical', + description: '', + category: 'Regression', + requiredVariable: ['real'], + covariate: ['real', 'text'], + configSchema: [ + { + key: 'event_var', + label: 'Event indicator variable', + desc: 'Variable from x to use as the binary event indicator.', + type: 'select', + enumType: 'input_var_names', + enumSource: ['x'], + required: true, + options: [], + }, + ], + isDisabled: false, + }; + + experimentStudioService.selectedAlgorithm.set(coxAlgorithm); + experimentStudioService.backendAlgorithms.set({ cox_regression_classical: coxAlgorithm }); + experimentStudioService.availableGroupedAlgorithms.set({ Regression: [coxAlgorithm] }); + experimentStudioService.algorithmX.set([ + { code: 'event_status', label: 'Event status', type: 'nominal' }, + { code: 'age', label: 'Age', type: 'continuous' }, + ]); + + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + + const root = fixture.nativeElement as HTMLElement; + expect(root.textContent).toContain('Variable from x to use as the binary event indicator.'); + const select = root.querySelector('select.config-select') as HTMLSelectElement; + expect(select).toBeTruthy(); + expect(select.textContent).toContain('Event status'); + expect(select.textContent).toContain('Age'); + }); + + it('lists positive_class options from the selected event indicator variable', async () => { + const coxAlgorithm: AlgorithmConfig = { + name: 'cox_regression_classical', + label: 'Cox Regression Classical', + description: '', + category: 'Regression', + requiredVariable: ['real'], + covariate: ['text'], + configSchema: [ + { + key: 'event_var', + label: 'Event indicator variable', + desc: 'Variable from x to use as the binary event indicator.', + type: 'select', + enumType: 'input_var_names', + enumSource: ['x'], + required: true, + options: [], + }, + { + key: 'positive_class', + label: 'Positive event class', + desc: 'Event level mapped to 1; other observed levels are mapped to 0.', + type: 'text', + required: true, + }, + ], + isDisabled: false, + }; + + experimentStudioService.selectedAlgorithm.set(coxAlgorithm); + experimentStudioService.algorithmX.set([ + { + code: 'procedure', + label: 'Procedure', + type: 'nominal', + enumerations: [ + { code: 'balloon', label: 'Balloon angioplasty' }, + { code: 'stent', label: 'Extracranial stent' }, + ], + }, + ]); + experimentStudioService.algorithmConfigurations.set({ + cox_regression_classical: { event_var: 'procedure' }, + }); + + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + + const root = fixture.nativeElement as HTMLElement; + const selects = root.querySelectorAll('select.config-select'); + expect(selects.length).toBe(2); + const positiveSelect = selects[1] as HTMLSelectElement; + expect(positiveSelect.textContent).toContain('Balloon angioplasty'); + expect(positiveSelect.textContent).toContain('Extracranial stent'); + expect(positiveSelect.textContent).not.toContain('None (optional)'); + expect(positiveSelect.textContent).toContain('-- Select --'); + expect(root.textContent).toContain('Select the event level mapped to 1'); + + const component = fixture.componentInstance; + expect(component.visibleConfigSchema().find((field) => field.key === 'positive_class')?.required).toBe(true); + const values = (component as any).normalizeFormValues( + { event_var: 'procedure', positive_class: 'yes' }, + component.visibleConfigSchema() + ); + expect(values.positive_class).toBe('yes'); + }); + + it('blocks Cox runs when positive_class is unset', async () => { + const coxAlgorithm: AlgorithmConfig = { + name: 'cox_regression_classical', + label: 'Cox Regression Classical', + description: '', + category: 'Regression', + requiredVariable: ['real'], + covariate: ['text'], + configSchema: [ + { + key: 'event_var', + label: 'Event indicator variable', + type: 'select', + enumType: 'input_var_names', + enumSource: ['x'], + required: true, + options: [], + }, + { + key: 'positive_class', + label: 'Positive event class', + type: 'text', + required: false, + }, + ], + isDisabled: false, + }; + + experimentStudioService.selectedAlgorithm.set(coxAlgorithm); + experimentStudioService.algorithmX.set([ + { + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [ + { code: 'male', label: 'male' }, + { code: 'female', label: 'female' }, + ], + }, + ]); + experimentStudioService.algorithmConfigurations.set({ + cox_regression_classical: { event_var: 'sex' }, + }); + + fixture.detectChanges(); + await fixture.whenStable(); + + const component = fixture.componentInstance; + component.onClickRunExp(); + + expect(experimentStudioService.runSelectedAlgorithm).not.toHaveBeenCalled(); + expect(component.configForm().get('positive_class')?.invalid).toBe(true); + }); + + it('shows covariate hint for empty input_var_names selects', async () => { + const coxAlgorithm: AlgorithmConfig = { + name: 'cox_regression_classical', + label: 'Cox Regression Classical', + description: '', + category: 'Regression', + requiredVariable: ['real'], + covariate: ['real'], + configSchema: [ + { + key: 'event_var', + label: 'Event indicator variable', + desc: 'Variable from x to use as the binary event indicator.', + type: 'select', + enumType: 'input_var_names', + enumSource: ['x'], + required: true, + options: [], + }, + ], + isDisabled: false, + }; + + experimentStudioService.selectedAlgorithm.set(coxAlgorithm); + experimentStudioService.algorithmX.set([]); + + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + + const root = fixture.nativeElement as HTMLElement; + const select = root.querySelector('select.config-select') as HTMLSelectElement; + expect(root.textContent).toContain('Select covariates first'); + expect(select?.options.length).toBe(1); + }); + + it('serializes outlier_report rules into dictionary parameters', async () => { + const outlierAlgorithm: AlgorithmConfig = { + name: 'outlier_report', + label: 'Outlier Report', + description: '', + category: 'Descriptive Statistics', + requiredVariable: ['real'], + covariate: ['real'], + configSchema: [ + { key: 'strategies', label: 'Strategies', type: 'dict' }, + { key: 'tails', label: 'Tails', type: 'dict' }, + { key: 'folds', label: 'Folds', type: 'dict' }, + ], + isDisabled: false, + }; + experimentStudioService.selectedAlgorithm.set(outlierAlgorithm); + experimentStudioService.backendAlgorithms.set({ outlier_report: outlierAlgorithm }); + experimentStudioService.availableGroupedAlgorithms.set({ 'Descriptive Statistics': [outlierAlgorithm] }); + experimentStudioService.selectedVariables.set([ + { code: 'age', label: 'Age', type: 'continuous' }, + { code: 'sex', label: 'Sex', type: 'nominal' }, + ]); + experimentStudioService.algorithmX.set([{ code: 'bmi', label: 'BMI', type: 'continuous' }]); + experimentStudioService.algorithmAssignableVariables.set([ + { code: 'age', label: 'Age', type: 'continuous' }, + { code: 'sex', label: 'Sex', type: 'nominal' }, + { code: 'bmi', label: 'BMI', type: 'continuous' }, + ]); + experimentStudioService.hasAppliedDescriptivePreprocessing.mockReturnValue(false); + experimentStudioService.runId.set('stale-run'); + experimentStudioService.runSelectedAlgorithm.mockReturnValue(of({ + uuid: 'kept-run', + status: 'success', + result: { featurewise: [] }, + })); + + fixture.detectChanges(); + await fixture.whenStable(); + const component = fixture.componentInstance; + component.onOutlierReportEnabledChange({ code: 'bmi', label: 'BMI', type: 'continuous' }, false); + component.onOutlierReportStrategyChange({ code: 'age', label: 'Age', type: 'continuous' }, 'quantile'); + component.onOutlierReportFoldChange({ code: 'age', label: 'Age', type: 'continuous' }, 0.05); + component.onClickRunExp(); + + expect(experimentStudioService.algorithmConfigurations()['outlier_report']).toEqual({ + strategies: { age: 'quantile' }, + tails: { age: 'both' }, + folds: { age: 0.05 }, + }); + expect(experimentStudioService.captureRunSetup).toHaveBeenCalledWith('outlier_report'); + expect(experimentStudioService.runSelectedAlgorithm).toHaveBeenCalledWith('outlier_report', 'outlier_report'); + // The kept run is what Save persists, so it must be the run behind the shown result. + expect(experimentStudioService.runId()).toBe('kept-run'); + expect((fixture.nativeElement as HTMLElement).textContent).toContain('Age'); + expect((fixture.nativeElement as HTMLElement).textContent).toContain('BMI'); + }); + + it('shows the run error and stops running when the run request fails', async () => { + experimentStudioService.selectedAlgorithm.set(algorithm); + experimentStudioService.runSelectedAlgorithm.mockReturnValue(throwError(() => new Error('500'))); + fixture.detectChanges(); + await fixture.whenStable(); + + fixture.componentInstance.onClickRunExp(); + + expect(experimentStudioService.runError()).toBe('Unable to run experiment. Please try again.'); + expect(experimentStudioService.runId()).toBeNull(); + expect(experimentStudioService.setRunning).toHaveBeenCalledWith(true); + expect(experimentStudioService.setRunning).toHaveBeenLastCalledWith(false); + }); + + it('blocks invalid outlier_report folds', async () => { + const outlierAlgorithm: AlgorithmConfig = { + name: 'outlier_report', + label: 'Outlier Report', + description: '', + category: 'Descriptive Statistics', + requiredVariable: ['real'], + covariate: ['real'], + configSchema: [ + { key: 'strategies', label: 'Strategies', type: 'dict' }, + { key: 'tails', label: 'Tails', type: 'dict' }, + { key: 'folds', label: 'Folds', type: 'dict' }, + ], + isDisabled: false, + }; + experimentStudioService.selectedAlgorithm.set(outlierAlgorithm); + experimentStudioService.selectedVariables.set([{ code: 'age', label: 'Age', type: 'continuous' }]); + experimentStudioService.algorithmAssignableVariables.set([{ code: 'age', label: 'Age', type: 'continuous' }]); + experimentStudioService.hasAppliedDescriptivePreprocessing.mockReturnValue(false); + fixture.detectChanges(); + await fixture.whenStable(); + + const component = fixture.componentInstance; + component.onOutlierReportStrategyChange({ code: 'age', label: 'Age', type: 'continuous' }, 'quantile'); + component.onOutlierReportFoldChange({ code: 'age', label: 'Age', type: 'continuous' }, 0.5); + experimentStudioService.runSelectedAlgorithm.mockClear(); + + component.onClickRunExp(); + + expect(experimentStudioService.runSelectedAlgorithm).not.toHaveBeenCalled(); + expect(component.errorMsg()).toBe('Fix the outlier report configuration before running.'); + }); + + it('lists availability and preprocessing requirements together', async () => { + const unavailableAlgorithm: AlgorithmConfig = { + ...algorithm, + name: 'unavailable_algorithm', + availability: { + available: false, + summary: 'Outcome needs at least 1, selected 0.', + details: [{ + role: 'y', + label: 'Outcome', + selectedCount: 0, + minCount: 1, + maxCount: 1, + required: true, + types: [], + stattypes: [], + messages: ['Outcome needs at least 1, selected 0.'], + satisfied: false, + }], + }, + }; + + experimentStudioService.availableGroupedAlgorithms.set({ Test: [unavailableAlgorithm] }); + experimentStudioService.backendAlgorithms.set({ unavailable_algorithm: unavailableAlgorithm }); + experimentStudioService.isAlgorithmAvailable.mockImplementation((name: string) => name !== 'unavailable_algorithm'); + experimentStudioService.getAlgorithmAvailability.mockImplementation((name: string) => ( + name === 'unavailable_algorithm' + ? unavailableAlgorithm.availability! + : { available: true, summary: '', details: [] } + )); + experimentStudioService.selectedAlgorithm.set(unavailableAlgorithm); + experimentStudioService.hasAppliedDescriptivePreprocessing.mockReturnValue(false); + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + + const items = Array.from( + (fixture.nativeElement as HTMLElement).querySelectorAll('.algorithm-requirement-item'), + ) as HTMLElement[]; + expect(items.length).toBe(3); + const needsText = items.map((item) => item.textContent ?? '').join(' '); + expect(needsText).toContain('Outcome needs at least 1, selected 0.'); + expect(needsText).toContain('Assign outcome'); + expect(needsText).toContain('Apply missing value preprocessing'); + expect(needsText).toContain('Go to preprocessing'); + }); + + it('keeps the availability run-requirement action on the algorithm panel for role assignment', async () => { + const unavailableAlgorithm: AlgorithmConfig = { + ...algorithm, + name: 'unavailable_algorithm', + availability: { + available: false, + summary: 'Outcome needs at least 1, selected 0.', + details: [{ + role: 'y', + label: 'Outcome', + selectedCount: 0, + minCount: 1, + maxCount: 1, + required: true, + types: [], + stattypes: [], + messages: ['Outcome needs at least 1, selected 0.'], + satisfied: false, + }], + }, + }; + + experimentStudioService.availableGroupedAlgorithms.set({ Test: [unavailableAlgorithm] }); + experimentStudioService.backendAlgorithms.set({ unavailable_algorithm: unavailableAlgorithm }); + experimentStudioService.isAlgorithmAvailable.mockImplementation((name: string) => name !== 'unavailable_algorithm'); + experimentStudioService.getAlgorithmAvailability.mockImplementation((name: string) => ( + name === 'unavailable_algorithm' + ? unavailableAlgorithm.availability! + : { available: true, summary: '', details: [] } + )); + experimentStudioService.selectedAlgorithm.set(unavailableAlgorithm); + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + + const action = (fixture.nativeElement as HTMLElement).querySelector( + '.algorithm-requirement-item .algorithm-requirement-action', + ) as HTMLButtonElement; + + expect(action?.textContent).toContain('Assign outcome'); + action.click(); + // Role assignment lives on this panel; the action does not navigate back to variables. + expect(studioNavigation.navigateToSection).not.toHaveBeenCalled(); + }); + + it('navigates to preprocessing from the requirements list', async () => { + experimentStudioService.hasAppliedDescriptivePreprocessing.mockReturnValue(false); + experimentStudioService.selectedAlgorithm.set({ ...algorithm }); + fixture.componentInstance.setStudioSubstep('parameters'); + fixture.detectChanges(); + await fixture.whenStable(); + + const action = (fixture.nativeElement as HTMLElement).querySelector( + '.algorithm-requirement-item .algorithm-requirement-action', + ) as HTMLButtonElement; + + expect(action?.textContent).toContain('Go to preprocessing'); + action.click(); + expect(studioNavigation.goToPreprocessing).toHaveBeenCalled(); + }); + + it('lists unavailable methods with their why badge in All mode', () => { + const disabledAlgorithm = { + ...algorithm, + isDisabled: true, + availability: { + available: false, + summary: 'Needs an outcome', + details: [], + }, + }; + + experimentStudioService.availableGroupedAlgorithms.set({ Test: [disabledAlgorithm] }); + experimentStudioService.getAlgorithmAvailability.mockReturnValue({ + available: false, + summary: 'Needs an outcome', + details: [], + }); + fixture.componentInstance.showOnlyActive.set(false); + fixture.detectChanges(); + expect(fixture.componentInstance.showOnlyActive()).toBe(false); + + // The group holds the selected method, so it is open even with nothing runnable. + const tile = (fixture.nativeElement as HTMLElement).querySelector('.algo-tile'); + expect(tile?.textContent).toContain('Flat Config Algorithm'); + expect(tile?.querySelector('.algo-why')?.textContent).toContain('Needs an outcome'); + }); + + it('nests the matching catalog in the outcomes board', () => { + fixture.detectChanges(); + const root = fixture.nativeElement as HTMLElement; + const board = root.querySelector('.algorithm-roles-board'); + expect(board?.querySelector('[data-guide="algorithm-selection"]')).toBeTruthy(); + expect(board?.querySelector('.algo-tile')?.textContent).toContain('Flat Config Algorithm'); + }); + + it('shows the pointing chevron only on the selected runnable tile', () => { + fixture.detectChanges(); + const tiles = Array.from((fixture.nativeElement as HTMLElement).querySelectorAll<HTMLElement>('.algo-tile')); + expect(tiles.length).toBeGreaterThan(0); + + tiles.forEach((tile) => { + const cue = tile.querySelector<HTMLElement>('.algo-tile-cue'); + const pointsAtConfig = tile.classList.contains('selected') && !tile.classList.contains('disabled-algo'); + expect(getComputedStyle(cue!).display).toBe(pointsAtConfig ? 'block' : 'none'); + }); + const pointing = tiles.filter( + (tile) => getComputedStyle(tile.querySelector<HTMLElement>('.algo-tile-cue')!).display === 'block', + ); + expect(pointing.length).toBe(1); + }); + + it('places the parameter pool above the catalog-and-configuration split', () => { + fixture.detectChanges(); + const root = fixture.nativeElement as HTMLElement; + const pool = root.querySelector('.algorithm-pool'); + const workspace = root.querySelector('.algorithm-workspace'); + // The pool is the full-width top band, a direct child under the card header. + expect(pool?.querySelector('app-algorithm-role-assignment')).toBeTruthy(); + expect(pool?.parentElement?.classList.contains('algorithm-roles-board')).toBe(true); + expect( + !!pool && !!workspace && + !!(pool.compareDocumentPosition(workspace) & Node.DOCUMENT_POSITION_FOLLOWING) + ).toBe(true); + // The bottom row is the master–detail split: catalog left, configuration right. + expect(workspace?.querySelector('app-algorithm-role-assignment')).toBeNull(); + expect(workspace?.querySelector('[data-guide="algorithm-selection"]')).toBeTruthy(); + expect( + workspace?.querySelector('.algorithm-config-column [data-guide="algorithm-settings"]') + ).toBeTruthy(); + expect(root.querySelector('.algo-tile')).toBeTruthy(); + expect(root.querySelector('.algorithm-roles-footer')).toBeNull(); + }); + + it('opens the configure view when a matching method is clicked', async () => { + experimentStudioService.selectedAlgorithm.set(null); + fixture.detectChanges(); + await fixture.whenStable(); + + const root = fixture.nativeElement as HTMLElement; + expect(root.querySelector('.algo-tile')).toBeTruthy(); + expect(root.querySelector('.documentation-panel')).toBeNull(); + + fixture.componentInstance.onAlgorithmClick(algorithm); + fixture.detectChanges(); + await fixture.whenStable(); + + expect(fixture.componentInstance.studioSubstep()).toBe('parameters'); + const details = root.querySelector('.documentation-panel') as HTMLDetailsElement | null; + expect(details?.open).toBe(false); + details?.querySelector('summary')?.dispatchEvent(new MouseEvent('click', { bubbles: true })); + expect(details?.open).toBe(true); + expect(details?.textContent).toContain('Line one.'); + expect(root.querySelector('.config-form')).toBeTruthy(); + // Setup stays on screen: the method grid is still rendered above the board. + expect(root.querySelector('.algo-tile')).toBeTruthy(); + }); + + it('renders setup and configuration in one unified card', () => { + fixture.componentInstance.onAlgorithmClick(algorithm); + fixture.detectChanges(); + + const root = fixture.nativeElement as HTMLElement; + const boards = root.querySelectorAll('.algorithm-roles-board'); + // One frame: roles, catalog, parameters, and details share a single studio card. + expect(boards.length).toBe(1); + expect(boards[0]?.querySelector('.algo-tile')).toBeTruthy(); + const paramsPane = root.querySelector('[data-guide="algorithm-settings"]'); + expect(boards[0]?.contains(paramsPane)).toBe(true); + + // The parameters pane must not paint its own card frame on top of the unified + // card, otherwise the workspace reads as separate cards again. + const styles = getComputedStyle(paramsPane as HTMLElement); + expect(styles.borderTopWidth).toBe('0px'); + expect(styles.backgroundColor).toMatch(/rgba\(0, 0, 0, 0\)|transparent/); + expect(styles.boxShadow).toBe('none'); + }); + + it('renders details and parameters in the configuration column beside the catalog', async () => { + experimentStudioService.selectedAlgorithm.set(null); + fixture.detectChanges(); + await fixture.whenStable(); + + const root = fixture.nativeElement as HTMLElement; + const column = root.querySelector('.algorithm-workspace .algorithm-config-column'); + const catalog = root.querySelector('[data-guide="algorithm-selection"]'); + const params = root.querySelector('[data-guide="algorithm-settings"]'); + // Nothing selected: the column still offers guidance instead of a parameter form. + expect(params?.querySelector('.algorithm-params__empty')).toBeTruthy(); + expect(root.querySelector('.algorithm-details')).toBeNull(); + expect(column?.contains(params ?? null)).toBe(true); + + fixture.componentInstance.onAlgorithmClick(algorithm); + fixture.detectChanges(); + await fixture.whenStable(); + + expect(params!.querySelector('.algorithm-params__empty')).toBeNull(); + expect(params!.querySelector('.config-form')).toBeTruthy(); + // Details head the column and the parameters follow them, both beside the catalog. + const details = root.querySelector('.algorithm-details'); + expect(details).toBeTruthy(); + expect(column?.contains(details)).toBe(true); + expect(catalog?.contains(details ?? null)).toBe(false); + expect( + !!(details!.compareDocumentPosition(params!) & Node.DOCUMENT_POSITION_FOLLOWING) + ).toBe(true); + }); + + it('keeps matching methods visible on setup until a method is chosen', () => { + experimentStudioService.selectedAlgorithm.set(null); + fixture.detectChanges(); + expect((fixture.nativeElement as HTMLElement).querySelector('.documentation-panel')).toBeNull(); + expect((fixture.nativeElement as HTMLElement).querySelector('.algo-tile')).toBeTruthy(); + }); + + it('defaults to runnable only and reveals unavailable methods on demand', () => { + // The catalog ships in Runnable mode: the chip is pressed from the first render. + const root = fixture.nativeElement as HTMLElement; + const modeTabs = () => Array.from(root.querySelectorAll('.catalog-mode-tabs button')) as HTMLButtonElement[]; + expect(modeTabs()[0]?.getAttribute('aria-pressed')).toBe('true'); + + const disabledAlgorithm = { ...algorithm, isDisabled: true, label: 'Locked Algorithm' }; + experimentStudioService.availableGroupedAlgorithms.set({ Test: [disabledAlgorithm] }); + fixture.componentInstance.showOnlyActive.set(false); + fixture.detectChanges(); + fixture.detectChanges(); + + // All mode lists unavailable methods with their reason; the group stays open + // because it holds the selected method. + expect(root.querySelector('.algo-tile')?.textContent).toContain('Locked Algorithm'); + + const allTab = modeTabs()[1]; + expect(allTab?.textContent).toContain('All'); + expect(allTab?.textContent).toContain('1'); + expect(allTab?.getAttribute('aria-pressed')).toBe('true'); + + modeTabs()[0].click(); + fixture.detectChanges(); + expect(fixture.componentInstance.showOnlyActive()).toBe(true); + expect(modeTabs()[0].getAttribute('aria-pressed')).toBe('true'); + expect(modeTabs()[0].textContent).toContain('Runnable'); + expect(root.querySelector('.algo-tile')).toBeNull(); + expect(root.textContent).toContain('No runnable methods for this assignment.'); + }); + + it('filters the catalog instantly by search text', () => { + const kruskal: AlgorithmConfig = { ...algorithm, name: 'kruskal', label: 'Kruskal-Wallis' }; + experimentStudioService.availableGroupedAlgorithms.set({ Test: [algorithm, kruskal] }); + fixture.detectChanges(); + + const root = fixture.nativeElement as HTMLElement; + expect(root.querySelectorAll('.algo-tile').length).toBe(2); + + fixture.componentInstance.algoSearchQuery.set('kruskal'); + fixture.detectChanges(); + const tiles = root.querySelectorAll('.algo-tile'); + expect(tiles.length).toBe(1); + expect(tiles[0].textContent).toContain('Kruskal-Wallis'); + + fixture.componentInstance.algoSearchQuery.set('no-such-method'); + fixture.detectChanges(); + expect(root.querySelector('.algo-tile')).toBeNull(); + expect(root.textContent).toContain('No methods match'); + }); + + /** + * Two Matching-method groups with no selection: the first holds runnable methods, + * the second is unavailable, for the default-expansion specs. + */ + function loadMethodGroups(): { groupComparison: AlgorithmConfig[]; association: AlgorithmConfig } { + const anova: AlgorithmConfig = { ...algorithm, name: 'anova_oneway', label: 'One-way ANOVA' }; + const kruskal: AlgorithmConfig = { ...algorithm, name: 'kruskal', label: 'Kruskal-Wallis' }; + const association: AlgorithmConfig = { + ...algorithm, + name: 'correlation', + label: 'Correlation', + isDisabled: true, + }; + + experimentStudioService.selectedAlgorithm.set(null); + experimentStudioService.availableGroupedAlgorithms.set({ + 'Group comparison': [anova, kruskal], + Association: [association], + }); + // These specs study category expansion over the full catalog, so opt into All mode. + fixture.componentInstance.showOnlyActive.set(false); + fixture.detectChanges(); + + return { groupComparison: [anova, kruskal], association }; + } + + function categoryHeader(row: Element): HTMLButtonElement { + return row.querySelector('.algorithm-match-cat') as HTMLButtonElement; + } + + it('opens only groups with runnable methods and keeps the rest collapsed', async () => { + loadMethodGroups(); + await fixture.whenStable(); + + const rows = (fixture.nativeElement as HTMLElement).querySelectorAll('.algorithm-match-row'); + expect(rows.length).toBe(2); + expect(categoryHeader(rows[0]).textContent).toContain('Group comparison'); + // Group comparison holds runnable methods: open by default. + expect(categoryHeader(rows[0]).getAttribute('aria-expanded')).toBe('true'); + expect(rows[0].querySelector('.algo-tile')?.textContent).toContain('One-way ANOVA'); + // Association has nothing runnable: collapsed until clicked. + expect(categoryHeader(rows[1]).getAttribute('aria-expanded')).toBe('false'); + expect(rows[1].querySelector('.algo-tile')).toBeNull(); + }); + + it('expands and collapses matching method groups independently', async () => { + loadMethodGroups(); + await fixture.whenStable(); + + const rows = (fixture.nativeElement as HTMLElement).querySelectorAll('.algorithm-match-row'); + + categoryHeader(rows[1]).click(); + fixture.detectChanges(); + expect(rows[1].querySelector('.algo-tile')?.textContent).toContain('Correlation'); + expect(rows[0].querySelector('.algo-tile')).toBeTruthy(); + + categoryHeader(rows[0]).click(); + fixture.detectChanges(); + expect(rows[0].querySelector('.algo-tile')).toBeNull(); + expect(rows[1].querySelector('.algo-tile')).toBeTruthy(); + }); + + it('opens the group of the selected method without closing the others', async () => { + const { groupComparison } = loadMethodGroups(); + await fixture.whenStable(); + + const rows = (fixture.nativeElement as HTMLElement).querySelectorAll('.algorithm-match-row'); + categoryHeader(rows[0]).click(); + fixture.detectChanges(); + expect(rows[0].querySelector('.algo-tile')).toBeNull(); + + fixture.componentInstance.onAlgorithmClick(groupComparison[1]); + fixture.detectChanges(); + await fixture.whenStable(); + + expect(categoryHeader(rows[0]).getAttribute('aria-expanded')).toBe('true'); + expect(rows[0].querySelector('.algo-tile')?.textContent).toContain('One-way ANOVA'); + expect(rows[1].querySelector('.algo-tile')).toBeNull(); + }); + + it('wires the role assignment component into the algorithm workspace', () => { + const root = fixture.nativeElement as HTMLElement; + expect(root.querySelector('app-algorithm-role-assignment')).toBeTruthy(); + }); + + it('exposes y/x role selections to the availability and config helpers', () => { + experimentStudioService.algorithmY.set([{ code: 'age', label: 'Age' }]); + experimentStudioService.algorithmX.set([{ code: 'sex', label: 'Sex' }]); + fixture.detectChanges(); + + const component = fixture.componentInstance; + expect(component.yVar()).toBe('age'); + expect(component.xVar()).toBe('sex'); + }); + + it('keeps parameters off Setup until a method is chosen', () => { + experimentStudioService.selectedAlgorithm.set(null); + fixture.detectChanges(); + + const root = fixture.nativeElement as HTMLElement; + expect(root.querySelector('.config-form')).toBeNull(); + expect(root.querySelector('.algorithm-readonly-fieldset')).toBeNull(); + expect(root.querySelector('.documentation-panel')).toBeNull(); + + fixture.componentInstance.onAlgorithmClick(algorithm); + fixture.detectChanges(); + + expect(root.querySelector('.documentation-panel')).toBeTruthy(); + }); + + it('keeps documentation in the details band and parameters under it', () => { + const component = fixture.componentInstance; + component.setStudioSubstep('parameters'); + fixture.detectChanges(); + const root = fixture.nativeElement as HTMLElement; + const params = root.querySelector('.algorithm-params'); + expect(params?.querySelector('.config-form')).toBeTruthy(); + expect(params?.querySelector('.documentation-panel')).toBeNull(); + const details = root.querySelector('.algorithm-details'); + expect(details?.querySelector('.documentation-panel')).toBeTruthy(); + expect(details?.querySelector('.config-form')).toBeNull(); + expect(root.querySelector('[data-guide="run-experiment"]')).toBeNull(); + expect(component.canRun()).toBe(true); + }); + + it('shows parameters on Parameter selection', () => { + const component = fixture.componentInstance; + component.setStudioSubstep('parameters'); + fixture.detectChanges(); + + const root = fixture.nativeElement as HTMLElement; + expect(root.querySelector('.config-form')).toBeTruthy(); + expect(root.querySelector('[data-guide="algorithm-settings"]')).toBeTruthy(); + const labels = Array.from(root.querySelectorAll('.config-field > label')).map( + (label) => label.textContent?.trim(), + ); + expect(labels).toEqual(expect.arrayContaining(['First optional', 'Second optional', 'Third optional', 'Fourth optional'])); + expect(component.configForm().get('first')?.value).toBe(1); + }); +}); diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts index 22ede16c9..aaf4f23a0 100644 --- a/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-panel.component.ts @@ -1,18 +1,58 @@ import { SessionStorageService } from './../../../services/session-storage.service'; -import { Component, inject, signal, computed, effect, Input, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject, signal, computed, effect, untracked, output } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ExperimentStudioService } from '../../../services/experiment-studio.service'; -import { FormBuilder, FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms'; +import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms'; import { buildFormControl } from '../../shared/utils/form-control.factory'; -import { AlgorithmResultComponent } from './algorithm-result/algorithm-result.component'; -import { getOutputSchema } from '../../../core/algorithm-mappers'; -import { EchartsxModule } from 'echarts-for-angular'; -import { AlgorithmConfig } from '../../../models/algorithm-definition.model'; -import { ResultsPdfExportService } from '../../../services/export-results-pdf.service'; +import { getOutputSchema, prettifyLabel as toHumanLabel } from '../../../core/algorithm-mappers'; +import { formatInputCountRange } from '../../../core/algorithm-input-counts'; +import { + omitEmptyOptionalParameters, + optionBindingValue, + serializeAlgorithmParameterValue, +} from '../../../core/algorithm-parameter.utils'; +import { AlgorithmAvailabilityDetail, AlgorithmConfig } from '../../../models/algorithm-definition.model'; +import { experimentTables, PdfExportService } from '../../../services/pdf-export.service'; +import { CsvExportService } from '../../../services/csv-export.service'; +import { formatFilterExpression } from '../../../core/filter-display.utils'; import { ErrorService } from '../../../services/error.service'; import { AuthService } from '../../../services/auth.service'; -import { SpinnerComponent } from '../../shared/spinner/spinner.component'; +import { AlgorithmNames, VariableTypes } from '../../../core/constants/algorithm.constants'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; +import { + createDefaultOutlierRule, + defaultFoldForStrategy, + hydrateOutlierRules, + isOutlierEligibleVariable, + OUTLIER_STRATEGIES, + OUTLIER_TAILS, + OutlierRule, + OutlierStrategy, + OutlierTail, + serializeOutlierRules, + validateOutlierRule, +} from '../../../core/outlier-rules'; +import { AlgorithmRoleAssignmentComponent } from './algorithm-role-assignment/algorithm-role-assignment.component'; +import { unavailableDatasetsFor, unavailableVariableTooltip } from '../../../core/variable-availability.utils'; + +type AlgorithmStudioSubstep = 'setup' | 'parameters'; + +type AlgorithmRunRequirementKind = 'availability' | 'preprocessing'; + +interface AlgorithmRunRequirement { + id: AlgorithmRunRequirementKind; + message: string; + actionLabel: string; +} +interface AlgorithmNeedItem { + key: string; + met: boolean; + title: string; + messages: string[]; + action?: AlgorithmRunRequirement; +} @Component({ selector: 'app-algorithm-panel', @@ -20,55 +60,205 @@ import { SpinnerComponent } from '../../shared/spinner/spinner.component'; CommonModule, FormsModule, ReactiveFormsModule, - AlgorithmResultComponent, - EchartsxModule, - SpinnerComponent + AlgorithmRoleAssignmentComponent, ], templateUrl: './algorithm-panel.component.html', - styleUrls: ['./algorithm-panel.component.css'] + styleUrl: './algorithm-panel.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class AlgorithmPanelComponent { - pdfExport = inject(ResultsPdfExportService); + pdfExport = inject(PdfExportService); + private readonly csvExport = inject(CsvExportService); private errorService = inject(ErrorService); private authService = inject(AuthService); - Object = Object; + private runtimeEnvService = inject(RuntimeEnvService); + private studioNavigation = inject(ExperimentStudioNavigationService); experimentStudioService = inject(ExperimentStudioService); sessionStorage = inject(SessionStorageService); - formBuilder = inject(FormBuilder); - result = signal<any | null>(null); lastUsedAlgorithm = ''; errorMsg = signal<string | null>(null); readonly isRunning = this.experimentStudioService.isRunning; - lastExperimentUUID = this.experimentStudioService.currentExperimentUUID; - saveAsMode = signal(false); - saveAsName = signal(''); - loadingText = signal('Processing experiment...'); - showSuccessNotification = signal(false); - mipVersion = (window as any).__env?.MIP_VERSION || '9.0.0'; + /** Studio: setup (roles + methods), then parameters (configure + run). Results live in the Execution step. */ + readonly studioSubstep = signal<AlgorithmStudioSubstep>('setup'); + readonly studioSubstepChange = output<AlgorithmStudioSubstep>(); + + setStudioSubstep(step: AlgorithmStudioSubstep): void { + this.studioSubstep.set(step); + this.studioSubstepChange.emit(step); + } + + readonly mipVersion = this.runtimeEnvService.mipVersion; readonly selectedAlgorithm = this.experimentStudioService.selectedAlgorithm; - readonly enumMaps = computed(() => this.experimentStudioService.getCategoricalEnumMaps()); - readonly yVar = computed(() => this.experimentStudioService.selectedVariables()[0]?.code ?? null); - readonly xVar = computed(() => this.experimentStudioService.selectedCovariates()[0]?.code ?? null); + readonly selectedAlgorithmDocumentation = computed(() => this.selectedAlgorithm()?.documentation?.trim() ?? ''); + readonly selectedAlgorithmUnavailable = computed(() => { + const algorithm = this.selectedAlgorithm(); + return !!algorithm && !this.experimentStudioService.isAlgorithmAvailable(algorithm.name); + }); + readonly selectedAvailabilityDetails = computed(() => this.availabilityDetails(this.selectedAlgorithm())); + + readonly algorithmNeeds = computed<AlgorithmNeedItem[]>(() => { + const algorithm = this.selectedAlgorithm(); + if (!algorithm) return []; + + const needs: AlgorithmNeedItem[] = []; + const details = this.selectedAvailabilityDetails(); + const detailByRole = new Map(details.map((detail) => [detail.role, detail])); + + for (const role of ['y', 'x'] as const) { + const detail = detailByRole.get(role); + if (detail) { + needs.push({ + key: `needs-${role}`, + met: detail.messages.length === 0, + title: this.needsRequirementText(detail), + messages: [...detail.messages], + }); + } else if (role === 'y' && !('y' in (algorithm.inputdata ?? {}))) { + needs.push({ + key: 'needs-y', + met: true, + title: 'No outcome needed', + messages: this.experimentStudioService.algorithmY().length ? ['Your outcome is ignored'] : [], + }); + } + } + + const unavailable = this.unavailableRunVariablesReason(); + if (unavailable) { + needs.push({ key: 'unavailable-variables', met: false, title: unavailable, messages: [] }); + } + + this.algorithmRunRequirements().forEach((requirement) => { + needs.push({ + key: `run-${requirement.id}`, + met: false, + title: requirement.message, + messages: [], + action: requirement, + }); + }); + + return needs; + }); + + needsRequirementText(detail: AlgorithmAvailabilityDetail): string { + const typeText = this.requirementTypeText(detail.types); + + if (detail.role === 'y') { + if (detail.minCount <= 0 && !detail.required) { + return 'No outcome needed'; + } + const article = typeText + ? `${/^[aeiou]/i.test(typeText) ? 'an' : 'a'} ${typeText} ` + : ''; + const noun = detail.minCount > 1 ? 'outcomes' : 'outcome'; + return detail.minCount > 1 + ? `Needs at least ${detail.minCount} ${noun}` + : `Needs ${article}${noun}`; + } + + const noun = detail.minCount > 1 ? 'predictors' : 'predictor'; + if (detail.minCount > 0) { + return `At least ${detail.minCount}${typeText ? ` ${typeText}` : ''} ${noun}`; + } + if (detail.maxCount !== null) { + return `Up to ${detail.maxCount} ${noun}`; + } + return 'Predictors optional'; + } + + private requirementTypeText(types: string[] | undefined | null): string { + if (!Array.isArray(types) || types.length === 0) return ''; + const labels = new Set<string>(); + types.forEach((type) => { + const normalized = String(type ?? '').trim().toLowerCase(); + if (!normalized) return; + if (['real', 'int', 'integer', 'numeric', 'numerical'].includes(normalized)) { + labels.add('numerical'); + } else if ( + ['text', 'nominal', 'categorical', 'polynominal', 'ordinal', 'string'].includes(normalized) + ) { + labels.add('categorical'); + } else { + labels.add(normalized); + } + }); + return Array.from(labels).join(' or '); + } + + readonly readinessText = computed(() => { + const algorithm = this.selectedAlgorithm(); + if (!algorithm) return 'Pick a method to continue'; + + if (!this.canRun()) { + const reasons = this.algorithmNeeds() + .filter((need) => !need.met) + .flatMap((need) => (need.action ? [need.title] : need.messages)) + .filter((message) => !!message); + if (!reasons.length) return 'Not ready to run yet'; + return `${reasons.length} ${reasons.length === 1 ? 'thing' : 'things'} left — ${reasons.slice(0, 2).join(' · ')}${reasons.length > 2 ? ` +${reasons.length - 2}` : ''}`; + } + + const y = this.experimentStudioService.algorithmY(); + const x = this.experimentStudioService.algorithmX(); + const outcome = y[0] ? ` · y ${y[0].label ?? y[0].name ?? y[0].code}` : ''; + const predictors = ` · ${x.length} ${x.length === 1 ? 'predictor' : 'predictors'}`; + return `Ready · ${algorithm.label}${outcome}${predictors}`; + }); + + categoryRunnableCount(category: { algorithms: AlgorithmConfig[] }): number { + return category.algorithms.filter((algorithm) => !algorithm.isDisabled).length; + } + + readonly selectedAlgorithmAvailabilitySummary = computed(() => { + const algorithm = this.selectedAlgorithm(); + if (!algorithm || !this.selectedAlgorithmUnavailable()) return ''; + const summary = this.experimentStudioService.getAlgorithmAvailability(algorithm.name).summary ?? algorithm.availability?.summary ?? ''; + return this.formatAvailabilityMessage(summary); + }); + readonly algorithmRunRequirements = computed((): AlgorithmRunRequirement[] => { + const algorithm = this.selectedAlgorithm(); + if (!algorithm) return []; + + const requirements: AlgorithmRunRequirement[] = []; + + if (this.selectedAlgorithmUnavailable()) { + const summary = this.selectedAlgorithmAvailabilitySummary(); + requirements.push({ + id: 'availability', + message: summary || 'Algorithm requirements are not satisfied.', + actionLabel: this.availabilityRequirementActionLabel(algorithm.name), + }); + } + + const preprocessingMessage = this.preprocessingRunBlockMessage(); + if (preprocessingMessage) { + requirements.push({ + id: 'preprocessing', + message: preprocessingMessage, + actionLabel: 'Go to preprocessing', + }); + } + + return requirements; + }); + readonly selectedAlgorithmCategory = computed(() => this.selectedAlgorithm()?.category?.trim() ?? ''); + readonly yVar = computed(() => this.experimentStudioService.algorithmY()[0]?.code ?? null); + readonly xVar = computed(() => this.experimentStudioService.algorithmX()[0]?.code ?? null); readonly crossValidationEnabled = signal(false); private readonly crossValidationSelections: Record<string, boolean> = {}; - readonly transformationEnabled = signal(false); - private readonly transformationSelectionsByAlgorithm: Record<string, Record<string, string>> = {}; readonly isCrossValidationOnly = computed(() => { const algorithm = this.selectedAlgorithm(); if (!algorithm) return false; return this.experimentStudioService.isCrossValidationOnly(algorithm.name); }); - readonly canToggleTransformation = computed(() => { - const algorithm = this.selectedAlgorithm(); - if (!algorithm) return false; - const baseName = this.experimentStudioService.getTransformationBase(algorithm.name) ?? algorithm.name; - return !!this.experimentStudioService.getTransformationVariant(baseName); - }); - readonly transformationTypes = ['standardize', 'center', 'exp'] as const; - transformationAssignments: Record<string, string> = {}; + readonly outlierStrategies = OUTLIER_STRATEGIES; + readonly outlierTails = OUTLIER_TAILS; + outlierReportRules: Record<string, OutlierRule> = {}; + outlierReportValidationErrors: Record<string, string> = {}; readonly canToggleCrossValidation = computed(() => { const algorithm = this.selectedAlgorithm(); if (!algorithm) return false; @@ -79,71 +269,227 @@ export class AlgorithmPanelComponent { !!this.experimentStudioService.backendAlgorithms()[baseName] ); }); - readonly labelMap = computed(() => { - const map: Record<string, string> = {}; - const items = [ - ...this.experimentStudioService.selectedVariables(), - ...this.experimentStudioService.selectedCovariates(), - ...this.experimentStudioService.selectedFilters(), - ]; - items.forEach((item) => { - if (item?.code && item?.label) { - map[item.code] = item.label; - } - }); - return map; - }); - readonly resultAlgorithmLabel = computed(() => { - const algoKey = - this.experimentStudioService.lastUsedAlgorithm() || - this.selectedAlgorithm()?.name || - ''; - - if (!algoKey) return 'Algorithm'; - - const algoConfig = this.experimentStudioService.backendAlgorithms()[algoKey]; - return algoConfig?.label || this.prettifyLabel(algoKey); + readonly isOutlierReportSelected = computed( + () => this.selectedAlgorithm()?.name === AlgorithmNames.OUTLIER_REPORT + ); + readonly hasAlgorithmParameterControls = computed(() => ( + this.visibleConfigSchema().length > 0 || + this.isOutlierReportSelected() || + this.canToggleCrossValidation() || + this.isCrossValidationOnly() + )); + readonly preprocessingRunBlockMessage = computed(() => { + const algorithm = this.selectedAlgorithm(); + if (!algorithm) return null; + if (algorithm.name === AlgorithmNames.OUTLIER_REPORT) return null; + if ( + this.experimentStudioService.hasAppliedDescriptivePreprocessing() || + this.experimentStudioService.hasRequestPreprocessingForRun(algorithm.name) + ) return null; + return 'Apply missing value preprocessing before running algorithms.'; }); - readonly resultDisplayTitle = computed(() => { - const explicitTitle = this.result()?.title; - if (typeof explicitTitle === 'string' && explicitTitle.trim()) { - return explicitTitle.trim(); + private readonly formInvalid = signal(false); + /** + * "Remove variables not available in: …" when a variable the run uses (outcome, predictor + * or cohort filter) is missing from a selected dataset; '' otherwise. Such variables stay + * in the pool grayed out, so the run is blocked until the user removes them. + */ + readonly unavailableRunVariablesReason = computed(() => { + const map = this.experimentStudioService.unavailableVariables(); + if (!map.size) return ''; + const datasets = new Set<string>(); + for (const node of [ + ...this.experimentStudioService.algorithmY(), + ...this.experimentStudioService.algorithmX(), + ...this.experimentStudioService.selectedFilters(), + ]) { + unavailableDatasetsFor(map, { code: node?.code }).forEach((label) => datasets.add(label)); } - return `Result ${this.resultAlgorithmLabel()}`; + return unavailableVariableTooltip([...datasets], 'Remove variables not available in'); }); - - readonly lastUsedSchema = signal<any[]>([]); - readonly availableAlgorithmCategories = computed(() => { - const grouped = this.experimentStudioService.availableGroupedAlgorithms(); - return Object.entries(grouped).map(([name, algorithms]) => ({ name, algorithms })); + readonly canRun = computed(() => ( + !!this.selectedAlgorithm() && + !this.isRunning() && + !this.selectedAlgorithmUnavailable() && + !this.unavailableRunVariablesReason() && + !this.preprocessingRunBlockMessage() && + !this.formInvalid() + )); + readonly runDisabledReason = computed(() => { + if (this.isRunning()) return 'Experiment is running'; + if (!this.selectedAlgorithm()) return 'Select an algorithm to run'; + if (this.selectedAlgorithmUnavailable()) return 'Algorithm unavailable for the current selection'; + const unavailable = this.unavailableRunVariablesReason(); + if (unavailable) return unavailable; + const preprocessing = this.preprocessingRunBlockMessage(); + if (preprocessing) return preprocessing; + if (this.formInvalid()) return 'Complete variable and algorithm selection to proceed'; + return 'Execute experiment'; }); + readonly labelMap = this.experimentStudioService.variableLabelMap; - // UI toggle: show only enabled algorithms - readonly showOnlyActive = signal(false); + // UI toggle: the catalog defaults to runnable methods only — each group lists just the + // available algorithms. Switching to All re-lists unavailable methods with their reason. + readonly showOnlyActive = signal(true); + /** Instant text filter over label, name, description, documentation, category, and flags. */ + readonly algoSearchQuery = signal(''); readonly hasAnyVisibleAlgorithms = computed(() => { return this.filteredAlgorithmCategories().some(c => c.algorithms?.length > 0); }); - prettifyLabel(label: string): string { - if (!label) return ''; - return label - .replace(/_/g, ' ') - .replace(/\b\w/g, c => c.toUpperCase()); - } - prettyFieldLabel(field: any): string { let base = field.label ?? field.key; if ((field.key === 'groupA' || field.key === 'groupB')) { - const x = this.experimentStudioService.selectedCovariates(); - if (x.length === 1) { - const cov = x[0]; - const covName = cov?.label || cov?.name || cov?.code; - if (covName) base = `${base} (${covName})`; - } + const x = this.experimentStudioService.algorithmX(); + const cov = x[0]; + const covName = cov?.label || cov?.name || cov?.code; + if (covName) base = `${base} (${covName})`; } return base; } + private variableOptionsForRoles(roles: string[]): Array<{ code: string; label: string }> { + const roleList = roles.length ? roles : ['x']; + return roleList.flatMap((role) => { + const selected = + role === 'y' + ? this.experimentStudioService.algorithmY() + : role === 'x' + ? this.experimentStudioService.algorithmX() + : []; + + return selected + .map((item: any) => { + const code = item?.code; + if (code === null || code === undefined || code === '') return null; + return { + code: String(code), + label: String(item?.label ?? item?.name ?? code), + }; + }) + .filter((item): item is { code: string; label: string } => !!item); + }); + } + private enumOptionsForRole(role: string | null | undefined): any[] { + const selected = + role === 'x' + ? this.experimentStudioService.algorithmX()[0] + : this.experimentStudioService.algorithmY()[0]; + return Array.isArray(selected?.enumerations) ? [...selected.enumerations] : []; + } + + private resolveSelectedEventVarCode(algorithmName: string | undefined): string | null { + if (!algorithmName) return null; + + const live = this.eventVarSelection(); + if (live !== null && live !== undefined && String(live).trim() !== '') { + return String(live); + } + + const stored = untracked(() => this.experimentStudioService.algorithmConfigurations())[ + algorithmName + ]?.['event_var']; + if (stored !== undefined && stored !== null && String(stored).trim() !== '') { + return String(stored); + } + + return null; + } + + private buildConfigSchemaSignature( + algorithmName: string, + schema: Array<{ key?: string; type?: string; required?: boolean; options?: unknown[] }> + ): string { + return JSON.stringify({ + algorithmName, + crossValidation: this.crossValidationEnabled(), + fields: schema.map((field) => ({ + key: field.key, + type: field.type, + required: field.required, + options: (field.options ?? []).map((opt) => this.optionValue(opt)), + })), + }); + } + + private syncEventVarSelectionFromValues( + algorithmName: string, + values: Record<string, unknown> + ): void { + if (!Object.prototype.hasOwnProperty.call(values, 'event_var')) return; + const raw = values['event_var']; + const next = + raw !== null && raw !== undefined && String(raw).trim() !== '' ? String(raw) : null; + if (this.eventVarSelection() === next) return; + this.eventVarSelection.set(next); + this.syncPositiveClassWithEventVar(algorithmName); + } + + private syncEventVarSelectionFromStored(stored: Record<string, unknown>): void { + const raw = stored['event_var']; + const next = + raw !== null && raw !== undefined && String(raw).trim() !== '' ? String(raw) : null; + this.eventVarSelection.set(next); + } + + private enumOptionsForVariableCode( + varCode: string | null | undefined + ): Array<{ code: string; label: string }> { + if (!varCode) return []; + + const variables = [ + ...this.experimentStudioService.algorithmY(), + ...this.experimentStudioService.algorithmX(), + ]; + const variable = variables.find((item) => String(item?.code) === String(varCode)); + if (!variable) return []; + + const enumerations = Array.isArray(variable.enumerations) ? variable.enumerations : []; + const seen = new Set<string>(); + + return enumerations + .map((entry: any) => { + const code = entry?.code ?? entry?.value ?? entry?.name ?? entry?.label; + if (code === null || code === undefined || String(code).trim() === '') { + return null; + } + const normalizedCode = String(code); + if (seen.has(normalizedCode)) return null; + seen.add(normalizedCode); + return { + code: normalizedCode, + label: String(entry?.label ?? entry?.name ?? normalizedCode), + }; + }) + .filter( + (item: { code: string; label: string } | null): item is { code: string; label: string } => + !!item + ); + } + + /** Live event_var from the config form; avoids reading FormGroup inside other computeds. */ + private readonly eventVarSelection = signal<string | null>(null); + + readonly selectedEventVarCode = computed(() => { + const algorithm = this.selectedAlgorithm(); + return algorithm ? this.resolveSelectedEventVarCode(algorithm.name) : null; + }); + + readonly positiveClassOptions = computed(() => + this.enumOptionsForVariableCode(this.selectedEventVarCode()) + ); + + private isCoxRegressionAlgorithm(algorithmName: string | null | undefined): boolean { + return algorithmName === 'cox_regression_classical' || algorithmName === 'cox_regression_stacked'; + } + + private optionValue(opt: any): any { + if (opt && typeof opt === 'object') { + return opt.code ?? opt.value ?? opt.name ?? opt.label ?? String(opt); + } + return opt; + } + // sensible defaults private readonly uiDefaults: Record<string, any> = { alpha: 0.05, @@ -179,76 +525,51 @@ export class AlgorithmPanelComponent { ? stored : this.experimentStudioService.isCrossValidationAlgorithm(algorithm.name); this.crossValidationEnabled.set(defaultValue); - }, { allowSignalWrites: true }); - - effect(() => { - const algorithm = this.selectedAlgorithm(); - const variables = this.experimentStudioService.selectedVariables(); - if (!algorithm) { - this.transformationEnabled.set(false); - this.transformationAssignments = {}; - return; - } - - const baseName = - this.experimentStudioService.getTransformationBase(algorithm.name) ?? algorithm.name; - const hasVariant = !!this.experimentStudioService.getTransformationVariant(baseName); - - if (!hasVariant) { - this.transformationEnabled.set(false); - this.transformationAssignments = {}; - return; - } - - const storedConfigs = this.experimentStudioService.algorithmConfigurations(); - const storedData = storedConfigs?.[baseName]?.['data_transformation']; - const fromConfig = this.extractTransformationAssignments(storedData); - const previous = this.transformationSelectionsByAlgorithm[baseName] ?? {}; - - const next: Record<string, string> = {}; - (variables ?? []).forEach((v) => { - const code = v?.code; - if (!code) return; - next[code] = fromConfig[code] ?? previous[code] ?? 'none'; - }); - - this.transformationAssignments = next; - this.transformationSelectionsByAlgorithm[baseName] = { ...next }; - - const hasAny = Object.values(next).some(v => v && v !== 'none'); - this.transformationEnabled.set(hasAny); - }, { allowSignalWrites: true }); + }); effect(() => { // Establish dependencies this.selectedAlgorithm(); - this.experimentStudioService.selectedVariables(); - this.experimentStudioService.selectedCovariates(); + this.experimentStudioService.algorithmY(); + this.experimentStudioService.algorithmX(); - // Clear results and reset Save As mode on any selection change - this.result.set(null); - this.saveAsMode.set(false); - this.saveAsName.set(''); - }, { allowSignalWrites: true }); + // A new selection invalidates whatever the last run produced + this.experimentStudioService.runResult.set(null); + this.experimentStudioService.runError.set(null); + }); effect(() => { - const groups = this.experimentStudioService.availableGroupedAlgorithms(); - if (!groups) return; - this.availableAlgorithmCategories(); + const algorithm = this.selectedAlgorithm(); + const variables = this.outlierReportVariables(); + if (!algorithm || algorithm.name !== AlgorithmNames.OUTLIER_REPORT) { + this.outlierReportValidationErrors = {}; + return; + } + const stored = untracked(() => this.experimentStudioService.algorithmConfigurations()[algorithm.name] ?? {}); + this.reconcileOutlierReportRules(variables, stored); }); - effect(() => { + effect((onCleanup) => { const algorithm = this.selectedAlgorithm(); - const schema = this.enrichedConfigSchema(); + const schema = this.visibleConfigSchema(); if (!algorithm) { - // if no algorithm,clean form + this.lastBuiltFormSchemaSignature = ''; + this.eventVarSelection.set(null); this.configForm.set(new FormGroup({})); + this.formInvalid.set(false); + return; + } + + const schemaSignature = this.buildConfigSchemaSignature(algorithm.name, schema); + if (schemaSignature === this.lastBuiltFormSchemaSignature) { return; } + this.lastBuiltFormSchemaSignature = schemaSignature; - const allConfigs = this.experimentStudioService.algorithmConfigurations(); + const allConfigs = untracked(() => this.experimentStudioService.algorithmConfigurations()); const stored = allConfigs?.[algorithm.name] || {}; + this.syncEventVarSelectionFromStored(stored); const group: { [key: string]: FormControl } = {}; @@ -281,7 +602,7 @@ export class AlgorithmPanelComponent { const label = field.label && field.label.trim() !== '' ? field.label - : this.prettifyLabel(field.key); + : toHumanLabel(field.key); const prettyField = { ...field, @@ -314,7 +635,9 @@ export class AlgorithmPanelComponent { group[field.key] = control; }); - this.configForm.set(new FormGroup(group, { updateOn: 'change' })); + const form = new FormGroup(group, { updateOn: 'change' }); + this.configForm.set(form); + this.formInvalid.set(form.invalid); Object.values(this.configForm().controls).forEach(control => { if (control.valid) { @@ -322,10 +645,23 @@ export class AlgorithmPanelComponent { } }); + const statusSub = form.statusChanges.subscribe(() => this.formInvalid.set(form.invalid)); + const subscription = form.valueChanges.subscribe((values) => { + this.syncEventVarSelectionFromValues(algorithm.name, values); + this.persistCurrentFormConfig( + algorithm.name, + untracked(() => this.visibleConfigSchema()) + ); + }); + onCleanup(() => { + subscription.unsubscribe(); + statusSub.unsubscribe(); + }); + this.formKey++; }); effect(() => { - const res = this.result(); + const res = this.experimentStudioService.runResult(); const schema = this.outputSchema(); if (!res || !schema.length) return; @@ -336,32 +672,17 @@ export class AlgorithmPanelComponent { } }); - // Auto-expand categories when they contain available algorithms effect(() => { const categories = this.filteredAlgorithmCategories(); - const newlyAvailable = categories - .filter(c => c.algorithms.some(a => !a.isDisabled)) - .map(c => c.name); - - if (newlyAvailable.length > 0) { - this.openCategories.update(current => { - const next = [...current]; - let changed = false; - newlyAvailable.forEach(cat => { - if (!next.includes(cat)) { - next.push(cat); - changed = true; - } - }); - return changed ? next : current; - }); - } - }, { allowSignalWrites: true }); + const selectedName = this.selectedAlgorithm()?.name ?? null; + untracked(() => this.syncMatchCategoryOpen(categories, selectedName)); + }); } configForm = signal<FormGroup>(new FormGroup({})); formKey = 0; + private lastBuiltFormSchemaSignature = ''; enrichedConfigSchema = computed(() => { const algorithm = this.selectedAlgorithm(); @@ -370,11 +691,26 @@ export class AlgorithmPanelComponent { // Basic algorithm schema (shallow copy) const schema = (algorithm.configSchema ?? []).map(f => ({ ...f })); - const yVar = this.experimentStudioService.selectedVariables()[0]; - const xVar = this.experimentStudioService.selectedCovariates()[0]; - + const yVars = this.experimentStudioService.algorithmY(); + const xVars = this.experimentStudioService.algorithmX(); + const yVar = yVars[0]; + const xVar = xVars[0]; + const eventVarCode = this.selectedEventVarCode(); + const positiveClassOptions = this.enumOptionsForVariableCode(eventVarCode); + const isCox = this.isCoxRegressionAlgorithm(algorithm.name); const enriched = schema.map(field => { let options = field.options ?? []; + const enumSource = Array.isArray(field.enumSource) ? field.enumSource : []; + + if (field.enumType === 'input_var_names') { + options = this.variableOptionsForRoles(enumSource); + } else if (field.enumType === 'input_var_CDE_enums') { + options = this.enumOptionsForRole(enumSource[0]); + } else if (field.key === 'category_order' && yVar?.enumerations?.length) { + options = [...yVar.enumerations]; + } else if (field.key === 'positive_class' && positiveClassOptions.length) { + options = positiveClassOptions; + } // Placeholder substitution for enums if (Array.isArray(options) && options.length === 1) { @@ -387,21 +723,37 @@ export class AlgorithmPanelComponent { } } - // Fallback for empty select fields + // Fallback for empty select fields (not input_var_names — those list y/x variables) if ( field.type === 'select' && + field.enumType !== 'input_var_names' && (!options || options.length === 0) && yVar?.enumerations?.length ) { options = [...yVar.enumerations]; } + const defaultValue = field.key === 'category_order' && Array.isArray(options) && options.length > 0 + ? options.map((opt: any) => this.optionValue(opt)) + : field.default; + // Normalize label and desc - const label = field.label?.trim() || this.prettifyLabel(field.key); - const desc = field.desc ?? field.description ?? ''; + const label = field.label?.trim() || toHumanLabel(field.key); + const desc = + field.key === 'positive_class' && isCox + ? 'Event level mapped to 1; other observed levels are mapped to 0.' + : field.desc ?? field.description ?? ''; + const type = + field.key === 'positive_class' && positiveClassOptions.length + ? 'select' + : field.type; + const required = + (field.key === 'positive_class' && isCox) || + field.required === true || + String(field.required ?? '').trim().toLowerCase() === 'true'; // Return enriched field - return { ...field, label, desc, options }; + return { ...field, label, desc, options, default: defaultValue, type, required }; }); if (this.crossValidationEnabled() && this.canToggleCrossValidation()) { @@ -409,8 +761,8 @@ export class AlgorithmPanelComponent { if (!hasSplits) { enriched.push({ key: 'n_splits', - label: 'Cross-validation folds (n_splits)', - desc: 'Number of folds to use for cross-validation.', + label: 'Folds', + desc: 'Number of parts the data is split into; each part is held out once.', type: 'number', min: 2, default: 5, @@ -421,25 +773,85 @@ export class AlgorithmPanelComponent { return enriched; }); + readonly visibleConfigSchema = computed(() => { + const schema = this.enrichedConfigSchema(); + if (!this.isOutlierReportSelected()) return schema; + return schema.filter((field) => field.type !== 'dict' && !['strategies', 'tails', 'folds'].includes(String(field.key))); + }); + + /** Backend metadata has no advanced flag yet; the heuristic mirrors the review handoff. */ + isAdvancedField(field: any): boolean { + return field?.advanced ?? (field?.default !== undefined && !field?.required); + } + + /** n_splits belongs to the cross-validation row, so it renders under that switch, not in the main form. */ + isCrossValidationField(field: any): boolean { + return field?.key === 'n_splits' && (this.canToggleCrossValidation() || this.isCrossValidationOnly()); + } + + readonly primaryConfigSchema = computed(() => + this.visibleConfigSchema().filter((field) => + !this.isCrossValidationField(field) && !this.isAdvancedField(field) && this.configForm().get(field.key)) + ); + + readonly advancedConfigSchema = computed(() => + this.visibleConfigSchema().filter((field) => + !this.isCrossValidationField(field) && this.isAdvancedField(field) && this.configForm().get(field.key)) + ); + + readonly crossValidationConfigSchema = computed(() => + this.crossValidationEnabled() + ? this.visibleConfigSchema().filter((field) => this.isCrossValidationField(field) && this.configForm().get(field.key)) + : [] + ); + + readonly outlierReportVariables = computed(() => { + const unique = new Map<string, any>(); + this.experimentStudioService.algorithmAssignableVariables().forEach((variable: any) => { + if (variable?.code) unique.set(String(variable.code), variable); + }); + return Array.from(unique.values()).filter((variable) => isOutlierEligibleVariable(variable)); + }); + readonly outputSchema = computed(() => getOutputSchema(this.selectedAlgorithm()?.name ?? '') ?? [] ); - + readonly searchActive = computed(() => this.algoSearchQuery().trim().length > 0); + + private matchesAlgorithmSearch(algorithm: AlgorithmConfig, query: string): boolean { + if (!query) return true; + const haystack = [ + algorithm.label, + algorithm.name, + algorithm.description, + algorithm.documentation ?? '', + algorithm.category ?? '', + (algorithm.flags ?? []).join(' '), + ].join(' ').toLowerCase(); + return query.split(/\s+/).every((term) => haystack.includes(term)); + } readonly filteredAlgorithmCategories = computed(() => { const grouped = this.experimentStudioService.availableGroupedAlgorithms(); const onlyActive = this.showOnlyActive(); + const query = this.algoSearchQuery().trim().toLowerCase(); const entries = Object.entries(grouped ?? {}).map(([name, algorithms]) => { - const filtered = onlyActive - ? algorithms.filter(a => !a.isDisabled) - : algorithms; + let filtered = (algorithms ?? []).filter(a => this.matchesAlgorithmSearch(a, query)); + if (onlyActive) { + filtered = filtered.filter(a => !a.isDisabled); + } + + // Runnable-first: when showing all methods, surface runnable ones before disabled ones. + const ordered = onlyActive + ? filtered + : [...filtered].sort((a, b) => Number(a.isDisabled) - Number(b.isDisabled)); - return { name, algorithms: filtered }; + return { name, algorithms: ordered }; }); - return onlyActive ? entries.filter(c => c.algorithms.length > 0) : entries; + return entries.filter(c => c.algorithms.length > 0); }); readonly algoCounts = computed(() => { @@ -453,6 +865,101 @@ export class AlgorithmPanelComponent { this.showOnlyActive.update(v => !v); } + onSearchInput(event: Event): void { + this.algoSearchQuery.set((event.target as HTMLInputElement)?.value ?? ''); + } + + clearSearch(): void { + this.algoSearchQuery.set(''); + } + + /** + * Collapsed Matching-method categories. A category missing from this set is open, so + * categories coming back after a filter switch keep the state the user left them in. + */ + private readonly collapsedMatchCategories = signal<ReadonlySet<string>>(new Set<string>()); + /** Categories the user opened/closed themselves; these are never changed automatically. */ + private readonly matchCategoryUserToggles = new Set<string>(); + private lastMatchSelectionName: string | null | undefined; + + isMatchCategoryOpen(categoryName: string): boolean { + return !this.collapsedMatchCategories().has(categoryName); + } + + toggleMatchCategory(categoryName: string): void { + this.matchCategoryUserToggles.add(categoryName); + const next = new Set(this.collapsedMatchCategories()); + if (next.has(categoryName)) { + next.delete(categoryName); + } else { + next.add(categoryName); + } + this.collapsedMatchCategories.set(next); + } + + /** + * Default state: a group is open only when it holds the selected method or at least one + * runnable method — with nothing runnable in a group it stays collapsed so the catalog + * opens straight to what can actually be executed. Beyond that default, the group a new + * selection lands in is revealed; groups the user toggled are never changed automatically. + */ + private syncMatchCategoryOpen( + categories: Array<{ name: string; algorithms: AlgorithmConfig[] }>, + selectedName: string | null, + ): void { + const selectionChanged = this.lastMatchSelectionName !== selectedName; + this.lastMatchSelectionName = selectedName; + + const selectedCategory = selectedName + ? categories.find((category) => + category.algorithms.some((algorithm) => algorithm.name === selectedName))?.name + : null; + + const next = new Set(this.collapsedMatchCategories()); + let changed = false; + + categories.forEach((category) => { + if (this.matchCategoryUserToggles.has(category.name)) return; + const shouldOpen = + category.name === selectedCategory || + category.algorithms.some((algorithm) => !algorithm.isDisabled); + if (shouldOpen === !next.has(category.name)) return; + if (shouldOpen) { + next.delete(category.name); + } else { + next.add(category.name); + } + changed = true; + }); + + if (selectedCategory && selectionChanged && next.delete(selectedCategory)) { + changed = true; + } + + if (changed) this.collapsedMatchCategories.set(next); + } + + algorithmUnavailableReason(algorithm: AlgorithmConfig): string { + const availability = this.experimentStudioService.getAlgorithmAvailability(algorithm.name); + const summary = availability?.summary || algorithm.availability?.summary || ''; + if (summary) return this.formatAvailabilityMessage(summary); + const details = availability?.details?.length + ? availability.details + : (algorithm.availability?.details ?? []); + const first = details.find((detail) => detail.messages.length > 0); + return first ? this.formatAvailabilityMessage(first.messages[0]) : 'Unavailable'; + } + + + readonly datasetsWithLabels = computed(() => { + const codes = this.experimentStudioService.selectedDatasets(); + const labelByCode = this.experimentStudioService.getDatasetLabelMap(); + const fallbackMap = this.labelMap(); + return codes.map((code) => ({ + code, + label: labelByCode[code] ?? fallbackMap[code] ?? toHumanLabel(code), + })); + }); readonly experimentInfo = computed(() => { const selected = this.experimentStudioService.selectedAlgorithm(); @@ -471,65 +978,308 @@ export class AlgorithmPanelComponent { const configs = (algoName && allConfigs[algoName]) || {}; + const preprocessingSteps = this.experimentStudioService.formatPreprocessingEntries( + this.experimentStudioService.getAppliedDescriptivePreprocessing(), + this.labelMap(), + this.experimentStudioService.getCategoricalEnumMaps(), + ); + return { experimentName: defaultName, - datasets: this.experimentStudioService.selectedDatasets(), - variables: this.experimentStudioService.selectedVariables(), - covariates: this.experimentStudioService.selectedCovariates(), + variables: this.experimentStudioService.algorithmY(), + covariates: this.experimentStudioService.algorithmX(), filters: this.experimentStudioService.selectedFilters(), algorithmConfigs: configs, + preprocessingSteps, }; }); + private syncPositiveClassWithEventVar(algorithmName: string): void { + const positiveControl = this.configForm().get('positive_class'); + if (!positiveControl) return; + + const options = this.enumOptionsForVariableCode(this.resolveSelectedEventVarCode(algorithmName)); + if (!options.length) return; + + const allowed = new Set(options.map((option) => option.code)); + const current = positiveControl.value; + if ( + current !== null && + current !== undefined && + String(current).trim() !== '' && + !allowed.has(String(current)) + ) { + positiveControl.setValue(null, { emitEvent: false }); + } + } + + private persistCurrentFormConfig(algorithmName: string, schema = this.visibleConfigSchema()): Record<string, any> { + const values = this.normalizeFormValues(this.configForm().getRawValue(), schema); + const nextValues = algorithmName === AlgorithmNames.OUTLIER_REPORT + ? this.mergeOutlierReportConfig(values) + : values; + const allConfigs = this.experimentStudioService.algorithmConfigurations(); + const previous = allConfigs[algorithmName]; + if (previous && JSON.stringify(previous) === JSON.stringify(nextValues)) { + return nextValues; + } + this.experimentStudioService.algorithmConfigurations.set({ + ...allConfigs, + [algorithmName]: nextValues, + }); + return nextValues; + } - objectKeys(obj: any): string[] { - return obj ? Object.keys(obj) : []; + bindOptionValue(option: unknown): string { + return optionBindingValue(option); } - prettifyKey(key: string): string { - return key - .replace(/_/g, ' ') - .replace(/\b\w/g, (char) => char.toUpperCase()); + private normalizeFormValues(values: Record<string, any>, schema: any[]): Record<string, any> { + const normalized = { ...values }; + schema + .filter((field) => field?.type === 'number') + .forEach((field) => { + const key = String(field.key); + const value = normalized[key]; + if (typeof value !== 'string') return; + const trimmed = value.trim(); + if (!trimmed) return; + const parsed = Number(trimmed); + if (Number.isFinite(parsed)) normalized[key] = parsed; + }); + + schema.forEach((field) => { + const key = String(field.key); + if (!(key in normalized)) return; + if (field?.type === 'select' || field?.type === 'multi-select' || key === 'positive_class') { + normalized[key] = serializeAlgorithmParameterValue(normalized[key], field); + } + }); + + return omitEmptyOptionalParameters(normalized, schema) as Record<string, any>; } + private mergeOutlierReportConfig(configValues: Record<string, any>): Record<string, any> { + if (!this.isOutlierReportSelected()) return configValues; + delete configValues['strategies']; + delete configValues['tails']; + delete configValues['folds']; + const payload = serializeOutlierRules( + this.outlierReportRules, + new Set(this.outlierReportVariables().map((variable) => variable.code)) + ); + return payload ? { ...configValues, ...payload } : configValues; + } + private persistOutlierReportConfig(): void { + const algorithm = this.selectedAlgorithm(); + if (!algorithm || algorithm.name !== AlgorithmNames.OUTLIER_REPORT) return; + const visibleValues = this.normalizeFormValues(this.configForm().getRawValue(), this.visibleConfigSchema()); + const nextConfig = this.mergeOutlierReportConfig(visibleValues); + this.experimentStudioService.algorithmConfigurations.set({ + ...this.experimentStudioService.algorithmConfigurations(), + [algorithm.name]: nextConfig, + }); + } + + private reconcileOutlierReportRules(variables: any[], stored: Record<string, any>): void { + const allowedCodes = new Set(variables.map((variable) => variable.code)); + const hydrated = hydrateOutlierRules(stored, allowedCodes); + const hasStoredRules = !!stored && typeof stored === 'object' && Object.keys(stored?.['strategies'] ?? {}).length > 0; + const hasExistingRules = Object.keys(this.outlierReportRules).length > 0; + const next: Record<string, OutlierRule> = {}; + + variables.forEach((variable) => { + const code = variable.code; + next[code] = this.outlierReportRules[code] + ? { ...this.outlierReportRules[code] } + : hydrated[code] + ? { ...hydrated[code] } + : createDefaultOutlierRule(code, hasExistingRules || !hasStoredRules); + }); + + this.outlierReportRules = next; + this.outlierReportValidationErrors = Object.fromEntries( + Object.entries(this.outlierReportValidationErrors).filter(([code]) => allowedCodes.has(code)) + ); + } + + outlierReportRuleFor(variable: any): OutlierRule { + if (!this.outlierReportRules[variable.code]) { + this.outlierReportRules = { + ...this.outlierReportRules, + [variable.code]: createDefaultOutlierRule(variable.code, true), + }; + } + return this.outlierReportRules[variable.code]; + } + + outlierVariableLabel(variable: any): string { + return variable?.label ?? variable?.name ?? variable?.code ?? ''; + } + + onOutlierReportEnabledChange(variable: any, enabled: boolean): void { + const existing = this.outlierReportRuleFor(variable); + this.outlierReportRules = { + ...this.outlierReportRules, + [variable.code]: { ...existing, enabled }, + }; + this.outlierReportValidationErrors = { + ...this.outlierReportValidationErrors, + [variable.code]: '', + }; + this.persistOutlierReportConfig(); + } + + onOutlierReportStrategyChange(variable: any, strategy: OutlierStrategy): void { + const existing = this.outlierReportRuleFor(variable); + this.outlierReportRules = { + ...this.outlierReportRules, + [variable.code]: { + ...existing, + enabled: true, + strategy, + fold: defaultFoldForStrategy(strategy), + }, + }; + this.outlierReportValidationErrors = { + ...this.outlierReportValidationErrors, + [variable.code]: '', + }; + this.persistOutlierReportConfig(); + } + + onOutlierReportTailChange(variable: any, tail: OutlierTail): void { + const existing = this.outlierReportRuleFor(variable); + this.outlierReportRules = { + ...this.outlierReportRules, + [variable.code]: { ...existing, enabled: true, tail }, + }; + this.persistOutlierReportConfig(); + } + + onOutlierReportFoldChange(variable: any, rawValue: string | number): void { + const existing = this.outlierReportRuleFor(variable); + const value = String(rawValue ?? '').trim(); + const fold = value === '' ? null : Number(value); + this.outlierReportRules = { + ...this.outlierReportRules, + [variable.code]: { + ...existing, + enabled: true, + fold: typeof fold === 'number' && Number.isFinite(fold) ? fold : null, + }, + }; + const error = validateOutlierRule(this.outlierReportRules[variable.code]); + this.outlierReportValidationErrors = { + ...this.outlierReportValidationErrors, + [variable.code]: error ?? '', + }; + this.persistOutlierReportConfig(); + } + + outlierReportRuleError(variable: any): string { + const rule = this.outlierReportRuleFor(variable); + return this.outlierReportValidationErrors[variable.code] || validateOutlierRule(rule) || ''; + } + + private validateOutlierReportConfig(): boolean { + if (!this.isOutlierReportSelected()) return true; + + const variables = this.outlierReportVariables(); + if (!variables.length) { + this.errorMsg.set('Select at least one numerical variable or covariate for the outlier report.'); + return false; + } + + const errors: Record<string, string> = {}; + variables.forEach((variable) => { + const rule = this.outlierReportRuleFor(variable); + const error = validateOutlierRule(rule); + if (error) errors[variable.code] = error; + }); + + this.outlierReportValidationErrors = errors; + if (Object.keys(errors).length > 0) { + this.errorMsg.set('Fix the outlier report configuration before running.'); + return false; + } + + const serialized = serializeOutlierRules( + this.outlierReportRules, + new Set(variables.map((variable) => variable.code)) + ); + if (!serialized) { + this.errorMsg.set('Enable at least one numerical variable for the outlier report.'); + return false; + } + + this.persistOutlierReportConfig(); + return true; + } + + + /** Transformation grid operates on the assigned outcome variables (y). */ get selectedVariables() { - return this.experimentStudioService.selectedVariables(); + return this.experimentStudioService.algorithmY(); } - get selectedCovariates() { - return this.experimentStudioService.selectedCovariates(); + /** Assignable pool drives the role-assignment UI (shown without a selected algorithm). */ + get assignablePool() { + return this.experimentStudioService.algorithmAssignableVariables(); } - openCategories = signal<string[]>([]); - infoPanelOpen = false; - tooltipVisible = false; - tooltipPosition = { x: 0, y: 0 }; - tooltipData: any = null; + + readonly tooltipVisible = signal(false); + readonly tooltipPosition = signal({ x: 0, y: 0 }); + readonly tooltipData = signal<any | null>(null); selectAlgorithm(algorithm: AlgorithmConfig) { + this.lastBuiltFormSchemaSignature = ''; + this.eventVarSelection.set(null); // Use service method so it enriches configSchema and persists to sessionStorage this.experimentStudioService.setAlgorithm(algorithm); - this.result.set(null); + this.experimentStudioService.runResult.set(null); + this.experimentStudioService.runError.set(null); this.errorMsg.set(null); } onAlgorithmClick(algorithm: AlgorithmConfig) { - if (algorithm.isDisabled) { - console.log( - '[AlgorithmPanel] click on disabled algorithm blocked:', - algorithm.name - ); - return; - } - this.selectAlgorithm(algorithm); + // Parent bookkeeping only: the details and parameters bands render from + // selectedAlgorithm() below the row, so no substep navigation or page scroll. + this.setStudioSubstep('parameters'); + this.hideTooltip(); + } + + /** Scrolls a studio anchor; `scroll-margin-top` on the target clears sticky chrome. */ + private scrollStudioTargetIntoView(target: HTMLElement | null, focus = false): void { + if (!target) return; + target.scrollIntoView({ behavior: 'smooth', block: 'start' }); + if (focus) target.focus({ preventScroll: true }); } onClickRunExp() { this.errorMsg.set(null); this.errorService.clearError(); + if (this.selectedAlgorithmUnavailable()) { + this.errorMsg.set('This algorithm is unavailable for the current selection.'); + return; + } + + const unavailableVariables = this.unavailableRunVariablesReason(); + if (unavailableVariables) { + this.errorMsg.set(unavailableVariables); + return; + } + + const preprocessingMessage = this.preprocessingRunBlockMessage(); + if (preprocessingMessage) { + this.errorMsg.set(preprocessingMessage); + return; + } + // algorithm does not run if form is invalid if (this.configForm() && this.configForm().invalid) { this.configForm().markAllAsTouched(); @@ -540,13 +1290,23 @@ export class AlgorithmPanelComponent { const algo = this.experimentStudioService.selectedAlgorithm(); this.errorMsg.set(null); this.errorService.clearError(); - this.loadingText.set('Processing experiment...'); + this.experimentStudioService.runStatusText.set('Processing experiment...'); + this.experimentStudioService.hasRunStarted.set(true); + this.experimentStudioService.runError.set(null); + this.experimentStudioService.runResult.set(null); + this.experimentStudioService.runId.set(null); this.experimentStudioService.setRunning(true); if (!algo) { console.error('No algorithm selected in service.'); const msg = 'Please choose an algorithm before running.'; this.errorMsg.set(msg); + this.experimentStudioService.setRunning(false); + this.experimentStudioService.hasRunStarted.set(false); + return; + } + + if (!this.validateOutlierReportConfig()) { this.experimentStudioService.setRunning(false); return; } @@ -554,57 +1314,46 @@ export class AlgorithmPanelComponent { const isCvOnly = this.experimentStudioService.isCrossValidationOnly(algo.name); const baseAlgorithmName = isCvOnly ? algo.name - : this.experimentStudioService.getCrossValidationBase(algo.name) ?? - this.experimentStudioService.getTransformationBase(algo.name) ?? - algo.name; + : this.experimentStudioService.getCrossValidationBase(algo.name) ?? algo.name; const cvVariant = this.experimentStudioService.getCrossValidationVariant(baseAlgorithmName); const shouldIncludeSplits = this.crossValidationEnabled() || this.experimentStudioService.isCrossValidationOnly(algo.name); const useCrossValidation = shouldIncludeSplits && !!cvVariant; - const transformationVariant = - this.experimentStudioService.getTransformationVariant(baseAlgorithmName); - const useTransformation = this.transformationEnabled() && !!transformationVariant; const effectiveAlgorithmName = useCrossValidation ? cvVariant - : useTransformation - ? transformationVariant - : baseAlgorithmName; + : baseAlgorithmName; const finalAlgorithmName = isCvOnly ? algo.name : effectiveAlgorithmName; this.experimentStudioService.lastUsedAlgorithm.set(finalAlgorithmName); this.errorMsg.set(null); - const configValues = this.configForm().getRawValue(); + let configValues = this.normalizeFormValues(this.configForm().getRawValue(), this.visibleConfigSchema()); + configValues = this.mergeOutlierReportConfig(configValues); if (!shouldIncludeSplits && configValues['n_splits'] !== undefined) { delete configValues['n_splits']; } - if (useTransformation) { - const payload = this.buildTransformationPayload(); - configValues.data_transformation = payload; - } else if (configValues.data_transformation) { - delete configValues.data_transformation; - } this.experimentStudioService.algorithmConfigurations.set({ ...this.experimentStudioService.algorithmConfigurations(), [baseAlgorithmName]: configValues, ...(effectiveAlgorithmName !== baseAlgorithmName ? { [effectiveAlgorithmName]: configValues } : {}) }); - const result$ = this.experimentStudioService.runSelectedAlgorithmTransient( + // Freeze the setup before the request leaves: the Execution step describes the run from + // this snapshot, and parameter edits after the run must not rewrite it. + this.experimentStudioService.captureRunSetup(finalAlgorithmName); + + const result$ = this.experimentStudioService.runSelectedAlgorithm( baseAlgorithmName, finalAlgorithmName ); if (!result$) { const msg = 'Unable to start the run. Check your selections.'; this.errorMsg.set(msg); - this.result.set({ - status: 'error', - error: msg, - }); this.experimentStudioService.setRunning(false); + this.experimentStudioService.hasRunStarted.set(false); return; } @@ -617,24 +1366,23 @@ export class AlgorithmPanelComponent { payload?.data || payload?.message || 'The server returned an error for this run.'; - this.errorMsg.set(msg); - this.result.set({ - status: 'error', - error: msg, - payload, - }); + this.experimentStudioService.runError.set(msg); return; } const schema = getOutputSchema(finalAlgorithmName ?? '') ?? []; - this.result.set({ + this.experimentStudioService.runResult.set({ ...res?.result ?? { message: "No result returned" }, }); + // Save persists this kept run, so the saved experiment is the result shown here. + this.experimentStudioService.runId.set(res?.uuid ?? null); this.lastUsedAlgorithm = finalAlgorithmName; - this.lastUsedSchema.set(schema); + this.experimentStudioService.lastRunSchema.set(schema); }, error: () => { - this.errorMsg.set('Unable to run experiment. Please try again.'); + this.experimentStudioService.runError.set('Unable to run experiment. Please try again.'); + // An errored stream never completes, so the run state is released here too. + this.experimentStudioService.setRunning(false); }, complete: () => { this.experimentStudioService.setRunning(false); @@ -643,23 +1391,6 @@ export class AlgorithmPanelComponent { } - isRunButtonDisabled(): boolean { - const algo = this.selectedAlgorithm(); - - // if no algorithm -> disabled - if (!algo) return true; - - // if experiment running -> disabled - if (this.isRunning()) return true; - - - if (this.configForm() && Object.keys(this.configForm().controls).length > 0) { - return this.configForm().invalid; - } - - return false; - } - toggleCrossValidation(event: Event) { const target = event.target as HTMLInputElement | null; const enabled = !!target?.checked; @@ -668,114 +1399,148 @@ export class AlgorithmPanelComponent { const baseName = this.experimentStudioService.getCrossValidationBase(algorithm.name) ?? algorithm.name; const variant = this.experimentStudioService.getCrossValidationVariant(baseName); if (!variant) return; + const configs = this.experimentStudioService.algorithmConfigurations(); + const currentFormValues = this.configForm()?.getRawValue?.() ?? {}; + const preservedConfig = { ...(configs[baseName] ?? {}), ...currentFormValues }; + this.crossValidationSelections[baseName] = enabled; - this.crossValidationEnabled.set(enabled); if (!enabled) { + if (preservedConfig['n_splits'] !== undefined) delete preservedConfig['n_splits']; + if (this.configForm()?.contains('n_splits')) { this.configForm().removeControl('n_splits'); } - const configs = this.experimentStudioService.algorithmConfigurations(); - const baseConfig = { ...(configs[baseName] ?? {}) }; const variantConfig = { ...(configs[variant] ?? {}) }; - if (baseConfig['n_splits'] !== undefined) delete baseConfig['n_splits']; if (variantConfig['n_splits'] !== undefined) delete variantConfig['n_splits']; this.experimentStudioService.algorithmConfigurations.set({ ...configs, - [baseName]: baseConfig, + [baseName]: preservedConfig, [variant]: variantConfig, }); + } else { + this.experimentStudioService.algorithmConfigurations.set({ + ...configs, + [baseName]: preservedConfig, + [variant]: { ...(configs[variant] ?? {}), ...preservedConfig }, + }); } + + this.crossValidationEnabled.set(enabled); } - toggleTransformation(event: Event) { - const target = event.target as HTMLInputElement | null; - const enabled = !!target?.checked; - const algorithm = this.selectedAlgorithm(); - if (!algorithm) return; - const baseName = this.experimentStudioService.getTransformationBase(algorithm.name) ?? algorithm.name; - if (!this.experimentStudioService.getTransformationVariant(baseName)) return; - this.transformationEnabled.set(enabled); + private multiSelectValues(key: string): string[] { + const value = this.configForm().get(key)?.value; + if (Array.isArray(value)) return value.map((entry) => String(entry)); + if (typeof value === 'string' && value.trim() !== '') { + return value.split(',').map((entry) => entry.trim()).filter((entry) => entry !== ''); + } + return []; } - setTransformationAssignment(variableCode: string, value: string) { - if (!variableCode) return; - this.transformationAssignments = { - ...this.transformationAssignments, - [variableCode]: value, - }; + isMultiSelectOptionSelected(key: string, option: unknown): boolean { + const value = String(this.bindOptionValue(option)); + return this.multiSelectValues(key).includes(value); + } - const algorithm = this.selectedAlgorithm(); - if (!algorithm) return; - const baseName = this.experimentStudioService.getTransformationBase(algorithm.name) ?? algorithm.name; - this.transformationSelectionsByAlgorithm[baseName] = { ...this.transformationAssignments }; + toggleMultiSelectOption(key: string, option: unknown): void { + const control = this.configForm().get(key); + if (!control) return; + const value = String(this.bindOptionValue(option)); + const current = this.multiSelectValues(key); + const next = current.includes(value) + ? current.filter((entry) => entry !== value) + : [...current, value]; + control.setValue(next); + control.markAsTouched(); + control.updateValueAndValidity(); } - getTransformationAssignment(variableCode: string): string { - return this.transformationAssignments?.[variableCode] ?? 'none'; + isAlgorithmAvailable(algorithm: string): boolean { + return this.experimentStudioService.isAlgorithmAvailable(algorithm); } - private extractTransformationAssignments(data: any): Record<string, string> { - if (!data || typeof data !== 'object') return {}; - const result: Record<string, string> = {}; - this.transformationTypes.forEach((type) => { - const values = Array.isArray(data[type]) ? data[type] : []; - values.forEach((code: any) => { - if (code === null || code === undefined) return; - result[String(code)] = type; - }); - }); - return result; + availabilityDetails(algorithm: AlgorithmConfig | null = null): AlgorithmAvailabilityDetail[] { + const target = algorithm ?? this.tooltipData(); + if (!target) return []; + if (target.availability?.details?.length) return target.availability.details; + return this.experimentStudioService.getAlgorithmAvailability(target.name).details ?? []; } - private buildTransformationPayload(): Record<string, string[]> { - const payload: Record<string, string[]> = { - standardize: [], - center: [], - exp: [], - }; - Object.entries(this.transformationAssignments).forEach(([code, choice]) => { - if (!choice || choice === 'none') return; - if (!payload[choice]) payload[choice] = []; - payload[choice].push(code); - }); - Object.keys(payload).forEach((key) => { - if (!payload[key].length) delete payload[key]; - }); - return payload; + availabilityRequirementText(detail: AlgorithmAvailabilityDetail): string { + const count = this.formatAvailabilityCount(detail); + const parts = [detail.label + ': ' + count + ', selected ' + detail.selectedCount]; + const types = this.formatRequirementTypes(detail.types); + if (types) { + parts.push('type: ' + types.join(', ')); + } + return parts.join(' • '); } - isAlgorithmAvailable(algorithm: string): boolean { - return this.experimentStudioService.isAlgorithmAvailable(algorithm); + private formatAvailabilityCount(detail: AlgorithmAvailabilityDetail): string { + return formatInputCountRange(detail.minCount, detail.maxCount); + } + + private formatAvailabilityMessage(message: string): string { + return message.replace( + /((?:Outcome|Predictor) type must be one of )([^.]*)\./g, + (_match, prefix: string, types: string) => { + const displayTypes = this.formatRequirementTypes( + types.split(',').map((type) => type.trim()) + ); + return prefix + (displayTypes ?? []).join(', ') + '.'; + } + ); + } + + resolveRunRequirement(requirement: AlgorithmRunRequirement): void { + if (requirement.id === 'preprocessing') { + this.studioNavigation.goToPreprocessing(); + return; + } + // Availability issues are fixed on the setup board, which stays on screen above. + this.setStudioSubstep('setup'); + requestAnimationFrame(() => + this.scrollStudioTargetIntoView( + document.querySelector<HTMLElement>('[data-guide="guide-role-assignment"]'), + true + ) + ); } - toggleInfoPanel() { - this.infoPanelOpen = !this.infoPanelOpen; + private availabilityRequirementActionLabel(algorithmName: string): string { + const failingDetail = this.experimentStudioService + .getAlgorithmAvailability(algorithmName) + .details + .find((detail) => !detail.satisfied && detail.messages.length > 0); + + if (failingDetail?.role === 'x') { + return 'Assign predictor'; + } + + return 'Assign outcome'; } showTooltip(algorithm: any, event: MouseEvent) { event.stopPropagation(); const item = (event.currentTarget as HTMLElement)?.closest('li') || (event.currentTarget as HTMLElement); const rect = item.getBoundingClientRect(); - const panel = item.closest('.algorithm-panel') as HTMLElement | null; - const panelRect = panel?.getBoundingClientRect(); + const container = item.closest('.experiment-container') as HTMLElement | null; + const containerRect = container?.getBoundingClientRect() ?? new DOMRect(0, 0, window.innerWidth, window.innerHeight); const viewportWidth = window.innerWidth; const viewportHeight = window.innerHeight; const padding = 16; const offset = 12; - // Horizontal: To the right of the sidebar panel - let x = (panelRect?.right ?? rect.right) + offset; - // Vertical: Aligned with the top of the hovered item - let y = rect.top; + this.tooltipVisible.set(true); + this.tooltipData.set(algorithm); - this.tooltipVisible = true; - this.tooltipData = algorithm; + const initialPosition = this.getTooltipPosition(rect, containerRect, 320, 200, viewportWidth, viewportHeight, padding, offset); // Immediate initial placement - this.tooltipPosition = { x, y }; + this.tooltipPosition.set(initialPosition); // Refinement after render setTimeout(() => { @@ -785,204 +1550,108 @@ export class AlgorithmPanelComponent { const w = el.offsetWidth || 320; const h = el.offsetHeight || 200; - // Handle horizontal overflow - if (x + w > viewportWidth - padding) { - x = (panelRect?.left ?? rect.left) - w - offset; - } - - // Handle vertical overflow (shift up if it hits the bottom) - if (y + h > viewportHeight - padding) { - y = viewportHeight - h - padding; - } - - // Safety: don't let it go off the top - if (y < padding) y = padding; - - this.tooltipPosition = { x: Math.max(padding, x), y }; + this.tooltipPosition.set( + this.getTooltipPosition(rect, containerRect, w, h, viewportWidth, viewportHeight, padding, offset) + ); }, 0); - - if (algorithm.isDisabled) { - this.tooltipData = { - ...algorithm, - description: `${algorithm.description || 'No description available.'}<br><span class='unavailable-warning'> This algorithm is currently unavailable for the selected variables.</span><br>`, - }; - } } hideTooltip() { - this.tooltipVisible = false; - } - - hasText(value: any): boolean { - if (value === null || value === undefined) return false; - const text = String(value).trim(); - return text.length > 0; - } - - private normalizeBool(value: boolean | string | undefined | null): boolean | null { - if (value === undefined || value === null) return null; - if (typeof value === 'boolean') return value; - const normalized = String(value).trim().toLowerCase(); - if (normalized === 'true') return true; - if (normalized === 'false') return false; - return null; + this.tooltipVisible.set(false); } - getRoleRequirement(field: any, label: string): string | null { - if (!field) return null; - - const isRequired = this.normalizeBool(field.required) === true; - const multiple = this.normalizeBool(field.multiple); - - let count = 'optional'; - if (multiple === false) { - count = isRequired ? 'exactly 1' : '0–1'; - } else if (isRequired) { - count = '1+'; + private getTooltipPosition( + rect: DOMRect, + containerRect: DOMRect, + tooltipWidth: number, + tooltipHeight: number, + viewportWidth: number, + viewportHeight: number, + padding: number, + offset: number, + ): { x: number; y: number } { + const rightAlignedX = rect.right + offset; + const leftAlignedX = rect.left - tooltipWidth - offset; + const centeredY = rect.top + (rect.height - tooltipHeight) / 2; + + let x = rightAlignedX; + if (rightAlignedX + tooltipWidth > viewportWidth - padding) { + x = leftAlignedX >= padding + ? leftAlignedX + : viewportWidth - tooltipWidth - padding; } - const types = Array.isArray(field.types) && field.types.length - ? field.types.join(',') - : null; - - const parts = [`${label}: ${count}`]; - if (types) { - parts.push(`types: ${types}`); + let y = centeredY; + if (y + tooltipHeight > viewportHeight - padding) { + y = viewportHeight - tooltipHeight - padding; + } + if (y < padding) { + y = padding; } - return parts.join(' • '); - } - - getVariableRequirement(algo?: any): string | null { - const target = algo || this.tooltipData; - const override = this.experimentStudioService.getAlgorithmRequirementOverrides(target); - if (override?.y) return override.y; - return this.getRoleRequirement(target?.inputdata?.y, 'Variable'); + return { + x: Math.max(padding, x - containerRect.left), + y: Math.max(padding, y - containerRect.top), + }; } - getCovariateRequirement(algo?: any): string | null { - const target = algo || this.tooltipData; - const override = this.experimentStudioService.getAlgorithmRequirementOverrides(target); - if (override?.x) { - if (/:\s*none$/i.test(override.x)) { - return null; - } - return override.x; + private normalizeRequirementDisplayType(type: unknown): string | null { + const normalized = String(type ?? '').trim().toLowerCase(); + if (!normalized) return null; + + switch (normalized) { + case VariableTypes.TEXT: + case 'polynominal': + case 'ordinal': + return VariableTypes.NOMINAL; + case VariableTypes.INTEGER: + return VariableTypes.INT; + default: + return normalized; } - return this.getRoleRequirement(target?.inputdata?.x, 'Covariate'); } - onSaveAs() { - if (!this.saveAsName().trim()) { - this.errorMsg.set('Please provide a name for the experiment.'); - return; + private formatRequirementTypes(types: unknown): string[] | null { + if (!Array.isArray(types) || types.length === 0) { + return null; } - this.errorMsg.set(null); - this.errorService.clearError(); - - const algo = this.experimentStudioService.selectedAlgorithm(); - if (!algo) return; + const normalized = types + .map((type) => this.normalizeRequirementDisplayType(type)) + .filter((type): type is string => !!type); - this.experimentStudioService.setRunning(true); - - const isCvOnly = this.experimentStudioService.isCrossValidationOnly(algo.name); - const baseAlgorithmName = isCvOnly - ? algo.name - : this.experimentStudioService.getCrossValidationBase(algo.name) ?? - this.experimentStudioService.getTransformationBase(algo.name) ?? - algo.name; - - const cvVariant = this.experimentStudioService.getCrossValidationVariant(baseAlgorithmName); - const shouldIncludeSplits = this.crossValidationEnabled() || this.experimentStudioService.isCrossValidationOnly(algo.name); - const useCrossValidation = shouldIncludeSplits && !!cvVariant; - const transformationVariant = this.experimentStudioService.getTransformationVariant(baseAlgorithmName); - const useTransformation = this.transformationEnabled() && !!transformationVariant; - const effectiveAlgorithmName = useCrossValidation - ? cvVariant - : useTransformation - ? transformationVariant - : baseAlgorithmName; - - const finalAlgorithmName = isCvOnly ? algo.name : effectiveAlgorithmName; - - const result$ = this.experimentStudioService.runSelectedAlgorithm( - baseAlgorithmName, - finalAlgorithmName, - this.saveAsName() - ); - - this.loadingText.set('Saving experiment...'); + const unique = Array.from(new Set(normalized)); + return unique.length ? unique : null; + } - if (!result$) { - this.errorMsg.set('Unable to start the save process.'); - this.experimentStudioService.setRunning(false); + onExportResult(section: HTMLElement) { + const payload = this.resultExportPayload(); + if (!section || !payload) { + console.warn('No result or element to export'); return; } - - result$.subscribe({ - next: (res) => { - const status = res?.status; - const payload = res?.result ?? {}; - - if (status === 'error') { - const msg = payload?.message || 'Failed to save experiment results.'; - this.errorMsg.set(msg); - return; - } - - this.saveAsMode.set(false); - this.saveAsName.set(''); - this.result.set(null); // Return to parameters view - this.triggerSuccessNotification(); - }, - error: () => { - this.errorMsg.set('Failed to save experiment.'); - }, - complete: () => { - this.experimentStudioService.setRunning(false); - } - }); - } - - cancelSaveAs() { - this.saveAsMode.set(false); - this.saveAsName.set(''); + this.pdfExport.exportExperimentPdf({ ...payload, chartContainer: section }); } - toggleSaveAsMode() { - this.saveAsMode.update(v => !v); - if (this.saveAsMode()) { - this.saveAsName.set(this.experimentInfo().experimentName); + onExportResultZip() { + const payload = this.resultExportPayload(); + if (!payload) { + console.warn('No result to export'); + return; } - } - - backToParameters() { - this.result.set(null); - this.saveAsMode.set(false); - this.saveAsName.set(''); - } - - isCategoryOpen(category: string): boolean { - return this.openCategories().includes(category); - } - - toggleCategory(category: string): void { - this.openCategories.update(current => { - if (current.includes(category)) { - return current.filter(c => c !== category); - } else { - return [...current, category]; - } + this.csvExport.exportExperimentZip({ + ...payload, + tables: experimentTables(payload.algorithmKey, payload.result), + }).catch((error) => { + console.error('Result ZIP export failed:', error); + this.errorMsg.set('Failed to export the result tables.'); }); } - onExportResult(section: HTMLElement) { - const result = this.result(); - if (!section || !result) { - console.warn('No result or element to export'); - return; - } + /** The run's setup and result, shared by the PDF report and the ZIP export. */ + private resultExportPayload() { + const result = this.experimentStudioService.runResult(); + if (!result) return null; const info = this.experimentInfo(); const algoKey = @@ -998,48 +1667,37 @@ export class AlgorithmPanelComponent { const createdBy = currentUser?.fullname || currentUser?.username || currentUser?.email || null; - const filename = info.experimentName; - - const transformations = this.transformationEnabled() - ? Object.entries(this.transformationAssignments) - .filter(([_, choice]) => choice && choice !== 'none') - .map(([code, choice]) => { - const label = this.labelMap()[code] || code; - return `${label}: ${choice}`; - }) - .join(', ') - : null; + const filterLogic = this.experimentStudioService.filterLogic(); + const enumMaps = this.experimentStudioService.getCategoricalEnumMaps(); + const filterExpression = filterLogic + ? formatFilterExpression(filterLogic, { labelMap: this.labelMap(), enumMaps }) + : ''; + const model = this.experimentStudioService.selectedDataModel(); - this.pdfExport.exportExperimentPdf({ - filename, + return { + filename: info.experimentName, details: { experimentName: info.experimentName, createdBy, createdAt: new Date(), algorithm: algoLabel, params: info.algorithmConfigs, - preprocessing: 'none', - domain: this.experimentStudioService.selectedDataModel()?.code ?? null, - datasets: (info.datasets ?? []).map(code => { - const ds = this.experimentStudioService.availableDatasets().find(d => d.code === code); - return ds?.label || code; - }), + preprocessing: this.experimentStudioService + .getEffectivePreprocessingEntries(algoKey, this.labelMap(), enumMaps) + .map((entry) => `${entry.label}: ${entry.value}`) + .join('\n'), + domain: model ? [model.label || model.code, model.version].filter(Boolean).join(' ') : null, + datasets: this.datasetsWithLabels().map((d) => d.label), variables: (info.variables ?? []).map((v: any) => v.label || v.name || v.code), covariates: (info.covariates ?? []).map((c: any) => c.label || c.name || c.code), - filters: (info.filters ?? []).map((f: any) => f.label || f.name || f.code), - transformations, + filters: filterExpression + ? [filterExpression] + : (info.filters ?? []).map((f: any) => f.label || f.name || f.code), + transformations: null, mipVersion: this.mipVersion, }, algorithmKey: algoKey, result, - chartContainer: section, - }); - } - - private triggerSuccessNotification() { - this.showSuccessNotification.set(true); - setTimeout(() => { - this.showSuccessNotification.set(false); - }, 4000); + }; } } diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.css b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.css index 2b001eacb..c09e05e7b 100644 --- a/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.css +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.css @@ -2,71 +2,104 @@ :host { display: block; - font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", - sans-serif; + font-family: var(--font-sans); font-size: 0.875rem; color: #1f2937; padding: 0.75rem 0; } -/* GENERIC COLLAPSIBLE PANELS */ +:host-context(.result-section-body) { + padding: 0; +} -.panel { - border: 1px solid #dde5e7; - border-radius: 12px; - margin-bottom: 1rem; - overflow: hidden; - background: #ffffff; - box-shadow: 0 2px 5px rgba(15, 23, 42, 0.06); - transition: box-shadow 0.18s ease, transform 0.12s ease; +.results-list { + padding: 0; + display: flex; + flex-direction: column; + gap: 1.5rem; } -.panel:hover { - box-shadow: 0 4px 10px rgba(15, 23, 42, 0.09); - transform: translateY(-1px); +.results-section-intro { + margin-bottom: 0.65rem; } -.panel-header { - background: linear-gradient(90deg, #f4fbfb, #e4f5f5); - padding: 0.75rem 1rem; - cursor: pointer; +.results-section-heading { + margin: 0; + font-size: 0.95rem; + font-weight: 700; + color: var(--text-main); + letter-spacing: 0.02em; +} + +/* The key figures and the note on how to read them, kept together in the list's gap. */ +.results-figures { display: flex; - justify-content: space-between; - align-items: center; + flex-direction: column; } -.panel-body { - padding: 0.9rem 1rem 1rem; - background: #ffffff; +.results-detail-section--with-chart { + padding-top: 0.25rem; +} + +/* Text button under the results, in the CSV link's style. */ +.tables-toggle { + align-self: flex-start; + padding: 6px 10px; + border: 0; + border-radius: 8px; + background: transparent; + color: var(--primary-color); + font: inherit; + font-size: 0.8rem; + font-weight: 600; + cursor: pointer; +} + +.tables-toggle:hover, +.tables-toggle:focus-visible { + background: #eef0fd; +} + +/* A visible focus ring, not only the hover tint (WCAG 2.4.7). */ +.tables-toggle:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +/* Every table already shown above: drop the empty section so it adds no gap. */ +.results-detail-section:has(> app-auto-renderer:empty) { + display: none; } /* CHARTS SECTION */ .gen-charts { - border-radius: 16px; - border: 1px solid #cdeaea; - background: radial-gradient( - circle at top left, - #ffffff 0, - #f3fcfc 55%, - #edf9f9 100% - ); + border-radius: 12px; + border: 1px solid rgba(148, 163, 184, 0.28); + background: #ffffff; padding: 16px 18px 18px; - box-shadow: 0 14px 34px rgba(15, 23, 42, 0.06), - 0 0 0 1px rgba(255, 255, 255, 0.9) inset; + box-shadow: none; } .gen-charts h3 { font-size: 0.98rem; - color: #026d6d; - font-weight: 600; + color: var(--text-main); + font-weight: 700; margin: 0 0 10px; - letter-spacing: 0.02em; + letter-spacing: 0; } .gen-charts app-chart-renderer { display: block; - margin-top: 4px; + margin-top: 2px; +} + +/* How to read the figures or charts above, in the regression card's note style. */ +.results-note { + margin: 10px 2px 0; + font-size: 0.76rem; + line-height: 1.5; + color: var(--text-muted); } /* ERROR MESSAGE */ @@ -121,8 +154,7 @@ td { padding: 0.5rem 0.85rem; border-bottom: 1px solid #e5ecec; white-space: nowrap; - font-family: "Roboto Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, - Consolas, "Liberation Mono", "Courier New", monospace; + font-family: var(--font-mono); font-size: 0.82rem; } diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.html b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.html index faed9e33c..9c2e18ec9 100644 --- a/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.html +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.html @@ -1,4 +1,4 @@ -<div class="p-4 space-y-6"> +<div class="results-list space-y-6" data-guide="results-list"> @if (errorMessage()) { <div class="algorithm-error"> @@ -7,26 +7,42 @@ </div> } - <!-- Custom structured renderer --> - @if (isRenderable()) { - <app-auto-renderer [value]="mappedResult()" [algorithm]="algorithm()" [labelMap]="labelMap()" [enumMaps]="enumMaps()" - [yVar]="yVar()" [fallbackTitle]="fallbackResultTitle()" /> + @if (regression(); as view) { + <app-regression-result [view]="view" /> + } @else { + @if (summaryFigures(); as figures) { + <div class="results-figures"> + <app-key-figures [figures]="figures" /> + @if (figureNote(); as note) { + <p class="results-note">{{ note }}</p> + } + </div> } - <!-- Charts --> @if (renderedCharts().length > 0) { - <section class="space-y-6 gen-charts"> - <h3>Generated Charts</h3> + <section class="gen-charts" aria-labelledby="results-chart-heading"> + <header class="results-section-intro"> + <h3 id="results-chart-heading" class="results-section-heading">{{ chartTitle() }}</h3> + </header> <app-chart-renderer [charts]="renderedCharts()" /> + @if (chartNote(); as note) { + <p class="results-note">{{ note }}</p> + } </section> } - <!-- Raw fallback --> - <!-- <details class="text-xs text-gray-500 mt-4"> - <summary class="cursor-pointer">View Raw JSON</summary> - <pre class="bg-gray-100 rounded p-2 overflow-auto"> - {{ result() | json }} - </pre> - </details> --> + @if (isRenderable()) { + <section class="results-detail-section" [class.results-detail-section--with-chart]="hasSummaryAbove()"> + <app-auto-renderer [value]="mappedResult()" [algorithm]="algorithm()" [labelMap]="labelMap()" [enumMaps]="enumMaps()" + [yVar]="yVar()" [xVar]="xVar()" [fallbackTitle]="fallbackResultTitle()" [covered]="tablesToCover()" + [heading]="hasSummaryAbove() ? 'Detailed results' : null" /> + </section> + } + @if (coveredTables().length) { + <button type="button" class="tables-toggle" (click)="showAllTables.set(!showAllTables())"> + {{ showAllTables() ? 'Hide tables repeated above' : 'Show all values as tables' }} + </button> + } + } </div> diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.spec.ts b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.spec.ts new file mode 100644 index 000000000..7fb12d994 --- /dev/null +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.spec.ts @@ -0,0 +1,216 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideEchartsCore } from 'ngx-echarts'; +import { AlgorithmResultComponent } from './algorithm-result.component'; + +describe('AlgorithmResultComponent linear-regression review layout', () => { + let fixture: ComponentFixture<AlgorithmResultComponent>; + + const linearResult = { + dependent_var: 'MMSE', + n_obs: 1284, + df_model: 2, + df_resid: 1281, + rse: 3.94, + r_squared: 0.412, + r_squared_adjusted: 0.4111, + f_stat: 448.72, + f_pvalue: 0.00001, + ll: -3581.2, + aic: 7168.4, + bic: 7183.9, + indep_vars: ['Intercept', 'Age', 'Left hippocampus'], + coefficients: [14.212, -0.031, 4.318], + std_err: [1.8041, 0.0151, 0.291], + t_stats: [7.8776, -2.053, 14.8385], + pvalues: [0.00001, 0.0403, 0.00001], + lower_ci: [10.6726, -0.0606, 3.7471], + upper_ci: [17.7514, -0.0014, 4.8889], + }; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [AlgorithmResultComponent], + providers: [provideZonelessChangeDetection(), provideEchartsCore({ echarts: () => import('echarts') })], + }).compileComponents(); + + fixture = TestBed.createComponent(AlgorithmResultComponent); + }); + + function render(result: Record<string, unknown>, algorithm = 'linear_regression'): HTMLElement { + fixture.componentRef.setInput('result', result); + fixture.componentRef.setInput('algorithm', algorithm); + fixture.detectChanges(); + return fixture.nativeElement as HTMLElement; + } + + it('renders key figures, sorted coefficients and interval plots for linear regression', () => { + const html = render(linearResult); + + expect(html.querySelector('.lr-result')).toBeTruthy(); + expect(html.querySelectorAll('.key-figure').length).toBe(5); + expect(html.querySelector('.coefficients-title')?.textContent?.trim()).toBe('Coefficients'); + expect(html.querySelector('.coef-row--head')?.textContent).toContain('Interval vs 0'); + expect(html.querySelectorAll('.coef-row').length).toBe(4); // header + 3 rows + + const variables = Array.from(html.querySelectorAll('.coef-row:not(.coef-row--head) .coef-variable')) + .map((el) => el.textContent?.trim()); + expect(variables).toEqual(['Left hippocampus', 'Age', 'Intercept']); + // Only the two non-intercept rows draw the interval-vs-zero plot. + expect(html.querySelectorAll('.coef-plot').length).toBe(2); + expect(html.querySelector('.coef-row.is-intercept')?.textContent).toContain('Intercept'); + }); + + it('expands to all coefficient columns and model-fit details on demand', () => { + const html = render(linearResult); + + const allColumns = Array.from(html.querySelectorAll<HTMLButtonElement>('.coef-columns button')) + .find((button) => button.textContent?.includes('All columns'))!; + allColumns.click(); + fixture.detectChanges(); + expect(html.querySelector('.coef-row--head')?.textContent).toContain('Std. error'); + expect(html.querySelector('.coef-row--head')?.textContent).toContain('t'); + + const fitToggle = html.querySelector<HTMLButtonElement>('.model-fit-summary')!; + expect(fitToggle.textContent).toContain('AIC'); + expect(html.querySelector('.model-fit-grid')).toBeFalsy(); + fitToggle.click(); + fixture.detectChanges(); + expect(html.querySelector('.model-fit-grid')?.textContent).toContain('Log-likelihood'); + }); + + it('shows the logistic log-odds estimate, SE, z and p as key columns and the odds ratio with all columns', () => { + const html = render({ + dependent_var: 'Good outcome', + indep_vars: ['Intercept', 'Age'], + summary: { + coefficients: [7.06, -0.063], stderr: [0.13, 0.0016], z_scores: [54.8, -39.5], pvalues: [0, 0], + lower_ci: [6.81, -0.066], upper_ci: [7.31, -0.06], n_obs: 27308, + }, + }, 'logistic_regression'); + const headers = () => Array.from(html.querySelectorAll('.coef-row--head [role="columnheader"]')).map((el) => el.textContent?.trim()); + + expect(headers()).toEqual(['Variable', 'Estimate', 'Std. error', 'z', 'p-value']); + expect(html.querySelector('.coef-row:not(.coef-row--head) .coef-estimate')?.textContent).toBe('−0.063'); + + Array.from(html.querySelectorAll<HTMLButtonElement>('.coef-columns button')).find((b) => b.textContent?.includes('All columns'))!.click(); + fixture.detectChanges(); + expect(headers()).toEqual(['Variable', 'Estimate', 'Std. error', 'z', 'Odds ratio', '95% CI', 'Interval vs 1', 'p-value']); + }); + + it('explains the figures and charts that need it under them', () => { + const cv = render({ dependent_var: 'MMSE', root_mean_sq_error: [3.1, 0.2], mean_abs_error: [2.4, 0.1], n_obs: [100, 101] }, 'linear_regression_cv'); + expect(cv.querySelector('.results-note')?.textContent).toContain('RMSE (root mean squared error)'); + + const nb = render({ + classes: ['no', 'yes'], feature_names: ['Age'], theta: [[78], [69]], var: [[131], [186]], class_count: [37366, 102452], + }, 'naive_bayes_gaussian'); + expect(nb.querySelector('.gen-charts .results-note')?.textContent).toContain('standard deviations'); + }); + + it('keeps the generic renderer for non-linear-regression algorithms', () => { + const html = render({ title: 'Some other result' }, 'kmeans'); + + expect(html.querySelector('.lr-result')).toBeFalsy(); + expect(html.querySelector('app-auto-renderer')).toBeTruthy(); + }); + + it('does not repeat a table whose values the key figures already show', () => { + const html = render({ t_stat: -3.9, df: 120, p: 0.0001, mean_diff: -1.2, se_diff: 0.3, ci_lower: -1.8, ci_upper: -0.6, cohens_d: -0.7 }, 'ttest_independent'); + + expect(html.querySelector('app-key-figures')).toBeTruthy(); + expect(html.querySelector('.gen-charts .results-section-heading')?.textContent).toBe('Mean difference'); + expect(html.querySelectorAll('.ar-card').length).toBe(0); + expect(html.querySelector('.ar-heading')).toBeFalsy(); + }); + + it('brings the covered tables back on request', () => { + const html = render({ t_stat: -3.9, df: 120, p: 0.0001, mean_diff: -1.2, se_diff: 0.3, ci_lower: -1.8, ci_upper: -0.6, cohens_d: -0.7 }, 'ttest_independent'); + const toggle = html.querySelector<HTMLButtonElement>('.tables-toggle')!; + + expect(toggle.textContent?.trim()).toBe('Show all values as tables'); + // The label says what the button does next; a pressed state on top would contradict it. + expect(toggle.hasAttribute('aria-pressed')).toBe(false); + toggle.click(); + fixture.detectChanges(); + expect(html.querySelectorAll('.ar-card').length).toBe(1); + expect(toggle.textContent?.trim()).toBe('Hide tables repeated above'); + }); + + const cardTitles = (html: HTMLElement) => Array.from(html.querySelectorAll('.ar-card .ar-title')).map((el) => el.textContent?.trim()); + const rowLabels = (html: HTMLElement, title: string) => { + const card = Array.from(html.querySelectorAll('.ar-card')).find((el) => el.querySelector('.ar-title')?.textContent?.trim() === title); + return Array.from(card?.querySelectorAll('tbody tr') ?? []).map((row) => row.querySelector('td')?.textContent?.trim()); + }; + + it('summarises a one-sided paired t-test with its bound, level and measurement order from the run', () => { + fixture.componentRef.setInput('parameters', { alt_hypothesis: 'greater', alpha: 0.1 }); + fixture.componentRef.setInput('xVar', 'rightorifg'); + fixture.componentRef.setInput('yVar', 'rightppplanumpolare'); + fixture.componentRef.setInput('labelMap', { rightorifg: 'Right orbital IFG', rightppplanumpolare: 'Right planum polare' }); + const html = render({ t_stat: -39.8, df: 216, p: 1, mean_diff: -0.502, se_diff: 0.0126, ci_lower: -0.5184, ci_upper: 'Infinity', cohens_d: -2.7 }, 'ttest_paired'); + + const notes = Array.from(html.querySelectorAll('.key-figure-note')).map((el) => el.textContent?.trim()); + expect(notes).toContain('Right orbital IFG − Right planum polare · 90% one-sided CI ≥ −0.518 · SE 0.013'); + }); + + it('keeps the t-test rows the key figures do not print', () => { + const html = render({ t_stat: -3.9, df: 120, p: 0.0001, mean_diff: -1.2, ci_lower: -1.8, cohens_d: -0.7, mean_x: 4.2 }, 'ttest_independent'); + + // One CI bound alone is not in the mean-difference figure, and mean_x has no figure. + expect(rowLabels(html, 'Independent T-Test')).toEqual(['CI Lower', 'Mean X']); + }); + + it('keeps the F diagnostic and undefined metrics under the linear regression CV figures', () => { + const html = render({ + dependent_var: 'y', + n_obs: [100, 101], + root_mean_sq_error: [1.2, 0.1], + r_squared: [null, null], + mean_abs_error: [0.7, 0.05], + f_stat: [12.3, 1.1], + }, 'linear_regression_cv'); + + expect(html.querySelectorAll('.key-figure').length).toBeGreaterThan(0); + expect(rowLabels(html, 'Error metrics')).toEqual(['R-squared', 'F diagnostic']); + }); + + describe('tables a chart replaces', () => { + const cluster = (id: string, center: Record<string, number>) => ({ + cluster_id: id, label: `Cluster ${id}`, size_interval: '10-20', center, profile: [], quality: { compactness: null }, + }); + + it('hides the k-means centres table under the heatmap, which prints every centre', () => { + const html = render({ + variables: ['a', 'b', 'c'], + clusters: [cluster('0', { a: 1, b: 2, c: 3 }), cluster('1', { a: 3, b: 4, c: 5 })], + }, 'kmeans'); + + expect(html.querySelector('.gen-charts')).toBeTruthy(); + expect(cardTitles(html)).not.toContain('Cluster Centers'); + }); + + it('keeps the k-means centres table under the two-variable bubble chart, which only places them', () => { + const html = render({ + variables: ['a', 'b'], + clusters: [cluster('0', { a: 1, b: 2 }), cluster('1', { a: 3, b: 4 })], + }, 'kmeans'); + + expect(html.querySelector('.gen-charts')).toBeTruthy(); + expect(cardTitles(html)).toContain('Cluster Centers'); + }); + + it('keeps the naive Bayes class summary when only the profile chart rendered', () => { + const html = render({ + classes: ['A', 'B'], + class_count: [40, 60], + feature_names: ['x'], + theta: [[1], [2]], + var: [[1], [1]], + }, 'naive_bayes_gaussian'); + + expect(html.querySelector('.gen-charts')).toBeTruthy(); + expect(cardTitles(html)).toContain('Class Summary'); + }); + }); +}); diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.ts b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.ts index 2a0820cf2..0809a2577 100644 --- a/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.ts +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component.ts @@ -1,20 +1,27 @@ import { ChartBuilderService } from './../../visualisations/charts/chart-builder.service'; -import { Component, input, signal, computed, inject, ChangeDetectionStrategy } from '@angular/core'; +import { Component, input, computed, inject, signal, ChangeDetectionStrategy } from '@angular/core'; import { CommonModule } from '@angular/common'; import { AutoRendererComponent } from '../../visualisations/auto-renderer/auto-renderer.component'; import { ChartRendererComponent } from '../../visualisations/charts/charts-renderer/charts-renderer.component'; -import { EChartsOption } from 'echarts'; +import { AlgorithmChartRegistry, tablesReplacedByCharts } from '../../visualisations/charts/chart-registry'; import { NgxEchartsModule } from 'ngx-echarts'; import { EnumMaps, LabelMap, mapAlgorithmResultEnums } from '../../../../core/algorithm-result-enum-mapper'; +import { prettifyLabel } from '../../../../core/algorithm-mappers'; +import { RegressionResultComponent } from '../../visualisations/regression/regression-result.component'; +import { buildRegressionView } from '../../visualisations/regression/regression-view'; +import { KeyFiguresComponent } from '../../visualisations/summary/key-figures.component'; +import { buildSummaryFigures, FIGURE_NOTES, SummaryContext, tablesShownAsFigures } from '../../visualisations/summary/summary-figures'; @Component({ selector: 'app-algorithm-result', imports: [CommonModule, AutoRendererComponent, ChartRendererComponent, + RegressionResultComponent, + KeyFiguresComponent, NgxEchartsModule], templateUrl: './algorithm-result.component.html', - styleUrls: ['./algorithm-result.component.css'], + styleUrl: './algorithm-result.component.css', changeDetection: ChangeDetectionStrategy.OnPush }) export class AlgorithmResultComponent { @@ -28,6 +35,43 @@ export class AlgorithmResultComponent { yVar = input<string | null>(null); xVar = input<string | null>(null); labelMap = input<LabelMap | null>(null); + /** The run's algorithm parameters; names the groups a two-group test compared. */ + parameters = input<Record<string, unknown> | null>(null); + + readonly regression = computed(() => + this.errorMessage() ? null : buildRegressionView(this.algorithm(), this.mappedResult(), this.yVar(), this.positiveClassLabel()) + ); + + /** The positive_class parameter of a binary model as the outcome's enumeration label. */ + private readonly positiveClassLabel = computed(() => { + const value = this.parameters()?.['positive_class']; + if (value === null || value === undefined || value === '') return null; + return this.enumMaps()?.[this.yVar() ?? '']?.[String(value)] ?? String(value); + }); + + readonly summaryFigures = computed(() => + this.errorMessage() ? null : buildSummaryFigures(this.algorithm(), this.mappedResult(), this.summaryContext()) + ); + + /** What the figures need from the run: groupA / groupB as the x variable's enumeration labels, + * x and y by label (a paired t-test's measurements 1 and 2), and the CI's alpha and alternative. */ + private readonly summaryContext = computed((): SummaryContext => { + const params = this.parameters(); + const enumMap = this.enumMaps()?.[this.xVar() ?? ''] ?? {}; + const label = (value: unknown) => (value === null || value === undefined || value === '' ? null : enumMap[String(value)] ?? String(value)); + const variableLabel = (code: string | null) => (code ? this.labelMap()?.[code] ?? code : null); + const rawAlpha = params?.['alpha']; + const alpha = rawAlpha === null || rawAlpha === undefined || rawAlpha === '' ? NaN : Number(rawAlpha); + const alternative = params?.['alt_hypothesis']; + return { + groupA: label(params?.['groupA']), + groupB: label(params?.['groupB']), + measurement1: variableLabel(this.xVar()), + measurement2: variableLabel(this.yVar()), + alpha: Number.isFinite(alpha) ? alpha : null, + alternative: typeof alternative === 'string' ? alternative : null, + }; + }); constructor() { } @@ -43,14 +87,17 @@ export class AlgorithmResultComponent { return !!this.result() && !!this.algorithm() && !this.errorMessage(); }); - mappedResult = computed(() => - this.errorMessage() - ? this.result() - : mapAlgorithmResultEnums(this.algorithm(), this.result(), this.enumMaps(), { - y: this.yVar(), - x: this.xVar(), - }, this.labelMap()) - ); + mappedResult = computed(() => { + if (this.errorMessage()) return this.result(); + const mapped = mapAlgorithmResultEnums(this.algorithm(), this.result(), this.enumMaps(), { + y: this.yVar(), + x: this.xVar(), + }, this.labelMap()); + // A binary tp/fp/fn/tn matrix names its classes after the positive class when the run says which. + const cm = mapped?.confusion_matrix; + const positive = this.positiveClassLabel(); + return positive && cm && cm.tp !== undefined ? { ...mapped, confusion_matrix: { ...cm, positive_label: positive } } : mapped; + }); fallbackResultTitle = computed(() => { const explicitTitle = this.mappedResult()?.title; @@ -61,67 +108,30 @@ export class AlgorithmResultComponent { const label = this.algorithmLabel()?.trim(); if (label) return `Result ${label}`; - return `Result ${this.prettifyAlgorithmName(this.algorithm())}`; + return `Result ${prettifyLabel(this.algorithm()) || 'Algorithm'}`; }); - chartOptions = computed<EChartsOption[]>(() => - this.errorMessage() - ? [] - : this.chartBuilder.getChartsForAlgorithm( - this.algorithm(), - this.mappedResult(), - this.fallbackResultTitle() - ) - ); - renderedCharts = computed(() => { - if (!this.result() || !this.algorithm() || this.errorMessage()) return []; - return this.chartBuilder.getChartsForAlgorithm(this.algorithm(), this.mappedResult(), this.fallbackResultTitle()); - }); + readonly chartTitle = computed(() => AlgorithmChartRegistry[this.algorithm()]?.title ?? 'Charts'); + readonly chartNote = computed(() => AlgorithmChartRegistry[this.algorithm()]?.note ?? null); + readonly figureNote = computed(() => FIGURE_NOTES[this.algorithm()] ?? null); - getMatrixRows(data: any): any[][] { - if (!Array.isArray(data)) return []; + /** What the figures or charts above already show of the tables, so it is not repeated below: + * only the values those figures print and the tables whose own chart rendered. */ + readonly coveredTables = computed(() => [ + ...(this.summaryFigures() ? tablesShownAsFigures(this.algorithm(), this.mappedResult(), this.summaryContext()) : []), + ...tablesReplacedByCharts(this.algorithm(), this.renderedCharts()), + ]); - if (data[0]?.values) { - return data.map((r: any) => r.values); - } - - if (Array.isArray(data[0])) { - return data; - } - - return []; - } - - expandedPanels = signal<Set<string>>(new Set()); - - getObjectKeys(obj: Record<string, any>): string[] { - return obj ? Object.keys(obj) : []; - } - - getKeys(obj: any): string[] { - return obj && typeof obj === 'object' ? Object.keys(obj) : []; - } + readonly hasSummaryAbove = computed(() => !!this.summaryFigures() || this.renderedCharts().length > 0); - isArray(value: any): boolean { - return Array.isArray(value); - } + /** The reader can ask for every value as a table again — what a chart shows is otherwise only pixels. */ + readonly showAllTables = signal(false); + readonly tablesToCover = computed(() => (this.showAllTables() ? [] : this.coveredTables())); - togglePanel(key: string) { - const current = this.expandedPanels(); - const updated = new Set(current); - updated.has(key) ? updated.delete(key) : updated.add(key); - this.expandedPanels.set(updated); - } - - isExpanded(key: string): boolean { - return this.expandedPanels().has(key); - } - - private prettifyAlgorithmName(name: string): string { - if (!name) return 'Algorithm'; - return name - .replace(/_/g, ' ') - .replace(/\b\w/g, (c) => c.toUpperCase()); - } + renderedCharts = computed(() => { + if (!this.result() || !this.algorithm() || this.errorMessage()) return []; + return this.chartBuilder.getChartsForAlgorithm(this.algorithm(), this.mappedResult(), this.fallbackResultTitle()); + }); } + diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.css b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.css new file mode 100644 index 000000000..ef7228df5 --- /dev/null +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.css @@ -0,0 +1,383 @@ +:host { + display: block; + width: 100%; + min-width: 0; + background: var(--studio-card-bg); +} + +.role-assignment { + display: flex; + flex-direction: column; + gap: 10px; + width: 100%; + min-width: 0; + padding: 10px 14px 14px; + box-sizing: border-box; + scroll-margin-top: calc( + var(--header-height, 64px) + var(--studio-sub-header-height, 44px) + var(--warning-banner-space, 0px) + 16px + ); +} + +.role-band { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px 14px; + min-width: 0; +} + +.step-kicker { + display: flex; + align-items: center; + gap: 8px; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config, 0.06em); + text-transform: uppercase; + color: var(--text-muted); +} + +.step-kicker-n { + display: inline-flex; + align-items: center; + justify-content: center; + width: 20px; + height: 20px; + border-radius: 50%; + box-shadow: inset 0 0 0 1.5px var(--studio-card-border); + font-size: 0.68rem; +} + +.role-step-kicker { + flex: 0 0 auto; + white-space: nowrap; +} + +.rails { + display: flex; + flex: 1 1 auto; + flex-wrap: wrap; + align-items: center; + gap: 8px 18px; + min-width: 0; +} + +.rail { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +} + +.rails.is-compact .rail { + display: contents; +} + +.rail-label { + flex: 0 0 auto; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.06em; + text-transform: uppercase; + color: var(--text-main); + white-space: nowrap; +} + +.rail-label em { + font-style: italic; + font-weight: 700; + text-transform: none; + letter-spacing: 0; + color: var(--primary-color); +} + +.rail--x .rail-label em { + color: var(--primary-dark); +} + +.rail-label span { + display: none; +} + +.chips { + display: flex; + flex-wrap: wrap; + gap: 6px; + align-items: center; + min-width: 0; +} + +.chip { + display: inline-flex; + align-items: center; + gap: 6px; + min-height: 28px; + border-radius: var(--radius-md, 8px); + padding: 4px 4px 4px 10px; + font-size: 0.8rem; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + color: var(--text-main); + cursor: grab; +} + +.chip:active { + cursor: grabbing; +} + +.chip.cdk-drag-preview { + box-shadow: 0 6px 16px rgba(15, 23, 42, 0.18); +} + +.cdk-drag-placeholder { + opacity: 0.35; +} + +.cdk-drag-animating { + transition: transform 0.2s ease; +} + +.chip--y { + background: #fff1d6; + border-color: #e9c27b; +} + +.chip--x { + background: #e8e8fa; + border-color: #c6c8f2; +} + +/* Missing from a selected dataset: grayed out, no role changes or drags; × still removes. */ +.chip.is-unavailable { + background: var(--unavailable-fill); + border-color: var(--unavailable-border); + border-style: dashed; + color: var(--unavailable-text); + cursor: not-allowed; +} + +.row.is-unavailable { + opacity: var(--unavailable-opacity); +} + +.row.is-unavailable .var-name { + color: var(--unavailable-text); +} + +.row.is-unavailable .role-tray button { + cursor: not-allowed; +} + +.chip-name { + font-weight: 700; + font-size: 0.8rem; +} + +.chip-type { + color: var(--text-muted); + font-size: 0.68rem; + font-weight: 400; + opacity: 0.72; +} + +.chip-x { + border: 0; + background: transparent; + color: var(--text-muted); + width: 22px; + height: 22px; + border-radius: var(--radius-sm, 4px); + line-height: 1; + padding: 0; + cursor: pointer; + font: inherit; + font-size: 0.85rem; +} + +.chip-x:hover, +.chip-x:focus-visible { + background: rgba(255, 255, 255, 0.7); + color: var(--text-main); + outline: none; +} + +.invite { + margin: 0; + font-size: 0.76rem; + line-height: 1.4; + color: var(--text-muted); + padding: 2px 0; +} + +.role-band-actions { + display: flex; + align-items: center; + margin-left: auto; +} + +.role-action { + display: inline-flex; + align-items: center; + justify-content: center; + min-height: 30px; + padding: 0 14px; + border-radius: 999px; + font: inherit; + font-size: 0.78rem; + font-weight: 600; + cursor: pointer; + white-space: nowrap; +} + +.role-action--add { + border: 1px solid rgba(127, 156, 232, 0.35); + background: #ffffff; + color: var(--primary-color); +} + +.role-action--add:hover, +.role-action--add:focus-visible { + background: color-mix(in srgb, var(--primary-color) 6%, white); + border-color: rgba(43, 51, 233, 0.45); + outline: none; +} + +.role-action--quiet { + border: 0; + background: transparent; + color: var(--text-muted); +} + +.role-action--quiet:hover, +.role-action--quiet:focus-visible { + color: var(--text-main); + background: color-mix(in srgb, var(--primary-color) 6%, white); + outline: none; +} + +.roster-tools { + display: block; + min-width: 0; +} + +.pool-search { + display: block; + width: 100%; + box-sizing: border-box; + padding: 8px 12px; + border-radius: var(--radius-md, 8px); + border: 1px solid rgba(148, 163, 184, 0.34); + background: var(--studio-card-bg); + color: var(--text-main); + font: inherit; + font-size: 0.85rem; + line-height: 1.4; +} + +.pool-search:focus { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px rgba(127, 156, 232, 0.16); +} + +.roster { + list-style: none; + margin: 0; + padding: 0; + max-height: calc(5 * 44px); + overflow-y: auto; + min-height: 0; +} + +.row { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 12px; + align-items: center; + min-height: 44px; + padding: 0 2px; + border-bottom: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); +} + +.row:last-child { + border-bottom: 0; +} + +.var-main { + display: flex; + flex-wrap: wrap; + align-items: baseline; + gap: 6px 8px; + min-width: 0; +} + +.var-name { + font-weight: 700; + font-size: 0.8rem; + color: var(--text-main); +} + +.var-type { + font-size: 0.68rem; + font-weight: 400; + color: var(--text-muted); + opacity: 0.72; +} + +.role-tray { + flex: 0 0 auto; +} + +.role-tray button { + min-width: 5.5rem; + padding: 0 10px; +} + +.role-tray button.active[data-role='y'] { + color: var(--primary-color); + font-weight: 700; +} + +.role-tray button.active[data-role='x'] { + color: var(--primary-dark); + font-weight: 700; +} + +.hint { + margin: 0; + font-size: 0.74rem; + line-height: 1.4; + color: var(--text-muted); +} + +.role-chip-created { + font-size: 0.58rem; + font-weight: 700; + color: inherit; + border: 1px solid currentColor; + border-radius: var(--radius-sm, 4px); + padding: 0 4px; + line-height: 1.4; +} + +.sr-only { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); +} + +@media (max-width: 720px) { + .role-band-actions { + margin-left: 0; + } + + .rails { + flex-basis: 100%; + order: 3; + } + + .role-tray button { + min-width: 4.5rem; + } +} diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.html b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.html new file mode 100644 index 000000000..e91da72c8 --- /dev/null +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.html @@ -0,0 +1,108 @@ +<section class="role-assignment" [class.is-expanded]="rolesExpanded()" aria-label="Assign variables and covariates" + data-guide="guide-role-assignment" tabindex="-1"> + <div class="role-band"> + <div class="step-kicker role-step-kicker"><span class="step-kicker-n">1</span> Roles</div> + + <div class="rails" [class.is-compact]="!rolesExpanded()"> + <div class="rail rail--y" [class.rail--empty]="y().length === 0"> + <div class="rail-label">Outcome <em>y</em><span>usually one</span></div> + <div class="chips" cdkDropList #yList="cdkDropList" cdkDropListOrientation="mixed" + [cdkDropListConnectedTo]="[xList]" (cdkDropListDropped)="onChipDrop($event, 'y')"> + @if (y().length === 0) { + <p class="invite">No outcome yet — add one below</p> + } + @for (node of y(); track node.code) { + <span class="chip chip--y" cdkDrag [cdkDragData]="node" + [cdkDragDisabled]="isUnavailable(node)" + [class.is-unavailable]="isUnavailable(node)" + [attr.aria-disabled]="isUnavailable(node) ? true : null" + [attr.title]="unavailableTooltip(node) || null"> + <span class="chip-name">{{ label(node) }}</span> + <span class="chip-type">{{ node.type }}</span> + @if (isCreated(node)) { + <span class="role-chip-created">created</span> + } + <button type="button" class="chip-x" (click)="removeFrom('y', node)" + [attr.aria-label]="'Remove ' + label(node) + ' from outcome'">×</button> + </span> + } + </div> + </div> + + <div class="rail rail--x" [class.rail--empty]="x().length === 0"> + <div class="rail-label">Predictors <em>x</em><span>zero to many</span></div> + <div class="chips" cdkDropList #xList="cdkDropList" cdkDropListOrientation="mixed" + [cdkDropListConnectedTo]="[yList]" (cdkDropListDropped)="onChipDrop($event, 'x')"> + @if (x().length === 0) { + <p class="invite">No predictors yet — add from the list or leave empty</p> + } + @for (node of x(); track node.code) { + <span class="chip chip--x" cdkDrag [cdkDragData]="node" + [cdkDragDisabled]="isUnavailable(node)" + [class.is-unavailable]="isUnavailable(node)" + [attr.aria-disabled]="isUnavailable(node) ? true : null" + [attr.title]="unavailableTooltip(node) || null"> + <span class="chip-name">{{ label(node) }}</span> + <span class="chip-type">{{ node.type }}</span> + @if (isCreated(node)) { + <span class="role-chip-created">created</span> + } + <button type="button" class="chip-x" (click)="removeFrom('x', node)" + [attr.aria-label]="'Remove ' + label(node) + ' from predictors'">×</button> + </span> + } + </div> + </div> + </div> + + <div class="role-band-actions"> + @if (rolesExpanded()) { + <button type="button" class="role-action role-action--quiet" (click)="collapseRoles()">Done</button> + } @else { + <button type="button" class="role-action role-action--add" (click)="expandRoles()">+ Add</button> + } + </div> + </div> + + @if (rolesExpanded()) { + <div class="roster-tools"> + <label class="sr-only" for="pool-filter">Filter variables</label> + <input class="pool-search" id="pool-filter" type="search" placeholder="Filter by name or type" autocomplete="off" + [value]="poolFilter()" (input)="onFilter($event)"> + </div> + + @if (filteredPool().length > 0) { + <ul class="roster" aria-label="Unassigned variables"> + @for (node of filteredPool(); track node.code) { + @let unavailable = unavailableTooltip(node); + <li class="row" [class.is-unavailable]="!!unavailable" [attr.aria-disabled]="unavailable ? true : null" + [attr.title]="unavailable || null"> + <div class="var-main"> + <span class="var-name">{{ label(node) }}</span> + <span class="var-type">{{ node.type }}</span> + @if (isCreated(node)) { + <span class="role-chip-created">created</span> + } + </div> + <div class="roles modal-tabs role-tray" role="radiogroup" [attr.aria-label]="'Role for ' + label(node)" + [attr.aria-disabled]="unavailable ? true : null" + [title]="unavailable || 'Click Outcome or Predictor on a row. Drag a chip between the slots to move it; × returns it to this list.'"> + <button type="button" role="radio" data-role="y" [class.active]="roleOf(node) === 'y'" + [attr.aria-checked]="roleOf(node) === 'y'" [attr.aria-disabled]="unavailable ? true : null" + (click)="setRole(node, 'y')"> + <span class="role-btn-label">Outcome</span> + </button> + <button type="button" role="radio" data-role="x" [class.active]="roleOf(node) === 'x'" + [attr.aria-checked]="roleOf(node) === 'x'" [attr.aria-disabled]="unavailable ? true : null" + (click)="setRole(node, 'x')"> + <span class="role-btn-label">Predictor</span> + </button> + </div> + </li> + } + </ul> + } @else { + <p class="hint">Nothing left to assign — clear the filter, or move a chip back with its ×.</p> + } + } +</section> diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.spec.ts b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.spec.ts new file mode 100644 index 000000000..19512e979 --- /dev/null +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.spec.ts @@ -0,0 +1,298 @@ +import type { Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { AlgorithmRoleAssignmentComponent } from './algorithm-role-assignment.component'; + +describe('AlgorithmRoleAssignmentComponent', () => { + let fixture: ComponentFixture<AlgorithmRoleAssignmentComponent>; + let experimentStudioService: { + algorithmAssignableVariables: ReturnType<typeof signal<any[]>>; + algorithmY: ReturnType<typeof signal<any[]>>; + algorithmX: ReturnType<typeof signal<any[]>>; + setAlgorithmY: Mock; + setAlgorithmX: Mock; + unavailableVariables: ReturnType<typeof signal<ReadonlyMap<string, readonly string[]>>>; + }; + + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + const bmi = { code: 'bmi', label: 'BMI', type: 'continuous' }; + const derived = { code: 'derived_col', label: 'Derived column', type: 'continuous', isCreatedColumn: true }; + + beforeEach(async () => { + experimentStudioService = { + algorithmAssignableVariables: signal<any[]>([age, sex, bmi, derived]), + algorithmY: signal<any[]>([]), + algorithmX: signal<any[]>([]), + unavailableVariables: signal<ReadonlyMap<string, readonly string[]>>(new Map()), + setAlgorithmY: vi.fn().mockImplementation((nodes: any[]) => { + experimentStudioService.algorithmY.set(nodes); + }), + setAlgorithmX: vi.fn().mockImplementation((nodes: any[]) => { + experimentStudioService.algorithmX.set(nodes); + }), + }; + + await TestBed.configureTestingModule({ + imports: [AlgorithmRoleAssignmentComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: experimentStudioService }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(AlgorithmRoleAssignmentComponent); + fixture.detectChanges(); + }); + + it('assigns a pool member to y via setAlgorithmY', () => { + fixture.componentInstance.assignTo('y', age); + + expect(experimentStudioService.setAlgorithmY).toHaveBeenCalledWith([age]); + expect(experimentStudioService.algorithmY()).toEqual([age]); + // The assigned member leaves the available source list. + expect(fixture.componentInstance.source()).not.toContainEqual(expect.objectContaining({ code: 'age' })); + }); + + it('assigns a pool member to x via setAlgorithmX', () => { + fixture.componentInstance.assignTo('x', sex); + + expect(experimentStudioService.setAlgorithmX).toHaveBeenCalledWith([sex]); + expect(experimentStudioService.algorithmX()).toEqual([sex]); + expect(fixture.componentInstance.source()).not.toContainEqual(expect.objectContaining({ code: 'sex' })); + }); + + it('removes a member from a role via setAlgorithmY/setAlgorithmX', () => { + experimentStudioService.algorithmY.set([age, bmi]); + experimentStudioService.algorithmX.set([sex]); + fixture.detectChanges(); + + fixture.componentInstance.removeFrom('y', age); + expect(experimentStudioService.setAlgorithmY).toHaveBeenCalledWith([bmi]); + expect(experimentStudioService.algorithmY()).toEqual([bmi]); + + fixture.componentInstance.removeFrom('x', sex); + expect(experimentStudioService.setAlgorithmX).toHaveBeenCalledWith([]); + expect(experimentStudioService.algorithmX()).toEqual([]); + }); + + it('renders a created badge for nodes flagged as created columns', () => { + experimentStudioService.algorithmY.set([derived]); + fixture.detectChanges(); + + expect(fixture.componentInstance.isCreated(derived)).toBe(true); + expect(fixture.componentInstance.isCreated(age)).toBe(false); + const nativeElement = fixture.nativeElement as HTMLElement; + expect(nativeElement.textContent).toContain('Derived column'); + expect(nativeElement.querySelector('.role-chip-created')).toBeTruthy(); + }); + + it('assigning to y removes the node from x (disjoint roles)', () => { + experimentStudioService.algorithmX.set([age]); + fixture.detectChanges(); + + fixture.componentInstance.assignTo('y', age); + + expect(experimentStudioService.setAlgorithmX).toHaveBeenCalledWith([]); + expect(experimentStudioService.setAlgorithmY).toHaveBeenCalledWith([age]); + expect(experimentStudioService.algorithmX()).toEqual([]); + expect(experimentStudioService.algorithmY()).toEqual([age]); + // The node is not in both roles. + expect(fixture.componentInstance.isAssigned(age, 'x')).toBe(false); + expect(fixture.componentInstance.isAssigned(age, 'y')).toBe(true); + }); + + it('assigning to x removes the node from y (disjoint roles)', () => { + experimentStudioService.algorithmY.set([bmi]); + fixture.detectChanges(); + + fixture.componentInstance.assignTo('x', bmi); + + expect(experimentStudioService.setAlgorithmY).toHaveBeenCalledWith([]); + expect(experimentStudioService.setAlgorithmX).toHaveBeenCalledWith([bmi]); + expect(experimentStudioService.algorithmY()).toEqual([]); + expect(experimentStudioService.algorithmX()).toEqual([bmi]); + expect(fixture.componentInstance.isAssigned(bmi, 'y')).toBe(false); + expect(fixture.componentInstance.isAssigned(bmi, 'x')).toBe(true); + }); + + it('assigning an already-assigned node is a no-op and keeps it out of the other role', () => { + experimentStudioService.algorithmY.set([sex]); + fixture.detectChanges(); + experimentStudioService.setAlgorithmY.mockClear(); + experimentStudioService.setAlgorithmX.mockClear(); + + fixture.componentInstance.assignTo('y', sex); + + expect(experimentStudioService.setAlgorithmY).not.toHaveBeenCalled(); + expect(experimentStudioService.setAlgorithmX).not.toHaveBeenCalled(); + expect(experimentStudioService.algorithmY()).toEqual([sex]); + expect(experimentStudioService.algorithmX()).toEqual([]); + }); + + it('hides assigned variables from the pool roster', () => { + experimentStudioService.algorithmY.set([age]); + fixture.detectChanges(); + + expect(fixture.componentInstance.filteredPool().some((node) => node.code === 'age')).toBe(false); + expect(fixture.componentInstance.roleOf(age)).toBe('y'); + expect((fixture.nativeElement as HTMLElement).querySelector('.roster')?.textContent).not.toContain('Age'); + }); + + it('drops a chip dragged into the other role slot', () => { + experimentStudioService.algorithmX.set([age]); + fixture.detectChanges(); + + const previous = {} as any; + // Move age from predictors to outcome. + fixture.componentInstance.onChipDrop( + { previousContainer: previous, container: {}, item: { data: age } } as any, + 'y', + ); + expect(experimentStudioService.algorithmX()).toEqual([]); + expect(experimentStudioService.algorithmY()).toEqual([age]); + + // Releasing inside its own slot changes nothing. + fixture.componentInstance.onChipDrop( + { previousContainer: previous, container: previous, item: { data: age } } as any, + 'y', + ); + expect(experimentStudioService.algorithmY()).toEqual([age]); + }); + + it('labels the role controls in plain language and still assigns', () => { + const buttons = (fixture.nativeElement as HTMLElement).querySelectorAll<HTMLElement>('.roster .roles button'); + expect(buttons.length).toBe(8); // 4 pool rows x 2 roles + expect(buttons[0].textContent).toContain('Outcome'); + expect(buttons[1].textContent).toContain('Predictor'); + buttons[0].click(); // first roster row is age; assign it as outcome + expect(experimentStudioService.algorithmY()).toEqual([age]); + }); + + it('renders the role band as a horizontal strip above the full-width roster', () => { + experimentStudioService.algorithmY.set([age]); + experimentStudioService.algorithmX.set([sex, bmi]); + fixture.detectChanges(); + + const root = fixture.nativeElement as HTMLElement; + const band = root.querySelector<HTMLElement>('.role-band')!; + const rails = root.querySelector<HTMLElement>('.rails')!; + const roster = root.querySelector<HTMLElement>('.roster')!; + const outcome = rails.querySelector<HTMLElement>('.rail--y')!; + const predictor = rails.querySelector<HTMLElement>('.rail--x')!; + + expect(getComputedStyle(rails).display).toBe('flex'); + expect(band.textContent).toContain('Roles'); + expect(band.textContent).toContain('Outcome'); + expect(band.textContent).toContain('Predictors'); + expect(root.querySelector('.role-action--quiet')?.textContent).toContain('Done'); + // The roster is a sibling below the rails, not a side column. + expect(roster.getBoundingClientRect().top).toBeGreaterThanOrEqual( + rails.getBoundingClientRect().bottom - 1, + ); + // Outcome and predictor sit on the same band row. + expect(Math.abs(outcome.getBoundingClientRect().top - predictor.getBoundingClientRect().top)) + .toBeLessThanOrEqual(1); + }); + + it('caps the roster at five rows and scrolls the overflow', () => { + experimentStudioService.algorithmAssignableVariables.set([ + age, + sex, + bmi, + derived, + { code: 'a', label: 'A', type: 'continuous' }, + { code: 'b', label: 'B', type: 'continuous' }, + { code: 'c', label: 'C', type: 'continuous' }, + ]); + fixture.detectChanges(); + + const roster = (fixture.nativeElement as HTMLElement).querySelector<HTMLElement>('.roster')!; + const style = getComputedStyle(roster); + + expect(style.overflowY).toBe('auto'); + expect(style.maxHeight).toBe('220px'); + }); + + it('collapses to the compact band with Add and expands back with Done', () => { + experimentStudioService.algorithmY.set([age]); + fixture.detectChanges(); + + fixture.componentInstance.collapseRoles(); + fixture.detectChanges(); + let root = fixture.nativeElement as HTMLElement; + expect(root.querySelector('.roster')).toBeNull(); + expect(root.querySelector('.role-action--add')?.textContent).toContain('Add'); + + (root.querySelector('.role-action--add') as HTMLButtonElement).click(); + fixture.detectChanges(); + root = fixture.nativeElement as HTMLElement; + expect(root.querySelector('.roster')).toBeTruthy(); + expect(root.querySelector('.role-action--quiet')?.textContent).toContain('Done'); + }); + + it('setRole unassigns a node back to the pool', () => { + experimentStudioService.algorithmY.set([age]); + fixture.detectChanges(); + + fixture.componentInstance.setRole(age, ''); + + expect(experimentStudioService.setAlgorithmY).toHaveBeenCalledWith([]); + expect(experimentStudioService.algorithmY()).toEqual([]); + expect(fixture.componentInstance.roleOf(age)).toBe(''); + }); + + it('setRole unassigns only the node\'s own role, leaving the other role untouched', () => { + experimentStudioService.algorithmY.set([sex]); + experimentStudioService.algorithmX.set([age]); + fixture.detectChanges(); + + fixture.componentInstance.setRole(age, ''); + + expect(experimentStudioService.setAlgorithmX).toHaveBeenCalledWith([]); + expect(experimentStudioService.algorithmX()).toEqual([]); + // age never was an outcome: setAlgorithmY must not be touched at all. + expect(experimentStudioService.setAlgorithmY).not.toHaveBeenCalled(); + expect(experimentStudioService.algorithmY()).toEqual([sex]); + }); + + it('grays an unavailable pool row, names the datasets and refuses role clicks', () => { + experimentStudioService.unavailableVariables.set(new Map([['sex', ['Dataset B']]])); + fixture.detectChanges(); + + const row = Array.from<HTMLElement>(fixture.nativeElement.querySelectorAll('li.row')) + .find((item) => item.textContent?.includes('Sex'))!; + expect(row.classList).toContain('is-unavailable'); + expect(row.getAttribute('aria-disabled')).toBe('true'); + expect(row.getAttribute('title')).toBe('Not available in: Dataset B'); + const outcome = row.querySelector('button[data-role="y"]') as HTMLButtonElement; + expect(outcome.getAttribute('aria-disabled')).toBe('true'); + + outcome.click(); + (row.querySelector('button[data-role="x"]') as HTMLButtonElement).click(); + outcome.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); + fixture.componentInstance.assignTo('x', sex); + expect(experimentStudioService.setAlgorithmY).not.toHaveBeenCalled(); + expect(experimentStudioService.setAlgorithmX).not.toHaveBeenCalled(); + }); + + it('keeps an assigned variable that became unavailable, grayed, undraggable and removable', () => { + experimentStudioService.algorithmY.set([sex]); + experimentStudioService.unavailableVariables.set(new Map([['sex', ['Dataset B']]])); + fixture.detectChanges(); + + const chip = fixture.nativeElement.querySelector('.chip--y') as HTMLElement; + expect(chip.classList).toContain('is-unavailable'); + expect(chip.classList).toContain('cdk-drag-disabled'); + expect(chip.getAttribute('aria-disabled')).toBe('true'); + expect(chip.getAttribute('title')).toBe('Not available in: Dataset B'); + + fixture.componentInstance.assignTo('x', sex); + expect(experimentStudioService.algorithmY()).toEqual([sex]); + expect(experimentStudioService.setAlgorithmX).not.toHaveBeenCalled(); + + (chip.querySelector('.chip-x') as HTMLButtonElement).click(); + expect(experimentStudioService.setAlgorithmY).toHaveBeenCalledWith([]); + }); +}); diff --git a/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.ts b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.ts new file mode 100644 index 000000000..b5e437cc9 --- /dev/null +++ b/src/app/pages/experiment-studio/algorithm-panel/algorithm-role-assignment/algorithm-role-assignment.component.ts @@ -0,0 +1,143 @@ +import { ChangeDetectionStrategy, Component, computed, effect, inject, signal } from '@angular/core'; +import { CdkDragDrop, DragDropModule } from '@angular/cdk/drag-drop'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { unavailableTooltipFor } from '../../../../core/variable-availability.utils'; + +type AlgorithmRole = 'y' | 'x'; +type PoolRole = AlgorithmRole | ''; + +@Component({ + selector: 'app-algorithm-role-assignment', + imports: [DragDropModule], + templateUrl: './algorithm-role-assignment.component.html', + styleUrl: './algorithm-role-assignment.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class AlgorithmRoleAssignmentComponent { + private readonly expStudioService = inject(ExperimentStudioService); + + readonly assignable = this.expStudioService.algorithmAssignableVariables; + readonly y = this.expStudioService.algorithmY; + readonly x = this.expStudioService.algorithmX; + readonly poolFilter = signal(''); + /** Compact once both roles have a value; expanded by default so y can be assigned. */ + readonly rolesExpanded = signal(this.y().length === 0); + + constructor() { + effect(() => { + if (this.y().length === 0 && !this.rolesExpanded()) { + this.rolesExpanded.set(true); + } + }); + } + + expandRoles(): void { + this.rolesExpanded.set(true); + requestAnimationFrame(() => { + document.getElementById('pool-filter')?.focus(); + }); + } + + collapseRoles(): void { + this.rolesExpanded.set(false); + requestAnimationFrame(() => { + document.querySelector<HTMLButtonElement>('.role-action--add')?.focus(); + }); + } + + /** Assignable pool members not yet assigned to either role. */ + readonly source = computed(() => { + const inY = new Set(this.y().map((v) => v.code)); + const inX = new Set(this.x().map((v) => v.code)); + return this.assignable().filter((v) => !inY.has(v.code) && !inX.has(v.code)); + }); + + /** Unassigned variables only — assigned ones live as chips in the role slots. */ + readonly filteredPool = computed(() => { + const q = this.poolFilter().trim().toLowerCase(); + const list = this.source(); + if (!q) return list; + return list.filter((node) => { + const name = this.label(node).toLowerCase(); + const type = String(node?.type ?? '').toLowerCase(); + return name.includes(q) || type.includes(q); + }); + }); + + /** A chip released over the other slot moves there; drops inside its own slot are ignored. */ + onChipDrop(event: CdkDragDrop<unknown>, role: AlgorithmRole): void { + if (event.previousContainer === event.container) return; + this.assignTo(role, event.item.data); + } + + onFilter(event: Event): void { + this.poolFilter.set((event.target as HTMLInputElement).value); + } + + roleOf(node: any): PoolRole { + if (this.isAssigned(node, 'y')) return 'y'; + if (this.isAssigned(node, 'x')) return 'x'; + return ''; + } + + setRole(node: any, role: PoolRole): void { + if (this.isUnavailable(node)) return; + if (role) this.assignTo(role, node); + else { + const current = this.roleOf(node); + if (current) this.removeFrom(current, node); + } + } + + isAssigned(node: any, role: AlgorithmRole): boolean { + const list = role === 'y' ? this.y() : this.x(); + return list.some((v) => v.code === node.code); + } + + assignTo(role: AlgorithmRole, node: any): void { + // Missing from a selected dataset: cannot be given a role or moved between roles. + if (this.isUnavailable(node)) return; + const target = role === 'y' ? this.y() : this.x(); + // Guard: a node can never appear in both roles. If it is already in the + // target role, assigning again is a no-op; if it is in the opposite role, + // remove it there so the roles stay disjoint. + if (target.some((v) => v.code === node.code)) return; + const opposite = role === 'y' ? this.x() : this.y(); + const otherNext = opposite.filter((v) => v.code !== node.code); + const next = [...target, node]; + if (role === 'y') { + this.expStudioService.setAlgorithmX(otherNext); + this.expStudioService.setAlgorithmY(next); + } else { + this.expStudioService.setAlgorithmY(otherNext); + this.expStudioService.setAlgorithmX(next); + } + } + + removeFrom(role: AlgorithmRole, node: any): void { + const list = role === 'y' ? this.y() : this.x(); + const next = list.filter((v) => v.code !== node.code); + if (role === 'y') { + this.expStudioService.setAlgorithmY(next); + } else { + this.expStudioService.setAlgorithmX(next); + } + } + + label(node: any): string { + return node?.label ?? node?.name ?? node?.code ?? ''; + } + + /** "Not available in: …" for a variable missing from a selected dataset, '' otherwise. */ + unavailableTooltip(node: any): string { + return unavailableTooltipFor(this.expStudioService.unavailableVariables(), { code: node?.code }); + } + + isUnavailable(node: any): boolean { + return this.unavailableTooltip(node) !== ''; + } + + isCreated(node: any): boolean { + return !!node?.isCreatedColumn; + } +} diff --git a/src/app/pages/experiment-studio/execution-panel/execution-panel.component.css b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.css new file mode 100644 index 000000000..e0254dd2d --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.css @@ -0,0 +1,484 @@ +/* Result workspace chrome (.experiment-panel-workspace, .result-section-toolbar, .toolbar-btn, + .save-as-inline) is global in styles.css. Only this step's own surfaces live here. */ + +/* Result workspace's second column (the grid track is declared in styles.css). Sticky below + the header + sub-header pair, like the other sticky offsets on this route; the card inside + scrolls itself, so the frame never grows past the viewport. */ +.experiment-panel-aside { + position: sticky; + top: calc(var(--header-height, 64px) + var(--studio-sub-header-height, 44px) + 12px); + min-width: 0; +} + +/* Under the workspace breakpoint the column drops below the result at full width, where + sticking a tall card to the bar only hides the page behind it. */ +@media (max-width: 1100px) { + .experiment-panel-aside { + position: static; + } +} + +.panel-loader { + margin: 20px 14px; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + min-height: 480px; + background: rgba(255, 255, 255, 0.5); + border-radius: 20px; + gap: 1.5rem; + border: 1px dashed rgba(148, 163, 184, 0.3); +} + +.panel-loader p { + color: var(--text-muted); + font-weight: 500; + font-size: 1.1rem; + margin: 0; +} + +.save-as-input { + padding: 8px 12px; + border-radius: var(--radius-sm); + border: 1px solid #cbd5e1; + font-size: 0.85rem; + outline: none; + min-width: 200px; +} + +.save-as-input:focus { + border-color: var(--primary-color); + box-shadow: 0 0 0 2px rgba(43, 51, 233, 0.1); +} + +.execution-error { + display: flex; + align-items: center; + gap: 14px; + flex-wrap: wrap; + margin: 16px 0; + padding: 18px 20px; + background: color-mix(in srgb, var(--danger) 6%, white); + border: 1px solid color-mix(in srgb, var(--danger) 28%, transparent); + border-radius: var(--radius-lg, 12px); +} + +.execution-error > i { + font-size: 1.25rem; + color: var(--danger); +} + +.execution-error-copy { + flex: 1 1 260px; + min-width: 0; +} + +.execution-error-title { + margin: 0 0 4px; + font-size: 0.95rem; + font-weight: 700; + color: var(--text-main); +} + +.execution-error-message { + margin: 0; + font-size: 0.85rem; + line-height: 1.5; + color: var(--text-muted); + overflow-wrap: anywhere; +} + +.execution-error-action { + appearance: none; + border: 0; + background: var(--primary-color); + color: #ffffff; + font-family: inherit; + font-weight: 650; + font-size: 0.85rem; + padding: 10px 16px; + border-radius: var(--radius-md, 8px); + cursor: pointer; + box-shadow: 0 2px 8px rgba(43, 51, 233, 0.18); + transition: background-color 0.2s ease; +} + +.execution-error-action:hover { + background: var(--primary-dark); +} + +.execution-empty { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + text-align: center; + gap: 8px; + padding: 64px 24px; + min-height: 320px; +} + +.execution-empty-icon { + display: grid; + place-items: center; + width: 48px; + height: 48px; + margin-bottom: 6px; + border-radius: 50%; + background: color-mix(in srgb, var(--primary-color) 8%, white); + color: var(--primary-color); + font-size: 1.1rem; +} + +.execution-empty h3 { + margin: 0; + font-size: 1.05rem; + color: var(--text-main); +} + +.execution-empty p { + margin: 0; + max-width: 46ch; + font-size: 0.88rem; + line-height: 1.6; + color: var(--text-muted); +} + +.execution-empty-action { + appearance: none; + border: 1px solid rgba(43, 51, 233, 0.25); + background: rgba(43, 51, 233, 0.08); + color: var(--primary-color); + font-family: inherit; + font-weight: 700; + font-size: 0.85rem; + padding: 10px 16px; + border-radius: 12px; + cursor: pointer; + margin-top: 12px; + transition: background-color 0.2s ease; +} + +.execution-empty-action:hover { + background: rgba(43, 51, 233, 0.14); +} + +.success-notification { + position: fixed; + bottom: 24px; + right: 24px; + z-index: 9999; + animation: slideInUp 0.3s ease-out, fadeOut 0.3s ease-in 3.5s forwards; +} + +.notification-content { + display: flex; + align-items: flex-start; + gap: 10px; + padding: 14px 20px; + background: white; + border: 1px solid rgba(16, 185, 129, 0.2); + border-radius: 12px; + box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1); + font-size: 0.9rem; + font-weight: 500; + color: #065f46; +} + +.notification-body { + display: flex; + flex-direction: column; + gap: 3px; +} + +.notification-title { + font-weight: 700; +} + +.notification-subtext { + font-size: 0.82rem; + line-height: 1.4; + color: #0f766e; +} + +.notification-link { + color: var(--primary-color); + font-weight: 700; + text-decoration: underline; + text-underline-offset: 2px; +} + +.notification-link:hover, +.notification-link:focus-visible { + color: var(--primary-dark); +} + +.notification-content i { + color: var(--success); + font-size: 1.1rem; + margin-top: 2px; +} + +@keyframes slideInUp { + from { + opacity: 0; + transform: translateY(20px); + } + + to { + opacity: 1; + transform: translateY(0); + } +} + +@keyframes fadeOut { + from { + opacity: 1; + } + + to { + opacity: 0; + } +} + +/* Results header (Results page review 1b): provenance + status on the left, + three flat actions on the right. The run bar lives in the shell, so this + header never duplicates the Run CTA. */ + +.result-header { + display: flex; + align-items: flex-end; + justify-content: space-between; + flex-wrap: wrap; + gap: 16px; + padding-bottom: 14px; + margin-bottom: 16px; + border-bottom: 1px solid rgba(148, 163, 184, 0.22); +} + +.result-header-main { + display: flex; + flex-direction: column; + gap: 2px; + min-width: 0; +} + +.result-eyebrow { + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.06em; + text-transform: uppercase; + color: var(--primary-color); +} + +.result-header .result-section-title { + margin: 0; + min-width: 0; + font-size: 1.4rem; + font-weight: 700; + line-height: 1.25; + letter-spacing: -0.005em; + color: var(--text-main); + overflow-wrap: anywhere; +} + +.result-meta { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 6px 12px; + margin-top: 2px; + font-size: 0.78rem; + color: var(--text-muted); + font-variant-numeric: tabular-nums; +} + +.result-status { + display: inline-flex; + align-items: center; + gap: 6px; + font-weight: 600; + color: #78350f; +} + +.result-status.is-saved { + color: #065f46; +} + +.result-status-dot { + flex: 0 0 auto; + width: 7px; + height: 7px; + border-radius: 50%; + background: #a06b10; +} + +.result-status.is-saved .result-status-dot { + background: #0f9d6b; +} + +.result-actions { + display: flex; + align-items: center; + justify-content: flex-end; + flex-wrap: wrap; + gap: 8px; + min-width: 0; +} + +.result-action { + display: inline-flex; + align-items: center; + justify-content: center; + gap: 6px; + height: 36px; + padding: 0 14px; + border: 1px solid rgba(43, 51, 233, 0.2); + border-radius: 999px; + background: #ffffff; + color: var(--primary-color); + font: inherit; + font-size: 0.82rem; + font-weight: 600; + white-space: nowrap; + cursor: pointer; + transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease; +} + +.result-action:hover, +.result-action:focus-visible { + background: #eef0fd; + border-color: rgba(43, 51, 233, 0.4); + outline: none; +} + +.result-action--primary { + border-color: var(--primary-color); + background: var(--primary-color); + color: #ffffff; + box-shadow: 0 1px 2px rgba(43, 51, 233, 0.2); +} + +.result-action--primary:hover, +.result-action--primary:focus-visible { + background: var(--primary-dark); + border-color: var(--primary-dark); +} + +.result-action--primary:disabled { + background: rgba(127, 156, 232, 0.55); + border-color: transparent; + cursor: not-allowed; + box-shadow: none; +} + +.result-action-caret { + font-size: 0.68rem; + line-height: 1; +} + +.result-export { + position: relative; +} + +.result-export-menu { + position: absolute; + top: calc(100% + 6px); + right: 0; + z-index: 20; + width: 250px; + padding: 6px; + border: 1px solid rgba(148, 163, 184, 0.3); + border-radius: 10px; + background: #ffffff; + box-shadow: 0 12px 32px rgba(15, 23, 42, 0.14); + display: flex; + flex-direction: column; +} + +.result-export-menu button { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 1px; + padding: 8px 10px; + border: 0; + border-radius: 6px; + background: transparent; + font: inherit; + text-align: left; + cursor: pointer; +} + +.result-export-menu button:hover, +.result-export-menu button:focus-visible { + background: #f3f5fe; + outline: none; +} + +.result-export-menu strong { + font-size: 0.84rem; + font-weight: 600; + color: var(--text-main); +} + +.result-export-menu small { + font-size: 0.74rem; + color: var(--text-muted); +} + +.result-save-form { + display: flex; + align-items: center; + justify-content: flex-end; + flex-wrap: wrap; + gap: 8px; + min-width: 0; +} + +.result-save-error { + flex-basis: 100%; + margin: 0; + font-size: 0.85rem; + /* Dark danger ink: --danger is a fill colour, too light for text (DESIGN.md). */ + color: #991b1b; + overflow-wrap: anywhere; +} + +.result-save-form .save-as-input { + width: 260px; + max-width: 100%; + height: 36px; + box-sizing: border-box; + padding: 0 12px; + border: 1px solid var(--primary-color); + border-radius: 8px; + font-size: 0.85rem; + outline: none; + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.12); +} + +.result-save-form .sr-only { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0, 0, 0, 0); +} + +@media (max-width: 640px) { + .result-header { + align-items: flex-start; + } + + .result-actions, + .result-save-form { + justify-content: flex-start; + width: 100%; + } + + .result-save-form .save-as-input { + width: 100%; + } +} diff --git a/src/app/pages/experiment-studio/execution-panel/execution-panel.component.html b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.html new file mode 100644 index 000000000..09264d6e7 --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.html @@ -0,0 +1,134 @@ +@if (isRunning()) { + <div class="panel-loader"> + <div class="studio-skeleton-card studio-skeleton-card--chart" role="status" aria-label="Experiment running"> + <span class="studio-skeleton-line long"></span> + <span class="studio-skeleton-line medium"></span> + <div class="studio-skeleton-chart" aria-hidden="true"> + <span class="studio-skeleton-bar" style="height: 38%"></span> + <span class="studio-skeleton-bar" style="height: 56%"></span> + <span class="studio-skeleton-bar" style="height: 72%"></span> + <span class="studio-skeleton-bar" style="height: 50%"></span> + <span class="studio-skeleton-bar" style="height: 82%"></span> + </div> + </div> + <p>{{ loadingText() }}</p> + </div> +} @else if (runError(); as error) { + <div class="execution-error" role="alert"> + <i class="fas fa-triangle-exclamation" aria-hidden="true"></i> + <div class="execution-error-copy"> + <h3 class="execution-error-title">The experiment did not finish</h3> + <p class="execution-error-message">{{ error }}</p> + </div> + <button type="button" class="execution-error-action" (click)="backToAlgorithm()"> + Back to Algorithm Selection + </button> + </div> +} @else if (result()) { + <div class="experiment-panel-workspace experiment-panel-workspace--result"> + <section class="experiment-panel-main result-section" #resultSection data-guide="experiment-result"> + <header class="result-header"> + <div class="result-header-main"> + <span class="result-eyebrow">{{ resultEyebrow() }}</span> + <h2 class="result-section-title">{{ resultHeadline() }}</h2> + <div class="result-meta"> + <span class="result-status" [class.is-saved]="savedTone() === 'is-saved'"> + <span class="result-status-dot" aria-hidden="true"></span>{{ savedText() }} + </span> + <span>{{ runTimeText() }}</span> + @if (runDurationText()) { + <span>{{ runDurationText() }}</span> + } + @if (mipVersion) { + <span>MIP {{ mipVersion }}</span> + } + </div> + </div> + + @if (!saveAsMode()) { + <div class="result-actions" data-guide="save-as-flow" role="group" aria-label="Result actions"> + <button type="button" class="result-action" (click)="backToAlgorithm()" + title="Edit parameters" data-guide="edit-parameters-action"> + Edit parameters + </button> + + <div class="result-export"> + <button type="button" class="result-action result-action--export" [attr.aria-expanded]="exportOpen()" + data-guide="result-export" title="Export result" (click)="toggleExport()"> + Export <span class="result-action-caret" aria-hidden="true">▾</span> + </button> + + @if (exportOpen()) { + <div class="result-export-menu" role="menu"> + <button type="button" role="menuitem" (click)="onExportPdf(resultSection)"> + <strong>PDF report</strong> + <small>Figures, tables and setup</small> + </button> + <button type="button" role="menuitem" (click)="onExportZip()"> + <strong>Tables as CSV (ZIP)</strong> + <small>One CSV per table, full result data and a README</small> + </button> + </div> + } + </div> + + <button type="button" class="result-action result-action--primary" (click)="toggleSaveAsMode()" + data-guide="save-as-action"> + {{ saveLabel() }} + </button> + </div> + } @else { + <div class="result-save-form" data-guide="save-as-form"> + <label class="sr-only" for="save-as-name-input">Experiment name</label> + <input id="save-as-name-input" type="text" [ngModel]="saveAsName()" (ngModelChange)="saveAsName.set($event)" + placeholder="Experiment name" class="save-as-input" /> + <button type="button" class="result-action" (click)="cancelSaveAs()">Cancel</button> + <button type="button" class="result-action result-action--primary" (click)="onSaveAs()" + [disabled]="!saveAsName().trim() || isRunning() || isSaving()"> + {{ isSaving() ? "Saving…" : "Save" }} + </button> + </div> + } + @if (saveError(); as error) { + <p class="result-save-error" role="alert">Saving failed: {{ error }}</p> + } + </header> + <div class="result-section-body"> + <app-algorithm-result [result]="result()" [schema]="schema()" [algorithm]="resultAlgorithmKey()" + [enumMaps]="enumMaps()" [yVar]="yVar()" [xVar]="xVar()" [labelMap]="labelMap()" + [algorithmLabel]="resultAlgorithmLabel()" [parameters]="setupSnapshot()?.parameters ?? null" /> + </div> + + </section> + + @if (setupSnapshot()) { + <aside class="experiment-panel-aside" aria-label="Experiment setup"> + <app-experiment-setup-summary></app-experiment-setup-summary> + </aside> + } + </div> +} @else { + <div class="execution-empty"> + <div class="execution-empty-icon"><i class="fas fa-play" aria-hidden="true"></i></div> + <h3>No results yet</h3> + <p>Configure the algorithm parameters and run the experiment to see its results here.</p> + <button type="button" class="execution-empty-action" (click)="backToAlgorithm()"> + Back to Algorithm Selection + </button> + </div> +} + +@if (showSuccessNotification()) { +<div class="success-notification" role="status" aria-live="polite"> + <div class="notification-content"> + <i class="fas fa-check-circle"></i> + <div class="notification-body"> + <span class="notification-title">Experiment saved successfully!</span> + <span class="notification-subtext"> + Review and compare your experiments on the + <a class="notification-link" routerLink="/experiments-dashboard">dashboard</a>. + </span> + </div> + </div> +</div> +} diff --git a/src/app/pages/experiment-studio/execution-panel/execution-panel.component.spec.ts b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.spec.ts new file mode 100644 index 000000000..746754764 --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.spec.ts @@ -0,0 +1,183 @@ +import { Component, provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideRouter } from '@angular/router'; + +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { AlgorithmResultComponent } from '../algorithm-panel/algorithm-result/algorithm-result.component'; +import { ExecutionPanelComponent } from './execution-panel.component'; + +/** The result view has its own suite; here it only needs to exist so the shell renders. */ +@Component({ + standalone: true, + selector: 'app-algorithm-result', + template: '<div class="fake-result"></div>', + inputs: ['result', 'schema', 'algorithm', 'enumMaps', 'yVar', 'xVar', 'labelMap', 'algorithmLabel', 'parameters'], +}) +class FakeResultComponent {} + +describe('ExecutionPanelComponent', () => { + let fixture: ComponentFixture<ExecutionPanelComponent>; + const studio = { + isRunning: signal(false), + isSaving: signal(false), + saveRun: vi.fn(), + runResult: signal<Record<string, unknown> | null>(null), + runSetup: signal(null), + runError: signal<string | null>(null), + lastRunSchema: signal<unknown>(null), + runStatusText: signal('Running the experiment…'), + saveSucceeded: signal(false), + saveError: signal<string | null>(null), + lastUsedAlgorithm: signal('flat_algo'), + backendAlgorithms: signal<Record<string, { label: string }>>({ flat_algo: { label: 'Flat Algo' } }), + algorithmY: signal<never[]>([]), + algorithmX: signal<never[]>([]), + variableLabelMap: signal<Record<string, string>>({}), + getCategoricalEnumMaps: () => ({}), + }; + const studioNavigation = { navigateToSection: vi.fn() }; + + /** Zoneless: every state is a signal write, so rendering is driven through the stub. */ + function render(state: Partial<{ running: boolean; result: Record<string, unknown> | null; error: string | null }> = {}): HTMLElement { + studio.isRunning.set(state.running ?? false); + studio.runResult.set(state.result ?? null); + studio.runError.set(state.error ?? null); + fixture.detectChanges(); + return fixture.nativeElement as HTMLElement; + } + + beforeEach(async () => { + studio.isRunning.set(false); + studio.isSaving.set(false); + studio.runResult.set(null); + studio.runError.set(null); + studio.saveError.set(null); + studio.saveSucceeded.set(false); + studio.saveRun.mockClear(); + studioNavigation.navigateToSection.mockClear(); + + await TestBed.configureTestingModule({ + imports: [ExecutionPanelComponent], + providers: [ + provideZonelessChangeDetection(), + provideRouter([]), + { provide: ExperimentStudioService, useValue: studio }, + { provide: ExperimentStudioNavigationService, useValue: studioNavigation }, + { provide: RuntimeEnvService, useValue: { mipVersion: 'test' } }, + ], + }).compileComponents(); + TestBed.overrideComponent(ExecutionPanelComponent, { + remove: { imports: [AlgorithmResultComponent] }, + add: { imports: [FakeResultComponent] }, + }); + + fixture = TestBed.createComponent(ExecutionPanelComponent); + }); + + it('shows why the last save failed next to the result', () => { + studio.saveError.set('Engine crashed'); + const html = render({ result: { title: 'Saved run' } }); + + expect(html.querySelector('[role="alert"].result-save-error')?.textContent).toContain('Saving failed: Engine crashed'); + }); + + it('asks for a run while there is no result', () => { + const html = render(); + + expect(html.querySelector('.execution-empty')).toBeTruthy(); + expect(html.querySelector('.fake-result')).toBeFalsy(); + }); + + it('shows the run skeleton with its status text instead of the empty state', () => { + const html = render({ running: true, result: { title: 'Ignored while running' } }); + + expect(html.querySelector('.panel-loader')).toBeTruthy(); + expect(html.querySelector('.panel-loader p')?.textContent?.trim()).toBe('Running the experiment…'); + expect(html.querySelector('.execution-empty')).toBeFalsy(); + }); + + it('reports a failed run and returns to the algorithm step', () => { + const html = render({ error: 'Backend exploded' }); + + expect(html.querySelector('.execution-error')?.textContent).toContain('Backend exploded'); + + html.querySelector<HTMLButtonElement>('.execution-error-action')!.click(); + expect(studioNavigation.navigateToSection).toHaveBeenCalledWith('algorithm-section'); + }); + + it('keeps the setup rail out of the result workspace until a run captured one', () => { + const html = render({ result: {} }); + + expect(html.querySelector('.experiment-panel-aside')).toBeFalsy(); + }); + + it('names an untitled result after its algorithm and keeps the result actions', () => { + const html = render({ result: {} }); + + expect(html.querySelector('.fake-result')).toBeTruthy(); + expect(html.querySelector('.result-section-title')?.textContent?.trim()).toBe('Result Flat Algo'); + expect(html.querySelector('[data-guide="save-as-action"]')).toBeTruthy(); + expect(html.querySelector('[data-guide="result-export"]')).toBeTruthy(); + }); + + it('prefers an explicit result title', () => { + const html = render({ result: { title: ' My run ' } }); + + expect(html.querySelector('.result-section-title')?.textContent?.trim()).toBe('My run'); + }); + + it('saves the shown run under the trimmed name and refuses a blank one', () => { + render({ result: {} }); + + fixture.componentInstance.toggleSaveAsMode(); + fixture.detectChanges(); + expect(htmlOf().querySelector('[data-guide="save-as-form"]')).toBeTruthy(); + expect(fixture.componentInstance.saveAsName()).toBe('Experiment for Flat Algo'); + + fixture.componentInstance.saveAsName.set(' '); + fixture.componentInstance.onSaveAs(); + expect(studio.saveRun).not.toHaveBeenCalled(); + + fixture.componentInstance.saveAsName.set(' My copy '); + fixture.componentInstance.onSaveAs(); + expect(studio.saveRun).toHaveBeenCalledExactlyOnceWith('My copy'); + }); + + it('keeps the result and its run time on screen while saving', () => { + render({ result: {} }); + const runAt = fixture.componentInstance.runAt(); + fixture.componentInstance.toggleSaveAsMode(); + fixture.componentInstance.saveAsName.set('My run'); + fixture.detectChanges(); + + studio.isSaving.set(true); + fixture.detectChanges(); + const saveButton = htmlOf().querySelector<HTMLButtonElement>('[data-guide="save-as-form"] .result-action--primary'); + expect(saveButton?.disabled).toBe(true); + expect(saveButton?.textContent?.trim()).toBe('Saving…'); + expect(htmlOf().querySelector('[data-guide="experiment-result"]')).toBeTruthy(); + + studio.isSaving.set(false); + studio.saveSucceeded.set(true); + fixture.detectChanges(); + expect(fixture.componentInstance.savedText()).toBe('Saved as “My run”'); + expect(fixture.componentInstance.runAt()).toBe(runAt); + }); + + it('leaves save-as mode when going back to the algorithm step', () => { + render({ result: {} }); + fixture.componentInstance.toggleSaveAsMode(); + fixture.detectChanges(); + + fixture.componentInstance.backToAlgorithm(); + + expect(fixture.componentInstance.saveAsMode()).toBe(false); + expect(studioNavigation.navigateToSection).toHaveBeenCalledWith('algorithm-section'); + }); + + function htmlOf(): HTMLElement { + return fixture.nativeElement as HTMLElement; + } +}); diff --git a/src/app/pages/experiment-studio/execution-panel/execution-panel.component.ts b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.ts new file mode 100644 index 000000000..03fde9418 --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/execution-panel.component.ts @@ -0,0 +1,197 @@ +import { ChangeDetectionStrategy, Component, computed, effect, inject, output, signal, untracked } from '@angular/core'; +import { FormsModule } from '@angular/forms'; +import { RouterLink } from '@angular/router'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { prettifyLabel } from '../../../core/algorithm-mappers'; +import { formulaLine } from '../../../core/result-label.utils'; +import { AlgorithmResultComponent } from '../algorithm-panel/algorithm-result/algorithm-result.component'; +import { ExperimentSetupSummaryComponent } from './experiment-setup-summary/experiment-setup-summary.component'; + +/** + * Execution Results step: the run skeleton, its error state, and the result view with + * its docked setup summary. + * It owns no run configuration — Export stays on the algorithm panel, which owns the parameter + * form, and is emitted back to it by the studio shell. Saving needs no configuration: it + * persists the kept run behind the result through the studio service. + */ +@Component({ + selector: 'app-execution-panel', + imports: [FormsModule, RouterLink, AlgorithmResultComponent, ExperimentSetupSummaryComponent], + templateUrl: './execution-panel.component.html', + styleUrl: './execution-panel.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class ExecutionPanelComponent { + private expStudioService = inject(ExperimentStudioService); + private studioNavigation = inject(ExperimentStudioNavigationService); + + readonly isRunning = this.expStudioService.isRunning; + readonly isSaving = this.expStudioService.isSaving; + readonly result = this.expStudioService.runResult; + /** Setup behind the result; the summary aside is only rendered when a run captured one. */ + readonly setupSnapshot = this.expStudioService.runSetup; + readonly runError = this.expStudioService.runError; + readonly schema = this.expStudioService.lastRunSchema; + readonly loadingText = this.expStudioService.runStatusText; + readonly mipVersion = inject(RuntimeEnvService).mipVersion; + + readonly saveAsMode = signal(false); + readonly saveAsName = signal(''); + readonly showSuccessNotification = this.expStudioService.saveSucceeded; + readonly saveError = this.expStudioService.saveError; + + readonly resultAlgorithmLabel = computed(() => { + const algoKey = this.expStudioService.lastUsedAlgorithm() ?? ''; + if (!algoKey) return 'Algorithm'; + return this.expStudioService.backendAlgorithms()[algoKey]?.label ?? prettifyLabel(algoKey); + }); + + readonly resultDisplayTitle = computed(() => { + const explicitTitle = this.result()?.title; + if (typeof explicitTitle === 'string' && explicitTitle.trim()) return explicitTitle.trim(); + return `Result ${this.resultAlgorithmLabel()}`; + }); + + readonly resultAlgorithmKey = computed(() => this.expStudioService.lastUsedAlgorithm() ?? ''); + readonly enumMaps = computed(() => this.expStudioService.getCategoricalEnumMaps()); + readonly yVar = computed(() => this.expStudioService.algorithmY()[0]?.code ?? null); + readonly xVar = computed(() => this.expStudioService.algorithmX()[0]?.code ?? null); + readonly labelMap = this.expStudioService.variableLabelMap; + + /** PDF and ZIP export need the parameter form, so the algorithm panel keeps them. */ + readonly exportPdf = output<HTMLElement>(); + readonly exportZip = output<void>(); + + readonly exportOpen = signal(false); + /** Set once a save round-trips, so the header can switch Unsaved -> Saved. */ + readonly savedName = signal<string | null>(null); + readonly runAt = signal<Date | null>(null); + readonly runDurationMs = signal<number | null>(null); + private runningSince: number | null = null; + + constructor() { + // The save completes in the studio service; its success flag is the shared signal that + // closes this panel's inline Save As form. + // Only the success flag is tracked: the flag stays up for 8s, and re-running on a name edit + // in that window would close a fresh "Save a copy" form and mark it saved. + effect(() => { + if (!this.expStudioService.saveSucceeded()) return; + untracked(() => { + const name = this.saveAsName().trim(); + this.saveAsMode.set(false); + if (name) { + this.savedName.set(name); + } else if (!this.savedName()) { + this.savedName.set(this.resultHeadline()); + } + }); + }); + + // Run provenance: captured locally so the header can say when and how long the run took + // without adding state to the shared service (and without breaking its test doubles). + effect(() => { + const running = this.isRunning(); + if (running) { + this.runningSince = Date.now(); + // A new run produces a new, unsaved result, with no earlier save failure. + untracked(() => { + this.savedName.set(null); + this.saveError.set(null); + }); + return; + } + if (this.runningSince === null) return; + this.runDurationMs.set(Date.now() - this.runningSince); + this.runningSince = null; + this.runAt.set(new Date()); + }); + + effect(() => { + if (this.result() && !this.runAt()) { + this.runAt.set(new Date()); + } + }); + } + + toggleSaveAsMode(): void { + this.saveAsMode.update((v) => !v); + if (this.saveAsMode()) { + this.saveAsName.set(`Experiment for ${this.resultAlgorithmLabel()}`); + } + } + + cancelSaveAs(): void { + this.saveAsMode.set(false); + this.saveAsName.set(''); + } + + onSaveAs(): void { + const name = this.saveAsName().trim(); + if (!name || this.isSaving()) return; + // Saves the run already on screen; the analysis is not executed again. + this.expStudioService.saveRun(name); + } + + readonly savedText = computed(() => { + const name = this.savedName(); + return name ? `Saved as “${name}”` : 'Unsaved'; + }); + + readonly savedTone = computed(() => (this.savedName() ? 'is-saved' : 'is-unsaved')); + + readonly saveLabel = computed(() => (this.savedName() ? 'Save a copy' : 'Save experiment')); + + readonly resultEyebrow = computed(() => this.resultAlgorithmLabel() || 'Algorithm'); + + readonly resultHeadline = computed(() => { + const result = this.result(); + const algorithm = this.resultAlgorithmKey(); + if ( + result && + (algorithm === 'linear_regression' || algorithm.startsWith('linear_regression_')) && + typeof result['dependent_var'] === 'string' + ) { + const predictors = Array.isArray(result['indep_vars']) + ? result['indep_vars'] + .map((value: unknown) => String(value ?? '')) + .filter((value: string) => !/^intercept$/i.test(value)) + : []; + if (predictors.length) return formulaLine([result['dependent_var']], predictors, ' + '); + } + return this.resultDisplayTitle(); + }); + + readonly runTimeText = computed(() => { + const date = this.runAt(); + if (!date) return 'Ran just now'; + const time = new Intl.DateTimeFormat('en-GB', { hour: '2-digit', minute: '2-digit' }).format(date); + return date.toDateString() === new Date().toDateString() ? `Ran today, ${time}` : `Ran ${date.toLocaleDateString()}, ${time}`; + }); + + readonly runDurationText = computed(() => { + const duration = this.runDurationMs(); + if (duration === null || duration <= 0) return ''; + return duration < 1000 ? `${duration} ms` : `${(duration / 1000).toFixed(1)} s`; + }); + + toggleExport(): void { + this.exportOpen.update((open) => !open); + } + + onExportPdf(section: HTMLElement): void { + this.exportOpen.set(false); + this.exportPdf.emit(section); + } + + onExportZip(): void { + this.exportOpen.set(false); + this.exportZip.emit(); + } + + backToAlgorithm(): void { + this.cancelSaveAs(); + this.studioNavigation.navigateToSection('algorithm-section'); + } +} diff --git a/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.css b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.css new file mode 100644 index 000000000..138826409 --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.css @@ -0,0 +1,226 @@ +:host { + display: block; + min-width: 0; +} + +.setup-summary { + display: flex; + flex-direction: column; + min-width: 0; + border: 1px solid rgba(148, 163, 184, 0.28); + border-radius: 12px; + background: #ffffff; + overflow: hidden; +} + +.setup-summary-header { + display: flex; + flex-direction: column; + gap: 2px; + padding: 14px 16px 10px; +} + +.setup-summary-title { + margin: 0; + font-size: 0.95rem; + font-weight: 700; + color: var(--text-main); +} + +.setup-summary-subtitle { + margin: 0; + font-size: 0.76rem; + line-height: 1.45; + color: var(--text-muted); +} + +.setup-summary-body { + display: flex; + flex-direction: column; + min-height: 0; + max-height: max(280px, calc(100vh - var(--header-height, 64px) - var(--studio-sub-header-height, 44px) - 128px)); + overflow-y: auto; + overscroll-behavior: contain; +} + +.setup-summary-section { + display: flex; + flex-direction: column; + gap: 6px; + min-width: 0; + padding: 12px 16px; + border-top: 1px solid rgba(148, 163, 184, 0.2); +} + +.setup-summary-section:first-child { + border-top: 0; +} + +.setup-section-head { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 8px; + min-width: 0; +} + +.setup-section-actions { + display: inline-flex; + align-items: baseline; + gap: 8px; + flex: 0 0 auto; +} + +.setup-micro-label { + margin: 0; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.05em; + text-transform: uppercase; + color: var(--text-muted); +} + +.setup-field-action { + appearance: none; + flex: 0 0 auto; + margin: 0; + padding: 0; + border: 0; + border-radius: 4px; + background: none; + color: var(--primary-color); + font-family: inherit; + font-size: 0.76rem; + font-weight: 600; + cursor: pointer; +} + +.setup-field-action:hover, +.setup-field-action:focus-visible { + text-decoration: underline; + text-underline-offset: 2px; + outline: none; +} + +.setup-field-value { + margin: 0; + font-size: 0.85rem; + font-weight: 600; + line-height: 1.35; + color: var(--text-main); + overflow-wrap: anywhere; +} + +.setup-field-text { + margin: 0; + font-size: 0.78rem; + line-height: 1.5; + color: var(--text-muted); + overflow-wrap: anywhere; +} + +/* Variables as y / x markers beside the values, not a row of tinted chips. */ +.setup-role-matrix { + display: grid; + grid-template-columns: 22px minmax(0, 1fr); + gap: 6px 8px; + align-items: start; + font-size: 0.82rem; +} + +.setup-role-marker { + display: grid; + place-items: center; + height: 20px; + border-radius: 5px; + font-size: 0.7rem; + font-weight: 700; + line-height: 1; +} + +.setup-role-marker--y { + background: #fdf3e1; + color: #78350f; +} + +.setup-role-marker--x { + background: #eef1fc; + color: #1b21a3; +} + +.setup-role-values { + display: flex; + flex-direction: column; + gap: 2px; + min-width: 0; +} + +.setup-role-value { + min-width: 0; + font-weight: 600; + color: var(--text-main); + overflow-wrap: anywhere; +} + +/* Cohort & handling: label/value rows in one compact list. */ +.setup-compact-list { + display: grid; + grid-template-columns: auto minmax(0, 1fr); + gap: 4px 12px; + margin: 0; + font-size: 0.8rem; +} + +.setup-compact-row { + display: contents; +} + +.setup-compact-row dt { + color: var(--text-muted); +} + +.setup-compact-row dd { + margin: 0; + min-width: 0; + text-align: right; + color: var(--text-main); + overflow-wrap: anywhere; +} + +/* Algorithm parameters as a light two-column list. */ +.setup-detail-list { + display: grid; + gap: 4px; + margin: 0; + min-width: 0; +} + +.setup-detail-row { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 12px; + min-width: 0; +} + +.setup-detail-name { + margin: 0; + font-size: 0.76rem; + color: var(--text-muted); +} + +.setup-detail-value { + margin: 0; + font-size: 0.8rem; + font-weight: 600; + color: var(--text-main); + text-align: right; + overflow-wrap: anywhere; +} + +@media (max-width: 1100px) { + .setup-summary-body { + max-height: none; + overflow-y: visible; + } +} diff --git a/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.html b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.html new file mode 100644 index 000000000..560cec44c --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.html @@ -0,0 +1,110 @@ +@if (setup()) { +<section class="setup-summary" aria-label="Experiment setup" data-guide="experiment-setup-summary"> + <header class="setup-summary-header"> + <h3 class="setup-summary-title">Experiment setup</h3> + <p class="setup-summary-subtitle">What this result was built from</p> + </header> + + <div class="setup-summary-body"> + <section class="setup-summary-section"> + <div class="setup-section-head"> + <h4 class="setup-field-label setup-micro-label">Data</h4> + <button type="button" class="setup-field-action" (click)="goToDatasets()" + title="Change datasets and variables">Edit</button> + </div> + <p class="setup-field-value">{{ dataLine() }}</p> + @if (datasetLine()) { + <p class="setup-field-text">{{ datasetLine() }}</p> + } + </section> + + @if (hasVariables()) { + <section class="setup-summary-section"> + <div class="setup-section-head"> + <h4 class="setup-field-label setup-micro-label">Variables</h4> + <button type="button" class="setup-field-action" (click)="goToDatasets()" + title="Change the selected variables">Edit</button> + </div> + + <div class="setup-role-matrix"> + @if (outcome().length) { + <span class="setup-role-marker setup-role-marker--y" aria-hidden="true">y</span> + <div class="setup-role-values"> + @for (item of outcome(); track item.code) { + <span class="setup-role-value">{{ item.label }}</span> + } + </div> + } + @if (covariates().length) { + <span class="setup-role-marker setup-role-marker--x" aria-hidden="true">x</span> + <div class="setup-role-values"> + @for (item of covariates(); track item.code) { + <span class="setup-role-value">{{ item.label }}</span> + } + </div> + } + </div> + </section> + } + + <section class="setup-summary-section"> + <div class="setup-section-head"> + <h4 class="setup-field-label setup-micro-label">Cohort & handling</h4> + <div class="setup-section-actions"> + <button type="button" class="setup-field-action" (click)="goToFilters()" + title="Change the cohort filters">Filters</button> + <button type="button" class="setup-field-action" (click)="goToDataHandling()" + title="Change preprocessing and transformations">Handling</button> + </div> + </div> + + <dl class="setup-compact-list"> + <div class="setup-compact-row"> + <dt>Filters</dt> + <dd>{{ filterSummary() }}</dd> + </div> + <div class="setup-compact-row"> + <dt>Missing values</dt> + <dd>{{ missingValuesSummary() }}</dd> + </div> + @if (transformSummary()) { + <div class="setup-compact-row"> + <dt>Transform</dt> + <dd>{{ transformSummary() }}</dd> + </div> + } + @if (rowsUsedSummary()) { + <div class="setup-compact-row"> + <dt>Rows used</dt> + <dd>{{ rowsUsedSummary() }}</dd> + </div> + } + </dl> + </section> + + @if (algorithmLabel()) { + <section class="setup-summary-section"> + <div class="setup-section-head"> + <h4 class="setup-field-label setup-micro-label">Algorithm</h4> + <button type="button" class="setup-field-action" (click)="goToAlgorithm()" + title="Edit the algorithm and its parameters">Edit</button> + </div> + <p class="setup-field-value">{{ algorithmLabel() }}</p> + + @if (parameters().length) { + <dl class="setup-detail-list"> + @for (param of parameters(); track param.key) { + <div class="setup-detail-row"> + <dt class="setup-detail-name">{{ param.label }}</dt> + <dd class="setup-detail-value">{{ param.value }}</dd> + </div> + } + </dl> + } @else { + <p class="setup-field-text">No parameters</p> + } + </section> + } + </div> +</section> +} diff --git a/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.spec.ts b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.spec.ts new file mode 100644 index 000000000..272ebd4a3 --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.spec.ts @@ -0,0 +1,158 @@ +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { ExperimentStudioNavigationService } from '../../../../services/experiment-studio-navigation.service'; +import { ExperimentRunSetup } from '../../../../models/experiment-run-setup.model'; +import { ExperimentSetupSummaryComponent } from './experiment-setup-summary.component'; + +function buildSetup(overrides: Partial<ExperimentRunSetup> = {}): ExperimentRunSetup { + return { + algorithmKey: 'anova_oneway', + dataModel: 'EU Children', + datasets: ['clinical', 'biochemistry'], + outcome: ['outcome_3m'], + covariates: ['age'], + filterLogic: { condition: 'AND', rules: [], valid: true }, + preprocessing: [{ label: 'Missing values', value: '3m mRS: drop' }], + parameters: { conf_level: 0.95 }, + ...overrides, + }; +} + +describe('ExperimentSetupSummaryComponent', () => { + let fixture: ComponentFixture<ExperimentSetupSummaryComponent>; + const studio = { + runSetup: signal<ExperimentRunSetup | null>(null), + runResult: signal<any | null>(null), + availableDatasets: signal([{ code: 'clinical', label: 'Clinical' }, { code: 'biochemistry', label: 'Biochemistry' }]), + variableLabelMap: signal<Record<string, string>>({ + outcome_3m: '3m mRS good outcome', + age: 'Age at onset', + sex: 'Sex', + }), + getCategoricalEnumMaps: () => ({ sex: { '1': 'female', '0': 'male' } }), + backendAlgorithms: signal<Record<string, unknown>>({ + anova_oneway: { + label: 'One-way ANOVA', + configSchema: [ + { key: 'conf_level', label: 'Confidence level', default: 0.95 }, + { key: 'groupA', label: 'Group A', type: 'select', options: [{ code: '1', label: 'female' }] }, + ], + }, + }), + }; + const navigation = { + navigateToSection: vi.fn(), + navigateToDescriptiveStep: vi.fn(), + goToPreprocessing: vi.fn(), + }; + + beforeEach(async () => { + studio.runSetup.set(null); + navigation.navigateToSection.mockClear(); + navigation.navigateToDescriptiveStep.mockClear(); + navigation.goToPreprocessing.mockClear(); + + await TestBed.configureTestingModule({ + imports: [ExperimentSetupSummaryComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: studio }, + { provide: ExperimentStudioNavigationService, useValue: navigation }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(ExperimentSetupSummaryComponent); + }); + + /** Zoneless: every state is a signal write, so rendering is driven through the stub. */ + function render(setup: ExperimentRunSetup | null): HTMLElement { + studio.runSetup.set(setup); + fixture.detectChanges(); + return fixture.nativeElement as HTMLElement; + } + + function textOf(html: HTMLElement, selector: string): string { + return html.querySelector(selector)?.textContent?.trim() ?? ''; + } + + it('renders nothing until a run has captured a setup', () => { + expect(render(null).querySelector('.setup-summary')).toBeFalsy(); + }); + + it('describes the run from data model to parameters', () => { + const html = render(buildSetup({ + filterLogic: { + condition: 'AND', + valid: true, + rules: [{ id: 'r1', field: 'sex', type: 'string', input: 'select', operator: 'equal', value: '1' }], + }, + })); + + expect(textOf(html, '.setup-summary-title')).toBe('Experiment setup'); + expect(textOf(html, '.setup-field-label')).toBe('Data'); + expect(textOf(html, '.setup-field-value')).toBe('EU Children · 2 datasets'); + expect(html.textContent).toContain('Clinical'); + expect(html.textContent).toContain('Biochemistry'); + expect(html.textContent).toContain('3m mRS good outcome'); + expect(html.textContent).toContain('Age at onset'); + expect(html.textContent).toContain('Sex = female'); + expect(html.textContent).toContain('1 rule'); + expect(html.textContent).toContain('Missing values'); + expect(html.textContent).toContain('3m mRS: drop'); + expect(textOf(html, '.setup-summary-body section:last-child .setup-field-value')).toBe('One-way ANOVA'); + }); + + it('lists configured parameters and drops unset or duplicated ones', () => { + const html = render(buildSetup({ + parameters: { conf_level: 0.95, groupA: '1', notes: '' }, + })); + + // Name and value are asserted apart: the whitespace between two cells is the + // template's business, not the component's. + const rows = Array.from(html.querySelectorAll('.setup-detail-row')).map((row) => [ + textOf(row as HTMLElement, '.setup-detail-name'), + textOf(row as HTMLElement, '.setup-detail-value'), + ].join(' = ')); + expect(rows).toContain('Confidence level = 0.95'); + expect(rows).toContain('Group A = female'); + expect(html.textContent).not.toContain('notes'); + }); + + it('says so when the cohort was not filtered', () => { + const html = render(buildSetup({ parameters: {} })); + + expect(html.textContent).toContain('FiltersNone'); + }); + + it('lists the schema default of a parameter the run left unset', () => { + const html = render(buildSetup({ parameters: { conf_level: '' } })); + + expect(textOf(html, '.setup-detail-name')).toBe('Confidence level'); + expect(textOf(html, '.setup-detail-value')).toBe('0.95'); + expect(html.textContent).not.toContain('Default parameters'); + }); + + it('says the algorithm has no parameters when its schema has none', () => { + const html = render(buildSetup({ algorithmKey: 'linear_regression', parameters: {} })); + + expect(html.textContent).toContain('No parameters'); + }); + + it('walks back to the step that owns each part of the setup', () => { + const html = render(buildSetup()); + const actions = Array.from(html.querySelectorAll<HTMLButtonElement>('.setup-field-action')); + + actions[0].click(); // data + actions[1].click(); // variables + actions[2].click(); // cohort filters + actions[3].click(); // data handling + actions[4].click(); // algorithm + + expect(navigation.navigateToSection).toHaveBeenCalledWith('variables-top'); + expect(navigation.navigateToDescriptiveStep).toHaveBeenCalledWith('filters'); + expect(navigation.goToPreprocessing).toHaveBeenCalled(); + expect(navigation.navigateToSection).toHaveBeenCalledWith('algorithm-section'); + }); +}); diff --git a/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.ts b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.ts new file mode 100644 index 000000000..188223254 --- /dev/null +++ b/src/app/pages/experiment-studio/execution-panel/experiment-setup-summary/experiment-setup-summary.component.ts @@ -0,0 +1,139 @@ +import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { ExperimentStudioNavigationService } from '../../../../services/experiment-studio-navigation.service'; +import { prettifyLabel } from '../../../../core/algorithm-mappers'; +import { LabeledItem, observationCount, pluralize, withLabels } from '../../../../core/result-label.utils'; +import { formatAlgorithmParameterValue, parameterDefaultsFromSchema } from '../../../../core/algorithm-parameter.utils'; +import { countFilterRules, formatFilterExpression } from '../../../../core/filter-display.utils'; + +interface SetupDetailRow { + key: string; + label: string; + value: string; +} + +/** + * The setup behind the result on the Execution Results step: data model, datasets, + * variable roles, cohort filters, data handling and algorithm parameters in one column. + * + * Every field comes from `ExperimentStudioService.runSetup`, the snapshot taken when the run + * was dispatched, so nothing here can describe a selection the result did not use. Labels are + * resolved at render time because they belong to a code, not to a run. + */ +@Component({ + selector: 'app-experiment-setup-summary', + templateUrl: './experiment-setup-summary.component.html', + styleUrl: './experiment-setup-summary.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class ExperimentSetupSummaryComponent { + private studio = inject(ExperimentStudioService); + private navigation = inject(ExperimentStudioNavigationService); + + /** Null when no run has been dispatched in this studio session. */ + readonly setup = this.studio.runSetup; + + readonly datasets = computed<LabeledItem[]>(() => { + const labelByCode = Object.fromEntries(this.studio.availableDatasets().map((item) => [item.code, item.label])); + return withLabels(this.setup()?.datasets, labelByCode); + }); + + readonly outcome = computed<LabeledItem[]>(() => + withLabels(this.setup()?.outcome, this.studio.variableLabelMap()) + ); + readonly covariates = computed<LabeledItem[]>(() => + withLabels(this.setup()?.covariates, this.studio.variableLabelMap()) + ); + + readonly filterRuleCount = computed(() => countFilterRules(this.setup()?.filterLogic ?? null)); + readonly filterExpression = computed(() => { + const setup = this.setup(); + if (!setup) return ''; + return formatFilterExpression(setup.filterLogic, { + labelMap: this.studio.variableLabelMap(), + enumMaps: this.studio.getCategoricalEnumMaps(), + }); + }); + + readonly preprocessing = computed(() => this.setup()?.preprocessing ?? []); + + readonly dataLine = computed(() => { + const setup = this.setup(); + if (!setup) return ''; + return `${setup.dataModel ?? 'Data'} · ${pluralize(setup.datasets?.length ?? 0, 'dataset')}`; + }); + + readonly datasetLine = computed(() => + this.datasets().map((dataset) => dataset.label).join(', ') + ); + + readonly filterSummary = computed(() => { + const count = this.filterRuleCount(); + if (!count) return 'None'; + const expression = this.filterExpression(); + return `${expression || 'Filters applied'} · ${pluralize(count, 'rule')}`; + }); + + readonly missingValuesSummary = computed(() => { + const row = this.preprocessing().find((entry) => /missing/i.test(entry.label)); + return row?.value ?? 'Drop rows'; + }); + + readonly transformSummary = computed(() => + this.preprocessing() + .filter((entry) => /transform/i.test(entry.label)) + .map((entry) => entry.value) + .join(', ') + ); + + /** Result metadata the snapshot does not carry: show what is known, skip when absent. */ + readonly rowsUsedSummary = computed(() => observationCount(this.studio.runResult())); + + readonly algorithmKey = computed(() => this.setup()?.algorithmKey ?? ''); + readonly algorithmLabel = computed(() => { + const key = this.algorithmKey(); + if (!key) return ''; + return this.studio.backendAlgorithms()[key]?.label ?? prettifyLabel(key); + }); + + /** + * Parameters as they were configured for this run, labelled from the algorithm schema. + * A parameter left unset runs with its schema default, so that value is listed. + */ + readonly parameters = computed<SetupDetailRow[]>(() => { + const setup = this.setup(); + if (!setup?.algorithmKey) return []; + const schema = this.studio.backendAlgorithms()[setup.algorithmKey]?.configSchema ?? []; + + const configured = Object.entries(setup.parameters) + .filter(([, value]) => formatAlgorithmParameterValue(value) !== ''); + return Object.entries({ ...parameterDefaultsFromSchema(schema), ...Object.fromEntries(configured) }) + .map(([key, value]) => { + const field = schema.find((entry: any) => String(entry?.key) === key); + return { + key, + label: String(field?.label ?? prettifyLabel(key)), + value: formatAlgorithmParameterValue(value, field), + }; + }) + .filter((entry) => entry.value); + }); + + readonly hasVariables = computed(() => this.outcome().length > 0 || this.covariates().length > 0); + + goToDatasets(): void { + this.navigation.navigateToSection('variables-top'); + } + + goToFilters(): void { + this.navigation.navigateToDescriptiveStep('filters'); + } + + goToDataHandling(): void { + this.navigation.goToPreprocessing(); + } + + goToAlgorithm(): void { + this.navigation.navigateToSection('algorithm-section'); + } +} diff --git a/src/app/pages/experiment-studio/experiment-studio.component.css b/src/app/pages/experiment-studio/experiment-studio.component.css index 6dfe74ff1..444e2ee8b 100644 --- a/src/app/pages/experiment-studio/experiment-studio.component.css +++ b/src/app/pages/experiment-studio/experiment-studio.component.css @@ -3,336 +3,326 @@ position: relative; } -/* Page wrapper */ +/* Page wrapper. No --header-height term: the bar is sticky and sits above this box. + --studio-sub-header-height stays because row 2 is still fixed and out of the flow. */ .experiment-studio-page { - padding: 32px; + --warning-banner-space: 0px; + --studio-sub-header-height: 44px; + padding: calc(var(--studio-sub-header-height) + 8px) var(--studio-page-gutter) 16px; position: relative; - background: radial-gradient(circle at 50% 0%, #f8fafc 0%, #f1f5f9 100%); - /* Organic Tech Slate */ + background: transparent; color: var(--text-main); + font-family: var(--font-sans); box-sizing: border-box; - - - overflow-x: hidden; - /* Prevent horizontal scroll */ + /* overflow-x: clip so the window stays the scroller */ + overflow-x: clip; } -:host { - --studio-card-bg: var(--card-bg); - --studio-card-border: var(--border-color); - --studio-card-shadow: var(--shadow-md); - --studio-card-header-bg: #f8fafc; - --studio-card-header-border: var(--border-color); +.experiment-studio-page.has-pathology-warning { + --warning-banner-space: 96px; + padding-top: calc(var(--studio-sub-header-height) + 24px + var(--warning-banner-space)); } -/* Inner wrapper */ -.studio-inner { - margin: 0; - width: 100%; - max-width: none; +/* + * Row 2 of the same bar as the header: same surface, same bottom border, same height. + * It used to be an inset tinted card with backdrop-filter, which both broke DESIGN.md + * (`--glass-blur: none`, "do not reintroduce frost") and read as two different teams' + * work stacked on top of each other. + * The surface is full-bleed; only the content is inset. `--chrome-inline-padding` is the + * floor (it matches row 1), `.sub-header-inner` is free to indent further. + */ +.studio-sub-header { + position: fixed; + top: var(--header-height); + left: 0; + right: 0; + z-index: 9990; display: flex; - align-items: flex-start; + align-items: stretch; + height: var(--studio-sub-header-height); + padding: 0 var(--chrome-inline-padding); + box-sizing: border-box; + background: var(--header-surface); + border-bottom: 1px solid var(--header-border); +} + +/* + * Row 2 exists only on this route, so it can share the measure of the panels below it + * without re-indenting the bar that every other route shows: the CTA ends on the card's + * right border instead of overhanging it, and the stepper starts on the panels' left + * border instead of left of them. Both effects are (100% - --studio-inner-max) / 2 wide + * at viewports past the cap, so an inset on the bar itself would have been wrong on + * every other route; capping the content is the only way to get the alignment. + */ +.sub-header-inner { + width: 100%; + max-width: var(--studio-inner-max, 1320px); + margin-inline: auto; + min-width: 0; + display: grid; + /* Side tracks are content-sized so the fixed CTA can never be squeezed to 0 and + overflow leftwards over the stepper; the stepper track is the only flexible one. */ + grid-template-columns: auto minmax(0, 1fr) auto; + align-items: center; } -.studio-layout { - position: relative; +.sub-header-prev { + justify-self: start; + display: inline-flex; + align-items: center; + justify-content: center; + background: transparent; + border: none; + color: var(--text-muted); + padding: 6px 8px; + border-radius: var(--radius-md, 6px); + cursor: pointer; + transition: color 0.15s ease, opacity 0.15s ease; } -.studio-sidenav { - position: fixed; - top: 80px; - left: max(16px, calc(50% - 700px - 240px)); - width: 210px; - height: fit-content; - display: flex; - flex-direction: column; - gap: 16px; - padding: 16px 12px; - background: rgba(255, 255, 255, 0.92); - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - border: 1px solid var(--border-color); - border-radius: 20px; - box-shadow: 0 12px 40px rgba(15, 23, 42, 0.08); - max-height: calc(100vh - 120px); - overflow: visible; - z-index: 1000; - transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1); -} - -/* Tier 2: Medium Screens (Laptops) - Fixed Icon Rail */ -@media (min-width: 1201px) and (max-width: 1440px) { - .studio-sidenav { - width: 72px !important; - left: max(16px, calc(50% - 700px - 100px)) !important; - padding: 16px 0 !important; - gap: 20px !important; - align-items: center !important; - } - - /* Hide labels and titles in icon rail mode */ - .studio-sidenav .studio-rail-context-title, - .studio-sidenav .studio-rail-section-title, - .studio-sidenav .studio-rail-label, - .studio-sidenav .studio-rail-context-text, - .studio-sidenav .studio-rail-item::after { - display: none !important; - } - - .studio-sidenav .studio-rail-context-link, - .studio-sidenav .studio-rail-item { - justify-content: center !important; - padding: 12px !important; - width: 44px; - height: 44px; - border-radius: 12px; - margin: 0 auto; - transform: none !important; - } - - .studio-sidenav .studio-rail-icon { - font-size: 1.1rem !important; - margin: 0 !important; - } - - .studio-main { - padding-left: 104px !important; - } +.sub-header-prev:hover { + color: var(--primary-color); } -/* Tier 3: Narrow Screens (Mobile/Tablet) - Hidden/Drawer */ -@media (max-width: 1200px) { - .studio-sidenav { - left: -260px; - /* Fully hidden */ - top: 100px; - height: calc(100vh - 120px); - border-radius: 24px; - box-shadow: 0 20px 50px rgba(15, 23, 42, 0.15); - width: 210px; - /* Full width when drawer opens */ - } - - .studio-main { - padding-left: 0 !important; - } +.sub-header-prev:focus-visible { + color: var(--primary-color); + outline: 2px solid var(--primary-color); + outline-offset: 2px; } -/* Rail Header Area */ -.studio-rail-header { - height: 0; - margin: 0; - padding: 0; - overflow: hidden; +.sub-header-prev i { + font-size: 0.8rem; } -.studio-rail-context { - padding: 16px 4px; - transition: all 0.3s ease; +.sub-header-prev-placeholder { + justify-self: start; } -.studio-rail-context-title { - font-size: 0.72rem; - font-weight: 800; - text-transform: uppercase; - letter-spacing: 0.12em; - color: #64748b; - margin-bottom: 20px; - white-space: nowrap; +.studio-sub-header app-studio-stepper { + /* stretch, not center: center would size the host to max-content and let it + spill out of its track. The inner .stepper-track does the centering. */ + justify-self: stretch; + display: flex; + align-items: center; + min-width: 0; } -.studio-rail-context-link { - display: flex; +.sub-header-cta { + justify-self: end; + display: inline-flex; align-items: center; - gap: 12px; - padding: 10px 14px; - background: rgba(43, 51, 233, 0.06); - border-radius: 12px; - color: var(--primary-color); - text-decoration: none; + gap: 8px; + padding: 6px 14px; + background: var(--primary-color); + color: #ffffff; + border: 1px solid var(--primary-color); + border-radius: 999px; + font-family: inherit; font-size: 0.82rem; font-weight: 600; - transition: all 0.2s ease; + cursor: pointer; white-space: nowrap; + box-shadow: 0 1px 2px rgba(43, 51, 233, 0.18); } -.studio-rail-context-link:hover { - background: white; - color: var(--primary-color); - border-color: var(--primary-color); - box-shadow: var(--shadow-sm); +.sub-header-cta:hover:not(:disabled) { + background: color-mix(in srgb, var(--primary-color) 88%, #000000); } -.studio-rail-title { - font-size: 0.72rem; - text-transform: uppercase; - letter-spacing: 0.08em; - color: #64748b; - font-weight: 700; +.sub-header-cta:focus-visible { + outline: 2px solid var(--primary-color); + outline-offset: 2px; } -.studio-rail-section { - display: flex; - flex-direction: column; - gap: 6px; - padding-top: 10px; - border-top: 1px solid rgba(148, 163, 184, 0.2); +.sub-header-cta:disabled { + background: color-mix(in srgb, var(--text-muted) 22%, #ffffff); + border-color: var(--border-color, #e2e8f0); + color: var(--text-muted); + cursor: not-allowed; + box-shadow: none; } -.studio-rail-section-title { - padding-left: 12px; - margin-bottom: 12px; - font-size: 0.65rem; - font-weight: 700; - text-transform: uppercase; - letter-spacing: 0.1em; - color: #94a3b8; - position: relative; +@media (max-width: 768px) { + .studio-sub-header { + padding: 0 12px; + } + + .sub-header-cta { + padding: 6px 10px; + font-size: 0.75rem; + } } -.studio-rail-section-title.is-active { - color: var(--primary-color); +:host { + --studio-card-bg: var(--card-bg); + --studio-card-border: var(--border-color); + --studio-card-shadow: var(--shadow-md); + --studio-card-header-bg: #ffffff; + --studio-card-header-border: var(--border-color); + --studio-content-max: 720px; + /* Full-width step surfaces (Data Handling pipeline rail) — narrower than the + 1320px panel so a wide summary table does not stretch every station card. */ + --studio-step-max: 1160px; + /* Compact width for the pipeline's overview rows (header, collapsed nodes, + terminal). A collapsed row only carries a title and its rail, so the full + 1160px rail left ~600px of dead space between them. */ + --studio-pipeline-overview-max: 860px; +} + +/* Inner wrapper: centers the whole studio content on the page. */ +.studio-inner { + margin: 0 auto; + width: 100%; + max-width: var(--studio-inner-max, 1320px); + display: flex; + align-items: flex-start; } -.studio-rail-section-title.is-active::before { - content: ''; +.studio-layout { + position: relative; +} + +/* Visually hidden live region used for the once-per-unlock announcement. */ +.studio-sr-only { position: absolute; - left: -2px; - top: 2px; - width: 6px; - height: 6px; - border-radius: 0px; - background: var(--primary-color); + width: 1px; + height: 1px; + padding: 0; + margin: -1px; + overflow: hidden; + clip: rect(0 0 0 0); + white-space: nowrap; + border: 0; } -.studio-rail-optional-chip { - display: inline-flex; +.global-error-banner { + display: flex; + justify-content: space-between; align-items: center; - margin-left: 6px; - padding: 2px 6px; - border-radius: var(--radius-sm); - font-size: 0.62rem; - font-weight: 700; - text-transform: none; - letter-spacing: 0.04em; - color: var(--primary-color); - background: rgba(43, 51, 233, 0.12); + gap: 10px; + /* Sticky in the flow: clears the fixed header and sub-header (it used to sit behind them) + and pushes the studio down instead of covering the pathology picker. */ + position: sticky; + top: calc(var(--header-height) + var(--studio-sub-header-height) + 12px); + max-width: 1200px; + margin: 12px auto; + box-sizing: border-box; + z-index: 2000; + background: color-mix(in srgb, var(--danger) 12%, white); + border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); + color: #7f1d1d; + border-radius: var(--radius-lg); + padding: 10px 14px; + box-shadow: var(--shadow-md); } -.studio-rail-item { +.pathology-warning-banner { display: flex; - align-items: center; - gap: 12px; + justify-content: space-between; + align-items: flex-start; + gap: 10px; + position: fixed; + /* Below the sub-header, not underneath it: the banner used to start at header+12px, + which put its top 32px behind the sub-header while the page still reserved 96px. */ + top: calc(var(--header-height) + var(--studio-sub-header-height) + 12px); + left: 16px; + right: 16px; + z-index: 2100; + border: 1px solid color-mix(in srgb, var(--accent-color) 35%, transparent); + background: color-mix(in srgb, var(--accent-color) 12%, white); + color: #7c2d12; + box-shadow: none; + margin: 0; + border-radius: var(--radius-lg); + box-sizing: border-box; padding: 12px 14px; - border-radius: 12px; - color: #475569; - text-decoration: none; - font-size: 0.88rem; - font-weight: 600; - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); - margin-bottom: 4px; } -.studio-rail-item:hover { - background: #f1f5f9; - color: var(--primary-color); - transform: translateX(4px); +.section-warning-banner { + display: flex; + justify-content: space-between; + align-items: flex-start; + gap: 10px; + background: #fffdf6; + border: 1px solid #f1e3b1; + color: #68510f; + border-radius: 12px; + padding: 12px 14px; } -.studio-rail-item.is-active { - background: var(--primary-color); - color: #ffffff; - box-shadow: 0 4px 12px rgba(43, 51, 233, 0.2); +.global-error-banner .banner-content { + display: flex; + align-items: center; + gap: 8px; + font-weight: 600; } -.studio-rail-item.is-active .studio-rail-icon { - color: #ffffff; +.pathology-warning-banner .banner-content { + display: flex; + align-items: flex-start; + gap: 8px; } -.studio-rail-item::after { - content: '›'; - position: absolute; - right: 8px; - opacity: 0; - color: currentColor; - transition: opacity 0.18s ease, transform 0.18s ease; - transform: translateX(-2px); +.section-warning-banner .banner-content { + display: flex; + align-items: flex-start; + gap: 8px; } -.studio-rail-item:hover::after { - opacity: 0.45; - transform: translateX(0); +.section-warning-banner .fa-exclamation-triangle { + margin-top: 2px; + color: #ad7d00; } -.studio-rail-footer { - display: flex; - flex-direction: column; - gap: 6px; - padding-top: 12px; - border-top: 1px solid rgba(148, 163, 184, 0.2); +.pathology-warning-banner .fa-exclamation-triangle { + margin-top: 2px; + color: #b45309; } -.studio-rail-scroll { +.warning-copy { display: flex; flex-direction: column; - gap: 14px; - min-height: 0; - flex: 1 1 auto; - overflow-y: auto; - padding-right: 4px; + gap: 4px; } -.studio-rail-footer-title { - font-size: 0.68rem; - text-transform: uppercase; - letter-spacing: 0.12em; - color: rgba(100, 116, 139, 0.9); - font-weight: 700; - padding-left: 6px; +.warning-copy p { + margin: 0; + line-height: 1.5; } -.studio-rail-footer-link { - font-size: 0.74rem; - color: #64748b; +.warning-email { + color: #9a3412; + font-weight: 700; + white-space: nowrap; text-decoration: none; - padding: 4px 6px; - border-radius: 0px; - transition: background 0.18s ease, color 0.18s ease; } -/* Mobile Hidden State */ -@media (max-width: 1200px) { - .studio-main { - padding-left: 0 !important; - } +.warning-email:hover { + text-decoration: underline; } -.global-error-banner { +.warning-content { display: flex; - justify-content: space-between; - align-items: center; - gap: 10px; - position: fixed; - top: 16px; - right: 16px; - left: 16px; - max-width: 1200px; - margin: 0 auto; - z-index: 2000; - background: linear-gradient(135deg, #fee2e2, #fecaca); - border: 1px solid #f87171; - color: #7f1d1d; - border-radius: 0px; - padding: 10px 14px; - margin-top: 3%; - box-shadow: 0 8px 18px rgba(127, 29, 29, 0.12); + flex-direction: column; + gap: 6px; } -.global-error-banner .banner-content { - display: flex; - align-items: center; - gap: 8px; - font-weight: 600; +.warning-title { + font-weight: 700; +} + +.warning-heading { + font-weight: 700; + color: #8c5b00; +} + +.warning-content ul { + margin: 0; + padding-left: 18px; +} + +.warning-content li { + margin: 2px 0; } .banner-close { @@ -343,102 +333,54 @@ line-height: 1; cursor: pointer; padding: 4px 6px; - border-radius: 0px; - transition: background-color 0.15s ease, color 0.15s ease; + border-radius: 8px; } -.banner-close:hover, -.banner-close:focus-visible { +.banner-close:hover { background: rgba(127, 29, 29, 0.08); - outline: none; } -/* Header */ -.studio-float-back { - position: sticky; - top: 28px; - align-self: flex-end; - margin-top: 20px; - z-index: 3; - display: inline-flex; - align-items: center; - gap: 6px; - border: none; - outline: none; - cursor: pointer; - border-radius: 0px; - padding: 8px 14px; - font-size: 0.8rem; - font-weight: 600; - color: #0f172a; - background: rgba(255, 255, 255, 0.95); - box-shadow: 0 10px 22px rgba(15, 23, 42, 0.18); - transition: transform 0.12s ease, box-shadow 0.12s ease, opacity 0.12s ease; -} - -.studio-float-back i { - font-size: 0.78rem; -} - -.studio-float-back:hover { - transform: translateY(-1px); - box-shadow: 0 14px 30px rgba(15, 23, 42, 0.25); - opacity: 0.96; +.banner-close:focus-visible { + background: rgba(127, 29, 29, 0.08); + outline: 2px solid var(--primary-color); + outline-offset: 2px; } -.studio-float-back:disabled { - opacity: 0.7; - cursor: not-allowed; - box-shadow: none; +.section-warning-banner .banner-close:hover, +.section-warning-banner .banner-close:focus-visible { + background: rgba(107, 78, 0, 0.08); } /* Main vertical layout */ .studio-main { display: flex; flex-direction: column; - gap: 24px; + gap: 20px; width: 100%; margin: 0; - padding-left: 220px; - /* Synchronized sidebar offset */ - transition: all 0.25s ease; min-width: 0; - /* Critical for grid item scaling */ } -/* Section wrapper around panels */ +/* Rail-switched views: only the active view is visible; the others stay + mounted but hidden so state is preserved. */ .studio-section { max-width: 100%; margin-bottom: 0; - scroll-margin-top: 90px; + display: none; + scroll-margin-top: calc( + var(--header-height, 64px) + var(--studio-sub-header-height, 44px) + var(--warning-banner-space, 0px) + 16px + ); } -/* Light frame */ -.studio-section--variables, -.studio-section--statistics, -.studio-section--algorithm { - padding: 24px; - border-radius: var(--radius-lg); - background: #ffffff; - border: 1px solid rgba(226, 232, 240, 0.8); - /* Level 3 Technical Shadows */ - box-shadow: - 0 2px 4px rgba(0, 0, 0, 0.01), - 0 12px 24px rgba(15, 23, 42, 0.04), - 0 24px 48px rgba(15, 23, 42, 0.06); - transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1); - transition: all 0.2s ease; - overflow: hidden; - /* For top border accents */ - position: relative; - /* Entrance Animation */ - animation: slideUp 0.5s cubic-bezier(0.16, 1, 0.3, 1) backwards; +.studio-section.is-visible { + display: block; + animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1); } @keyframes slideUp { from { opacity: 0; - transform: translateY(20px); + transform: translateY(12px); } to { @@ -447,297 +389,133 @@ } } -/* Stagger Animations */ -.studio-section--variables { - animation-delay: 0.1s; - border-top: 4px solid var(--primary-color); - margin-top: 48px; -} - +.studio-section--variables, .studio-section--statistics { - animation-delay: 0.2s; - border-top: 4px solid #6366f1; - /* Indigo */ + padding: 16px 20px 20px; + border-radius: var(--radius-lg); + background: #ffffff; + position: relative; + /* Clip to the rounded frame. overflow:visible lets station cards paint outside + the panel when the page scrolls under the stepper, so they look stacked on a + second sheet. clip does not create a nested scroller. */ + overflow: clip; } .studio-section--algorithm { - animation-delay: 0.3s; - border-top: 4px solid var(--primary-color); - /* MIP Blue */ + padding: 0; + background: transparent; + position: relative; } -/* Global full-page overlay loader */ -.global-overlay-loader { - position: fixed; - inset: 0; - background: rgba(15, 23, 42, 0.35); - backdrop-filter: blur(2px); +/* Inline empty state for locked steps */ +.studio-section-empty-state { display: flex; + flex-direction: column; align-items: center; justify-content: center; - z-index: 9999; - pointer-events: all; - animation: studioFadeIn 0.2s ease-out; -} - -@keyframes studioFadeIn { - from { - opacity: 0; - } - - to { - opacity: 1; - } + text-align: center; + gap: 8px; + padding: 64px 24px; + min-height: 320px; } -/* Responsive */ -@media (max-width: 960px) { - .experiment-studio-page { - padding: 16px; - margin-top: 4%; - } - - .studio-inner { - max-width: 100%; - } - - - - .studio-step { - grid-template-columns: 16px auto; - } - - .studio-step::before { - display: none; - } - - .studio-float-back { - align-self: flex-start; - } - - .studio-main { - gap: 12px; - } - - .studio-section--variables, - .studio-section--statistics, - .studio-section--algorithm { - border-radius: 0px; - padding: 6px 4px; - } +/* Overlay empty state: covers a locked step's panel (kept mounted underneath) */ +.studio-section-empty-state--overlay { + position: absolute; + inset: 0; + z-index: 5; + min-height: 0; + padding: 64px 24px; + background: rgba(248, 250, 252, 0.9); + backdrop-filter: var(--glass-blur); + -webkit-backdrop-filter: var(--glass-blur); + border-radius: var(--radius-lg); } -/* STUDIO CONTEXT RIBBON */ -.studio-context-ribbon { - position: sticky; - top: 10px; - z-index: 99; - margin-bottom: 24px; - display: flex; +.studio-section-empty-icon { + width: 64px; + height: 64px; + border-radius: 20px; + display: inline-flex; align-items: center; - gap: 20px; - padding: 10px 24px; - background: rgba(255, 255, 255, 0.7); - backdrop-filter: blur(14px) saturate(140%); - border: 1px solid rgba(255, 255, 255, 0.5); - border-radius: 0px; - box-shadow: 0 8px 32px rgba(15, 23, 42, 0.12); - width: fit-content; - align-self: center; -} - -.ribbon-item { - display: flex; - flex-direction: column; -} - -.ribbon-label { - font-size: 0.62rem; - font-weight: 800; - color: var(--text-muted); - letter-spacing: 0.08em; - text-transform: uppercase; -} - -.ribbon-value { - font-size: 0.85rem; - font-weight: 700; + justify-content: center; + font-size: 1.6rem; color: var(--primary-color); + background: rgba(43, 51, 233, 0.08); + margin-bottom: 8px; } -.ribbon-divider { - width: 1px; - height: 20px; - background: var(--border-color); - opacity: 0.6; -} - -/* SECTION ACCENTS */ -/* .studio-section {} - Moved to active definitions */ - - -/* DATAIKU COCKPIT HEADER */ -.workspace-header { - position: sticky; - top: 16px; - z-index: 1000; - /* Fixed: Removed negative left margin to avoid sidebar conflict */ - margin: -16px 0 24px 0; - /* Adjusted vertical position */ - padding: 10px 24px; - background: rgba(255, 255, 255, 0.9); - border-bottom: 1px solid rgba(15, 23, 42, 0.08); - backdrop-filter: blur(16px) saturate(160%); - border-bottom: 1px solid var(--border-color); - display: flex; - align-items: center; - justify-content: space-between; - box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05); -} - -.workspace-breadcrumbs { - display: flex; - align-items: center; - gap: 12px; -} - -.breadcrumb-root { - font-size: 0.7rem; +.studio-section-empty-state h3 { + margin: 0; + font-size: 1.15rem; font-weight: 800; - color: #64748b; - text-decoration: none; - letter-spacing: 0.1em; - transition: color 0.2s; -} - -.breadcrumb-root:hover { - color: var(--primary-color); -} - -.breadcrumb-sep { - color: #cbd5e1; - font-weight: 300; -} - -.experiment-name-input-group { - display: flex; - align-items: center; - gap: 8px; -} - -.header-name-input { - background: transparent; - border: 1px solid transparent; - font-size: 0.95rem; - font-weight: 700; - color: #020617; - padding: 4px 10px; - border-radius: 0px; - width: 280px; - transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); - -webkit-font-smoothing: antialiased; -} - -.header-name-input:focus { - background: white; - border-color: var(--primary-color); - box-shadow: 0 0 0 4px rgba(2, 128, 128, 0.08); - outline: none; + color: var(--text-main); } -.header-name-input:hover, -.header-name-input:focus { - background: white; - border-color: #cbd5e1; - box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.05); +.studio-section-empty-state p { + margin: 0; + max-width: 420px; + color: var(--text-muted); + font-size: 0.9rem; + line-height: 1.5; } -.header-save-btn { - font-size: 0.65rem; - font-weight: 800; +.studio-section-empty-action { + appearance: none; + border: 1px solid rgba(43, 51, 233, 0.25); + background: rgba(43, 51, 233, 0.08); color: var(--primary-color); - background: #f0f7ff; - border: 1px solid #dbeafe; - padding: 2px 8px; - border-radius: 0px; + font-weight: 700; + font-size: 0.85rem; + padding: 10px 16px; + border-radius: 12px; cursor: pointer; - transition: all 0.2s; -} - -.header-save-btn:hover:not(:disabled) { - background: #ccfbf1; + margin-top: 12px; + transition: background-color 0.2s ease; } -.header-save-btn:disabled { - opacity: 0; - pointer-events: none; -} - -.workspace-telemetry { - display: flex; - align-items: center; - gap: 16px; +.studio-section-empty-action:hover { + background: rgba(43, 51, 233, 0.14); } -.telemetry-pill { - display: flex; - align-items: center; - gap: 8px; - background: rgba(15, 23, 42, 0.03); - padding: 4px 12px; - border-radius: 0px; - border: 1px solid rgba(15, 23, 42, 0.05); +.studio-section--warning { + margin-top: -6px; + display: block; } -.tel-label { - font-size: 0.58rem; - font-weight: 800; - color: #64748b; - letter-spacing: 0.08em; -} +/* Responsive */ +@media (max-width: 960px) { + .experiment-studio-page { + /* Must keep the sub-header in the offset — dropping it tucked the first panel under the bar. */ + padding: calc(var(--studio-sub-header-height) + 12px) var(--studio-page-gutter) 16px; + } -.tel-val { - font-size: 0.7rem; - font-weight: 700; - color: var(--primary-color); -} + .studio-inner { + max-width: 100%; + } -.tel-label-old { - font-weight: 800; - color: #94a3b8; - letter-spacing: 0.1em; -} + .studio-main { + gap: 16px; + } -.tel-val-old { - font-weight: 700; - color: #334155; -} + .studio-section--variables, + .studio-section--statistics { + border-radius: var(--radius-lg, 12px); + padding: 6px 4px; + } -.header-run-btn { - background: var(--primary-color); - color: white; - border: none; - padding: 8px 20px; - border-radius: 0px; - font-size: 0.82rem; - font-weight: 800; - display: flex; - align-items: center; - gap: 10px; - cursor: pointer; - box-shadow: 0 8px 20px rgba(2, 128, 128, 0.25); - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); + .studio-section--warning { + margin-top: 0; + } } -.header-run-btn:hover:not(:disabled) { - transform: translateY(-1px); - background: var(--primary-dark); - box-shadow: 0 12px 24px rgba(2, 128, 128, 0.35); -} +@media (max-width: 768px) { + .experiment-studio-page.has-pathology-warning { + --warning-banner-space: 132px; + } -.header-run-btn:disabled { - background: #e2e8f0; - color: #94a3b8; - box-shadow: none; - cursor: not-allowed; + .pathology-warning-banner { + left: 12px; + right: 12px; + } } diff --git a/src/app/pages/experiment-studio/experiment-studio.component.html b/src/app/pages/experiment-studio/experiment-studio.component.html index 6603b5875..44147606f 100644 --- a/src/app/pages/experiment-studio/experiment-studio.component.html +++ b/src/app/pages/experiment-studio/experiment-studio.component.html @@ -1,4 +1,64 @@ -<div class="experiment-studio-page full-bleed" id="studio-top"> +<div class="experiment-studio-page full-bleed" id="studio-top" + [class.has-pathology-warning]="!!visiblePathologyAccessWarning()"> + + <div class="studio-sub-header"> + <div class="sub-header-inner"> + @if (previousSection(); as prev) { + <button + type="button" + class="sub-header-prev" + [title]="backLabel()" + [attr.aria-label]="backLabel()" + (click)="onBackClick()" + > + <i class="fas fa-chevron-left" aria-hidden="true"></i> + </button> + } @else { + <div class="sub-header-prev-placeholder" aria-hidden="true"></div> + } + + <app-studio-stepper></app-studio-stepper> + + @if (activeSection() !== 'execution-section') { + <button + type="button" + class="sub-header-cta" + [class.is-run]="isAlgorithmActive()" + [disabled]="!canContinue()" + [attr.aria-disabled]="!canContinue()" + [title]="ctaDisabledReason()" + [attr.data-guide]="isAlgorithmActive() ? 'run-experiment' : null" + (click)="onCtaClick()" + > + <span>{{ ctaLabel() }}</span> + @if (isRunning()) { + <i class="fas fa-spinner fa-spin" aria-hidden="true"></i> + } @else if (isAlgorithmActive()) { + <i class="fas fa-play" aria-hidden="true"></i> + } @else { + <i class="fas fa-arrow-right" aria-hidden="true"></i> + } + </button> + } + </div> + </div> + + @if (visiblePathologyAccessWarning(); as warning) { + <div class="pathology-warning-banner" role="status" aria-live="polite" aria-atomic="true"> + <div class="banner-content"> + <i class="fas fa-exclamation-triangle"></i> + <div class="warning-copy"> + <div class="warning-title">{{ warning.title }}</div> + <p> + {{ warning.message }} + Please contact us at + <a class="warning-email" href="mailto:mip@chuv.ch">mip@chuv.ch</a>. + </p> + </div> + </div> + </div> + } + @if (errorMessage()) { <div class="global-error-banner" role="alert" aria-live="assertive" aria-atomic="true"> <div class="banner-content"> @@ -11,76 +71,96 @@ </div> } - - <div class="studio-inner studio-layout"> - <nav class="studio-sidenav" [class.is-collapsed]="sidebarCollapsed()" aria-label="Studio navigation"> - <div class="studio-rail-context"> - <div class="studio-rail-context-title">Experiment Studio</div> - <a class="studio-rail-context-link" routerLink="/experiments-dashboard" - [attr.title]="sidebarCollapsed() ? 'Back to dashboard' : null"> - <i class="fas fa-arrow-left"></i> - <span class="studio-rail-context-text">Back to dashboard</span> - </a> + <div class="studio-main"> + <!-- Visually-hidden live region for the once-per-unlock announcement. --> + <div class="studio-sr-only" aria-live="polite"> + @if (unlockAnnouncement(); as announcement) { + <span>{{ announcement }}</span> + } </div> + <!-- VIEW: Variables --> + <section + class="studio-section studio-section--variables" + [class.is-visible]="activeSection() === 'variables-top'" + [class.is-complete]="isDataReviewReady()" + > + <app-variables-panel></app-variables-panel> + </section> - - <div class="studio-rail-scroll"> - <div class="studio-rail-section"> - <div class="studio-rail-section-title" - [class.is-active]="activeSection() === 'variables-top' || activeSection() === 'data-model-visualization'"> - Studio - </div> - <a routerLink="." [fragment]="'studio-top'" queryParamsHandling="preserve" class="studio-rail-item" - [class.is-active]="activeSection() === 'variables-top'" - [attr.aria-current]="activeSection() === 'variables-top' ? 'page' : null"> - <span class="studio-rail-icon"><i class="fas fa-database"></i></span> - <span class="studio-rail-label">Datasets & Variables</span> - </a> + <!-- VIEW: Statistics --> + <section + class="studio-section studio-section--statistics" + [class.is-visible]="activeSection() === 'statistics-section'" + [class.is-complete]="descriptiveProgress().preprocessingStatus === 'applied'" + > + <app-statistic-analysis-panel (progressStateChange)="descriptiveProgress.set($event)"></app-statistic-analysis-panel> + @if (!isDataReviewReady()) { + <div class="studio-section-empty-state studio-section-empty-state--overlay"> + <div class="studio-section-empty-icon"><i class="fas fa-chart-line"></i></div> + <h3>Data handling</h3> + <p>Select at least one variable to activate data handling.</p> + <button type="button" class="studio-section-empty-action" (click)="switchSection('variables-top')"> + Back to Datasets & Variables + </button> </div> + } + </section> - <div class="studio-rail-section"> - <div class="studio-rail-section-title" - [class.is-active]="activeSection() === 'statistics-section' || activeSection() === 'algorithm-section'"> - Analysis + @if (dataExclusionWarnings().length > 0) { + <section class="studio-section studio-section--warning" role="status" aria-live="polite" aria-atomic="true"> + <div class="section-warning-banner"> + <div class="banner-content"> + <i class="fas fa-exclamation-triangle"></i> + <div class="warning-content"> + <span class="warning-heading">Excluded datasets:</span> + <span> + {{ dataExclusionWarnings().join(', ') }} + </span> + </div> </div> - <a routerLink="." [fragment]="'statistics-section'" queryParamsHandling="preserve" class="studio-rail-item" - [class.is-active]="activeSection() === 'statistics-section'" - [attr.aria-current]="activeSection() === 'statistics-section' ? 'page' : null"> - <span class="studio-rail-icon"><i class="fas fa-chart-line"></i></span> - <span class="studio-rail-label">Descriptive statistics</span> - </a> - <a routerLink="." [fragment]="'algorithm-section'" queryParamsHandling="preserve" class="studio-rail-item" - [class.is-active]="activeSection() === 'algorithm-section'" - [attr.aria-current]="activeSection() === 'algorithm-section' ? 'page' : null"> - <span class="studio-rail-icon"><i class="fas fa-cogs"></i></span> - <span class="studio-rail-label">Algorithm</span> - </a> + <button type="button" class="banner-close" (click)="dismissDataExclusionWarnings()" aria-label="Dismiss warning"> + × + </button> </div> - </div> - - </nav> - - <div class="studio-main" [class.sidebar-collapsed]="sidebarCollapsed()"> - <!-- TOP: Variables --> - <section class="studio-section studio-section--variables"> - <app-variables-panel></app-variables-panel> </section> + } - <section class="studio-section studio-section--statistics" id="statistics-section"> - <app-statistic-analysis-panel></app-statistic-analysis-panel> + <!-- VIEW: Algorithm --> + <section + class="studio-section studio-section--algorithm" + [class.is-visible]="activeSection() === 'algorithm-section'" + [class.is-complete]="!!selectedAlgorithm()" + > + <app-algorithm-panel></app-algorithm-panel> + @if (!isAlgorithmReady()) { + <div class="studio-section-empty-state studio-section-empty-state--overlay"> + <div class="studio-section-empty-icon"><i class="fas fa-microscope"></i></div> + <h3>Algorithm selection & run</h3> + <p>Select at least one variable to activate algorithm configuration.</p> + <button type="button" class="studio-section-empty-action" (click)="switchSection('variables-top')"> + Back to Datasets & Variables + </button> + </div> + } </section> - <!-- BOTTOM: Algorithm --> - <section class="studio-section studio-section--algorithm" id="algorithm-section"> - <app-algorithm-panel></app-algorithm-panel> + <!-- VIEW: Execution --> + <section + class="studio-section studio-section--execution" + [class.is-visible]="activeSection() === 'execution-section'" + [class.is-complete]="!!runResult()" + > + <app-execution-panel + (exportPdf)="algorithmPanel()?.onExportResult($event)" + (exportZip)="algorithmPanel()?.onExportResultZip()" + ></app-execution-panel> </section> </div> </div> - @if (expStudioService.isFilterConfigOpen()) { - <app-filter-config-modal (closeModal)="expStudioService.toggleFilterConfigModal(false)" - [filterLogic]="expStudioService.filterLogic()"></app-filter-config-modal> - } -</div> \ No newline at end of file + <app-experiment-studio-guide + [activateGuideTarget]="activateGuideTarget" + ></app-experiment-studio-guide> +</div> diff --git a/src/app/pages/experiment-studio/experiment-studio.component.spec.ts b/src/app/pages/experiment-studio/experiment-studio.component.spec.ts new file mode 100644 index 000000000..67fb37f46 --- /dev/null +++ b/src/app/pages/experiment-studio/experiment-studio.component.spec.ts @@ -0,0 +1,398 @@ +import type { Mock } from 'vitest'; +import { signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideRouter, Router } from '@angular/router'; +import { of } from 'rxjs'; +import { ErrorService } from '../../services/error.service'; +import { ExperimentStudioService } from '../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../services/experiments-dashboard.service'; +import { AuthService } from '../../services/auth.service'; +import { ExperimentStudioNavigationService } from '../../services/experiment-studio-navigation.service'; +import { ExperimentStudioComponent } from './experiment-studio.component'; + +describe('ExperimentStudioComponent view switching', () => { + let fixture: ComponentFixture<ExperimentStudioComponent>; + let component: ExperimentStudioComponent; + let router: Router; + let navService: ExperimentStudioNavigationService; + let experimentStudioService: { + isRunning: ReturnType<typeof signal<boolean>>; + hasRunStarted: ReturnType<typeof signal<boolean>>; + runResult: ReturnType<typeof signal<any>>; + selectedDataModel: ReturnType<typeof signal<any>>; + selectedDatasets: ReturnType<typeof signal<string[]>>; + selectedAlgorithm: ReturnType<typeof signal<any>>; + selectedVariables: ReturnType<typeof signal<any[]>>; + dataExclusionWarnings: ReturnType<typeof signal<string[]>>; + pathologyAccessWarning: ReturnType<typeof signal<any>>; + clearDataExclusionWarnings: Mock; + setEditingExistingExperiment: Mock; + loadAndCategorizeModels: Mock; + resetStudioState: Mock; + filterLogic: any; + algorithmY: ReturnType<typeof signal<any[]>>; + algorithmX: ReturnType<typeof signal<any[]>>; + availableDatasets: ReturnType<typeof signal<any[]>>; + getDatasetLabelMap: Mock; + }; + + beforeEach(async () => { + experimentStudioService = { + isRunning: signal(false), + hasRunStarted: signal(false), + runResult: signal<any>(null), + selectedDataModel: signal<any>(null), + selectedDatasets: signal<string[]>([]), + selectedAlgorithm: signal<any>(null), + selectedVariables: signal<any[]>([]), + dataExclusionWarnings: signal<string[]>([]), + pathologyAccessWarning: signal<any>(null), + clearDataExclusionWarnings: vi.fn(), + setEditingExistingExperiment: vi.fn(), + loadAndCategorizeModels: vi.fn().mockReturnValue(of([])), + resetStudioState: vi.fn(), + filterLogic: signal<any>(null), + algorithmY: signal<any[]>([]), + algorithmX: signal<any[]>([]), + availableDatasets: signal([]), + getDatasetLabelMap: vi.fn().mockReturnValue({}), + }; + + await TestBed.configureTestingModule({ + imports: [ExperimentStudioComponent], + providers: [ + provideRouter([]), + ExperimentStudioNavigationService, + { provide: ExperimentStudioService, useValue: experimentStudioService }, + { provide: ExperimentsDashboardService, useValue: { getExperiment: vi.fn() } }, + { provide: ErrorService, useValue: { error: signal(null), clearError: vi.fn() } }, + { provide: AuthService, useValue: {} }, + ], + }) + .overrideComponent(ExperimentStudioComponent, { set: { template: '' } }) + .compileComponents(); + + navService = TestBed.inject(ExperimentStudioNavigationService); + fixture = TestBed.createComponent(ExperimentStudioComponent); + component = fixture.componentInstance; + router = TestBed.inject(Router); + fixture.detectChanges(); + }); + + afterEach(() => { + document.querySelectorAll('.studio-section-test-target').forEach((element) => element.remove()); + }); + + it('starts on the variables view', () => { + expect(component.activeSection()).toBe('variables-top'); + }); + + it('publishes state to ExperimentStudioNavigationService on changes', () => { + expect(navService.state().activeSection).toBe('variables-top'); + expect(navService.state().railStatus['statistics-section']).toBe('locked'); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + fixture.detectChanges(); + + expect(navService.state().railStatus['statistics-section']).toBe('available'); + }); + + it('switchSection activates the requested view', () => { + vi.spyOn(router, 'navigate').mockReturnValue(Promise.resolve(true)); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + + component.switchSection('statistics-section'); + + expect(component.activeSection()).toBe('statistics-section'); + }); + + it('switchSection ignores clicks on locked steps (hard lock)', () => { + vi.spyOn(router, 'navigate').mockResolvedValue(true); + + component.switchSection('statistics-section'); + expect(component.activeSection()).toBe('variables-top'); + component.switchSection('algorithm-section'); + expect(component.activeSection()).toBe('variables-top'); + expect(router.navigate).not.toHaveBeenCalled(); + }); + + it('railStatus locks statistics until a variable or covariate is selected', () => { + expect(component.railStatus()['statistics-section']).toBe('locked'); + expect(component.railStatus()['algorithm-section']).toBe('locked'); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + + expect(component.railStatus()['statistics-section']).toBe('available'); + expect(component.railStatus()['algorithm-section']).toBe('available'); + expect(component.railStatus()['variables-top']).toBe('active'); + }); + + it('railStatus only marks a step complete when its work is actually done', () => { + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + + expect(component.railStatus()['variables-top']).toBe('active'); + component.activeSection.set('statistics-section'); + expect(component.railStatus()['variables-top']).toBe('complete'); + + expect(component.railStatus()['statistics-section']).toBe('active'); + component.activeSection.set('variables-top'); + expect(component.railStatus()['statistics-section']).toBe('available'); + component.descriptiveProgress.set({ + pendingChangeCount: 0, + preprocessingStatus: 'applied', + transformationStatusLabel: 'Not defined', + }); + expect(component.railStatus()['statistics-section']).toBe('complete'); + + expect(component.railStatus()['algorithm-section']).toBe('available'); + experimentStudioService.selectedAlgorithm.set({ name: 'logistic' }); + expect(component.railStatus()['algorithm-section']).toBe('complete'); + }); + + it('keeps data review available until the user actually does work', () => { + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + component.activeSection.set('variables-top'); + + expect(component.railStatus()['statistics-section']).toBe('available'); + expect(component.dataReviewWorked()).toBe(false); + + component.descriptiveProgress.set({ + pendingChangeCount: 0, + preprocessingStatus: 'none', + transformationStatusLabel: 'Not defined', + }); + expect(component.dataReviewWorked()).toBe(false); + expect(component.railStatus()['statistics-section']).toBe('available'); + + experimentStudioService.filterLogic.set({ rules: [{ code: 'x' }] }); + expect(component.dataReviewWorked()).toBe(true); + expect(component.railStatus()['statistics-section']).toBe('complete'); + }); + + it('explains why a locked step is locked', () => { + expect(component.hasDatasetContext()).toBe(false); + expect(component.lockedStepReason).toBe('Add a dataset to unlock'); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + expect(component.hasDatasetContext()).toBe(true); + expect(component.railStatus()['statistics-section']).toBe('locked'); + expect(component.lockedStepReason).toBe('Add a variable to unlock'); + }); + + it('activateGuideTarget reveals the view owning a guide section', () => { + component.activateGuideTarget('Explore'); + expect(component.activeSection()).toBe('variables-top'); + + component.activateGuideTarget('Analysis'); + expect(component.activeSection()).toBe('statistics-section'); + + component.activateGuideTarget('Experiment'); + expect(component.activeSection()).toBe('algorithm-section'); + + component.activateGuideTarget('Results'); + expect(component.activeSection()).toBe('execution-section'); + }); + + it('activateGuideTarget opens the matching statistics sub-tab for Analysis steps', () => { + const goToSection = vi.fn(); + const sectionOpen = signal<Record<string, boolean>>({ + raw: false, setup: false, filters: false, processed: false, transformation: false, + }); + (component as any).statisticPanel = signal({ goToSection, sectionOpen }); + + component.activateGuideTarget('Analysis', 'analysis-filtering'); + expect(goToSection).toHaveBeenCalledWith('filters'); + + component.activateGuideTarget('Analysis', 'analysis-raw-statistics'); + expect(goToSection).toHaveBeenCalledWith('raw'); + + component.activateGuideTarget('Analysis', 'analysis-preprocessing'); + expect(goToSection).toHaveBeenCalledWith('setup'); + + component.activateGuideTarget('Analysis', 'analysis-processed-summary'); + expect(goToSection).toHaveBeenCalledWith('processed'); + + component.activateGuideTarget('Explore'); + expect(goToSection.mock.calls.length).toBe(4); + }); + + it('goToDescriptiveStep ignores in-step anchors while statistics is locked', () => { + const goToSection = vi.fn(); + const sectionOpen = signal<Record<string, boolean>>({ + raw: false, setup: false, filters: false, processed: false, transformation: false, + }); + const statisticPanel = { goToSection, sectionOpen }; + (component as any).statisticPanel = signal(statisticPanel); + vi.spyOn(router, 'navigate').mockResolvedValue(true); + + component.goToDescriptiveStep('setup'); + + expect(component.activeSection()).toBe('variables-top'); + expect(statisticPanel.goToSection).not.toHaveBeenCalled(); + expect(router.navigate).not.toHaveBeenCalled(); + }); + + it('goToDescriptiveStep activates the statistics view and scrolls the panel', async () => { + const goToSection = vi.fn(); + const sectionOpen = signal<Record<string, boolean>>({ + raw: false, setup: false, filters: false, processed: false, transformation: false, + }); + const statisticPanel = { goToSection, sectionOpen }; + (component as any).statisticPanel = signal(statisticPanel); + vi.spyOn(router, 'navigate').mockReturnValue(Promise.resolve(true)); + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback): number => { + queueMicrotask(() => callback(0)); + return 0; + }); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + fixture.detectChanges(); + + component.goToDescriptiveStep('processed'); + + expect(component.activeSection()).toBe('statistics-section'); + await Promise.resolve(); + await Promise.resolve(); + expect(statisticPanel.goToSection).toHaveBeenCalledWith('processed'); + }); + + describe('stepper navigation service state publication', () => { + it('publishes the four-step rail state to navService', () => { + fixture.detectChanges(); + expect(navService.state().activeSection).toBe('variables-top'); + expect(navService.state().railStatus['statistics-section']).toBe('locked'); + expect(navService.state().railStatus['execution-section']).toBe('locked'); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + fixture.detectChanges(); + + // Unlocked, not active: the user is still on Data Exploration. + expect(component.railStatus()['statistics-section']).toBe('available'); + expect(navService.state().railStatus['statistics-section']).toBe('available'); + }); + + it('keeps execution locked until a run has started, then completes it on a result', () => { + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + fixture.detectChanges(); + + expect(component.railStatus()['execution-section']).toBe('locked'); + + experimentStudioService.hasRunStarted.set(true); + fixture.detectChanges(); + expect(component.railStatus()['execution-section']).toBe('available'); + + experimentStudioService.runResult.set({ status: 'success' }); + fixture.detectChanges(); + expect(component.railStatus()['execution-section']).toBe('complete'); + }); + + it('refuses to switch to execution before a run', () => { + component.activeSection.set('variables-top'); + + component.switchSection('execution-section'); + + expect(component.activeSection()).toBe('variables-top'); + }); + }); + + describe('sub-header Continue CTA', () => { + it('stays disabled until data review is ready, then advances to statistics', () => { + expect(component.canContinue()).toBe(false); + expect(component.ctaLabel()).toBe('Continue'); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + fixture.detectChanges(); + + expect(component.canContinue()).toBe(true); + expect(component.ctaLabel()).toBe('Continue with 1 variable'); + component.onCtaClick(); + expect(component.activeSection()).toBe('statistics-section'); + expect(component.ctaLabel()).toBe('Continue to Algorithm Selection'); + }); + + it('labels the CTA Run experiment on the algorithm step', () => { + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + experimentStudioService.selectedAlgorithm.set({ name: 'logistic' }); + component.activeSection.set('algorithm-section'); + fixture.detectChanges(); + + expect(component.ctaLabel()).toBe('Run experiment'); + }); + }); + + describe('sub-header Previous Step button', () => { + it('exposes no previous section on the first step (variables-top)', () => { + component.activeSection.set('variables-top'); + fixture.detectChanges(); + + expect(component.previousSection()).toBeNull(); + }); + + it('navigates back from statistics to variables-top on click', () => { + component.activeSection.set('statistics-section'); + fixture.detectChanges(); + + expect(component.previousSection()).toBe('variables-top'); + component.onBackClick(); + expect(component.activeSection()).toBe('variables-top'); + }); + + it('navigates back from algorithm to statistics on click', () => { + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + component.activeSection.set('algorithm-section'); + fixture.detectChanges(); + + expect(component.previousSection()).toBe('statistics-section'); + component.onBackClick(); + expect(component.activeSection()).toBe('statistics-section'); + }); + }); + + describe('unlock announcement', () => { + it('announces once when statistics goes from locked to unlocked', () => { + vi.useFakeTimers(); + try { + fixture.detectChanges(); + expect(component.unlockAnnouncement()).toBeNull(); + + experimentStudioService.selectedDataModel.set({ code: 'm', version: '1' }); + experimentStudioService.selectedDatasets.set(['d']); + experimentStudioService.selectedVariables.set([{ code: 'v' }]); + fixture.detectChanges(); + + expect(component.unlockAnnouncement()).toBe( + 'Data Handling and Algorithm Selection are now available.' + ); + + vi.advanceTimersByTime(1600); + expect(component.unlockAnnouncement()).toBeNull(); + } finally { + vi.useRealTimers(); + } + }); + }); +}); diff --git a/src/app/pages/experiment-studio/experiment-studio.component.ts b/src/app/pages/experiment-studio/experiment-studio.component.ts index b430b0227..b8f0356ac 100644 --- a/src/app/pages/experiment-studio/experiment-studio.component.ts +++ b/src/app/pages/experiment-studio/experiment-studio.component.ts @@ -1,39 +1,57 @@ -import { AfterViewInit, ChangeDetectionStrategy, Component, HostListener, inject, OnDestroy, OnInit, signal, ViewChild } from '@angular/core'; -import { CommonModule } from '@angular/common'; +import { ChangeDetectionStrategy, Component, effect, inject, OnDestroy, OnInit, signal, viewChild, computed } from '@angular/core'; import { VariablesPanelComponent } from './variables-panel/variables-panel.component'; -import { ActivatedRoute, Router, RouterLink } from '@angular/router'; +import { ActivatedRoute, Router } from '@angular/router'; import { ExperimentStudioService } from '../../services/experiment-studio.service'; import { AlgorithmPanelComponent } from './algorithm-panel/algorithm-panel.component'; +import { ExecutionPanelComponent } from './execution-panel/execution-panel.component'; import { AuthService } from '../../services/auth.service'; import { ExperimentsDashboardService } from '../../services/experiments-dashboard.service'; import { ErrorService } from '../../services/error.service'; -import { StatisticAnalysisPanelComponent } from './statistic-analysis-panel/statistic-analysis-panel.component'; +import { StudioStepperComponent } from './stepper/studio-stepper.component'; +import { + DescriptiveProgressState, + StatisticAnalysisPanelComponent, +} from './statistic-analysis-panel/statistic-analysis-panel.component'; +import { countFilterRules } from '../../core/filter-display.utils'; import { Subject, takeUntil } from 'rxjs'; -import { FilterConfigModalComponent } from './variables-panel/filter-config-modal/filter-config-modal.component'; +import { ExperimentStudioGuideComponent } from './guide/experiment-studio-guide.component'; +import { GuideSection } from './guide/experiment-studio-guide.content'; +import { getAnalysisGuideLayout } from './guide/experiment-studio-analysis-guide.util'; +import { + DescriptiveStep, + ExperimentStudioNavigationHost, + ExperimentStudioNavigationService, + ExperimentStudioSection, +} from '../../services/experiment-studio-navigation.service'; + +type StudioSectionId = + | 'variables-top' + | 'statistics-section' + | 'algorithm-section' + | 'execution-section'; +type AlgorithmSubstepKey = 'setup' | 'parameters'; @Component({ selector: 'app-experiment-studio', imports: [ - CommonModule, VariablesPanelComponent, AlgorithmPanelComponent, + ExecutionPanelComponent, StatisticAnalysisPanelComponent, - RouterLink, - FilterConfigModalComponent + ExperimentStudioGuideComponent, + StudioStepperComponent, ], templateUrl: './experiment-studio.component.html', - styleUrls: ['./experiment-studio.component.css'], + styleUrl: './experiment-studio.component.css', changeDetection: ChangeDetectionStrategy.OnPush, - host: { - '(window:resize)': 'onResize()' - } }) -export class ExperimentStudioComponent implements OnInit, OnDestroy, AfterViewInit { +export class ExperimentStudioComponent implements OnInit, OnDestroy { private route = inject(ActivatedRoute); private router = inject(Router); private dashboardService = inject(ExperimentsDashboardService); public auth = inject(AuthService); private errorService = inject(ErrorService); + private studioNavigation = inject(ExperimentStudioNavigationService); // Public service for telemetry/ribbon signals public expStudioService = inject(ExperimentStudioService); @@ -41,53 +59,219 @@ export class ExperimentStudioComponent implements OnInit, OnDestroy, AfterViewIn readonly selectedDataModel = this.expStudioService.selectedDataModel; readonly selectedDatasets = this.expStudioService.selectedDatasets; readonly selectedAlgorithm = this.expStudioService.selectedAlgorithm; - @ViewChild(AlgorithmPanelComponent) algorithmPanel?: AlgorithmPanelComponent; + readonly dataExclusionWarnings = this.expStudioService.dataExclusionWarnings; + readonly pathologyAccessWarning = this.expStudioService.pathologyAccessWarning; + readonly dismissedPathologyWarning = signal(false); + readonly visiblePathologyAccessWarning = computed(() => { + const warning = this.pathologyAccessWarning(); + return warning && !this.dismissedPathologyWarning() ? warning : null; + }); + readonly algorithmPanel = viewChild(AlgorithmPanelComponent); + readonly runResult = this.expStudioService.runResult; + readonly statisticPanel = viewChild(StatisticAnalysisPanelComponent); + + // --- Step views ------------------------------------------------- + activeSection = signal<StudioSectionId>('variables-top'); + readonly hasDatasetContext = computed(() => ( + !!this.selectedDataModel() && this.selectedDatasets().length > 0 + )); + readonly hasAnalysisSelection = computed(() => ( + this.expStudioService.selectedVariables().length > 0 + )); + readonly isDataReviewReady = computed(() => this.hasDatasetContext() && this.hasAnalysisSelection()); + readonly isAlgorithmReady = computed(() => this.isDataReviewReady()); + + readonly isAlgorithmActive = computed(() => this.activeSection() === 'algorithm-section'); + + readonly previousSection = computed<StudioSectionId | null>(() => { + const idx = this.sectionIds.indexOf(this.activeSection()); + return idx > 0 ? this.sectionIds[idx - 1] : null; + }); + + /** Stepper labels are the source for the back affordance, so the two never drift. */ + readonly backLabel = computed(() => { + const prev = this.previousSection(); + if (!prev) return ''; + const labels: Record<StudioSectionId, string> = { + 'variables-top': 'Data Exploration', + 'statistics-section': 'Data Handling', + 'algorithm-section': 'Algorithm Selection', + 'execution-section': 'Execution Results', + }; + return `Back to ${labels[prev]}`; + }); - onRunClick() { - this.algorithmPanel?.onClickRunExp(); - } + readonly ctaLabel = computed(() => { + if (this.isAlgorithmActive()) { + return this.isRunning() ? 'Running…' : 'Run experiment'; + } + if (this.activeSection() === 'variables-top') { + const count = this.expStudioService.selectedVariables().length; + return count > 0 ? `Continue with ${count} variable${count > 1 ? 's' : ''}` : 'Continue'; + } + if (this.activeSection() === 'statistics-section') { + return 'Continue to Algorithm Selection'; + } + return 'Continue'; + }); + + readonly canContinue = computed(() => { + const active = this.activeSection(); + const status = this.railStatus(); + if (this.isRunning()) return false; + if (active === 'variables-top') { + return status['statistics-section'] !== 'locked'; + } + if (active === 'statistics-section') { + return status['algorithm-section'] !== 'locked'; + } + if (active === 'algorithm-section') { + return this.algorithmPanel()?.canRun() ?? false; + } + return false; + }); + + readonly ctaDisabledReason = computed(() => { + if (this.canContinue()) return ''; + if (this.isRunning()) return 'Experiment is running'; + if (this.isAlgorithmActive()) { + return this.algorithmPanel()?.runDisabledReason() ?? 'Select an algorithm to run'; + } + return this.lockedStepReason; + }); - isRunDisabled() { - return this.algorithmPanel?.isRunButtonDisabled() ?? true; + private get unlockWait(): string { + return this.hasDatasetContext() ? 'a variable' : 'a dataset'; } - private destroy$ = new Subject<void>(); - errorMessage = signal(''); - activeSection = signal('variables-top'); - sidebarCollapsed = signal(false); - private sectionObserver?: IntersectionObserver; - - + get lockedStepReason(): string { + return `Add ${this.unlockWait} to unlock`; + } + descriptiveProgress = signal<DescriptiveProgressState>({ + pendingChangeCount: 0, + preprocessingStatus: 'none', + transformationStatusLabel: 'Not defined', + }); + readonly sectionIds: StudioSectionId[] = [ + 'variables-top', + 'statistics-section', + 'algorithm-section', + 'execution-section', + ] as const; + + readonly algorithmSubstep = signal<AlgorithmSubstepKey>('setup'); + + readonly filterRuleCount = computed(() => + countFilterRules(this.expStudioService.filterLogic()) + ); + + readonly dataReviewWorked = computed(() => { + const progress = this.descriptiveProgress(); + return ( + progress.preprocessingStatus === 'applied' || + this.filterRuleCount() > 0 + ); + }); + + readonly railStatus = computed<Record<StudioSectionId, 'active' | 'available' | 'complete' | 'locked'>>(() => { + const active = this.activeSection(); + const variables: 'active' | 'available' | 'complete' = + active === 'variables-top' + ? 'active' + : this.isDataReviewReady() + ? 'complete' + : 'available'; + + const statistics: 'active' | 'available' | 'complete' | 'locked' = + !this.isDataReviewReady() + ? 'locked' + : active === 'statistics-section' + ? 'active' + : this.dataReviewWorked() + ? 'complete' + : 'available'; + + const algorithm: 'active' | 'available' | 'complete' | 'locked' = + !this.isAlgorithmReady() + ? 'locked' + : active === 'algorithm-section' + ? 'active' + : !!this.expStudioService.selectedAlgorithm() && this.dataReviewWorked() + ? 'complete' + : 'available'; + + // Execution is session-scoped: it unlocks the first time a run is dispatched here. + const execution: 'active' | 'available' | 'complete' | 'locked' = + !this.expStudioService.hasRunStarted() + ? 'locked' + : active === 'execution-section' + ? 'active' + : !!this.expStudioService.runResult() + ? 'complete' + : 'available'; + + return { + 'variables-top': variables, + 'statistics-section': statistics, + 'algorithm-section': algorithm, + 'execution-section': execution, + }; + }); + + readonly lockedStepReasons: Partial<Record<StudioSectionId, string>> = { + 'execution-section': 'Run the experiment to unlock', + }; + + readonly unlockAnnouncement = signal<string | null>(null); + private prevLocked = this.railStatus()['statistics-section'] === 'locked'; + private unlockTimer: ReturnType<typeof setTimeout> | undefined; + + constructor() { + // Sync state into the bridge service so the header stepper stays reactive + effect(() => { + this.studioNavigation.publishState({ + activeSection: this.activeSection(), + railStatus: this.railStatus(), + lockedStepReason: this.lockedStepReason, + lockedStepReasons: this.lockedStepReasons, + isRunning: this.isRunning(), + }); + }); - onResize() { - this.checkSidebarCollapse(); + effect(() => { + const locked = this.railStatus()['statistics-section'] === 'locked'; + const wasLocked = this.prevLocked; + this.prevLocked = locked; + if (wasLocked && !locked) { + this.unlockAnnouncement.set('Data Handling and Algorithm Selection are now available.'); + if (this.unlockTimer) clearTimeout(this.unlockTimer); + this.unlockTimer = setTimeout(() => this.unlockAnnouncement.set(null), 1500); + } + }); } - private checkSidebarCollapse() { - const width = window.innerWidth; - if (width >= 1440) { - // Large Desktop: User can toggle, default to expanded - this.sidebarCollapsed.set(false); - } else if (width >= 1200) { - // Medium Screens: Force Icon Rail - this.sidebarCollapsed.set(true); - } else { - // Narrow screens: Fully hidden (handled via CSS), sidebar itself is expanded in drawer - this.sidebarCollapsed.set(false); - } - } + private readonly navigationHost: ExperimentStudioNavigationHost = { + navigateToSection: (sectionId, anchorId) => this.navigateToStudioSection(sectionId, anchorId), + navigateToDescriptiveStep: (step) => this.goToDescriptiveStep(step), + runExperiment: () => this.runCurrentExperiment(), + backToDashboard: () => this.onBackToDashboard(), + }; + + private destroy$ = new Subject<void>(); + errorMessage = computed(() => this.errorService.error() ?? ''); ngOnInit(): void { - this.checkSidebarCollapse(); - // Reset any lingering global errors when arriving on the studio this.errorService.clearError(); + this.dismissedPathologyWarning.set(false); + this.expStudioService.clearDataExclusionWarnings(); + const initialMode = this.route.snapshot.queryParamMap.get('mode'); + const initialExperimentId = this.route.snapshot.queryParamMap.get('experimentId'); + if (initialMode === 'edit' && initialExperimentId) { + this.expStudioService.setEditingExistingExperiment(true); + } this.expStudioService.loadAndCategorizeModels().subscribe(); - this.errorService.error$ - .pipe(takeUntil(this.destroy$)) - .subscribe((msg) => this.errorMessage.set(msg ?? '')); - this.route.queryParamMap .pipe(takeUntil(this.destroy$)) .subscribe((params) => { @@ -95,131 +279,203 @@ export class ExperimentStudioComponent implements OnInit, OnDestroy, AfterViewIn const mode = params.get('mode'); if (mode === 'edit' && experimentId) { - // EDIT MODE this.loadExperimentForEdit(experimentId); + } else if (mode === 'duplicate' && experimentId) { + this.loadExperimentForDuplicate(experimentId); } else { - // CREATE MODE this.initCreateMode(); } }); - } - - ngAfterViewInit(): void { - this.setupSectionObserver(); - this.scrollToHash(this.route.snapshot.fragment); + this.activateFromFragment(this.route.snapshot.fragment); this.route.fragment .pipe(takeUntil(this.destroy$)) - .subscribe((fragment) => this.scrollToHash(fragment)); + .subscribe((fragment) => this.activateFromFragment(fragment)); } ngOnDestroy(): void { - this.sectionObserver?.disconnect(); + if (this.unlockTimer) clearTimeout(this.unlockTimer); + this.studioNavigation.unregister(this.navigationHost); this.destroy$.next(); this.destroy$.complete(); } - + ngAfterViewInit(): void { + this.studioNavigation.register(this.navigationHost); + } dismissError(): void { this.errorService.clearError(); this.expStudioService.loadAndCategorizeModels().subscribe(); - this.errorMessage.set(''); - } - - private setupSectionObserver(): void { - const sectionIds = [ - 'variables-top', - 'data-model-visualization', - 'distribution-graph', - 'parameters-listing', - 'statistics-section', - 'algorithm-section', - ]; - - const observerCallback: IntersectionObserverCallback = (entries) => { - const visible = entries.filter((entry) => entry.isIntersecting); - if (!visible.length) return; - - const inView = visible - .map((entry) => ({ - id: entry.target.id, - top: entry.boundingClientRect.top, - })) - .sort((a, b) => a.top - b.top); - - const firstBelowTop = inView.find((entry) => entry.top >= 0); - this.activeSection.set((firstBelowTop ?? inView[0]).id); - }; + } + + dismissDataExclusionWarnings(): void { + this.expStudioService.clearDataExclusionWarnings(); + } + + dismissPathologyWarning(): void { + this.dismissedPathologyWarning.set(true); + } + + goToDescriptiveStep(section: DescriptiveStep): void { + if (this.railStatus()['statistics-section'] === 'locked') { + return; + } + this.navigateToStudioSection('statistics-section'); + this.scrollToDescriptiveStep(section); + } - // Retry setup for dynamic content - let attempts = 0; - const tryObserve = () => { - const targets = sectionIds - .map((id) => document.getElementById(id)) - .filter((el): el is HTMLElement => !!el); - - if (targets.length === sectionIds.length || attempts > 5) { - if (this.sectionObserver) this.sectionObserver.disconnect(); - - this.sectionObserver = new IntersectionObserver(observerCallback, { - root: null, - threshold: [0.1, 0.2, 0.4], - rootMargin: '0px 0px -60% 0px', - }); - - targets.forEach((el) => this.sectionObserver?.observe(el)); - } else { - attempts++; - setTimeout(tryObserve, 200); + switchSection(sectionId: StudioSectionId): void { + if (this.railStatus()[sectionId] === 'locked') { + return; + } + this.navigateToStudioSection(sectionId); + } + + readonly activateGuideTarget = (view: GuideSection, stepId?: string): void => { + // Explore -> variables, Analysis -> statistics, Experiment -> algorithm setup, + // Results -> execution (result / save-as chrome). Keep these distinct so the + // guide can see zero-size-sensitive targets like [data-guide="experiment-result"]. + const target: StudioSectionId = + view === 'Analysis' + ? 'statistics-section' + : view === 'Results' + ? 'execution-section' + : view === 'Experiment' + ? 'algorithm-section' + : 'variables-top'; + this.activeSection.set(target); + + if (target === 'statistics-section' && stepId) { + const section = this.guideStepToSection(stepId); + if (section) { + this.statisticPanel()?.goToSection(section); } - }; + } + }; - tryObserve(); + private guideStepToSection(stepId: string): DescriptiveStep | null { + const layout = getAnalysisGuideLayout(stepId); + if (!layout || layout.expandSection === 'none') { + return null; + } + return layout.expandSection as DescriptiveStep; + } + + private scrollToDescriptiveStep(section: DescriptiveStep): void { + requestAnimationFrame(() => this.statisticPanel()?.goToSection(section)); } - private scrollToHash(fragment: string | null): void { + private runCurrentExperiment(): void { + if (this.activeSection() !== 'algorithm-section') { + this.navigateToStudioSection('algorithm-section'); + } + this.algorithmPanel()?.onClickRunExp(); + if (this.expStudioService.isRunning()) { + this.navigateToStudioSection('execution-section'); + } + } + + private navigateToStudioSection(sectionId: ExperimentStudioSection, anchorId?: string): void { + this.activeSection.set(sectionId as StudioSectionId); + if (sectionId === 'algorithm-section' && !anchorId) { + const step: AlgorithmSubstepKey = this.selectedAlgorithm() ? 'parameters' : 'setup'; + this.algorithmSubstep.set(step); + queueMicrotask(() => this.algorithmPanel()?.setStudioSubstep(step)); + } + + void this.router.navigate([], { + fragment: sectionId, + queryParamsHandling: 'preserve', + }); + + requestAnimationFrame(() => { + if (anchorId) { + document.getElementById(anchorId)?.scrollIntoView({ behavior: 'smooth', block: 'start' }); + return; + } + window.scrollTo({ top: 0, behavior: 'smooth' }); + }); + } + + private activateFromFragment(fragment: string | null): void { if (!fragment) return; - const target = document.getElementById(fragment); - if (!target) return; + const targetId = fragment === 'studio-top' ? 'variables-top' : fragment; + const section = this.sectionIds.find((id) => id === targetId); + if (!section) return; + if (section === 'execution-section' && !this.expStudioService.hasRunStarted()) return; + + if (this.activeSection() === section) { + return; + } + + this.activeSection.set(section); requestAnimationFrame(() => { - target.scrollIntoView({ behavior: 'smooth', block: 'start' }); + window.scrollTo({ top: 0, behavior: 'smooth' }); }); } - // Clean create mode. private initCreateMode(): void { this.expStudioService.setEditingExistingExperiment(false); } private loadExperimentForEdit(uuid: string): void { + this.expStudioService.setEditingExistingExperiment(true); this.dashboardService.getExperiment(uuid).subscribe({ next: (backendExp) => { - // Prefill Experiment Studio (datasets, domain, variables, filters, algo, params) this.expStudioService.hydrateFromBackendExperiment(backendExp); }, error: (err) => { console.error('Failed to load experiment for edit:', err); - // fallback turns to create mode if something goes wrong this.initCreateMode(); }, }); } + private loadExperimentForDuplicate(uuid: string): void { + this.dashboardService.getExperiment(uuid).subscribe({ + next: (backendExp) => { + this.expStudioService.hydrateFromBackendExperiment(backendExp); + // Keep the configuration, drop the persisted identity and run it as a new experiment. + this.expStudioService.clearCurrentExperimentUUID(); + this.expStudioService.setEditingExistingExperiment(false); + }, + error: (err) => { + console.error('Failed to load experiment for duplication:', err); + this.initCreateMode(); + }, + }); + } + + onBackClick(): void { + const prev = this.previousSection(); + if (prev) { + this.switchSection(prev); + } + } + onBackToDashboard(): void { - // If an experiment is running, ignore the click if (this.isRunning()) { return; } - // Clean up experiment studio state this.expStudioService.resetStudioState(); this.expStudioService.setEditingExistingExperiment(false); this.errorService.clearError(); this.expStudioService.loadAndCategorizeModels().subscribe(); - // Go to experiments dashboard this.router.navigate(['/experiments-dashboard']); } + onCtaClick(): void { + if (!this.canContinue()) return; + const active = this.activeSection(); + if (active === 'variables-top') { + this.switchSection('statistics-section'); + } else if (active === 'statistics-section') { + this.switchSection('algorithm-section'); + } else if (active === 'algorithm-section') { + this.runCurrentExperiment(); + } + } } diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-analysis-guide.util.spec.ts b/src/app/pages/experiment-studio/guide/experiment-studio-analysis-guide.util.spec.ts new file mode 100644 index 000000000..bc3421a25 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-analysis-guide.util.spec.ts @@ -0,0 +1,16 @@ +import { getAnalysisGuideLayout } from './experiment-studio-analysis-guide.util'; + +describe('experiment-studio-analysis-guide.util', () => { + it('opens the matching workflow section for each analysis guide step', () => { + expect(getAnalysisGuideLayout('analysis-intro')?.expandSection).toBe('none'); + expect(getAnalysisGuideLayout('analysis-filtering')?.expandSection).toBe('filters'); + expect(getAnalysisGuideLayout('analysis-raw-statistics')).toEqual({ + expandSection: 'raw', + summaryKind: 'raw', + summaryTab: 'Statistics', + }); + expect(getAnalysisGuideLayout('analysis-preprocessing')?.preprocessingStep).toBe('missing'); + expect(getAnalysisGuideLayout('analysis-processed-summary')?.expandSection).toBe('processed'); + expect(getAnalysisGuideLayout('analysis-transformation')?.expandSection).toBe('transformation'); + }); +}); diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-analysis-guide.util.ts b/src/app/pages/experiment-studio/guide/experiment-studio-analysis-guide.util.ts new file mode 100644 index 000000000..1a9999f6f --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-analysis-guide.util.ts @@ -0,0 +1,24 @@ +type AnalysisWorkflowSection = 'filters' | 'raw' | 'setup' | 'processed' | 'transformation' | 'none'; +type AnalysisSummaryTab = 'Statistics' | 'Charts' | 'Histogram'; +export type AnalysisPreprocessingStep = 'missing' | 'outlier' | 'longitudinal'; + +export interface AnalysisGuideLayout { + expandSection: AnalysisWorkflowSection; + summaryKind?: 'raw' | 'processed'; + summaryTab?: AnalysisSummaryTab; + preprocessingStep?: AnalysisPreprocessingStep; +} + +const ANALYSIS_GUIDE_LAYOUTS: Record<string, AnalysisGuideLayout> = { + 'analysis-intro': { expandSection: 'none' }, + 'analysis-filtering': { expandSection: 'filters' }, + 'analysis-raw-statistics': { expandSection: 'raw', summaryKind: 'raw', summaryTab: 'Statistics' }, + 'analysis-preprocessing': { expandSection: 'setup', preprocessingStep: 'missing' }, + 'analysis-processed-summary': { expandSection: 'processed', summaryKind: 'processed', summaryTab: 'Statistics' }, + 'analysis-transformation': { expandSection: 'transformation' }, +}; + +export function getAnalysisGuideLayout(stepId: string | null): AnalysisGuideLayout | null { + if (!stepId) return null; + return ANALYSIS_GUIDE_LAYOUTS[stepId] ?? null; +} diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide-mask.util.spec.ts b/src/app/pages/experiment-studio/guide/experiment-studio-guide-mask.util.spec.ts new file mode 100644 index 000000000..178583a75 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide-mask.util.spec.ts @@ -0,0 +1,74 @@ +import { computeGuideBlockingRects, measureGuideInteractionRect } from './experiment-studio-guide-mask.util'; + +describe('computeGuideBlockingRects', () => { + it('returns blockers around a single interaction hole without covering it', () => { + const hole = { top: 100, left: 80, right: 620, bottom: 640, width: 540, height: 540 }; + const blockers = computeGuideBlockingRects([hole], { right: 1200, bottom: 800 }); + + expect(blockers.length).toBeGreaterThan(0); + expect(blockers.some((rect) => rect.top === 0 && rect.bottom === hole.top)).toBe(true); + expect(blockers.some((rect) => rect.top === hole.bottom && rect.bottom === 800)).toBe(true); + expect(blockers.every((rect) => + rect.right <= hole.left + || rect.left >= hole.right + || rect.bottom <= hole.top + || rect.top >= hole.bottom + )).toBe(true); + }); + + it('keeps separate holes open for multi-target interaction steps', () => { + const browser = { top: 120, left: 80, right: 620, bottom: 640, width: 540, height: 520 }; + const details = { top: 360, left: 660, right: 1180, bottom: 760, width: 520, height: 400 }; + const blockers = computeGuideBlockingRects([browser, details], { right: 1200, bottom: 800 }); + + const intersects = (rect: { left: number; right: number; top: number; bottom: number }) => + blockers.some((blocker) => + blocker.left < rect.right + && blocker.right > rect.left + && blocker.top < rect.bottom + && blocker.bottom > rect.top + ); + + expect(intersects(browser)).toBe(false); + expect(intersects(details)).toBe(false); + expect(blockers.some((rect) => rect.left === 620 && rect.right === 660)).toBe(true); + }); + + it('expands interaction holes to include visible search suggestions', () => { + const host = document.createElement('section'); + const search = document.createElement('div'); + search.className = 'search-container'; + const suggestions = document.createElement('div'); + suggestions.className = 'suggestions-container'; + host.appendChild(search); + host.appendChild(suggestions); + + vi.spyOn(host, 'getBoundingClientRect').mockReturnValue({ + top: 120, + right: 620, + bottom: 220, + left: 80, + width: 540, + height: 100, + x: 80, + y: 120, + toJSON: () => ({}), + } as DOMRect); + vi.spyOn(suggestions, 'getBoundingClientRect').mockReturnValue({ + top: 228, + right: 620, + bottom: 420, + left: 80, + width: 540, + height: 192, + x: 80, + y: 228, + toJSON: () => ({}), + } as DOMRect); + + const measured = measureGuideInteractionRect(host); + + expect(measured.top).toBe(110); + expect(measured.bottom).toBe(430); + }); +}); diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide-mask.util.ts b/src/app/pages/experiment-studio/guide/experiment-studio-guide-mask.util.ts new file mode 100644 index 000000000..8dce8d22c --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide-mask.util.ts @@ -0,0 +1,121 @@ +export interface GuideMaskRect { + top: number; + left: number; + width: number; + height: number; + right: number; + bottom: number; +} + +function expandGuideMaskRect(rect: Pick<GuideMaskRect, 'top' | 'left' | 'right' | 'bottom'>, padding = 10): GuideMaskRect { + const left = rect.left - padding; + const top = rect.top - padding; + const right = rect.right + padding; + const bottom = rect.bottom + padding; + + return { + top, + left, + right, + bottom, + width: Math.max(right - left, 0), + height: Math.max(bottom - top, 0), + }; +} + +function unionDomRects(rects: Array<Pick<DOMRect, 'top' | 'left' | 'right' | 'bottom'>>): DOMRect { + const left = Math.min(...rects.map((rect) => rect.left)); + const top = Math.min(...rects.map((rect) => rect.top)); + const right = Math.max(...rects.map((rect) => rect.right)); + const bottom = Math.max(...rects.map((rect) => rect.bottom)); + + return { + x: left, + y: top, + left, + top, + right, + bottom, + width: Math.max(right - left, 0), + height: Math.max(bottom - top, 0), + toJSON: () => ({}), + } as DOMRect; +} + +export function measureGuideInteractionRect(element: HTMLElement, padding = 10): GuideMaskRect { + const rects: Array<Pick<DOMRect, 'top' | 'left' | 'right' | 'bottom'>> = [ + element.getBoundingClientRect(), + ]; + + element.querySelectorAll('.suggestions-container').forEach((node) => { + if (!(node instanceof HTMLElement)) { + return; + } + + const rect = node.getBoundingClientRect(); + if (rect.width > 0 && rect.height > 0) { + rects.push(rect); + } + }); + + return expandGuideMaskRect(unionDomRects(rects), padding); +} + +export function computeGuideBlockingRects( + holes: GuideMaskRect[], + viewport: Pick<GuideMaskRect, 'right' | 'bottom'> = { + right: typeof window !== 'undefined' ? window.innerWidth : 0, + bottom: typeof window !== 'undefined' ? window.innerHeight : 0, + } +): GuideMaskRect[] { + if (!holes.length || viewport.right <= 0 || viewport.bottom <= 0) { + return []; + } + + const xBreaks = Array.from(new Set([ + 0, + viewport.right, + ...holes.flatMap((hole) => [hole.left, hole.right]), + ])).sort((a, b) => a - b); + + const yBreaks = Array.from(new Set([ + 0, + viewport.bottom, + ...holes.flatMap((hole) => [hole.top, hole.bottom]), + ])).sort((a, b) => a - b); + + const blockers: GuideMaskRect[] = []; + + for (let row = 0; row < yBreaks.length - 1; row += 1) { + for (let column = 0; column < xBreaks.length - 1; column += 1) { + const cell = { + left: xBreaks[column], + top: yBreaks[row], + right: xBreaks[column + 1], + bottom: yBreaks[row + 1], + }; + + const insideHole = holes.some((hole) => + cell.left >= hole.left + && cell.right <= hole.right + && cell.top >= hole.top + && cell.bottom <= hole.bottom + ); + + if (insideHole) { + continue; + } + + blockers.push({ + top: cell.top, + left: cell.left, + width: cell.right - cell.left, + height: cell.bottom - cell.top, + right: cell.right, + bottom: cell.bottom, + }); + } + } + + return blockers; +} diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide-state.service.spec.ts b/src/app/pages/experiment-studio/guide/experiment-studio-guide-state.service.spec.ts new file mode 100644 index 000000000..aa08839b5 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide-state.service.spec.ts @@ -0,0 +1,67 @@ +import { TestBed } from '@angular/core/testing'; +import { ExperimentStudioGuideStateService } from './experiment-studio-guide-state.service'; + +type RuntimeWindow = Window & { + __env?: { + GUIDE_COVARIATE?: unknown; + GUIDE_VARIABLE?: unknown; + }; +}; + +describe('ExperimentStudioGuideStateService', () => { + let service: ExperimentStudioGuideStateService; + let originalEnv: RuntimeWindow['__env']; + + beforeEach(() => { + originalEnv = (window as RuntimeWindow).__env; + TestBed.configureTestingModule({ + providers: [ExperimentStudioGuideStateService], + }); + }); + + afterEach(() => { + if (originalEnv === undefined) { + delete (window as RuntimeWindow).__env; + return; + } + + (window as RuntimeWindow).__env = originalEnv; + }); + + it('matches the intended sex tutorial variable by exact tutorial aliases', () => { + service = TestBed.inject(ExperimentStudioGuideStateService); + + expect(service.matchesTutorialCovariate({ code: 'sex', label: 'Sex' }, 'sex')).toBe(true); + expect(service.matchesTutorialCovariate({ code: 'biol_sex', label: 'Biological Sex' }, 'sex')).toBe(true); + }); + + it('does not match unrelated variables that only contain the target as a substring', () => { + service = TestBed.inject(ExperimentStudioGuideStateService); + + expect(service.matchesTutorialCovariate({ code: 'timemetrics', label: 'Timemetrics' }, 'sex')).toBe(false); + expect(service.matchesTutorialCovariate({ code: 'sextraction_flag', label: 'Sextraction Flag' }, 'sex')).toBe(false); + }); + + it('maps the add-age-variable step to the Age tutorial target', () => { + service = TestBed.inject(ExperimentStudioGuideStateService); + service.setActiveStep('add-age-variable'); + + expect(service.expectedTutorialCovariate()).toBe('Age'); + }); + + it('uses the runtime-configured guide covariate and variable targets', () => { + (window as RuntimeWindow).__env = { + GUIDE_COVARIATE: 'biol_sex', + GUIDE_VARIABLE: 'age_years', + }; + service = TestBed.inject(ExperimentStudioGuideStateService); + + service.setActiveStep('add-sex-covariate'); + expect(service.expectedTutorialCovariate()).toBe('biol_sex'); + expect(service.matchesTutorialCovariate({ code: 'biol_sex', label: 'Biological Sex' })).toBe(true); + + service.setActiveStep('add-age-variable'); + expect(service.expectedTutorialCovariate()).toBe('age_years'); + expect(service.matchesTutorialCovariate({ code: 'age_years', label: 'Age Years' })).toBe(true); + }); +}); diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide-state.service.ts b/src/app/pages/experiment-studio/guide/experiment-studio-guide-state.service.ts new file mode 100644 index 000000000..9ec3ee45e --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide-state.service.ts @@ -0,0 +1,69 @@ +import { Injectable, computed, inject, signal } from '@angular/core'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; + +@Injectable({ providedIn: 'root' }) +export class ExperimentStudioGuideStateService { + private readonly runtimeEnv = inject(RuntimeEnvService); + private readonly activeStepIdSignal = signal<string | null>(null); + private readonly selectedHierarchyNodeSignal = signal<{ code?: unknown; label?: unknown; name?: unknown } | null>(null); + + readonly activeStepId = this.activeStepIdSignal.asReadonly(); + readonly selectedHierarchyNode = this.selectedHierarchyNodeSignal.asReadonly(); + readonly guideCovariate = this.runtimeEnv.guideCovariate; + readonly guideVariable = this.runtimeEnv.guideVariable; + readonly expectedTutorialCovariate = computed(() => { + switch (this.activeStepIdSignal()) { + case 'select-sex-variable': + case 'add-sex-covariate': + return this.guideCovariate.value; + case 'select-age-variable': + case 'add-age-variable': + return this.guideVariable.value; + default: + return null; + } + }); + + setActiveStep(stepId: string | null): void { + this.activeStepIdSignal.set(stepId); + } + + setSelectedHierarchyNode(node: { code?: unknown; label?: unknown; name?: unknown } | null): void { + this.selectedHierarchyNodeSignal.set(node); + } + + matchesTutorialCovariate(node: { code?: unknown; label?: unknown; name?: unknown } | null | undefined, expected?: string | null): boolean { + const target = expected ?? this.expectedTutorialCovariate(); + if (!node || !target) { + return false; + } + + const aliases = this.getTutorialAliases(target); + const candidates = [node.code, node.label, node.name] + .map((value) => this.normalize(value)) + .filter((value): value is string => !!value); + + return candidates.some((value) => aliases.has(value)); + } + + private getTutorialAliases(target: string): Set<string> { + switch (this.normalize(target)) { + case 'sex': + case 'biolsex': + case 'biologicalsex': + case 'sexatbirth': + return new Set(['sex', 'biolsex', 'biologicalsex', 'sexatbirth']); + case 'age': + return new Set(['age']); + default: + return new Set([this.normalize(target)]); + } + } + + private normalize(value: unknown): string { + return String(value ?? '') + .trim() + .toLowerCase() + .replace(/[^a-z0-9]+/g, ''); + } +} diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.css b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.css new file mode 100644 index 000000000..388db69b1 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.css @@ -0,0 +1,576 @@ +:host { + --guide-accent: var(--primary-color); + --guide-accent-secondary: var(--primary-light); + --guide-accent-soft: rgba(223, 239, 228, 0.78); + --guide-card-bg: rgba(255, 255, 255, 0.86); + --guide-card-surface: rgba(244, 247, 255, 0.74); + --guide-highlight-glow: rgba(43, 51, 233, 0.24); + --guide-overlay: rgba(2, 6, 23, 0.65); + --guide-ink: var(--text-main); + --guide-muted: var(--text-muted); + --guide-border: rgba(127, 156, 232, 0.2); + /* Above app-header (10000) and studio sub-header (9990) so chrome is dimmed and + header targets (account, launcher, step bar) can receive the spotlight cutout. */ + position: relative; + z-index: 11000; +} + +/* The launcher control lives in the app header (see GuideLauncherService). It used to be + a fixed circle at z 10001; now the open guide itself outranks the chrome. */ + +.guide-overlay { + position: fixed; + inset: 0; + z-index: 11002; + pointer-events: none; +} + +.guide-mask { + position: fixed; + background: transparent; + pointer-events: auto; +} + +.guide-mask--full { + inset: 0; +} + +/* These four are used only by the dashboard tour, which @imports this sheet. They are + not dead just because the Studio tour itself now uses --blocker instead. */ +.guide-mask--top { + top: 0; + left: 0; + right: 0; +} + +.guide-mask--left { + left: 0; +} + +.guide-mask--right { + right: 0; +} + +.guide-mask--bottom { + left: 0; + right: 0; + bottom: 0; +} + +.guide-mask--blocker { + top: 0; + left: 0; +} + +.guide-highlight { + position: fixed; + border-radius: 12px; + border: 2px solid rgba(255, 255, 255, 0.88); + box-shadow: + 0 12px 36px rgba(15, 23, 42, 0.22), + 0 0 0 9999px var(--guide-overlay); + pointer-events: none; + transition: + top 0.22s ease, + left 0.22s ease, + width 0.22s ease, + height 0.22s ease; +} + +.guide-highlight::before { + content: ''; + position: absolute; + inset: -7px; + border-radius: inherit; + border: 1px solid rgba(127, 156, 232, 0.58); + box-shadow: + 0 0 0 4px rgba(43, 51, 233, 0.28), + 0 0 26px var(--guide-highlight-glow); +} + +.guide-highlight--interactive { + box-shadow: none; +} + +.guide-banner { + position: fixed; + z-index: 11010; + left: 50%; + bottom: max(16px, calc(env(safe-area-inset-bottom) + 8px)); + transform: translateX(-50%); + width: min(1180px, calc(100vw - 32px)); + display: flex; + flex-direction: column; + gap: 14px; + padding: 24px 32px; + box-sizing: border-box; + border-radius: 16px; + background: + linear-gradient(145deg, rgba(255, 255, 255, 0.9) 0%, var(--guide-card-surface) 100%), + var(--guide-card-bg); + border: 1px solid var(--guide-border); + box-shadow: + 0 24px 54px rgba(15, 23, 42, 0.18), + 0 0 0 1px rgba(255, 255, 255, 0.72) inset, + 0 8px 24px rgba(127, 156, 232, 0.16); + backdrop-filter: var(--glass-blur); + -webkit-backdrop-filter: var(--glass-blur); + color: var(--guide-ink); + pointer-events: auto; +} + +.guide-banner--collapsed { + padding: 14px 16px; + border-radius: 14px; +} + +.guide-banner:focus-visible { + outline: 3px solid rgba(43, 51, 233, 0.35); + outline-offset: 3px; +} + +.guide-banner-top, +.guide-banner-body { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; +} + +.guide-banner-meta { + display: flex; + align-items: center; + gap: 8px 10px; + flex-wrap: wrap; + flex: 1 1 auto; + min-width: 0; +} + +.guide-section-pill { + display: inline-flex; + align-items: center; + padding: 6px 12px; + border-radius: 999px; + background: linear-gradient(135deg, rgba(223, 239, 228, 0.92) 0%, rgba(255, 255, 255, 0.8) 100%); + border: 1px solid rgba(127, 156, 232, 0.22); + color: var(--guide-accent); + font-size: 0.72rem; + font-weight: 800; + text-transform: uppercase; + letter-spacing: 0.12em; +} + +.guide-step-count { + color: rgba(15, 23, 42, 0.58); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.12em; + text-transform: uppercase; +} + +.guide-step-count--compact { + font-size: 0.78rem; + letter-spacing: 0.08em; +} + +.guide-banner-utilities { + display: inline-flex; + align-items: center; + gap: 8px; + flex-shrink: 0; +} + +.guide-progress { + height: 4px; + border-radius: 999px; + overflow: hidden; + background: rgba(127, 156, 232, 0.15); + margin-bottom: 16px; +} + +/* The fill carries a scaleX: animating width re-laid out the whole banner on every + tour step. The ratio arrives as --guide-progress from the template. */ +.guide-progress-bar { + height: 100%; + border-radius: inherit; + background: linear-gradient(90deg, var(--guide-accent) 0%, var(--guide-accent-secondary) 100%); + transform-origin: left center; + transform: scaleX(var(--guide-progress, 0)); + transition: transform 0.35s var(--ease-standard); +} + +.guide-banner-copy { + display: flex; + flex-direction: column; + gap: 10px; + min-width: 0; + flex: 1 1 auto; +} + +.guide-banner-copy h3, +.guide-step-title { + margin: 0; + color: var(--guide-ink); +} + +.guide-banner-copy h3 { + font-size: 1.22rem; + line-height: 1.2; + letter-spacing: -0.02em; +} + +.guide-banner-copy .guide-title--compact, +.guide-step-title--compact { + font-size: 1rem; + line-height: 1.42; + letter-spacing: -0.01em; + font-weight: 700; +} + +.guide-banner-copy p { + margin: 0; + max-width: 680px; + color: rgba(71, 85, 105, 0.92); + line-height: 1.62; +} + +:host ::ng-deep .guide-copy-green { + color: #22c55e !important; + font-weight: 800; +} + +.guide-requirement { + margin: 2px 0 0; + max-width: 680px; + padding: 10px 14px; + border-radius: 12px; + background: rgba(43, 51, 233, 0.08); + color: rgba(43, 51, 233, 0.84) !important; + font-weight: 600; + line-height: 1.55; +} + +.guide-warning { + margin: 10px 0 0; + max-width: 680px; + padding: 10px 14px; + border-radius: 12px; + border: 1px solid rgba(245, 158, 11, 0.35); + background: rgba(254, 243, 199, 0.72); + color: #92400e !important; + font-weight: 600; + line-height: 1.55; +} + +.guide-banner-actions { + display: inline-flex; + align-items: center; + justify-content: flex-end; + gap: 10px; + flex-shrink: 0; + margin-left: auto; +} + +.guide-collapse, +.guide-close, +.guide-secondary, +.guide-primary, +.guide-nav-icon { + border: 0; + font: inherit; + cursor: pointer; + transition: + background-color 0.16s ease, + color 0.16s ease, + border-color 0.16s ease, + box-shadow 0.16s ease, + opacity 0.16s ease; +} + +.guide-collapse { + padding: 8px 12px; + border-radius: 999px; + background: rgba(255, 255, 255, 0.5); + border: 1px solid rgba(127, 156, 232, 0.18); + color: var(--guide-accent); + font-weight: 700; + box-shadow: 0 10px 18px rgba(127, 156, 232, 0.08); +} + +.guide-collapse:hover, +.guide-collapse:focus-visible { + background: rgba(255, 255, 255, 0.72); + border-color: rgba(43, 51, 233, 0.24); +} + +.guide-close { + background: transparent; + color: var(--guide-muted); + font-weight: 700; +} + +.guide-close:hover, +.guide-close:focus-visible { + color: var(--guide-ink); +} + +.guide-close-icon, +.guide-collapse-icon { + display: inline-flex; + align-items: center; + justify-content: center; + width: 34px; + height: 34px; + padding: 0; + border-radius: 999px; +} + +.guide-window-control { + display: inline-flex; + align-items: center; + justify-content: center; + width: 36px; + height: 36px; + padding: 0; + border-radius: 999px; + background: rgba(255, 255, 255, 0.42); + box-shadow: none; +} + +.guide-window-control i { + font-size: 0.9rem; +} + +.guide-close-icon { + background: rgba(255, 255, 255, 0.42); +} + +.guide-collapse-icon { + background: rgba(255, 255, 255, 0.5); + box-shadow: none; +} + +.guide-close-icon i { + font-size: 0.9rem; +} + +.guide-collapse-icon i { + font-size: 0.88rem; +} + +.guide-close-icon:hover, +.guide-close-icon:focus-visible, +.guide-collapse-icon:hover, +.guide-collapse-icon:focus-visible, +.guide-window-control:hover, +.guide-window-control:focus-visible { + background: rgba(43, 51, 233, 0.08); + color: var(--guide-accent); +} + +.guide-secondary, +.guide-primary { + min-width: 104px; + padding: 11px 16px; + border-radius: 14px; + border: 1px solid transparent; + font-weight: 700; +} + +.guide-secondary { + background: rgba(255, 255, 255, 0.62); + border-color: rgba(148, 163, 184, 0.28); + color: var(--guide-ink); +} + +.guide-secondary:hover:not(:disabled), +.guide-secondary:focus-visible:not(:disabled) { + border-color: rgba(43, 51, 233, 0.28); +} + +.guide-secondary:disabled { + cursor: not-allowed; + opacity: 0.46; +} + +.guide-primary { + background: var(--guide-accent); + color: #ffffff; + box-shadow: 0 8px 20px rgba(43, 51, 233, 0.25); +} + +.guide-primary:hover:not(:disabled), +.guide-primary:focus-visible:not(:disabled) { + box-shadow: 0 12px 26px rgba(43, 51, 233, 0.3); +} + +.guide-primary:disabled { + background: rgba(148, 163, 184, 0.2); + border-color: rgba(148, 163, 184, 0.24); + color: var(--guide-muted); + cursor: not-allowed; + opacity: 1; + box-shadow: none; +} + +.guide-nav-icon { + display: inline-flex; + align-items: center; + justify-content: center; + width: 34px; + height: 34px; + padding: 0; + border: 1px solid rgba(148, 163, 184, 0.35); + border-radius: 999px; + background: rgba(255, 255, 255, 0.62); + color: var(--guide-muted); +} + +.guide-nav-icon:hover, +.guide-nav-icon:focus-visible { + border-color: rgba(43, 51, 233, 0.28); + color: var(--guide-accent); +} + +.guide-nav-icon--primary { + background: rgba(43, 51, 233, 0.12); + color: var(--guide-accent); + border-color: rgba(43, 51, 233, 0.2); +} + +.guide-nav-icon:disabled, +.guide-nav-icon--primary:disabled { + cursor: not-allowed; + opacity: 0.45; +} + +.guide-banner-compact { + display: grid; + grid-template-columns: auto minmax(0, 1fr) auto auto; + grid-template-areas: 'meta copy nav controls'; + align-items: center; + gap: 14px; +} + +.guide-banner-compact .guide-banner-meta { + grid-area: meta; + flex-wrap: nowrap; +} + +.guide-banner-compact .guide-step-count { + white-space: nowrap; +} + +.guide-banner-compact-copy { + grid-area: copy; + min-width: 0; +} + +.guide-banner-compact-copy .guide-step-title { + font-size: 0.98rem; + line-height: 1.38; +} + +.guide-banner-compact-nav { + grid-area: nav; + display: inline-flex; + align-items: center; + gap: 8px; + justify-self: end; +} + +.guide-banner-compact .guide-banner-utilities { + grid-area: controls; + justify-self: end; +} + +@media (prefers-reduced-motion: reduce) { + .guide-highlight, + .guide-progress-bar, + .guide-collapse, + .guide-close, + .guide-secondary, + .guide-primary, + .guide-nav-icon { + transition: none; + } +} + +@media (max-width: 960px) { + .guide-banner { + width: calc(100vw - 24px); + padding: 22px 24px; + } + + .guide-banner--collapsed { + padding: 13px 14px; + } + + .guide-banner-body { + flex-direction: column; + align-items: stretch; + } + + .guide-banner-actions { + justify-content: flex-start; + } + + .guide-banner-compact { + grid-template-columns: minmax(0, 1fr) auto; + grid-template-areas: + 'meta controls' + 'copy nav'; + align-items: end; + } +} + +@media (max-width: 720px) { + .guide-banner { + width: calc(100vw - 20px); + bottom: max(10px, calc(env(safe-area-inset-bottom) + 6px)); + gap: 12px; + padding: 18px 18px 20px; + border-radius: 16px; + } + + .guide-banner--collapsed { + padding: 12px 12px 14px; + border-radius: 14px; + } + + .guide-banner-top { + flex-direction: column; + align-items: stretch; + } + + .guide-banner-utilities { + justify-content: flex-end; + } + + .guide-banner-copy h3 { + font-size: 1.08rem; + } + + .guide-banner-compact { + grid-template-columns: minmax(0, 1fr) auto; + grid-template-areas: + 'meta controls' + 'copy copy' + 'nav nav'; + align-items: start; + } + + .guide-banner-compact-nav { + justify-self: start; + } + + .guide-secondary, + .guide-primary { + min-width: 0; + flex: 1 1 0; + } + + .guide-banner-actions { + width: 100%; + } +} diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.html b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.html new file mode 100644 index 000000000..ac18625c8 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.html @@ -0,0 +1,119 @@ +@if (isOpen() && currentStep(); as step) { +<div class="guide-overlay" aria-hidden="false"> + @if (highlightRect(); as rect) { + @if (step.allowTargetInteraction && blockingMaskRects().length) { + @for (blocker of blockingMaskRects(); track $index) { + <div class="guide-mask guide-mask--blocker" [style.top.px]="blocker.top" [style.left.px]="blocker.left" + [style.width.px]="blocker.width" [style.height.px]="blocker.height"></div> + } + } @else if (!step.allowTargetInteraction) { + <div class="guide-mask guide-mask--full" [style.background]="'transparent'"></div> + } + <div class="guide-highlight" [class.guide-highlight--interactive]="step.allowTargetInteraction" + [style.top.px]="rect.top" [style.left.px]="rect.left" [style.width.px]="rect.width" + [style.height.px]="rect.height"></div> + } @else if (!step.allowTargetInteraction) { + <div class="guide-mask guide-mask--full" [style.background]="'rgba(2, 6, 23, 0.65)'"></div> + } + + <section + #guideCard + class="guide-banner" + role="dialog" + aria-labelledby="experiment-studio-guide-title" + [class.guide-banner--collapsed]="isCollapsed()" + tabindex="-1"> + @if (isCollapsed()) { + <div class="guide-banner-compact"> + <div class="guide-banner-meta"> + <span class="guide-section-pill">{{ step.section }}</span> + <span class="guide-step-count guide-step-count--compact">{{ currentStepNumber() }} / {{ totalSteps() }}</span> + </div> + + <div class="guide-banner-compact-copy"> + <h3 + id="experiment-studio-guide-title" + class="guide-step-title" + [class.guide-step-title--compact]="step.compactTitle" + [innerHTML]="step.title"></h3> + </div> + + @if (step.id !== 'no-pathology-access') { + <div class="guide-banner-compact-nav"> + <button type="button" class="guide-nav-icon" [disabled]="!canGoToPrevious()" (click)="goToPreviousStep()" + [attr.aria-label]="labels.back" [attr.title]="labels.back"> + <i class="fas fa-chevron-left" aria-hidden="true"></i> + </button> + <button type="button" class="guide-nav-icon guide-nav-icon--primary" [disabled]="!canGoToNext()" + (click)="goToNextStep()" [attr.aria-label]="nextButtonLabel()" + [attr.title]="nextButtonLabel()"> + <i class="fas fa-chevron-right" aria-hidden="true"></i> + </button> + </div> + } + + <div class="guide-banner-utilities"> + <button type="button" class="guide-collapse guide-collapse-icon" (click)="toggleCollapsed()" + [attr.aria-label]="labels.expand" [attr.title]="labels.expand"> + <i class="fas fa-expand-alt" aria-hidden="true"></i> + </button> + <button type="button" class="guide-close guide-close-icon" (click)="closeGuide()" + [attr.aria-label]="labels.exit" [attr.title]="labels.exit"> + <i class="fas fa-times" aria-hidden="true"></i> + </button> + </div> + </div> + } @else { + <div class="guide-banner-top"> + <div class="guide-banner-meta"> + <span class="guide-section-pill">{{ step.section }}</span> + <span class="guide-step-count">Step {{ currentStepNumber() }} of {{ totalSteps() }}</span> + </div> + <div class="guide-banner-utilities"> + <button type="button" class="guide-collapse guide-window-control" (click)="toggleCollapsed()" + [attr.aria-label]="labels.collapse" [attr.title]="labels.collapse"> + <i class="fas fa-compress-alt" aria-hidden="true"></i> + </button> + <button type="button" class="guide-close guide-window-control" (click)="closeGuide()" + [attr.aria-label]="labels.exit" [attr.title]="labels.exit"> + <i class="fas fa-times" aria-hidden="true"></i> + </button> + </div> + </div> + + <div class="guide-progress" aria-hidden="true"> + <div class="guide-progress-bar" [style.--guide-progress]="progressPercent() / 100"></div> + </div> + + <div class="guide-banner-body"> + <div class="guide-banner-copy"> + <h3 + id="experiment-studio-guide-title" + [class.guide-title--compact]="step.compactTitle" + [innerHTML]="step.title"></h3> + @if (step.body) { + <p [innerHTML]="step.body"></p> + } + @if (studioResetWarning(); as warning) { + <p class="guide-warning">{{ warning }}</p> + } + @if (pendingRequirementHint(); as hint) { + <p class="guide-requirement">{{ hint }}</p> + } + </div> + + @if (step.id !== 'no-pathology-access') { + <div class="guide-banner-actions"> + <button type="button" class="guide-secondary" [disabled]="!canGoToPrevious()" (click)="goToPreviousStep()"> + {{ labels.back }} + </button> + <button type="button" class="guide-primary" [disabled]="!canGoToNext()" (click)="goToNextStep()"> + {{ nextButtonLabel() }} + </button> + </div> + } + </div> + } + </section> +</div> +} 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 new file mode 100644 index 000000000..dbb668602 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.spec.ts @@ -0,0 +1,937 @@ +import type { MockedObject, Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { Router } from '@angular/router'; +import { ExperimentStudioGuideComponent } from './experiment-studio-guide.component'; +import { EXPERIMENT_STUDIO_GUIDE_STEPS } from './experiment-studio-guide.content'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { GuideOnboardingService } from '../../../services/guide-onboarding.service'; +import { ExperimentStudioGuideStateService } from './experiment-studio-guide-state.service'; +import { createSpyObj } from '../../../testing/spy-obj.testing'; + +describe('ExperimentStudioGuideComponent', () => { + let component: ExperimentStudioGuideComponent; + let router: MockedObject<Router>; + let guideOnboarding: MockedObject<GuideOnboardingService>; + let experimentStudioService: { + resetStudioState: Mock; + resetStudioStateForGuide: Mock; + hasPersistedStudioWork: Mock; + pathologyAccessWarning: ReturnType<typeof signal>; + selectedVariables: ReturnType<typeof signal>; + algorithmY: ReturnType<typeof signal>; + algorithmX: ReturnType<typeof signal>; + selectedAlgorithm: ReturnType<typeof signal>; + runResult: ReturnType<typeof signal>; + currentExperimentUUID: ReturnType<typeof signal>; + }; + + beforeEach(() => { + router = createSpyObj<Router>('Router', ['navigate']); + router.navigate.mockReturnValue(Promise.resolve(true)); + + guideOnboarding = createSpyObj<GuideOnboardingService>( + 'GuideOnboardingService', + ['hasSeenStudioGuide', 'markStudioGuideSeen'], + ); + guideOnboarding.hasSeenStudioGuide.mockReturnValue(false); + guideOnboarding.markStudioGuideSeen.mockReturnValue(undefined); + + experimentStudioService = { + resetStudioState: vi.fn(), + resetStudioStateForGuide: vi.fn(), + hasPersistedStudioWork: vi.fn().mockReturnValue(false), + pathologyAccessWarning: signal(null), + selectedVariables: signal([]), + algorithmY: signal([]), + algorithmX: signal([]), + selectedAlgorithm: signal(null), + runResult: signal<unknown | null>(null), + currentExperimentUUID: signal<string | null>(null), + }; + + TestBed.configureTestingModule({ + imports: [ExperimentStudioGuideComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: Router, useValue: router }, + { provide: ExperimentStudioService, useValue: experimentStudioService }, + { provide: GuideOnboardingService, useValue: guideOnboarding }, + ExperimentStudioGuideStateService, + ], + }); + + component = TestBed.createComponent(ExperimentStudioGuideComponent).componentInstance; + }); + + afterEach(() => { + document.querySelectorAll('.studio-stepper').forEach((element) => element.remove()); + document.querySelectorAll('[data-guide]').forEach((element) => element.remove()); + }); + + it('does not auto-open the guide on first visit', () => { + expect(component.isOpen()).toBe(false); + + component.startGuide(); + + expect(component.isOpen()).toBe(true); + component.closeGuide(); + }); + + it('does not reset studio state on the first guide visit', () => { + component.startGuide(); + + expect(experimentStudioService.resetStudioState).not.toHaveBeenCalled(); + + component.closeGuide(); + }); + + it('does not reset studio state until the user continues past the launcher step', () => { + guideOnboarding.hasSeenStudioGuide.mockReturnValue(true); + experimentStudioService.hasPersistedStudioWork.mockReturnValue(true); + + component.startGuide(); + component.currentIndex.set(1); + + expect(experimentStudioService.resetStudioStateForGuide).not.toHaveBeenCalled(); + + component.goToNextStep(); + + expect(experimentStudioService.resetStudioStateForGuide).toHaveBeenCalled(); + component.closeGuide(); + }); + + it('shows a reset warning on the launcher step when an existing session would be cleared', () => { + guideOnboarding.hasSeenStudioGuide.mockReturnValue(true); + experimentStudioService.hasPersistedStudioWork.mockReturnValue(true); + + component.startGuide(); + component.currentIndex.set(1); + + expect(component.studioResetWarning()).toContain('continuing past this step will reset'); + component.closeGuide(); + }); + + it('explains blue-panel interaction on the welcome and launcher steps', () => { + const steps = (component as any).resolveSteps(); + const welcomeStep = steps.find((step: any) => step.id === 'welcome'); + const launcherStep = steps.find((step: any) => step.id === 'launcher'); + + expect(welcomeStep?.body).toContain('blue panel'); + expect(launcherStep?.body).toContain('highlighted blue panel'); + }); + + it('does not steal focus from inputs during passive layout refresh', () => { + const input = document.createElement('input'); + document.body.appendChild(input); + input.focus(); + + const guideFocus = vi.fn(); + (component as any).guideCard = { nativeElement: { focus: guideFocus } }; + component.activeSteps.set([ + { + id: 'select-sex-variable', + section: 'Explore', + title: 'Select Sex', + body: '', + selector: '[data-guide="variable-browser"]', + allowTargetInteraction: true, + requirement: 'selected-sex', + }, + ] as any); + + (component as any).updateLayout(false); + + expect(guideFocus).not.toHaveBeenCalled(); + expect(document.activeElement).toBe(input); + }); + + it('tracks the highlighted target rectangle for the active step', () => { + const target = document.createElement('section'); + target.setAttribute('data-guide', 'variable-selection'); + vi.spyOn(target, 'getBoundingClientRect').mockReturnValue({ + top: 140, + right: 760, + bottom: 540, + left: 220, + width: 540, + height: 400, + x: 220, + y: 140, + toJSON: () => ({}), + } as DOMRect); + document.body.appendChild(target); + + component.activeSteps.set([ + { + id: 'variable-selection', + section: 'Explore', + title: 'Variables Selection', + body: '', + selector: '[data-guide="variable-selection"]', + }, + ] as any); + + (component as any).updateLayout(); + + expect(component.highlightRect()).toEqual(expect.objectContaining({ + top: 130, + left: 210, + })); + expect(component.highlightRect()?.right).toBeGreaterThan(component.highlightRect()!.left); + expect(component.highlightRect()?.bottom).toBeGreaterThan(component.highlightRect()!.top); + }); + + it('keeps the highlight aligned when the target scrolls above the viewport', () => { + const target = document.createElement('section'); + target.setAttribute('data-guide', 'analysis-section'); + vi.spyOn(target, 'getBoundingClientRect').mockReturnValue({ + top: -24, + right: 760, + bottom: 140, + left: 220, + width: 540, + height: 164, + x: 220, + y: -24, + toJSON: () => ({}), + } as DOMRect); + document.body.appendChild(target); + + component.activeSteps.set([ + { + id: 'analysis-intro', + section: 'Analysis', + title: 'Analysis Section', + body: '', + selector: '[data-guide="analysis-section"]', + }, + ] as any); + + (component as any).updateLayout(); + + expect(component.highlightRect()).toEqual(expect.objectContaining({ + top: -34, + left: 210, + bottom: 150, + right: 770, + })); + }); + + it('clears the highlight when the active step target is missing', () => { + component.highlightRect.set({ + top: 40, + right: 160, + bottom: 120, + left: 24, + width: 136, + height: 80, + }); + component.activeSteps.set([ + { + id: 'missing-target', + section: 'Explore', + title: 'Missing', + body: '', + selector: '[data-guide="missing-target"]', + }, + ] as any); + + (component as any).updateLayout(); + + expect(component.highlightRect()).toBeNull(); + }); + + it('scrolls the analysis section guide step to the top of the viewport', () => { + const analysisSection = document.createElement('section'); + analysisSection.setAttribute('data-guide', 'analysis-section'); + vi.spyOn(analysisSection, 'getBoundingClientRect').mockReturnValue({ + top: 320, + right: 920, + bottom: 880, + left: 260, + width: 660, + height: 560, + x: 260, + y: 320, + toJSON: () => ({}), + } as DOMRect); + const scrollSpy = vi.spyOn(analysisSection, 'scrollIntoView').mockReturnValue(undefined); + document.body.appendChild(analysisSection); + vi.spyOn(component as any, 'scheduleLayoutUpdate').mockReturnValue(undefined); + + component.activeSteps.set([ + { + id: 'analysis-intro', + section: 'Analysis', + title: 'Analysis Section', + body: '', + selector: '[data-guide="analysis-section"]', + }, + ] as any); + + (component as any).syncStepLayout(); + + expect(scrollSpy).toHaveBeenCalledWith(expect.objectContaining({ + behavior: 'smooth', + block: 'start', + inline: 'nearest', + })); + }); + + it('keeps the guide collapsed when navigating to the next step from collapsed mode', () => { + vi.spyOn(component as any, 'syncStepLayout').mockReturnValue(undefined); + component.activeSteps.set([ + { id: 'step-1', section: 'Explore', title: 'Step 1', body: '' }, + { id: 'step-2', section: 'Explore', title: 'Step 2', body: '' }, + ] as any); + component.isOpen.set(true); + component.isCollapsed.set(true); + + component.goToNextStep(); + + expect(component.currentIndex()).toBe(1); + expect(component.isCollapsed()).toBe(true); + }); + + it('disables Back when the previous step requires action', () => { + vi.spyOn(component as any, 'syncStepLayout').mockReturnValue(undefined); + component.activeSteps.set([ + { id: 'step-1', section: 'Explore', title: 'Step 1', body: '', requirement: 'selected-sex' }, + { id: 'step-2', section: 'Explore', title: 'Step 2', body: '' }, + ] as any); + component.isOpen.set(true); + component.currentIndex.set(1); + + expect(component.canGoToPrevious()).toBe(false); + + component.goToPreviousStep(); + + expect(component.currentIndex()).toBe(1); + }); + + it('keeps Back available when the previous step is informational', () => { + vi.spyOn(component as any, 'syncStepLayout').mockReturnValue(undefined); + component.activeSteps.set([ + { id: 'step-1', section: 'Explore', title: 'Step 1', body: '' }, + { id: 'step-2', section: 'Explore', title: 'Step 2', body: '' }, + ] as any); + component.isOpen.set(true); + component.currentIndex.set(1); + + expect(component.canGoToPrevious()).toBe(true); + + component.goToPreviousStep(); + + expect(component.currentIndex()).toBe(0); + }); + + it('keeps the guide expanded when navigating onto compact-title steps', () => { + vi.spyOn(component as any, 'syncStepLayout').mockReturnValue(undefined); + component.activeSteps.set([ + { id: 'step-1', section: 'Explore', title: 'Step 1', body: '' }, + { id: 'step-2', section: 'Explore', title: 'Step 2', body: '', compactTitle: true }, + { id: 'step-3', section: 'Explore', title: 'Step 3', body: '' }, + ] as any); + component.isOpen.set(true); + + component.goToNextStep(); + + expect(component.currentIndex()).toBe(1); + expect(component.isCollapsed()).toBe(false); + + component.isCollapsed.set(true); + component.goToNextStep(); + + expect(component.currentIndex()).toBe(2); + expect(component.isCollapsed()).toBe(true); + }); + + it('replaces exact Sex and Age guide copy with the configured guide labels', () => { + (component as any).guideCovariateLabel = 'Biological Sex'; + (component as any).guideVariableLabel = 'Age Years'; + + const replaced = (component as any).replaceGuideTargets('Use Sex as Covariate and Age as Variable.'); + const steps = (component as any).resolveSteps(); + + expect(replaced).toBe('Use Biological Sex as Covariate and Age Years as Variable.'); + expect(steps.find((step: any) => step.id === 'add-sex-covariate')?.title).toBe('Add Biological Sex as Variable'); + expect(steps.find((step: any) => step.id === 'add-age-variable')?.title).toBe('Add Age Years as Variable'); + }); + + it('treats the Age guide step as complete only when both targets are added to the variables pool', () => { + experimentStudioService.selectedVariables.set([ + { code: 'sex', label: 'Sex' }, + { code: 'age', label: 'Age' }, + ]); + + expect((component as any).isStepRequirementSatisfied({ requirement: 'variable-age' })).toBe(true); + }); + + it('unlocks the algorithm selection guide step when any algorithm is selected', () => { + expect((component as any).isStepRequirementSatisfied({ requirement: 'algorithm-selected' })).toBe(false); + + experimentStudioService.selectedAlgorithm.set({ name: 'linear_regression', label: 'Linear Regression' }); + expect((component as any).isStepRequirementSatisfied({ requirement: 'algorithm-selected' })).toBe(true); + }); + + it('marks the add-sex and add-age guide steps as requiring action until the variables are added', () => { + component.activeSteps.set([ + { id: 'add-sex-covariate', section: 'Explore', title: 'Add Sex', body: '', requirement: 'variable-sex' }, + { id: 'add-age-variable', section: 'Explore', title: 'Add Age', body: '', requirement: 'variable-age' }, + ] as any); + + component.currentIndex.set(0); + expect(component.stepNeedsAction()).toBe(true); + expect(component.nextButtonLabel()).toBe('Action required'); + + experimentStudioService.selectedVariables.set([{ code: 'sex', label: 'Sex' }]); + expect(component.stepNeedsAction()).toBe(false); + + component.currentIndex.set(1); + expect(component.stepNeedsAction()).toBe(true); + expect(component.nextButtonLabel()).toBe('Action required'); + + experimentStudioService.selectedVariables.set([ + { code: 'sex', label: 'Sex' }, + { code: 'age', label: 'Age' }, + ]); + expect(component.stepNeedsAction()).toBe(false); + }); + + it('requires the guide variable (Age) in y and the guide covariate (Sex) in x', () => { + expect((component as any).isStepRequirementSatisfied({ requirement: 'roles-assigned' })).toBe(false); + + // Guide covariate (Sex) in x only; Age not yet assigned. + experimentStudioService.algorithmX.set([{ code: 'sex', label: 'Sex' }]); + expect((component as any).isStepRequirementSatisfied({ requirement: 'roles-assigned' })).toBe(false); + + // Both targets in y (wrong mix) does not satisfy the requirement. + experimentStudioService.algorithmY.set([ + { code: 'age', label: 'Age' }, + { code: 'sex', label: 'Sex' }, + ]); + experimentStudioService.algorithmX.set([]); + expect((component as any).isStepRequirementSatisfied({ requirement: 'roles-assigned' })).toBe(false); + + // Guide variable (Age) in x (wrong mix) does not satisfy the requirement. + experimentStudioService.algorithmY.set([]); + experimentStudioService.algorithmX.set([ + { code: 'sex', label: 'Sex' }, + { code: 'age', label: 'Age' }, + ]); + expect((component as any).isStepRequirementSatisfied({ requirement: 'roles-assigned' })).toBe(false); + + // Guide variable (Age) in y and guide covariate (Sex) in x satisfies it. + experimentStudioService.algorithmY.set([{ code: 'age', label: 'Age' }]); + experimentStudioService.algorithmX.set([{ code: 'sex', label: 'Sex' }]); + expect((component as any).isStepRequirementSatisfied({ requirement: 'roles-assigned' })).toBe(true); + }); + + it('keeps future experiment steps in the guide flow even before the save flow is opened', () => { + const steps = (component as any).resolveSteps(); + + expect(steps.some((step: any) => step.id === 'experiment-set-parameters')).toBe(false); + expect(steps.some((step: any) => step.id === 'experiment-wait-for-save')).toBe(true); + expect(steps.some((step: any) => step.id === 'experiment-finish')).toBe(true); + }); + + it('lets the algorithm selection step interact with the algorithm catalog', () => { + const steps = (component as any).resolveSteps(); + const algorithmStep = steps.find((step: any) => step.id === 'experiment-select-algorithm'); + + expect(algorithmStep?.selector).toBe('[data-guide="algorithm-selection"]'); + expect(algorithmStep?.allowTargetInteraction).toBe(true); + }); + + it('marks the Save as step as requiring action until the save form is opened', () => { + component.activeSteps.set([ + { + id: 'experiment-save-as-action', + section: 'Experiment', + title: 'Save as', + body: '', + selector: '[data-guide="save-as-action"]', + allowTargetInteraction: true, + advanceOnTargetClick: true, + requirement: 'save-as-opened', + }, + ] as any); + + expect(component.stepNeedsAction()).toBe(true); + expect(component.nextButtonLabel()).toBe('Action required'); + expect((component as any).isStepRequirementSatisfied({ requirement: 'save-as-opened' })).toBe(false); + + const saveForm = document.createElement('div'); + saveForm.setAttribute('data-guide', 'save-as-form'); + vi.spyOn(saveForm, 'getBoundingClientRect').mockReturnValue({ + top: 240, + right: 420, + bottom: 320, + left: 240, + width: 180, + height: 80, + x: 240, + y: 240, + toJSON: () => ({}), + } as DOMRect); + document.body.appendChild(saveForm); + + expect((component as any).isStepRequirementSatisfied({ requirement: 'save-as-opened' })).toBe(true); + }); + + it('moves to the next step after clicking the Save as target', () => new Promise<void>((done) => { + const saveButton = document.createElement('button'); + saveButton.setAttribute('data-guide', 'save-as-action'); + vi.spyOn(saveButton, 'getBoundingClientRect').mockReturnValue({ + top: 240, + right: 360, + bottom: 280, + left: 240, + width: 120, + height: 40, + x: 240, + y: 240, + toJSON: () => ({}), + } as DOMRect); + document.body.appendChild(saveButton); + + const saveForm = document.createElement('div'); + saveForm.setAttribute('data-guide', 'save-as-form'); + vi.spyOn(saveForm, 'getBoundingClientRect').mockReturnValue({ + top: 240, + right: 460, + bottom: 330, + left: 240, + width: 220, + height: 90, + x: 240, + y: 240, + toJSON: () => ({}), + } as DOMRect); + document.body.appendChild(saveForm); + + vi.spyOn(component as any, 'syncStepLayout').mockImplementation(() => undefined); + component.isOpen.set(true); + component.activeSteps.set([ + { + id: 'experiment-save-as-action', + section: 'Experiment', + title: 'Save as', + body: '', + selector: '[data-guide="save-as-action"]', + allowTargetInteraction: true, + advanceOnTargetClick: true, + requirement: 'save-as-opened', + }, + { + id: 'experiment-wait-for-save', + section: 'Experiment', + title: 'Save Experiment', + body: '', + selector: '[data-guide="save-as-flow"]', + allowTargetInteraction: true, + requirement: 'experiment-saved-as', + }, + ] as any); + + component.onDocumentClick({ target: saveButton } as unknown as MouseEvent); + + setTimeout(() => { + expect(component.currentIndex()).toBe(1); + done(); + }, 320); + })); + + it('places the result action familiarization steps before the save-complete waiting step', () => { + const steps = (component as any).resolveSteps(); + const runIndex = steps.findIndex((step: any) => step.id === 'experiment-run'); + const exploreIndex = steps.findIndex((step: any) => step.id === 'experiment-explore-result'); + const editIndex = steps.findIndex((step: any) => step.id === 'experiment-edit-parameters'); + const summaryActionIndex = steps.findIndex((step: any) => step.id === 'experiment-result-actions'); + const saveActionIndex = steps.findIndex((step: any) => step.id === 'experiment-save-as-action'); + const saveWaitIndex = steps.findIndex((step: any) => step.id === 'experiment-wait-for-save'); + + expect(runIndex).toBeGreaterThan(-1); + expect(exploreIndex).toBe(runIndex + 1); + expect(editIndex).toBe(exploreIndex + 1); + expect(summaryActionIndex).toBe(editIndex + 1); + expect(saveActionIndex).toBe(summaryActionIndex + 1); + expect(saveWaitIndex).toBe(saveActionIndex + 1); + }); + + it('keeps the Edit parameters familiarization step non-interactive', () => { + const steps = (component as any).resolveSteps(); + const editStep = steps.find((step: any) => step.id === 'experiment-edit-parameters'); + + expect(editStep?.allowTargetInteraction).toBe(false); + }); + + it('keeps the Analysis filtering step interactive and optional', () => { + const steps = (component as any).resolveSteps(); + const filteringStep = steps.find((step: any) => step.id === 'analysis-filtering'); + + expect(filteringStep?.allowTargetInteraction).toBe(true); + expect(filteringStep?.body).toContain('Optional'); + expect(filteringStep?.body).toContain('Add condition'); + }); + + it('keeps the Analysis preprocessing step interactive and optional', () => { + const steps = (component as any).resolveSteps(); + const preprocessingStep = steps.find((step: any) => step.id === 'analysis-preprocessing'); + + expect(preprocessingStep?.allowTargetInteraction).toBe(true); + expect(preprocessingStep?.body).toContain('Optional'); + expect(preprocessingStep?.body).toContain('missing values are already dropped'); + }); + + it('keeps the raw and processed summary guide steps interactive across the full section', () => { + const steps = (component as any).resolveSteps(); + const rawStatisticsStep = steps.find((step: any) => step.id === 'analysis-raw-statistics'); + const processedStep = steps.find((step: any) => step.id === 'analysis-processed-summary'); + + expect(rawStatisticsStep?.selector).toBe('[data-guide="analysis-raw-summary"]'); + expect(processedStep?.selector).toBe('[data-guide="analysis-processed-summary"]'); + expect(rawStatisticsStep?.allowTargetInteraction).toBe(true); + expect(processedStep?.allowTargetInteraction).toBe(true); + expect(steps.some((step: any) => step.id === 'analysis-raw-charts')).toBe(false); + }); + + it('points the experiment summary step at the visible summary panel', () => { + const steps = (component as any).resolveSteps(); + const summaryStep = steps.find((step: any) => step.id === 'experiment-result-actions'); + + expect(summaryStep?.selector).toBe('[data-guide="save-as-flow"]'); + expect(summaryStep?.allowTargetInteraction).toBe(true); + }); + + it('keeps the final experiment step open to page interaction', () => { + const steps = (component as any).resolveSteps(); + const finishStep = steps.find((step: any) => step.id === 'experiment-finish'); + + expect(finishStep?.allowTargetInteraction).toBe(true); + expect(finishStep?.selector).toBe('[data-guide="dashboard-nav"]'); + expect(finishStep?.body).toContain('header compass'); + expect(finishStep?.body).not.toContain('the tour moves'); + }); + + it('labels the final experiment step action as Move to dashboard', () => { + component.activeSteps.set([ + { id: 'experiment-finish', section: 'Experiment', title: 'Done', body: '', allowTargetInteraction: true }, + ] as any); + + expect(component.nextButtonLabel()).toBe('Move to dashboard'); + }); + + it('navigates to the dashboard from the final experiment step', () => { + component.activeSteps.set([ + { id: 'experiment-finish', section: 'Experiment', title: 'Done', body: '', allowTargetInteraction: true }, + ] as any); + component.isOpen.set(true); + + component.goToNextStep(); + + expect(router.navigate).toHaveBeenCalledWith(['/experiments-dashboard']); + expect(component.isOpen()).toBe(false); + }); + + it('keeps the run step locked until an experiment result is rendered', () => { + expect((component as any).isStepRequirementSatisfied({ requirement: 'experiment-result-ready' })).toBe(false); + + // Service state, not DOM: the Execution view can be display:none while the Run step + // still owns Algorithm Selection, so a target-size check would miss a finished run. + experimentStudioService.runResult.set({ algorithm: 'chi-square' }); + + expect((component as any).isStepRequirementSatisfied({ requirement: 'experiment-result-ready' })).toBe(true); + }); + + it('keeps the save-wait step locked until Save as creates a new experiment', () => { + component.isOpen.set(true); + component.activeSteps.set([ + { + id: 'experiment-wait-for-save', + section: 'Experiment', + title: 'Save Experiment', + body: '', + requirement: 'experiment-saved-as', + }, + ] as any); + (component as any).experimentSaveBaselineUuid.set(null); + + // Saving does not re-run the experiment, so the result stays on screen throughout. + const resultSection = document.createElement('section'); + resultSection.setAttribute('data-guide', 'experiment-result'); + document.body.appendChild(resultSection); + + expect((component as any).isStepRequirementSatisfied({ requirement: 'experiment-saved-as' })).toBe(false); + + experimentStudioService.currentExperimentUUID.set('saved-experiment-uuid'); + + expect((component as any).isStepRequirementSatisfied({ requirement: 'experiment-saved-as' })).toBe(true); + + resultSection.remove(); + }); + + it('auto-advances after an algorithm is selected', () => { + expect((component as any).shouldAutoAdvance({ requirement: 'algorithm-selected' })).toBe(true); + }); + + it('auto-advances after the experiment result becomes available', () => { + expect((component as any).shouldAutoAdvance({ requirement: 'experiment-result-ready' })).toBe(true); + }); + + it('auto-advances after Save as opens the save form', () => { + expect((component as any).shouldAutoAdvance({ requirement: 'save-as-opened' })).toBe(true); + }); + + it('auto-advances after Save as creates a new experiment', () => { + expect((component as any).shouldAutoAdvance({ requirement: 'experiment-saved-as' })).toBe(true); + }); + + it('tracks interaction cutouts for variable selection and details panels', () => { + const browser = document.createElement('div'); + browser.setAttribute('data-guide', 'variable-browser'); + vi.spyOn(browser, 'getBoundingClientRect').mockReturnValue({ + top: 120, + right: 620, + bottom: 640, + left: 80, + width: 540, + height: 520, + x: 80, + y: 120, + toJSON: () => ({}), + } as DOMRect); + + const details = document.createElement('section'); + details.setAttribute('data-guide', 'variable-details'); + vi.spyOn(details, 'getBoundingClientRect').mockReturnValue({ + top: 360, + right: 1180, + bottom: 760, + left: 660, + width: 520, + height: 400, + x: 660, + y: 360, + toJSON: () => ({}), + } as DOMRect); + + document.body.appendChild(browser); + document.body.appendChild(details); + + component.activeSteps.set([ + { + id: 'select-age-variable', + section: 'Explore', + title: 'Select Age', + body: '', + selector: '[data-guide="variable-browser"]', + interactionSelectors: ['[data-guide="variable-details"]'], + allowTargetInteraction: true, + requirement: 'selected-age', + }, + ] as any); + + (component as any).updateLayout(); + + expect(component.interactionCutouts().length).toBe(2); + expect(component.blockingMaskRects().length).toBeGreaterThan(0); + }); + + it('keeps the Explore Variable Views step interactive and optional', () => { + const steps = (component as any).resolveSteps(); + const exploreStep = steps.find((step: any) => step.id === 'variable-selection'); + + expect(exploreStep?.title).toBe('Explore Variable Views'); + expect(exploreStep?.selector).toBe('[data-guide="variable-selection"]'); + expect(exploreStep?.allowTargetInteraction).toBe(true); + expect(exploreStep?.body).toContain('Map'); + expect(exploreStep?.body).toContain('List'); + expect(exploreStep?.body).toContain('Graph'); + expect(exploreStep?.body).toContain('Next'); + expect(exploreStep?.interactionSelectors).toBeUndefined(); + expect(steps.some((step: any) => step.id === 'explore-variable-visualizations')).toBe(false); + }); + + it('builds click-through blockers around interactive guide targets', () => { + const target = document.createElement('div'); + target.setAttribute('data-guide', 'variable-selection'); + vi.spyOn(target, 'getBoundingClientRect').mockReturnValue({ + top: 180, + right: 620, + bottom: 640, + left: 80, + width: 540, + height: 460, + x: 80, + y: 180, + toJSON: () => ({}), + } as DOMRect); + document.body.appendChild(target); + + component.activeSteps.set([ + { + id: 'variable-selection', + section: 'Explore', + title: 'Explore Variable Views', + body: '', + selector: '[data-guide="variable-selection"]', + allowTargetInteraction: true, + }, + ] as any); + + (component as any).updateLayout(); + + expect(component.blockingMaskRects().length).toBeGreaterThan(0); + expect(component.blockingMaskRects().some((rect) => rect.top === 0 && rect.bottom === 170)).toBe(true); + }); + + it('keeps the Sex selection step interactive without highlighting variable details', () => { + const steps = (component as any).resolveSteps(); + const selectSexStep = steps.find((step: any) => step.id === 'select-sex-variable'); + + expect(selectSexStep?.allowTargetInteraction).toBe(true); + expect(selectSexStep?.selector).toBe('[data-guide="variable-selection"]'); + expect(selectSexStep?.interactionSelectors).toBeUndefined(); + }); + + it('places variable preview steps after each guided variable selection', () => { + const steps = (component as any).resolveSteps(); + const selectSexIndex = steps.findIndex((step: any) => step.id === 'select-sex-variable'); + const previewSexIndex = steps.findIndex((step: any) => step.id === 'preview-sex-variable-details'); + const addSexIndex = steps.findIndex((step: any) => step.id === 'add-sex-covariate'); + const selectAgeIndex = steps.findIndex((step: any) => step.id === 'select-age-variable'); + const previewAgeIndex = steps.findIndex((step: any) => step.id === 'preview-age-variable-details'); + const addAgeIndex = steps.findIndex((step: any) => step.id === 'add-age-variable'); + + expect(previewSexIndex).toBe(selectSexIndex + 1); + expect(addSexIndex).toBe(previewSexIndex + 1); + expect(previewAgeIndex).toBe(selectAgeIndex + 1); + expect(addAgeIndex).toBe(previewAgeIndex + 1); + expect(steps[previewSexIndex].selector).toBe('[data-guide="variable-details"]'); + expect(steps[previewAgeIndex].allowTargetInteraction).toBe(true); + }); + + it('points every content step with a selector at its data-guide target', () => { + const stepsWithSelectors = EXPERIMENT_STUDIO_GUIDE_STEPS.filter((step) => !!step.selector); + expect(stepsWithSelectors.length).toBeGreaterThan(0); + + for (const step of stepsWithSelectors) { + document.querySelectorAll('[data-guide]').forEach((element) => element.remove()); + + const target = document.createElement('div'); + const guideValue = step.selector!.match(/data-guide="([^"]+)"/)?.[1]; + expect(guideValue, step.id).toBeTruthy(); + target.setAttribute('data-guide', guideValue!); + Object.defineProperty(target, 'getBoundingClientRect', { + configurable: true, + value: () => ({ + top: 120, + right: 420, + bottom: 200, + left: 40, + width: 380, + height: 80, + x: 40, + y: 120, + toJSON: () => ({}), + }), + }); + document.body.appendChild(target); + + component.activeSteps.set([...EXPERIMENT_STUDIO_GUIDE_STEPS] as any); + component.currentIndex.set(EXPERIMENT_STUDIO_GUIDE_STEPS.findIndex((entry) => entry.id === step.id)); + (component as any).updateLayout(); + + const highlight = component.highlightRect(); + expect(highlight, step.id).not.toBeNull(); + expect(highlight!.width, step.id).toBeGreaterThan(0); + expect(highlight!.height, step.id).toBeGreaterThan(0); + // Pathology & Datasets contract: target center sits inside the spotlight. + const centerX = 230; + const centerY = 160; + expect(highlight!.left <= centerX && centerX <= highlight!.right, step.id).toBe(true); + expect(highlight!.top <= centerY && centerY <= highlight!.bottom, step.id).toBe(true); + } + }); + + it('documents that the guide host stacks above the app header', () => { + // app-header uses z-index 10000; guide :host must stay above that (see component CSS). + // This keeps the Account menu / launcher spotlight visible on chrome targets. + const guideHostZIndex = 11000; + const appHeaderZIndex = 10000; + expect(guideHostZIndex).toBeGreaterThan(appHeaderZIndex); + }); + + it('ignores arrow keys while a text field is focused', () => { + component.activeSteps.set([ + { id: 'one', section: 'Explore', title: 'One', body: '' }, + { id: 'two', section: 'Explore', title: 'Two', body: '' }, + ] as any); + component.isOpen.set(true); + component.currentIndex.set(0); + + const input = document.createElement('input'); + const preventDefault = vi.fn(); + const typingEvent = new KeyboardEvent('keydown', { key: 'ArrowRight' }); + Object.defineProperty(typingEvent, 'target', { configurable: true, value: input }); + typingEvent.preventDefault = preventDefault; + + component.onWindowKeydown(typingEvent); + + expect(preventDefault).not.toHaveBeenCalled(); + expect(component.currentIndex()).toBe(0); + + const bodyEvent = new KeyboardEvent('keydown', { key: 'ArrowRight' }); + Object.defineProperty(bodyEvent, 'target', { configurable: true, value: document.body }); + + component.onWindowKeydown(bodyEvent); + + expect(component.currentIndex()).toBe(1); + + component.closeGuide(); + }); + + it('spots a target that renders after the step and bounds its retries', () => { + vi.useFakeTimers(); + const late = document.createElement('div'); + late.setAttribute('data-guide', 'late-target'); + late.style.cssText = 'position:fixed;top:0;left:0;width:120px;height:40px;'; + + try { + component.activeSteps.set([ + { id: 'late', section: 'Explore', title: 'Late', body: '', selector: '[data-guide="late-target"]' }, + ] as any); + component.isOpen.set(true); + component.currentIndex.set(0); + + (component as any).syncStepLayout(); + vi.advanceTimersByTime(1); + expect(component.highlightRect()).toBeNull(); + + // The owning view rendered late: the pending retry must pick the target up. + document.body.appendChild(late); + vi.advanceTimersByTime(150); + vi.advanceTimersByTime(260); + expect(component.highlightRect()).not.toBeNull(); + + // Attempts are bounded: an unreachable target stops polling. + component.activeSteps.set([ + { id: 'never', section: 'Explore', title: 'Gone', body: '', selector: '[data-guide="never"]' }, + ] as any); + (component as any).syncStepLayout(); + vi.advanceTimersByTime(150 * 12); + + expect((component as any).targetRetryTimer).toBeNull(); + expect(component.highlightRect()).toBeNull(); + expect(component.canGoToNext()).toBe(true); + } finally { + late.remove(); + vi.useRealTimers(); + } + + component.closeGuide(); + }); + +}); diff --git a/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.ts b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.ts new file mode 100644 index 000000000..8cda4cd79 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide.component.ts @@ -0,0 +1,712 @@ +import { CommonModule, DOCUMENT } from '@angular/common'; +import { ChangeDetectionStrategy, Component, ElementRef, Input, OnDestroy, OnInit, ViewChild, computed, effect, inject, signal } from '@angular/core'; +import { Router } from '@angular/router'; +import { + EXPERIMENT_STUDIO_GUIDE_LABELS, + EXPERIMENT_STUDIO_GUIDE_STEPS, + ExperimentStudioGuideStep, + GuideSection, +} from './experiment-studio-guide.content'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { GuideOnboardingService } from '../../../services/guide-onboarding.service'; +import { GuideLauncherService, GuideLauncher } from '../../../services/guide-launcher.service'; +import { ExperimentStudioGuideStateService } from './experiment-studio-guide-state.service'; +import { computeGuideBlockingRects, measureGuideInteractionRect } from './experiment-studio-guide-mask.util'; +import { escapeHtml } from '../../../core/html.utils'; +import { isTypingTarget } from '../../../core/keyboard.utils'; + +interface GuideRect { + top: number; + left: number; + width: number; + height: number; + right: number; + bottom: number; +} + +@Component({ + selector: 'app-experiment-studio-guide', + imports: [CommonModule], + templateUrl: './experiment-studio-guide.component.html', + styleUrl: './experiment-studio-guide.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, + host: { + '(window:resize)': 'onViewportChange()', + '(window:scroll)': 'onViewportChange()', + '(window:keydown)': 'onWindowKeydown($event)', + '(document:click)': 'onDocumentClick($event)', + }, +}) +export class ExperimentStudioGuideComponent implements OnInit, OnDestroy { + private readonly document = inject(DOCUMENT); + private readonly router = inject(Router); + private readonly experimentStudioService = inject(ExperimentStudioService); + private readonly guideOnboarding = inject(GuideOnboardingService); + private readonly guideLauncher = inject(GuideLauncherService); + private readonly guideState = inject(ExperimentStudioGuideStateService); + private layoutTimer: number | null = null; + private autoAdvanceTimer: number | null = null; + private targetRetryTimer: number | null = null; + private targetResizeObserver: ResizeObserver | null = null; + private observedTarget: HTMLElement | null = null; + private domMutationObserver: MutationObserver | null = null; + + /** The bar draws this (see GuideLauncherService); the launcher button used to be a + * fixed circle floating over the header at z 10001. */ + private readonly launcherHandle: GuideLauncher = { + label: EXPERIMENT_STUDIO_GUIDE_LABELS.launcher, + start: () => this.startGuide(), + }; + + /** + * View-activation hook provided by the studio shell. The guide must activate a + * rail-switched view before querying/measuring a target inside it, because hidden + * views are display:none and report zero dimensions. The step id lets the shell + * additionally open the relevant sub-tab inside the activated view (e.g. an + * Analysis tour step opens its statistics sub-tab before the guide measures it). + */ + @Input() activateGuideTarget?: (view: GuideSection, stepId?: string) => void; + + @ViewChild('guideCard') + private guideCard?: ElementRef<HTMLElement>; + + readonly labels = EXPERIMENT_STUDIO_GUIDE_LABELS; + readonly guideCovariateLabel = this.guideState.guideCovariate.label; + readonly guideVariableLabel = this.guideState.guideVariable.label; + readonly isOpen = signal(false); + readonly activeSteps = signal<ExperimentStudioGuideStep[]>([]); + readonly currentIndex = signal(0); + readonly highlightRect = signal<GuideRect | null>(null); + readonly interactionCutouts = signal<GuideRect[]>([]); + readonly blockingMaskRects = signal<GuideRect[]>([]); + readonly isCollapsed = signal(false); + private readonly domRevision = signal(0); + private readonly experimentSaveBaselineUuid = signal<string | null | undefined>(undefined); + private readonly pendingStudioResetOnContinue = signal(false); + + readonly currentStep = computed(() => this.activeSteps()[this.currentIndex()] ?? null); + readonly previousStep = computed(() => this.activeSteps()[this.currentIndex() - 1] ?? null); + readonly totalSteps = computed(() => this.activeSteps().length); + readonly currentStepNumber = computed(() => (this.currentStep() ? this.currentIndex() + 1 : 0)); + readonly progressPercent = computed(() => { + const total = this.totalSteps(); + return total ? (this.currentStepNumber() / total) * 100 : 0; + }); + readonly hasPrevious = computed(() => this.currentIndex() > 0); + readonly canGoToPrevious = computed(() => this.hasPrevious() && !this.previousStep()?.requirement); + readonly isLastStep = computed(() => this.currentIndex() >= this.totalSteps() - 1); + readonly canGoToNext = computed(() => this.isStepRequirementSatisfied(this.currentStep())); + readonly stepNeedsAction = computed(() => !!this.currentStep()?.requirement && !this.canGoToNext()); + readonly isDashboardStep = computed(() => this.currentStep()?.id === 'experiment-finish'); + readonly nextButtonLabel = computed(() => + this.stepNeedsAction() + ? 'Action required' + : this.isDashboardStep() + ? this.labels.moveToDashboard + : this.isLastStep() + ? this.labels.done + : this.labels.next + ); + readonly studioResetWarning = computed(() => { + if (this.currentStep()?.id !== 'launcher' || !this.pendingStudioResetOnContinue()) { + return null; + } + + return 'Warning: continuing past this step will reset your current session, including variables, covariates, preprocessing, and algorithm selection.'; + }); + readonly pendingRequirementHint = computed(() => { + const step = this.currentStep(); + if (!step?.requirement || this.canGoToNext()) { + return null; + } + + switch (step.requirement) { + case 'selected-sex': + return this.replaceGuideTargets('Either use the search bar to find the Sex variable or click the green-highlighted variable through the bubble chart to continue.'); + case 'variable-sex': + return 'Click Add to put ' + this.guideCovariateLabel + ' into the experiment. The Variables in this experiment card below the details panel keeps the list visible.'; + case 'selected-age': + return this.replaceGuideTargets('Either use the search bar to find the Age variable or click the green-highlighted variable through the bubble chart to continue. You can also explore the chart and details on the right.'); + case 'variable-age': + return 'Click Add to put ' + this.guideVariableLabel + ' into the experiment. The Variables in this experiment card below the details panel keeps the list visible.'; + case 'roles-assigned': + return 'Assign each added variable as an outcome (Variables / y) or a predictor (Covariates / x) on the algorithm panel to continue.'; + case 'algorithm-selected': + return 'Select any available algorithm with a green tick to continue.'; + case 'experiment-result-ready': + return 'Run the experiment and wait for the result view to load before continuing.'; + case 'save-as-opened': + return 'Click Save as to open the save form and continue.'; + case 'experiment-saved-as': + return 'Enter a name and click Save. The guide continues automatically once the experiment has been saved.'; + default: + return null; + } + }); + + constructor() { + effect(() => { + this.guideState.setActiveStep(this.isOpen() ? this.currentStep()?.id ?? null : null); + }); + + effect(() => { + const step = this.currentStep(); + + if (!this.isOpen() || step?.requirement !== 'experiment-saved-as') { + this.experimentSaveBaselineUuid.set(undefined); + return; + } + + const currentUuid = this.experimentStudioService.currentExperimentUUID(); + if (this.experimentSaveBaselineUuid() === undefined) { + this.experimentSaveBaselineUuid.set(currentUuid); + } + }); + + effect(() => { + this.experimentStudioService.selectedVariables(); + this.experimentStudioService.algorithmY(); + this.experimentStudioService.algorithmX(); + this.experimentStudioService.selectedAlgorithm(); + this.experimentStudioService.runResult(); + this.experimentStudioService.currentExperimentUUID(); + this.guideState.selectedHierarchyNode(); + const step = this.currentStep(); + + if ( + !this.isOpen() || + !step?.requirement || + !this.shouldAutoAdvance(step) || + !this.isStepRequirementSatisfied(step) + ) { + this.clearAutoAdvanceTimer(); + return; + } + + this.clearAutoAdvanceTimer(); + this.autoAdvanceTimer = window.setTimeout(() => { + if (this.isOpen() && this.currentStep()?.id === step.id && this.isStepRequirementSatisfied(step)) { + this.goToNextStep(); + } + }, 320); + }); + } + + ngOnInit(): void { + this.guideLauncher.register(this.launcherHandle); + } + + ngOnDestroy(): void { + this.guideLauncher.unregister(this.launcherHandle); + this.disconnectTargetObserver(); + this.disconnectDomObserver(); + this.clearLayoutTimer(); + this.clearTargetRetryTimer(); + this.clearAutoAdvanceTimer(); + } + + startGuide(): void { + this.pendingStudioResetOnContinue.set( + this.guideOnboarding.hasSeenStudioGuide() + && this.experimentStudioService.hasPersistedStudioWork(), + ); + + const warning = this.experimentStudioService.pathologyAccessWarning(); + let resolvedSteps: ExperimentStudioGuideStep[]; + + if (warning) { + resolvedSteps = [{ + id: 'no-pathology-access', + section: 'Explore', + title: 'No Access to Federation Pathologies', + body: '', + selector: '.pathology-warning-banner', + allowTargetInteraction: false + }]; + } else { + resolvedSteps = this.resolveSteps(); + if (!resolvedSteps.length) { + return; + } + } + + this.activeSteps.set(resolvedSteps); + this.currentIndex.set(0); + this.isCollapsed.set(false); + this.isOpen.set(true); + this.startDomObserver(); + this.syncStepLayout(); + } + + closeGuide(): void { + this.guideOnboarding.markStudioGuideSeen(); + this.pendingStudioResetOnContinue.set(false); + this.isOpen.set(false); + this.isCollapsed.set(false); + this.activeSteps.set([]); + this.currentIndex.set(0); + this.highlightRect.set(null); + this.interactionCutouts.set([]); + this.blockingMaskRects.set([]); + this.disconnectTargetObserver(); + this.disconnectDomObserver(); + this.clearLayoutTimer(); + this.clearTargetRetryTimer(); + this.clearAutoAdvanceTimer(); + } + + goToNextStep(): void { + if (!this.canGoToNext()) { + return; + } + + const step = this.currentStep(); + if (step?.id === 'launcher' && this.pendingStudioResetOnContinue()) { + this.experimentStudioService.resetStudioStateForGuide(); + this.pendingStudioResetOnContinue.set(false); + } + + if (this.isDashboardStep()) { + this.closeGuide(); + void this.router.navigate(['/experiments-dashboard']); + return; + } + + if (this.isLastStep()) { + this.closeGuide(); + return; + } + + const nextIndex = this.currentIndex() + 1; + this.currentIndex.set(nextIndex); + this.syncStepLayout(); + } + + goToPreviousStep(): void { + if (!this.canGoToPrevious()) { + return; + } + + const previousIndex = this.currentIndex() - 1; + this.currentIndex.set(previousIndex); + this.syncStepLayout(); + } + + toggleCollapsed(): void { + this.isCollapsed.update((collapsed) => !collapsed); + this.scheduleLayoutUpdate(0); + } + + onViewportChange(): void { + if (!this.isOpen()) { + return; + } + + this.scheduleLayoutUpdate(0); + } + + onWindowKeydown(event: KeyboardEvent): void { + if (!this.isOpen()) { + return; + } + + if (event.key === 'Escape') { + event.preventDefault(); + this.closeGuide(); + return; + } + + if (event.key === 'ArrowRight') { + if (isTypingTarget(event.target)) { + return; + } + event.preventDefault(); + this.goToNextStep(); + return; + } + + if (event.key === 'ArrowLeft') { + if (isTypingTarget(event.target)) { + return; + } + event.preventDefault(); + this.goToPreviousStep(); + } + } + + onDocumentClick(event: MouseEvent): void { + const step = this.currentStep(); + if (!this.isOpen() || !step?.advanceOnTargetClick || !step.selector) { + return; + } + + const clickedNode = event.target; + if (!(clickedNode instanceof Node)) { + return; + } + + const target = this.findTarget(step.selector); + if (!target || !target.contains(clickedNode)) { + return; + } + + window.setTimeout(() => { + if (this.isOpen() && this.currentStep()?.id === step.id) { + this.goToNextStep(); + } + }, 260); + } + + private resolveSteps(): ExperimentStudioGuideStep[] { + return EXPERIMENT_STUDIO_GUIDE_STEPS.map((step) => ({ + ...step, + title: this.replaceGuideTargets(step.title), + body: this.replaceGuideTargets(step.body), + })); + } + + private replaceGuideTargets(text: string): string { + // Labels come from runtime env and land in strings bound with [innerHTML], + // so escape them; the static step HTML keeps its markup. + const covariateLabel = escapeHtml(this.guideCovariateLabel); + const variableLabel = escapeHtml(this.guideVariableLabel); + + return text + .replace(/\bSex\b/g, covariateLabel) + .replace(/\bAge\b/g, variableLabel) + .replace(/\{\{GUIDE_COVARIATE\}\}/g, covariateLabel) + .replace(/\{\{GUIDE_VARIABLE\}\}/g, variableLabel); + } + + private syncStepLayout(): void { + const step = this.currentStep(); + if (!step) { + return; + } + + // A step change must never let a pending retry apply to the new step. + this.clearTargetRetryTimer(); + + // Reveal the owning view before measuring the target (hidden views have no size). + this.activateViewForStep(step); + + const target = step.selector ? this.findTarget(step.selector) : null; + this.focusStepTarget(step, target); + + // The owning view may not be rendered yet after activation, so retry the + // target scroll on a bounded schedule until the view has appeared. + if (!target && step.selector) { + this.scheduleTargetRetry(step); + } + } + + /** + * Observes and scrolls to the step target, then lets the smooth scroll settle + * before the spotlight is re-measured. + */ + private focusStepTarget(step: ExperimentStudioGuideStep, target: HTMLElement | null): void { + this.observeTarget(target); + if (target) { + target.scrollIntoView({ + behavior: 'smooth', + block: this.getScrollBlock(step), + inline: 'nearest', + }); + } + this.scheduleLayoutUpdate(target ? 260 : 0, true); + } + + /** + * Bounded retry for a step whose target is not measurable yet because its view + * is still rendering. Stops when the target appears, the step changes, or the + * attempts run out (then the spotlight simply stays off, as for any no-selector step). + */ + private scheduleTargetRetry(step: ExperimentStudioGuideStep, left = 12): void { + this.clearTargetRetryTimer(); + + this.targetRetryTimer = window.setTimeout(() => { + this.targetRetryTimer = null; + if (!this.isOpen() || this.currentStep()?.id !== step.id || !step.selector) { + return; + } + + const target = this.findTarget(step.selector); + if (target) { + this.focusStepTarget(step, target); + } else if (left > 1) { + this.scheduleTargetRetry(step, left - 1); + } + }, 150); + } + + private clearTargetRetryTimer(): void { + if (this.targetRetryTimer !== null) { + window.clearTimeout(this.targetRetryTimer); + this.targetRetryTimer = null; + } + } + + private scheduleLayoutUpdate(delayMs: number, focusGuideCard = false): void { + this.clearLayoutTimer(); + this.layoutTimer = window.setTimeout(() => this.updateLayout(focusGuideCard), delayMs); + } + + private clearLayoutTimer(): void { + if (this.layoutTimer !== null) { + window.clearTimeout(this.layoutTimer); + this.layoutTimer = null; + } + } + + private clearAutoAdvanceTimer(): void { + if (this.autoAdvanceTimer !== null) { + window.clearTimeout(this.autoAdvanceTimer); + this.autoAdvanceTimer = null; + } + } + + private observeTarget(target: HTMLElement | null): void { + if (this.observedTarget === target) { + return; + } + + this.disconnectTargetObserver(); + this.observedTarget = target; + + if (!target || typeof ResizeObserver === 'undefined') { + return; + } + + this.targetResizeObserver = new ResizeObserver(() => { + if (this.isOpen()) { + this.scheduleLayoutUpdate(0); + } + }); + this.targetResizeObserver.observe(target); + } + + private disconnectTargetObserver(): void { + this.targetResizeObserver?.disconnect(); + this.targetResizeObserver = null; + this.observedTarget = null; + } + + private startDomObserver(): void { + if (this.domMutationObserver || typeof MutationObserver === 'undefined') { + return; + } + + const root = this.document.querySelector('.studio-main'); + if (!(root instanceof HTMLElement)) { + return; + } + + this.domMutationObserver = new MutationObserver(() => { + this.domRevision.update((revision) => revision + 1); + if (this.isOpen()) { + this.scheduleLayoutUpdate(0); + } + }); + + this.domMutationObserver.observe(root, { + childList: true, + subtree: true, + attributes: true, + }); + } + + private disconnectDomObserver(): void { + this.domMutationObserver?.disconnect(); + this.domMutationObserver = null; + } + + private updateLayout(focusGuideCard = false): void { + const step = this.currentStep(); + if (!step) { + return; + } + + // Ensure the owning view is active so the target is measurable. + this.activateViewForStep(step); + + const target = step.selector ? this.findTarget(step.selector) : null; + if (target) { + // The target appeared on its own, so there is nothing left to poll for. + this.clearTargetRetryTimer(); + } + this.highlightRect.set(target ? this.expandRect(target.getBoundingClientRect()) : null); + + const cutoutTargets = step.allowTargetInteraction + ? this.getInteractionSelectors(step) + .map((selector) => this.findTarget(selector)) + .filter((element): element is HTMLElement => !!element) + : []; + const cutouts = cutoutTargets.map((element) => measureGuideInteractionRect(element)); + this.interactionCutouts.set(cutouts); + this.blockingMaskRects.set(computeGuideBlockingRects(cutouts)); + + if (focusGuideCard && this.canFocusGuideCard()) { + window.setTimeout(() => this.guideCard?.nativeElement.focus(), 0); + } + } + + private getInteractionSelectors(step: ExperimentStudioGuideStep): string[] { + return [ + step.selector, + ...(step.interactionSelectors ?? []), + ].filter((selector, index, selectors): selector is string => + !!selector && selectors.indexOf(selector) === index + ); + } + + private canFocusGuideCard(): boolean { + return !isTypingTarget(this.document.activeElement); + } + + /** + * Activates the studio view that owns this guide step so its target is visible, + * and opens the relevant sub-tab for steps inside a tabbed view. Steps are + * grouped by section: Explore -> variables, Analysis -> statistics, + * Experiment -> algorithm, Results -> execution. + */ + private activateViewForStep(step: ExperimentStudioGuideStep): void { + if (!this.activateGuideTarget) { + return; + } + this.activateGuideTarget(step.section, step.id); + } + + private getScrollBlock(step: ExperimentStudioGuideStep): ScrollLogicalPosition { + const selector = step.selector ?? ''; + if ( + (selector.includes('analysis-') + && !selector.includes('analysis-raw-summary') + && !selector.includes('analysis-raw-details') + && !selector.includes('analysis-processed-summary') + && !selector.includes('analysis-processed-details')) + || selector === '[data-guide="experiment-workspace"]' + || selector === '[data-guide="analysis-section"]' + ) { + return 'start'; + } + return 'center'; + } + + private expandRect(rect: DOMRect, padding = 10): GuideRect { + const left = rect.left - padding; + const top = rect.top - padding; + const right = rect.right + padding; + const bottom = rect.bottom + padding; + + return { + top, + left, + right, + bottom, + width: Math.max(right - left, 0), + height: Math.max(bottom - top, 0), + }; + } + + private findTarget(selector: string): HTMLElement | null { + const target = this.document.querySelector(selector); + if (!(target instanceof HTMLElement)) { + return null; + } + + const rect = target.getBoundingClientRect(); + if (rect.width <= 0 || rect.height <= 0) { + return null; + } + + return target; + } + + private isStepRequirementSatisfied(step: ExperimentStudioGuideStep | null): boolean { + if (!step?.requirement) { + return true; + } + + this.domRevision(); + + switch (step.requirement) { + case 'selected-sex': + return this.hasSelectedHierarchyNode(this.guideState.guideCovariate.value); + case 'variable-sex': + return this.hasVariable(this.guideState.guideCovariate.value); + case 'selected-age': + return this.hasVariable(this.guideState.guideCovariate.value) + && this.hasSelectedHierarchyNode(this.guideState.guideVariable.value); + case 'variable-age': + return this.hasVariable(this.guideState.guideCovariate.value) + && this.hasVariable(this.guideState.guideVariable.value); + case 'roles-assigned': + // The guide 'variable' target (e.g. age) must be assigned to y and the + // guide 'covariate' target (e.g. sex) to x, so the tutorial stays on a + // mapping compatible with the available algorithms (y max 1). + return this.hasRoleVariable(this.guideState.guideVariable.value) + && this.hasRoleCovariate(this.guideState.guideCovariate.value); + case 'algorithm-selected': + return !!this.experimentStudioService.selectedAlgorithm(); + case 'experiment-result-ready': + // Prefer service state: the Execution view can be display:none while the + // Run step still owns Algorithm Selection, so a DOM size check would miss it. + return !!this.experimentStudioService.runResult(); + case 'save-as-opened': + return !!this.findTarget('[data-guide="save-as-form"]'); + case 'experiment-saved-as': { + const baselineUuid = this.experimentSaveBaselineUuid(); + const currentUuid = this.experimentStudioService.currentExperimentUUID(); + // Saving no longer re-runs the experiment, so the result stays on screen: the new + // experiment id alone marks a successful save. + return baselineUuid !== undefined + && !!currentUuid + && currentUuid !== baselineUuid; + } + default: + // Compile-time net: a requirement added to the union without a case fails the build. + return step.requirement satisfies never; + } + } + + private shouldAutoAdvance(step: ExperimentStudioGuideStep | null): boolean { + return step?.requirement === 'selected-sex' + || step?.requirement === 'variable-sex' + || step?.requirement === 'selected-age' + || step?.requirement === 'variable-age' + || step?.requirement === 'roles-assigned' + || step?.requirement === 'algorithm-selected' + || step?.requirement === 'experiment-result-ready' + || step?.requirement === 'save-as-opened' + || step?.requirement === 'experiment-saved-as'; + } + + /** + * Pool membership: does the code exist in the variables-panel pool + * (selectedVariables). Used by the Explore add-to-pool steps. + */ + private hasVariable(expected: string): boolean { + return this.experimentStudioService.selectedVariables().some((node) => + this.guideState.matchesTutorialCovariate(node, expected) + ); + } + + /** Role membership: code assigned as outcome (algorithmY). */ + private hasRoleVariable(expected: string): boolean { + return this.experimentStudioService.algorithmY().some((node) => + this.guideState.matchesTutorialCovariate(node, expected) + ); + } + + /** Role membership: code assigned as predictor/covariate (algorithmX). */ + private hasRoleCovariate(expected: string): boolean { + return this.experimentStudioService.algorithmX().some((node) => + this.guideState.matchesTutorialCovariate(node, expected) + ); + } + + private hasSelectedHierarchyNode(expected: string): boolean { + return this.guideState.matchesTutorialCovariate(this.guideState.selectedHierarchyNode(), expected); + } + +} 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 new file mode 100644 index 000000000..e76557146 --- /dev/null +++ b/src/app/pages/experiment-studio/guide/experiment-studio-guide.content.ts @@ -0,0 +1,297 @@ +export type GuideSection = 'Explore' | 'Analysis' | 'Experiment' | 'Results'; + +export interface ExperimentStudioGuideStep { + id: string; + section: GuideSection; + title: string; + body: string; + compactTitle?: boolean; + selector?: string; + interactionSelectors?: string[]; + allowTargetInteraction?: boolean; + advanceOnTargetClick?: boolean; + requirement?: + | 'selected-sex' + | 'variable-sex' + | 'selected-age' + | 'variable-age' + | 'roles-assigned' + | 'algorithm-selected' + | 'experiment-result-ready' + | 'save-as-opened' + | 'experiment-saved-as'; + optional?: boolean; +} + +export const EXPERIMENT_STUDIO_GUIDE_LABELS = { + launcher: 'User Guide', + exit: 'Exit', + collapse: 'Collapse', + expand: 'Expand', + back: 'Back', + next: 'Next', + skip: 'Skip', + done: 'Done', + moveToDashboard: 'Move to dashboard', +} as const; + +export const EXPERIMENT_STUDIO_GUIDE_STEPS: ExperimentStudioGuideStep[] = [ + { + id: 'welcome', + section: 'Explore', + title: 'Experiment Studio User Guide', + body: 'This guide walks you through Experiment Studio step by step. During interactive steps, you can only interact with the highlighted <strong>blue panel</strong> on the page—the rest of the interface stays dimmed until you press Next.', + }, + { + id: 'launcher', + section: 'Explore', + title: 'Guide Launcher', + body: 'Use this button to reopen the guide at any time on the current page. While the guide is active, interaction is limited to the highlighted blue panel; use Back and Next here to move between steps.', + selector: '[data-guide="launcher"]', + }, + { + id: 'header-account', + section: 'Explore', + title: 'Account menu', + body: 'Open the account control to reach <strong>Account settings</strong> or <strong>Sign Out</strong>. Primary navigation for <strong>My experiments</strong> and <strong>Studio</strong> lives in the header to the left.', + selector: '[data-guide="header-account"]', + }, + { + id: 'navigation', + section: 'Explore', + title: 'Studio step bar', + body: 'Use the step bar to move between <strong>Data Exploration</strong>, <strong>Data Handling</strong>, <strong>Algorithm Selection</strong>, and <strong>Execution Results</strong>. The CTA on the right starts as <strong>Continue</strong>, becomes <strong>Continue with N variables</strong> after you add variables, <strong>Continue to Algorithm Selection</strong> on Data Handling, and <strong>Run experiment</strong> on Algorithm Selection. Switch to the list anytime with <strong>My experiments</strong> in the header — not from this step bar.', + selector: '[data-guide="studio-navigation"]', + }, + { + id: 'study-context', + section: 'Explore', + title: 'Pathology & Datasets', + body: 'Choose the pathology you want to work with, then pick the datasets or cohorts to include in the current analysis. Click the chip to open both selectors — the available variables update from this selection.', + selector: '[data-guide="study-context"]', + allowTargetInteraction: true, + interactionSelectors: [ + '[data-guide="data-model-selector"]', + '[data-guide="dataset-selector"]', + ], + }, + { + id: 'search-variables', + section: 'Explore', + title: 'Search Variables', + body: 'Click the search icon to open the search bar, then search for variables or groups and narrow the results by Variables or Groups and by variable type.', + selector: '[data-guide="search-bar"]', + allowTargetInteraction: true, + }, + { + id: 'variable-selection', + section: 'Explore', + title: 'Explore Variable Views', + body: 'Browse variables in <strong>Map</strong>, <strong>List</strong>, or <strong>Graph</strong> — try switching modes here, or press <strong>Next</strong>. Charts and details appear on the right.', + selector: '[data-guide="variable-selection"]', + allowTargetInteraction: true, + }, + { + id: 'variable-details', + section: 'Explore', + title: 'Variable Details', + body: 'This panel shows the selected variable histogram or the selected group information. Export actions appear at the top right corner when histogram data is available.', + selector: '[data-guide="variable-details"]', + }, + { + id: 'variable-containers', + section: 'Explore', + title: 'Compose Your Experiment', + body: 'Select an item in the Map or List view, then click <strong>Add</strong> (or double-click the item). Every added variable stays visible in the <strong>Variables in this experiment</strong> card, where you can jump to it or remove it.', + selector: '[data-guide="variable-containers"]', + }, + { + id: 'select-sex-variable', + section: 'Explore', + title: 'Select the <span class="guide-copy-green">green</span>-highlighted {{GUIDE_COVARIATE}} variable.', + body: '', + compactTitle: true, + selector: '[data-guide="variable-selection"]', + allowTargetInteraction: true, + requirement: 'selected-sex', + }, + { + id: 'preview-sex-variable-details', + section: 'Explore', + title: 'Preview {{GUIDE_COVARIATE}} Details', + body: 'Review the histogram and metadata for the selected variable. Switch between <strong>Chart</strong> and <strong>Details</strong>, and use export actions when available.', + selector: '[data-guide="variable-details"]', + allowTargetInteraction: true, + }, + { + id: 'add-sex-covariate', + section: 'Explore', + title: 'Add {{GUIDE_COVARIATE}} as Variable', + body: '', + selector: '[data-guide="guide-add-variable"]', + allowTargetInteraction: true, + requirement: 'variable-sex', + }, + { + id: 'select-age-variable', + section: 'Explore', + title: 'Select the <span class="guide-copy-green">green</span>-highlighted {{GUIDE_VARIABLE}} variable.', + body: '', + compactTitle: true, + selector: '[data-guide="variable-selection"]', + allowTargetInteraction: true, + interactionSelectors: ['[data-guide="variable-details"]'], + requirement: 'selected-age', + }, + { + id: 'preview-age-variable-details', + section: 'Explore', + title: 'Preview {{GUIDE_VARIABLE}} Details', + body: 'Review the histogram and metadata for the selected variable. Switch between <strong>Chart</strong> and <strong>Details</strong>, and use export actions when available.', + selector: '[data-guide="variable-details"]', + allowTargetInteraction: true, + }, + { + id: 'add-age-variable', + section: 'Explore', + title: 'Add {{GUIDE_VARIABLE}} as Variable', + body: '', + selector: '[data-guide="guide-add-variable"]', + allowTargetInteraction: true, + requirement: 'variable-age', + }, + { + id: 'analysis-intro', + section: 'Analysis', + title: 'Data Handling', + body: 'Data Handling is optional: Filtering, Preprocessing, and Transformation, plus a read-only Raw data preview. Click a step header to open or fold it. A chip reading <strong>In run</strong> or <strong>Applied</strong> means the step is in the run; <strong>Not in run yet</strong> means it changes nothing. Tour the stations next or skip with <strong>Next</strong>, then <strong>Continue to Algorithm Selection</strong>.', + selector: '[data-guide="analysis-section"]', + allowTargetInteraction: true, + }, + { + id: 'analysis-filtering', + section: 'Analysis', + title: '1. Filtering', + body: 'Optional — click <strong>Add condition</strong> to try a rule, or press <strong>Next</strong> to skip. <strong>Preview data</strong> shows the cohort; <strong>Apply</strong> saves rules, <strong>Close</strong> leaves an empty step.', + selector: '[data-guide="analysis-filtering"]', + allowTargetInteraction: true, + }, + { + id: 'analysis-raw-statistics', + section: 'Analysis', + title: '2. Raw Data Summary', + body: 'Optional — use Filtering <strong>Preview data</strong> for tables/charts, then <strong>Close</strong>. Press <strong>Next</strong> if you do not need this view.', + selector: '[data-guide="analysis-raw-summary"]', + allowTargetInteraction: true, + }, + { + id: 'analysis-preprocessing', + section: 'Analysis', + title: '3. Preprocessing', + body: 'Optional — missing values are already dropped (<strong>Default</strong>). Open the card to change imputation or add outlier clipping, or press <strong>Next</strong> to keep defaults.', + selector: '[data-guide="analysis-preprocessing"]', + allowTargetInteraction: true, + }, + { + id: 'analysis-processed-summary', + section: 'Analysis', + title: '4. Processed Data Summary', + body: 'Optional — Preprocessing <strong>Preview data</strong> shows processed tables after defaults/edits. Press <strong>Next</strong> to continue.', + selector: '[data-guide="analysis-processed-summary"]', + allowTargetInteraction: true, + }, + { + id: 'analysis-transformation', + section: 'Analysis', + title: '5. Transformation', + body: 'Optional — pick a column type to create a derived column, or press <strong>Next</strong> to skip. Then choose <strong>Continue to Algorithm Selection</strong>.', + selector: '[data-guide="analysis-transformation"]', + allowTargetInteraction: true, + }, + { + id: 'experiment-intro', + section: 'Experiment', + title: 'Experiment workspace', + body: 'This step is for assigning outcome and predictors, choosing an algorithm, configuring parameters, then running from the step-bar CTA on the right (<strong>Run experiment</strong>).', + selector: '[data-guide="experiment-workspace"]', + allowTargetInteraction: false, + }, + { + id: 'experiment-role-assignment', + section: 'Experiment', + title: 'Assign outcome & predictors', + body: 'In the <strong>Experiment pool</strong>, set each variable as <strong>Outcome y</strong> or <strong>Predictor x</strong> (usually one outcome). Assigned variables move into the rails above — drag a chip between rails to move it, or use × to return it to the list. Matching algorithms update as you assign. A created transformation column can also be assigned here.', + selector: '[data-guide="guide-role-assignment"]', + allowTargetInteraction: true, + requirement: 'roles-assigned', + }, + { + id: 'experiment-select-algorithm', + section: 'Experiment', + title: 'Algorithm Selection', + body: 'The catalog on the left lists runnable methods per group by default — switch the chip to All to see unavailable ones and their reasons. Click a method to open its documentation and parameters in <strong>Algorithm configuration</strong> beside the catalog.', + selector: '[data-guide="algorithm-selection"]', + allowTargetInteraction: true, + requirement: 'algorithm-selected', + }, + { + id: 'experiment-run', + section: 'Experiment', + title: 'Run experiment', + body: 'Review documentation and optional parameters in Algorithm configuration, then press <strong>Run experiment</strong> on the right of the step bar. The guide waits until Execution Results shows a result.', + selector: '[data-guide="run-experiment"]', + allowTargetInteraction: true, + requirement: 'experiment-result-ready', + }, + { + id: 'experiment-explore-result', + section: 'Results', + title: 'Explore the result', + body: 'Execution Results shows the result title, charts/tables, and a toolbar with <strong>Edit parameters</strong>, <strong>Save as</strong>, and <strong>Export PDF</strong>. Inspect the output here, then press Next when you are ready to continue.', + selector: '[data-guide="experiment-result"]', + allowTargetInteraction: true, + }, + { + id: 'experiment-edit-parameters', + section: 'Results', + title: 'Edit parameters', + body: 'Use <strong>Edit parameters</strong> to go back to Algorithm Selection and adjust the setup before saving or running again.', + selector: '[data-guide="edit-parameters-action"]', + allowTargetInteraction: false, + }, + { + id: 'experiment-result-actions', + section: 'Results', + title: 'Result actions', + body: 'The result toolbar groups follow-ups. This tour continues with <strong>Save as</strong>; <strong>Edit parameters</strong> and <strong>Export PDF</strong> stay available on any result.', + selector: '[data-guide="save-as-flow"]', + allowTargetInteraction: true, + }, + { + id: 'experiment-save-as-action', + section: 'Results', + title: 'Save as', + body: 'Use <strong>Save as</strong> to keep this result as a named experiment. That opens the inline save form in the result toolbar.', + selector: '[data-guide="save-as-action"]', + allowTargetInteraction: true, + advanceOnTargetClick: true, + requirement: 'save-as-opened', + }, + { + id: 'experiment-wait-for-save', + section: 'Results', + title: 'Save experiment', + body: 'Enter an <strong>Experiment name</strong>, then click <strong>Save experiment</strong>. The guide continues automatically after a successful save (you can Cancel to stay on the result).', + selector: '[data-guide="save-as-form"]', + allowTargetInteraction: true, + requirement: 'experiment-saved-as', + }, + { + id: 'experiment-finish', + section: 'Results', + title: 'Studio guide done', + body: 'You finished the Studio path through Execution Results. Next opens <strong>My experiments</strong>. That page has its own guide, started from the header compass.', + selector: '[data-guide="dashboard-nav"]', + allowTargetInteraction: true, + } +]; diff --git a/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.css b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.css new file mode 100644 index 000000000..f1a3ccb7b --- /dev/null +++ b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.css @@ -0,0 +1,568 @@ +.inline-filter-builder { + display: block; + width: 100%; +} + +.inline-filter-builder .filter-modal { + width: 100%; + max-width: none; + max-height: none; + border-radius: 0; + box-shadow: none; + border: none; + background: transparent; + animation: none; +} + +.inline-filter-builder .filter-modal-body { + padding: 0; + background: transparent; + flex: 0 0 auto; + min-height: 0; + overflow: visible; +} + +/* Base modal body fallback */ +.filter-modal-body { + padding: 16px; + background: #ffffff; +} + +.where-error { + border: 1px solid color-mix(in srgb, var(--danger, #dc2626) 42%, transparent); + border-radius: var(--radius-md, 8px); + background: #fef2f2; + color: #991b1b; + font-size: 0.82rem; + font-weight: 700; + padding: 10px 14px; + margin-top: 10px; +} + +/* ===== STREAMLINED TREE GROUP ===== */ +.filter-block-group { + display: flex; + flex-direction: column; + gap: 10px; + width: 100%; + box-sizing: border-box; +} + +.filter-block-group.is-root { + border: none; + background: transparent; + padding: 0; +} + +.filter-block-group.is-nested { + position: relative; + margin: 0; + padding: 4px 0 6px; + background: var(--card-bg); + border: 0; + border-radius: var(--radius-md); + box-shadow: inset 0 0 0 1px var(--studio-card-border); +} + +.filter-tree-node:has(> .filter-block-group.is-nested) { + padding: 10px 12px 10px 66px; + background: var(--studio-card-header-bg); +} + +.filter-block-group.is-nested .filter-group-header { + padding: 6px 12px 4px; +} + +.filter-block-group.is-nested .filter-tree-rail { + border: 0; +} + +/* Header & Scope Line */ +.filter-group-header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + min-height: 32px; +} + +.filter-group-scope { + display: inline-flex; + align-items: center; + gap: 10px; + font-size: 0.86rem; + font-weight: 400; + color: #334155; +} + +.scope-text { + font-size: inherit; + font-weight: inherit; + color: inherit; +} + +.scope-tray { + display: inline-flex; + gap: 2px; + padding: 3px; + border-radius: 999px; + background: rgba(148, 163, 184, 0.12); + box-shadow: inset 0 0 0 1px var(--border-color); +} + +.scope-tray button { + height: 26px; + padding: 0 12px; + border: 0; + border-radius: 999px; + background: transparent; + color: var(--text-muted); + font: inherit; + font-size: 0.78rem; + font-weight: 600; + cursor: pointer; +} + +.scope-tray button.active { + background: var(--card-bg); + color: var(--primary-color); + box-shadow: var(--shadow-sm); +} + +.scope-tray button:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +.filter-group-remove { + border: 0; + background: transparent; + min-height: 0; + padding: 0 6px; + color: var(--text-muted); + font-size: 0.76rem; + font-weight: 600; +} + +.filter-group-remove:hover { + color: var(--primary-color); + background: transparent; + box-shadow: none; +} + +.filter-group-actions { + display: flex; + align-items: center; + gap: 6px; +} + +/* ===== GUIDE RAIL & TREE NODES ===== */ +.filter-tree-rail { + position: relative; + padding-left: 0; + display: flex; + flex-direction: column; + gap: 0; + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-md); + overflow: hidden; +} + +.filter-tree-rail::before, +.filter-tree-node::before { + display: none; +} + +.filter-tree-node { + position: relative; + display: flex; + flex-direction: column; +} + +.filter-tree-node + .filter-tree-node { + border-top: 1px solid color-mix(in srgb, var(--primary-color) 6%, transparent); +} + +/* ===== RESTING ROW: the <summary> of a .condition-row disclosure ===== */ +.compact-condition-row { + display: grid; + grid-template-columns: 44px minmax(0, 1.3fr) 150px minmax(0, 1.2fr) 36px; + gap: 10px; + align-items: center; + list-style: none; + min-height: 0; + padding: 8px 12px; + border: 0; + border-radius: 0; + background: transparent; + cursor: pointer; + user-select: none; +} + +.compact-condition-row:hover { + border: 0; + background: rgba(43, 51, 233, 0.03); + box-shadow: none; +} + +.compact-condition-row::-webkit-details-marker { + display: none; +} + +.compact-condition-row:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: -2px; +} + +.compact-join { + font-size: 0.72rem; + font-weight: 600; + color: var(--text-muted); +} + +.compact-variable { + display: inline-flex; + align-items: center; + gap: 8px; + min-width: 0; + font-size: 0.84rem; + font-weight: 600; + color: var(--text-main); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +/* Variable missing from a selected dataset: grayed out, never resolved as the condition field. */ +.compact-variable.is-unavailable { + color: var(--unavailable-text); + cursor: not-allowed; +} + +/* .field-col: outranks the page-wide `.experiment-studio-page input:not(...)` reset. */ +.field-col .filter-field-input.is-unavailable { + color: var(--unavailable-text); + border-style: dashed; +} + +.field-col.field-variable:has(.filter-unavailable-note) { + flex-wrap: wrap; +} + +.filter-unavailable-note { + display: block; + flex-basis: 100%; + margin-top: 4px; + color: #64748b; + font-size: 0.72rem; +} + +.compact-operator { + padding: 0; + border: 0; + background: none; + font-size: 0.82rem; + font-weight: 600; + color: var(--primary-color); + white-space: nowrap; +} + +.compact-value { + display: inline-flex; + align-items: center; + flex-wrap: wrap; + gap: 4px; + max-width: none; + padding: 0; + border: 0; + background: none; + font-size: 0.84rem; + font-weight: 600; + color: var(--text-main); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.type-badge { + border: 0; + background: none; + padding: 0; + font-family: var(--font-mono); + font-size: 0.7rem; + font-weight: 400; + text-transform: none; + letter-spacing: 0; + color: var(--text-muted); + white-space: nowrap; + pointer-events: none; + line-height: 1.4; +} + +.compact-delete-btn { + width: 28px; + height: 28px; + display: inline-flex; + align-items: center; + justify-content: center; + background: transparent; + border: none; + color: var(--text-muted); + cursor: pointer; + padding: 0; + border-radius: 999px; + font-size: 0.82rem; + transition: all 0.15s ease; +} + +.compact-delete-btn:hover { + background: color-mix(in srgb, var(--danger) 8%, white); + color: #991b1b; +} + +.compact-delete-btn.is-expanded-action { + align-self: flex-end; +} + +/* ===== EXPANDED EDIT STATE: the open side of the same disclosure ===== */ +.expanded-row { + display: flex; + align-items: flex-start; + gap: 8px; + margin: 0 6px 6px; + background: var(--studio-card-bg, #ffffff); + border: 0; + border-radius: var(--radius-md); + box-shadow: inset 0 0 0 1px var(--primary-color), 0 0 0 3px rgba(127, 156, 232, 0.24); + padding: 10px; +} + +.expanded-row-fields { + flex: 1; + min-width: 0; +} + +/* Condition fields horizontal row */ +.condition-fields { + display: grid; + grid-template-columns: minmax(180px, 1.2fr) minmax(140px, 0.9fr) minmax(180px, 1.4fr); + gap: 8px; + align-items: flex-start; + width: 100%; + min-width: 0; +} + +.field-col { + position: relative; + display: flex; + align-items: flex-start; + min-width: 0; +} + +.field-col.is-multi { + flex-direction: column; +} + +.filter-field-input { + width: 100%; + min-height: 32px; + border: 1px solid rgba(43, 51, 233, 0.16); + border-radius: var(--radius-md); + background: #ffffff; + color: var(--text-main); + font-size: 0.82rem; + padding: 4px 10px; + box-sizing: border-box; + outline: none; + transition: border-color 0.15s ease, box-shadow 0.15s ease; +} + +.filter-field-input:focus { + border-color: var(--primary-color); + box-shadow: 0 0 0 2px rgba(43, 51, 233, 0.1); +} + +.type-badge.is-inline { + position: absolute; + right: 8px; + top: 7px; + border: 0; + background: none; + padding: 0; + font-family: var(--font-mono); + font-size: 0.7rem; + font-weight: 400; + text-transform: none; + letter-spacing: 0; + color: var(--text-muted); + pointer-events: none; +} + +.filter-field-input.unary-disabled { + background: var(--bg-neutral, #f8fafc); + color: var(--text-muted); + border-style: dashed; + border-color: #cbd5e1; + cursor: not-allowed; + user-select: none; +} + +.value-chip { + display: inline-flex; + align-items: center; + border-radius: 999px; + padding: 2px 8px; + font-size: 0.74rem; + font-weight: 600; +} + +.value-chip-more { + color: var(--text-muted); + background: rgba(148, 163, 184, 0.12); +} + +/* Multi-value Category Chip Picker */ +.value-chip-picker { + display: flex; + flex-direction: column; + gap: 6px; + width: 100%; +} + +.value-chip-list { + display: flex; + flex-wrap: wrap; + gap: 5px; +} + +.value-chip { + background: #ffffff; + border: 1px solid rgba(43, 51, 233, 0.18); + border-radius: 999px; + padding: 3px 10px; + font-size: 0.76rem; + font-weight: 600; + color: var(--text-main); + cursor: pointer; + transition: all 0.15s ease; +} + +.value-chip:hover { + border-color: var(--primary-color); + background: var(--bg-color, #f3f8ff); +} + +.value-chip.is-selected { + background: var(--primary-color); + border-color: var(--primary-color); + color: #ffffff; + font-weight: 700; +} + +.value-chip-count { + font-size: 0.72rem; + color: var(--text-muted); + font-weight: 600; +} + +/* Action Buttons & Footers */ +.filter-group-foot { + display: flex; + align-items: center; + gap: 8px; + padding-left: 0; + margin-top: 4px; +} + +.builder-token-btn { + height: 30px; + min-height: 0; + padding: 0 14px; + border: 1px dashed rgba(43, 51, 233, 0.3); + border-radius: 999px; + background: transparent; + color: var(--primary-color); + cursor: pointer; + font-family: inherit; + font-size: 0.78rem; + font-weight: 600; + display: inline-flex; + align-items: center; + gap: 6px; + transition: all 0.15s ease; +} + +.builder-token-btn:hover { + border-color: var(--primary-color); + background: rgba(43, 51, 233, 0.04); + box-shadow: none; +} + +.builder-token-btn.muted { + color: var(--text-muted); + border-color: color-mix(in srgb, var(--text-muted) 30%, transparent); +} + +.builder-token-btn.muted:hover { + color: var(--text-main); + border-color: var(--text-muted); +} + +.filter-block-group.is-nested .filter-group-foot { + padding-left: 54px; +} + +/* Empty group state */ +.empty-filter-group { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + border: 1px dashed rgba(43, 51, 233, 0.22); + border-radius: var(--radius-md, 8px); + color: var(--text-muted); + padding: 12px 16px; + background: rgba(248, 250, 252, 0.6); +} + +.empty-group-actions { + display: flex; + align-items: center; + gap: 6px; +} + +/* Preview Expression */ +.block-preview { + margin-top: 12px; + font-size: 0.78rem; + font-weight: 400; + color: var(--text-muted); +} + +.block-preview summary { + cursor: pointer; + font-weight: 600; + width: fit-content; +} + +.block-preview code { + display: block; + margin-top: 6px; + border: 0; + border-radius: var(--radius-md, 8px); + background: var(--bg-neutral, #f8fafc); + color: var(--text-main); + box-shadow: inset 0 0 0 1px var(--studio-card-border); + font-family: var(--font-mono); + font-weight: 400; + padding: 10px 14px; + white-space: normal; + line-height: 1.5; +} + +@media (max-width: 768px) { + .condition-fields { + grid-template-columns: minmax(0, 1fr); + gap: 6px; + } +} diff --git a/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.html b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.html new file mode 100644 index 000000000..f2ccd25b7 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.html @@ -0,0 +1,247 @@ +<div class="inline-filter-builder"> + <div class="filter-modal"> + <div class="filter-modal-body"> + @if(allFilterVariables().length > 0) { + <ng-container + [ngTemplateOutlet]="groupTemplate" + [ngTemplateOutletContext]="{ group: rootGroup(), depth: 0, removable: false }"> + </ng-container> + + @if (previewExpression()) { + <details class="block-preview"> + <summary>Show as expression</summary> + <code>{{ previewExpression() }}</code> + </details> + } + + @if (filterError()) { + <div class="where-error" role="alert">{{ filterError() }}</div> + } + } @else { + <div class="where-error" role="status">{{ emptyPoolMessage() }}</div> + } + </div> + </div> +</div> + +<ng-template #groupTemplate let-group="group" let-depth="depth" let-removable="removable"> + <div class="filter-block-group" [class.is-root]="depth === 0" [class.is-nested]="depth > 0"> + <div class="filter-group-header"> + <div class="filter-group-scope"> + <span class="scope-text">{{ depth === 0 ? 'Keep records that match' : 'Group · match' }}</span> + <div class="scope-tray" role="radiogroup" aria-label="Match"> + <button type="button" role="radio" [attr.aria-checked]="groupCondition(group) === 'AND'" + [class.active]="groupCondition(group) === 'AND'" (click)="setGroupCondition(group.id, 'AND')">all</button> + <button type="button" role="radio" [attr.aria-checked]="groupCondition(group) === 'OR'" + [class.active]="groupCondition(group) === 'OR'" (click)="setGroupCondition(group.id, 'OR')">any</button> + </div> + <span class="scope-text">{{ depth === 0 ? 'of these conditions' : 'of these' }}</span> + </div> + + <div class="filter-group-actions"> + @if (removable) { + <button + type="button" + class="builder-token-btn muted filter-group-remove" + title="Remove group" + (click)="removeBlock(group.id)" + >Remove group</button> + } + </div> + </div> + + @if (group.rules.length === 0) { + <div class="empty-filter-group"> + <span>No conditions yet.</span> + <div class="empty-group-actions"> + <button type="button" class="builder-token-btn" (click)="addCondition(group.id, 0)"> + <i class="fas fa-plus" aria-hidden="true"></i> Add condition + </button> + <button type="button" class="builder-token-btn muted" (click)="addGroup(group.id, 0)"> + <i class="fas fa-layer-group" aria-hidden="true"></i> Group + </button> + </div> + </div> + } @else { + <div class="filter-tree-rail"> + @for (block of group.rules; track block.id) { + <div class="filter-tree-node"> + @if (block.kind === 'condition') { + <!-- + <details>/<summary> owns the resting/expanded switch: click and keyboard come + with the element, activeBlockId only force-opens a row the builder just added. + --> + <details class="condition-row" [open]="activeBlockId() === block.id"> + <summary class="compact-condition-row" (click)="toggleActiveBlock(block.id)"> + <span class="compact-join">{{ $first ? 'where' : (groupCondition(group) === 'AND' ? 'and' : 'or') }}</span> + @let compactUnavailable = conditionUnavailableTooltip(block); + <span class="compact-variable" + [class.is-unavailable]="!!compactUnavailable" + [attr.aria-disabled]="compactUnavailable ? true : null" + [attr.title]="compactUnavailable || null"> + {{ block.variableText || 'Select variable...' }} + @if (variableType(block)) { + <span class="type-badge">{{ variableType(block) }}</span> + } + </span> + <span class="compact-operator">{{ operatorDisplayLabel(block.operator) }}</span> + @if (!isUnaryOperator(block.operator)) { + <span class="compact-value" [title]="compactValueSummary(block)"> + @if (isMultiValueOperator(block.operator) && categoryOptions(block).length) { + @for (category of restingCategoryOptions(block); track category.value) { + <span class="value-chip" [class.is-selected]="isCategorySelected(block, category.value)">{{ category.label }}</span> + } + @if (hiddenCategoryCount(block) > 0) { + <span class="value-chip value-chip-more">+{{ hiddenCategoryCount(block) }} more</span> + } + } @else { + {{ compactValueSummary(block) }} + } + </span> + } + <button + type="button" + class="compact-delete-btn" + title="Remove condition" + (click)="$event.preventDefault(); removeBlock(block.id)" + > + <i class="fas fa-xmark" aria-hidden="true"></i> + </button> + </summary> + + <div class="expanded-row"> + <div class="expanded-row-fields"> + <div class="condition-fields"> + @let fieldUnavailable = conditionUnavailableTooltip(block); + <div class="field-col field-variable"> + <input + type="text" + class="filter-field-input" + [class.is-unavailable]="!!fieldUnavailable" + [attr.title]="fieldUnavailable || null" + [attr.list]="'filter-variable-options-' + block.id" + [ngModel]="block.variableText" + (ngModelChange)="onConditionVariableTextChange(block.id, $event)" + placeholder="Type to find a variable" + /> + @if (variableType(block)) { + <span class="type-badge is-inline">{{ variableType(block) }}</span> + } + <datalist [id]="'filter-variable-options-' + block.id"> + @for (field of visibleFilterVariables(block.variableText); track field.code) { + <!-- A datalist option shows only its label; class, aria-disabled and title are ignored. --> + <option + [value]="variableDisplayLabel(field)" + [attr.label]="variableUnavailableTooltip(field) || null"></option> + } + </datalist> + @if (fieldUnavailable) { + <span class="filter-unavailable-note" role="note">{{ fieldUnavailable }}</span> + } + </div> + + <div class="field-col field-operator"> + <select + class="filter-field-input" + [ngModel]="block.operator" + (ngModelChange)="setConditionOperator(block.id, $event)" + > + @for (operator of operatorOptions(block); track operator) { + <option [ngValue]="operator">{{ operatorDisplayLabel(operator) }}</option> + } + </select> + </div> + + <div class="field-col field-value" [class.is-multi]="isMultiValueOperator(block.operator)"> + @if (isUnaryOperator(block.operator)) { + <input + type="text" + class="filter-field-input unary-disabled" + disabled + value="— (not required)" + /> + } @else if (isMultiValueOperator(block.operator) && categoryOptions(block).length > 0) { + <div class="value-chip-picker"> + <div + class="value-chip-list" + role="group" + [attr.aria-label]="'Categories for ' + (block.field || 'this variable')" + > + @for (category of categoryOptions(block); track category.value) { + <button + type="button" + class="value-chip" + [class.is-selected]="isCategorySelected(block, category.value)" + [attr.aria-pressed]="isCategorySelected(block, category.value)" + (click)="toggleCategory(block.id, category.value)" + >{{ category.label }}</button> + } + </div> + <span class="value-chip-count"> + {{ block.values.length }} of {{ categoryOptions(block).length }} + {{ categoryOptions(block).length === 1 ? 'category' : 'categories' }} selected + </span> + </div> + } @else if (isMultiValueOperator(block.operator)) { + <input + type="text" + class="filter-field-input" + [ngModel]="block.values.join(', ')" + (ngModelChange)="setMultiValueText(block.id, $event)" + placeholder="Value 1, Value 2, …" + /> + } @else if (categoryOptions(block).length > 0) { + <select + class="filter-field-input" + [ngModel]="block.values[0] ?? ''" + (ngModelChange)="setConditionValue(block.id, $event)" + > + <option value="">Choose category</option> + @for (category of categoryOptions(block); track category.value) { + <option [ngValue]="category.value">{{ category.label }}</option> + } + </select> + } @else { + <input + type="text" + class="filter-field-input" + [ngModel]="block.values[0] ?? ''" + (ngModelChange)="setConditionValue(block.id, $event)" + placeholder="Value" + /> + } + </div> + </div> + </div> + + <button + type="button" + class="compact-delete-btn is-expanded-action" + title="Remove condition" + (click)="removeBlock(block.id)" + > + <i class="fas fa-xmark" aria-hidden="true"></i> + </button> + </div> + </details> + } @else { + <ng-container + [ngTemplateOutlet]="groupTemplate" + [ngTemplateOutletContext]="{ group: block, depth: depth + 1, removable: true }"> + </ng-container> + } + </div> + } + </div> + + <div class="filter-group-foot"> + <button type="button" class="builder-token-btn" (click)="addCondition(group.id, group.rules.length)"> + <i class="fas fa-plus" aria-hidden="true"></i> Condition + </button> + <button type="button" class="builder-token-btn muted" (click)="addGroup(group.id, group.rules.length)"> + <i class="fas fa-layer-group" aria-hidden="true"></i> Group + </button> + </div> + } + </div> +</ng-template> diff --git a/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.spec.ts b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.spec.ts new file mode 100644 index 000000000..d9a68fb5a --- /dev/null +++ b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.spec.ts @@ -0,0 +1,559 @@ +import type { Mock } from 'vitest'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { FilterConfigModalComponent } from './filter-config-modal.component'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; + +describe('FilterConfigModalComponent block builder', () => { + let fixture: ComponentFixture<FilterConfigModalComponent>; + let component: FilterConfigModalComponent; + let expStudio: any; + + beforeEach(async () => { + expStudio = { + selectedDataModel: signal({ + code: 'dm', + label: 'Data model', + variables: [ + { code: 'age', label: 'Age', type: 'continuous' }, + { + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [ + { code: 'female', label: 'Female' }, + { code: 'male', label: 'Male' }, + ], + }, + ], + }), + selectedVariables: signal<any[]>([ + { + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [ + { code: 'female', label: 'Female' }, + { code: 'male', label: 'Male' }, + ], + }, + { code: 'mrs_score', label: 'mRS score', type: 'discrete' }, + { code: 'notes', label: 'Notes', type: 'nominal' }, + ]), + setFilters: vi.fn(), + setFilterLogic: vi.fn(), + unavailableVariables: signal<ReadonlyMap<string, readonly string[]>>(new Map()), + }; + + await TestBed.configureTestingModule({ + imports: [FilterConfigModalComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: expStudio }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(FilterConfigModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + component.allFilterVariables.set([ + { code: 'age', label: 'Age', type: 'continuous' }, + { + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [ + { code: 'female', label: 'Female' }, + { code: 'male', label: 'Male' }, + ], + }, + ]); + }); + + it('renders an existing nested filter tree as blocks', () => { + fixture.componentRef.setInput('filterLogic', { + condition: 'OR', + rules: [ + { field: 'age', operator: 'greater', value: 2 }, + { condition: 'AND', rules: [{ field: 'sex', operator: 'equal', value: 'female' }] }, + ], + }); + fixture.detectChanges(); + + expect(component.activeRulesCount()).toBe(2); + expect(component.previewExpression()).toContain('Age > 2'); + expect(component.previewExpression()).toContain('Sex = Female'); + }); + + it('associatively flattens nested groups that share the same condition', () => { + fixture.componentRef.setInput('filterLogic', { + condition: 'AND', + rules: [ + { + condition: 'AND', + rules: [ + { field: 'age', operator: 'greater', value: 2 }, + { field: 'sex', operator: 'equal', value: 'female' }, + ], + }, + { field: 'mrs_score', operator: 'less', value: 5 }, + ], + }); + fixture.detectChanges(); + + const root = component.rootGroup(); + expect(root.rules.length).toBe(3); + expect((root.rules[0] as any).field).toBe('age'); + expect((root.rules[1] as any).field).toBe('sex'); + expect((root.rules[2] as any).field).toBe('mrs_score'); + }); + + it('manages active focus block and compact value summary', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + const condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + component.setConditionOperator(condition.id, 'IN'); + component.toggleCategory(condition.id, 'female'); + component.toggleCategory(condition.id, 'male'); + + // Every edit replaces the block, so the summary is checked on the current one. + expect(component.compactValueSummary(component.rootGroup().rules[0] as any)).toBe('Female, Male'); + + // `<details>` owns the resting/expanded switch; the signal only follows it and + // force-opens the row addCondition just created. + expect(component.activeBlockId()).toBe(condition.id); + }); + + it('saves edited condition and nested group to backend filter payload', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + let condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Age (continuous)'); + component.setConditionOperator(condition.id, '>'); + component.setConditionValue(condition.id, '2'); + + component.addGroup(root.id, 1); + const group = component.rootGroup().rules[1] as any; + component.setGroupCondition(root.id, 'OR'); + component.addCondition(group.id, 0); + condition = (component.rootGroup().rules[1] as any).rules[0]; + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + component.setConditionValue(condition.id, 'female'); + + component.saveFilters(); + + expect(expStudio.setFilters).toHaveBeenCalledWith([ + expect.objectContaining({ code: 'age' }), + expect.objectContaining({ code: 'sex' }), + ]); + expect(expStudio.setFilterLogic).toHaveBeenCalledWith(expect.objectContaining({ + condition: 'OR', + rules: expect.any(Array), + valid: true, + })); + }); + + it('offers membership operators on nominal variables and exposes their categories', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + let condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + condition = component.rootGroup().rules[0] as any; + + expect(component.operatorOptions(condition)).toEqual(['=', '!=', 'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL']); + expect(component.categoryOptions(condition).map((item) => item.label)).toEqual(['Female', 'Male']); + }); + + it('shows nominal category labels in the preview while keeping stored codes', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + const condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + component.setConditionValue(condition.id, 'female'); + + expect(component.previewExpression()).toContain('Sex = Female'); + + component.saveFilters(); + + expect(expStudio.setFilterLogic).toHaveBeenCalledWith(expect.objectContaining({ + rules: [expect.objectContaining({ + field: 'sex', + value: 'female', + })], + })); + }); + + it('exposes null checks for numeric variables', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + let condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Age (continuous)'); + condition = component.rootGroup().rules[0] as any; + + expect(component.operatorOptions(condition)).toContain('IS NULL'); + expect(component.operatorOptions(condition)).toContain('IS NOT NULL'); + }); + + it('renders readable operator labels without changing stored operator values', () => { + expect(component.operatorDisplayLabel('=')).toBe('is'); + expect(component.operatorDisplayLabel('!=')).toBe('is not'); + expect(component.operatorDisplayLabel('>')).toBe('is greater than'); + expect(component.operatorDisplayLabel('>=')).toBe('is at least'); + expect(component.operatorDisplayLabel('<')).toBe('is less than'); + expect(component.operatorDisplayLabel('<=')).toBe('is at most'); + expect(component.operatorDisplayLabel('IS NULL')).toBe('is empty'); + expect(component.operatorDisplayLabel('IS NOT NULL')).toBe('is not empty'); + }); + + it('saves null checks without requiring a value', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + const condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Age (continuous)'); + component.setConditionOperator(condition.id, 'IS NULL'); + + component.saveFilters(); + + expect(component.filterError()).toBeNull(); + expect(expStudio.setFilterLogic).toHaveBeenCalledWith(expect.objectContaining({ + rules: [expect.objectContaining({ + field: 'age', + operator: 'is_null', + value: null, + })], + })); + }); + + it('renders existing null checks as unary preview expressions', () => { + fixture.componentRef.setInput('filterLogic', { + condition: 'OR', + rules: [ + { field: 'age', operator: 'greater', value: 2 }, + { field: 'age', operator: 'is_null', value: null }, + ], + }); + fixture.detectChanges(); + + expect(component.previewExpression()).toContain('Age > 2'); + expect(component.previewExpression()).toContain('Age IS NULL'); + }); + + it('keeps a stored OR group inside an AND group instead of flattening it', () => { + fixture.componentRef.setInput('filterLogic', { + condition: 'AND', + rules: [ + { field: 'age', operator: 'greater', value: 10 }, + { + condition: 'OR', + rules: [ + { field: 'sex', operator: 'equal', value: 'female' }, + { field: 'sex', operator: 'equal', value: 'male' }, + ], + }, + ], + }); + fixture.detectChanges(); + + expect(component.previewExpression()).toBe('Age > 10 AND (Sex = Female OR Sex = Male)'); + + component.saveFilters(); + + expect(expStudio.setFilterLogic).toHaveBeenCalledWith(expect.objectContaining({ + condition: 'AND', + rules: [ + expect.objectContaining({ field: 'age', value: 10 }), + expect.objectContaining({ + condition: 'OR', + rules: expect.arrayContaining([ + expect.objectContaining({ field: 'sex', value: 'female' }), + expect.objectContaining({ field: 'sex', value: 'male' }), + ]), + }), + ], + valid: true, + })); + // arrayContaining ignores extra entries; the OR group must hold exactly these two. + expect((vi.mocked(expStudio.setFilterLogic).mock.lastCall![0] as any).rules[1].rules).toHaveLength(2); + }); + + it('renders as an inline builder with no modal chrome', () => { + fixture.detectChanges(); + const root = fixture.nativeElement.firstElementChild as HTMLElement; + expect(root.classList).toContain('inline-filter-builder'); + for (const selector of ['.modal-overlay', '.filter-modal-header', '.filter-helper', '.filter-modal-footer', '.btn-preview']) { + expect(fixture.nativeElement.querySelector(selector)).toBeNull(); + } + }); + + it('frames nested groups but not the empty root group', () => { + fixture.detectChanges(); + const root = fixture.nativeElement.querySelector('.filter-block-group.is-root') as HTMLElement; + expect(root.querySelector('.empty-filter-group')).toBeTruthy(); + expect(root.textContent).toContain('No conditions yet.'); + expect(root.textContent).toContain('Add condition'); + expect(root.textContent).toContain('Group'); + + component.addGroup(component.rootGroup().id, 0); + fixture.detectChanges(); + + const nested = fixture.nativeElement.querySelector('.filter-block-group:not(.is-root)') as HTMLElement; + expect(nested).toBeTruthy(); + expect(nested.textContent).toContain('Group · match'); + expect(nested.querySelector('.empty-filter-group')).toBeTruthy(); + }); + + it('offers only the selected variables to a transformation rule builder', async () => { + fixture.componentRef.setInput('variableScope', 'selectedVariables'); + await fixture.whenStable(); + fixture.detectChanges(); + + // `age` exists in the data model but was never selected; `notes` cannot be filtered on. + const codes = component.allFilterVariables().map((variable: any) => variable.code); + expect(codes).toEqual(['sex', 'mrs_score']); + + component.addCondition(component.rootGroup().id, 0); + fixture.detectChanges(); + const suggestions = Array.from(fixture.nativeElement.querySelectorAll('.condition-fields datalist option')) + .map((option) => (option as HTMLOptionElement).value); + expect(suggestions).toEqual(['Sex (nominal)', 'mRS score (discrete)']); + }); + + it('keeps the whole data model in the pool for the cohort filter station', async () => { + expStudio.selectedDataModel.set({ + code: 'dm', + label: 'Data model', + variables: [{ code: 'mrs_score', label: 'mRS score', type: 'discrete' }], + }); + await fixture.whenStable(); + fixture.detectChanges(); + + expect(component.allFilterVariables().map((variable: any) => variable.code)).toEqual(['mrs_score']); + }); + + it('asks for a variable selection when the scoped pool is empty', async () => { + expStudio.selectedVariables.set([]); + fixture.componentRef.setInput('variableScope', 'selectedVariables'); + await fixture.whenStable(); + fixture.detectChanges(); + + expect(fixture.nativeElement.querySelector('.where-error').textContent) + .toContain('No variables are selected for the Data Handling pipeline yet.'); + }); + + describe('membership rules (In (any of) / Not in (none of))', () => { + it('names the membership operators the way the builder shows them', () => { + expect(component.operatorDisplayLabel('IN')).toBe('is any of'); + expect(component.operatorDisplayLabel('NOT IN')).toBe('is none of'); + }); + + it('reads a stored in-rule back with its operator and every value picked', () => { + fixture.componentRef.setInput('filterLogic', { + condition: 'AND', + rules: [{ field: 'sex', operator: 'in', value: ['female', 'male'], type: 'string' }], + }); + fixture.detectChanges(); + + const condition = component.rootGroup().rules[0] as any; + expect(condition.operator).toBe('IN'); + expect(condition.values).toEqual(['female', 'male']); + expect(component.isCategorySelected(condition, 'female')).toBe(true); + expect(component.previewExpression()).toBe('Sex IN Female, Male'); + }); + + it('keeps in/not_in and their arrays intact through a save', () => { + fixture.componentRef.setInput('filterLogic', { + condition: 'AND', + rules: [ + { field: 'sex', operator: 'in', value: ['female'], type: 'string' }, + { field: 'sex', operator: 'not_in', value: ['male'], type: 'string' }, + ], + }); + fixture.detectChanges(); + + component.saveFilters(); + + expect(component.filterError()).toBeNull(); + expect(expStudio.setFilterLogic).toHaveBeenCalledWith(expect.objectContaining({ + rules: [ + expect.objectContaining({ field: 'sex', operator: 'in', value: ['female'] }), + expect.objectContaining({ field: 'sex', operator: 'not_in', value: ['male'] }), + ], + })); + }); + + it('lets the user tick any number of categories', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + let condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + condition = component.rootGroup().rules[0] as any; + component.setConditionOperator(condition.id, 'IN'); + + component.toggleCategory(condition.id, 'female'); + component.toggleCategory(condition.id, 'male'); + expect((component.rootGroup().rules[0] as any).values).toEqual(['female', 'male']); + + component.toggleCategory(condition.id, 'female'); + condition = component.rootGroup().rules[0] as any; + expect(condition.values).toEqual(['male']); + expect(component.previewExpression()).toBe('Sex IN Male'); + }); + + it('treats a comma-separated line as the same set for a variable without categories', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + let condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Age (continuous)'); + condition = component.rootGroup().rules[0] as any; + component.setConditionOperator(condition.id, 'IN'); + component.setMultiValueText(condition.id, '20, 35.5, '); + + condition = component.rootGroup().rules[0] as any; + expect(condition.values).toEqual(['20', '35.5']); + + component.saveFilters(); + + expect(expStudio.setFilterLogic).toHaveBeenCalledWith(expect.objectContaining({ + rules: [expect.objectContaining({ field: 'age', operator: 'in', value: [20, 35.5] })], + })); + }); + + it('refuses to save a membership rule with nothing picked', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + let condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + condition = component.rootGroup().rules[0] as any; + component.setConditionOperator(condition.id, 'IN'); + + component.saveFilters(); + + expect(component.filterError()).toBe('Choose at least one value for every condition.'); + expect(expStudio.setFilterLogic).not.toHaveBeenCalled(); + }); + + it('keeps one value shape across operators instead of retyping it', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + let condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + condition = component.rootGroup().rules[0] as any; + component.setConditionValue(condition.id, 'female'); + component.setConditionOperator(condition.id, 'IN'); + + condition = component.rootGroup().rules[0] as any; + expect(condition.values).toEqual(['female']); + + component.toggleCategory(condition.id, 'male'); + condition = component.rootGroup().rules[0] as any; + component.setConditionOperator(condition.id, '='); + condition = component.rootGroup().rules[0] as any; + expect(condition.values).toEqual(['female', 'male']); + + // A single-value operator reads the first entry, so nothing had to be reshaped. + component.saveFilters(); + const saved = (expStudio.setFilterLogic as Mock).mock.lastCall![0]; + expect(saved.rules[0]).toEqual(expect.objectContaining({ field: 'sex', operator: 'equal', value: 'female' })); + }); + }); + + it('reads a bare category-rule condition into one visible condition', () => { + fixture.componentRef.setInput('filterLogic', { + id: 'clinical_sdr', + field: 'clinical_sdr', + operator: 'in', + value: ['1', '2'], + type: 'string', + }); + fixture.detectChanges(); + + expect(component.activeRulesCount()).toBe(1); + const condition = component.rootGroup().rules[0] as any; + expect(condition.operator).toBe('IN'); + expect(condition.values).toEqual(['1', '2']); + }); + + it('exportFilterLogic returns payload without writing cohort filters', () => { + const root = component.rootGroup(); + component.addCondition(root.id, 0); + const condition = component.rootGroup().rules[0] as any; + component.onConditionVariableTextChange(condition.id, 'Age (continuous)'); + component.setConditionOperator(condition.id, '>'); + component.setConditionValue(condition.id, '2'); + + const logic = component.exportFilterLogic(); + + expect(logic).toEqual(expect.objectContaining({ + condition: expect.any(String), + rules: expect.any(Array), + valid: true, + })); + expect(expStudio.setFilters).not.toHaveBeenCalled(); + expect(expStudio.setFilterLogic).not.toHaveBeenCalled(); + }); + + describe('unavailable variables', () => { + function openCondition(): any { + component.ensureFirstCondition(); + fixture.detectChanges(); + return component.rootGroup().rules[0]; + } + + it('lists an unavailable variable grayed with the datasets that lack it, and does not resolve it', () => { + expStudio.unavailableVariables.set(new Map([['sex', ['Dataset B']]])); + const condition = openCondition(); + + const options = Array.from<HTMLOptionElement>(fixture.nativeElement.querySelectorAll('datalist option')); + const sexOption = options.find((option) => option.value === 'Sex (nominal)')!; + expect(sexOption.getAttribute('label')).toBe('Not available in: Dataset B'); + const ageOption = options.find((option) => option.value === 'Age (continuous)')!; + expect(ageOption.getAttribute('label')).toBeNull(); + + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + fixture.detectChanges(); + const updated = component.rootGroup().rules[0] as any; + expect(updated.field).toBe(''); + expect(updated.values).toEqual([]); + const input = fixture.nativeElement.querySelector('.field-variable input') as HTMLInputElement; + expect(input.classList).toContain('is-unavailable'); + expect(input.getAttribute('title')).toBe('Not available in: Dataset B'); + expect(fixture.nativeElement.querySelector('.filter-unavailable-note')?.textContent?.trim()) + .toBe('Not available in: Dataset B'); + + component.onConditionVariableTextChange(condition.id, 'Age (continuous)'); + expect((component.rootGroup().rules[0] as any).field).toBe('age'); + }); + + it('says which datasets lack a typed unavailable variable when saving', () => { + expStudio.unavailableVariables.set(new Map([['sex', ['Dataset B', 'Dataset C']]])); + const condition = openCondition(); + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + + component.saveFilters(); + + expect(component.filterError()).toBe('Sex is not available in: Dataset B, Dataset C.'); + expect(expStudio.setFilters).not.toHaveBeenCalled(); + expect(expStudio.setFilterLogic).not.toHaveBeenCalled(); + + component.onConditionVariableTextChange(condition.id, 'nothing like this'); + component.saveFilters(); + expect(component.filterError()).toBe('Choose a variable for every condition.'); + }); + + it('keeps a condition chosen before its variable became unavailable, grayed in the resting row', () => { + const condition = openCondition(); + component.onConditionVariableTextChange(condition.id, 'Sex (nominal)'); + expect((component.rootGroup().rules[0] as any).field).toBe('sex'); + + expStudio.unavailableVariables.set(new Map([['sex', ['Dataset B']]])); + fixture.detectChanges(); + + expect((component.rootGroup().rules[0] as any).field).toBe('sex'); + const compact = fixture.nativeElement.querySelector('.compact-variable') as HTMLElement; + expect(compact.classList).toContain('is-unavailable'); + expect(compact.getAttribute('aria-disabled')).toBe('true'); + expect(compact.getAttribute('title')).toBe('Not available in: Dataset B'); + }); + }); +}); diff --git a/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.ts b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.ts new file mode 100644 index 000000000..adba6a898 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/filter-config-modal/filter-config-modal.component.ts @@ -0,0 +1,660 @@ +import { DataModel, Group, Variable } from '../../../../models/data-model.interface'; +import { ChangeDetectionStrategy, Component, effect, signal, computed, output, inject, input } from '@angular/core'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { CommonModule } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { + filterOperatorBackendFor, + filterOperatorPhrase, + filterOperatorSymbolFor, + filterOperatorSymbolsForType, + filterOperatorValueKind, + isMultiValueFilterOperator, + isUnaryFilterOperator, +} from '../../../../core/filter-logic.utils'; +import { unavailableDatasetsFor, unavailableTooltipFor, unavailableVariableTooltip } from '../../../../core/variable-availability.utils'; +import { isCategoricalVariableType, isNumericalVariableType } from '../../../../core/data-model.utils'; + +type GroupCondition = 'AND' | 'OR'; +type FilterBlock = FilterGroupBlock | FilterConditionBlock; + +interface FilterGroupBlock { + kind: 'group'; + id: string; + connector?: GroupCondition; + rules: FilterBlock[]; +} + +interface FilterConditionBlock { + kind: 'condition'; + id: string; + connector?: GroupCondition; + field: string; + variableText: string; + /** A symbol from FILTER_OPERATORS. */ + operator: string; + /** Always a list: `in` / `not_in` pick many, everything else uses the first entry. */ + values: string[]; +} + +/** Splits the comma-separated editing form of a membership value into its codes. */ +function splitFilterValues(text: string): string[] { + return text.split(',').map((entry) => entry.trim()).filter((entry) => entry.length > 0); +} + +/** Backend value → the builder's list form. `null`/`''` mean "nothing typed yet". */ +function toFilterValues(value: unknown): string[] { + if (value === undefined || value === null || value === '') return []; + return (Array.isArray(value) ? value : [value]).map(String); +} + +@Component({ + selector: 'app-filter-config-modal', + imports: [CommonModule, FormsModule], + templateUrl: './filter-config-modal.component.html', + styleUrl: './filter-config-modal.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class FilterConfigModalComponent { + private expStudio = inject(ExperimentStudioService); + + readonly filterLogic = input<any | null>(null); + /** + * Which variables the builder may reference. The cohort-filter station needs the + * whole data model because any CDE can define a cohort. The transformation + * category-rule builders pass 'selectedVariables' so a rule can only be built from + * the variables carried into the Data Handling pipeline. + */ + readonly variableScope = input<'dataModel' | 'selectedVariables'>('dataModel'); + readonly filtersApplied = output<void>(); + + readonly allFilterVariables = signal<any[]>([]); + readonly rootGroup = signal<FilterGroupBlock>(this.createGroup()); + readonly filterError = signal<string | null>(null); + readonly previewExpression = computed(() => this.groupPreview(this.rootGroup())); + readonly activeRulesCount = computed(() => this.countRules(this.rootGroup())); + readonly emptyPoolMessage = computed(() => this.variableScope() === 'selectedVariables' + ? 'No variables are selected for the Data Handling pipeline yet. Select variables in Data Exploration first.' + : 'No filterable variables are available for this pathology.'); + + constructor() { + effect(() => { + this.allFilterVariables.set(this.variableScope() === 'selectedVariables' + ? this.normalizeFilterVariables(this.expStudio.selectedVariables()) + : this.flattenDataModelVariables(this.expStudio.selectedDataModel())); + }); + + effect(() => { + this.rootGroup.set(this.backendToGroup(this.filterLogic())); + this.filterError.set(null); + }); + } + + /** The one condition row the user is editing; `<details>` follows it and opens new rows. */ + readonly activeBlockId = signal<string | null>(null); + + /** A new category rule starts on its first condition, not on an empty "Add condition" prompt. */ + ensureFirstCondition(): void { + const root = this.rootGroup(); + if (root.rules.length === 0) this.addCondition(root.id, 0); + } + + addCondition(groupId: string, index: number): void { + const newCondition = this.createCondition(); + this.updateGroup(groupId, (group) => ({ + ...group, + rules: this.insertAt(group.rules, index, newCondition), + })); + this.activeBlockId.set(newCondition.id); + } + + addGroup(groupId: string, index: number): void { + this.updateGroup(groupId, (group) => ({ + ...group, + rules: this.insertAt(group.rules, index, this.createGroup()), + })); + } + + removeBlock(blockId: string): void { + if (this.activeBlockId() === blockId) { + this.activeBlockId.set(null); + } + this.rootGroup.update((root) => this.ensureGroupConnectors(this.removeBlockFromGroup(root, blockId))); + this.filterError.set(null); + } + + /** `<summary>` click handler: the browser toggles `open`, this keeps the signal in step. */ + toggleActiveBlock(blockId: string): void { + this.activeBlockId.update((current) => current === blockId ? null : blockId); + } + + groupCondition(group: FilterGroupBlock): GroupCondition { + const secondRule = group.rules[1]; + return secondRule?.connector ?? 'AND'; + } + + setGroupCondition(groupId: string, condition: GroupCondition): void { + this.updateGroup(groupId, (group) => ({ + ...group, + rules: group.rules.map((block, index) => index === 0 ? block : { ...block, connector: condition }), + })); + } + + variableType(block: FilterConditionBlock): string { + const filter = this.selectedFilter(block); + return filter?.type ? String(filter.type).toLowerCase() : ''; + } + + compactValueSummary(block: FilterConditionBlock): string { + if (this.isUnaryOperator(block.operator)) return ''; + const labels = block.values.map((value) => this.valueLabel(block, value)); + if (this.normalizedOperator(block.operator) === 'between' && labels.length >= 2) { + return `${labels[0]} and ${labels[1]}`; + } + if (labels.length <= 2) return labels.join(', ') || 'None'; + return `${labels.slice(0, 2).join(', ')} (+${labels.length - 2} more)`; + } + + /** Category chips for the resting row: all options up to six, then the selected + * ones plus a `+N more` chip. */ + restingCategoryOptions(block: FilterConditionBlock): Array<{ value: string; label: string }> { + const options = this.categoryOptions(block); + if (options.length <= 6) return options; + const selected = options.filter((option) => this.isCategorySelected(block, option.value)); + return selected.length ? selected : options.slice(0, 6); + } + + hiddenCategoryCount(block: FilterConditionBlock): number { + return this.categoryOptions(block).length - this.restingCategoryOptions(block).length; + } + + onConditionVariableTextChange(blockId: string, value: string): void { + // A variable missing from a selected dataset never resolves, so picking it changes no field. + const resolved = this.resolveFilterField(value); + const selected = resolved && !this.variableUnavailableTooltip(resolved) ? resolved : null; + this.updateCondition(blockId, (block) => { + const options = selected ? filterOperatorSymbolsForType(selected.type) : []; + const nextOperator = selected && !options.includes(block.operator) ? options[0] : block.operator; + return { + ...block, + variableText: value, + field: selected?.code ?? '', + operator: nextOperator ?? '=', + values: selected ? [] : block.values, + }; + }); + } + + setConditionOperator(blockId: string, operator: string): void { + // The value keeps its list form across operators: `=` reads values[0], `IN` reads them all. + this.updateCondition(blockId, (block) => ({ ...block, operator })); + } + + setConditionValue(blockId: string, value: string): void { + this.updateCondition(blockId, (block) => ({ ...block, values: [value] })); + } + + isUnaryOperator(operator: string): boolean { + return isUnaryFilterOperator(operator); + } + + /** `in` / `not_in`: the value is a set, so any number of categories can be picked. */ + isMultiValueOperator(operator: string): boolean { + return isMultiValueFilterOperator(operator); + } + + isCategorySelected(block: FilterConditionBlock, category: string): boolean { + return block.values.includes(category); + } + + toggleCategory(blockId: string, category: string): void { + this.updateCondition(blockId, (block) => ({ + ...block, + values: block.values.includes(category) + ? block.values.filter((entry) => entry !== category) + : [...block.values, category], + })); + } + + /** Comma-separated editing surface for a membership rule on a variable without enumerations. */ + setMultiValueText(blockId: string, text: string): void { + this.updateCondition(blockId, (block) => ({ ...block, values: splitFilterValues(text) })); + } + + visibleFilterVariables(query: string): any[] { + const normalized = query.trim().toLowerCase(); + return this.allFilterVariables().filter((variable) => { + if (!normalized) return true; + return String(variable.code ?? '').toLowerCase().includes(normalized) || + String(variable.label ?? variable.name ?? '').toLowerCase().includes(normalized); + }); + } + + selectedFilter(block: FilterConditionBlock): any | null { + return this.allFilterVariables().find((filter) => filter.code === block.field) ?? null; + } + + // An unresolved field is not treated as nominal, so every operator stays available. + operatorOptions(block: FilterConditionBlock): string[] { + return filterOperatorSymbolsForType(this.selectedFilter(block)?.type); + } + + private normalizedOperator(operator: string): string { + return filterOperatorSymbolFor(operator).trim().toLowerCase(); + } + + operatorDisplayLabel(operator: string): string { + return filterOperatorPhrase(operator); + } + + categoryOptions(block: FilterConditionBlock): Array<{ value: string; label: string }> { + const filter = this.selectedFilter(block); + return (filter?.enumerations ?? []).map((entry: any) => ({ + value: String(entry.code ?? entry.label ?? entry.name ?? ''), + label: String(entry.label ?? entry.name ?? entry.code ?? ''), + })).filter((entry: { value: string; label: string }) => entry.value); + } + + /** "Not available in: …" for a variable missing from a selected dataset, '' otherwise. */ + variableUnavailableTooltip(variable: any): string { + return unavailableTooltipFor(this.expStudio.unavailableVariables(), { code: variable?.code }); + } + + /** + * Why the condition's variable cannot be used: the one already chosen (kept, never removed) + * or the one typed into the field. '' when the variable is available or unresolved. + */ + conditionUnavailableTooltip(block: FilterConditionBlock): string { + const variable = this.selectedFilter(block) ?? this.resolveFilterField(block.variableText ?? ''); + return variable ? this.variableUnavailableTooltip(variable) : ''; + } + + variableDisplayLabel(variable: any): string { + const label = variable?.label ?? variable?.name ?? variable?.code ?? ''; + const type = variable?.type ? ` (${variable.type})` : ''; + return `${label}${type}`; + } + + /** + * Validate the current builder and return backend filter logic without writing + * cohort filters on ExperimentStudioService. + */ + exportFilterLogic(): any | null { + const validationError = this.validateGroup(this.rootGroup()); + if (validationError) { + this.filterError.set(validationError); + return null; + } + + const normalized = this.normalizeGroup(this.rootGroup()); + this.filterError.set(null); + return normalized.rules.length > 0 ? this.formatFiltersForBackend(normalized) : null; + } + + saveFilters(): void { + const toStore = this.exportFilterLogic(); + if (this.filterError()) { + return; + } + const selectedFilters = toStore + ? this.extractFilterCodes(toStore) + .map((code) => this.allFilterVariables().find((variable) => variable.code === code)) + .filter((variable): variable is any => !!variable) + : []; + + this.expStudio.setFilters(selectedFilters); + this.expStudio.setFilterLogic(toStore); + this.filtersApplied.emit(undefined); + this.filterError.set(null); + } + + clearFilters(): void { + this.rootGroup.set(this.createGroup()); + this.expStudio.setFilters([]); + this.expStudio.setFilterLogic(null); + this.filterError.set(null); + } + + private createGroup(rules: FilterBlock[] = []): FilterGroupBlock { + return this.ensureGroupConnectors({ kind: 'group', id: this.nextId(), rules }); + } + + private createCondition(): FilterConditionBlock { + return { kind: 'condition', id: this.nextId(), field: '', variableText: '', operator: '=', values: [] }; + } + + private nextId(): string { + return `filter-${Date.now()}-${Math.random().toString(36).slice(2)}`; + } + + private insertAt<T>(items: T[], index: number, item: T): T[] { + return [...items.slice(0, index), item, ...items.slice(index)]; + } + + private updateGroup(groupId: string, updater: (group: FilterGroupBlock) => FilterGroupBlock): void { + this.rootGroup.update((root) => this.ensureGroupConnectors(this.mapGroups(root, groupId, updater))); + this.filterError.set(null); + } + + private updateCondition(blockId: string, updater: (block: FilterConditionBlock) => FilterConditionBlock): void { + this.rootGroup.update((root) => this.ensureGroupConnectors(this.mapConditions(root, blockId, updater))); + this.filterError.set(null); + } + + private mapGroups(group: FilterGroupBlock, groupId: string, updater: (group: FilterGroupBlock) => FilterGroupBlock): FilterGroupBlock { + const mapped = { + ...group, + rules: group.rules.map((block) => block.kind === 'group' ? this.mapGroups(block, groupId, updater) : block), + }; + return mapped.id === groupId ? updater(mapped) : mapped; + } + + private mapConditions(group: FilterGroupBlock, blockId: string, updater: (block: FilterConditionBlock) => FilterConditionBlock): FilterGroupBlock { + return { + ...group, + rules: group.rules.map((block) => { + if (block.kind === 'condition') return block.id === blockId ? updater(block) : block; + return this.mapConditions(block, blockId, updater); + }), + }; + } + + private removeBlockFromGroup(group: FilterGroupBlock, blockId: string): FilterGroupBlock { + return { + ...group, + rules: group.rules + .filter((block) => block.id !== blockId) + .map((block) => block.kind === 'group' ? this.removeBlockFromGroup(block, blockId) : block), + }; + } + + private ensureGroupConnectors(group: FilterGroupBlock): FilterGroupBlock { + return { + ...group, + connector: undefined, + rules: group.rules.map((block, index) => { + const normalizedConnector = index === 0 ? undefined : (block.connector ?? 'AND'); + if (block.kind === 'group') { + return { + ...this.ensureGroupConnectors(block), + connector: normalizedConnector, + }; + } + return { + ...block, + connector: normalizedConnector, + }; + }), + }; + } + + private resolveFilterField(raw: string): any | null { + const normalized = raw.toLowerCase(); + return this.allFilterVariables().find((filter) => + String(filter.code ?? '').toLowerCase() === normalized || + String(filter.label ?? '').toLowerCase() === normalized || + String(filter.name ?? '').toLowerCase() === normalized || + this.variableDisplayLabel(filter).toLowerCase() === normalized + ) ?? null; + } + + /** + * Accepts every shape the backend can hand back: a group of rules, and the bare condition + * a `categorical_column_creator` category rule stores — one rule becomes a one-condition + * group, so a persisted derived column reads back into the builder. + */ + private backendToGroup(logic: any): FilterGroupBlock { + if (!logic) return this.createGroup(); + if (!Array.isArray(logic.rules)) { + if (!this.looksLikeCondition(logic)) return this.createGroup(); + return this.ensureGroupConnectors({ + kind: 'group', + id: this.nextId(), + rules: [this.backendToBlock(logic)], + }); + } + const condition = this.backendCondition(logic); + // Associative flattening: a child group with the same condition as its parent (or holding + // a single rule) says nothing the user did not write, so it is folded away. + const flatten = (rules: any[]): any[] => rules.flatMap((rule: any) => this.looksLikeGroup(rule) + && (this.backendCondition(rule) === condition || rule.rules.length <= 1) + ? flatten(rule.rules) + : [rule]); + + const rules = flatten(logic.rules) + .filter((rule: any) => this.looksLikeCondition(rule) || this.looksLikeGroup(rule)) + .map((rule: any, index: number) => ({ + ...this.backendToBlock(rule), + connector: index === 0 ? undefined : condition, + })); + return this.ensureGroupConnectors({ kind: 'group', id: this.nextId(), rules }); + } + + private backendCondition(node: any): GroupCondition { + return String(node?.condition ?? 'AND').toUpperCase() === 'OR' ? 'OR' : 'AND'; + } + + private looksLikeGroup(node: any): boolean { + return !!node && typeof node === 'object' && !!node.condition && Array.isArray(node.rules); + } + + private backendToBlock(rule: any): FilterBlock { + if (this.looksLikeGroup(rule)) return this.backendToGroup(rule); + const field = String(rule?.field ?? rule?.id ?? ''); + const variable = this.allFilterVariables().find((entry) => entry.code === field); + const backendOperator = String(rule?.operator ?? '='); + return { + kind: 'condition', + id: this.nextId(), + field, + variableText: variable ? this.variableDisplayLabel(variable) : field, + operator: filterOperatorSymbolFor(backendOperator), + values: toFilterValues(rule?.value), + }; + } + + private looksLikeCondition(node: any): boolean { + return !!node && typeof node === 'object' && !Array.isArray(node) + && (node.operator !== undefined || node.field !== undefined || node.id !== undefined); + } + + + + private validateGroup(group: FilterGroupBlock): string | null { + for (const block of group.rules) { + if (block.kind === 'group') { + const groupError = this.validateGroup(block); + if (groupError) return groupError; + continue; + } + if (!block.field) return this.unavailableTypedVariableError(block) || 'Choose a variable for every condition.'; + if (!block.operator) return 'Choose an operator for every condition.'; + if (this.isUnaryOperator(block.operator)) continue; + if (this.isMultiValueOperator(block.operator)) { + if (block.values.length === 0) return 'Choose at least one value for every condition.'; + continue; + } + if (!block.values[0]) return 'Choose or enter a value for every condition.'; + } + return null; + } + + /** "Age is not available in: Dataset B." when the typed variable is missing from a selected dataset. */ + private unavailableTypedVariableError(block: FilterConditionBlock): string { + const typed = this.resolveFilterField(block.variableText ?? ''); + const datasets = unavailableDatasetsFor(this.expStudio.unavailableVariables(), { code: typed?.code }); + const label = typed?.label ?? typed?.name ?? typed?.code; + return datasets.length ? `${unavailableVariableTooltip(datasets, `${label} is not available in`)}.` : ''; + } + + private normalizeGroup(group: FilterGroupBlock): any { + return this.groupToBackendLogic(group); + } + + private normalizeCondition(block: FilterConditionBlock): any { + const variable = this.selectedFilter(block); + const kind = filterOperatorValueKind(block.operator); + return { + field: block.field, + // A unary rule sent without its value key is what the engine rejects, so `value` is + // always written: null for `is null`, the picked set for `in`, one entry for the rest. + operator: filterOperatorBackendFor(block.operator), + value: kind === 'none' + ? null + : kind === 'values' + ? block.values.map((entry) => this.coerceValue(variable, entry)) + : this.coerceValue(variable, block.values[0] ?? ''), + }; + } + + private coerceValue(variable: any | null, value: string): unknown { + const numeric = Number(value); + return isNumericalVariableType(variable?.type) && value.trim() !== '' && Number.isFinite(numeric) + ? numeric + : value; + } + + private formatFiltersForBackend(rawLogic: { condition: string; rules: any[] }): any { + return { + condition: String(rawLogic.condition || 'AND').toUpperCase(), + rules: rawLogic.rules.map((rule) => this.formatRuleForBackend(rule)), + valid: true, + }; + } + + private formatRuleForBackend(rule: any): any { + if (rule?.condition && Array.isArray(rule.rules)) { + return { + condition: String(rule.condition || 'AND').toUpperCase(), + rules: rule.rules.map((child: any) => this.formatRuleForBackend(child)), + }; + } + + const fieldKey = rule.field ?? rule.id; + return { + id: fieldKey, + field: fieldKey, + type: this.detectType(fieldKey), + input: this.detectInput(fieldKey), + operator: rule.operator, + value: rule.value, + entity: undefined, + }; + } + + private detectType(field: string): 'string' | 'integer' | 'real' { + const variable = this.allFilterVariables().find((entry) => entry.code === field); + if (variable?.type === 'discrete') return 'integer'; + if (variable?.type === 'continuous') return 'real'; + return 'string'; + } + + private detectInput(field: string): 'text' | 'number' | 'select' { + const variable = this.allFilterVariables().find((entry) => entry.code === field); + if (isNumericalVariableType(variable?.type)) return 'number'; + if (variable?.enumerations?.length) return 'select'; + return 'text'; + } + + private extractFilterCodes(logic: any): string[] { + const codes = new Set<string>(); + const walk = (node: any): void => { + if (!node) return; + if (Array.isArray(node.rules)) { + node.rules.forEach(walk); + return; + } + const code = node.field ?? node.id; + if (code) codes.add(String(code)); + }; + walk(logic); + return Array.from(codes); + } + + private countRules(group: FilterGroupBlock): number { + return group.rules.reduce((count, block) => count + (block.kind === 'group' ? this.countRules(block) : 1), 0); + } + + private groupPreview(group: FilterGroupBlock): string { + if (!group.rules.length) return ''; + return group.rules.reduce((expression, block, index) => { + const blockPreview = block.kind === 'group' ? `(${this.groupPreview(block)})` : this.conditionPreview(block); + if (index === 0) return blockPreview; + return `${expression} ${block.connector ?? 'AND'} ${blockPreview}`; + }, ''); + } + + private conditionPreview(block: FilterConditionBlock): string { + const variable = this.selectedFilter(block); + const label = variable?.label ?? variable?.name ?? block.field ?? 'Variable'; + if (this.isUnaryOperator(block.operator)) return `${label} ${block.operator}`; + const values = block.values.map((value) => this.valueLabel(block, value)).join(', '); + return `${label} ${block.operator} ${values || 'no value chosen'}`; + } + + /** A stored code as the user reads it; non-enumerated variables have no label to find. */ + private valueLabel(block: FilterConditionBlock, value: string): string { + return this.categoryOptions(block).find((option) => option.value === value)?.label ?? value; + } + + private flattenDataModelVariables(model: DataModel | null): any[] { + if (!model) return []; + const collected: Variable[] = []; + const visitGroups = (groups: Group[] = []): void => { + groups.forEach((group) => { + collected.push(...(group.variables ?? [])); + visitGroups(group.groups ?? []); + }); + }; + + collected.push(...(model.variables ?? [])); + visitGroups(model.groups ?? []); + return this.normalizeFilterVariables(collected); + } + + /** + * Pool guard shared by both sources: keeps the fields the builder renders and drops + * types it cannot express. Selected CDE nodes carry experiment metadata (supported + * algorithms, role flags) that must not leak into a filter condition. + */ + private normalizeFilterVariables(variables: Variable[]): any[] { + const seen = new Map<string, any>(); + variables.forEach((variable) => { + if (!variable?.code || seen.has(variable.code)) return; + // Categorical CDEs without enumerations are free text and cannot be filtered. + const categoricalWithValues = isCategoricalVariableType(variable.type) && !!variable.enumerations?.length; + if (!isNumericalVariableType(variable.type) && !categoricalWithValues) return; + seen.set(variable.code, { + code: variable.code, + label: variable.label, + name: (variable as any).name, + type: variable.type, + enumerations: variable.enumerations, + }); + }); + return Array.from(seen.values()); + } + + private groupToBackendLogic(group: FilterGroupBlock): any { + const backendRules = group.rules.map((block) => block.kind === 'group' ? this.groupToBackendLogic(block) : this.normalizeCondition(block)); + if (backendRules.length === 0) return { condition: 'AND', rules: [] }; + if (backendRules.length === 1) return { condition: 'AND', rules: backendRules }; + + let folded = backendRules[0]; + for (let index = 1; index < backendRules.length; index += 1) { + const operator = group.rules[index].connector ?? 'AND'; + const next = backendRules[index]; + if (this.isBackendGroup(folded) && folded.condition === operator) { + folded = { ...folded, rules: [...folded.rules, next] }; + } else { + folded = { condition: operator, rules: [folded, next] }; + } + } + + return this.isBackendGroup(folded) ? folded : { condition: 'AND', rules: [folded] }; + } + + private isBackendGroup(value: any): value is { condition: GroupCondition; rules: any[] } { + return !!value && typeof value === 'object' && Array.isArray(value.rules) && (value.condition === 'AND' || value.condition === 'OR'); + } +} diff --git a/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.css b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.css new file mode 100644 index 000000000..6e2c0042b --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.css @@ -0,0 +1,196 @@ +:host { + display: block; +} + +.station-action-bar { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + padding: 12px 18px; + border: 0; + border-top: 1px solid var(--studio-card-header-border); + border-radius: 0; + background: var(--studio-card-header-bg); +} + +/* Standalone (non-footer) bars keep their own frame. */ +.station-action-bar:not(.is-flush) { + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-lg); +} + +.station-action-bar.tone-pending, +.station-action-bar.tone-applied { + background: var(--studio-card-header-bg); + border-color: var(--studio-card-header-border); +} + +.station-action-status { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; + font-size: 0.8rem; + color: var(--text-muted); +} + +.station-action-count { + display: inline-flex; + align-items: center; + justify-content: center; + min-width: 1.5rem; + padding: 2px 8px; + border-radius: 999px; + background: color-mix(in srgb, var(--accent-color) 16%, white); + color: #78350f; + font-size: 0.74rem; + font-weight: 700; + line-height: 1.4; + font-variant-numeric: tabular-nums; +} + +.station-action-text { + letter-spacing: 0.01em; +} + +.tone-applied .station-action-text { + color: #065f46; + font-weight: 600; +} + +.station-action-buttons { + display: flex; + align-items: center; + justify-content: flex-end; + flex-wrap: wrap; + gap: 8px; + margin-left: auto; +} + +/* Secondary action: the solid Apply button is the only primary-coloured + control in this bar. Blue-on-white reset read as a second primary. */ +.station-action-reset { + padding: 0 12px; + border: 0; + border-radius: 999px; + background: transparent; + color: var(--text-muted); + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + white-space: nowrap; + cursor: pointer; + transition: background 0.2s ease, color 0.2s ease, opacity 0.2s ease; +} + +.station-action-reset:hover:not(:disabled) { + color: var(--text-main); + background: color-mix(in srgb, var(--primary-color) 6%, white); +} + +/* Middle action: outlined, so it reads as a step (Clear · Preview · Apply) not a link pair. + Disabled wears the same treatment as its neighbours; the station's fix list says why. */ +.station-action-preview { + display: inline-flex; + align-items: center; + gap: 8px; + padding: 0 16px; + border: 1px solid rgba(127, 156, 232, 0.38); + border-radius: 999px; + background: rgba(255, 255, 255, 0.96); + color: #334155; + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + white-space: nowrap; + cursor: pointer; + transition: background 0.2s ease, border-color 0.2s ease, opacity 0.2s ease; +} + +.station-action-preview i { + color: var(--primary-color); + font-size: 0.76rem; +} + +.station-action-preview:hover:not(:disabled) { + border-color: rgba(43, 51, 233, 0.45); + background: rgba(43, 51, 233, 0.06); + color: var(--primary-color); +} + +.station-action-apply { + display: inline-flex; + align-items: center; + gap: 6px; + padding: 0 18px; + border: 0; + border-radius: 999px; + background: var(--primary-color); + color: #ffffff; + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + white-space: nowrap; + cursor: pointer; + box-shadow: var(--shadow-sm); + transition: background 0.2s ease, box-shadow 0.2s ease, opacity 0.2s ease; +} + +.station-action-apply:hover:not(:disabled) { + background: var(--primary-dark); + box-shadow: var(--shadow-md); +} + +/* Close, not Apply: same slot and same geometry, ring drawn as an inset shadow so adding + a border never nudges the button against its Clear / Preview neighbours. */ +.station-action-apply.is-quiet { + background: transparent; + color: var(--text-main); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 24%, transparent); +} + +.station-action-apply.is-quiet:hover:not(:disabled) { + background: color-mix(in srgb, var(--primary-color) 6%, white); + box-shadow: inset 0 0 0 1px var(--primary-color); +} + +.station-action-reset, +.station-action-preview, +.station-action-apply { + height: 34px; + border-radius: 999px; + font-size: 0.8rem; + font-weight: 600; +} + +.station-action-reset:disabled, +.station-action-preview:disabled, +.station-action-apply:disabled { + opacity: 0.45; + cursor: not-allowed; + box-shadow: none; +} + +.station-action-reset:focus-visible, +.station-action-preview:focus-visible, +.station-action-apply:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +.station-action-apply i { + font-size: 0.85em; +} + +@media (max-width: 768px) { + .station-action-bar { + align-items: flex-start; + flex-direction: column; + padding: 12px 12px; + } + + .station-action-buttons { + width: 100%; + } +} diff --git a/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.html b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.html new file mode 100644 index 000000000..633f30815 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.html @@ -0,0 +1,41 @@ +<div class="station-action-bar" [class.is-flush]="flush()" [class.tone-pending]="tone() === 'pending'" [class.tone-applied]="tone() === 'applied'"> + @if (pendingCount() > 0 || statusText()) { + <div class="station-action-status"> + @if (pendingCount() > 0) { + <span class="station-action-count">{{ pendingCount() }}</span> + } + @if (statusText()) { + <span class="station-action-text"> + {{ statusText() }} + </span> + } + </div> + } + + <div class="station-action-buttons"> + @if (resetLabel()) { + <button type="button" class="station-action-reset" [disabled]="resetDisabled()" (click)="reset.emit()">{{ resetLabel() }}</button> + } + @if (previewLabel()) { + <button + type="button" + class="station-action-preview" + (click)="preview.emit()" + > + <i class="fas fa-eye" aria-hidden="true"></i>{{ previewLabel() }} + </button> + } + <button + type="button" + class="station-action-apply" + [class.is-quiet]="applyVariant() === 'quiet'" + [disabled]="applyDisabled()" + (click)="apply.emit()" + > + @if (applyIcon()) { + <i [attr.class]="'icon-left ' + applyIcon()" aria-hidden="true"></i> + } + {{ applyLabel() }} + </button> + </div> +</div> diff --git a/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.spec.ts b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.spec.ts new file mode 100644 index 000000000..c169f7ba0 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.spec.ts @@ -0,0 +1,102 @@ +import { Component, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { StationActionBarComponent } from './station-action-bar.component'; + +@Component({ + standalone: true, + imports: [StationActionBarComponent], + template: ` + <app-station-action-bar + [pendingCount]="count()" + [statusText]="text()" + [tone]="tone()" + [resetLabel]="resetLabel()" + [previewLabel]="previewLabel()" + applyLabel="Apply Preprocessing" + applyIcon="fa fa-magic" + [applyVariant]="variant()" + [applyDisabled]="count() === 0" + (reset)="resetClicks = resetClicks + 1" + (apply)="applyClicks = applyClicks + 1"></app-station-action-bar> + `, +}) +class HostComponent { + readonly count = signal(2); + readonly text = signal('pending steps'); + readonly tone = signal<'default' | 'pending' | 'applied'>('pending'); + readonly resetLabel = signal('Reset Changes'); + readonly previewLabel = signal('Preview data'); + readonly variant = signal<'primary' | 'quiet'>('primary'); + resetClicks = 0; + applyClicks = 0; +} + +describe('StationActionBarComponent', () => { + let fixture: ComponentFixture<HostComponent>; + + /** Zoneless: signal writes are what mark the fixture dirty, so state is driven through signals. */ + function render( + state: { + count?: number; + text?: string; + tone?: 'default' | 'pending' | 'applied'; + resetLabel?: string; + variant?: 'primary' | 'quiet'; + } = {} + ): HTMLElement { + const host = fixture.componentInstance; + if (state.count !== undefined) host.count.set(state.count); + if (state.text !== undefined) host.text.set(state.text); + if (state.tone !== undefined) host.tone.set(state.tone); + if (state.resetLabel !== undefined) host.resetLabel.set(state.resetLabel); + if (state.variant !== undefined) host.variant.set(state.variant); + fixture.detectChanges(); + return fixture.nativeElement; + } + + beforeEach(async () => { + await TestBed.configureTestingModule({ imports: [HostComponent] }).compileComponents(); + fixture = TestBed.createComponent(HostComponent); + }); + + it('shows the count chip, status text and both actions', () => { + const html = render(); + + expect(html.querySelector('.station-action-status')?.textContent).toContain('pending steps'); + expect(html.querySelector('.station-action-count')?.textContent?.trim()).toBe('2'); + expect(html.textContent).toContain('2 pending steps'); + expect(html.querySelector('.station-action-reset')?.textContent?.trim()).toBe('Reset Changes'); + expect(html.querySelector('.station-action-preview i')?.className).toContain('fa-eye'); + expect(html.querySelector('.station-action-apply')?.textContent?.trim()).toContain('Apply Preprocessing'); + expect(html.querySelector('.station-action-apply i')?.className).toContain('fa-magic'); + expect(html.querySelector('.station-action-bar')?.classList.contains('tone-pending')).toBe(true); + }); + + it('emits reset and apply', () => { + const html = render(); + + html.querySelector<HTMLButtonElement>('.station-action-reset')!.click(); + html.querySelector<HTMLButtonElement>('.station-action-apply')!.click(); + + expect(fixture.componentInstance.resetClicks).toBe(1); + expect(fixture.componentInstance.applyClicks).toBe(1); + }); + + it('hides the chip, text and reset action when there is nothing to report', () => { + const html = render({ count: 0, text: '', resetLabel: '', tone: 'applied' }); + + expect(html.querySelector('.station-action-status')).toBeFalsy(); + expect(html.querySelector('.station-action-reset')).toBeFalsy(); + expect(html.querySelector<HTMLButtonElement>('.station-action-apply')!.disabled).toBe(true); + expect(html.querySelector('.station-action-bar')?.classList.contains('tone-applied')).toBe(true); + }); + + it('draws the primary slot as a solid button by default and as an outline when quiet', () => { + const primary = render(); + expect(primary.querySelector('.station-action-apply')?.classList.contains('is-quiet')).toBe(false); + + const quiet = render({ variant: 'quiet' }); + expect(quiet.querySelector('.station-action-apply')?.classList.contains('is-quiet')).toBe(true); + }); +}); diff --git a/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.ts b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.ts new file mode 100644 index 000000000..2ab15d55b --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-action-bar/station-action-bar.component.ts @@ -0,0 +1,36 @@ +import { ChangeDetectionStrategy, Component, booleanAttribute, input, output } from '@angular/core'; + +/** + * Footer action bar shared by every Data handling station: count chip + status text on the + * left, Clear + Preview data + primary Apply & Continue on the right. + */ +@Component({ + selector: 'app-station-action-bar', + templateUrl: './station-action-bar.component.html', + styleUrl: './station-action-bar.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class StationActionBarComponent { + readonly pendingCount = input(0); + readonly statusText = input(''); + readonly tone = input<'default' | 'pending' | 'applied'>('default'); + /** Empty label hides the secondary action. */ + readonly resetLabel = input(''); + /** Empty label hides the middle preview action (shows the applied-state data). */ + readonly previewLabel = input(''); + readonly applyLabel = input('Apply'); + readonly applyIcon = input(''); + /** + * 'quiet' draws the primary slot as an outline instead of a solid fill. Used when the + * slot has nothing to commit and says Close: the solid button stays reserved for a + * change that will actually be written. + */ + readonly applyVariant = input<'primary' | 'quiet'>('primary'); + readonly resetDisabled = input(false); + readonly applyDisabled = input(false); + /** Footer variant: drops the own border/radius so it merges with the station card's bottom border. */ + readonly flush = input(false, { transform: booleanAttribute }); + readonly reset = output<void>(); + readonly preview = output<void>(); + readonly apply = output<void>(); +} diff --git a/src/app/pages/experiment-studio/shared/station-card/station-card.component.css b/src/app/pages/experiment-studio/shared/station-card/station-card.component.css new file mode 100644 index 000000000..89e049d39 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-card/station-card.component.css @@ -0,0 +1,162 @@ +:host { + display: block; +} + +.station-card { + background: var(--studio-card-bg); + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: var(--radius-lg, 12px); + box-shadow: var(--studio-card-shadow, 0 1px 2px rgba(43, 51, 233, 0.06)); + overflow: hidden; +} + +/* One header row per card: the toggle (or the static heading) plus the projected + [stationAction] slot. The action is a sibling of the toggle on purpose — a button + inside the toggle is invalid HTML and swallows the click into the collapse. */ +.station-card-header-row { + display: flex; + align-items: center; + gap: 10px; + box-sizing: border-box; + padding: 0; + background: var(--studio-card-header-bg); + border-bottom: 1px solid var(--studio-card-header-border, rgba(43, 51, 233, 0.08)); +} + +.station-card:not(.is-open) .station-card-header-row { + border-bottom-color: transparent; +} + +.station-card-header { + display: flex; + align-items: center; + gap: 12px; + flex: 1 1 auto; + min-width: 0; + box-sizing: border-box; + width: 100%; + padding: 12px 16px; + margin: 0; + text-align: left; + font-family: inherit; + font-size: inherit; + color: inherit; + background: transparent; + border: 0; +} + +.station-card-header-row.is-static .station-card-header { + width: auto; + padding: 12px 0 12px 16px; +} + +.station-card-header-row > [stationAction] { + display: inline-flex; + align-items: center; + flex: 0 0 auto; + margin-right: 14px; +} + +button.station-card-header { + cursor: pointer; + transition: background 0.2s ease; +} + +button.station-card-header:hover { + background: color-mix(in srgb, var(--primary-color) 4%, white); +} + +button.station-card-header:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: -2px; +} + +.station-card-icon { + display: inline-flex; + align-items: center; + justify-content: center; + flex: 0 0 auto; + width: 30px; + height: 30px; + border-radius: var(--radius-md, 8px); + background: var(--card-bg); + color: var(--primary-color); + font-size: 0.8rem; + line-height: 1; + box-shadow: inset 0 0 0 1px var(--studio-card-border); +} + +.station-card-heading { + display: grid; + gap: 1px; + flex: 1 1 auto; + min-width: 0; +} + +.station-card-title { + color: var(--text-main); + font-size: 0.9rem; + font-weight: 700; + letter-spacing: 0.01em; + line-height: 1.3; +} + +.station-card-description { + color: var(--text-muted); + font-size: 0.78rem; + font-weight: 400; + line-height: 1.4; +} + +.station-card-chevron { + flex: 0 0 auto; + color: var(--text-muted); + font-size: 0.72rem; + transition: transform 0.2s ease; +} + +.is-open > .station-card-header .station-card-chevron { + transform: rotate(180deg); +} + +.station-card-body { + display: none; + padding: 14px; +} + +.is-open > .station-card-body { + display: block; +} + +/* Footer is a full-bleed band: the projected content (a flush station-action bar) + owns its own padding, so it merges with the card's bottom border instead of + floating as a second bordered card. */ +.station-card-footer { + display: none; + padding: 0; +} + +.station-card-footer:has(> *) { + display: block; +} + +/* A folded card shows only its header; its own actions come back with the body. */ +.station-card:not(.is-open) > .station-card-footer { + display: none; +} + +@media (max-width: 768px) { + .station-card-header, + .station-card-body { + padding-left: 12px; + padding-right: 12px; + } + + .station-card-header-row.is-static .station-card-header { + padding-left: 12px; + } + + .station-card-header-row > [stationAction] { + margin-right: 12px; + } +} diff --git a/src/app/pages/experiment-studio/shared/station-card/station-card.component.html b/src/app/pages/experiment-studio/shared/station-card/station-card.component.html new file mode 100644 index 000000000..24f7493d0 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-card/station-card.component.html @@ -0,0 +1,48 @@ +<section class="station-card" [class.is-open]="open()"> + <ng-template #heading> + @if (icon()) { + <span class="station-card-icon" aria-hidden="true"><i [class]="icon()"></i></span> + } + <span class="station-card-heading"> + <span class="station-card-title">{{ title() }}</span> + @if (description()) { + <span class="station-card-description">{{ description() }}</span> + } + </span> + @if (statusLabel()) { + <span + class="preprocessing-state-chip" + [class.state-pending]="status() === 'pending'" + [class.state-applied]="status() === 'applied'" + [class.state-default]="status() === 'default'" + >{{ statusLabel() }}</span + > + } + @if (collapsible()) { + <i class="fas fa-chevron-down station-card-chevron" aria-hidden="true"></i> + } + </ng-template> + + @if (collapsible() || icon() || title() || description() || statusLabel()) { + <div class="station-card-header-row" [class.is-static]="!collapsible()"> + @if (collapsible()) { + <button type="button" class="station-card-header" [attr.aria-expanded]="open()" (click)="openChange.emit(!open())"> + <ng-container *ngTemplateOutlet="heading"></ng-container> + </button> + } @else { + <div class="station-card-header"> + <ng-container *ngTemplateOutlet="heading"></ng-container> + </div> + } + <ng-content select="[stationAction]"></ng-content> + </div> + } + + <div class="station-card-body"> + <ng-content></ng-content> + </div> + + <div class="station-card-footer"> + <ng-content select="[stationFooter]"></ng-content> + </div> +</section> diff --git a/src/app/pages/experiment-studio/shared/station-card/station-card.component.spec.ts b/src/app/pages/experiment-studio/shared/station-card/station-card.component.spec.ts new file mode 100644 index 000000000..3ba0894d2 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-card/station-card.component.spec.ts @@ -0,0 +1,110 @@ +import { Component, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { StationCardComponent } from './station-card.component'; + +@Component({ + standalone: true, + imports: [StationCardComponent], + template: ` + <app-station-card + icon="fas fa-eraser" + title="Missing Values" + description="Default: NA removal." + [statusLabel]="label()" + [status]="status()" + [collapsible]="true" + [open]="open()" + (openChange)="open.set($event)"> + <p class="body">station body</p> + <button stationAction type="button" class="host-action">Remove Step</button> + <button stationFooter type="button">Apply</button> + </app-station-card> + `, +}) +class HostComponent { + readonly open = signal(false); + readonly label = signal('1 change · not applied'); + readonly status = signal<'default' | 'pending' | 'applied'>('pending'); +} + +@Component({ + standalone: true, + imports: [StationCardComponent], + template: `<app-station-card><p class="body">only body</p></app-station-card>`, +}) +class HeaderlessHost {} + +describe('StationCardComponent', () => { + let fixture: ComponentFixture<HostComponent>; + + function html(): HTMLElement { + return fixture.nativeElement; + } + + /** Zoneless: signal writes are what mark the fixture dirty, so state is driven through signals. */ + function render(state: { open?: boolean; label?: string; status?: 'default' | 'pending' | 'applied' } = {}): HTMLElement { + const host = fixture.componentInstance; + if (state.open !== undefined) host.open.set(state.open); + if (state.label !== undefined) host.label.set(state.label); + if (state.status !== undefined) host.status.set(state.status); + fixture.detectChanges(); + return html(); + } + + beforeEach(async () => { + await TestBed.configureTestingModule({ imports: [HostComponent, HeaderlessHost] }).compileComponents(); + fixture = TestBed.createComponent(HostComponent); + }); + + it('renders the icon tile, title, description, body and footer action', () => { + render(); + + expect(html().querySelector('.station-card-icon i')?.className).toContain('fa-eraser'); + expect(html().querySelector('.station-card-title')?.textContent?.trim()).toBe('Missing Values'); + expect(html().querySelector('.station-card-description')?.textContent?.trim()).toBe('Default: NA removal.'); + expect(html().querySelector('.station-card-body .body')).toBeTruthy(); + expect(html().querySelector('.station-card-footer button')?.textContent?.trim()).toBe('Apply'); + }); + + it('projects the station action into the header row and never inside the collapse toggle', () => { + render(); + + expect(html().querySelector('.station-card-header-row > [stationAction]')?.textContent?.trim()).toBe('Remove Step'); + // A button nested in the toggle is invalid HTML and its click would also collapse the card. + expect(html().querySelector('.station-card-header [stationAction]')).toBeNull(); + }); + + it('marks the chip with the matching state class', () => { + expect(render().querySelector('.preprocessing-state-chip.state-pending')?.textContent?.trim()).toBe('1 change · not applied'); + expect(render({ label: 'Applied', status: 'applied' }).querySelector('.preprocessing-state-chip.state-applied')?.textContent?.trim()).toBe('Applied'); + }); + + it('drops the chip without a status label and keeps the chevron when collapsible', () => { + const rendered = render({ label: '' }); + + expect(rendered.querySelector('.preprocessing-state-chip')).toBeFalsy(); + expect(rendered.querySelector('.station-card-chevron')).toBeTruthy(); + }); + + it('toggles the station through the header and reports aria-expanded', () => { + render(); + const header = html().querySelector<HTMLButtonElement>('.station-card-header'); + expect(header?.getAttribute('aria-expanded')).toBe('false'); + expect(html().querySelector<HTMLDivElement>('.station-card')!.classList.contains('is-open')).toBe(false); + + header!.click(); + fixture.detectChanges(); + + expect(fixture.componentInstance.open()).toBe(true); + expect(html().querySelector<HTMLDivElement>('.station-card')!.classList.contains('is-open')).toBe(true); + expect(html().querySelector('.station-card-header')?.getAttribute('aria-expanded')).toBe('true'); + }); + + it('omits the header when the station has no title, icon, description or status', () => { + const headerless = TestBed.createComponent(HeaderlessHost); + headerless.detectChanges(); + expect(headerless.nativeElement.querySelector('.station-card-header')).toBeNull(); + expect(headerless.nativeElement.querySelector('.station-card-body .body')).toBeTruthy(); + }); +}); diff --git a/src/app/pages/experiment-studio/shared/station-card/station-card.component.ts b/src/app/pages/experiment-studio/shared/station-card/station-card.component.ts new file mode 100644 index 000000000..dfb249379 --- /dev/null +++ b/src/app/pages/experiment-studio/shared/station-card/station-card.component.ts @@ -0,0 +1,27 @@ +import { ChangeDetectionStrategy, Component, booleanAttribute, input, output } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +/** Chip states shared with the per-variable preprocessing chips (see src/styles.css). */ +export type StationStatus = 'default' | 'pending' | 'applied'; + +/** + * Station card used by every Data review sub-step: icon tile + title + description + * header, status chip, collapsible body and an optional footer action slot. + */ +@Component({ + selector: 'app-station-card', + imports: [CommonModule], + templateUrl: './station-card.component.html', + styleUrl: './station-card.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class StationCardComponent { + readonly icon = input(''); + readonly title = input(''); + readonly description = input(''); + readonly statusLabel = input(''); + readonly status = input<StationStatus>('default'); + readonly open = input(true); + readonly collapsible = input(false, { transform: booleanAttribute }); + readonly openChange = output<boolean>(); +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.css b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.css new file mode 100644 index 000000000..c0fd4acdb --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.css @@ -0,0 +1,367 @@ +:host { + display: block; +} + +.kmeans-cluster-source { + display: grid; + gap: 10px; +} + +.kmeans-mode-switch { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 2px; + padding: 3px; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: var(--radius-md, 8px); + background: var(--bg-neutral, #f8fafc); +} + +.kmeans-mode-btn { + min-width: 0; + padding: 7px 10px; + border: 0; + border-radius: calc(var(--radius-md, 8px) - 2px); + background: transparent; + color: var(--text-muted); + font-family: inherit; + font-size: 0.76rem; + font-weight: 600; + cursor: pointer; +} + +.kmeans-mode-btn:hover { + color: var(--text-main); +} + +.kmeans-mode-btn.active { + background: var(--card-bg, #ffffff); + color: var(--primary-dark, #1b21a3); + box-shadow: var(--studio-card-shadow, 0 1px 2px rgba(43, 51, 233, 0.06)); +} + + +.kmeans-cluster-source-field { + display: grid; + gap: 4px; + font-size: 0.78rem; + color: var(--text-main); +} + +/* Look comes from the global `.experiment-studio-page` control styles. There is no + global box-sizing reset, so keep the control inside its grid column. */ +.kmeans-cluster-source-field select, +.kmeans-cluster-source-field input { + box-sizing: border-box; +} + +.kmeans-cluster-source-applied { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 8px; + padding: 10px 12px; + border: 1px solid color-mix(in srgb, var(--primary-color) 22%, transparent); + border-radius: var(--radius-md, 8px); + background: color-mix(in srgb, var(--primary-color) 4%, white); + font-size: 0.8rem; +} + +.kmeans-cluster-source-applied-code { + font-weight: 700; + color: var(--text-main); +} + +.kmeans-cluster-source-applied-detail { + color: var(--text-muted); +} + +/* Parameters disclosure: a quiet full-width row between the variables and Run. Closed, + it says "All defaults" or lists what changed; open, the fields grid hangs off it. */ +.kmeans-cluster-source-advanced { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; + padding: 7px 12px; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: var(--radius-md, 8px); + background: var(--bg-neutral, #f8fafc); + color: var(--text-muted); + font-family: inherit; + font-size: 0.76rem; + text-align: left; + cursor: pointer; +} + +.kmeans-cluster-source-advanced:hover { + border-color: rgba(43, 51, 233, 0.3); +} + +.kmeans-cluster-source-advanced[aria-expanded='true'] { + border-bottom-left-radius: 0; + border-bottom-right-radius: 0; +} + +.kmeans-params-label { + flex: none; + font-weight: 600; + color: var(--text-main); +} + +.kmeans-params-summary { + flex: 1; + min-width: 0; + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; +} + +.kmeans-params-chevron { + margin-left: auto; + font-size: 0.66rem; + color: var(--primary-color); +} + +.kmeans-cluster-source-params { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); + gap: 10px 12px; + margin-top: -8px; + padding: 10px 12px; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-top: 0; + border-radius: 0 0 var(--radius-md, 8px) var(--radius-md, 8px); + background: var(--bg-neutral, #f8fafc); +} + +.kmeans-cluster-source-apply { + display: inline-flex; + align-items: center; + gap: 8px; + justify-self: start; + height: 34px; + padding: 0 18px; + border: 0; + border-radius: 999px; + background: var(--primary-color); + color: #ffffff; + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + cursor: pointer; + box-shadow: 0 1px 2px rgba(43, 51, 233, 0.05); +} + +.kmeans-cluster-source-apply:disabled { + opacity: 0.45; + cursor: not-allowed; +} + +.kmeans-cluster-source-remove { + margin-left: auto; + padding: 4px 10px; + border: 1px solid #e2e8f0; + border-radius: var(--radius-sm, 4px); + background: transparent; + color: var(--text-muted); + font-family: inherit; + font-size: 0.74rem; + font-weight: 600; + cursor: pointer; +} + +.kmeans-cluster-source-error { + margin: 0; + font-size: 0.78rem; + color: var(--danger, #ef4444); +} + +.kmeans-cluster-source-note { + margin: 0; + font-size: 0.74rem; + color: var(--text-muted); +} + +/* In-place K-means report run, for when there is no finished compatible run yet: + three numbered steps (choose variables, check clusters, name the column). */ +.kmeans-cluster-source { + gap: 16px; +} + +.kmeans-step { + display: grid; + gap: 8px; +} + +.kmeans-step-head { + display: flex; + align-items: center; + gap: 8px; + font-size: 0.84rem; + font-weight: 700; + color: var(--text-main); +} + +.kmeans-step-number { + display: inline-flex; + align-items: center; + justify-content: center; + flex: none; + width: 18px; + height: 18px; + border-radius: 50%; + background: var(--primary-color); + color: #ffffff; + font-size: 0.68rem; +} + +.kmeans-step-meta { + margin-left: auto; +} + +.kmeans-step-meta { + font-size: 0.74rem; + font-weight: 500; + color: var(--text-muted); +} + +.kmeans-step-run { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 10px 12px; +} + +.kmeans-step-code { + box-sizing: border-box; + width: 260px; + max-width: 100%; + font-family: ui-monospace, SFMono-Regular, Menlo, monospace; + font-size: 0.84rem; +} + +.kmeans-cluster-source-note code { + font-family: ui-monospace, SFMono-Regular, Menlo, monospace; + color: var(--primary-dark, #1b21a3); +} + + +.kmeans-cluster-source-vars { + display: flex; + flex-wrap: wrap; + gap: 6px; +} + +.kmeans-cluster-source-var { + display: inline-flex; + align-items: center; + gap: 6px; + height: 30px; + padding: 0 12px; + border: 0; + border-radius: 999px; + background: var(--card-bg, #ffffff); + box-shadow: inset 0 0 0 1px rgba(43, 51, 233, 0.18); + font-family: inherit; + font-size: 0.78rem; + font-weight: 600; + color: var(--text-main); + cursor: pointer; +} + + +.kmeans-cluster-source-var i { + font-size: 0.66rem; +} + +/* Tonal, not solid: Run is the one filled primary in the step, so a chosen variable + must not read as a second button to press. */ +.kmeans-cluster-source-var.is-selected { + background: color-mix(in srgb, var(--primary-color) 8%, white); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 55%, transparent); + color: var(--primary-dark, #1b21a3); +} + +.kmeans-cluster-source-var:hover:not(.is-selected) { + background: rgba(43, 51, 233, 0.06); + box-shadow: inset 0 0 0 1px rgba(43, 51, 233, 0.45); +} + +.kmeans-cluster-report { + display: grid; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: var(--radius-md, 8px); + overflow: hidden; + background: var(--card-bg); +} + + + + +.kmeans-cluster-report-row { + display: grid; + grid-template-columns: 110px 190px minmax(0, 1fr); + gap: 12px; + align-items: center; + padding: 9px 12px; + border-top: 1px solid rgba(43, 51, 233, 0.06); + font-size: 0.8rem; +} + +.kmeans-cluster-report-size { + display: flex; + align-items: center; + gap: 8px; + color: var(--text-muted); + font-size: 0.78rem; + font-variant-numeric: tabular-nums; +} + +.kmeans-cluster-report-profile { + color: #334155; + font-size: 0.78rem; + line-height: 1.4; +} + +.kmeans-cluster-report-header { + border-top: 0; + padding: 8px 12px; + background: var(--studio-card-header-bg, #f8fbff); + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.015em; + text-transform: uppercase; +} + +.kmeans-cluster-report-bar { + flex: 1; + height: 6px; + border-radius: 999px; + background: color-mix(in srgb, var(--text-muted) 14%, white); + overflow: hidden; +} + +.kmeans-cluster-report-bar > span { + display: block; + height: 100%; + border-radius: 999px; + background: var(--primary-color); +} + +.kmeans-cluster-report-size > span:last-child { + min-width: 64px; + text-align: right; +} + +@media (max-width: 720px) { + .kmeans-cluster-report-row { + grid-template-columns: 90px minmax(0, 1fr); + } + + .kmeans-cluster-report-profile, + .kmeans-cluster-report-header > :nth-child(3) { + grid-column: 1 / -1; + } +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.html b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.html new file mode 100644 index 000000000..b097a61be --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.html @@ -0,0 +1,288 @@ +<section class="kmeans-cluster-source"> + @if (applied(); as creator) { + <div class="kmeans-cluster-source-applied"> + <span class="kmeans-cluster-source-applied-code">Column {{ appliedColumnName() }}</span> + <span class="kmeans-cluster-source-applied-detail"> + {{ clusterCount() }} clusters from {{ clusterVariables().join(', ') }} + </span> + <button + type="button" + class="kmeans-cluster-source-remove" + [disabled]="!canRemove()" + [title]="removeTooltip()" + (click)="remove()">Remove</button> + </div> + } @else { + <div class="kmeans-mode-switch" role="group" aria-label="K-means column source"> + <button type="button" class="kmeans-mode-btn" + [class.active]="sourceMode() === 'result'" + [attr.aria-pressed]="sourceMode() === 'result'" + (click)="setSourceMode('result')"> + From a K-means result + </button> + <button type="button" class="kmeans-mode-btn" + [class.active]="sourceMode() === 'fit'" + [attr.aria-pressed]="sourceMode() === 'fit'" + (click)="setSourceMode('fit')"> + Fit on selected variables + </button> + </div> + + @if (sourceMode() === 'fit') { + <div class="kmeans-step"> + <div class="kmeans-step-head"> + <span class="kmeans-step-number">1</span>Choose the numerical variables to cluster + </div> + + @if (fitNumericVariables().length === 0) { + <p class="kmeans-cluster-source-note">No numerical variables in this experiment yet.</p> + } @else { + <div class="kmeans-cluster-source-vars"> + @for (variable of fitNumericVariables(); track variable.code) { + <button type="button" class="kmeans-cluster-source-var" + [class.is-selected]="fitSelectedCodes().includes(variable.code)" + [attr.aria-pressed]="fitSelectedCodes().includes(variable.code)" + (click)="toggleRunVariable(variable.code)"> + <i class="fas" [class.fa-check]="fitSelectedCodes().includes(variable.code)" [class.fa-plus]="!fitSelectedCodes().includes(variable.code)" aria-hidden="true"></i> + {{ variable.label || variable.code }} + </button> + } + </div> + } + + <div class="kmeans-cluster-source-field"> + <label for="kmeans-fit-k">Number of clusters</label> + <input id="kmeans-fit-k" type="number" min="1" step="1" + [value]="fitK()" + (input)="setFitK($any($event.target).value)" /> + </div> + + <button type="button" class="kmeans-cluster-source-advanced" + aria-controls="kmeans-fit-params" + [attr.aria-expanded]="advancedOpen()" + (click)="advancedOpen.set(!advancedOpen())"> + <i class="fas fa-sliders" aria-hidden="true"></i> + <span class="kmeans-params-label">Parameters</span> + @if (!advancedOpen() && parameterSummary()) { + <span class="kmeans-params-summary">{{ parameterSummary() }}</span> + } + <i class="fas kmeans-params-chevron" [class.fa-chevron-down]="!advancedOpen()" [class.fa-chevron-up]="advancedOpen()" aria-hidden="true"></i> + </button> + + @if (advancedOpen()) { + <div id="kmeans-fit-params" class="kmeans-cluster-source-params"> + @for (field of fitParameterFields; track field.key) { + <div class="kmeans-cluster-source-field"> + <label [attr.for]="'kmeans-fit-param-' + field.key">{{ field.label || field.key }}</label> + @if (field.type === 'select') { + <select [id]="'kmeans-fit-param-' + field.key" + [value]="parameterValue(field)" + (change)="setParameter(field.key, $any($event.target).value)"> + @for (opt of field.options ?? []; track optionBindingValue(opt)) { + <option [value]="optionBindingValue(opt)">{{ optionLabel(opt) }}</option> + } + </select> + } @else { + <input [id]="'kmeans-fit-param-' + field.key" type="number" + [step]="field.types?.includes('real') ? 'any' : 1" + [value]="parameterValue(field)" + (input)="setParameter(field.key, $any($event.target).value)" /> + } + </div> + } + </div> + } + + <div class="kmeans-cluster-source-field"> + <label for="kmeans-fit-code">New column name</label> + <input id="kmeans-fit-code" type="text" + [ngModel]="code()" (ngModelChange)="code.set($event)" + placeholder="e.g. kmeans_cluster" /> + </div> + + @if (error()) { + <p class="kmeans-cluster-source-error">{{ error() }}</p> + } + + <button type="button" class="kmeans-cluster-source-apply" + [disabled]="fitSelectedCodes().length === 0 || !code().trim() || fitK() < 1" + (click)="applyFit()"> + <i class="fas fa-check" aria-hidden="true"></i> + Add cluster column + </button> + </div> + } @else { + @if (loading()) { + <p class="kmeans-cluster-source-note">Loading K-means experiments…</p> + } + @if (error()) { + <p class="kmeans-cluster-source-error">{{ error() }}</p> + } + @if (!loading() && compatible().length === 0) { + <!-- No reusable run: three numbered steps run a report here and turn it into the column. --> + <div class="kmeans-step"> + <div class="kmeans-step-head"> + <span class="kmeans-step-number">1</span>Choose the numerical variables to group by + </div> + + @if (numericVariables().length === 0) { + <p class="kmeans-cluster-source-note">No numerical variables in this experiment yet.</p> + } @else { + <div class="kmeans-cluster-source-vars"> + @for (variable of numericVariables(); track variable.code) { + <button type="button" class="kmeans-cluster-source-var" + [class.is-selected]="runSelectedCodes().includes(variable.code)" + [attr.aria-pressed]="runSelectedCodes().includes(variable.code)" + (click)="toggleRunVariable(variable.code)"> + <i class="fas" [class.fa-check]="runSelectedCodes().includes(variable.code)" [class.fa-plus]="!runSelectedCodes().includes(variable.code)" aria-hidden="true"></i> + {{ variable.label || variable.code }} + </button> + } + </div> + } + + <!-- Between the variables and Run, in reading order: closed, it says whether the run + departs from the catalog defaults without opening anything. --> + @if (parameterFields().length) { + <button type="button" class="kmeans-cluster-source-advanced" + aria-controls="kmeans-params" + [attr.aria-expanded]="advancedOpen()" + (click)="advancedOpen.set(!advancedOpen())"> + <i class="fas fa-sliders" aria-hidden="true"></i> + <span class="kmeans-params-label">Parameters</span> + @if (!advancedOpen() && parameterSummary()) { + <span class="kmeans-params-summary">{{ parameterSummary() }}</span> + } + <i class="fas kmeans-params-chevron" [class.fa-chevron-down]="!advancedOpen()" [class.fa-chevron-up]="advancedOpen()" aria-hidden="true"></i> + </button> + } + + @if (advancedOpen() && parameterFields().length) { + <div id="kmeans-params" class="kmeans-cluster-source-params"> + @for (field of parameterFields(); track field.key) { + <div class="kmeans-cluster-source-field"> + <label [attr.for]="'kmeans-param-' + field.key">{{ field.label || field.key }}</label> + @if (field.type === 'select') { + <select [id]="'kmeans-param-' + field.key" + [value]="parameterValue(field)" + (change)="setParameter(field.key, $any($event.target).value)"> + @for (opt of field.options ?? []; track optionBindingValue(opt)) { + <option [value]="optionBindingValue(opt)">{{ optionLabel(opt) }}</option> + } + </select> + } @else if (field.type === 'number') { + <input [id]="'kmeans-param-' + field.key" type="number" + [step]="field.types?.includes('real') ? 'any' : 1" + [value]="parameterValue(field)" + (input)="setParameter(field.key, $any($event.target).value)" /> + } @else { + <input [id]="'kmeans-param-' + field.key" type="text" + [value]="parameterValue(field)" + (input)="setParameter(field.key, $any($event.target).value)" /> + } + </div> + } + </div> + } + + <div class="kmeans-step-run"> + <button type="button" class="kmeans-cluster-source-apply" + [disabled]="runLoading() || runSelectedCodes().length === 0" + (click)="runReport()"> + <i class="fas fa-play" aria-hidden="true"></i> + {{ runLoading() ? 'Running K-means…' : 'Run K-means report' }} + </button> + <span class="kmeans-cluster-source-note"> + {{ runLoading() ? 'Waiting for every node to answer…' : 'Uses your current datasets and filters.' }} + </span> + </div> + + @if (runError()) { + <p class="kmeans-cluster-source-error">{{ runError() }}</p> + } + </div> + + @if (report(); as result) { + <div class="kmeans-step"> + <div class="kmeans-step-head"> + <span class="kmeans-step-number">2</span>Check the clusters + <span class="kmeans-step-meta"> + {{ result.selected_k }} clusters · {{ result.k_selection }} selection@if (result.elbow; as elbow) { (k {{ elbow.k_min }}–{{ elbow.k_max }})} · {{ result.n_obs_interval }} records + </span> + </div> + <div class="kmeans-cluster-report"> + <div class="kmeans-cluster-report-row kmeans-cluster-report-header"> + <span>Cluster</span><span>Records</span><span>What sets it apart</span> + </div> + @for (cluster of result.clusters; track cluster.cluster_id) { + <div class="kmeans-cluster-report-row"> + <strong>{{ cluster.label }}</strong> + <span class="kmeans-cluster-report-size"> + <span class="kmeans-cluster-report-bar"><span [style.width.%]="clusterShares()[cluster.cluster_id]"></span></span> + <span>{{ cluster.size_interval }}</span> + </span> + <span class="kmeans-cluster-report-profile">{{ cluster.interpretation || (cluster.profile || []).join('; ') }}</span> + </div> + } + </div> + </div> + + @if (reportReusable()) { + <div class="kmeans-step"> + <div class="kmeans-step-head"><span class="kmeans-step-number">3</span>Name the column and use it</div> + <div class="kmeans-step-run"> + <input id="kmeans-cluster-source-run-code" type="text" class="kmeans-step-code" + aria-label="New column name" + [ngModel]="code()" (ngModelChange)="code.set($event)" + placeholder="e.g. kmeans_cluster" /> + <button type="button" class="kmeans-cluster-source-apply" + [disabled]="!code().trim()" (click)="useReport()"> + <i class="fas fa-check" aria-hidden="true"></i> + Use as cluster column + </button> + </div> + <span class="kmeans-cluster-source-note">Adds <code>{{ code().trim() || 'the column' }}</code> with one value per cluster — then Apply below to include it in the run.</span> + </div> + } + } + } + @if (!loading() && compatible().length > 0) { + <div class="kmeans-cluster-source-field"> + <label for="kmeans-cluster-source-experiment">K-means experiment</label> + <select + id="kmeans-cluster-source-experiment" + [ngModel]="selectedUuid()" + (ngModelChange)="selectedUuid.set($event)"> + @for (experiment of compatible(); track experiment.uuid) { + <option [value]="experiment.uuid"> + {{ experiment.name }} · {{ experiment.created | date: 'mediumDate' }} + </option> + } + </select> + </div> + <div class="kmeans-cluster-source-field"> + <label for="kmeans-cluster-source-code">New column name</label> + <input + id="kmeans-cluster-source-code" + type="text" + [ngModel]="code()" + (ngModelChange)="code.set($event)" + placeholder="e.g. kmeans_cluster" /> + </div> + <button + type="button" + class="kmeans-cluster-source-apply" + [disabled]="applying() || !selectedUuid()" + (click)="apply()"> + Add column + </button> + } + @if (!loading() && hiddenCount() > 0) { + <p class="kmeans-cluster-source-note"> + {{ hiddenCount() }} K-means experiments hidden because their data model, datasets or filters + differ. + </p> + } + } + } +</section> diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.spec.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.spec.ts new file mode 100644 index 000000000..473c5a639 --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.spec.ts @@ -0,0 +1,491 @@ +import type { Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { of, throwError } from 'rxjs'; + +import { BackendExperiment } from '../../../../models/backend-experiment.model'; +import { KMeansReusablePreprocessing } from '../../../../core/kmeans-cluster-source.utils'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../../services/experiments-dashboard.service'; +import { KMeansClusterSourceComponent } from './kmeans-cluster-source.component'; + +const CONTEXT = { dataModel: 'Stroke:3.7', datasets: ['diabetes'], filters: null }; + +function kmeansExperiment(uuid: string, overrides: Record<string, unknown> = {}): BackendExperiment { + return { + uuid, + name: `K-means ${uuid}`, + created: '2024-01-01T00:00:00Z', + finished: '2024-01-01T00:05:00Z', + shared: false, + viewed: false, + status: 'success', + analysis: { + inputdata: { + data_model: CONTEXT.dataModel, + datasets: CONTEXT.datasets, + filters: null, + variables: ['age'], + }, + algorithm: { name: 'kmeans' }, + }, + ...overrides, + } as BackendExperiment; +} + +function reusable(): KMeansReusablePreprocessing { + return { + enumeration_prefix: 'cluster_', + preprocessing_name: 'kmeans_cluster_creator', + cluster_variables: ['age', 'bmi'], + centers: { c1: { age: 40 } }, + source_context: { data_model: CONTEXT.dataModel!, datasets: CONTEXT.datasets, input_fingerprint: 'fp' }, + available_outputs: [], + cluster_choices: [ + { cluster_id: 'c1', label: 'Cluster 1' }, + { cluster_id: 'c2', label: 'Cluster 2' }, + ], + }; +} + +describe('KMeansClusterSourceComponent', () => { + let fixture: ComponentFixture<KMeansClusterSourceComponent>; + let component: KMeansClusterSourceComponent; + let studio: { + selectedVariables: Mock; + algorithmAssignableVariables: Mock; + getActiveDataModelCode: Mock; + requestDatasets: Mock; + requestFilters: Mock; + appliedTransformations: ReturnType<typeof signal<any[]>>; + upsertTransformation: Mock; + outputsInUse: Mock; + removeTransformation: Mock; + loadKMeansReport: Mock; + backendAlgorithms: ReturnType<typeof signal<Record<string, { configSchema?: unknown[] }>>>; + }; + let dashboard: { + listKMeansExperiments: Mock; + getExperimentResult: Mock; + }; + + function html(): HTMLElement { + return fixture.nativeElement; + } + + beforeEach(async () => { + studio = { + selectedVariables: vi.fn().mockReturnValue([]), + algorithmAssignableVariables: vi.fn().mockReturnValue([]), + getActiveDataModelCode: vi.fn().mockReturnValue(CONTEXT.dataModel), + requestDatasets: vi.fn().mockReturnValue(CONTEXT.datasets), + requestFilters: vi.fn().mockReturnValue(CONTEXT.filters), + appliedTransformations: signal([] as any[]), + outputsInUse: vi.fn().mockReturnValue([]), + upsertTransformation: vi.fn((step: any) => { + const current = studio.appliedTransformations(); + const index = current.findIndex((existing) => existing.id === step.id); + studio.appliedTransformations.set( + index === -1 ? [...current, step] : current.map((existing, i) => (i === index ? step : existing)), + ); + }), + removeTransformation: vi.fn((id: string) => { + studio.appliedTransformations.set(studio.appliedTransformations().filter((step) => step.id !== id)); + }), + loadKMeansReport: vi.fn().mockReturnValue(of({})), + backendAlgorithms: signal({}), + }; + dashboard = { + listKMeansExperiments: vi.fn().mockReturnValue(of([])), + getExperimentResult: vi.fn().mockReturnValue(of({})), + }; + + await TestBed.configureTestingModule({ + imports: [KMeansClusterSourceComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: studio }, + { provide: ExperimentsDashboardService, useValue: dashboard }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(KMeansClusterSourceComponent); + fixture.componentRef.setInput('stepId', 'kmeans-step-1'); + component = fixture.componentInstance; + }); + + it('lists only compatible experiments and reports the hidden count', () => { + dashboard.listKMeansExperiments.mockReturnValue( + of([ + kmeansExperiment('a'), + // Different datasets → hidden. + kmeansExperiment('b', { analysis: { inputdata: { data_model: CONTEXT.dataModel, datasets: ['other'], filters: null, variables: [] }, algorithm: { name: 'kmeans' } } }), + // Different data model → hidden. + kmeansExperiment('c', { analysis: { inputdata: { data_model: 'Other:1', datasets: CONTEXT.datasets, filters: null, variables: [] }, algorithm: { name: 'kmeans' } } }), + ]), + ); + + fixture.detectChanges(); + + expect(component.compatible().map((experiment) => experiment.uuid)).toEqual(['a']); + expect(component.hiddenCount()).toBe(2); + expect(html().querySelectorAll('select option').length).toBe(1); + expect(html().textContent).toContain('2 K-means experiments hidden'); + }); + + it('apply() stores a valid reusable_preprocessing under the default code', () => { + dashboard.listKMeansExperiments.mockReturnValue(of([kmeansExperiment('a')])); + fixture.detectChanges(); + component.selectedUuid.set('a'); + dashboard.getExperimentResult.mockReturnValue(of({ result: { reusable_preprocessing: reusable() } })); + + component.apply(); + + expect(studio.upsertTransformation).toHaveBeenCalledWith({ + id: 'kmeans-step-1', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['kmeans_cluster'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: reusable(), + }, + }); + expect(component.error()).toBeNull(); + }); + + it('apply() refuses a result without reusable_preprocessing', () => { + dashboard.listKMeansExperiments.mockReturnValue(of([kmeansExperiment('a')])); + fixture.detectChanges(); + component.selectedUuid.set('a'); + dashboard.getExperimentResult.mockReturnValue(of({ result: { clusters: [] } })); + + component.apply(); + + expect(component.error()).toBe( + 'This K-means result cannot be reused. Re-run the K-means experiment and try again.', + ); + expect(studio.upsertTransformation).not.toHaveBeenCalled(); + }); + + it('apply() rejects a column name that is not an identifier', () => { + dashboard.listKMeansExperiments.mockReturnValue(of([kmeansExperiment('a')])); + fixture.detectChanges(); + component.selectedUuid.set('a'); + component.code.set('1st column'); + + component.apply(); + + expect(component.error()).toBe( + 'Use letters, digits and underscores, starting with a letter or underscore.', + ); + expect(dashboard.getExperimentResult).not.toHaveBeenCalled(); + expect(studio.upsertTransformation).not.toHaveBeenCalled(); + }); + + it('apply() and useReport() refuse a colliding column name without upserting', () => { + dashboard.listKMeansExperiments.mockReturnValue(of([kmeansExperiment('a')])); + fixture.componentRef.setInput('columnNameError', () => 'Name already used'); + fixture.detectChanges(); + component.selectedUuid.set('a'); + dashboard.getExperimentResult.mockReturnValue(of({ result: { reusable_preprocessing: reusable() } })); + + component.apply(); + + expect(component.error()).toBe('Name already used'); + expect(studio.upsertTransformation).not.toHaveBeenCalled(); + + studio.upsertTransformation.mockClear(); + component.reportReusable.set(reusable()); + component.useReport(); + + expect(component.runError()).toBe('Name already used'); + expect(studio.upsertTransformation).not.toHaveBeenCalled(); + }); + + it('remove() drops the applied cluster column', () => { + studio.appliedTransformations.set([{ + id: 'kmeans-step-1', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['kmeans_cluster'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: reusable(), + }, + }]); + + component.remove(); + + expect(studio.removeTransformation).toHaveBeenCalledWith('kmeans-step-1'); + }); + + it('inner Remove is a no-op when canRemove is false', () => { + studio.appliedTransformations.set([{ + id: 'kmeans-step-1', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['kmeans_cluster'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: reusable(), + }, + }]); + fixture.componentRef.setInput('canRemove', false); + fixture.componentRef.setInput('removeTooltip', 'Used by later'); + fixture.detectChanges(); + + component.remove(); + + expect(studio.removeTransformation).not.toHaveBeenCalled(); + }); + + it('asks the stage to run the creator once the column is applied', () => { + dashboard.listKMeansExperiments.mockReturnValue(of([kmeansExperiment('a')])); + fixture.detectChanges(); + component.selectedUuid.set('a'); + dashboard.getExperimentResult.mockReturnValue(of({ result: { reusable_preprocessing: reusable() } })); + const runs: number[] = []; + component.runRequested.subscribe(() => runs.push(1)); + + component.apply(); + + expect(runs.length).toBe(1); + }); + + it('reports a failed experiment list instead of an empty picker', () => { + dashboard.listKMeansExperiments.mockReturnValue(throwError(() => new Error('offline'))); + + fixture.detectChanges(); + + expect(component.error()).toBe('Could not load K-means experiments.'); + expect(component.loading()).toBe(false); + }); + + it('offers the in-place K-means report when the user has no finished run', () => { + dashboard.listKMeansExperiments.mockReturnValue(of([])); + + fixture.detectChanges(); + + expect(html().textContent).toContain('Choose the numerical variables to group by'); + expect(html().textContent).toContain('Run K-means report'); + }); + + it('runs the K-means report in place and stores its reusable preprocessing', () => { + studio.selectedVariables.mockReturnValue([{ code: 'age', label: 'Age', type: 'continuous' }]); + const reusablePreprocessing = reusable(); + studio.loadKMeansReport.mockReturnValue(of({ + result_type: 'privacy_safe_cluster_report', + selected_k: 2, + k_selection: 'manual', + n_obs_interval: '100–200', + clusters: [ + { cluster_id: 'c1', label: 'Cluster 1', size_interval: '40–60', center: { age: 40 }, profile: [], interpretation: 'Younger', quality: { compactness: null } }, + ], + elbow: null, + reusable_preprocessing: reusablePreprocessing, + })); + + fixture.detectChanges(); + component.runReport(); + + expect(studio.loadKMeansReport).toHaveBeenCalledWith(['age'], {}); + expect(component.report()?.selected_k).toBe(2); + expect(component.reportReusable()).toBe(reusablePreprocessing); + + component.useReport(); + expect(studio.upsertTransformation).toHaveBeenCalledWith({ + id: 'kmeans-step-1', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['kmeans_cluster'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: reusablePreprocessing, + }, + }); + }); + + it('keeps catalog defaults until Parameters changes one', () => { + studio.selectedVariables.mockReturnValue([{ code: 'age', label: 'Age', type: 'continuous' }]); + studio.backendAlgorithms.set({ + kmeans: { + configSchema: [ + { key: 'k_selection', label: 'K selection', type: 'select', required: true, default: 'manual', options: ['manual', 'elbow'] }, + { key: 'k', label: 'Number of clusters', type: 'number', types: ['int'], required: false, default: 4 }, + ], + }, + }); + + fixture.detectChanges(); + + const advanced = html().querySelector('.kmeans-cluster-source-advanced') as HTMLButtonElement; + expect(advanced.querySelector('.kmeans-params-label')?.textContent?.trim()).toBe('Parameters'); + // Closed, the row says whether the run departs from the catalog. + expect(advanced.querySelector('.kmeans-params-summary')?.textContent?.trim()).toBe('All defaults'); + expect(html().querySelector('.kmeans-cluster-source-params')).toBeNull(); + + advanced.click(); + fixture.detectChanges(); + + const k = html().querySelector<HTMLInputElement>('#kmeans-param-k'); + expect(k?.value).toBe('4'); + k!.value = '3'; + k!.dispatchEvent(new Event('input')); + fixture.detectChanges(); + expect(component.parameterSummary()).toBe('Number of clusters 3'); + + component.runReport(); + expect(studio.loadKMeansReport).toHaveBeenCalledWith(['age'], { k: 3 }); + + k!.value = ' 4 '; + k!.dispatchEvent(new Event('input')); + fixture.detectChanges(); + expect(component.parameterSummary()).toBe('All defaults'); + + k!.value = 'nope'; + k!.dispatchEvent(new Event('input')); + fixture.detectChanges(); + expect(component.parameterSummary()).toBe('All defaults'); + }); + + it('sizes each cluster share bar from the interval midpoint', () => { + studio.selectedVariables.mockReturnValue([{ code: 'age', label: 'Age', type: 'continuous' }]); + studio.loadKMeansReport.mockReturnValue(of({ + result_type: 'privacy_safe_cluster_report', + selected_k: 2, + k_selection: 'manual', + n_obs_interval: '100–200', + clusters: [ + { cluster_id: 'c1', label: 'Cluster 1', size_interval: '40–60', center: { age: 40 }, profile: [], interpretation: 'Younger', quality: { compactness: null } }, + { cluster_id: 'c2', label: 'Cluster 2', size_interval: '120–140', center: { age: 60 }, profile: [], interpretation: 'Older', quality: { compactness: null } }, + ], + elbow: null, + reusable_preprocessing: reusable(), + })); + + fixture.detectChanges(); + component.runReport(); + + // Privacy intervals arrive as "40–60", so the bar is sized off the midpoint: 50 of 180. + expect(component.clusterShares()['c1']).toBeCloseTo((50 / 180) * 100, 5); + expect(component.clusterShares()['c2']).toBeCloseTo((130 / 180) * 100, 5); + + fixture.detectChanges(); + expect(html().querySelectorAll('.kmeans-cluster-report-bar > span').length).toBe(2); + }); + + it('setSourceMode clears parameterDraft and runExcludedCodes', () => { + (component as any).parameterDraft.set({ k: '5', maxiter: '50' }); + (component as any).runExcludedCodes.set(new Set(['age'])); + + component.setSourceMode('fit'); + + expect((component as any).parameterDraft()).toEqual({}); + expect((component as any).runExcludedCodes().size).toBe(0); + expect(component.sourceMode()).toBe('fit'); + }); + + it('fit mode stores variables without reusable_preprocessing', () => { + studio.selectedVariables.mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'bmi', label: 'BMI', type: 'real' }, + ]); + studio.algorithmAssignableVariables.mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'bmi', label: 'BMI', type: 'real' }, + ]); + + fixture.detectChanges(); + component.sourceMode.set('fit'); + component.code.set('km_fit'); + component.fitK.set(3); + // Deselect nothing — both numerical CDEs start selected. + component.applyFit(); + + expect(studio.upsertTransformation).toHaveBeenCalledWith({ + id: 'kmeans-step-1', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km_fit'], + variables: ['age', 'bmi'], + k: 3, + enumeration_prefix: 'cluster_', + }, + }); + const params = studio.upsertTransformation.mock.calls[0][0].parameters; + expect(params).not.toHaveProperty('reusable_preprocessing'); + }); + + it('fit mode offers earlier created columns unselected until the user picks them', () => { + studio.appliedTransformations.set([ + { id: 'pca-step', name: 'pca_column_creator', parameters: { new_column_names: ['pca_pc1'] } }, + { id: 'kmeans-step-1', name: 'kmeans_cluster_creator', parameters: {} }, + ]); + studio.selectedVariables.mockReturnValue([{ code: 'age', label: 'Age', type: 'real' }]); + studio.algorithmAssignableVariables.mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'pca_pc1', label: 'pca_pc1', type: 'real', isCreatedColumn: true }, + ]); + + fixture.detectChanges(); + component.setSourceMode('fit'); + + expect(component.fitNumericVariables().map((variable) => variable.code)).toEqual(['age', 'pca_pc1']); + expect(component.fitSelectedCodes()).toEqual(['age']); + + component.toggleRunVariable('pca_pc1'); + expect(component.fitSelectedCodes()).toEqual(['age', 'pca_pc1']); + }); + + it('two fit-mode components with different stepId values write two steps', () => { + studio.selectedVariables.mockReturnValue([{ code: 'age', label: 'Age', type: 'real' }]); + studio.algorithmAssignableVariables.mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + ]); + + fixture.detectChanges(); + component.sourceMode.set('fit'); + component.code.set('km_a'); + component.fitK.set(2); + component.applyFit(); + + const second = TestBed.createComponent(KMeansClusterSourceComponent); + second.componentRef.setInput('stepId', 'kmeans-step-2'); + const other = second.componentInstance; + second.detectChanges(); + other.sourceMode.set('fit'); + other.code.set('km_b'); + other.fitK.set(4); + other.applyFit(); + + expect(studio.appliedTransformations().map((step) => step.id)).toEqual([ + 'kmeans-step-1', + 'kmeans-step-2', + ]); + expect(studio.appliedTransformations().map((step) => step.parameters['new_column_names'])).toEqual([ + ['km_a'], + ['km_b'], + ]); + for (const step of studio.appliedTransformations()) { + expect(step.parameters).not.toHaveProperty('reusable_preprocessing'); + expect(step.parameters['variables']).toEqual(['age']); + expect(step.parameters['enumeration_prefix']).toBe('cluster_'); + } + }); + + it('renaming a cluster column a later step reads is refused', () => { + studio.selectedVariables.mockReturnValue([{ code: 'age', label: 'Age', type: 'real' }]); + studio.outputsInUse.mockImplementation((id: string, next: string[]) => + id === 'kmeans-step-1' && !next.includes('km') ? ['km'] : [], + ); + fixture.detectChanges(); + component.sourceMode.set('fit'); + component.code.set('km_renamed'); + + component.applyFit(); + + expect(component.error()).toBe('Used by a later step: km'); + expect(studio.upsertTransformation).not.toHaveBeenCalled(); + }); +}); diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.ts new file mode 100644 index 000000000..70196c91a --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/kmeans-cluster-source/kmeans-cluster-source.component.ts @@ -0,0 +1,525 @@ +import { + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + OnInit, + computed, + effect, + inject, + input, + output, + signal, +} from '@angular/core'; +import { DatePipe } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../../services/experiments-dashboard.service'; +import { BackendExperiment } from '../../../../models/backend-experiment.model'; +import { KMeansResult } from '../../../../models/algorithm-results.model'; +import { + formatAlgorithmParameterValue, + optionBindingValue, +} from '../../../../core/algorithm-parameter.utils'; +import { isOutlierEligibleVariable } from '../../../../core/outlier-rules'; +import { COLUMN_CODE_PATTERN, fitSelectedCodes, fitVariablePool } from '../../../../core/transformation-steps.utils'; +import { + KMEANS_CLUSTER_CREATOR, + KMeansReusablePreprocessing, + KMeansSourceContext, + canonicalFilterKey, + findKMeansReport, + findReusablePreprocessing, + isPlainObject, + analysisSourceIsReusable, +} from '../../../../core/kmeans-cluster-source.utils'; + +/** Optional fit-mode creator params and their Exaflow defaults. Always-sent keys (k, + * enumeration_prefix, variables, new_column_names) are not listed here. */ +const FIT_PARAMETER_FIELDS: KMeansParameterField[] = [ + { + key: 'init_method', + label: 'Initialization method', + type: 'select', + default: 'random_range', + options: ['random_range', 'multi_start_random_range'], + }, + { key: 'n_init', label: 'Number of initializations', type: 'number', types: ['int'], default: 1 }, + { key: 'tol', label: 'Convergence tolerance', type: 'number', types: ['real'], default: 0.0001 }, + { key: 'maxiter', label: 'Maximum iterations', type: 'number', types: ['int'], default: 100 }, + { key: 'random_state', label: 'Random seed', type: 'number', types: ['int'], default: 123 }, +]; + +interface KMeansParameterField { + key: string; + label?: string; + type?: string; + types?: string[]; + required?: boolean; + default?: unknown; + options?: unknown[]; +} + +/** Stored creator config: recipe mode has reusable_preprocessing; fit mode has variables. */ +type AppliedKMeansCreator = { + new_column_names: string[]; + k: number; + enumeration_prefix: string; + reusable_preprocessing?: KMeansReusablePreprocessing; + variables?: string[]; +}; + +type SourceMode = 'result' | 'fit'; + +/** + * Reuses a finished K-means run as a categorical cluster column, or fits K-means + * directly on selected numerical variables. Each card edits one transformation step. + */ +@Component({ + selector: 'app-kmeans-cluster-source', + imports: [DatePipe, FormsModule], + templateUrl: './kmeans-cluster-source.component.html', + styleUrl: './kmeans-cluster-source.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class KMeansClusterSourceComponent implements OnInit { + private readonly studio = inject(ExperimentStudioService); + private readonly cdr = inject(ChangeDetectorRef); + private readonly dashboard = inject(ExperimentsDashboardService); + + /** Store step this editor writes; the Transformation stage passes one per card. */ + readonly stepId = input.required<string>(); + + /** + * Optional uniqueness check from the Transformation stage. When the name collides, + * apply/reuse must not call upsertTransformation. + */ + readonly columnNameError = input<(stepId: string, name: string) => string | null>(); + + /** False when a later step reads this card's outputs (station Remove uses the same gate). */ + readonly canRemove = input(true); + + /** Tooltip when Remove is disabled because of dependents. */ + readonly removeTooltip = input('Remove'); + + /** 'result' = recipe / in-place report; 'fit' = direct fit on selected variables. */ + readonly sourceMode = signal<SourceMode>('result'); + + readonly experiments = signal<BackendExperiment[]>([]); + readonly loading = signal(false); + readonly error = signal<string | null>(null); + readonly selectedUuid = signal<string | null>(null); + readonly code = signal('kmeans_cluster'); + readonly applying = signal(false); + + /** In-place report run: pick numerical variables, run `kmeans`, reuse its result. + * Only opt-outs are stored, so the picks follow the variable pool: a removed variable + * is never sent and a newly added one starts ticked. */ + private readonly runExcludedCodes = signal<ReadonlySet<string>>(new Set()); + readonly runLoading = signal(false); + readonly runError = signal<string | null>(null); + readonly advancedOpen = signal(false); + /** Edited parameter values. An absent key still means the schema default. */ + private readonly parameterDraft = signal<Record<string, string>>({}); + readonly report = signal<KMeansResult | null>(null); + readonly reportReusable = signal<KMeansReusablePreprocessing | null>(null); + + /** Fit mode: cluster count (always sent; Exaflow has no default). */ + readonly fitK = signal(4); + + readonly numericVariables = computed(() => + (this.studio.selectedVariables() as any[]).filter((variable) => isOutlierEligibleVariable(variable)) + ); + + /** + * Fit-mode pool: numerical CDEs plus numerical outputs of earlier transformation + * steps (e.g. PCA columns). The current step's own outputs are excluded. + */ + readonly fitNumericVariables = computed(() => + fitVariablePool(this.studio.appliedTransformations(), this.stepId(), [ + ...((this.studio.algorithmAssignableVariables() as any[]) ?? []), + ...this.numericVariables(), + ]) + ); + + readonly runSelectedCodes = computed(() => { + const excluded = this.runExcludedCodes(); + return this.numericVariables() + .map((variable) => String(variable.code)) + .filter((code) => !excluded.has(code)); + }); + + /** + * CDEs start selected and created columns start unselected: clustering raw variables + * together with components built from them is opt-in. A toggle flips the default. + */ + readonly fitSelectedCodes = computed(() => fitSelectedCodes(this.fitNumericVariables(), this.runExcludedCodes())); + + /** K-means parameters from the algorithm catalog. Closed until Parameters is opened. */ + readonly parameterFields = computed((): KMeansParameterField[] => { + const schema = this.studio.backendAlgorithms()?.['kmeans']?.configSchema ?? []; + return schema.filter((field): field is KMeansParameterField => + !!field?.key && ['number', 'select', 'text'].includes(field.type) + ); + }); + + /** Fit-mode optional creator parameters (Exaflow kmeans_cluster_creator defaults). */ + readonly fitParameterFields = FIT_PARAMETER_FIELDS; + + /** Closed Parameters row. Same values `parameterOverrides()` will send, so a trimmed or invalid draft is not listed. */ + readonly parameterSummary = computed(() => { + const fields = this.sourceMode() === 'fit' ? this.fitParameterFields : this.parameterFields(); + const overrides = this.parameterOverrides(fields); + const changed = fields + .filter((field) => field.key in overrides) + .map((field) => { + const label = formatAlgorithmParameterValue(overrides[field.key], field) || '—'; + return `${field.label || field.key} ${label}`; + }); + return changed.length ? changed.join(' · ') : 'All defaults'; + }); + + /** Share-bar width (%) per cluster id. Sizes arrive as privacy intervals ("40–60"), + * so each bar uses the midpoint of the numbers it holds: a "0–10" cluster is not empty. */ + readonly clusterShares = computed((): Record<string, number> => { + const clusters = this.report()?.clusters ?? []; + const sizes = clusters.map((cluster) => { + const bounds = (String(cluster.size_interval).replace(/,/g, '').match(/\d+(?:\.\d+)?/g) ?? []).map(Number); + return bounds.length ? bounds.reduce((sum, bound) => sum + bound, 0) / bounds.length : 0; + }); + const total = sizes.reduce((sum, size) => sum + size, 0); + return Object.fromEntries(clusters.map((cluster, i) => [cluster.cluster_id, total ? (sizes[i] / total) * 100 : 0])); + }); + + /** Inputs a report was run on; a report from other inputs must not become a column. */ + private readonly runContextKey = computed(() => { + const context = this.currentContext(); + return canonicalFilterKey({ + dataModel: context.dataModel, + datasets: [...context.datasets].map(String).sort(), + filters: context.filters ?? null, + codes: [...this.runSelectedCodes()].sort(), + parameters: this.parameterOverrides(this.parameterFields()), + }); + }); + private reportContextKey: string | null = null; + + /** What the next run would send — the context a past run must match to be reusable. */ + currentContext(): KMeansSourceContext { + return { + dataModel: this.studio.getActiveDataModelCode() || null, + datasets: this.studio.requestDatasets(), + filters: this.studio.requestFilters(), + }; + } + + readonly compatible = computed(() => { + const context = this.currentContext(); + return this.experiments().filter((experiment) => + analysisSourceIsReusable(experiment, 'kmeans', context)); + }); + + readonly hiddenCount = computed(() => this.experiments().length - this.compatible().length); + + readonly applied = computed((): AppliedKMeansCreator | null => { + const step = this.studio.appliedTransformations().find((candidate) => candidate.id === this.stepId()); + if (!step || step.name !== KMEANS_CLUSTER_CREATOR) return null; + const creator = step.parameters as unknown as AppliedKMeansCreator | undefined; + if (!isPlainObject(creator)) return null; + const names = creator.new_column_names; + const code = Array.isArray(names) ? String(names[0] ?? '').trim() : ''; + if (!code) return null; + if (typeof creator.k !== 'number') return null; + if (typeof creator.enumeration_prefix !== 'string') return null; + // Recipe mode. + if (isPlainObject(creator.reusable_preprocessing)) return creator; + // Fit mode: variables required, no reusable_preprocessing. + if (Array.isArray(creator.variables) && creator.variables.length > 0) return creator; + return null; + }); + + /** Emitted after a creator is applied, so the stage runs it and shows the cluster column. */ + readonly runRequested = output<void>(); + + constructor() { + // Steps stay mounted, so data model, datasets, filters or picks can change under a + // finished report; drop it rather than offer centers fitted on other data. + effect(() => { + const key = this.runContextKey(); + if (this.reportContextKey !== null && key !== this.reportContextKey) { + this.reportContextKey = null; + this.report.set(null); + this.reportReusable.set(null); + this.runError.set(null); + } + }); + } + + ngOnInit(): void { + this.loadExperiments(); + } + + setSourceMode(mode: SourceMode): void { + this.sourceMode.set(mode); + this.error.set(null); + this.runError.set(null); + // Result vs fit must not share parameter drafts or variable opt-outs. + this.parameterDraft.set({}); + this.runExcludedCodes.set(new Set()); + } + + /** A failed list read must not leave the picker looking empty by design. */ + private loadExperiments(): void { + this.loading.set(true); + this.dashboard.listKMeansExperiments().subscribe({ + next: (experiments) => { + this.experiments.set(experiments); + this.loading.set(false); + }, + error: () => { + this.error.set('Could not load K-means experiments.'); + this.loading.set(false); + }, + }); + } + + /** The applied column's name (exaflow `new_column_names[0]`), or empty when none. */ + appliedColumnName(): string { + return String(this.applied()?.new_column_names[0] ?? '').trim(); + } + + /** Cluster count the applied column carries, or null when the record is malformed. */ + clusterCount(): number | null { + const creator = this.applied(); + if (!creator) return null; + const choices = creator.reusable_preprocessing?.cluster_choices; + if (Array.isArray(choices) && choices.length) return choices.length; + return typeof creator.k === 'number' ? creator.k : null; + } + + /** The clustering variables of the applied column, for the summary line. */ + clusterVariables(): string[] { + const creator = this.applied(); + if (!creator) return []; + const fromRecipe = creator.reusable_preprocessing?.cluster_variables; + if (Array.isArray(fromRecipe)) return fromRecipe; + return Array.isArray(creator.variables) ? creator.variables.map(String) : []; + } + + apply(): void { + const code = this.code().trim(); + if (!this.selectedUuid() || !code) return; + if (!COLUMN_CODE_PATTERN.test(code)) { + this.error.set('Use letters, digits and underscores, starting with a letter or underscore.'); + return; + } + + this.applying.set(true); + this.dashboard.getExperimentResult(this.selectedUuid()!).subscribe({ + next: (response) => { + const reusable = findReusablePreprocessing(response); + this.applying.set(false); + if (!reusable) { + this.error.set('This K-means result cannot be reused. Re-run the K-means experiment and try again.'); + return; + } + this.store(code, reusable); + }, + error: () => { + this.applying.set(false); + this.error.set('Could not load the K-means result.'); + }, + }); + } + + /** Direct fit: write variables + k without a reusable_preprocessing recipe. */ + applyFit(): void { + const code = this.code().trim(); + const variables = this.fitSelectedCodes(); + const k = this.fitK(); + if (!code || !variables.length || !Number.isFinite(k) || k < 1) return; + if (!COLUMN_CODE_PATTERN.test(code)) { + this.error.set('Use letters, digits and underscores, starting with a letter or underscore.'); + return; + } + const nameError = this.nameProblem(code); + if (nameError) { + this.error.set(nameError); + return; + } + + this.error.set(null); + this.runError.set(null); + this.selectedUuid.set(null); + this.studio.upsertTransformation({ + id: this.stepId(), + name: KMEANS_CLUSTER_CREATOR, + parameters: { + new_column_names: [code], + variables, + k: Math.trunc(k), + enumeration_prefix: 'cluster_', + ...this.parameterOverrides(this.fitParameterFields), + }, + }); + this.runRequested.emit(); + } + + remove(): void { + if (!this.canRemove()) return; + this.error.set(null); + this.selectedUuid.set(null); + this.studio.removeTransformation(this.stepId()); + } + + toggleRunVariable(code: string): void { + this.runExcludedCodes.update((excluded) => { + const next = new Set(excluded); + if (next.has(code)) next.delete(code); + else next.add(code); + return next; + }); + } + + /** Run the K-means report on the selected numerical variables. Unchanged + * parameters stay on the catalog default; Advanced sends only what changed. */ + runReport(): void { + const codes = this.runSelectedCodes(); + if (!codes.length) return; + const overrides = this.parameterOverrides(this.parameterFields()); + const request$ = this.studio.loadKMeansReport(codes, overrides); + if (!request$) { + this.runError.set('K-means is not available for this workspace.'); + return; + } + + const requestKey = this.runContextKey(); + this.runLoading.set(true); + this.runError.set(null); + this.report.set(null); + this.reportReusable.set(null); + this.reportContextKey = null; + request$.subscribe({ + next: (response) => { + this.runLoading.set(false); + if (this.runContextKey() !== requestKey) { + this.runError.set('The selection changed while K-means was running. Run the report again.'); + this.cdr.markForCheck(); + return; + } + const status = (response as { status?: unknown } | null)?.status; + if (status === 'error') { + const payload = (response as { result?: any } | null)?.result ?? {}; + this.runError.set( + String(payload?.data ?? payload?.message ?? 'The K-means report failed.'), + ); + this.cdr.markForCheck(); + return; + } + + const reusable = findReusablePreprocessing(response); + this.report.set(findKMeansReport(response) as KMeansResult | null); + this.reportReusable.set(reusable); + this.reportContextKey = requestKey; + if (!reusable) { + this.runError.set('The K-means report did not include reusable cluster centers.'); + } + this.cdr.markForCheck(); + }, + error: () => { + this.runLoading.set(false); + this.runError.set('Could not run the K-means report.'); + this.cdr.markForCheck(); + }, + }); + } + + useReport(): void { + const reusable = this.reportReusable(); + const code = this.code().trim(); + if (!reusable || !code) return; + if (!COLUMN_CODE_PATTERN.test(code)) { + this.runError.set('Use letters, digits and underscores, starting with a letter or underscore.'); + return; + } + this.runError.set(null); + this.store(code, reusable); + } + + parameterValue(field: KMeansParameterField): string { + const edited = this.parameterDraft()[field.key]; + if (edited !== undefined) return edited; + return field.default === undefined || field.default === null ? '' : String(field.default); + } + + setParameter(key: string, value: string | number | null): void { + this.parameterDraft.update((current) => ({ + ...current, + [key]: value === null || value === undefined ? '' : String(value), + })); + } + + setFitK(value: string | number | null): void { + const numeric = Number(value); + this.fitK.set(Number.isFinite(numeric) && numeric >= 1 ? Math.trunc(numeric) : 1); + } + + protected readonly optionBindingValue = optionBindingValue; + + optionLabel(option: unknown): string { + if (option && typeof option === 'object') { + const record = option as Record<string, unknown>; + return String(record['label'] ?? record['name'] ?? record['code'] ?? ''); + } + return String(option); + } + + /** Values the user changed. Unchanged fields stay on the catalog / creator default. */ + private parameterOverrides(fields: KMeansParameterField[]): Record<string, unknown> { + const overrides: Record<string, unknown> = {}; + for (const field of fields) { + const raw = this.parameterValue(field).trim(); + const fallback = field.default === undefined || field.default === null ? '' : String(field.default); + if (raw === fallback || (raw === '' && !field.required)) continue; + if (field.type === 'number') { + const numeric = Number(raw); + if (!Number.isFinite(numeric)) continue; + overrides[field.key] = field.types?.includes('int') ? Math.trunc(numeric) : numeric; + continue; + } + overrides[field.key] = raw; + } + return overrides; + } + + /** Why `code` cannot be written: a name collision, or a later step still reads the old column. */ + private nameProblem(code: string): string | null { + const inUse = this.studio.outputsInUse(this.stepId(), [code]); + return this.columnNameError()?.(this.stepId(), code) + ?? (inUse.length ? `Used by a later step: ${inUse.join(', ')}` : null); + } + + private store(code: string, reusablePreprocessing: KMeansReusablePreprocessing): void { + const nameError = this.nameProblem(code); + if (nameError) { + this.error.set(nameError); + this.runError.set(nameError); + return; + } + this.error.set(null); + this.runError.set(null); + this.selectedUuid.set(null); + // Exaflow names the created column with `new_column_names`; `k` and `enumeration_prefix` + // are derived from the replayable preprocessing, not re-entered here. + this.studio.upsertTransformation({ + id: this.stepId(), + name: KMEANS_CLUSTER_CREATOR, + parameters: { + new_column_names: [code], + k: reusablePreprocessing.cluster_choices.length, + enumeration_prefix: reusablePreprocessing.enumeration_prefix, + reusable_preprocessing: reusablePreprocessing, + }, + }); + this.runRequested.emit(); + } +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.css b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.css new file mode 100644 index 000000000..519fee706 --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.css @@ -0,0 +1,212 @@ +:host { + display: block; +} + +.pca-column-source { + display: grid; + gap: 16px; +} + +.pca-mode-switch { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 2px; + padding: 3px; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: var(--radius-md, 8px); + background: var(--bg-neutral, #f8fafc); +} + +.pca-mode-btn { + min-width: 0; + padding: 7px 10px; + border: 0; + border-radius: calc(var(--radius-md, 8px) - 2px); + background: transparent; + color: var(--text-muted); + font-family: inherit; + font-size: 0.76rem; + font-weight: 600; + cursor: pointer; +} + +.pca-mode-btn:hover { + color: var(--text-main); +} + +.pca-mode-btn.active { + background: var(--card-bg, #ffffff); + color: var(--primary-dark, #1b21a3); + box-shadow: var(--studio-card-shadow, 0 1px 2px rgba(43, 51, 233, 0.06)); +} + +.pca-column-source-field { + display: grid; + gap: 4px; + font-size: 0.78rem; + color: var(--text-main); +} + +.pca-column-source-field select, +.pca-column-source-field input { + box-sizing: border-box; +} + +.pca-column-source-applied { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 8px; + padding: 10px 12px; + border: 1px solid color-mix(in srgb, var(--primary-color) 22%, transparent); + border-radius: var(--radius-md, 8px); + background: color-mix(in srgb, var(--primary-color) 4%, white); + font-size: 0.8rem; +} + +.pca-column-source-applied-code { + font-weight: 700; + color: var(--text-main); +} + +.pca-column-source-applied-detail { + color: var(--text-muted); +} + +.pca-column-source-apply { + display: inline-flex; + align-items: center; + gap: 8px; + justify-self: start; + height: 34px; + padding: 0 18px; + border: 0; + border-radius: 999px; + background: var(--primary-color); + color: #ffffff; + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + cursor: pointer; + box-shadow: 0 1px 2px rgba(43, 51, 233, 0.05); +} + +.pca-column-source-apply:disabled { + opacity: 0.45; + cursor: not-allowed; +} + +.pca-column-source-remove { + margin-left: auto; + padding: 4px 10px; + border: 1px solid #e2e8f0; + border-radius: var(--radius-sm, 4px); + background: transparent; + color: var(--text-muted); + font-family: inherit; + font-size: 0.74rem; + font-weight: 600; + cursor: pointer; +} + +.pca-column-source-error { + margin: 0; + font-size: 0.78rem; + color: var(--danger, #ef4444); +} + +.pca-column-source-note { + margin: 0; + font-size: 0.74rem; + color: var(--text-muted); +} + +.pca-step { + display: grid; + gap: 8px; +} + +.pca-step-head { + display: flex; + align-items: center; + gap: 8px; + font-size: 0.84rem; + font-weight: 700; + color: var(--text-main); +} + +.pca-step-number { + display: inline-flex; + align-items: center; + justify-content: center; + flex: none; + width: 18px; + height: 18px; + border-radius: 50%; + background: var(--primary-color); + color: #ffffff; + font-size: 0.68rem; +} + +.pca-column-source-vars { + display: flex; + flex-wrap: wrap; + gap: 6px; +} + +.pca-column-source-var { + display: inline-flex; + align-items: center; + gap: 6px; + height: 30px; + padding: 0 12px; + border: 0; + border-radius: 999px; + background: var(--card-bg, #ffffff); + box-shadow: inset 0 0 0 1px rgba(43, 51, 233, 0.18); + font-family: inherit; + font-size: 0.78rem; + font-weight: 600; + color: var(--text-main); + cursor: pointer; +} + +.pca-column-source-var i { + font-size: 0.66rem; +} + +.pca-column-source-var-meta { + font-weight: 500; + color: var(--text-muted); + font-variant-numeric: tabular-nums; +} + +.pca-column-source-var.is-selected { + background: color-mix(in srgb, var(--primary-color) 8%, white); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 55%, transparent); + color: var(--primary-dark, #1b21a3); +} + +.pca-column-source-var.is-selected .pca-column-source-var-meta { + color: var(--primary-dark, #1b21a3); +} + +.pca-column-source-var:hover:not(.is-selected) { + background: rgba(43, 51, 233, 0.06); + box-shadow: inset 0 0 0 1px rgba(43, 51, 233, 0.45); +} + +/* In-place PCA report run (same flow as the K-means card). */ +.pca-step-meta { + margin-left: auto; + font-size: 0.74rem; + font-weight: 500; + color: var(--text-muted); +} + +.pca-step-run { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 10px 12px; +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.html b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.html new file mode 100644 index 000000000..585e848b0 --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.html @@ -0,0 +1,184 @@ +<section class="pca-column-source"> + @if (applied(); as creator) { + <div class="pca-column-source-applied"> + <span class="pca-column-source-applied-code">{{ creator.new_column_names.join(', ') }}</span> + <span class="pca-column-source-applied-detail"> + {{ creator.selected_components.join(', ') }} from {{ appliedVariables().join(', ') }} + </span> + <button + type="button" + class="pca-column-source-remove" + [disabled]="!canRemove()" + [title]="removeTooltip()" + (click)="remove()">Remove</button> + </div> + } @else { + <div class="pca-mode-switch" role="group" aria-label="PCA column source"> + <button type="button" class="pca-mode-btn" + [class.active]="sourceMode() === 'result'" + [attr.aria-pressed]="sourceMode() === 'result'" + (click)="setSourceMode('result')"> + From a PCA result + </button> + <button type="button" class="pca-mode-btn" + [class.active]="sourceMode() === 'fit'" + [attr.aria-pressed]="sourceMode() === 'fit'" + (click)="setSourceMode('fit')"> + Fit on selected variables + </button> + </div> + + @if (sourceMode() === 'fit') { + <div class="pca-step"> + <div class="pca-step-head"> + <span class="pca-step-number">1</span>Choose at least two numerical variables + </div> + + @if (fitNumericVariables().length === 0) { + <p class="pca-column-source-note">No numerical variables in this experiment yet.</p> + } @else { + <div class="pca-column-source-vars"> + @for (variable of fitNumericVariables(); track variable.code) { + <button type="button" class="pca-column-source-var" + [class.is-selected]="fitSelectedCodes().includes(variable.code)" + [attr.aria-pressed]="fitSelectedCodes().includes(variable.code)" + (click)="toggleFitVariable(variable.code)"> + <i class="fas" [class.fa-check]="fitSelectedCodes().includes(variable.code)" [class.fa-plus]="!fitSelectedCodes().includes(variable.code)" aria-hidden="true"></i> + {{ variable.label || variable.code }} + </button> + } + </div> + @if (fitSelectedCodes().length > 0 && fitSelectedCodes().length < 2) { + <p class="pca-column-source-note">Select at least two variables.</p> + } + } + </div> + } @else { + @if (loading()) { + <p class="pca-column-source-note">Loading PCA experiments…</p> + } + @if (!loading() && compatible().length === 0) { + <!-- No reusable run: same as the K-means card, run a PCA report here and use its components. --> + <div class="pca-step"> + <div class="pca-step-head"> + <span class="pca-step-number">1</span>Choose at least two numerical variables + @if (recipeFromReport()) { + <span class="pca-step-meta"> + {{ availableComponents().length }} components@if (reportObservations() !== null) { · {{ reportObservations() }} records} + </span> + } + </div> + + @if (numericVariables().length === 0) { + <p class="pca-column-source-note">No numerical variables in this experiment yet.</p> + } @else { + <div class="pca-column-source-vars"> + @for (variable of numericVariables(); track variable.code) { + <button type="button" class="pca-column-source-var" + [class.is-selected]="runSelectedCodes().includes(variable.code)" + [attr.aria-pressed]="runSelectedCodes().includes(variable.code)" + (click)="toggleRunVariable(variable.code)"> + <i class="fas" [class.fa-check]="runSelectedCodes().includes(variable.code)" [class.fa-plus]="!runSelectedCodes().includes(variable.code)" aria-hidden="true"></i> + {{ variable.label || variable.code }} + </button> + } + </div> + } + + <div class="pca-step-run"> + <button type="button" class="pca-column-source-apply" + [disabled]="runLoading() || runSelectedCodes().length < 2" + (click)="runReport()"> + <i class="fas fa-play" aria-hidden="true"></i> + {{ runLoading() ? 'Running PCA…' : 'Run PCA report' }} + </button> + <span class="pca-column-source-note"> + {{ runLoading() ? 'Waiting for every node to answer…' : 'Uses your current datasets and filters.' }} + </span> + </div> + + @if (runError()) { + <p class="pca-column-source-error">{{ runError() }}</p> + } + </div> + } + @if (!loading() && compatible().length > 0) { + <div class="pca-column-source-field"> + <label for="pca-column-source-experiment">PCA experiment</label> + <select + id="pca-column-source-experiment" + [ngModel]="selectedUuid() ?? ''" + (ngModelChange)="onExperimentChange($event)"> + <option value="">Select a finished PCA run…</option> + @for (experiment of compatible(); track experiment.uuid) { + <option [value]="experiment.uuid"> + {{ experiment.name }} · {{ experiment.finished | date: 'mediumDate' }} + </option> + } + </select> + </div> + } + @if (!loading() && hiddenCount() > 0) { + <p class="pca-column-source-note"> + {{ hiddenCount() }} PCA experiment{{ hiddenCount() === 1 ? '' : 's' }} hidden — different data model, datasets or filters. + </p> + } + } + + @if (availableComponents().length) { + <div class="pca-step"> + <div class="pca-step-head"> + <span class="pca-step-number">{{ componentsStepNumber() }}</span>Select components + </div> + <div class="pca-column-source-vars"> + @for (choice of availableComponents(); track choice.component_id) { + <button type="button" class="pca-column-source-var" + [class.is-selected]="selectedComponents().includes(choice.component_id)" + [attr.aria-pressed]="selectedComponents().includes(choice.component_id)" + (click)="toggleComponent(choice.component_id)"> + <i class="fas" [class.fa-check]="selectedComponents().includes(choice.component_id)" [class.fa-plus]="!selectedComponents().includes(choice.component_id)" aria-hidden="true"></i> + {{ choice.component_id }} + @if (choice.explained_variance_ratio !== undefined) { + <span class="pca-column-source-var-meta">{{ varianceLabel(choice.explained_variance_ratio) }}</span> + } + </button> + } + </div> + </div> + + <div class="pca-step"> + <div class="pca-step-head"> + <span class="pca-step-number">{{ componentsStepNumber() + 1 }}</span>Name the new columns + </div> + <div class="pca-column-source-field"> + <label for="pca-column-source-prefix">Name prefix</label> + <input id="pca-column-source-prefix" type="text" + [ngModel]="prefix()" (ngModelChange)="setPrefix($event)" + placeholder="e.g. pca" /> + </div> + @for (componentId of selectedComponents(); track componentId) { + <div class="pca-column-source-field"> + <label [attr.for]="'pca-column-name-' + componentId">{{ componentId }}</label> + <input [id]="'pca-column-name-' + componentId" type="text" + [ngModel]="columnNameFor(componentId)" + (ngModelChange)="setColumnName(componentId, $event)" + [placeholder]="'e.g. ' + prefix() + '_' + componentId.toLowerCase()" /> + </div> + } + </div> + + @if (error()) { + <p class="pca-column-source-error">{{ error() }}</p> + } + + <button type="button" class="pca-column-source-apply" + [disabled]="!canApply() || applying()" + (click)="apply()"> + <i class="fas fa-check" aria-hidden="true"></i> + Add component columns + </button> + } @else if (error()) { + <p class="pca-column-source-error">{{ error() }}</p> + } + } +</section> diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.spec.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.spec.ts new file mode 100644 index 000000000..9fcfef39e --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.spec.ts @@ -0,0 +1,399 @@ +import type { Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { Subject, of } from 'rxjs'; + +import { BackendExperiment } from '../../../../models/backend-experiment.model'; +import { PCAReusablePreprocessing } from '../../../../core/pca-column-source.utils'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../../services/experiments-dashboard.service'; +import { PcaColumnSourceComponent } from './pca-column-source.component'; + +const CONTEXT = { dataModel: 'Stroke:3.7', datasets: ['diabetes'], filters: null }; + +function pcaExperiment(uuid: string, overrides: Record<string, unknown> = {}): BackendExperiment { + return { + uuid, + name: `PCA ${uuid}`, + created: '2024-01-01T00:00:00Z', + finished: '2024-01-01T00:05:00Z', + shared: false, + viewed: false, + status: 'success', + analysis: { + inputdata: { + data_model: CONTEXT.dataModel, + datasets: CONTEXT.datasets, + filters: null, + variables: ['age', 'bmi'], + }, + algorithm: { name: 'pca' }, + }, + ...overrides, + } as BackendExperiment; +} + +function recipe(): PCAReusablePreprocessing { + return { + schema_version: '1', + preprocessing_name: 'pca_column_creator', + base_parameters: { + variables: ['age', 'bmi'], + pca_variant: 'pca', + }, + component_choices: [ + { component_id: 'PC1', explained_variance_ratio: 0.855 }, + { component_id: 'PC2', explained_variance_ratio: 0.145 }, + ], + }; +} + +describe('PcaColumnSourceComponent', () => { + let fixture: ComponentFixture<PcaColumnSourceComponent>; + let component: PcaColumnSourceComponent; + let studio: { + selectedVariables: Mock; + selectedDataModel: Mock; + algorithmAssignableVariables: Mock; + getActiveDataModelCode: Mock; + requestDatasets: Mock; + requestFilters: Mock; + appliedTransformations: ReturnType<typeof signal<any[]>>; + upsertTransformation: Mock; + outputsInUse: Mock; + removeTransformation: Mock; + loadPcaReport: Mock; + }; + let dashboard: { + listPcaExperiments: Mock; + getExperimentResult: Mock; + }; + + beforeEach(async () => { + studio = { + selectedVariables: vi.fn().mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'bmi', label: 'BMI', type: 'real' }, + ]), + selectedDataModel: vi.fn().mockReturnValue({ + variables: [{ code: 'age' }, { code: 'bmi' }, { code: 'chol' }], + groups: [], + }), + algorithmAssignableVariables: vi.fn().mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'bmi', label: 'BMI', type: 'real' }, + ]), + getActiveDataModelCode: vi.fn().mockReturnValue(CONTEXT.dataModel), + requestDatasets: vi.fn().mockReturnValue(CONTEXT.datasets), + requestFilters: vi.fn().mockReturnValue(CONTEXT.filters), + appliedTransformations: signal([] as any[]), + outputsInUse: vi.fn().mockReturnValue([]), + upsertTransformation: vi.fn((step: any) => { + const current = studio.appliedTransformations(); + const index = current.findIndex((existing) => existing.id === step.id); + studio.appliedTransformations.set( + index === -1 ? [...current, step] : current.map((existing, i) => (i === index ? step : existing)), + ); + }), + loadPcaReport: vi.fn().mockReturnValue(of({ status: 'success', result: { n_obs: 289, reusable_preprocessing: recipe() } })), + removeTransformation: vi.fn((id: string) => { + studio.appliedTransformations.set(studio.appliedTransformations().filter((step) => step.id !== id)); + }), + }; + dashboard = { + listPcaExperiments: vi.fn().mockReturnValue(of([])), + getExperimentResult: vi.fn().mockReturnValue(of({})), + }; + + await TestBed.configureTestingModule({ + imports: [PcaColumnSourceComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: studio }, + { provide: ExperimentsDashboardService, useValue: dashboard }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(PcaColumnSourceComponent); + fixture.componentRef.setInput('stepId', 'pca-step-1'); + component = fixture.componentInstance; + }); + + it('applying writes one pca_column_creator step through upsertTransformation', () => { + dashboard.listPcaExperiments.mockReturnValue(of([pcaExperiment('a')])); + dashboard.getExperimentResult.mockReturnValue(of({ result: { reusable_preprocessing: recipe() } })); + fixture.detectChanges(); + + component.onExperimentChange('a'); + component.apply(); + + expect(studio.upsertTransformation).toHaveBeenCalledWith({ + id: 'pca-step-1', + name: 'pca_column_creator', + parameters: { + reusable_preprocessing: recipe(), + selected_components: ['PC1', 'PC2'], + new_column_names: ['pca_pc1', 'pca_pc2'], + n_components: 2, + }, + }); + }); + + it('ignores a result that arrives after another experiment was picked', () => { + const responses: Record<string, Subject<unknown>> = { a: new Subject(), b: new Subject() }; + dashboard.listPcaExperiments.mockReturnValue(of([pcaExperiment('a'), pcaExperiment('b')])); + dashboard.getExperimentResult.mockImplementation((uuid: string) => responses[uuid]); + fixture.detectChanges(); + + component.onExperimentChange('a'); + component.onExperimentChange('b'); + const recipeB = { ...recipe(), component_choices: [{ component_id: 'PC1', explained_variance_ratio: 0.9 }] }; + responses['b'].next({ result: { reusable_preprocessing: recipeB } }); + responses['a'].next({ result: { reusable_preprocessing: recipe() } }); + + expect(component.selectedUuid()).toBe('b'); + expect(component.loadedRecipe()).toEqual(recipeB); + expect(component.applying()).toBe(false); + }); + + it('refuses a PCA result built on created columns', () => { + const onCreated = { ...recipe(), base_parameters: { variables: ['age', 'km_score'], pca_variant: 'pca' } }; + dashboard.listPcaExperiments.mockReturnValue(of([pcaExperiment('a')])); + dashboard.getExperimentResult.mockReturnValue(of({ result: { reusable_preprocessing: onCreated } })); + fixture.detectChanges(); + + component.onExperimentChange('a'); + + expect(component.loadedRecipe()).toBeNull(); + expect(component.error()).toContain('created'); + }); + + it('fit mode offers earlier created columns unselected until the user picks them', () => { + studio.appliedTransformations.set([ + { id: 'pca-step-0', name: 'pca_column_creator', parameters: { new_column_names: ['pca_pc1'] } }, + { id: 'pca-step-1', name: 'pca_column_creator', parameters: {} }, + ]); + studio.algorithmAssignableVariables.mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'bmi', label: 'BMI', type: 'real' }, + { code: 'pca_pc1', label: 'pca_pc1', type: 'real', isCreatedColumn: true }, + ]); + fixture.detectChanges(); + component.setSourceMode('fit'); + + expect(component.fitSelectedCodes()).toEqual(['age', 'bmi']); + + component.toggleFitVariable('pca_pc1'); + expect(component.fitSelectedCodes()).toEqual(['age', 'bmi', 'pca_pc1']); + }); + + it('fit mode applies only the components the current variables still provide', () => { + const self = { id: 'pca-step-1', name: 'pca_column_creator', parameters: {} }; + studio.appliedTransformations.set([ + { id: 'pca-step-0', name: 'pca_column_creator', parameters: { new_column_names: ['pca_pc1'] } }, + self, + ]); + studio.algorithmAssignableVariables.mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'bmi', label: 'BMI', type: 'real' }, + { code: 'pca_pc1', label: 'pca_pc1', type: 'real', isCreatedColumn: true }, + ]); + fixture.detectChanges(); + component.setSourceMode('fit'); + component.toggleFitVariable('pca_pc1'); + component.toggleComponent('PC3'); + expect(component.selectedComponents()).toEqual(['PC1', 'PC2', 'PC3']); + + // The earlier step goes away outside this card, and its column leaves the pool. + studio.appliedTransformations.set([self]); + component.apply(); + + expect(studio.upsertTransformation).toHaveBeenCalledWith(expect.objectContaining({ + parameters: expect.objectContaining({ + variables: ['age', 'bmi'], + selected_components: ['PC1', 'PC2'], + new_column_names: ['pca2_pc1', 'pca2_pc2'], + n_components: 2, + }), + })); + }); + + it('a second card defaults to the prefix pca2', () => { + studio.appliedTransformations.set([ + { + id: 'pca-step-other', + name: 'pca_column_creator', + parameters: { + new_column_names: ['pca_pc1', 'pca_pc2'], + selected_components: ['PC1', 'PC2'], + n_components: 2, + variables: ['age', 'bmi'], + }, + }, + ]); + + const second = TestBed.createComponent(PcaColumnSourceComponent); + second.componentRef.setInput('stepId', 'pca-step-2'); + const other = second.componentInstance; + second.detectChanges(); + + expect(other.prefix()).toBe('pca2'); + }); + + it('inner Remove does not drop a PCA step that has a K-means dependent', () => { + studio.appliedTransformations.set([ + { + id: 'pca-step-1', + name: 'pca_column_creator', + parameters: { + new_column_names: ['pca_pc1', 'pca_pc2'], + selected_components: ['PC1', 'PC2'], + n_components: 2, + variables: ['age', 'bmi'], + }, + }, + { + id: 'kmeans-step', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km'], + reusable_preprocessing: { cluster_variables: ['pca_pc1', 'pca_pc2'] }, + }, + }, + ]); + fixture.componentRef.setInput('canRemove', false); + fixture.componentRef.setInput('removeTooltip', 'Used by km'); + fixture.detectChanges(); + + const removeBtn = fixture.nativeElement.querySelector( + '.pca-column-source-remove', + ) as HTMLButtonElement | null; + expect(removeBtn?.disabled).toBe(true); + expect(removeBtn?.title).toContain('km'); + + removeBtn?.click(); + component.remove(); + + expect(studio.removeTransformation).not.toHaveBeenCalled(); + expect(studio.appliedTransformations().map((step) => step.id)).toEqual([ + 'pca-step-1', + 'kmeans-step', + ]); + }); + + it('re-applying without a column a later step reads is refused', () => { + studio.outputsInUse.mockImplementation((id: string, next: string[]) => + id === 'pca-step-1' && !next.includes('pca_pc2') ? ['pca_pc2'] : [], + ); + fixture.detectChanges(); + component.setSourceMode('fit'); + component.toggleComponent('PC2'); + + component.apply(); + + expect(component.error()).toBe('Used by a later step: pca_pc2'); + expect(studio.upsertTransformation).not.toHaveBeenCalled(); + }); + + describe('without any prior PCA run (like the K-means report)', () => { + it('shows the in-place report instead of a dead end', () => { + fixture.detectChanges(); + const el = fixture.nativeElement as HTMLElement; + + expect(component.sourceMode()).toBe('result'); + expect(el.textContent).toContain('Run PCA report'); + expect(el.textContent).not.toContain('Run PCA first'); + expect(el.querySelector('#pca-column-source-experiment')).toBeNull(); + expect(component.runSelectedCodes()).toEqual(['age', 'bmi']); + }); + + it('runs PCA transiently, then stores the report recipe without a saved experiment', () => { + fixture.detectChanges(); + + component.runReport(); + fixture.detectChanges(); + + expect(studio.loadPcaReport).toHaveBeenCalledWith(['age', 'bmi']); + expect(dashboard.getExperimentResult).not.toHaveBeenCalled(); + expect(component.recipeFromReport()).toBe(true); + expect(component.reportObservations()).toBe(289); + expect(component.availableComponents().map((choice) => choice.component_id)).toEqual(['PC1', 'PC2']); + expect(component.componentsStepNumber()).toBe(2); + expect((fixture.nativeElement as HTMLElement).textContent).toContain('85.5%'); + + component.apply(); + + expect(studio.upsertTransformation).toHaveBeenCalledWith({ + id: 'pca-step-1', + name: 'pca_column_creator', + parameters: { + reusable_preprocessing: recipe(), + selected_components: ['PC1', 'PC2'], + new_column_names: ['pca_pc1', 'pca_pc2'], + n_components: 2, + }, + }); + }); + + it('needs at least two variables before it runs', () => { + fixture.detectChanges(); + component.toggleRunVariable('bmi'); + + component.runReport(); + + expect(component.runSelectedCodes()).toEqual(['age']); + expect(studio.loadPcaReport).not.toHaveBeenCalled(); + }); + + it('shows the backend error of a failed report', () => { + studio.loadPcaReport.mockReturnValue(of({ status: 'error', result: { data: 'Not enough data' } })); + fixture.detectChanges(); + + component.runReport(); + + expect(component.runError()).toBe('Not enough data'); + expect(component.loadedRecipe()).toBeNull(); + }); + + it('drops a report whose variables changed while it ran', () => { + const response$ = new Subject<unknown>(); + studio.loadPcaReport.mockReturnValue(response$); + fixture.detectChanges(); + + component.runReport(); + expect(component.runLoading()).toBe(true); + component.toggleRunVariable('bmi'); + response$.next({ status: 'success', result: { reusable_preprocessing: recipe() } }); + + expect(component.loadedRecipe()).toBeNull(); + expect(component.runError()).toContain('selection changed'); + }); + + it('drops a finished report recipe when the picks change afterwards', () => { + studio.selectedVariables.mockReturnValue([ + { code: 'age', label: 'Age', type: 'real' }, + { code: 'bmi', label: 'BMI', type: 'real' }, + { code: 'chol', label: 'Chol', type: 'real' }, + ]); + fixture.detectChanges(); + component.runReport(); + expect(component.loadedRecipe()).not.toBeNull(); + + component.toggleRunVariable('chol'); + fixture.detectChanges(); + + expect(component.loadedRecipe()).toBeNull(); + expect(component.recipeFromReport()).toBe(false); + expect(component.canApply()).toBe(false); + }); + + it('reports when PCA is not available', () => { + studio.loadPcaReport.mockReturnValue(null); + fixture.detectChanges(); + + component.runReport(); + + expect(component.runError()).toBe('PCA is not available for this workspace.'); + }); + }); +}); diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.ts new file mode 100644 index 000000000..c7c4c9e48 --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/pca-column-source/pca-column-source.component.ts @@ -0,0 +1,531 @@ +import { + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + OnInit, + computed, + effect, + inject, + input, + signal, +} from '@angular/core'; +import { DatePipe } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../../services/experiments-dashboard.service'; +import { BackendExperiment } from '../../../../models/backend-experiment.model'; +import { isOutlierEligibleVariable } from '../../../../core/outlier-rules'; +import { + COLUMN_CODE_PATTERN, + TransformationStep, + fitSelectedCodes, + fitVariablePool, + stepOutputNames, +} from '../../../../core/transformation-steps.utils'; +import { collectVariableCodes } from '../../../../core/variable-availability.utils'; +import { + KMeansSourceContext, + analysisSourceIsReusable, + canonicalFilterKey, +} from '../../../../core/kmeans-cluster-source.utils'; +import { + PCA_COLUMN_CREATOR, + PCAReusablePreprocessing, + buildPcaCreatorParameters, + defaultPcaColumnNames, + findPcaRecipe, + nextPcaColumnPrefix, +} from '../../../../core/pca-column-source.utils'; + +type SourceMode = 'result' | 'fit'; + +type AppliedPcaCreator = { + new_column_names: string[]; + selected_components: string[]; + n_components: number; + reusable_preprocessing?: PCAReusablePreprocessing; + variables?: string[]; +}; + +/** + * Builds component score columns like the K-means card builds cluster columns: run a PCA + * report here (no saved PCA experiment needed) and use its recipe, reuse a finished + * compatible PCA run, or fit PCA directly on selected numerical variables. Each card edits + * one `pca_column_creator` step. + */ +@Component({ + selector: 'app-pca-column-source', + imports: [DatePipe, FormsModule], + templateUrl: './pca-column-source.component.html', + styleUrl: './pca-column-source.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class PcaColumnSourceComponent implements OnInit { + private readonly studio = inject(ExperimentStudioService); + private readonly dashboard = inject(ExperimentsDashboardService); + private readonly cdr = inject(ChangeDetectorRef); + + /** Store step this editor writes; the Transformation stage passes one per card. */ + readonly stepId = input.required<string>(); + + /** + * Optional uniqueness check from the Transformation stage. When a name collides, + * apply must not call upsertTransformation. + */ + readonly columnNameError = input<(stepId: string, name: string) => string | null>(); + + /** False when a later step reads this card's outputs (station Remove uses the same gate). */ + readonly canRemove = input(true); + + /** Tooltip when Remove is disabled because of dependents. */ + readonly removeTooltip = input('Remove'); + + readonly sourceMode = signal<SourceMode>('result'); + readonly experiments = signal<BackendExperiment[]>([]); + readonly loading = signal(false); + readonly error = signal<string | null>(null); + readonly selectedUuid = signal<string | null>(null); + readonly applying = signal(false); + readonly prefix = signal('pca'); + /** Selected component ids, in display order. */ + readonly selectedComponents = signal<string[]>([]); + /** Editable column name per selected component id. */ + readonly columnNamesById = signal<Record<string, string>>({}); + /** Fit-mode: variables the user opted out of (same pattern as K-means). */ + private readonly excludedCodes = signal<ReadonlySet<string>>(new Set()); + /** Recipe loaded for the selected experiment, or from the in-place report (result mode). */ + readonly loadedRecipe = signal<PCAReusablePreprocessing | null>(null); + + /** In-place report run (same flow as the K-means card): pick numerical variables, run + * `pca`, reuse its recipe. Only opt-outs are stored, so the picks follow the variable pool. */ + private readonly runExcludedCodes = signal<ReadonlySet<string>>(new Set()); + readonly runLoading = signal(false); + readonly runError = signal<string | null>(null); + /** Observation count of the in-place report, once it has run. */ + readonly reportObservations = signal<number | null>(null); + /** Context key the in-place report ran on; null when the loaded recipe is not from a report. */ + private reportContextKey: string | null = null; + + readonly numericVariables = computed(() => + (this.studio.selectedVariables() as any[]).filter((variable) => isOutlierEligibleVariable(variable)) + ); + + /** + * Fit-mode pool: numerical CDEs plus numerical outputs of earlier transformation + * steps. The current step's own outputs are excluded. + */ + readonly fitNumericVariables = computed(() => + fitVariablePool(this.studio.appliedTransformations(), this.stepId(), [ + ...((this.studio.algorithmAssignableVariables() as any[]) ?? []), + ...this.numericVariables(), + ]) + ); + + /** + * CDEs start selected and created columns start unselected: mixing raw variables with + * components already built from them is opt-in. A toggle flips the default. + */ + readonly fitSelectedCodes = computed(() => fitSelectedCodes(this.fitNumericVariables(), this.excludedCodes())); + + /** In-place report picks: data-model numerical variables (a recipe must replay on CDEs). */ + readonly runSelectedCodes = computed(() => { + const excluded = this.runExcludedCodes(); + return this.numericVariables() + .map((variable) => String(variable.code)) + .filter((code) => !excluded.has(code)); + }); + + /** Inputs a report was run on; a report from other inputs must not become columns. */ + private readonly runContextKey = computed(() => { + const context = this.currentContext(); + return canonicalFilterKey({ + dataModel: context.dataModel, + datasets: [...context.datasets].map(String).sort(), + filters: context.filters ?? null, + codes: [...this.runSelectedCodes()].sort(), + }); + }); + + /** True when the result-mode recipe came from the in-place report (steps are numbered 1–3). */ + readonly recipeFromReport = signal(false); + + /** Components available in the active mode. */ + readonly availableComponents = computed((): Array<{ component_id: string; explained_variance_ratio?: number }> => { + if (this.sourceMode() === 'result') { + return this.loadedRecipe()?.component_choices ?? []; + } + const n = this.fitSelectedCodes().length; + return Array.from({ length: n }, (_, index) => ({ component_id: `PC${index + 1}` })); + }); + + /** Selected components the active mode still provides (the fit pool can shrink after a pick). */ + readonly validSelectedComponents = computed(() => { + const available = new Set(this.availableComponents().map((choice) => choice.component_id)); + return this.selectedComponents().filter((id) => available.has(id)); + }); + + currentContext(): KMeansSourceContext { + return { + dataModel: this.studio.getActiveDataModelCode() || null, + datasets: this.studio.requestDatasets(), + filters: this.studio.requestFilters(), + }; + } + + readonly compatible = computed(() => { + const context = this.currentContext(); + return this.experiments().filter((experiment) => + analysisSourceIsReusable(experiment, 'pca', context)); + }); + + readonly hiddenCount = computed(() => this.experiments().length - this.compatible().length); + + readonly applied = computed((): AppliedPcaCreator | null => { + const step = this.studio.appliedTransformations().find((candidate) => candidate.id === this.stepId()); + if (!step || step.name !== PCA_COLUMN_CREATOR) return null; + const creator = step.parameters as unknown as AppliedPcaCreator | undefined; + if (!creator || typeof creator !== 'object') return null; + const names = creator.new_column_names; + if (!Array.isArray(names) || !names.length) return null; + if (!Array.isArray(creator.selected_components) || !creator.selected_components.length) return null; + if (typeof creator.n_components !== 'number') return null; + if (creator.reusable_preprocessing) return creator; + if (Array.isArray(creator.variables) && creator.variables.length > 0) return creator; + return null; + }); + + constructor() { + // The card stays mounted, so data model, datasets, filters or picks can change under a + // finished report; drop its recipe rather than offer components fitted on other data. + effect(() => { + const key = this.runContextKey(); + if (this.reportContextKey !== null && key !== this.reportContextKey) { + this.reportContextKey = null; + this.recipeFromReport.set(false); + this.loadedRecipe.set(null); + this.reportObservations.set(null); + this.selectedComponents.set([]); + this.columnNamesById.set({}); + this.runError.set(null); + } + }); + } + + ngOnInit(): void { + this.prefix.set(this.computeDefaultPrefix()); + this.loadExperiments(); + } + + setSourceMode(mode: SourceMode): void { + this.sourceMode.set(mode); + this.error.set(null); + this.runError.set(null); + this.excludedCodes.set(new Set()); + this.runExcludedCodes.set(new Set()); + this.clearReport(); + this.loadedRecipe.set(null); + this.selectedUuid.set(null); + this.selectedComponents.set([]); + this.columnNamesById.set({}); + if (mode === 'fit') { + this.syncFitComponents(); + } + } + + private loadExperiments(): void { + this.loading.set(true); + this.dashboard.listPcaExperiments().subscribe({ + next: (experiments) => { + this.experiments.set(experiments); + this.loading.set(false); + }, + error: () => { + this.error.set('Could not load PCA experiments.'); + this.loading.set(false); + }, + }); + } + + private computeDefaultPrefix(): string { + const used = this.studio.appliedTransformations() + .filter((step) => step.id !== this.stepId() && step.name === PCA_COLUMN_CREATOR) + .flatMap((step) => stepOutputNames(step)); + return nextPcaColumnPrefix(used); + } + + onExperimentChange(uuid: string): void { + this.selectedUuid.set(uuid || null); + this.clearReport(); + this.loadedRecipe.set(null); + this.selectedComponents.set([]); + this.columnNamesById.set({}); + this.error.set(null); + this.applying.set(!!uuid); + if (!uuid) return; + + // A response for a run the user has since moved away from must not load its recipe. + this.dashboard.getExperimentResult(uuid).subscribe({ + next: (response) => { + if (this.selectedUuid() !== uuid) return; + this.applying.set(false); + const recipe = findPcaRecipe(response); + if (!recipe) { + this.error.set('This PCA result cannot be reused. Re-run the PCA experiment and try again.'); + return; + } + // The recipe's variables are sent as data-model variables; a PCA run on created + // columns cannot be replayed here. + const cdes = new Set(collectVariableCodes(this.studio.selectedDataModel())); + if (cdes.size && recipe.base_parameters.variables.some((code) => !cdes.has(code))) { + this.error.set('This PCA used created columns. Re-run it on data model variables to reuse it.'); + return; + } + this.loadedRecipe.set(recipe); + this.preselectComponents(recipe.component_choices.map((choice) => choice.component_id)); + }, + error: () => { + if (this.selectedUuid() !== uuid) return; + this.applying.set(false); + this.error.set('Could not load the PCA result.'); + }, + }); + } + + toggleRunVariable(code: string): void { + this.runExcludedCodes.update((excluded) => { + const next = new Set(excluded); + if (next.has(code)) next.delete(code); + else next.add(code); + return next; + }); + } + + /** Run PCA on the selected variables without saving an experiment, then offer its components. */ + runReport(): void { + const codes = this.runSelectedCodes(); + if (codes.length < 2) return; + const request$ = this.studio.loadPcaReport(codes); + if (!request$) { + this.runError.set('PCA is not available for this workspace.'); + return; + } + + const requestKey = this.runContextKey(); + this.runLoading.set(true); + this.runError.set(null); + this.error.set(null); + this.clearReport(); + this.loadedRecipe.set(null); + this.selectedComponents.set([]); + this.columnNamesById.set({}); + request$.subscribe({ + next: (response) => { + this.runLoading.set(false); + if (this.runContextKey() !== requestKey) { + this.runError.set('The selection changed while PCA was running. Run the report again.'); + this.cdr.markForCheck(); + return; + } + const status = (response as { status?: unknown } | null)?.status; + if (status === 'error') { + const payload = (response as { result?: any } | null)?.result ?? {}; + this.runError.set(String(payload?.data ?? payload?.message ?? 'The PCA report failed.')); + this.cdr.markForCheck(); + return; + } + const recipe = findPcaRecipe(response); + if (!recipe) { + this.runError.set('The PCA report did not include reusable components.'); + this.cdr.markForCheck(); + return; + } + this.reportContextKey = requestKey; + this.recipeFromReport.set(true); + this.reportObservations.set(this.findObservationCount(response)); + this.loadedRecipe.set(recipe); + this.preselectComponents(recipe.component_choices.map((choice) => choice.component_id)); + this.cdr.markForCheck(); + }, + error: () => { + this.runLoading.set(false); + this.runError.set('Could not run the PCA report.'); + this.cdr.markForCheck(); + }, + }); + } + + private clearReport(): void { + this.reportContextKey = null; + this.recipeFromReport.set(false); + this.reportObservations.set(null); + } + + private findObservationCount(response: unknown): number | null { + const outer = (response as { result?: any } | null)?.result; + for (const candidate of [response, outer, outer?.result]) { + const n = (candidate as { n_obs?: unknown } | null | undefined)?.n_obs; + if (typeof n === 'number' && Number.isFinite(n)) return n; + } + return null; + } + + /** Number shown on the Select components step (Name follows it). */ + componentsStepNumber(): number { + if (this.sourceMode() === 'fit' || this.recipeFromReport()) return 2; + return 1; + } + + toggleFitVariable(code: string): void { + this.excludedCodes.update((excluded) => { + const next = new Set(excluded); + if (next.has(code)) next.delete(code); + else next.add(code); + return next; + }); + this.syncFitComponents(); + } + + /** Rebuild fit-mode components (PC1..PCn) and keep a sensible default selection. */ + private syncFitComponents(): void { + const ids = this.availableComponents().map((choice) => choice.component_id); + const previous = new Set(this.selectedComponents()); + const kept = ids.filter((id) => previous.has(id)); + if (kept.length) { + this.setSelectedComponents(kept); + } else { + this.preselectComponents(ids); + } + } + + private preselectComponents(ids: string[]): void { + if (!ids.length) { + this.setSelectedComponents([]); + return; + } + // PC1+PC2 when both exist; PC1 alone otherwise. + const defaults = ids.length >= 2 ? ids.slice(0, 2) : ids.slice(0, 1); + this.setSelectedComponents(defaults); + } + + toggleComponent(componentId: string): void { + const current = this.selectedComponents(); + if (current.includes(componentId)) { + this.setSelectedComponents(current.filter((id) => id !== componentId)); + } else { + const order = this.availableComponents().map((choice) => choice.component_id); + this.setSelectedComponents(order.filter((id) => id === componentId || current.includes(id))); + } + } + + private setSelectedComponents(ids: string[]): void { + this.selectedComponents.set(ids); + const names = defaultPcaColumnNames(this.prefix(), ids); + this.columnNamesById.set(Object.fromEntries(ids.map((id, index) => [id, names[index]]))); + } + + setPrefix(value: string): void { + this.prefix.set(value); + const ids = this.selectedComponents(); + const names = defaultPcaColumnNames(value, ids); + this.columnNamesById.set(Object.fromEntries(ids.map((id, index) => [id, names[index]]))); + } + + setColumnName(componentId: string, value: string): void { + this.columnNamesById.update((current) => ({ ...current, [componentId]: value })); + } + + columnNameFor(componentId: string): string { + return this.columnNamesById()[componentId] ?? ''; + } + + varianceLabel(ratio: number | undefined): string { + if (typeof ratio !== 'number' || !Number.isFinite(ratio)) return ''; + return `${(ratio * 100).toFixed(1)}%`; + } + + apply(): void { + if (this.sourceMode() === 'fit') { + this.applyFit(); + return; + } + this.applyResult(); + } + + private applyResult(): void { + const recipe = this.loadedRecipe(); + const selected = this.validSelectedComponents(); + const names = selected.map((id) => this.columnNameFor(id).trim()); + if (!recipe || !selected.length || names.some((name) => !name)) return; + if (!this.validateNames(names)) return; + this.store(buildPcaCreatorParameters({ recipe, selected, names })); + } + + private applyFit(): void { + const variables = this.fitSelectedCodes(); + const selected = this.validSelectedComponents(); + const names = selected.map((id) => this.columnNameFor(id).trim()); + if (variables.length < 2 || !selected.length || names.some((name) => !name)) return; + if (!this.validateNames(names)) return; + this.store(buildPcaCreatorParameters({ variables, selected, names })); + } + + /** Writes this card's step, unless a later step reads a column the new parameters drop. */ + private store(parameters: Record<string, unknown>): void { + const step = { id: this.stepId(), name: PCA_COLUMN_CREATOR, parameters } as TransformationStep; + const inUse = this.studio.outputsInUse(step.id, stepOutputNames(step)); + if (inUse.length) { + this.error.set(`Used by a later step: ${inUse.join(', ')}`); + return; + } + this.error.set(null); + this.runError.set(null); + this.clearReport(); + this.studio.upsertTransformation(step); + } + + private validateNames(names: string[]): boolean { + for (const name of names) { + if (!COLUMN_CODE_PATTERN.test(name)) { + this.error.set('Use letters, digits and underscores, starting with a letter or underscore.'); + return false; + } + } + if (new Set(names).size !== names.length) { + this.error.set('Column names must be unique.'); + return false; + } + for (const name of names) { + const nameError = this.columnNameError()?.(this.stepId(), name) ?? null; + if (nameError) { + this.error.set(nameError); + return false; + } + } + return true; + } + + remove(): void { + if (!this.canRemove()) return; + this.error.set(null); + this.selectedUuid.set(null); + this.loadedRecipe.set(null); + this.clearReport(); + this.studio.removeTransformation(this.stepId()); + } + + appliedVariables(): string[] { + const creator = this.applied(); + if (!creator) return []; + const fromRecipe = creator.reusable_preprocessing?.base_parameters?.variables; + if (Array.isArray(fromRecipe)) return fromRecipe; + return Array.isArray(creator.variables) ? creator.variables.map(String) : []; + } + + canApply(): boolean { + const selected = this.validSelectedComponents(); + const names = selected.map((id) => this.columnNameFor(id).trim()); + if (!selected.length || names.some((name) => !name)) return false; + if (this.sourceMode() === 'fit') return this.fitSelectedCodes().length >= 2; + return !!this.loadedRecipe(); + } +} 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 43d7f3e84..d747ae42c 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 @@ -1,532 +1,1084 @@ -/* Container */ -.stats-panel-shell { - background: var(--studio-card-bg, rgba(255, 255, 255, 0.92)); - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.45)); - border-radius: 0px; - box-shadow: var(--studio-card-shadow, 0 8px 18px rgba(15, 23, 42, 0.08)); - overflow: hidden; +.workflow-section, +.pipeline-node { + scroll-margin-top: calc( + var(--header-height, 64px) + var(--studio-sub-header-height, 44px) + var(--warning-banner-space, 0px) + 16px + ); } -.modal-container { - position: fixed; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - width: min(960px, 92vw); - max-height: 85vh; - overflow: hidden; - background: #f6fafb; - border-radius: 0px; - box-shadow: - 0 18px 45px rgba(15, 23, 42, 0.25), - 0 1px 3px rgba(15, 23, 42, 0.18); - z-index: 6001; - font-family: "Roboto", sans-serif; - animation: fadeIn 0.25s ease-out both; +.stats-panel-shell { + --panel-success-bg: #dfefe4; + --panel-success-text: #166534; + --panel-warning-text: color-mix(in srgb, var(--primary-color) 70%, var(--accent-color) 30%); + + background: transparent; display: flex; flex-direction: column; + overflow: visible; + font-family: inherit; } .modal-content { display: flex; flex-direction: column; gap: 0; - background: var(--studio-card-bg, #f6fafb); - border-radius: 0px; + background: transparent; + overflow: visible; } -/* Header */ -.modal-header { +.stats-panel-shell i { + transform: translateY(1px); +} + +/* One toolbar row over the results pane: variable identity on the left, the + Table / Charts / Histogram tray and one Export menu on the right. */ +.summary-detail-toolbar { display: flex; + align-items: center; justify-content: space-between; + gap: 12px; + padding: 14px 20px; + border-bottom: 1px solid var(--studio-card-header-border); +} + +.summary-detail-actions { + display: flex; align-items: center; - padding: 12px 16px; - background: #f8fafc; - border-bottom: 1px solid var(--border-color); - border-top-left-radius: 0px; - border-top-right-radius: 0px; + flex-wrap: wrap; + gap: 10px; + flex-shrink: 0; } -.modal-header p { - margin: 0; - font-size: 0.82rem; - font-weight: 800; - text-transform: uppercase; - letter-spacing: 0.08em; +.histogram-split { + display: inline-flex; + align-items: center; + gap: 8px; + min-height: 34px; color: var(--text-muted); + font-size: 0.78rem; + font-weight: 600; + letter-spacing: 0.01em; +} + +.histogram-split select { + appearance: none; + -webkit-appearance: none; + width: auto; + min-height: 34px; + max-width: 180px; + padding: 0 34px 0 14px; + border: 0; + border-radius: 999px; + background-color: var(--card-bg); + background-image: + linear-gradient(45deg, transparent 50%, var(--primary-color) 50%), + linear-gradient(135deg, var(--primary-color) 50%, transparent 50%); + background-position: + calc(100% - 17px) 50%, + calc(100% - 12px) 50%; + background-size: 5px 5px, 5px 5px; + background-repeat: no-repeat; + box-shadow: inset 0 0 0 1px var(--border-color), var(--shadow-sm); + color: var(--text-main); + font: inherit; + font-weight: 600; } -/* Tabs (Variables / Model / Boxplots) */ -.modal-tabs { +.histogram-split select:focus-visible { + outline: 2px solid var(--primary-color); + outline-offset: 2px; +} + +/* Beside the .modal-tabs tray, Export takes the tray's geometry (34px pill, + 1px inset edge, 0.78rem type) and reads like its active pill. */ +.summary-detail-actions .studio-export-btn { + min-height: 34px; + padding: 0 14px; + border: 0; + border-radius: 999px; + background: var(--card-bg); + box-shadow: inset 0 0 0 1px var(--border-color), var(--shadow-sm); + font-size: 0.78rem; + letter-spacing: 0.01em; +} + +.summary-detail-actions .studio-export-btn:hover:not(:disabled), +.summary-detail-actions .studio-export-btn[aria-expanded="true"]:not(:disabled) { + background: color-mix(in srgb, var(--primary-color) 6%, var(--card-bg)); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 35%, transparent), var(--shadow-sm); +} + +.statistics-panel-heading h4 { display: flex; - justify-content: center; + align-items: center; gap: 8px; - padding: 8px 16px; - background: var(--studio-card-header-bg, #eef5f6); - border-bottom: none; - border-radius: 0px; - margin: 0; - border-bottom: 1px solid var(--studio-card-header-border, rgba(226, 232, 240, 0.9)); + margin: 2px 0 0; + font-size: 1rem; } -.modal-tabs button { - border: 1px solid rgba(43, 51, 233, 0.35); +.statistics-panel-code { + padding: 1px 6px; border-radius: var(--radius-sm); - padding: 6px 14px; - background: #ffffff; - color: var(--primary-color); - font-weight: 600; - font-size: 0.78rem; - cursor: pointer; - text-transform: none; - letter-spacing: 0.02em; - transition: - background 0.15s ease-in-out, - color 0.15s ease-in-out, - transform 0.08s ease-in-out, - border-color 0.15s ease-in-out; + background: rgba(43, 51, 233, 0.06); + color: var(--primary-dark); + font-family: var(--font-mono); + font-size: 0.72rem; + font-weight: 500; } -.modal-tabs button.active, -.modal-tabs button:hover { - background: var(--primary-color); - color: #ffffff; - border-color: var(--primary-color); - transform: translateY(-1px); +.summary-export-group { + position: relative; + display: inline-flex; } -/* Body */ -.modal-body { - padding: 12px 14px 14px; - background: var(--studio-card-bg, #f6fafb); - max-height: calc(85vh - 150px); - overflow-y: auto; - border-radius: 0px; - margin: 0; - border-bottom: none; +.summary-export-group .summary-export-caret { + font-size: 0.58rem; + transition: transform 0.18s ease; } -/* Empty state ("No data available.") */ -.modal-body>p { - margin: 18px 0; - text-align: center; - font-size: 0.8rem; - color: #4b5563; - padding: 14px; - background: #ffffff; - border-radius: 0px; - border: 1px dashed var(--studio-card-border, rgba(148, 163, 184, 0.4)); +.summary-export-group .summary-export-caret.open { + transform: rotate(180deg); } -/* Tab description (Model tab explanation) */ -.tab-description { - margin: 0 0 12px 0; - padding: 10px 14px; - font-size: 0.8rem; - font-style: italic; - color: #4b5563; - background: rgba(2, 109, 109, 0.06); - border-left: 3px solid var(--primary-color, #026d6d); - border-radius: 0 4px 4px 0; +.statistics-panel { + padding: 16px 20px 20px; } -/* Expand / Collapse buttons */ -.accordion-controls { +/* ========================================================================= + VERTICAL NODE-PIPELINE CANVAS + ========================================================================= */ + +/* Width is explicit on purpose: `margin: 0 auto` opts this flex item out of the + cross-axis stretch it would otherwise get, so an auto width resolves to + max-content — the widest open section then set the width of every station + card (the summary table made the whole rail ~120px wider than Filtering). + One capped width keeps Filtering, both summaries and Preprocessing on the + same two edges. */ +.pipeline-canvas-shell { display: flex; - justify-content: flex-end; - gap: 6px; - margin: 8px 0 14px 0; - flex-wrap: wrap; + flex-direction: column; + align-items: stretch; + width: min(100%, var(--studio-step-max, 1160px)); + max-width: 100%; + margin: 0 auto; + gap: 0; } -.toolbar-segmented { - display: inline-flex; - align-items: center; - border: 1px solid rgba(2, 109, 109, 0.35); - border-radius: 0px; - overflow: hidden; - background: #ffffff; +/* Every step renders as a solid card; one that adds nothing to the next run says so + in its status chip. A collapsed step (`is-compact`) shows only its overview rail, + so it reads as a compact flowchart at `--studio-pipeline-overview-max`. An open + step re-expands to the full rail width, which is what the QueryBuilder, summary + tables and charts need. `align-self` opts the row out of the shell's cross-axis + stretch. */ +.pipeline-node.is-compact, +.pipeline-terminal-card { + align-self: center; + width: 100%; + max-width: var(--studio-pipeline-overview-max, 860px); } -.toolbar-seg { - border: none; +/* The header titles the whole rail rather than one step, so it is not a card: + one hairline under the title, no frame or shadow, and the stage chips sit on + that line instead of in a badge box. */ +.pipeline-canvas-header { + display: flex; + align-items: flex-end; + justify-content: space-between; + box-sizing: border-box; + width: 100%; + min-height: 0; + margin-bottom: 18px; + padding: 4px 2px 18px; background: transparent; - color: var(--primary-color); - padding: 6px 12px; - cursor: pointer; - font-weight: 600; - font-size: 12.5px; - letter-spacing: 0.2px; - transition: - background 0.15s ease-in-out, - color 0.15s ease-in-out; + border: 0; + border-bottom: 1px solid var(--studio-card-header-border); + border-radius: 0; + box-shadow: none; + gap: 16px; } -.toolbar-seg+.toolbar-seg { - border-left: 1px solid rgba(43, 51, 233, 0.2); +.pipeline-header-title-group h2 { + font-size: 1.15rem; + font-weight: 700; + color: var(--text-main); + margin: 0 0 4px 0; + letter-spacing: 0; } -.toolbar-seg:hover { - background: rgba(2, 109, 109, 0.08); - color: #0f172a; +.pipeline-header-title-group p { + font-size: 0.82rem; + color: var(--text-muted); + margin: 0; + line-height: 1.4; + max-width: 62ch; + text-wrap: pretty; } -.toolbar-icon-btn i { - font-size: 0.95em; +/* Pipeline Node Card */ +.pipeline-node { + background: var(--card-bg, #ffffff); + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-lg, 12px); + box-shadow: var(--studio-card-shadow); + transition: border-color 0.2s ease, box-shadow 0.2s ease, opacity 0.2s ease; + overflow: hidden; } -.toolbar-icon-btn { - padding: 8px 18px; - border-radius: var(--radius-sm); - border: none; - background: var(--primary-color); - color: #ffffff; +/* Step 0 is permanent, so it borrows the compact width of the collapsed rows + rather than the full rail; only its snapshot needs the wide pivot table, and + widening then keeps the card and the table on the same two edges. */ +.pipeline-node.is-readonly { + align-self: center; + width: 100%; + max-width: var(--studio-pipeline-overview-max, 860px); +} + +.pipeline-node.is-readonly.is-active { + align-self: stretch; + max-width: 100%; +} + +/* No body to divide from while the snapshot is hidden. */ +.pipeline-node.is-readonly:not(.is-active) .pipeline-node-header { + border-bottom: none; +} + +.btn-add-step { display: inline-flex; align-items: center; - justify-content: center; - cursor: pointer; - box-shadow: 0 6px 14px rgba(6, 95, 70, 0.25); - gap: 6px; + gap: 8px; + padding: 8px 16px; + background: rgba(255, 255, 255, 0.96); + color: #334155; + border: 1px solid rgba(127, 156, 232, 0.38); + border-radius: 999px; + font-family: inherit; + font-size: 0.8rem; font-weight: 600; - font-size: 12.5px; - letter-spacing: 0.2px; - transition: - transform 0.08s ease-in-out, - box-shadow 0.15s ease-in-out, - filter 0.15s ease-in-out; -} - -.toolbar-icon-btn:hover { - transform: translateY(-1px); - filter: brightness(1.05); - box-shadow: 0 8px 18px rgba(6, 95, 70, 0.35); -} - -/* Accordion cards (per variable) */ -.accordion { - background: var(--studio-card-bg, #ffffff); - border-radius: 0px; - box-shadow: var(--studio-card-shadow, 0 4px 12px rgba(15, 23, 42, 0.08)); - margin-bottom: 12px; - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.45)); - overflow: hidden; - transition: box-shadow 0.3s ease; + line-height: 1.2; + cursor: pointer; + transition: background var(--motion-exit) var(--ease-standard), + border-color var(--motion-exit) var(--ease-standard), + color var(--motion-exit) var(--ease-standard); + white-space: nowrap; } -.accordion:hover { - box-shadow: 0 3px 10px rgba(15, 23, 42, 0.12); +.btn-add-step i { + font-size: 0.76rem; + color: var(--primary-color); } -/* Accordion header */ -.accordion-header { - background: var(--studio-card-header-bg, rgba(241, 245, 249, 0.85)); +.btn-add-step:hover { + background: rgba(43, 51, 233, 0.06); + border-color: rgba(43, 51, 233, 0.45); color: var(--primary-color); - padding: 8px 14px; - border-radius: 0px; - border-bottom: 1px solid var(--studio-card-header-border, rgba(226, 232, 240, 0.9)); + transform: none; + box-shadow: none; +} + +/* Remove: quiet text action, danger ink on hover only. */ +.btn-remove-step-node { + 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); +} + +.pipeline-node-header { display: flex; + align-items: center; justify-content: space-between; + padding: 14px 18px; + background: var(--studio-card-header-bg); + border-bottom: 1px solid rgba(43, 51, 233, 0.08); + gap: 12px; + cursor: pointer; +} + +.pipeline-node-header-left { + display: flex; align-items: center; - font-size: 0.85rem; - font-weight: 600; - user-select: none; + gap: 12px; + min-width: 0; } -.accordion-header p { - margin: 0; +/* The step title is the header's keyboard and screen-reader toggle; it keeps the h3 look. */ +.pipeline-node-toggle { + appearance: none; + padding: 0; + border: 0; + background: none; + font: inherit; + color: inherit; + text-align: left; + cursor: pointer; + border-radius: 4px; } -.accordion-icon { - font-size: 12px; - opacity: 0.75; +/* Fold cue: the whole header toggles its step. */ +.pipeline-node-chevron { + color: var(--text-muted); + font-size: 0.8rem; + transition: transform 0.2s ease, color 0.2s ease; } -/* Smooth expand */ -.accordion-body { - max-height: 0; - overflow: hidden; - opacity: 0; - transform: translateY(-4px); - transition: - max-height 0.4s cubic-bezier(0.25, 0.1, 0.25, 1), - opacity 0.35s ease, - transform 0.35s ease; - background: #ffffff; +/* Outranks `.stats-panel-shell i`, whose 1px nudge it keeps. */ +.pipeline-node-header .pipeline-node-chevron.is-open { + transform: translateY(1px) rotate(180deg); } -.accordion-body.open { - max-height: 1000px; - opacity: 1; - transform: translateY(0); +.pipeline-node-header:hover .pipeline-node-chevron { + color: var(--primary-color); } -/* Table */ -.table-wrapper { - padding: 10px 12px 12px; +.pipeline-node-icon { + width: 38px; + height: 38px; + border-radius: var(--radius-md, 8px); + background: var(--card-bg); + color: var(--primary-color); + display: flex; + align-items: center; + justify-content: center; + font-size: 0.95rem; + flex-shrink: 0; + border: 0; + box-shadow: inset 0 0 0 1px var(--studio-card-border); } -.data-table { - width: 100%; - border-collapse: collapse; - background: #ffffff; - border-radius: 0px; - overflow: hidden; +.pipeline-node-title-group h3 { + font-size: 0.96rem; + font-weight: 700; + color: var(--text-main); + margin: 0; + line-height: 1.3; } -.data-table th { - background: rgba(2, 109, 109, 0.12); - color: #026d6d; - text-align: center; - padding: 8px 12px; - font-size: 0.8rem; +.pipeline-node-subtitle { + font-size: 0.78rem; + color: var(--text-muted); + line-height: 1.35; +} + +.pipeline-node-header-right { + display: flex; + align-items: center; + gap: 10px; + flex-shrink: 0; +} + +.pipeline-status-badge { + display: inline-flex; + align-items: center; + gap: 5px; + font-size: 0.72rem; font-weight: 600; - letter-spacing: 0.2px; + padding: 2px 9px; + border: 0; + border-radius: 999px; + letter-spacing: var(--letter-spacing-config); + white-space: nowrap; } -.data-table td { - text-align: center; - padding: 8px 12px; - font-size: 0.8rem; - color: #374151; - border-bottom: 1px solid rgba(226, 232, 240, 0.9); +/* Not in the request: neutral outline. */ +.pipeline-status-badge.optional { + background: transparent; + color: var(--text-muted); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--text-muted) 25%, transparent); } -.data-table tbody tr:nth-child(odd) { - background-color: rgba(241, 245, 249, 0.5); +/* In the request: primary tint. */ +.pipeline-status-badge.default { + background: color-mix(in srgb, var(--primary-color) 8%, white); + color: var(--primary-color); } -.data-table tbody tr:nth-child(even) { - background-color: #ffffff; +/* Dark inks per DESIGN.md "Text on a state fill". */ +.pipeline-status-badge.applied { + background: color-mix(in srgb, var(--success) 12%, white); + color: #065f46; } -.data-table tbody tr:hover { - background-color: rgba(2, 109, 109, 0.06); - transition: background-color 0.2s ease; +.pipeline-status-badge.pending { + background: color-mix(in srgb, var(--accent-color) 16%, white); + color: #78350f; } -/* Metric column darker*/ -.data-table td:first-child, -.metric-cell { - font-weight: 600; - color: var(--primary-color); - text-align: left; - padding-left: 16px; +/* Read only: plain label, no pill. */ +.pipeline-status-badge.readonly { + padding: 0; + background: transparent; + color: var(--text-muted); } -/* Boxplots section */ -.boxplots-section { +.pipeline-status-badge.readonly i { + font-size: 0.66rem; +} + +.pipeline-node-body { + padding: 0; +} + +/* Sub-node rail: compact connected tree of active sub-steps, shown while the + stage header is collapsed. Rail line + milestone dot echo the pipeline spine. */ +/* Collapsed stage contents are inset (~64px a side on the 860px compact card, which + lines their edge up under the stage title), so they read as children of the stage + while the rows keep room for title, subtitle, impact and status on one line. */ +.pipeline-subnodes-tree { + box-sizing: border-box; + max-width: 85%; + margin: 0 auto; + padding: 12px 18px 14px; +} + +.pipeline-subnode-item { + position: relative; + display: flex; + gap: 10px; +} + +.pipeline-subnode-rail { + position: relative; + flex-shrink: 0; + width: 14px; + display: none; +} + +/* Continuous line through the rail column; dots punch through it. */ +.pipeline-subnode-rail::before { + content: ''; + position: absolute; + top: 0; + bottom: 0; + width: 2px; + background: color-mix(in srgb, var(--primary-color) 18%, #cbd5e1); +} + +.pipeline-subnode-item:first-child .pipeline-subnode-rail::before { + top: 12px; +} + +.pipeline-subnode-item:last-child .pipeline-subnode-rail::before { + bottom: calc(100% - 12px); +} + +.pipeline-subnode-dot { + position: relative; + z-index: 1; + width: 8px; + height: 8px; + margin-top: 8px; + border-radius: 50%; + background: var(--card-bg, #ffffff); + border: 2px solid color-mix(in srgb, var(--primary-color) 45%, #cbd5e1); +} + +.pipeline-subnode-content { + flex: 1; + min-width: 0; display: flex; - flex-direction: column; align-items: center; - gap: 0.9rem; + gap: 10px; + padding: 10px 12px; + margin-bottom: 6px; + background: var(--card-bg, #ffffff); + border: 1px solid var(--border-color, rgba(43, 51, 233, 0.12)); + border-radius: var(--radius-md, 8px); + font-family: inherit; + text-align: left; + cursor: pointer; + transition: border-color 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease; +} + +.pipeline-subnode-content:hover { + border-color: color-mix(in srgb, var(--primary-color) 45%, transparent); + background: color-mix(in srgb, var(--primary-color) 2%, white); + box-shadow: 0 2px 8px rgba(43, 51, 233, 0.08); +} + +.pipeline-subnode-content > i { + color: var(--primary-color); + font-size: 0.82rem; + width: 16px; + text-align: center; + flex-shrink: 0; } -/* Distributions section (Box Plots + Pie Charts) */ -.distributions-section { +.pipeline-subnode-text { display: flex; flex-direction: column; - gap: 1.5rem; + gap: 1px; + min-width: 0; +} + +.pipeline-subnode-title { + font-size: 0.8rem; + font-weight: 650; + color: var(--text-main); + line-height: 1.3; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.pipeline-subnode-subtitle { + font-size: 0.72rem; + color: var(--text-muted); + line-height: 1.35; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.pipeline-subnode-status { + margin-left: auto; + flex-shrink: 0; + font-size: 0.68rem; + font-weight: 650; + padding: 2px 8px; + border-radius: 999px; + letter-spacing: 0.02em; + white-space: nowrap; +} + +.pipeline-subnode-impact { + margin-left: auto; + font-size: 0.74rem; + color: var(--text-muted); + white-space: nowrap; + font-variant-numeric: tabular-nums; +} + +.pipeline-subnode-impact + .pipeline-subnode-status { + margin-left: 12px; +} + +.pipeline-subnode-status[data-tone='applied'] { + background: color-mix(in srgb, var(--success) 12%, white); + color: #065f46; + border: 0; +} + +.pipeline-subnode-status[data-tone='default'] { + background: color-mix(in srgb, var(--primary-color) 8%, white); + color: var(--primary-color); + border: 0; +} + +.pipeline-subnode-status[data-tone='pending'] { + background: color-mix(in srgb, var(--accent-color) 16%, white); + color: #78350f; + border: 0; +} + +/* Dashed rail row: a sub-step the stage can still create. It rides the same spine as + the live rows, so "add" reads as the next node in the flow instead of a button glued + under the card - and it stays reachable while the stage is collapsed. */ +.pipeline-subnode-item.is-ghost .pipeline-subnode-content { + background: transparent; + border-color: color-mix(in srgb, var(--primary-color) 25%, transparent); + border-style: dashed; + box-shadow: none; +} + +.pipeline-subnode-item.is-ghost .pipeline-subnode-content:hover { + background: color-mix(in srgb, var(--primary-color) 7%, white); + border-color: var(--primary-color); + border-style: solid; +} + +.pipeline-subnode-item.is-ghost .pipeline-subnode-dot { + border-style: dashed; } -.distribution-group { +.pipeline-subnode-item.is-ghost .pipeline-subnode-title { + color: var(--primary-color); +} + +/* Beats \`.pipeline-subnode-content > i\`, which sizes the leading glyph. */ +.pipeline-subnode-content > .pipeline-subnode-add { + margin-left: auto; + width: auto; + font-size: 0.7rem; +} + +/* Vertical Directional Connectors */ +.pipeline-connector { + height: 32px; display: flex; flex-direction: column; align-items: center; - gap: 0.75rem; - padding: 12px; - background: var(--studio-card-bg, #ffffff); - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.45)); - border-radius: 4px; + justify-content: center; + position: relative; + margin: 0; } -.distribution-title { - margin: 0; - font-size: 0.85rem; - font-weight: 600; - color: var(--primary-color, #026d6d); - text-transform: uppercase; - letter-spacing: 0.05em; +.pipeline-connector-line { + position: absolute; + top: 0; + bottom: 0; + width: 2px; + background: color-mix(in srgb, var(--primary-color) 18%, #cbd5e1); + z-index: 1; } -/* Distribution sheet tabs (Numeric / Nominal) */ -.distribution-sheet-tabs { +.pipeline-connector-line.is-active { + background: var(--primary-color); +} + +.pipeline-connector-arrow { + display: none; + width: 20px; + height: 20px; + border-radius: 50%; + background: #ffffff; + border: 2px solid color-mix(in srgb, var(--primary-color) 25%, #cbd5e1); + color: var(--primary-color); display: flex; + align-items: center; justify-content: center; - gap: 8px; - margin-bottom: 12px; + font-size: 0.65rem; + z-index: 2; + box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05); } -.distribution-sheet-tabs button { - border: 1px solid var(--primary-color, #026d6d); - border-radius: var(--radius-sm, 4px); - padding: 8px 18px; - background: #ffffff; - color: var(--primary-color, #026d6d); - font-weight: 600; - font-size: 0.82rem; +/* 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.15s ease-in-out, - color 0.15s ease-in-out, - transform 0.08s ease-in-out; + transition: background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease; } -.distribution-sheet-tabs button.active, -.distribution-sheet-tabs button:hover { - background: var(--primary-color, #026d6d); - color: #ffffff; - transform: translateY(-1px); +.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); } -.distribution-sheet { - display: flex; - flex-direction: column; +.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; + grid-template-columns: minmax(0, 1fr) auto; align-items: center; - gap: 0.75rem; - padding: 12px; - background: var(--studio-card-bg, #ffffff); - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.45)); - border-radius: 4px; + padding: 16px 20px; + background: var(--studio-card-header-bg); + border: 1px solid var(--border-color, rgba(43, 51, 233, 0.15)); + border-radius: var(--radius-lg, 12px); + box-shadow: none; + gap: 20px; + margin-top: 4px; } -/* mini-tabs */ -.mini-tabs { +.pipeline-terminal-summary { display: flex; + flex-direction: column; gap: 8px; + min-width: 0; +} + +.pipeline-terminal-kicker { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; +} + +.pipeline-terminal-facts { + display: flex; flex-wrap: wrap; - justify-content: center; + align-items: baseline; + gap: 6px 22px; + font-size: 0.84rem; + color: var(--text-muted); } -.mini-tabs button { - border-radius: var(--radius-sm); - border: 1px solid rgba(2, 109, 109, 0.25); - background: #ffffff; - padding: 6px 12px; - color: #026d6d; +.pipeline-terminal-facts strong { + font-size: 1.05rem; + font-weight: 700; + color: var(--text-main); +} + +.btn-finish-pipeline { + display: inline-flex; + align-items: center; + gap: 8px; + padding: 10px 22px; + background: var(--primary-color); + color: #ffffff; + border: none; + border-radius: 999px; + font-family: inherit; + font-size: 0.86rem; + font-weight: 600; cursor: pointer; + box-shadow: var(--shadow-sm); + transition: all 0.2s ease; + white-space: nowrap; +} + +/* Unapplied-changes guard */ +.unapplied-warning-card { + grid-column: 1 / -1; + flex: 1 1 100%; + display: flex; + align-items: center; + gap: 14px; + padding: 12px 16px; + background: color-mix(in srgb, #f59e0b 8%, white); + border: 1px solid color-mix(in srgb, #f59e0b 30%, transparent); + border-radius: var(--radius-md, 8px); +} + +.unapplied-warning-icon { + color: #78350f; + font-size: 1.1rem; + flex-shrink: 0; +} + +.unapplied-warning-text h4 { + font-size: 0.88rem; + font-weight: 700; + color: var(--text-main); + margin: 0 0 2px; +} + +.unapplied-warning-text p { font-size: 0.78rem; - transition: - background 0.15s ease-in-out, - color 0.15s ease-in-out, - border-color 0.15s ease-in-out, - transform 0.08s ease-in-out; + color: var(--text-muted); + margin: 0; } -.mini-tabs button.active, -.mini-tabs button:hover { - background: linear-gradient(135deg, var(--primary-color), var(--primary-light)); - color: #ffffff; - border-color: var(--primary-color); - transform: translateY(-1px); +.unapplied-warning-actions { + margin-left: auto; + display: flex; + gap: 8px; + align-items: center; } -.chart-container { - width: 90%; - max-width: 700px; - min-height: 360px; - background: var(--studio-card-bg, #ffffff); - border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.45)); - border-radius: 0px; - padding: 10px 12px; - box-shadow: var(--studio-card-shadow, 0 8px 18px rgba(15, 23, 42, 0.08)); +/* Secondary and ghost siblings of the primary pill: same shape, no fill. The frame is + an inset shadow so the box stays exactly as tall as .btn-finish-pipeline (border: none). */ +.unapplied-warning-btn.secondary { + background: var(--card-bg); + color: var(--primary-color); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 35%, transparent); } -.toolbar-btn i { - font-size: 0.95em; +.unapplied-warning-btn.ghost { + background: transparent; + color: var(--text-muted); + box-shadow: none; } -/* PDF export helpers */ -.hidden-charts-for-export { - position: fixed; - top: -9999px; - left: -9999px; - width: 900px; - height: auto; - opacity: 1; - pointer-events: none; - background: #fff; +.unapplied-warning-btn.secondary:hover, +.unapplied-warning-btn.ghost:hover { + transform: none; } -.hidden-charts-for-export app-chart-renderer { - width: 800px; - height: 500px; - display: block; +.unapplied-warning-btn.secondary:hover { + background: color-mix(in srgb, var(--primary-color) 8%, white); } -body.pdf-exporting, -.modal-container.pdf-exporting { - cursor: wait !important; - pointer-events: none; - user-select: none; +.unapplied-warning-btn.ghost:hover { + background: transparent; + color: var(--text-main); } -.pdf-overlay { - position: absolute; - inset: 0; - background: rgba(255, 255, 255, 0.8); +.btn-finish-pipeline:hover { + background: var(--primary-dark, #1b21a3); + transform: none; + box-shadow: var(--shadow-md); +} + +@media (max-width: 768px) { + .pipeline-canvas-header { + flex-wrap: wrap; + } + .pipeline-node-header { + flex-direction: column; + align-items: flex-start; + } + .pipeline-node-header-right { + width: 100%; + justify-content: space-between; + } + .pipeline-terminal-card { + grid-template-columns: minmax(0, 1fr); + align-items: stretch; + } + .btn-finish-pipeline { + justify-content: center; + } +} + + +.workflow-section { + background: transparent; + border: none; + border-radius: 0; + box-shadow: none; + overflow: visible; + transition: background-color 0.25s ease; + margin-bottom: 0; +} + +.workflow-section-body { + display: none; + padding: 14px 0 16px; +} + +/* Inside a pipeline node the station card draws its own frame; without an inset + it sat flush on the node border and the two 1px lines vibrated as one. */ +.pipeline-node .workflow-section-body { + padding-inline: 16px; +} + +.pipeline-node.is-readonly .workflow-section-body { + padding: 0; +} + +/* Summary workspaces nested inside a pipeline node inherit the node's frame; + strip the inner card chrome so only one border is visible. */ +.pipeline-node .workflow-section .statistics-workspace { + border: none; + border-radius: 0; + box-shadow: none; +} + +/* Stage header extras: the step kicker above the title and the "Records out" figure + before the status badge. */ +.pipeline-node-kicker { + display: block; + font-size: 0.66rem; + font-weight: 700; + letter-spacing: 0.06em; + text-transform: uppercase; + color: var(--text-muted); + line-height: 1.4; +} + +.pipeline-records-out { display: flex; flex-direction: column; - justify-content: center; - align-items: center; - z-index: 999; - font-size: 1.2rem; - color: #333; + align-items: flex-end; + line-height: 1.25; + padding-right: 4px; + font-variant-numeric: tabular-nums; +} + +.pipeline-records-out-label { + font-size: 0.64rem; + font-weight: 700; + letter-spacing: 0.06em; + text-transform: uppercase; + color: var(--text-muted); +} + +.pipeline-records-out-value { + font-size: 0.9rem; + font-weight: 700; + color: var(--text-main); +} + +.pipeline-records-out-value span { + margin-left: 0.25em; font-weight: 500; + color: var(--text-muted); } -/* Animation */ -@keyframes fadeIn { - from { - opacity: 0; - transform: translate(-50%, -48%) scale(0.98); - } +.pipeline-subnodes-caption { + margin: 0 0 8px; + font-size: 0.74rem; + color: var(--text-muted); +} - to { - opacity: 1; - transform: translate(-50%, -50%) scale(1); - } +/* "Edit ›" / "Add +" cue at the end of a rail row. */ +.pipeline-subnode-cta { + display: inline-flex; + align-items: center; + gap: 6px; + margin-left: 8px; + font-size: 0.74rem; + font-weight: 600; + color: var(--primary-color); + white-space: nowrap; } -/* Responsiveness */ +.pipeline-subnode-text + .pipeline-subnode-cta { + margin-left: auto; +} -@media (max-width: 768px) { - .modal-container { - width: 96vw; - max-height: 90vh; - border-radius: 0px; - } +.pipeline-subnode-content > .pipeline-subnode-cta i { + font-size: 0.62rem; +} - .modal-content { - border-radius: 0px; - } +/* "How to use" strip directly under an expanded stage header; a <details> folded by + default, so only the summary line shows until someone asks for the steps. */ +.how-to-strip { + background: color-mix(in srgb, var(--primary-color) 4%, white); + border-bottom: 1px solid rgba(43, 51, 233, 0.08); +} - .modal-header { - padding: 10px 14px; - } +.how-to-summary { + display: flex; + align-items: center; + gap: 10px; + padding: 8px 18px; + list-style: none; + cursor: pointer; +} - .modal-header p { - font-size: 16px; - } +.how-to-summary::-webkit-details-marker { + display: none; +} - .modal-body { - padding: 12px 12px 8px; - max-height: calc(90vh - 140px); - } +.how-to-summary:hover .how-to-peek { + color: var(--primary-color); +} - .accordion-header { - padding: 8px 10px; - font-size: 13px; +.how-to-summary:focus-visible { + outline: 2px solid var(--primary-color); + outline-offset: -2px; +} + +.how-to-label { + font-size: 0.66rem; + font-weight: 700; + letter-spacing: 0.06em; + text-transform: uppercase; + color: var(--primary-color); +} + +.how-to-peek { + font-size: 0.74rem; + color: var(--text-muted); +} + +.how-to-chevron { + font-size: 0.6rem; + color: var(--primary-color); + transition: transform 0.15s ease; +} + +.how-to-strip[open] .how-to-chevron { + transform: rotate(180deg); +} + +.how-to-strip[open] .how-to-peek { + display: none; +} + +.how-to-body { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px 14px; + padding: 0 18px 10px; +} + +.how-to-step { + display: inline-flex; + align-items: center; + gap: 7px; + font-size: 0.78rem; + color: #334155; +} + +.how-to-number { + display: inline-flex; + align-items: center; + justify-content: center; + flex: none; + width: 18px; + height: 18px; + border-radius: 50%; + background: var(--primary-color); + color: #fff; + font-size: 0.68rem; + font-weight: 700; +} + +.how-to-note { + flex-basis: 100%; + font-size: 0.76rem; + color: var(--text-muted); +} + +/* Projected into a station card, the strip spans the card body edge to edge. + `app-station-card` is the host this template owns, so emulated styles reach it. */ +app-station-card .how-to-strip { + margin: -14px -14px 14px; +} + +/* Live match chip under the cohort builder, filled by Preview. */ +.filter-match-row { + display: flex; + justify-content: flex-end; + padding: 14px 14px 10px; +} + +.filter-match-chip { + display: inline-flex; + align-items: center; + gap: 8px; + padding: 4px 12px; + border-radius: 999px; + font-size: 0.78rem; + font-weight: 600; + color: #065f46; + background: color-mix(in srgb, var(--success) 12%, white); +} + +@media (max-width: 768px) { + app-station-card .how-to-strip { + margin: -14px -12px 14px; } +} - .data-table th, - .data-table td { - padding: 6px 8px; - font-size: 12px; +@media (max-width: 720px) { + .pipeline-records-out { + display: none; } +} - .toolbar-seg { - padding: 6px 12px; - font-size: 13px; +@media (max-width: 720px) { + .pipeline-subnodes-tree { + max-width: none; } -} \ No newline at end of file +} 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 04de2c8b4..514c9563c 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 @@ -1,170 +1,1904 @@ -<div class="stats-panel-shell"> - <div class="modal-header"> - <p>Descriptive statistics</p> - </div> - +<div class="stats-panel-shell" data-guide="analysis-section"> <div class="modal-content"> - <!-- Tabs --> - <div class="modal-tabs"> - <button [class.active]="activeTab === 'Variables'" (click)="setTab('Variables')"> - Variables - </button> - <button [class.active]="activeTab === 'Model'" (click)="setTab('Model')"> - Model - </button> - @if (showBoxPlots) { - <button [class.active]="activeTab === 'Distributions'" (click)="setTab('Distributions')"> - Distributions - </button> - } - </div> - - <div class="modal-body"> - @if (isLoading) { - <app-spinner text="Descriptive statistics Loading" [overlay]="false" /> - } - @if (!isLoading) { - <!-- VARIABLES / MODEL TAB --> - @if (activeTab === 'Variables' || activeTab === 'Model') { - <section class="pivot-section"> - @if ((activeTab === 'Variables' ? processedData : modelData).length > 0) { - @if (activeTab === 'Model') { - <p class="tab-description">Descriptive statistics for complete dataset only, based on the variables and - covariates - you selected.</p> - } - <div class="accordion-controls"> - <div class="toolbar-segmented"> - <button class="toolbar-seg" (click)="expandAll()">Expand All</button> - <button class="toolbar-seg" (click)="collapseAll()">Collapse All</button> - </div> - <button class="toolbar-icon-btn" (click)="exportAllDescriptiveToPDF()" title="Export PDF"> - <i class="fa fa-file-pdf-o"></i> - Export - </button> + <div class="pipeline-canvas-shell" data-guide="analysis-progress-rail"> + <header class="pipeline-canvas-header"> + <div class="pipeline-header-title-group"> + <h2>Data Handling Pipeline</h2> + <p>Get your records ready for analysis. Stages run top to bottom — Raw data is a read-only preview, Preprocessing always runs, and Filtering and Transformations are optional.</p> </div> + </header> - @for (v of (activeTab === 'Variables' ? processedData : modelData); track v; let i = $index) { - <div class="accordion pivot-card"> - <div class="accordion-header" (click)="toggleAccordion(v.name)"> - <p>{{ v.name }}</p> - <span class="accordion-icon" [class.open]="isAccordionOpen(v.name)"> - ▾ + <!-- Shared summary workspace: all three Data Handling surfaces (step-0 snapshot, Raw + preview, Processed preview) render one template - variable browser, statistics, + charts, histograms, export - from their own SummaryView. The outlet passes the + data plus the two things that differ per surface: the exit button's copy and the + empty-state line. Anchors come from summaryGuideAnchor, so only the surfaces the + tour and QA actually target wear them. --> + <ng-template #summaryWorkspace let-kind="kind" let-summary="summary" let-emptyHint="emptyHint"> + @if (kind === 'processed' && cohortRetention; as retention) { + <div class="cohort-retention-strip" role="status"> + <i class="fas fa-users" aria-hidden="true"></i> + <span> + Cohort retention: {{ retention.processed.toLocaleString() }} / {{ retention.raw.toLocaleString() }} records + @if (retention.dropped > 0) { + ({{ retention.dropped.toLocaleString() }} rows removed by preprocessing) + } + · {{ retention.percentage.toFixed(1) }}% retained </span> </div> + } + + @if (kind !== 'source') { + <div class="station-preview-bar"> + <button + type="button" + class="station-preview-toggle station-preview-close" + title="Close the preview" + (click)="toggleSummaryWorkspace(kind)"> + <i class="fas fa-times" aria-hidden="true"></i> + <span>Close</span> + </button> + </div> + } - <div class="accordion-body fade" [class.open]="isAccordionOpen(v.name)"> - <div class="table-wrapper"> - <table class="data-table pivot-table"> - <thead> - <tr> - <th>Metric</th> - @for (ds of v.columns; track ds) { - <th>{{ ds }}</th> + @if (summary.isLoading) { + <div class="federated-info-note chart-note"> + <i class="fa fa-info-circle"></i> + <span><strong>{{ summaryScopeRunLabel() }}.</strong> The summary is calculated on every node and waits for the slowest one.</span> + </div> + <div class="studio-skeleton-card" role="status" [attr.aria-label]="summaryKindLabel(kind) + ' summary loading'"> + <span class="studio-skeleton-line medium"></span> + <span class="studio-skeleton-line short"></span> + <div class="studio-skeleton-table" aria-hidden="true"> + @for (_ of [1, 2, 3, 4]; track _) { + <span class="studio-skeleton-cell head"></span> + } + @for (_ of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; track _) { + <span class="studio-skeleton-cell"></span> + } + </div> + </div> + } + + @if (!summary.isLoading) { + <section class="summary-workspace"> + @if (summary.data.length > 0) { + @if (filteredStatisticBlocks(kind).length > 0) { + <section class="statistics-workspace"> + <aside class="statistics-browser" [attr.aria-label]="summaryKindLabel(kind) + ' summary variables'"> + <div class="statistics-browser-header"> + <span>Variables</span> + <small>{{ filteredStatisticBlocks(kind).length }} shown · missing</small> + </div> + <div class="search-inline chart-search"> + <i class="fas fa-search search-inline-icon" aria-hidden="true"></i> + <input class="preprocessing-search" type="search" placeholder="Search variables..." [attr.aria-label]="'Search ' + summaryKindLabel(kind).toLowerCase() + ' summary variables'" [(ngModel)]="statisticsSearch[kind]" /> + </div> + + @if (statisticBlocks(kind, 'numeric').length > 0) { + <div class="statistics-variable-group"> + <div class="statistics-variable-group-title">Numerical <span>{{ statisticBlocks(kind, 'numeric').length }}</span></div> + @for (block of statisticBlocks(kind, 'numeric'); track block.code ?? block.name) { + <button type="button" class="statistics-variable-btn" [class.active]="isStatisticBlockSelected(kind, block)" (click)="selectStatisticBlock(kind, block)"> + <span>{{ block.name }}</span> + <span class="statistics-variable-meta"> + @if (blockCoverage(block); as coverage) { + <small class="statistics-variable-coverage" [attr.data-severity]="coverageSeverity(coverage)" [title]="coverageTitle(coverage)">{{ coveragePercent(coverage) }}</small> + } + </span> + @if (blockCoverage(block); as coverage) { + <span class="statistics-variable-bar" [attr.data-severity]="coverageSeverity(coverage)" aria-hidden="true"> + <span [style.width.%]="barWidth(coverage.share, 1000)"></span> + </span> } - </tr> - </thead> - <tbody> - @for (r of v.rows; track r) { - <tr> - <td class="metric-cell">{{ r.metric }}</td> - @for (ds of v.columns; track ds) { - <td>{{ r.values[ds] }}</td> + </button> + } + </div> + } + + @if (statisticBlocks(kind, 'nominal').length > 0) { + <div class="statistics-variable-group"> + <div class="statistics-variable-group-title">Categorical <span>{{ statisticBlocks(kind, 'nominal').length }}</span></div> + @for (block of statisticBlocks(kind, 'nominal'); track block.code ?? block.name) { + <button type="button" class="statistics-variable-btn" [class.active]="isStatisticBlockSelected(kind, block)" (click)="selectStatisticBlock(kind, block)"> + <span>{{ block.name }}</span> + <span class="statistics-variable-meta"> + @if (blockCoverage(block); as coverage) { + <small class="statistics-variable-coverage" [attr.data-severity]="coverageSeverity(coverage)" [title]="coverageTitle(coverage)">{{ coveragePercent(coverage) }}</small> + } + </span> + @if (blockCoverage(block); as coverage) { + <span class="statistics-variable-bar" [attr.data-severity]="coverageSeverity(coverage)" aria-hidden="true"> + <span [style.width.%]="barWidth(coverage.share, 1000)"></span> + </span> + } + </button> + } + </div> + } + @if (selectionCoverage(kind); as pooled) { + <div class="statistics-browser-foot"> + <span class="statistics-browser-foot-label">All shown variables</span> + <div class="statistics-browser-foot-value"> + <strong [attr.data-severity]="coverageSeverity(pooled)">{{ coveragePercent(pooled) }}</strong> + <small>{{ coverageTitle(pooled) }}</small> + </div> + </div> + } + </aside> + + <div class="summary-detail-column"> + @if (selectedStatisticBlock(kind); as selectedBlock) { + <div class="summary-detail-toolbar"> + <div class="statistics-panel-heading"> + <span class="statistics-panel-kicker">{{ statisticBlockTypeLabel(kind, selectedBlock) }} variable</span> + <h4>{{ selectedBlock.name }} @if (selectedBlock.code) {<code class="statistics-panel-code">{{ selectedBlock.code }}</code>}</h4> + </div> + <div class="summary-detail-actions"> + <div class="summary-tabs modal-tabs" role="group" [attr.aria-label]="summaryKindLabel(kind) + ' summary overlays'" [attr.data-guide]="summaryGuideAnchor(kind, 'charts')"> + <button + type="button" + [class.active]="summary.activeTab === 'Statistics'" + [attr.aria-pressed]="summary.activeTab === 'Statistics'" + (click)="setSummaryTab(kind, 'Statistics')"> + <i class="fas fa-table" aria-hidden="true"></i> + Table + </button> + <button + type="button" + [class.active]="summary.activeTab === 'Charts'" + [attr.aria-pressed]="summary.activeTab === 'Charts'" + (click)="setSummaryTab(kind, 'Charts')"> + <i class="fas fa-chart-simple" aria-hidden="true"></i> + Charts + </button> + <button + type="button" + [class.active]="summary.activeTab === 'Histogram'" + [attr.aria-pressed]="summary.activeTab === 'Histogram'" + (click)="setSummaryTab(kind, 'Histogram')"> + <i class="fas fa-chart-column" aria-hidden="true"></i> + Histogram + </button> + </div> + @if (summary.activeTab === 'Histogram' && canSplitSummaryHistogram(kind, selectedBlock)) { + <label class="histogram-split"> + <span>Split by</span> + <select + [value]="histogramSplitValue(kind, selectedBlock)" + (change)="onHistogramSplitChange(kind, selectedBlock, $event)" + aria-label="Split histogram by category"> + <option value="">None</option> + @for (option of histogramSplitOptions(selectedBlock); track option.code) { + <option [value]="option.code" [disabled]="option.disabled" [title]="option.title">{{ option.label }}</option> + } + </select> + </label> } - </tr> + <div class="summary-export-group"> + <button + type="button" + class="studio-export-btn" + [attr.data-guide]="summaryGuideAnchor(kind, 'export')" + [attr.aria-expanded]="summaryExportMenuKind() === kind" + aria-haspopup="menu" + title="Export summary" + (click)="toggleSummaryExportMenu(kind)"> + <i class="fa fa-download" aria-hidden="true"></i> + <span>Export</span> + <i class="fa fa-chevron-down summary-export-caret" [class.open]="summaryExportMenuKind() === kind" aria-hidden="true"></i> + </button> + @if (summaryExportMenuKind() === kind) { + <div class="studio-export-menu" role="menu" aria-label="Export summary"> + <button type="button" class="studio-export-menu-item" role="menuitem" (click)="exportAllDescriptiveToPDF(kind)"> + <i class="fa fa-file-pdf" aria-hidden="true"></i> + <span>PDF</span> + </button> + @if (summary.activeTab === 'Statistics') { + <button type="button" class="studio-export-menu-item" role="menuitem" (click)="exportSummaryToCSV(kind)"> + <i class="fa fa-file-csv" aria-hidden="true"></i> + <span>CSV</span> + </button> + } + </div> + } + </div> + </div> + </div> + + <div class="statistics-panel" [attr.data-guide]="summaryGuideAnchor(kind, 'details')"> + @if (summary.activeTab === 'Statistics') { + <div class="table-wrapper statistics-table-wrapper" [class.statistics-table-wrapper--numeric]="selectedSummaryChartType(kind, selectedBlock) === 'numeric'"> + <table class="data-table pivot-table statistics-table"> + <thead> + <tr> + <th>Metric</th> + @for (ds of selectedBlock.columns; track ds) { + <th [class.statistics-col-rollup]="isRollupColumn(ds)"> + {{ datasetLabel(ds) }} + </th> + } + </tr> + </thead> + <tbody> + @for (r of selectedBlock.rows; track r.metric; let ri = $index) { + @if (metricGroupLabel(selectedBlock.rows, ri); as groupLabel) { + <tr class="statistics-metric-group"> + <td [attr.colspan]="selectedBlock.columns.length + 1">{{ groupLabel }}</td> + </tr> + } + <tr> + <td class="metric-cell">{{ r.metric }}</td> + @for (ds of selectedBlock.columns; track ds) { + <td [class.statistics-col-rollup]="isRollupColumn(ds)">{{ displayNumber(r.values[ds]) }}@if (missingShareNote(selectedBlock, r, ds); as share) {<span class="statistics-cell-note">{{ share }}</span>} + </td> + } + </tr> + } + </tbody> + </table> + </div> } - </tbody> - </table> + + @if (summary.activeTab === 'Charts') { + @if (selectedSummaryChartOptions(kind, selectedBlock).length > 0) { + <div class="chart-container chart-container--workspace fade"> + <app-chart-renderer [charts]="selectedSummaryChartOptions(kind, selectedBlock)"></app-chart-renderer> + </div> + } @else { + <div class="empty-state-block compact-empty chart-empty-panel"> + <i class="fa fa-bar-chart"></i> + <p>No chart available for {{ selectedBlock.name }}.</p> + <span>Select another variable or review its statistics.</span> + </div> + } + } + + @if (summary.activeTab === 'Histogram') { + @if (summaryHistogramLoading(kind, selectedBlock)) { + <!-- A numerical histogram is not in the summary payload, so this is a live + federated run that waits on the slowest node. Two shimmer lines inside a + pane that reserves 420px for a chart read as a stalled surface, so the + wait says what it is doing and borrows the Chart skeleton from + Datasets & Variables. --> + <div class="federated-info-note chart-note"> + <i class="fa fa-info-circle"></i> + <span><strong>{{ summaryHistogramRunLabel(selectedBlock) }}.</strong> Numerical histograms are not part of the summary payload, so the first run for each variable is computed live and waits for the slowest node.</span> + </div> + <div class="studio-skeleton-card studio-skeleton-card--chart" role="status" [attr.aria-label]="summaryKindLabel(kind) + ' histogram loading'"> + <span class="studio-skeleton-line medium"></span> + <div class="studio-skeleton-chart" aria-hidden="true"> + <span class="studio-skeleton-bar" style="height: 42%"></span> + <span class="studio-skeleton-bar" style="height: 68%"></span> + <span class="studio-skeleton-bar" style="height: 56%"></span> + <span class="studio-skeleton-bar" style="height: 84%"></span> + <span class="studio-skeleton-bar" style="height: 48%"></span> + <span class="studio-skeleton-bar" style="height: 64%"></span> + </div> + </div> + } @else if (summaryHistogramError(kind, selectedBlock)) { + <div class="row-error">{{ summaryHistogramError(kind, selectedBlock) }}</div> + } @else if (selectedSummaryHistogramData(kind, selectedBlock)) { + <div class="chart-container chart-container--workspace fade"> + <app-histogram [data]="selectedSummaryHistogramData(kind, selectedBlock)" [config]="{ color: '#2b33e9', clipNullEdges: false }"></app-histogram> + </div> + } @else { + <div class="empty-state-block compact-empty chart-empty-panel"> + <i class="fa fa-bar-chart"></i> + <p>No histogram available for {{ selectedBlock.name }}.</p> + <span>Select another variable or review its statistics.</span> + </div> + } + } + </div> + } + </div> + </section> + } @else { + <div class="empty-state-block compact-empty"> + <i class="fa fa-search"></i> + <p>No variables match the current search.</p> + <span>Clear the search field to show all statistics.</span> </div> + } + } + @else { + <div class="empty-state-block"> + <i class="fa fa-table"></i> + <p>No data available.</p> + <span>{{ emptyHint }}</span> + </div> + } + </section> + } + </ng-template> + + <!-- "How to use" strip under an expanded stage header: numbered steps plus one note. + Folded by default so it never pushes the editor down; the summary line opens it. --> + <ng-template #howToStrip let-guide> + <details class="how-to-strip"> + <summary class="how-to-summary"> + <span class="how-to-label">How to use</span> + <span class="how-to-peek">{{ guide.steps.length }} steps</span> + <i class="fas fa-chevron-down how-to-chevron" aria-hidden="true"></i> + </summary> + <div class="how-to-body"> + @for (step of guide.steps; track step; let i = $index) { + <span class="how-to-step"><span class="how-to-number">{{ i + 1 }}</span>{{ step }}</span> + } + <span class="how-to-note">{{ guide.note }}</span> </div> - </div> - } - } - @else { - <p>No data available.</p> + </details> + </ng-template> + + <!-- One preprocessing card's own footer: Discard · Preview processed data · Apply/Close, + all scoped to that card's pending edits. --> + <ng-template #prepFooter let-step> + @if (preprocessingApplyError[step]) { + <div class="glass-feedback glass-feedback-warning" role="alert">{{ preprocessingApplyError[step] }}</div> } + <app-station-action-bar + flush + [pendingCount]="stepPendingCount(step)" + [statusText]="stepPendingCount(step) ? 'change not applied yet — the run still uses your last applied rules.' : ''" + [tone]="stepPendingCount(step) ? 'pending' : 'applied'" + resetLabel="Discard" + previewLabel="Preview processed data" + [applyLabel]="stepApplyLabel(step)" + [applyIcon]="stepPendingCount(step) ? 'fas fa-check' : 'fas fa-times'" + [applyVariant]="stepPendingCount(step) ? 'primary' : 'quiet'" + [resetDisabled]="!stepPendingCount(step) || isApplyingPreprocessing" + [applyDisabled]="isApplyingPreprocessing" + (reset)="resetChanges(step)" + (preview)="previewProcessedData(step)" + (apply)="commitOrCloseStep(step)" + ></app-station-action-bar> + </ng-template> + + <!-- One derived column's category counts: shown inside its card in place of the editor. --> + <ng-template #transformationStats let-code> + <div class="transformation-statistics"> + @if (isTransformationStatsLoading) { + <div class="studio-skeleton-line"></div> + <div class="studio-skeleton-line short"></div> + } @else if (transformationStatisticsError && transformationStatsFor(code).length === 0) { + <div class="glass-feedback glass-feedback-warning"> + {{ transformationStatisticsError }} + </div> + } @else if (transformationStatsFor(code).length === 0) { + <div class="empty-state-block compact-empty"> + <i class="fa fa-bar-chart"></i> + <p>No counts for this column yet.</p> + <span>Run the report and name the column, then preview again.</span> + </div> + } @else { + @if (transformationStatisticsError) { + <div class="glass-feedback glass-feedback-warning"> + {{ transformationStatisticsError }} + </div> + } @else if (showsRuleColumnCounts()) { + <div class="glass-feedback"> + Category counts from a describe run with your applied transformation. + </div> + } + @for (block of transformationStatsFor(code); track block.code) { + @if (transformationCountChart(block); as chart) { + <div class="transformation-count-chart"> + <app-histogram [data]="chart" [config]="{ color: '#2b33e9', orientation: 'horizontal' }"></app-histogram> + </div> + } + @if (kmeansColumnView(block.code); as column) { + <div class="transformation-stats-block transformation-create"> + <h4 class="transformation-stats-title">Nominal column · {{ column.code }}</h4> + <p class="transformation-stats-lead">{{ column.lead }}</p> + <div class="transformation-rules"> + <div class="transformation-rule-head"> + <span></span> + <span>Value</span> + <span>Rule</span> + <span>Records</span> + <span></span> + </div> + <div class="transformation-rules-list"> + @for (row of block.rows; track row.value; let rowIndex = $index) { + <div class="transformation-rule-row"> + <span class="transformation-rule-number">{{ rowIndex + 1 }}</span> + <span class="transformation-rule-value transformation-rule-value--fixed">{{ row.value }}</span> + <span class="transformation-rule-summary">{{ column.rules[row.value] || 'Nearest cluster' }}</span> + <span class="transformation-rule-record-count">{{ row.count === null ? '—' : row.count }}</span> + <span></span> + </div> + } + </div> + <p class="transformation-rules-foot">{{ column.foot }}</p> + </div> + </div> + } @else { + <div class="transformation-stats-block"> + <h4 class="transformation-stats-heading"><code>{{ block.code }}</code></h4> + <table class="transformation-stats-table"> + <thead> + <tr><th>Category</th><th>Count</th></tr> + </thead> + <tbody> + @for (row of block.rows; track row.value) { + <tr> + <td>{{ row.value }}</td> + <td>{{ row.count === null ? '—' : row.count }}</td> + </tr> + } + </tbody> + </table> + </div> + } + } + } + </div> + </ng-template> + + <!-- NODE 0: SOURCE DATA - read-only snapshot, never part of the request --> + <section + class="pipeline-node is-readonly" + [class.is-active]="sectionOpen().source" + data-guide="analysis-source-data" + > + <header class="pipeline-node-header" (click)="onStepHeaderClick('source', $event)"> + <div class="pipeline-node-header-left"> + <div class="pipeline-node-icon"><i class="fas fa-database" aria-hidden="true"></i></div> + <div class="pipeline-node-title-group"> +<span class="pipeline-node-kicker">Step 1</span> + <h3><button type="button" class="pipeline-node-toggle" [attr.aria-expanded]="isStepOpen('source')" (click)="toggleStep('source')">Raw data</button></h3> + <span class="pipeline-node-subtitle">Look at your selected variables exactly as stored. Nothing is changed here.</span> + </div> + </div> + <div class="pipeline-node-header-right"> + @if (recordsOut.source !== null) { + <span class="pipeline-records-out"><span class="pipeline-records-out-label">Records out</span><span class="pipeline-records-out-value">{{ recordsOut.source!.toLocaleString() }}</span></span> + } + <span class="pipeline-status-badge readonly"><i class="fas fa-lock" aria-hidden="true"></i> Read only</span> + <i class="fas fa-chevron-down pipeline-node-chevron" [class.is-open]="isStepOpen('source')" aria-hidden="true"></i> + </div> + </header> + <div class="pipeline-node-body"> + <!-- STEP 0 PREVIEW: the shared summary workspace, fed by an unfiltered describe --> + <div #sourceSection class="workflow-section" data-guide="analysis-source-summary"> + <div class="workflow-section-body" [class.open]="sectionOpen().source"> + <!-- Rendered only while open: a hidden second workspace would double every count-based DOM query + and hand the tour or a shared toggle the wrong surface. --> + @if (sectionOpen().source) { + <ng-container [ngTemplateOutlet]="howToStrip" [ngTemplateOutletContext]="{ $implicit: howTo.source }"></ng-container> + <ng-template + [ngTemplateOutlet]="summaryWorkspace" + [ngTemplateOutletContext]="{ kind: 'source', summary: sourceSummary, emptyHint: 'Select variables and datasets to load the data summary.' }"></ng-template> + } + </div> + </div> + </div> </section> - } - <!-- DISTRIBUTIONS TAB (Two sheets: Numeric + Nominal) --> - @if (activeTab === 'Distributions' && showBoxPlots) { - <section class="distributions-section"> - <div class="accordion-controls no-margin-top"> - <button class="toolbar-icon-btn" (click)="exportAllDescriptiveToPDF()" title="Export PDF"> - <i class="fa fa-file-pdf-o"></i> - Export - </button> + <!-- CONNECTOR 0 -> 1 --> + <div class="pipeline-connector" aria-hidden="true"> + <div class="pipeline-connector-line" [class.is-active]="sectionOpen().source"></div> + <div class="pipeline-connector-arrow"><i class="fas fa-chevron-down"></i></div> + </div> + + <!-- NODE 1: COHORT FILTERING --> + <section + #filtersSection + class="pipeline-node filter-workflow-section" + [class.is-compact]="!sectionOpen().filters && !sectionOpen().raw" + data-guide="analysis-filtering" + > + <header class="pipeline-node-header" (click)="onStepHeaderClick('filters', $event)"> + <div class="pipeline-node-header-left"> + <div class="pipeline-node-icon"><i class="fas fa-filter" aria-hidden="true"></i></div> + <div class="pipeline-node-title-group"> +<span class="pipeline-node-kicker">Step 2 · Optional</span> + <h3><button type="button" class="pipeline-node-toggle" [attr.aria-expanded]="isStepOpen('filters')" (click)="toggleStep('filters')">Cohort filtering</button></h3> + <span class="pipeline-node-subtitle">Keep only the patient records you want — for example, age 60 and over.</span> + </div> + </div> + <div class="pipeline-node-header-right"> + @if (filterRuleCount() > 0 && recordsOut.filtered !== null) { + <span class="pipeline-records-out"><span class="pipeline-records-out-label">Records out</span><span class="pipeline-records-out-value">{{ recordsOut.filtered!.toLocaleString() }}@if (recordsOut.source !== null) { <span>of {{ recordsOut.source!.toLocaleString() }}</span>}</span></span> + } + <span class="pipeline-status-badge" [class.applied]="filterRuleCount() > 0" [class.optional]="filterRuleCount() === 0"> + {{ filteringStatusLabel }} + </span> + @if (isStepAdded('filters')) { + <button + type="button" + class="btn-remove-step-node" + title="Remove cohort filtering from pipeline" + (click)="removeStep('filters')"> + <i class="fas fa-trash-can" aria-hidden="true"></i> + <span>Remove Step</span> + </button> + } + <i class="fas fa-chevron-down pipeline-node-chevron" [class.is-open]="isStepOpen('filters')" aria-hidden="true"></i> + </div> + </header> + + <!-- Collapsed overview: the stored rules, each a way back into the builder. --> + @if (!sectionOpen().filters && !sectionOpen().raw) { + <div class="pipeline-subnodes-tree"> + @if (cohortRuleRows.length) { + <p class="pipeline-subnodes-caption">Records are kept only if they match {{ cohortMatchesAny ? 'any of these rules' : 'these rules' }}. Click a rule to edit it.</p> + } + <div role="list" aria-label="Cohort filtering rules"> + @for (row of cohortRuleRows; track $index) { + <div class="pipeline-subnode-item" role="listitem"> + <span class="pipeline-subnode-rail" aria-hidden="true"><span class="pipeline-subnode-dot"></span></span> + <button type="button" class="pipeline-subnode-content" (click)="goToSection('filters')" title="Edit this rule"> + <i class="fas fa-filter" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">{{ row.title }}</span> + <span class="pipeline-subnode-subtitle">{{ row.subtitle }}</span> + </span> + <span class="pipeline-subnode-cta">Edit <i class="fas fa-chevron-right" aria-hidden="true"></i></span> + </button> + </div> + } + <div class="pipeline-subnode-item is-ghost" role="listitem"> + <span class="pipeline-subnode-rail" aria-hidden="true"><span class="pipeline-subnode-dot"></span></span> + <button type="button" class="pipeline-subnode-content" (click)="goToSection('filters')" title="Open the rule builder"> + <i class="fas fa-plus" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">Add condition</span> + <span class="pipeline-subnode-subtitle">Narrow the cohort further</span> + </span> + <i class="fas fa-plus pipeline-subnode-add" aria-hidden="true"></i> + </button> + </div> + </div> </div> - <!-- Sub-tabs to switch between Numeric and Nominal sheets (only if both exist) --> - @if (nonNominalVariables.length > 0 && nominalVariables.length > 0) { - <div class="distribution-sheet-tabs"> - <button [class.active]="distributionSubTab === 'Numeric'" (click)="distributionSubTab = 'Numeric'"> - Numeric Variables - </button> - <button [class.active]="distributionSubTab === 'Nominal'" (click)="distributionSubTab = 'Nominal'"> - Nominal Variables - </button> + } + + @if (isStepAdded('filters')) { + <div class="pipeline-node-body"> + <!-- Outside the padded body so the strip spans the node right under its header. --> + @if (sectionOpen().filters) { + <ng-container [ngTemplateOutlet]="howToStrip" [ngTemplateOutletContext]="{ $implicit: howTo.filters }"></ng-container> + } + <div class="workflow-section-body" [class.open]="sectionOpen().filters"> + <app-station-card> + <!-- Apply saves and folds the editor. Preview data opens the Raw tables. --> + <app-filter-config-modal + #filterModal + [filterLogic]="expStudioService.filterLogic()" + (filtersApplied)="onCohortFiltersApplied()" + ></app-filter-config-modal> + @if (filterPreviewStatus) { + <div class="filter-match-row"> + <span class="filter-match-chip"><i class="fas fa-users" aria-hidden="true"></i>{{ filterPreviewStatus }}</span> + </div> + } + <app-station-action-bar + stationFooter + flush + resetLabel="Clear" + previewLabel="Preview matching records" + statusText="Rules take effect only when applied." + [applyLabel]="filterModal.activeRulesCount() > 0 ? 'Apply filters' : 'Close'" + [applyIcon]="filterModal.activeRulesCount() > 0 ? 'fas fa-check' : 'fas fa-times'" + [applyVariant]="filterModal.activeRulesCount() > 0 ? 'primary' : 'quiet'" + (reset)="clearFilterRules(filterModal)" + (preview)="previewFilterData(filterModal)" + (apply)="commitOrCloseFilterStation(filterModal)" + ></app-station-action-bar> + </app-station-card> + </div> + <div #rawSection class="workflow-section" data-guide="analysis-raw-summary"> + <div class="workflow-section-body" [class.open]="sectionOpen().raw"> + <ng-template + [ngTemplateOutlet]="summaryWorkspace" + [ngTemplateOutletContext]="{ kind: 'raw', summary: rawSummary, emptyHint: 'Select variables and datasets to load the data summary.' }"></ng-template> + </div> + </div> </div> } + </section> + + <!-- CONNECTOR 1 -> 2 --> + <div class="pipeline-connector" aria-hidden="true"> + <div class="pipeline-connector-line" [class.is-active]="isStepAdded('filters')"></div> + <div class="pipeline-connector-arrow"><i class="fas fa-chevron-down"></i></div> + </div> - <!-- Numeric Variables Sheet (Box Plots) --> - @if (distributionSubTab === 'Numeric' && nonNominalVariables.length > 0) { - <div class="distribution-sheet"> - @if (nonNominalVariables.length > 1) { - <div class="mini-tabs"> - @for (v of nonNominalVariables; track v; let i = $index) { - <button [class.active]="activeBoxPlotIndex === i" (click)="activeBoxPlotIndex = i"> - {{ v?.name || v?.label || v?.code }} + <!-- NODE 2: PREPROCESSING --> + <!-- MIP drops rows with missing values whether or not anyone configured it, so this + stage is part of the request from the first render and its rail tags the + untouched handler Default. --> + <section + #setupSection + class="pipeline-node preprocessing-section" + [class.is-active]="isStepAdded('setup')" + [class.is-compact]="!sectionOpen().setup && !sectionOpen().processed" + data-guide="analysis-preprocessing" + > + <header class="pipeline-node-header" (click)="onStepHeaderClick('setup', $event)"> + <div class="pipeline-node-header-left"> + <div class="pipeline-node-icon"><i class="fas fa-sliders" aria-hidden="true"></i></div> + <div class="pipeline-node-title-group"> +<span class="pipeline-node-kicker">Step 3 · Always on</span> + <h3><button type="button" class="pipeline-node-toggle" [attr.aria-expanded]="isStepOpen('setup')" (click)="toggleStep('setup')">Preprocessing</button></h3> + <span class="pipeline-node-subtitle">Decide what happens to missing values, and optionally cap outliers.</span> + </div> + </div> + <div class="pipeline-node-header-right"> + @if (recordsOut.processed !== null) { + <span class="pipeline-records-out"><span class="pipeline-records-out-label">Records out</span><span class="pipeline-records-out-value">{{ recordsOut.processed!.toLocaleString() }}@if (recordsOut.filtered !== null) { <span>of {{ recordsOut.filtered!.toLocaleString() }}</span>}</span></span> + } + <span + class="pipeline-status-badge" + [class.applied]="preprocessingBadgeTone === 'applied'" + [class.pending]="preprocessingBadgeTone === 'pending'" + [class.default]="preprocessingBadgeTone === 'default'" + > + {{ preprocessingStatusLabel }} + </span> + @if (isStepAdded('setup')) { + <button + type="button" + class="btn-remove-step-node" + title="Remove customization and revert to default NaN removal" + (click)="removeStep('setup')"> + <i class="fas fa-rotate-left" aria-hidden="true"></i> + <span>Revert to Default (Drop NaN)</span> + </button> + } @else { + <button type="button" class="btn-add-step" (click)="addStep('setup')"> + <i class="fas fa-sliders" aria-hidden="true"></i> + <span>Customize rules</span> </button> } + <i class="fas fa-chevron-down pipeline-node-chevron" [class.is-open]="isStepOpen('setup')" aria-hidden="true"></i> + </div> + </header> + + <!-- Collapsed overview: the rail of every sub-step the pipeline will run, followed + by the dashed rows that create the ones it does not hold yet. --> + @if (!sectionOpen().setup && !sectionOpen().processed) { + <div class="pipeline-subnodes-tree" role="list" aria-label="Preprocessing steps"> + <p class="pipeline-subnodes-caption">Everything below runs in this order. Click a step to edit it; dashed rows add optional steps.</p> + @for (node of appliedPreprocessingSubNodes; track node.id) { + <div class="pipeline-subnode-item" role="listitem"> + <span class="pipeline-subnode-rail" aria-hidden="true"><span class="pipeline-subnode-dot"></span></span> + <button type="button" class="pipeline-subnode-content" (click)="jumpToSubNode('setup', node.id)" title="Open this preprocessing stage"> + <i [class]="node.icon" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">{{ node.title }}</span> + <span class="pipeline-subnode-subtitle">{{ node.subtitle }}</span> + </span> + @if (node.impact) { + <span class="pipeline-subnode-impact">{{ node.impact }}</span> + } + <span class="pipeline-subnode-status" [attr.data-tone]="node.statusTone">{{ node.statusLabel }}</span> + <span class="pipeline-subnode-cta">Edit <i class="fas fa-chevron-right" aria-hidden="true"></i></span> + </button> + </div> + } + <!-- Dashed rail rows: a sub-step the stage can still create, so a stage grows + without being expanded first. Each row is the whole add flow and the station + it opens is already narrowed to the matching sub-step. A row disappears only + once the rail itself holds the step, never because an editor was opened. --> + @if (!outlierSubNodeApplied) { + <div class="pipeline-subnode-item is-ghost" role="listitem"> + <span class="pipeline-subnode-rail" aria-hidden="true"><span class="pipeline-subnode-dot"></span></span> + <button type="button" class="pipeline-subnode-content" (click)="addPreprocessingSubNode('outlier')" title="Add and open this step"> + <i class="fas fa-line-chart" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">Add outlier clipping</span> + <span class="pipeline-subnode-subtitle">Cap extreme values so a few outliers don’t skew results — numerical variables only</span> + </span> + <i class="fas fa-plus pipeline-subnode-add" aria-hidden="true"></i> + </button> + </div> + } + @if (isLongitudinalModel && !expStudioService.appliedPreprocessingConfig()?.['longitudinal_transformer']) { + <div class="pipeline-subnode-item is-ghost" role="listitem"> + <span class="pipeline-subnode-rail" aria-hidden="true"><span class="pipeline-subnode-dot"></span></span> + <button type="button" class="pipeline-subnode-content" (click)="addPreprocessingSubNode('longitudinal')" title="Add and open this step"> + <i class="fas fa-code-compare" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">Add longitudinal comparison</span> + <span class="pipeline-subnode-subtitle">Compare two visits per variable (required for two-visit pathologies)</span> + </span> + <i class="fas fa-plus pipeline-subnode-add" aria-hidden="true"></i> + </button> </div> } - <div class="chart-container fade"> - <app-chart-renderer [charts]="chartsForBoxPlot[activeBoxPlotIndex] || []"></app-chart-renderer> + </div> + } + + <!-- The editor exists only once the stage is opened: while the request still holds + the untouched default, the rail above is the whole card. --> + @if (isStepAdded('setup')) { + <div class="pipeline-node-body"> + @if (sectionOpen().setup) { + <ng-container [ngTemplateOutlet]="howToStrip" [ngTemplateOutletContext]="{ $implicit: howTo.setup }"></ng-container> + } + <div class="workflow-section-body" [class.open]="sectionOpen().setup"> + <div class="preprocessing-shell"> + <app-station-card + collapsible + icon="fas fa-eraser" + title="Missing Values" + description="Choose what happens to missing values, one variable at a time." + [statusLabel]="missingPreprocessingStatusLabel" + [status]="statusTone(missingPreprocessingStatusLabel, hasPendingMissingChanges)" + [open]="preprocessingStepOpen.missing" + (openChange)="togglePreprocessingStep('missing')"> + @if (preprocessingStepDocumentation('missing_values_handler')) { + <button stationAction type="button" class="preprocessing-doc-toggle" + [attr.aria-expanded]="preprocessingDocOpen().missing" + (click)="togglePreprocessingDoc('missing')"> + <i class="fas fa-book-open" aria-hidden="true"></i> + <span>How this works</span> + </button> + } + @if (preprocessingStepDocumentation('missing_values_handler') && preprocessingDocOpen().missing) { + <div class="preprocessing-documentation-panel is-open"> + <div class="preprocessing-documentation-content" [innerHTML]="formatPreprocessingDocumentationHtml('missing_values_handler')"></div> + </div> + } + + @if (preprocessingVariables.length > 0) { + <section class="statistics-workspace preprocessing-workspace"> + <aside class="statistics-browser preprocessing-browser" aria-label="Preprocessing variables"> + <div class="statistics-browser-header"> + <span>Variables · action</span> + <small>{{ filteredPrepVariables('missing').length }} shown</small> + <div class="batch-menu"> + <button + type="button" + class="studio-export-btn batch-menu-trigger" + [attr.aria-expanded]="batchMenuOpen() === 'missing'" + (click)="toggleBatchMenu('missing')"> + <i class="fas fa-bolt" aria-hidden="true"></i> + Set all + </button> + @if (batchMenuOpen() === 'missing') { + <div class="batch-menu-list" role="menu"> + <button type="button" role="menuitem" (click)="applyBatchMissingStrategy('mean')">Set all numerical to Mean imputation</button> + <button type="button" role="menuitem" (click)="applyBatchMissingStrategy('median')">Set all numerical to Median imputation</button> + <button type="button" role="menuitem" (click)="applyBatchMissingStrategy('drop')">Reset all to Remove rows (Drop NaN)</button> + </div> + } + </div> + </div> + <div class="search-inline chart-search"> + <i class="fas fa-search search-inline-icon" aria-hidden="true"></i> + <input id="preprocessing-search" class="preprocessing-search" type="search" placeholder="Search variables..." aria-label="Search preprocessing variables" [(ngModel)]="prepSearch.missing" /> + </div> + + @for (group of prepGroups('missing'); track group.key) { + <div class="statistics-variable-group preprocessing-variable-group" [attr.data-state]="group.key"> + <div class="statistics-variable-group-title">{{ group.title }} <span>{{ group.variables.length }}</span></div> + @for (variable of group.variables; track variable.code) { + <button type="button" class="statistics-variable-btn preprocessing-variable-btn" [class.active]="isPrepVariableSelected('missing', variable)" (click)="selectPrepVariable('missing', variable)"> + <span>{{ variableLabel(variable) }}</span> + <span class="statistics-variable-meta"> + @if (coverageFor(variable); as coverage) { + <small class="preprocessing-variable-coverage" [attr.data-severity]="coverageSeverity(coverage)" [title]="coverageTitle(coverage)">{{ coveragePercent(coverage) }}</small> + } + <span + class="preprocessing-state-mini" + [class.state-pending]="prepVariableHasPendingChange('missing', variable)" + [class.state-applied]="prepVariableStateLabel('missing', variable) === 'Applied'" + [class.state-default]="prepVariableStateLabel('missing', variable) === 'Default'" + > + {{ prepVariableStateLabel('missing', variable) }} + </span> + </span> + <span class="prep-rail-action" [attr.data-state]="prepVariableHasPendingChange('missing', variable) ? 'pending' : 'set'"> + {{ missingStrategyLabel(ruleFor(variable).action) }}{{ ruleFor(variable).action === 'drop' && prepVariableStateLabel('missing', variable) === 'Default' ? ' · default' : '' }}{{ prepVariableHasPendingChange('missing', variable) ? ' · not applied' : '' }} + </span> + </button> + } + </div> + } + @if (prepGroups('missing').length === 0) { + <div class="empty-state-block compact-empty"> + <i class="fa fa-search"></i> + <p>No variables match the current search.</p> + <span>Clear the search field to show selected variables.</span> + </div> + } + </aside> + + <div class="statistics-panel preprocessing-detail-panel"> + @if (selectedPrepVariable('missing'); as variable) { + <div class="statistics-panel-header"> + <div> + <span class="statistics-panel-kicker">{{ variableTypeLabel(variable) }} variable</span> + <h4>{{ variableLabel(variable) }} @if (variable.code) {<code class="statistics-panel-code">{{ variable.code }}</code>}</h4> + </div> + @if (coverageFor(variable); as headerCoverage) { + <span class="preprocessing-header-coverage">{{ headerCoverage.missing.toLocaleString() }} of {{ headerCoverage.total.toLocaleString() }} values missing</span> + } + <span + class="preprocessing-state-chip" + [class.state-pending]="prepVariableHasPendingChange('missing', variable)" + [class.state-applied]="prepVariableStateLabel('missing', variable) === 'Applied'" + [class.state-default]="prepVariableStateLabel('missing', variable) === 'Default'" + > + {{ prepVariableStateLabel('missing', variable) }} + </span> + </div> + + <div class="preprocessing-detail-section"> + <div class="preprocessing-detail-heading"> + <i class="fa fa-wrench" aria-hidden="true"></i> + <span>Missing value handling</span> + </div> + <fieldset class="missing-choice-grid"> + <legend class="preprocessing-evidence-title">What to do with missing values</legend> + @for (action of missingActions; track action.value) { + <label class="missing-choice" [class.is-selected]="ruleFor(variable).action === action.value" + [class.is-disabled]="isMissingActionDisabled(variable, action.value)"> + <input type="radio" [name]="'missing-' + variable.code" [value]="action.value" + [checked]="ruleFor(variable).action === action.value" + [disabled]="isMissingActionDisabled(variable, action.value)" + (change)="onMissingActionChange(variable, action.value)" /> + <span class="missing-choice-label">{{ action.label }}</span> + <span class="missing-choice-effect">{{ missingActionEffect(variable, action.value) }}</span> + </label> + } + </fieldset> + @if (ruleFor(variable).action === 'constant') { + <label class="preprocessing-control"> + <span>Fill with</span> + @if (ruleFor(variable).action === 'constant' && isCategoricalVariable(variable) && enumOptions(variable).length > 0) { + <select class="table-select" [ngModel]="ruleFor(variable).value" (ngModelChange)="onMissingValueChange(variable, $event)"> + <option value="">Select category</option> + @for (option of enumOptions(variable); track option.code) { + <option [ngValue]="option.code">{{ option.label }}</option> + } + </select> + } @else { + <input class="table-input" type="text" [ngModel]="ruleFor(variable).value" (ngModelChange)="onMissingValueChange(variable, $event)" placeholder="Value" /> + } + </label> + } + @if (preprocessingValidationErrors[variable.code]) { + <div class="row-error">{{ preprocessingValidationErrors[variable.code] }}</div> + } + </div> + + @if (coverageFor(variable); as coverage) { + <aside class="preprocessing-evidence" [attr.data-severity]="coverageSeverity(coverage)"> + <div class="preprocessing-evidence-group"> + <h5 class="preprocessing-evidence-title">Dataset coverage</h5> + @if (coverageByDataset(variable).length) { + <div class="coverage-tiles"> + @for (tile of coverageByDataset(variable); track tile.label) { + <div class="coverage-tile"> + <div class="coverage-tile-head"> + <strong>{{ tile.label }}</strong> + <span>{{ tile.missing.toLocaleString() }} of {{ tile.total.toLocaleString() }}</span> + </div> + <span + class="preprocessing-meter" + role="img" + [attr.aria-label]="tile.missing + ' of ' + tile.total + ' values missing (' + coveragePercent(tile) + ')'" + > + <span class="preprocessing-meter-fill" [style.width.%]="barWidth(tile.share, 800)"></span> + </span> + <div class="coverage-tile-head"> + <span>{{ coveragePercent(tile) }} missing</span> + </div> + </div> + } + </div> + } @else { + <dl class="preprocessing-stat-grid"> + <div class="preprocessing-stat"> + <dt>Total rows</dt> + <dd>{{ coverage.total.toLocaleString() }}</dd> + </div> + <div class="preprocessing-stat"> + <dt>Missing</dt> + <dd>{{ coverage.missing.toLocaleString() }}</dd> + </div> + <div class="preprocessing-stat"> + <dt>Share missing</dt> + <dd>{{ coveragePercent(coverage) }}</dd> + </div> + </dl> + <span + class="preprocessing-meter" + role="img" + [attr.aria-label]="coverage.missing + ' of ' + coverage.total + ' values missing (' + coveragePercent(coverage) + ')'" + > + <span class="preprocessing-meter-fill" [style.width.%]="coverage.share * 100"></span> + </span> + } + </div> + + @let distribution = distributionFor(variable); + @if (distribution.length) { + <div class="preprocessing-evidence-group"> + <h5 class="preprocessing-evidence-title">Distribution</h5> + <dl class="preprocessing-stat-strip"> + @for (point of distribution; track point.label) { + <div class="preprocessing-stat"> + <dt>{{ point.label }}</dt> + <dd>{{ point.value }}</dd> + </div> + } + </dl> + </div> + } + + @let categories = categoryCountsFor(variable); + @if (categories.length) { + <div class="preprocessing-evidence-group preprocessing-evidence-wide"> + <h5 class="preprocessing-evidence-title">Category frequencies</h5> + <ul class="preprocessing-frequency-list"> + @for (category of categories; track category.label) { + <li class="preprocessing-frequency"> + <span class="preprocessing-frequency-label">{{ category.label }}</span> + <span class="preprocessing-frequency-track"> + <span class="preprocessing-frequency-bar" [style.width.%]="category.share * 100"></span> + </span> + <span class="preprocessing-frequency-value">{{ category.value }}</span> + </li> + } + </ul> + </div> + } + </aside> + } @else { + <!-- The evidence slot is what makes this card read as a decision + rather than a bare select. When statistics are unavailable it + has to say so — blank space reads as a broken card. --> + <aside class="preprocessing-evidence" data-severity="none"> + <div class="preprocessing-evidence-group"> + <h5 class="preprocessing-evidence-title">Dataset coverage</h5> + <p class="preprocessing-consequence">{{ summaryUnavailableNote() }}</p> + </div> + </aside> + } + + } @else { + <div class="empty-state-block compact-empty"> + <i class="fa fa-search"></i> + <p>No variables match the current search.</p> + <span>Clear the search field to choose a preprocessing variable.</span> + </div> + } + </div> + </section> + } @else { + <div class="empty-state-block compact-empty"> + <i class="fa fa-table"></i> + <p>No variables selected.</p> + <span>Select variables before configuring preprocessing.</span> + </div> + } + <ng-container ngProjectAs="[stationFooter]" [ngTemplateOutlet]="prepFooter" [ngTemplateOutletContext]="{ $implicit: 'missing' }"></ng-container> + </app-station-card> + + @if (!showOutliersSection) { + <button type="button" class="btn-add-dashed" (click)="enableOutlierHandling()"> + <i class="fas fa-plus" aria-hidden="true"></i> + <span>Add outlier handling — cap extreme values (optional)</span> + </button> + } @else { + <app-station-card + collapsible + icon="fas fa-line-chart" + title="Outlier Handling" + description="Caps extreme values per variable with a winsorizer." + [statusLabel]="outlierPreprocessingStatusLabel" + [status]="statusTone(outlierPreprocessingStatusLabel, hasPendingOutlierChanges)" + [open]="preprocessingStepOpen.outlier" + (openChange)="togglePreprocessingStep('outlier')"> + <!-- Projected next to the status chip: one header, not a second strip glued on top. --> + <button type="button" class="btn-remove-step-node" stationAction (click)="removeOutlierHandling()"> + <i class="fas fa-trash-can" aria-hidden="true"></i> + <span>Remove Step</span> + </button> + @if (preprocessingStepDocumentation('outlier_winsorizer')) { + <button stationAction type="button" class="preprocessing-doc-toggle" + [attr.aria-expanded]="preprocessingDocOpen().outlier" + (click)="togglePreprocessingDoc('outlier')"> + <i class="fas fa-book-open" aria-hidden="true"></i> + <span>How this works</span> + </button> + } + @if (preprocessingStepDocumentation('outlier_winsorizer') && preprocessingDocOpen().outlier) { + <div class="preprocessing-documentation-panel is-open"> + <div class="preprocessing-documentation-content" [innerHTML]="formatPreprocessingDocumentationHtml('outlier_winsorizer')"></div> + </div> + } + + @if (outlierPreprocessingVariables.length > 0) { + <section class="statistics-workspace preprocessing-workspace outlier-preprocessing-workspace"> + <aside class="statistics-browser preprocessing-browser" aria-label="Outlier preprocessing variables"> + <div class="statistics-browser-header"> + <span>Variables</span> + <small>{{ filteredPrepVariables('outlier').length }} shown</small> + <div class="batch-menu"> + <button + type="button" + class="studio-export-btn batch-menu-trigger" + [attr.aria-expanded]="batchMenuOpen() === 'outlier'" + (click)="toggleBatchMenu('outlier')"> + <i class="fas fa-bolt" aria-hidden="true"></i> + Batch + </button> + @if (batchMenuOpen() === 'outlier') { + <div class="batch-menu-list" role="menu"> + <button type="button" role="menuitem" (click)="toggleAllOutliers(true)">Enable for all numerical</button> + <button type="button" role="menuitem" (click)="toggleAllOutliers(false)">Disable for all</button> + </div> + } + </div> + </div> + <div class="search-inline chart-search"> + <i class="fas fa-search search-inline-icon" aria-hidden="true"></i> + <input class="preprocessing-search" type="search" placeholder="Search variables..." aria-label="Search outlier preprocessing variables" [(ngModel)]="prepSearch.outlier" /> + </div> + + @for (group of prepGroups('outlier'); track group.key) { + <div class="statistics-variable-group preprocessing-variable-group" [attr.data-state]="group.key"> + <div class="statistics-variable-group-title">{{ group.title }} <span>{{ group.variables.length }}</span></div> + @for (variable of group.variables; track variable.code) { + <button type="button" class="statistics-variable-btn preprocessing-variable-btn" [class.active]="isPrepVariableSelected('outlier', variable)" (click)="selectPrepVariable('outlier', variable)"> + <span>{{ variableLabel(variable) }}</span> + <span class="statistics-variable-meta"> + <span + class="preprocessing-state-mini" + [class.state-pending]="prepVariableHasPendingChange('outlier', variable)" + [class.state-applied]="prepVariableStateLabel('outlier', variable) === 'Applied'" + > + {{ prepVariableStateLabel('outlier', variable) }} + </span> + <label class="toggle-switch is-mini" title="Enable outlier clipping for this variable" + (click)="$event.stopPropagation()"> + <input type="checkbox" [checked]="outlierRuleFor(variable).enabled" + (change)="onOutlierEnabledChange(variable, $any($event.target).checked)" /> + <span class="toggle-slider"></span> + </label> + </span> + <span class="prep-rail-action" [attr.data-state]="outlierRuleFor(variable).enabled ? (prepVariableHasPendingChange('outlier', variable) ? 'pending' : 'set') : 'off'"> + {{ outlierRuleSummary(variable) }} + </span> + </button> + } + </div> + } + @if (prepGroups('outlier').length === 0) { + <div class="empty-state-block compact-empty"> + <i class="fa fa-search"></i> + <p>No variables match the current search.</p> + <span>Clear the search field to show selected numerical variables.</span> + </div> + } + </aside> + + <div class="preprocessing-detail-column"> + <div class="statistics-panel preprocessing-detail-panel"> + @if (selectedPrepVariable('outlier'); as variable) { + <div class="statistics-panel-header"> + <div> + <span class="statistics-panel-kicker">Numerical variable</span> + <h4>{{ variableLabel(variable) }}</h4> + </div> + <span + class="preprocessing-state-chip" + [class.state-pending]="prepVariableHasPendingChange('outlier', variable)" + [class.state-applied]="prepVariableStateLabel('outlier', variable) === 'Applied'" + > + {{ prepVariableStateLabel('outlier', variable) }} + </span> + </div> + + <div class="preprocessing-detail-section"> + <div class="preprocessing-detail-heading"> + <i class="fa fa-line-chart" aria-hidden="true"></i> + <span>Outlier winsorizer</span> + </div> + @if (!outlierRuleFor(variable).enabled) { + <p class="preprocessing-consequence">Turn on clipping for this variable in the list.</p> + } + <div class="preprocessing-control-grid outlier-control-grid"> + <div class="preprocessing-control"> + <span>Method</span> + <div class="modal-tabs" role="radiogroup" aria-label="Outlier method"> + @for (strategy of outlierStrategies; track strategy.value) { + <button type="button" role="radio" + [class.active]="outlierRuleFor(variable).strategy === strategy.value" + [attr.aria-checked]="outlierRuleFor(variable).strategy === strategy.value" + [disabled]="!outlierRuleFor(variable).enabled" + (click)="onOutlierStrategyChange(variable, strategy.value)">{{ strategy.label }}</button> + } + </div> + </div> + <div class="preprocessing-control"> + <span>Clip</span> + <div class="modal-tabs" role="radiogroup" aria-label="Outlier clip"> + @for (tail of outlierTails; track tail.value) { + <button type="button" role="radio" + [class.active]="outlierRuleFor(variable).tail === tail.value" + [attr.aria-checked]="outlierRuleFor(variable).tail === tail.value" + [disabled]="!outlierRuleFor(variable).enabled" + (click)="onOutlierTailChange(variable, tail.value)">{{ outlierTailDisplay(tail.value) }}</button> + } + </div> + </div> + <label class="preprocessing-control"> + <span>{{ outlierFoldDisplay(outlierRuleFor(variable).strategy) }}</span> + <input class="table-input outlier-fold-input" type="number" step="any" [ngModel]="outlierRuleFor(variable).fold" (ngModelChange)="onOutlierFoldChange(variable, $event)" [disabled]="!outlierRuleFor(variable).enabled" /> + </label> + </div> + @if (outlierRuleError(variable)) { + <div class="row-error">{{ outlierRuleError(variable) }}</div> + } + @if (outlierBoundsView(variable); as bounds) { + <div class="bounds-strip"> + <div class="bounds-labels"> + @if (bounds.loLabel) { + <span [style.left.%]="bounds.loPct" + [style.transform]="(bounds.loPct ?? 0) < 12 ? 'translateX(0)' : ((bounds.loPct ?? 0) > 88 ? 'translateX(-100%)' : 'translateX(-50%)')">{{ bounds.loLabel }}</span> + } + @if (bounds.hiLabel) { + <span [style.left.%]="bounds.hiPct" + [style.transform]="(bounds.hiPct ?? 0) < 12 ? 'translateX(0)' : ((bounds.hiPct ?? 0) > 88 ? 'translateX(-100%)' : 'translateX(-50%)')">{{ bounds.hiLabel }}</span> + } + </div> + <div class="bounds-track"> + <span class="bounds-iqr" [style.left.%]="bounds.iqrLeft" [style.width.%]="bounds.iqrWidth"></span> + <span class="bounds-median" [style.left.%]="bounds.medianLeft"></span> + @if (bounds.cappedLeft !== null) { + <span class="bounds-capped" [style.left.%]="0" [style.width.%]="bounds.cappedLeft"></span> + <span class="bounds-marker" [style.left.%]="bounds.loPct"></span> + } + @if (bounds.cappedRight !== null) { + <span class="bounds-capped" [style.left.%]="bounds.hiPct" [style.width.%]="bounds.cappedRight"></span> + <span class="bounds-marker" [style.left.%]="bounds.hiPct"></span> + } + </div> + <div class="bounds-scale"> + <span class="is-min">{{ bounds.min.toLocaleString() }}</span> + <span [style.left.%]="bounds.medianLeft">{{ distributionMapValue(variable, 'Median') }}</span> + <span class="is-max">{{ distributionMapValue(variable, 'Max') }}</span> + </div> + <span class="preprocessing-consequence">{{ bounds.note }}</span> + </div> + } + </div> + + @let distribution = distributionFor(variable); + <aside class="preprocessing-evidence"> + <h5 class="preprocessing-evidence-title">Distribution</h5> + @if (distribution.length) { + <dl class="preprocessing-stat-strip"> + @for (point of distribution; track point.label) { + <div class="preprocessing-stat"> + <dt>{{ point.label }}</dt> + <dd>{{ point.value }}</dd> + </div> + } + </dl> + @if (coverageFor(variable); as coverage) { + <p class="preprocessing-consequence">{{ coverage.missing.toLocaleString() }} of {{ coverage.total.toLocaleString() }} values missing ({{ coveragePercent(coverage) }}) are ignored by the winsorizer.</p> + } + } @else { + <p class="preprocessing-consequence">{{ summaryUnavailableNote() }}</p> + } + </aside> + + } @else { + <div class="empty-state-block compact-empty"> + <i class="fa fa-search"></i> + <p>No variables match the current search.</p> + <span>Clear the search field to choose a numerical variable.</span> + </div> + } + </div> + + <div class="outlier-report-section"> + @if (selectedPrepVariable('outlier'); as reportVariable) { + <div class="outlier-report-header"> + <h5 class="preprocessing-evidence-title">Values that would be capped · per dataset</h5> + <button type="button" class="station-action-preview" + [disabled]="!hasOutlierRulesForPreview || isLoadingOutlierPreview" + (click)="previewOutlierReport()"> + <i class="fas fa-eye" aria-hidden="true"></i> + Refresh report + </button> + </div> + + @if (isLoadingOutlierPreview) { + <div class="studio-skeleton-card outlier-preview-skeleton" role="status" aria-label="Outlier report preview loading"> + <span class="studio-skeleton-line medium"></span> + <span class="studio-skeleton-line short"></span> + <div class="studio-skeleton-table" aria-hidden="true"> + @for (_ of [1, 2, 3, 4]; track _) { + <span class="studio-skeleton-cell head"></span> + } + @for (_ of [1, 2, 3, 4, 5, 6]; track _) { + <span class="studio-skeleton-cell"></span> + } + </div> + </div> + } + + @if (!isLoadingOutlierPreview && outlierPreviewError) { + <div class="row-error">{{ outlierPreviewError }}</div> + } + + @if (!isLoadingOutlierPreview && outlierPreviewRowsFor(reportVariable).length > 0) { + <div class="table-wrapper statistics-table-wrapper"> + <table class="data-table outlier-report-table"> + <thead> + <tr> + <th>Dataset</th> + <th>Lower bound</th> + <th>Upper bound</th> + <th>Below</th> + <th>Above</th> + <th>Share capped</th> + </tr> + </thead> + <tbody> + @for (row of outlierPreviewRowsFor(reportVariable); track row.variable + '-' + row.dataset) { + <tr> + <td>{{ row.dataset }}</td> + <td>{{ row.lowerBound }}</td> + <td>{{ row.upperBound }}</td> + <td>{{ row.lowerOutliers }}</td> + <td>{{ row.upperOutliers }}</td> + <td class="share-cell"> + <span class="share-bar"><span [style.width.%]="barWidth(row.outlierPercentage, 8)"></span></span> + <strong>{{ row.outlierPercentage }}</strong> + </td> + </tr> + } + </tbody> + </table> + </div> + } + + @if (coverageFor(reportVariable); as reportCoverage) { + <p class="preprocessing-consequence">Bounds are fitted per dataset. {{ reportCoverage.missing.toLocaleString() }} missing values ({{ coveragePercent(reportCoverage) }}) are skipped.</p> + } + } + </div> + </div> + </section> + } @else { + <div class="empty-state-block compact-empty"> + <i class="fa fa-table"></i> + <p>No numerical variables selected.</p> + <span>Outlier handling is available for selected numerical variables.</span> + </div> + } + <ng-container ngProjectAs="[stationFooter]" [ngTemplateOutlet]="prepFooter" [ngTemplateOutletContext]="{ $implicit: 'outlier' }"></ng-container> + </app-station-card> + } + + @if (isLongitudinalModel) { + <app-station-card + collapsible + icon="fas fa-calendar" + title="Longitudinal Transformation" + description="Required for longitudinal pathologies: compare two visits per variable." + [statusLabel]="longitudinalPreprocessingStatusLabel" + [status]="statusTone(longitudinalPreprocessingStatusLabel, pendingLongitudinalChangeCount > 0)" + [open]="preprocessingStepOpen.longitudinal" + (openChange)="togglePreprocessingStep('longitudinal')"> + @if (preprocessingStepDocumentation('longitudinal_transformer')) { + <button stationAction type="button" class="preprocessing-doc-toggle" + [attr.aria-expanded]="preprocessingDocOpen().longitudinal" + (click)="togglePreprocessingDoc('longitudinal')"> + <i class="fas fa-book-open" aria-hidden="true"></i> + <span>How this works</span> + </button> + } + @if (preprocessingStepDocumentation('longitudinal_transformer') && preprocessingDocOpen().longitudinal) { + <div class="preprocessing-documentation-panel is-open"> + <div class="preprocessing-documentation-content" [innerHTML]="formatPreprocessingDocumentationHtml('longitudinal_transformer')"></div> + </div> + } + + <div class="longitudinal-controls"> + <div class="visit-controls"> + <label> + <span>Visit 1</span> + <select class="table-select" [ngModel]="longitudinalVisit1" (ngModelChange)="onLongitudinalVisitChange('visit1', $event)"> + <option value="">Select visit</option> + @for (visit of visitOptions; track visit.code) { + <option [ngValue]="visit.code">{{ visit.label || visit.code }}</option> + } + </select> + </label> + <label> + <span>Visit 2</span> + <select class="table-select" [ngModel]="longitudinalVisit2" (ngModelChange)="onLongitudinalVisitChange('visit2', $event)"> + <option value="">Select visit</option> + @for (visit of visitOptions; track visit.code) { + <option [ngValue]="visit.code">{{ visit.label || visit.code }}</option> + } + </select> + </label> + </div> + </div> + @if (preprocessingValidationErrors['__longitudinal__']) { + <div class="row-error longitudinal-error">{{ preprocessingValidationErrors['__longitudinal__'] }}</div> + } + + @if (preprocessingVariables.length > 0) { + <section class="statistics-workspace preprocessing-workspace"> + <aside class="statistics-browser preprocessing-browser" aria-label="Longitudinal preprocessing variables"> + <div class="statistics-browser-header"> + <span>Variables</span> + <small>{{ filteredPrepVariables('longitudinal').length }} shown</small> + </div> + <div class="search-inline chart-search"> + <i class="fas fa-search search-inline-icon" aria-hidden="true"></i> + <input class="preprocessing-search" type="search" placeholder="Search variables..." aria-label="Search longitudinal preprocessing variables" [(ngModel)]="prepSearch.longitudinal" /> + </div> + + @for (group of prepGroups('longitudinal'); track group.key) { + <div class="statistics-variable-group preprocessing-variable-group"> + <div class="statistics-variable-group-title">{{ group.title }} <span>{{ group.variables.length }}</span></div> + @for (variable of group.variables; track variable.code) { + <button type="button" class="statistics-variable-btn preprocessing-variable-btn" [class.active]="isPrepVariableSelected('longitudinal', variable)" (click)="selectPrepVariable('longitudinal', variable)"> + <span>{{ variableLabel(variable) }}</span> + <span class="statistics-variable-meta"> + <small>{{ variableTypeLabel(variable) }}</small> + <span + class="preprocessing-state-mini" + [class.state-pending]="prepVariableHasPendingChange('longitudinal', variable)" + [class.state-applied]="prepVariableStateLabel('longitudinal', variable) === 'Applied'" + > + {{ prepVariableStateLabel('longitudinal', variable) }} + </span> + </span> + </button> + } + </div> + } + @if (prepGroups('longitudinal').length === 0) { + <div class="empty-state-block compact-empty"> + <i class="fa fa-search"></i> + <p>No variables match the current search.</p> + <span>Clear the search field to show selected variables.</span> + </div> + } + </aside> + + <div class="statistics-panel preprocessing-detail-panel"> + @if (selectedPrepVariable('longitudinal'); as variable) { + <div class="statistics-panel-header"> + <div> + <span class="statistics-panel-kicker">{{ variableTypeLabel(variable) }} variable</span> + <h4>{{ variableLabel(variable) }}</h4> + </div> + <span + class="preprocessing-state-chip" + [class.state-pending]="prepVariableHasPendingChange('longitudinal', variable)" + [class.state-applied]="prepVariableStateLabel('longitudinal', variable) === 'Applied'" + > + {{ prepVariableStateLabel('longitudinal', variable) }} + </span> + </div> + + <div class="preprocessing-detail-section"> + <div class="preprocessing-detail-heading"> + <i class="fa fa-calendar" aria-hidden="true"></i> + <span>Longitudinal strategy</span> + </div> + <label class="preprocessing-control"> + <span>Strategy for {{ variableLabel(variable) }}</span> + <select class="table-select" [ngModel]="longitudinalStrategyFor(variable)" (ngModelChange)="onLongitudinalStrategyChange(variable, $event)"> + @for (strategy of longitudinalStrategies; track strategy.value) { + <option [ngValue]="strategy.value" [disabled]="isLongitudinalStrategyDisabled(variable, strategy.value)">{{ strategy.label }}</option> + } + </select> + </label> + @if (preprocessingValidationErrors[variable.code]) { + <div class="row-error">{{ preprocessingValidationErrors[variable.code] }}</div> + } + </div> + } @else { + <div class="empty-state-block compact-empty"> + <i class="fa fa-search"></i> + <p>No variables match the current search.</p> + <span>Clear the search field to choose a longitudinal strategy variable.</span> + </div> + } + </div> + </section> + } @else { + <div class="empty-state-block compact-empty"> + <i class="fa fa-table"></i> + <p>No variables selected.</p> + <span>Select variables before configuring longitudinal transformation.</span> + </div> + } + <ng-container ngProjectAs="[stationFooter]" [ngTemplateOutlet]="prepFooter" [ngTemplateOutletContext]="{ $implicit: 'longitudinal' }"></ng-container> + </app-station-card> + } + + </div> + </div> + <div #processedSection class="workflow-section" data-guide="analysis-processed-summary"> + <div class="workflow-section-body" [class.open]="sectionOpen().processed"> + <ng-template + [ngTemplateOutlet]="summaryWorkspace" + [ngTemplateOutletContext]="{ kind: 'processed', summary: processedSummary, emptyHint: 'Select variables and datasets to load the processed data summary.' }"></ng-template> + </div> </div> </div> } + </section> + + <!-- CONNECTOR 2 -> 3 --> + <div class="pipeline-connector" aria-hidden="true"> + <div class="pipeline-connector-line" [class.is-active]="isStepAdded('setup')"></div> + <div class="pipeline-connector-arrow"><i class="fas fa-chevron-down"></i></div> + </div> - <!-- Nominal Variables Sheet (Grouped Bar Charts) --> - @if (distributionSubTab === 'Nominal' && nominalVariables.length > 0) { - <div class="distribution-sheet"> - @if (nominalVariables.length > 1) { - <div class="mini-tabs"> - @for (v of nominalVariables; track v; let i = $index) { - <button [class.active]="activeNominalIndex === i" (click)="activeNominalIndex = i"> - {{ v?.name || v?.label || v?.code }} + <!-- NODE 3: DATA TRANSFORMATIONS --> + <section + #transformationSection + class="pipeline-node preprocessing-section" + [class.is-compact]="!sectionOpen().transformation" + data-guide="analysis-transformation" + > + <header class="pipeline-node-header" (click)="onStepHeaderClick('transformation', $event)"> + <div class="pipeline-node-header-left"> + <div class="pipeline-node-icon"><i class="fas fa-code-branch" aria-hidden="true"></i></div> + <div class="pipeline-node-title-group"> +<span class="pipeline-node-kicker">Step 4 · Optional</span> + <h3><button type="button" class="pipeline-node-toggle" [attr.aria-expanded]="isStepOpen('transformation')" (click)="toggleStep('transformation')">Data transformations</button></h3> + <span class="pipeline-node-subtitle">Create a new variable from rules — for example, group MMSE scores into ‘impaired’ and ‘normal’.</span> + </div> + </div> + <div class="pipeline-node-header-right"> + <span class="pipeline-status-badge" [class.applied]="transformationStatusLabel === 'Applied'" [class.optional]="transformationStatusLabel !== 'Applied'"> + {{ transformationBadgeLabel }} + </span> + @if (isStepAdded('transformation')) { + <button + type="button" + class="btn-remove-step-node" + title="Remove transformations from pipeline" + (click)="removeStep('transformation')"> + <i class="fas fa-trash-can" aria-hidden="true"></i> + <span>Remove Step</span> </button> } + <i class="fas fa-chevron-down pipeline-node-chevron" [class.is-open]="isStepOpen('transformation')" aria-hidden="true"></i> + </div> + </header> + + <!-- Collapsed overview: the derived columns the pipeline will create. --> + @if (!sectionOpen().transformation) { + <div class="pipeline-subnodes-tree" role="list" aria-label="Data transformation steps"> + @for (node of appliedTransformationSubNodes; track node.id) { + <div class="pipeline-subnode-item" role="listitem"> + <span class="pipeline-subnode-rail" aria-hidden="true"><span class="pipeline-subnode-dot"></span></span> + <button type="button" class="pipeline-subnode-content" (click)="jumpToSubNode('transformation', node.id)" title="Open this transformation"> + <i [class]="node.icon" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">{{ node.title }}</span> + <span class="pipeline-subnode-subtitle">{{ node.subtitle }}</span> + </span> + <span class="pipeline-subnode-status" [attr.data-tone]="node.statusTone">{{ node.statusLabel }}</span> + <span class="pipeline-subnode-cta">Edit <i class="fas fa-chevron-right" aria-hidden="true"></i></span> + </button> </div> } - <div class="chart-container fade"> - <app-chart-renderer [charts]="chartsForNominal[activeNominalIndex] || []"></app-chart-renderer> + <div class="pipeline-subnode-item is-ghost" role="listitem"> + <span class="pipeline-subnode-rail" aria-hidden="true"><span class="pipeline-subnode-dot"></span></span> + <button type="button" class="pipeline-subnode-content" (click)="addTransformationSubNode()" title="Add and open a derived column"> + <i class="fas fa-tag" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">Add derived column</span> + <span class="pipeline-subnode-subtitle">Create another nominal or numeric feature</span> + </span> + <i class="fas fa-plus pipeline-subnode-add" aria-hidden="true"></i> + </button> + </div> + </div> + } + + @if (isStepAdded('transformation')) { + <div class="pipeline-node-body"> + @if (sectionOpen().transformation) { + <ng-container [ngTemplateOutlet]="howToStrip" [ngTemplateOutletContext]="{ $implicit: howTo.transformation }"></ng-container> + } + <div class="workflow-section-body" [class.open]="sectionOpen().transformation"> + <div class="preprocessing-shell"> + <!-- The stage's own vertical graph: one node per derived column, joined by the + pipeline connector, ending in the type chooser. Unchosen blank drafts stay + off screen; they hold no rules, so the #ruleModal order is unaffected. --> + <div class="transformation-graph"> + @for (step of cards(); track step.id) { + <div class="transformation-card-wrap" [attr.data-step-id]="step.id"> + @if (!isCreatorAvailable(step.name)) { + <app-station-card + collapsible + [icon]="creatorChooserCopy(step.name).icon" + [title]="stepTitle(step)" + description="This column creator is not available on this deployment." + [statusLabel]="stepStatus(step).label" + [status]="stepStatus(step).tone" + [open]="isCardOpen(step.id)" + (openChange)="toggleCardOpen(step.id, $event)"> + <button + type="button" + class="btn-remove-step-node" + stationAction + [disabled]="!canRemoveTransformation(step.id)" + [title]="removeTransformationTooltip(step.id)" + (click)="removeTransformationDraft(step.id)"> + <i class="fas fa-trash-can" aria-hidden="true"></i> + <span>Remove column</span> + </button> + <p class="glass-feedback glass-feedback-warning">Not available on this deployment</p> + </app-station-card> + } @else if (step.name === 'categorical_column_creator') { + @if (draftForStep(step.id); as draft) { + <app-station-card + collapsible + icon="fas fa-square-plus" + [title]="stepTitle(step)" + description="Each record gets the first category whose rule it matches." + [statusLabel]="transformationDraftStatus(draft).label" + [status]="transformationDraftStatus(draft).tone" + [open]="draft.open" + (openChange)="toggleTransformationDraft(draft)"> + <button + type="button" + class="btn-remove-step-node" + stationAction + [disabled]="!canRemoveTransformation(step.id)" + [title]="removeTransformationTooltip(step.id)" + (click)="removeTransformationDraft(draft.id)"> + <i class="fas fa-trash-can" aria-hidden="true"></i> + <span>Remove column</span> + </button> + @if (countsDraftId === draft.id && (isTransformationStatsLoading || transformationStatisticsError)) { + <div class="transformation-statistics"> + @if (isTransformationStatsLoading) { + <div class="studio-skeleton-line"></div> + <div class="studio-skeleton-line short"></div> + } @else { + <div class="glass-feedback glass-feedback-warning">{{ transformationStatisticsError }}</div> + } + </div> + } + @for (block of transformationStatsFor(draft.code); track block.code) { + @if (transformationCountChart(block); as chart) { + <div class="transformation-count-chart"> + <app-histogram [data]="chart" [config]="{ color: '#2b33e9', orientation: 'horizontal' }"></app-histogram> + </div> + } + } + <div class="transformation-create"> + <section class="column-step"> + <label class="column-step-head" [attr.for]="'transformation-code-' + draft.id"> + <span class="column-step-number">1</span>Name the new column + </label> + <div class="transformation-name-row"> + <input [id]="'transformation-code-' + draft.id" class="transformation-code-input" type="text" [ngModel]="draft.code" (ngModelChange)="draft.code = $event; onTransformationInput()" placeholder="e.g. mrs_good_outcome" /> + @if (columnNameError(draft.id, draft.code); as nameError) { + <span class="transformation-hint transformation-hint-error">{{ nameError }}</span> + } @else { + <span class="transformation-hint">Lowercase letters, digits and _ only</span> + } + </div> + </section> + + <section class="column-step"> + <div class="column-step-head"> + <span class="column-step-number">2</span>Add categories, each with a rule + <span class="column-step-meta"> + @if (draft.openRuleIndex != null) { + Editing category {{ draft.openRuleIndex + 1 }} of {{ draft.rules.length }} + } @else { + {{ draft.rules.length }} {{ draft.rules.length === 1 ? 'category' : 'categories' }} + fallback + } + </span> + </div> + <div class="transformation-rules" [class.is-focused]="draft.openRuleIndex != null"> + <div class="transformation-rule-head"> + <span></span> + <span>Value</span> + <span>Rule — records that…</span> + <span>Records</span> + <span></span> + </div> + <div class="transformation-rules-list"> + @for (rule of draft.rules; track $index; let ruleIndex = $index) { + <div class="transformation-rule-row" [class.is-editing]="draft.openRuleIndex === ruleIndex"> + <span class="transformation-rule-number">{{ ruleIndex + 1 }}</span> + <input + type="text" + [ngModel]="rule.value" + (ngModelChange)="rule.value = $event; onTransformationInput()" + placeholder="Enumeration value" + class="transformation-rule-value input" + /> + <span class="transformation-rule-summary"> + <span class="transformation-rule-sentence" [class.is-empty]="!rule.filter">{{ transformationRuleSummary(rule) }}</span> + @if (draft.openRuleIndex !== ruleIndex) { + <button type="button" class="transformation-rule-edit-link" + (click)="toggleTransformationRule(draft, ruleIndex)"> + <i class="fas" [class.fa-pen]="rule.filter" [class.fa-plus]="!rule.filter" aria-hidden="true"></i> + {{ rule.filter ? 'Edit rule' : 'Set rule' }} + </button> + } + </span> + <span class="transformation-rule-record-count">{{ transformationCount(draft, rule.value) }}</span> + <button type="button" class="transformation-rule-remove" title="Remove category" + (click)="removeTransformationRule(draft, ruleIndex)"> + <i class="fas fa-xmark" aria-hidden="true"></i> + </button> + <div class="transformation-rule-editor" [class.is-open]="draft.openRuleIndex === ruleIndex"> + <p class="transformation-rule-editor-lead"> + Records that match get + <strong>{{ rule.value.trim() || 'this category' }}</strong>@if (ruleIndex > 0) {, unless an earlier category already took them}. + </p> + <app-filter-config-modal + #ruleModal + variableScope="selectedVariables" + [filterLogic]="rule.filter" + ></app-filter-config-modal> + <div class="transformation-rule-editor-foot"> + <button type="button" class="transformation-rule-done" + (click)="finishTransformationRule(draft, ruleIndex)"> + <i class="fas fa-check" aria-hidden="true"></i> + Done + </button> + <span>Saves this rule and returns to all categories.</span> + </div> + </div> + </div> + } + <button type="button" class="transformation-add-category" (click)="addTransformationRule(draft)"> + <i class="fas fa-plus" aria-hidden="true"></i> + <span>{{ draft.rules.length ? 'Add category' : 'Add the first category, then define the rule that selects its records' }}</span> + </button> + <div class="transformation-rule-row transformation-rule-fallback"> + <span class="transformation-rule-number">↳</span> + <input + type="text" + [ngModel]="draft.defaultEnumeration" + (ngModelChange)="draft.defaultEnumeration = $event; onTransformationInput()" + placeholder="unknown" + class="transformation-rule-value input" + /> + <span class="transformation-rule-summary">Otherwise — records that match no rule</span> + <span class="transformation-rule-record-count">{{ transformationCount(draft, draft.defaultEnumeration) }}</span> + <span></span> + </div> + </div> + <p class="transformation-rules-foot"> + Rules are checked top to bottom; the first match wins · + {{ draft.defaultEnumeration ? 'the fallback covers every other record' : 'records matching no rule get no value' }} + </p> + </div> + </section> + </div> + <app-station-action-bar + stationFooter + flush + [statusText]="draftHasNewWork(draft) ? 'Not applied — finish the column to add it to the run.' : ''" + [tone]="transformationDraftStatus(draft).tone" + [applyLabel]="draftHasNewWork(draft) ? 'Apply' : 'Close'" + [applyIcon]="draftHasNewWork(draft) ? 'fas fa-check' : 'fas fa-times'" + [applyVariant]="draftHasNewWork(draft) ? 'primary' : 'quiet'" + (apply)="commitTransformationDraft(draft)" + ></app-station-action-bar> + </app-station-card> + } + } @else if (step.name === 'kmeans_cluster_creator') { + <app-station-card + collapsible + icon="fas fa-diagram-project" + [title]="stepTitle(step)" + description="Group records by similarity, then use the cluster as a nominal variable." + [statusLabel]="stepStatus(step).label" + [status]="stepStatus(step).tone" + [open]="isCardOpen(step.id)" + (openChange)="toggleCardOpen(step.id, $event)"> + <button + type="button" + class="btn-remove-step-node" + stationAction + [disabled]="!canRemoveTransformation(step.id)" + [title]="removeTransformationTooltip(step.id)" + (click)="removeTransformationDraft(step.id)"> + <i class="fas fa-trash-can" aria-hidden="true"></i> + <span>Remove column</span> + </button> + @if (!stepOutputNames(step).length) { + <ng-container [ngTemplateOutlet]="howToStrip" [ngTemplateOutletContext]="{ $implicit: howTo.kmeans }"></ng-container> + } + @if (previewDraftId === step.id) { + <ng-container [ngTemplateOutlet]="transformationStats" [ngTemplateOutletContext]="{ $implicit: stepOutputNames(step)[0] ?? '' }"></ng-container> + } + <app-kmeans-cluster-source + [stepId]="step.id" + [columnNameError]="kmeansColumnNameError" + [canRemove]="canRemoveTransformation(step.id)" + [removeTooltip]="removeTransformationTooltip(step.id)" + [class.is-previewing]="previewDraftId === step.id" + (runRequested)="previewTransformationCard(step.id)" + /> + @if (stepOutputNames(step).length) { + <app-station-action-bar + stationFooter + flush + tone="applied" + [previewLabel]="previewDraftId === step.id ? 'Back to column' : 'Preview cluster counts'" + applyLabel="Close" + applyIcon="fas fa-times" + applyVariant="quiet" + (preview)="toggleTransformationPreview()" + (apply)="closeTransformationPreview(); toggleCardOpen(step.id, false)" + ></app-station-action-bar> + } + </app-station-card> + } @else if (step.name === 'pca_column_creator') { + <app-station-card + collapsible + icon="fas fa-chart-line" + [title]="stepTitle(step)" + description="Project numerical variables onto principal components." + [statusLabel]="stepStatus(step).label" + [status]="stepStatus(step).tone" + [open]="isCardOpen(step.id)" + (openChange)="toggleCardOpen(step.id, $event)"> + <button + type="button" + class="btn-remove-step-node" + stationAction + [disabled]="!canRemoveTransformation(step.id)" + [title]="removeTransformationTooltip(step.id)" + (click)="removeTransformationDraft(step.id)"> + <i class="fas fa-trash-can" aria-hidden="true"></i> + <span>Remove column</span> + </button> + @if (!stepOutputNames(step).length) { + <ng-container [ngTemplateOutlet]="howToStrip" [ngTemplateOutletContext]="{ $implicit: howTo.pca }"></ng-container> + } + <app-pca-column-source + [stepId]="step.id" + [columnNameError]="kmeansColumnNameError" + [canRemove]="canRemoveTransformation(step.id)" + [removeTooltip]="removeTransformationTooltip(step.id)" + /> + @if (stepOutputNames(step).length) { + <app-station-action-bar + stationFooter + flush + tone="applied" + applyLabel="Close" + applyIcon="fas fa-times" + applyVariant="quiet" + (apply)="toggleCardOpen(step.id, false)" + ></app-station-action-bar> + } + </app-station-card> + } + </div> + <div class="pipeline-connector" aria-hidden="true"> + <div class="pipeline-connector-line"></div> + <div class="pipeline-connector-arrow"><i class="fas fa-chevron-down"></i></div> + </div> + } + <div class="transformation-chooser"> + @if (transformationChooserOnly) { + <p class="pipeline-subnodes-caption">Choose the kind of column to add. You can add more afterwards.</p> + } + <div role="list" aria-label="Kinds of derived column"> + @for (kind of chooserCreators(); track kind) { + <div class="pipeline-subnode-item is-ghost" role="listitem"> + <button type="button" class="pipeline-subnode-content" (click)="chooseTransformation(kind)"> + <i [class]="creatorChooserCopy(kind).icon" aria-hidden="true"></i> + <span class="pipeline-subnode-text"> + <span class="pipeline-subnode-title">{{ creatorChooserCopy(kind).title }}</span> + <span class="pipeline-subnode-subtitle">{{ creatorChooserCopy(kind).subtitle }}</span> + </span> + <i class="fas fa-plus pipeline-subnode-add" aria-hidden="true"></i> + </button> + </div> + } + </div> + </div> + </div> + </div> </div> </div> } </section> - } - } - <!-- Hidden charts for PDF export --> - <div class="hidden-charts-for-export"> + <!-- CONNECTOR 3 -> TERMINAL --> + <div class="pipeline-connector" aria-hidden="true"> + <div class="pipeline-connector-line" [class.is-active]="isStepAdded('transformation')"></div> + <div class="pipeline-connector-arrow"><i class="fas fa-arrow-down"></i></div> + </div> + + <!-- PIPELINE TERMINAL --> + <div class="pipeline-terminal-card"> + @if (showUnappliedChangesWarning()) { + <div class="unapplied-warning-card" role="alert"> + <div class="unapplied-warning-icon"><i class="fas fa-triangle-exclamation" aria-hidden="true"></i></div> + <div class="unapplied-warning-text"> + <h4>You have unapplied changes</h4> + <p>Apply them or discard them before moving to Algorithm Selection.</p> + </div> + <div class="unapplied-warning-actions"> + <button type="button" class="btn-finish-pipeline" (click)="applyAndContinue()"> + <span>Apply & Continue</span> + <i class="fas fa-check" aria-hidden="true"></i> + </button> + <button type="button" class="btn-finish-pipeline unapplied-warning-btn secondary" (click)="discardAndContinue()"> + <span>Discard Changes & Continue</span> + </button> + <button type="button" class="btn-finish-pipeline unapplied-warning-btn ghost" (click)="showUnappliedChangesWarning.set(false)"> + <span>Stay in Pipeline</span> + </button> + </div> + </div> + } + <div class="pipeline-terminal-summary"> + <span class="pipeline-terminal-kicker">The next run will use</span> + <div class="pipeline-terminal-facts"> + @if (cohortRetention; as r) { + <span><strong>{{ r.processed.toLocaleString() }}</strong> of {{ r.raw.toLocaleString() }} records</span> + } + <span><strong>{{ preprocessingVariables.length }}</strong> variables · {{ expStudioService.selectedDatasets().length }} datasets</span> + <span>{{ terminalPreprocessingLine }}</span> + </div> + </div> + <button type="button" class="btn-finish-pipeline" (click)="finishDataHandling()"> + <span>Continue to Algorithm Selection</span> + <i class="fas fa-arrow-right" aria-hidden="true"></i> + </button> + </div> + + <!-- Off-screen chart surfaces the PDF export reads by `${kind}-export` class. They + stay mounted while the summary is closed, so the export button always finds them. --> + @for (kind of summaryKinds; track kind) { + @if (getSummary(kind); as summary) { + <div + class="hidden-charts-for-export" + [class.source-export]="kind === 'source'" + [class.raw-export]="kind === 'raw'" + [class.processed-export]="kind === 'processed'" + > <div class="numeric-charts-export"> - @for (v of nonNominalVariables; track v; let i = $index) { - <app-chart-renderer class="invisible" [charts]="chartsForBoxPlot[i]"></app-chart-renderer> + @for (v of summary.nonNominalVariables; track v.code; let i = $index) { + <app-chart-renderer class="invisible" [charts]="summary.chartsForBoxPlot[i]"></app-chart-renderer> } </div> <div class="nominal-charts-export"> - @for (v of nominalVariables; track v; let i = $index) { - <app-chart-renderer class="invisible" [charts]="chartsForNominal[i]"></app-chart-renderer> + @for (v of summary.nominalVariables; track v.code; let i = $index) { + <app-chart-renderer class="invisible" [charts]="summary.chartsForNominal[i]"></app-chart-renderer> } </div> </div> + } + } </div> </div> </div> @if (isExporting) { <div class="pdf-overlay"> - <app-spinner text="Generating PDF..." color="#2b33e9" size="72px" [overlay]="false" /> + <div class="studio-skeleton-card" role="status" aria-label="Generating PDF"> + <span class="studio-skeleton-line medium"></span> + <span class="studio-skeleton-line short"></span> + </div> </div> -} \ No newline at end of file +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.spec.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.spec.ts index 9910c73e8..3c43eed0a 100644 --- a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.spec.ts +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.spec.ts @@ -1,32 +1,142 @@ +import type { MockedObject } from 'vitest'; import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { QueryList } from '@angular/core'; import { StatisticAnalysisPanelComponent } from './statistic-analysis-panel.component'; import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; import { ChartBuilderService } from '../visualisations/charts/chart-builder.service'; import { PdfExportService } from '../../../services/pdf-export.service'; -import { of } from 'rxjs'; -import { signal } from '@angular/core'; +import { CsvExportService } from '../../../services/csv-export.service'; +import { of, Subject } from 'rxjs'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { provideEchartsCore } from 'ngx-echarts'; +import { createSpyObj } from '../../../testing/spy-obj.testing'; describe('StatisticAnalysisPanelComponent', () => { let component: StatisticAnalysisPanelComponent; let fixture: ComponentFixture<StatisticAnalysisPanelComponent>; - let mockExpService: jasmine.SpyObj<ExperimentStudioService>; - let mockChartBuilder: jasmine.SpyObj<ChartBuilderService>; - let mockPdfService: jasmine.SpyObj<PdfExportService>; + let mockExpService: MockedObject<ExperimentStudioService>; + let mockChartBuilder: MockedObject<ChartBuilderService>; + let mockPdfService: MockedObject<PdfExportService>; beforeEach(async () => { - mockExpService = jasmine.createSpyObj('ExperimentStudioService', ['loadDescriptiveOverview'], { + mockExpService = createSpyObj('ExperimentStudioService', [ + 'loadDescriptiveOverview', + 'loadOutlierReportPreview', + 'getAlgorithmResults', + 'getDatasetLabelMap', + 'setDataExclusionWarnings', + 'clearDataExclusionWarnings', + 'setAppliedDescriptivePreprocessing', + 'getAppliedDescriptivePreprocessing', + 'setFilters', + 'setFilterLogic', + 'filterVariableCodes', + 'buildTransformationDescribePreprocessing', + 'transformationSourceCodesForInput', + 'requestDatasets', + 'requestFilters', + 'getActiveDataModelCode', + 'upsertTransformation', + 'removeTransformation', + 'clearTransformations', + 'transformationDependents', + 'outputsInUse', + 'availableCreatorNames', + 'hasPreprocessingSpec', + 'catalogLoaded', + 'stepOutputNames', + ], { selectedVariables: signal([]), - selectedCovariates: signal([]), selectedFilters: signal([]), - selectedDataModel: signal(null) + selectedDatasets: signal(['dataset-a']), + excludedDatasets: signal([]), + selectedDataModel: signal({ code: 'Stroke', version: '3.7', variables: [] }), + unavailableVariables: signal(new Map()), + filterLogic: signal(null), + editingExistingExperiment: () => false, + appliedPreprocessingConfig: signal(null), + appliedTransformations: signal([] as { id: string; name: string; parameters: Record<string, unknown> }[]), + backendAlgorithms: signal({}) }); - mockChartBuilder = jasmine.createSpyObj('ChartBuilderService', ['getChartsForAlgorithm']); - mockPdfService = jasmine.createSpyObj('PdfExportService', ['exportDescriptiveStatisticsPdf']); + mockChartBuilder = createSpyObj('ChartBuilderService', ['getChartsForAlgorithm']); + mockChartBuilder.getChartsForAlgorithm.mockReturnValue([]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + mockExpService.loadOutlierReportPreview.mockReturnValue(of({ result: { featurewise: [] } })); + mockExpService.getAlgorithmResults.mockReturnValue(of({ result: { histogram: [] } })); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(null); + mockExpService.getDatasetLabelMap.mockReturnValue({ 'dataset-a': 'Dataset A' }); + mockExpService.buildTransformationDescribePreprocessing.mockImplementation((singletons: any) => { + const steps: { name: string; parameters: Record<string, unknown> }[] = []; + if (singletons && typeof singletons === 'object') { + for (const [name, parameters] of Object.entries(singletons)) { + if (parameters && typeof parameters === 'object') { + steps.push({ name, parameters: parameters as Record<string, unknown> }); + } + } + } + for (const step of mockExpService.appliedTransformations() as any[]) { + if (Array.isArray(step.parameters?.new_column_names) && step.parameters.new_column_names.some((n: unknown) => String(n ?? '').trim())) { + steps.push({ name: step.name, parameters: step.parameters }); + } + } + return steps.length ? steps : null; + }); + mockExpService.transformationSourceCodesForInput.mockReturnValue([]); + mockExpService.requestDatasets.mockReturnValue(['dataset-a']); + mockExpService.requestFilters.mockReturnValue(null); + mockExpService.getActiveDataModelCode.mockReturnValue('Stroke:3.7'); + mockExpService.availableCreatorNames.mockReturnValue([ + 'categorical_column_creator', + 'kmeans_cluster_creator', + ]); + mockExpService.hasPreprocessingSpec.mockImplementation((name: string) => + ['categorical_column_creator', 'kmeans_cluster_creator'].includes(name), + ); + mockExpService.catalogLoaded.mockReturnValue(true); + mockExpService.transformationDependents.mockReturnValue([]); + mockExpService.outputsInUse.mockReturnValue([]); + mockExpService.upsertTransformation.mockImplementation((step: any) => { + const current = [...(mockExpService.appliedTransformations() as any[])]; + const index = current.findIndex((existing) => existing.id === step.id); + const next = index === -1 + ? [...current, step] + : current.map((existing, i) => (i === index ? step : existing)); + (mockExpService.appliedTransformations as any).set(next); + return true; + }); + mockExpService.clearTransformations.mockImplementation(() => + (mockExpService.appliedTransformations as any).set([])); + mockExpService.removeTransformation.mockImplementation((id: string) => { + const next = (mockExpService.appliedTransformations() as any[]).filter((step) => step.id !== id); + (mockExpService.appliedTransformations as any).set(next); + }); + // Mirrors the real collector: a group walks its rules, a condition is its field. + mockExpService.filterVariableCodes.mockImplementation((logic: any) => { + const codes = new Set<string>(); + const walk = (node: any) => { + if (!node) return; + if (Array.isArray(node.rules)) { + node.rules.forEach(walk); + } else if (node.field || node.id) { + codes.add(String(node.field ?? node.id)); + } + }; + walk(logic); + return [...codes]; + }); + mockPdfService = createSpyObj('PdfExportService', ['exportDescriptiveStatisticsPdf']); await TestBed.configureTestingModule({ imports: [StatisticAnalysisPanelComponent], // Standalone component providers: [ + provideZonelessChangeDetection(), + provideEchartsCore({ + echarts: () => import('echarts'), + }), { provide: ExperimentStudioService, useValue: mockExpService }, + { provide: ExperimentsDashboardService, useValue: { listKMeansExperiments: () => of([]), listPcaExperiments: () => of([]) } }, { provide: ChartBuilderService, useValue: mockChartBuilder }, { provide: PdfExportService, useValue: mockPdfService } ] @@ -37,10 +147,401 @@ describe('StatisticAnalysisPanelComponent', () => { fixture.detectChanges(); }); + function configureRawSummary(chartOptions: any[] = []) { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + mockChartBuilder.getChartsForAlgorithm.mockReturnValue(chartOptions); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + { dataset: 'dataset-a', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + { dataset: 'all datasets', variable: 'sex', data: { counts: { female: 6, male: 4 }, num_dtps: 10, num_na: 1, num_total: 11 } }, + ], + }, + })); + (mockExpService.selectedVariables as any).set([age, sex]); + fixture.detectChanges(); + return { age, sex }; + } + + /** A minimal valid category rule payload, as QueryBuilder would export it. */ + function categoryFilter(field = 'age', operator = 'greater', value = 65) { + return { + condition: 'AND' as const, + rules: [{ + id: `${field}-${operator}`, + field, + type: 'integer' as const, + input: 'number' as const, + operator, + value, + }], + }; + } + + function workflowSection(title: string): HTMLElement { + const guideMap: Record<string, string> = { + 'Raw Data Summary': 'analysis-raw-summary', + 'Processed Data Summary': 'analysis-processed-summary', + Filtering: 'analysis-filtering', + Preprocessing: 'analysis-preprocessing', + Transformation: 'analysis-transformation', + }; + // Stations render as .pipeline-node, the two summaries as .workflow-section. + const sections = () => + Array.from(fixture.nativeElement.querySelectorAll('.workflow-section, .pipeline-node')) as HTMLElement[]; + const guide = guideMap[title]; + if (guide) { + return sections().find((section) => section.getAttribute('data-guide') === guide) as HTMLElement; + } + return sections().find((section) => section.textContent?.includes(title)) as HTMLElement; + } + + /** + * Pipeline nodes render collapsed until they hold work or were opened this visit, + * so a spec that reads a station's editor has to open it first. Opening is a read. + */ + /** Seed N categorical cards through the store-backed chooser (drafts start empty). */ + function ensureCategoricalDrafts(count = 1): void { + while (component.transformationDrafts.length < count) { + component.chooseTransformation('categorical_column_creator'); + } + fixture.detectChanges(); + } + + function openStation(step: 'filters' | 'setup' | 'transformation'): HTMLElement { + component.addStep(step); + fixture.detectChanges(); + return workflowSection({ filters: 'Filtering', setup: 'Preprocessing', transformation: 'Transformation' }[step]); + } + it('should create', () => { expect(component).toBeTruthy(); }); + it('labels enumeration values in a derived-column rule summary', () => { + (mockExpService.selectedVariables as any).set([{ + code: 'clinical_sdr', + label: 'Clinical syndrome', + enumerations: [{ code: '1', label: 'TACS' }, { code: '2', label: 'PACS' }], + }]); + + expect(component.transformationRuleSummary({ + value: 'ACS', + filter: { id: 'clinical_sdr', field: 'clinical_sdr', operator: 'in', value: ['1', '2'], type: 'string' } as any, + })).toBe('Clinical syndrome is any of TACS, PACS'); + }); + + it('toggles Filtering exclusive of Raw Summary via Preview data', () => { + configureRawSummary(); + component.goToSection('filters'); + fixture.detectChanges(); + + const filtering = workflowSection('Filtering'); + expect(component.sectionOpen().filters).toBe(true); + expect(component.sectionOpen().raw).toBe(false); + + const preview = filtering.querySelector('.station-action-preview') as HTMLButtonElement; + expect(preview.textContent?.trim()).toBe('Preview matching records'); + preview.click(); + fixture.detectChanges(); + + expect(component.sectionOpen().filters).toBe(false); + expect(component.sectionOpen().raw).toBe(true); + expect(component.sectionOpen().setup).toBe(false); + + const raw = workflowSection('Raw Data Summary'); + const tabLabels = Array.from(raw.querySelectorAll('.summary-tabs button')).map((button) => button.textContent?.trim()); + expect(tabLabels).toEqual(['Table', 'Charts', 'Histogram']); + expect(raw.querySelector('.station-preview-toggle')?.textContent?.trim()).toBe('Close'); + + const close = workflowSection('Raw Data Summary').querySelector('.station-preview-toggle') as HTMLButtonElement; + expect(close.textContent?.trim()).toBe('Close'); + close.click(); + fixture.detectChanges(); + + expect(component.sectionOpen().filters).toBe(true); + expect(component.sectionOpen().raw).toBe(false); + }); + + /** + * The station footer's primary slot only ever says Apply when there is something to + * commit; with an empty station it reads Close and folds the station away instead. + */ + it('labels the filtering primary action Close while there is nothing to commit', () => { + configureRawSummary(); + const filtering = openStation('filters'); + + const apply = filtering.querySelector('.station-action-apply') as HTMLButtonElement; + expect(apply.textContent?.trim()).toBe('Close'); + expect(apply.disabled).toBe(false); + expect(apply.classList.contains('is-quiet')).toBe(true); + + apply.click(); + fixture.detectChanges(); + + // An optional station with no conditions has nothing to keep: it folds to its rail. + expect(component.isStepAdded('filters')).toBe(false); + expect(component.sectionOpen().filters).toBe(false); + expect(workflowSection('Filtering').querySelector('.btn-remove-step-node')).toBeNull(); + }); + + it('labels the filtering primary action Apply once conditions exist', () => { + configureRawSummary(); + (mockExpService.filterLogic as any).set(categoryFilter()); + const filtering = openStation('filters'); + fixture.detectChanges(); + + const apply = filtering.querySelector('.station-action-apply') as HTMLButtonElement; + expect(apply.textContent?.trim()).toBe('Apply filters'); + expect(apply.classList.contains('is-quiet')).toBe(false); + + apply.click(); + fixture.detectChanges(); + + expect(mockExpService.setFilterLogic).toHaveBeenCalled(); + expect(component.sectionOpen().filters).toBe(false); + expect(component.sectionOpen().raw).toBe(false); + expect(component.sectionOpen().setup).toBe(false); + expect(component.sectionOpen().transformation).toBe(false); + expect(component.isStepAdded('filters')).toBe(true); + }); + + it('closes the Missing Values card from Close without writing anything', () => { + configureRawSummary(); + const preprocessing = openStation('setup'); + + // The card's own footer owns the action: no stage-level bar any more. + const apply = preprocessing.querySelector('.station-card-footer .station-action-apply') as HTMLButtonElement; + expect(component.stepPendingCount('missing')).toBe(0); + expect(apply.textContent?.trim()).toBe('Close'); + expect(apply.disabled).toBe(false); + + const persistCalls = mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length; + apply.click(); + fixture.detectChanges(); + + // Close folds its own card and leaves the stage exactly as it was. + expect(component.preprocessingStepOpen.missing).toBe(false); + expect(component.sectionOpen().setup).toBe(true); + expect(mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length).toBe(persistCalls); + }); + + it('closes the preprocessing Batch menu when the click is outside it', () => { + configureRawSummary(); + const preprocessing = openStation('setup'); + const trigger = preprocessing.querySelector('.batch-menu-trigger') as HTMLButtonElement; + trigger.click(); + fixture.detectChanges(); + expect(preprocessing.querySelector('.batch-menu-list')).toBeTruthy(); + + fixture.nativeElement.click(); + fixture.detectChanges(); + expect(preprocessing.querySelector('.batch-menu-list')).toBeNull(); + expect(component.batchMenuOpen()).toBeNull(); + }); + + it('previews processed data from the preprocessing card footer, exclusive of processed summary', () => { + configureRawSummary(); + component.goToSection('setup'); + fixture.detectChanges(); + + const preprocessing = workflowSection('Preprocessing'); + const preview = preprocessing.querySelector( + '.station-card-footer .station-action-preview') as HTMLButtonElement; + expect(preview.textContent?.trim()).toBe('Preview processed data'); + expect(preprocessing.querySelector('.station-card-description')?.textContent?.trim()).toContain('Choose what happens to missing values'); + // One footer bar per open card: the open Missing Values card owns the station's only + // preview control, and nothing sits outside a card footer. + expect(preprocessing.querySelectorAll('.station-action-bar .station-action-preview').length).toBe(1); + + preview.click(); + fixture.detectChanges(); + + expect(component.sectionOpen().setup).toBe(false); + expect(component.sectionOpen().processed).toBe(true); + + const processed = workflowSection('Processed Data Summary'); + const tabLabels = Array.from(processed.querySelectorAll('.summary-tabs button')).map((button) => button.textContent?.trim()); + expect(tabLabels).not.toContain('Statistics'); + expect(processed.querySelector('.station-preview-toggle')?.textContent?.trim()).toBe('Close'); + }); + + /** + * "Preview data" runs a transient describe of the pending preprocessing config + * (finding F1 / Phase 3 of data-handling-flow-plan.md): the processed workspace + * fills without Apply, and nothing is persisted. + */ + it('opens processed tables from Preview data without clicking Apply', () => { + vi.spyOn(Element.prototype, 'scrollIntoView').mockReturnValue(undefined); + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback): number => { + void Promise.resolve().then(() => callback(0)); + return 0; + }); + configureRawSummary(); + component.goToSection('setup'); + fixture.detectChanges(); + + const preprocessing = workflowSection('Preprocessing'); + const apply = preprocessing.querySelector('.station-action-apply') as HTMLButtonElement; + // Nothing pending: the slot reads Close (enabled) rather than a disabled Apply. + expect(apply.disabled).toBe(false); + expect(preprocessing.textContent).not.toContain('No preprocessing has been applied yet'); + + const persistCalls = mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length; + const preview = preprocessing.querySelector('.station-action-preview') as HTMLButtonElement; + preview.click(); + fixture.detectChanges(); + + expect(component.sectionOpen().processed).toBe(true); + expect(component.sectionOpen().setup).toBe(false); + expect(component.processedSummary.isLoading).toBe(false); + expect(component.selectedStatisticBlock('processed')?.name).toBe('Age'); + // Transient: previewing wrote nothing into the applied request. + expect(mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length).toBe(persistCalls); + + const processed = workflowSection('Processed Data Summary'); + expect(processed.textContent).not.toContain('No preprocessing has been applied yet'); + expect(processed.querySelector('[aria-label="Processed data summary variables"]')).toBeTruthy(); + }); + + /** + * The preview has to survive the fold. The processed summary used to live *inside* the + * station editor's `.workflow-section-body`, which is `display: none` the moment + * "Preview data" closes the editor, so the click looked like it only collapsed the card. + * Both stations must therefore mount the summary as a sibling of the editor body. + */ + it('renders both previews outside the folded station editor', () => { + configureRawSummary(); + + component.goToSection('filters'); + fixture.detectChanges(); + (workflowSection('Filtering').querySelector('.station-action-preview') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const raw = workflowSection('Raw Data Summary'); + expect(raw.closest('.workflow-section-body')).toBeNull(); + expect(getComputedStyle(raw.querySelector('.workflow-section-body') as HTMLElement).display).not.toBe('none'); + + component.goToSection('setup'); + fixture.detectChanges(); + (workflowSection('Preprocessing').querySelector('.station-action-preview') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const processed = workflowSection('Processed Data Summary'); + const editorBody = workflowSection('Preprocessing') + .querySelector('.pipeline-node-body > .workflow-section-body') as HTMLElement; + expect(component.sectionOpen().setup).toBe(false); + expect(editorBody).toBeTruthy(); + expect(editorBody.contains(processed)).toBe(false); + expect(processed.closest('.workflow-section-body')).toBeNull(); + expect(getComputedStyle(processed.querySelector('.workflow-section-body') as HTMLElement).display).not.toBe('none'); + }); + + /** + * Close on a data preview reopens the station editor without writing the request. + */ + it('closes the processed preview back to the preprocessing editor without writing', () => { + configureRawSummary(); + component.goToSection('setup'); + fixture.detectChanges(); + + const preprocessing = workflowSection('Preprocessing'); + (preprocessing.querySelector('.station-action-preview') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const processed = workflowSection('Processed Data Summary'); + const close = processed.querySelector('.station-preview-close') as HTMLButtonElement; + expect(close.textContent?.trim()).toBe('Close'); + expect(processed.querySelectorAll('.station-preview-toggle').length).toBe(1); + + const persistCalls = mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length; + close.click(); + fixture.detectChanges(); + + expect(component.sectionOpen().processed).toBe(false); + expect(component.sectionOpen().setup).toBe(true); + expect(mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length).toBe(persistCalls); + }); + + it('closes the raw preview back to the Filtering editor without writing', () => { + configureRawSummary(); + component.goToSection('filters'); + fixture.detectChanges(); + + (workflowSection('Filtering').querySelector('.station-action-preview') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const raw = workflowSection('Raw Data Summary'); + const close = raw.querySelector('.station-preview-close') as HTMLButtonElement; + expect(close.textContent?.trim()).toBe('Close'); + expect(raw.querySelectorAll('.station-preview-toggle').length).toBe(1); + + const persistCalls = mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length; + close.click(); + fixture.detectChanges(); + + expect(component.sectionOpen().raw).toBe(false); + expect(component.sectionOpen().filters).toBe(true); + expect(mockExpService.setAppliedDescriptivePreprocessing.mock.calls.length).toBe(persistCalls); + }); + + it('keeps category counts on the rule rows and has no preview button', () => { + configureRawSummary(); + component.goToSection('transformation'); + // The stage opens on its type chooser: the editor, and its preview, live in the card. + component.chooseTransformation('categorical'); + fixture.detectChanges(); + + const transformation = workflowSection('Transformation'); + expect(transformation.querySelector('.transformation-tabs')).toBeNull(); + expect(transformation.querySelector('.station-card-footer .station-action-preview')).toBeNull(); + expect(transformation.querySelector('.station-card-footer .station-action-apply')).toBeTruthy(); + }); + + it('renders preprocessing step documentation from backend algorithm metadata', () => { + (mockExpService.backendAlgorithms as any).set({ + linear_regression: { + preprocessing: [ + { name: 'missing_values_handler', documentation: 'Missing docs.\nSecond line.' }, + { name: 'outlier_winsorizer', documentation: 'Outlier docs.' }, + ], + }, + }); + + openStation('setup'); + component.enableOutlierHandling(); + fixture.detectChanges(); + + const before = (fixture.nativeElement as HTMLElement).textContent ?? ''; + expect(before).toContain('How this works'); + expect(before).not.toContain('Missing docs.'); + + const toggles = Array.from( + fixture.nativeElement.querySelectorAll('.preprocessing-doc-toggle') + ) as HTMLButtonElement[]; + expect(toggles.length).toBe(2); + toggles.forEach((toggle) => toggle.click()); + fixture.detectChanges(); + + const text = (fixture.nativeElement as HTMLElement).textContent ?? ''; + expect(text).toContain('Missing docs.'); + expect(text).toContain('Second line.'); + expect(text).toContain('Outlier docs.'); + }); + + it('does not fetch descriptive stats until a model and dataset are ready', () => { + (mockExpService.selectedDataModel as any).set(null); + (mockExpService.selectedDatasets as any).set([]); + (mockExpService.selectedVariables as any).set([{ code: 'age', label: 'Age', type: 'continuous' }]); + + fixture.detectChanges(); + + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + expect(component.isLoading).toBe(false); + }); + it('should treat variable as nominal if counts are present in response, even if metadata type is missing', () => { // 1. Setup metadata (variable without nominal type) const variable = { code: 'biol_sex', label: 'Biological Sex', type: 'unknown' }; @@ -49,7 +550,7 @@ describe('StatisticAnalysisPanelComponent', () => { // 2. Setup response with counts const response = { result: { - variable_based: [ + featurewise: [ { variable: 'biol_sex', data: { @@ -60,52 +561,3184 @@ describe('StatisticAnalysisPanelComponent', () => { } } ], - model_based: [] } }; - mockExpService.loadDescriptiveOverview.and.returnValue(of(response)); + mockExpService.loadDescriptiveOverview.mockReturnValue(of(response)); // 3. Trigger fetch component.fetchDescriptiveStatistics(); // 4. Verify categorization // Should be in nominalVariables - expect(component.nominalVariables.find(v => v.code === 'biol_sex')).toBeTruthy(); + expect(component.rawSummary.nominalVariables.find(v => v.code === 'biol_sex')).toBeTruthy(); // Should NOT be in nonNominalVariables - expect(component.nonNominalVariables.find(v => v.code === 'biol_sex')).toBeFalsy(); + expect(component.rawSummary.nonNominalVariables.find(v => v.code === 'biol_sex')).toBeFalsy(); // Charts should be built for nominal - expect(component.chartsForNominal.length).toBe(1); + expect(component.rawSummary.chartsForNominal.length).toBe(1); }); it('should treat variable as nominal if metadata type is nominal', () => { const variable = { code: 'var_nom', type: 'nominal' }; (mockExpService.selectedVariables as any).set([variable]); - const response = { result: { variable_based: [], model_based: [] } }; // Empty data - mockExpService.loadDescriptiveOverview.and.returnValue(of(response)); + const response = { result: { featurewise: [] } }; // Empty data + mockExpService.loadDescriptiveOverview.mockReturnValue(of(response)); component.fetchDescriptiveStatistics(); - expect(component.nominalVariables.find(v => v.code === 'var_nom')).toBeTruthy(); + expect(component.rawSummary.nominalVariables.find(v => v.code === 'var_nom')).toBeTruthy(); }); it('should treat variable as numeric if no counts and not nominal type', () => { - const variable = { code: 'age', type: 'real' }; + const variable = { code: 'age', type: 'continuous' }; (mockExpService.selectedVariables as any).set([variable]); const response = { result: { - variable_based: [ + featurewise: [ { variable: 'age', data: { mean: 30, std: 5 } } // No counts ], - model_based: [] } }; - mockExpService.loadDescriptiveOverview.and.returnValue(of(response)); + mockExpService.loadDescriptiveOverview.mockReturnValue(of(response)); component.fetchDescriptiveStatistics(); - expect(component.nonNominalVariables.find(v => v.code === 'age')).toBeTruthy(); - expect(component.nominalVariables.find(v => v.code === 'age')).toBeFalsy(); + expect(component.rawSummary.nonNominalVariables.find(v => v.code === 'age')).toBeTruthy(); + expect(component.rawSummary.nominalVariables.find(v => v.code === 'age')).toBeFalsy(); + }); + + it('renders the source snapshot ahead of the pipeline stages without a model tab', () => { + fixture.detectChanges(); + + // The sub-tab buttons moved to the studio stepper; the panel now + // exposes only the pipeline nodes, located by data-guide anchors. + // Step 0 leads the flow and owns its preview: the source summary is + // nested inside the source node. The raw and processed summaries are + // nested in their owning nodes, so they only appear once those steps + // are added (e.g. after opening Filtering or Preprocessing). + const guides = (Array.from( + fixture.nativeElement.querySelectorAll('.workflow-section, .pipeline-node') + ) as HTMLElement[]).map((section) => section.getAttribute('data-guide')); + expect(guides).toEqual([ + 'analysis-source-data', + 'analysis-source-summary', + 'analysis-filtering', + 'analysis-preprocessing', + 'analysis-transformation', + ]); + + const buttonLabels = (Array.from( + fixture.nativeElement.querySelectorAll('button') + ) as HTMLButtonElement[]).map((button) => button.textContent?.trim()); + expect(buttonLabels).not.toContain('Model'); + }); + + it('shows longitudinal preprocessing only for longitudinal data models', () => { + (mockExpService.selectedDataModel as any).set({ + code: 'longitudinal_dm', + label: 'Longitudinal Data Model', + longitudinal: true, + variables: [ + { + code: 'visitid', + label: 'Visit ID', + enumerations: [ + { code: 'BL', label: 'Baseline' }, + { code: 'FL1', label: 'Follow-up 1' }, + ], + }, + ], + }); + (mockExpService.selectedVariables as any).set([ + { code: 'age', label: 'Age', type: 'continuous' }, + ]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + + openStation('setup'); + expect(fixture.nativeElement.textContent).toContain('Required for longitudinal pathologies'); + expect(fixture.nativeElement.textContent).toContain('Longitudinal Transformation'); + expect(fixture.nativeElement.textContent).toContain('Longitudinal strategy'); + expect(fixture.nativeElement.textContent).toContain('Diff (Visit 2 - Visit 1)'); + expect(fixture.nativeElement.textContent).not.toContain('Enable longitudinal transformation'); + + (mockExpService.selectedDataModel as any).set({ code: 'dm', label: 'Data Model', longitudinal: false }); + fixture.detectChanges(); + expect(fixture.nativeElement.textContent).not.toContain('Longitudinal Transformation'); + expect(fixture.nativeElement.textContent).not.toContain('Longitudinal strategy'); + }); + + it('filters preprocessing rows by variable search', () => { + (mockExpService.selectedVariables as any).set([ + { code: 'age', label: 'Age', type: 'continuous' }, + { code: 'biol_sex', label: 'Biological Sex', type: 'nominal' }, + ]); + component.prepSearch.missing = 'age'; + + expect(component.filteredPrepVariables('missing').map((v) => v.code)).toEqual(['age']); + }); + + it('shows the same empty state for longitudinal preprocessing when search has no matches', () => { + (mockExpService.selectedDataModel as any).set({ + code: 'longitudinal_dm', + label: 'Longitudinal Pathology', + longitudinal: true, + variables: [ + { + code: 'visitid', + label: 'Visit ID', + enumerations: [ + { code: 'BL', label: 'Baseline' }, + { code: 'FL1', label: 'Follow-up 1' }, + ], + }, + ], + }); + (mockExpService.selectedVariables as any).set([ + { code: 'age', label: 'Age', type: 'continuous' }, + ]); + component.prepSearch.missing = 'no-match'; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + + const preprocessingSection = openStation('setup'); + expect(preprocessingSection.querySelector('.empty-state-block')).toBeTruthy(); + expect(preprocessingSection.textContent).toContain('No variables match the current search.'); + }); + + it('renders missing values as a selected-variable workspace', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + + const preprocessingSection = openStation('setup'); + const missingStep = (Array.from(preprocessingSection.querySelectorAll('.station-card')) as HTMLElement[]) + .find((step) => step.textContent?.includes('Missing Values')) as HTMLElement; + + expect(missingStep.querySelector('.preprocessing-browser')).toBeTruthy(); + expect(missingStep.querySelector('.preprocessing-detail-panel')).toBeTruthy(); + expect(missingStep.querySelector('.preprocessing-detail-panel h4')?.textContent).toContain('Age'); + expect(missingStep.textContent).toContain('Missing value handling'); + }); + + it('gives Filtering, Preprocessing and Transformation the same station anatomy', () => { + fixture.detectChanges(); + + openStation('filters'); + openStation('setup'); + openStation('transformation'); + // The stage opens on its type chooser, so a column card exists only once a type is chosen. + component.chooseTransformation('categorical'); + fixture.detectChanges(); + + // Filtering and Preprocessing keep a preview action. A categorical column does not: + // its counts already sit on each rule row. + for (const title of ['Filtering', 'Preprocessing', 'Transformation']) { + const node = workflowSection(title); + expect(node.querySelector('h3')?.textContent?.trim()).toBeTruthy(); + expect(node.querySelector('.station-card')).toBeTruthy(); + } + expect(workflowSection('Filtering').querySelectorAll('.station-action-bar:has(.station-action-preview)').length).toBe(1); + expect(workflowSection('Preprocessing').querySelectorAll('.station-action-bar:has(.station-action-preview)').length).toBe(1); + expect(workflowSection('Transformation').querySelector('.station-card-footer .station-action-preview')).toBeNull(); + + // The K-means station is opened from the chooser's dashed row, like the categorical draft. + const addKmeans = Array.from( + workflowSection('Transformation').querySelectorAll('.transformation-chooser .pipeline-subnode-content') + ).find((button) => button.textContent?.includes('K-means')) as HTMLButtonElement; + addKmeans.click(); + fixture.detectChanges(); + + // Titled cards keep the shared anatomy: icon tile + collapsible header. + const titled = (Array.from( + (fixture.nativeElement as HTMLElement).querySelectorAll('.pipeline-node .station-card') + ) as HTMLElement[]).filter((card) => card.querySelector('.station-card-title')?.textContent?.trim()); + expect(titled.map((card) => card.querySelector('.station-card-title')?.textContent?.trim())).toEqual([ + 'Missing Values', + 'Nominal column', + 'K-means cluster column', + ]); + for (const card of titled) { + expect(card.querySelector('.station-card-icon i')).toBeTruthy(); + expect(card.querySelector('button.station-card-header[aria-expanded]')).toBeTruthy(); + } + }); + + it('keeps the preprocessing and transformation stations collapsible', () => { + fixture.detectChanges(); + + // Filtering is the only static station; the other four toggle through their header. + openStation('setup'); + openStation('transformation'); + component.enableOutlierHandling(); + component.chooseTransformation('kmeans'); + // The column card joins the graph only after its type is chosen. + component.chooseTransformation('categorical'); + fixture.detectChanges(); + + const headers = Array.from((fixture.nativeElement as HTMLElement).querySelectorAll<HTMLElement>('.pipeline-node .station-card-header')); + expect(headers.filter((header) => header.tagName === 'BUTTON' && header.hasAttribute('aria-expanded')).length).toBe(4); + + const missingHeader = headers.find((header) => header.textContent?.includes('Missing Values')) as HTMLButtonElement; + expect(missingHeader.getAttribute('aria-expanded')).toBe('true'); + + missingHeader.click(); + fixture.detectChanges(); + + expect(component.preprocessingStepOpen.missing).toBe(false); + expect(missingHeader.getAttribute('aria-expanded')).toBe('false'); + }); + + it('updates preprocessing detail controls when a variable is selected', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + + const preprocessingSection = openStation('setup'); + const sexButton = (Array.from(preprocessingSection.querySelectorAll('.preprocessing-variable-btn')) as HTMLButtonElement[]) + .find((button) => button.textContent?.includes('Sex')); + sexButton?.click(); + fixture.detectChanges(); + + expect(component.selectedPrepVariable('missing')?.code).toBe('sex'); + expect(preprocessingSection.querySelector('.preprocessing-detail-panel h4')?.textContent).toContain('Sex'); + }); + + it('uses independent selected variables for missing values and longitudinal transformation', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + (mockExpService.selectedDataModel as any).set({ + code: 'longitudinal_dm', + label: 'Longitudinal Data Model', + longitudinal: true, + variables: [ + { + code: 'visitid', + label: 'Visit ID', + enumerations: [ + { code: 'BL', label: 'Baseline' }, + { code: 'FL1', label: 'Follow-up 1' }, + ], + }, + ], + }); + fixture.detectChanges(); + + component.selectPrepVariable('missing', sex); + component.selectPrepVariable('longitudinal', age); + fixture.detectChanges(); + + expect(component.selectedPrepVariable('missing')?.code).toBe('sex'); + expect(component.selectedPrepVariable('longitudinal')?.code).toBe('age'); + }); + + it('counts pending preprocessing by step instead of by variable rule', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + (mockExpService.selectedDataModel as any).set({ + code: 'longitudinal_dm', + label: 'Longitudinal Data Model', + longitudinal: true, + variables: [ + { + code: 'visitid', + label: 'Visit ID', + enumerations: [ + { code: 'BL', label: 'Baseline' }, + { code: 'FL1', label: 'Follow-up 1' }, + ], + }, + ], + }); + fixture.detectChanges(); + + // Missing Values defaults (NA removal) are implicit, so they don't + // contribute to the pending count. Only the longitudinal step counts. + openStation('setup'); + expect(component.pendingChangeCount).toBe(1); + expect(fixture.nativeElement.textContent).toContain('1 change'); + }); + + it('shows category choices for categorical constant preprocessing values', () => { + const variable = { + code: 'pad', + label: 'PAD', + type: 'nominal', + enumerations: [ + { code: '0', label: 'No' }, + { code: '1', label: 'Yes' }, + ], + }; + (mockExpService.selectedVariables as any).set([variable]); + component.onMissingActionChange(variable, 'constant'); + + const preprocessingSection = openStation('setup'); + const options = Array.from(preprocessingSection.querySelectorAll('.preprocessing-detail-panel option')) as HTMLOptionElement[]; + + expect(options.map((option) => option.textContent?.trim())).toContain('No'); + expect(options.map((option) => option.textContent?.trim())).toContain('Yes'); + }); + + it('splits preprocessing rows by default, applied and pending state', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + component.appliedPreprocessingRules = { + age: { variableCode: 'age', action: 'drop', value: '', enabled: true }, + }; + component.pendingPreprocessingRules = { + age: { variableCode: 'age', action: 'drop', value: '', enabled: true }, + }; + + const pendingGroup = component.prepGroups('missing').find((group) => group.key === 'pending'); + const appliedGroup = component.prepGroups('missing').find((group) => group.key === 'applied'); + const defaultGroup = component.prepGroups('missing').find((group) => group.key === 'default'); + + // `sex` was just added and sits on the implicit NA-removal default. + expect(pendingGroup).toBeUndefined(); + expect(defaultGroup?.variables.map((v) => v.code)).toEqual(['sex']); + expect(appliedGroup?.variables.map((v) => v.code)).toEqual(['age']); + }); + + it('keeps preprocessing rules when variables are removed and re-added', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'continuous' }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + component.pendingPreprocessingRules = { + age: { variableCode: 'age', action: 'drop', value: '', enabled: true }, + }; + component.appliedPreprocessingRules = { + age: { variableCode: 'age', action: 'drop', value: '', enabled: true }, + }; + + (mockExpService.selectedVariables as any).set([sex]); + fixture.detectChanges(); + // Dropping `age` from the selection must not drop its applied rule. + expect(component.appliedPreprocessingRules['age']?.action).toBe('drop'); + expect(Object.keys(component.appliedPreprocessingRules)).toEqual(['age']); + + (mockExpService.selectedVariables as any).set([age, sex]); + fixture.detectChanges(); + + const appliedGroup = component.prepGroups('missing').find((group) => group.key === 'applied'); + const defaultGroup = component.prepGroups('missing').find((group) => group.key === 'default'); + // `age` comes back as applied; `sex` was never applied and sits on the + // request-time NA-removal default (b0899cf removed the auto-persist). + expect(appliedGroup?.variables.map((v) => v.code)).toEqual(['age']); + expect(defaultGroup?.variables.map((v) => v.code)).toEqual(['sex']); + }); + + it('keeps the default NA removal in memory when variables are first selected', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + (mockExpService.selectedVariables as any).set([age, sex]); + fixture.detectChanges(); + + // The implicit default is shown as a default: algorithm runs get drop from + // resolveRequestPreprocessing, so it is not persisted work. + expect(component.prepVariableStateLabel('missing',age)).toBe('Default'); + expect(component.prepVariableStateLabel('missing',sex)).toBe('Default'); + expect(component.pendingChangeCount).toBe(0); + const persisted = mockExpService.setAppliedDescriptivePreprocessing.mock.calls + .map(([config]) => config) + .filter((config) => config !== null); + expect(persisted).toEqual([]); + expect(component.isStepAdded('setup')).toBe(false); + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalled(); + expect(component.preprocessingStatus).toBe('none'); + expect((component as any).userPreprocessingApplied).toBe(false); + }); + + it('reports applied status only after a real apply', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + // The bare set spy does not round-trip the persisted config; wire it up + // so the post-apply reconcile hydrates from the applied config (as the + // real service does) instead of treating it as a fresh session. + mockExpService.setAppliedDescriptivePreprocessing.mockImplementation((config: unknown) => { + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(config as never); + }); + (mockExpService.selectedVariables as any).set([age]); + component.onMissingActionChange(age, 'mean'); + expect(component.preprocessingStatus).not.toBe('applied'); + + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + component.applyPreprocessing(); + fixture.detectChanges(); + + expect((component as any).userPreprocessingApplied).toBe(true); + expect(component.preprocessingStatus).toBe('applied'); + }); + + it('covers variables added after applied preprocessing with the inherited NA removal default', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + const preprocessing = { + missing_values_handler: { + strategies: { + age: 'drop', + }, + }, + }; + (mockExpService.appliedPreprocessingConfig as any).set(preprocessing); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(preprocessing); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + expect(component.prepVariableStateLabel('missing',age)).toBe('Applied'); + expect(component.pendingChangeCount).toBe(0); + + (mockExpService.selectedVariables as any).set([age, sex]); + fixture.detectChanges(); + + const defaultGroup = component.prepGroups('missing').find((group) => group.key === 'default'); + // The NA drop for a newly added variable is injected per request, not stored, so the + // honest label is Default: covered by the inherited default, owed nothing, pending nothing. + expect(component.prepVariableStateLabel('missing',sex)).toBe('Default'); + expect(component.prepVariableHasPendingChange('missing', sex)).toBe(false); + expect(component.pendingChangeCount).toBe(0); + expect(defaultGroup?.variables.map((v) => v.code)).toEqual(['sex']); + }); + + it('creates pending state when a missing value action changes', () => { + const variable = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([variable]); + component.onMissingActionChange(variable, 'mean'); + + expect(component.pendingChangeCount).toBe(1); + expect(component.preprocessingStatus).toBe('pending'); + }); + + it('validates constant missing value rules before apply', () => { + const variable = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([variable]); + component.onMissingActionChange(variable, 'constant'); + + component.applyPreprocessing(); + + expect(component.preprocessingValidationErrors['age']).toBe('A constant value is required.'); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + }); + + it('previews the processed data with pending, unapplied preprocessing changes', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + // Pending (not yet applied) missing-value strategy. + component.onMissingActionChange(age, 'mean'); + expect(component.preprocessingStatus).toBe('pending'); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + + // Opening the processed preview uses the pending rules, not the applied config. + component.goToSection('processed'); + fixture.detectChanges(); + + expect(component.sectionOpen().processed).toBe(true); + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith( + ['age'], + { missing_values_handler: { strategies: { age: 'mean' } } } + ); + }); + + it('refreshes the open processed preview when pending preprocessing changes', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + component.onMissingActionChange(age, 'mean'); + component.goToSection('processed'); + fixture.detectChanges(); + + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith( + ['age'], + { missing_values_handler: { strategies: { age: 'mean' } } } + ); + + mockExpService.loadDescriptiveOverview.mockClear(); + component.onMissingActionChange(age, 'median'); + fixture.detectChanges(); + + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith( + ['age'], + { missing_values_handler: { strategies: { age: 'median' } } } + ); + }); + + it('deduplicates in-flight processed preview requests and keeps the newest response', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + component.onMissingActionChange(age, 'mean'); + + const first = new Subject<any>(); + mockExpService.loadDescriptiveOverview.mockReturnValue(first.asObservable()); + (component as any).fetchProcessedPreview(); + + // Same pending config: the request is already in flight, so it must not fan out. + (component as any).fetchProcessedPreview(); + expect(mockExpService.loadDescriptiveOverview.mock.calls.length).toBe(1); + + component.onMissingActionChange(age, 'median'); + const second = new Subject<any>(); + mockExpService.loadDescriptiveOverview.mockReturnValue(second.asObservable()); + (component as any).fetchProcessedPreview(); + expect(mockExpService.loadDescriptiveOverview.mock.calls.length).toBe(2); + + second.next({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 82 } }, + ], + }, + }); + first.next({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + ], + }, + }); + fixture.detectChanges(); + + expect(component.processedSummary.featurewiseRows[0].data.mean).toBe(82); + + first.complete(); + second.complete(); + }); + + it('supersedes an in-flight source preview when its selection changes', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + const stale = new Subject<any>(); + mockExpService.loadDescriptiveOverview.mockReturnValue(stale.asObservable()); + component.fetchSourceSummary(); + + (mockExpService.selectedVariables as any).set([age, sex]); + const current = new Subject<any>(); + mockExpService.loadDescriptiveOverview.mockReturnValue(current.asObservable()); + component.fetchSourceSummary(); + + current.next({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 82 } }, + ], + }, + }); + stale.next({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + ], + }, + }); + fixture.detectChanges(); + + expect(component.sourceSummary.featurewiseRows.length).toBe(1); + expect(component.sourceSummary.featurewiseRows[0].data.mean).toBe(82); + expect(component.sourceSummary.isLoading).toBe(false); + + stale.complete(); + current.complete(); + }); + + it('selects the first processed statistic after preprocessing is applied', () => { + const variable = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([variable]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'age', + data: { + num_dtps: 10, + num_na: 0, + num_total: 10, + mean: 71, + }, + }, + ], + }, + })); + + component.onMissingActionChange(variable, 'mean'); + component.applyPreprocessing(); + + // Apply saves and closes the station: nothing is handed to the next one, and the + // summary it computed is still selected for the trip Preview data makes. + expect(component.sectionOpen().setup).toBe(false); + expect(component.sectionOpen().processed).toBe(false); + expect(component.sectionOpen().transformation).toBe(false); + expect(component.selectedStatisticBlock('processed')?.name).toBe('Age'); + }); + + it('opens and scrolls to the processed summary while preprocessing is loading', () => new Promise<void>((done) => { + const { age } = configureRawSummary(); + const response$ = new Subject<unknown>(); + const scrollSpy = vi.spyOn(Element.prototype, 'scrollIntoView').mockReturnValue(undefined); + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback): number => { + void Promise.resolve().then(() => callback(0)); + return 0; + }); + mockExpService.loadDescriptiveOverview.mockReturnValue(response$.asObservable()); + + component.onMissingActionChange(age, 'mean'); + component.applyPreprocessing(); + fixture.detectChanges(); + + const processedSection = workflowSection('Processed Data Summary'); + const processedBody = processedSection.querySelector('.workflow-section-body.open') as HTMLElement; + expect(component.sectionOpen().processed).toBe(true); + expect(component.processedSummary.isLoading).toBe(true); + expect(processedSection.querySelector('[aria-label="Processed data summary loading"]')).toBeTruthy(); + expect(processedBody).toBeTruthy(); + + setTimeout(() => { + expect(scrollSpy).toHaveBeenCalledWith({ behavior: 'smooth', block: 'start' }); + + response$.next({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'age', + data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 }, + }, + ], + }, + }); + response$.complete(); + fixture.detectChanges(); + + // Apply folded the station instead of opening the next one; the summary is + // still built and selected, and Preview data is what shows it. + expect(component.sectionOpen().setup).toBe(false); + expect(component.sectionOpen().processed).toBe(false); + expect(component.sectionOpen().transformation).toBe(false); + expect(component.processedSummary.isLoading).toBe(false); + expect(component.selectedStatisticBlock('processed')?.name).toBe('Age'); + + (mockExpService.appliedPreprocessingConfig as any).set({ + missing_values_handler: { + strategies: { age: 'mean' }, + }, + }); + fixture.detectChanges(); + // A later config write invalidates the workspace the same way, without + // losing the statistic selection built for it. + expect(component.sectionOpen().processed).toBe(false); + expect(component.selectedStatisticBlock('processed')?.name).toBe('Age'); + done(); + }); + })); + + it('scrolls the workflow subsection into view', () => new Promise<void>((done) => { + configureRawSummary(); + const scrollSpy = vi.spyOn(Element.prototype, 'scrollIntoView').mockReturnValue(undefined); + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback): number => { + void Promise.resolve().then(() => callback(0)); + return 0; + }); + + component.goToSection('filters'); + fixture.detectChanges(); + + setTimeout(() => { + expect(scrollSpy).toHaveBeenCalledWith({ behavior: 'smooth', block: 'start' }); + done(); + }); + })); + + it('keeps the processed summary open and stops the spinner when preprocessing fails', () => { + const { age } = configureRawSummary(); + const response$ = new Subject<unknown>(); + vi.spyOn(console, 'error').mockReturnValue(undefined); + mockExpService.loadDescriptiveOverview.mockReturnValue(response$.asObservable()); + + component.onMissingActionChange(age, 'mean'); + component.applyPreprocessing(); + fixture.detectChanges(); + + expect(component.sectionOpen().processed).toBe(true); + expect(component.processedSummary.isLoading).toBe(true); + + response$.error(new Error('preprocessing failed')); + fixture.detectChanges(); + + const processedSection = workflowSection('Processed Data Summary'); + expect(component.sectionOpen().processed).toBe(true); + expect(component.processedSummary.isLoading).toBe(false); + expect(processedSection.textContent).not.toContain('Processed data summary loading'); + }); + + it('renders raw statistics as a grouped analysis workspace', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'age', + data: { + num_dtps: 10, + num_na: 0, + num_total: 10, + mean: 71, + }, + }, + { + dataset: 'all datasets', + variable: 'sex', + data: { + counts: { female: 6, male: 4 }, + num_dtps: 10, + num_na: 1, + num_total: 11, + }, + }, + ], + }, + })); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + + const workspace = fixture.nativeElement.querySelector('.statistics-workspace') as HTMLElement; + const text = workspace.textContent ?? ''; + + expect(workspace).toBeTruthy(); + expect(text).toContain('Numerical'); + expect(text).toContain('Categorical'); + expect(text).toContain('Age'); + expect(text).toContain('Sex'); + expect(text).toContain('Export'); + const exportTrigger = workspace.querySelector('.summary-export-group .studio-export-btn') as HTMLButtonElement; + expect(exportTrigger).toBeTruthy(); + exportTrigger.click(); + fixture.detectChanges(); + const exportMenu = workspace.querySelector('.studio-export-menu') as HTMLElement; + expect(exportMenu).toBeTruthy(); + expect(exportMenu.textContent).toContain('PDF'); + expect(exportMenu.textContent).toContain('CSV'); + const variableButtons = Array.from(workspace.querySelectorAll('.statistics-variable-btn')) as HTMLButtonElement[]; + const ageButton = variableButtons.find((button) => button.textContent?.includes('Age')); + const ageButtonText = ageButton?.textContent ?? ''; + // Deliberate change from "10 datapoints · 0 missing": the rail is a column + // you compare top to bottom, and absolute counts are not comparable across + // variables of different size. The share is, and it fits one line in the + // 300px rail; the counts stay in the row's tooltip. + expect(ageButtonText).toContain('0%'); + expect(ageButton?.querySelector('.statistics-variable-coverage')?.getAttribute('title')).toContain('of 10'); + expect(ageButtonText).not.toContain('Numerical'); + + // The bar puts the missing share under the name; 0% still shows the track. + const bars = Array.from(workspace.querySelectorAll('.statistics-variable-bar')) as HTMLElement[]; + expect(bars.length).toBe(2); + expect(bars[0].querySelector('span')?.getAttribute('style')).toContain('2%'); + expect(bars[0].getAttribute('data-severity')).toBe('none'); + expect(bars[1].querySelector('span')?.getAttribute('style')).toContain('90.9091%'); + expect(bars[1].getAttribute('data-severity')).toBe('none'); + + // The Total row carries n; the dataset header does not repeat it. + expect(workspace.querySelector('.statistics-table th small')).toBeNull(); + }); + + it('keeps CSV to the Table tab and opens PDF from the merged Export menu', () => { + configureRawSummary(); + component.addStep('filters'); + fixture.detectChanges(); + + const rawSection = workflowSection('Raw Data Summary'); + const trigger = rawSection.querySelector('.summary-export-group .studio-export-btn') as HTMLButtonElement; + // The tour's analysis-export spotlight rides the merged Export control. + expect(trigger.getAttribute('data-guide')).toBe('analysis-export'); + trigger.click(); + fixture.detectChanges(); + expect(rawSection.querySelector('.studio-export-menu')?.textContent).toContain('CSV'); + expect(rawSection.querySelector('.studio-export-menu')?.textContent).toContain('PDF'); + + component.setSummaryTab('raw', 'Charts'); + fixture.detectChanges(); + expect(component.summaryExportMenuKind()).toBeNull(); + trigger.click(); + fixture.detectChanges(); + const menuText = rawSection.querySelector('.studio-export-menu')?.textContent ?? ''; + expect(menuText).toContain('PDF'); + expect(menuText).not.toContain('CSV'); + }); + + it('pools the rail foot over counts rather than averaging the row shares', () => { + configureRawSummary(); + component.fetchDescriptiveStatistics(); + fixture.detectChanges(); + + const pooled = component.selectionCoverage('raw'); + expect(pooled?.variables).toBe(2); + expect(pooled?.total).toBe(21); + expect(pooled?.missing).toBe(1); + // Re-pooled: 1 of 21. Averaging the two row shares (0% and 9.1%) would + // report 4.6%, a number that describes no dataset in the selection. + expect(component.coveragePercent(pooled!)).toBe('4.8%'); + }); + + it('marks only numeric summary tables for the compact no-scroll layout', () => { + const { age } = configureRawSummary(); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + + const rawSection = workflowSection('Raw Data Summary'); + const rawWrapper = rawSection.querySelector('.statistics-table-wrapper') as HTMLElement; + expect(rawWrapper.classList.contains('statistics-table-wrapper--numeric')).toBe(true); + + component.onMissingActionChange(age, 'mean'); + component.applyPreprocessing(); + fixture.detectChanges(); + + const processedWrapper = workflowSection('Processed Data Summary').querySelector('.statistics-table-wrapper') as HTMLElement; + expect(processedWrapper.classList.contains('statistics-table-wrapper--numeric')).toBe(true); + + const sexButton = (Array.from(workflowSection('Raw Data Summary').querySelectorAll('.statistics-variable-btn')) as HTMLButtonElement[]) + .find((button) => button.textContent?.includes('Sex')); + sexButton?.click(); + fixture.detectChanges(); + + const nominalWrapper = workflowSection('Raw Data Summary').querySelector('.statistics-table-wrapper') as HTMLElement; + expect(nominalWrapper.classList.contains('statistics-table-wrapper--numeric')).toBe(false); + }); + + it('filters raw statistics browser by variable label or code', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + { dataset: 'all datasets', variable: 'sex', data: { counts: { female: 6 }, num_dtps: 6, num_na: 0, num_total: 6 } }, + ], + }, + })); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + + const search = fixture.nativeElement.querySelector('input[aria-label="Search raw data summary variables"]') as HTMLInputElement; + search.value = 'sex'; + search.dispatchEvent(new Event('input')); + fixture.detectChanges(); + + expect(component.filteredStatisticBlocks('raw').map((block) => block.name)).toEqual(['Sex']); + expect(component.selectedStatisticBlock('raw')?.name).toBe('Sex'); + const text = fixture.nativeElement.querySelector('.statistics-workspace')?.textContent ?? ''; + expect(text).toContain('Sex'); + expect(text).not.toContain('Age'); + }); + + it('updates the statistics detail panel when a variable is selected', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + { dataset: 'all datasets', variable: 'sex', data: { counts: { female: 6 }, num_dtps: 6, num_na: 0, num_total: 6 } }, + ], + }, + })); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + + const buttons = Array.from(fixture.nativeElement.querySelectorAll('.statistics-variable-btn')) as HTMLButtonElement[]; + const sexButton = buttons.find((button) => button.textContent?.includes('Sex')); + sexButton?.click(); + fixture.detectChanges(); + + expect(component.selectedStatisticBlock('raw')?.name).toBe('Sex'); + expect(fixture.nativeElement.querySelector('.summary-detail-toolbar h4')?.textContent).toContain('Sex'); + }); + + it('keeps the raw variable browser visible when the right panel switches to charts', () => { + configureRawSummary([{ title: { text: 'Age chart' }, series: [] }]); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + + component.setSummaryTab('raw', 'Charts'); + fixture.detectChanges(); + + const rawSection = workflowSection('Raw Data Summary'); + expect(rawSection.querySelectorAll('.statistics-browser').length).toBe(1); + expect(rawSection.querySelector('.chart-browser')).toBeNull(); + expect(rawSection.querySelector('.statistics-browser')?.textContent).toContain('Age'); + expect(rawSection.querySelector('.summary-detail-toolbar h4')?.textContent).toContain('Age'); + expect(rawSection.querySelector('app-chart-renderer')).toBeTruthy(); + }); + + it('preserves the selected raw variable across Statistics and Charts tabs', () => { + configureRawSummary(); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + + const buttons = Array.from(fixture.nativeElement.querySelectorAll('.statistics-variable-btn')) as HTMLButtonElement[]; + buttons.find((button) => button.textContent?.includes('Sex'))?.click(); + fixture.detectChanges(); + + component.setSummaryTab('raw', 'Charts'); + fixture.detectChanges(); + expect(component.selectedStatisticBlock('raw')?.name).toBe('Sex'); + expect(workflowSection('Raw Data Summary').querySelector('.summary-detail-toolbar h4')?.textContent).toContain('Sex'); + + component.setSummaryTab('raw', 'Statistics'); + fixture.detectChanges(); + expect(component.selectedStatisticBlock('raw')?.name).toBe('Sex'); + expect(workflowSection('Raw Data Summary').querySelector('.summary-detail-toolbar h4')?.textContent).toContain('Sex'); + }); + + it('shows a chart empty state for a selected variable without chart options', () => { + configureRawSummary([]); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + + component.setSummaryTab('raw', 'Charts'); + fixture.detectChanges(); + + const rawSection = workflowSection('Raw Data Summary'); + expect(component.selectedStatisticBlock('raw')?.name).toBe('Age'); + expect(rawSection.textContent).toContain('No chart available for Age.'); + expect(rawSection.querySelector('.statistics-browser')).toBeTruthy(); + }); + + it('uses the same selected-variable workspace for processed summaries', () => { + const { age } = configureRawSummary([{ title: { text: 'Age chart' }, series: [] }]); + + component.onMissingActionChange(age, 'mean'); + component.applyPreprocessing(); + component.setSummaryTab('processed', 'Charts'); + fixture.detectChanges(); + + const processedSection = workflowSection('Processed Data Summary'); + expect(processedSection.querySelector('.statistics-browser')).toBeTruthy(); + expect(processedSection.querySelector('.chart-browser')).toBeNull(); + expect(component.selectedStatisticBlock('processed')?.name).toBe('Age'); + expect(processedSection.querySelector('.summary-detail-toolbar h4')?.textContent).toContain('Age'); + }); + + it('applies longitudinal preprocessing with visit pair and per-variable strategies', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + (mockExpService.selectedDataModel as any).set({ + code: 'longitudinal_dm', + label: 'Longitudinal Data Model', + longitudinal: true, + variables: [ + { + code: 'visitid', + label: 'Visit ID', + enumerations: [ + { code: 'BL', label: 'Baseline' }, + { code: 'FL1', label: 'Follow-up 1' }, + ], + }, + ], + }); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + + component.applyPreprocessing(); + + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith(['age', 'sex'], { + missing_values_handler: { + strategies: { + age: 'drop', + sex: 'drop', + }, + }, + longitudinal_transformer: { + visit1: 'BL', + visit2: 'FL1', + strategies: { + age: 'diff', + sex: 'first', + }, + }, + }); + expect(mockExpService.setAppliedDescriptivePreprocessing).toHaveBeenCalledWith({ + missing_values_handler: { + strategies: { + age: 'drop', + sex: 'drop', + }, + }, + longitudinal_transformer: { + visit1: 'BL', + visit2: 'FL1', + strategies: { + age: 'diff', + sex: 'first', + }, + }, + }); + }); + + it('applies outlier winsorizer together with missing values for numerical variables only', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + const bmi = { code: 'bmi', label: 'BMI', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age, sex, bmi]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + expect(component.outlierPreprocessingVariables.map((variable) => variable.code)).toEqual(['age', 'bmi']); + + component.onOutlierStrategyChange(age, 'quantile'); + component.onOutlierFoldChange(age, 0.05); + component.onOutlierEnabledChange(bmi, true); + component.applyPreprocessing(); + + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith(['age', 'sex', 'bmi'], { + missing_values_handler: { + strategies: { + age: 'drop', + sex: 'drop', + bmi: 'drop', + }, + }, + outlier_winsorizer: { + strategies: { + age: 'quantile', + bmi: 'iqr', + }, + tails: { + age: 'both', + bmi: 'both', + }, + folds: { + age: 0.05, + bmi: 1.5, + }, + }, + }); + }); + + it('previews pending outlier winsorizer rules with an outlier report before applying', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + mockExpService.loadOutlierReportPreview.mockReturnValue(of({ + result: { + featurewise: [ + { + variable: 'age', + dataset: 'dataset-a', + data: { + strategy: 'iqr', + tail: 'both', + fold: 1.5, + lower_bound: null, + upper_bound: 90, + lower_outlier_count: 0, + upper_outlier_count: 2, + total_outlier_count: 2, + total_outlier_percentage: 0.2, + }, + }, + ], + }, + })); + openStation('setup'); + mockExpService.setAppliedDescriptivePreprocessing.mockClear(); + + component.onOutlierEnabledChange(age, true); + component.previewOutlierReport(); + + expect(mockExpService.loadOutlierReportPreview).toHaveBeenCalledWith( + ['age'], + { + strategies: { age: 'iqr' }, + tails: { age: 'both' }, + folds: { age: 1.5 }, + }, + { + missing_values_handler: { + strategies: { + age: 'drop', + }, + }, + } + ); + expect(mockExpService.setAppliedDescriptivePreprocessing).not.toHaveBeenCalled(); + expect(component.outlierPreviewRows).toEqual([ + expect.objectContaining({ + variable: 'Age', + dataset: 'dataset-a', + strategy: 'IQR', + fold: '1.5', + upperBound: '90', + lowerBound: '-', + lowerOutliers: '0', + totalOutliers: '2', + }), + ]); + + fixture.detectChanges(); + const preview = fixture.nativeElement.querySelector('.outlier-report-section') as HTMLElement; + expect(preview).toBeTruthy(); + expect(preview.textContent).toContain('Values that would be capped'); + expect(preview.textContent).toContain('dataset-a'); + expect(preview.textContent).toContain('2'); + }); + + it('excludes all datasets rows from outlier report preview like descriptive numerical charts', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age]); + mockExpService.loadOutlierReportPreview.mockReturnValue(of({ + result: { + featurewise: [ + { + variable: 'age', + dataset: 'dataset-a', + data: { strategy: 'iqr', tail: 'both', fold: 1.5 }, + }, + { + variable: 'age', + dataset: 'all datasets', + data: { strategy: 'iqr', tail: 'both', fold: 1.5 }, + }, + ], + }, + })); + fixture.detectChanges(); + + component.onOutlierEnabledChange(age, true); + component.previewOutlierReport(); + + expect(component.outlierPreviewRows.map((row) => row.dataset)).toEqual(['dataset-a']); + }); + + it('validates outlier fold boundaries before applying preprocessing', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + component.onOutlierStrategyChange(age, 'quantile'); + component.onOutlierFoldChange(age, 0.5); + component.applyPreprocessing(); + + expect(component.outlierValidationErrors['age']).toBe('Quantile fold must be greater than 0 and less than 0.5.'); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + + component.onOutlierStrategyChange(age, 'iqr'); + component.onOutlierFoldChange(age, 0); + component.applyPreprocessing(); + + expect(component.outlierValidationErrors['age']).toBe('Fold must be greater than 0.'); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + }); + + it('resets missing value and longitudinal pending state together', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + (mockExpService.selectedDataModel as any).set({ + code: 'longitudinal_dm', + label: 'Longitudinal Data Model', + longitudinal: true, + variables: [ + { + code: 'visitid', + label: 'Visit ID', + enumerations: [ + { code: 'BL', label: 'Baseline' }, + { code: 'FL1', label: 'Follow-up 1' }, + ], + }, + ], + }); + component.appliedPreprocessingRules = { + age: { variableCode: 'age', action: 'mean', value: '', enabled: true }, + sex: { variableCode: 'sex', action: 'drop', value: '', enabled: true }, + }; + component.appliedLongitudinalEnabled = true; + component.appliedLongitudinalVisit1 = 'BL'; + component.appliedLongitudinalVisit2 = 'FL1'; + component.appliedLongitudinalStrategies = { age: 'diff', sex: 'first' }; + component.onMissingActionChange(age, 'median'); + component.onLongitudinalStrategyChange(sex, 'second'); + + component.resetChanges(); + + expect(component.ruleFor(age).action).toBe('mean'); + expect(component.longitudinalStrategyFor(sex)).toBe('first'); + expect(component.pendingChangeCount).toBe(0); + }); + + it('hydrates saved preprocessing as applied without pending changes', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + const preprocessing = { + missing_values_handler: { + strategies: { + age: 'median', + sex: 'drop', + }, + }, + }; + (mockExpService.appliedPreprocessingConfig as any).set(preprocessing); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(preprocessing); + (mockExpService.selectedVariables as any).set([age, sex]); + + fixture.detectChanges(); + + expect(component.ruleFor(age).action).toBe('median'); + expect(component.ruleFor(sex).action).toBe('drop'); + expect((component as any).userPreprocessingApplied).toBe(false); + // Internal status stays config-driven; the flag (and the emitted, + // downgraded status) is what keeps the stepper from claiming Done. + expect(component.preprocessingStatus).toBe('applied'); + expect(component.pendingChangeCount).toBe(0); + }); + + it('hydrates saved outlier preprocessing as applied without enabling categorical variables', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + const preprocessing = { + missing_values_handler: { + strategies: { + age: 'drop', + sex: 'drop', + }, + }, + outlier_winsorizer: { + strategies: { + age: 'iqr', + }, + tails: { + age: 'both', + }, + folds: { + age: 1.5, + }, + }, + }; + (mockExpService.appliedPreprocessingConfig as any).set(preprocessing); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(preprocessing); + (mockExpService.selectedVariables as any).set([age, sex]); + + fixture.detectChanges(); + + expect(component.outlierPreprocessingVariables.map((variable) => variable.code)).toEqual(['age']); + expect(component.outlierRuleFor(age).enabled).toBe(true); + expect(component.prepVariableStateLabel('outlier', age)).toBe('Applied'); + // Internal status stays config-driven (outlier config is applied); the + // user-apply flag is what keeps the stepper from claiming Done. + expect((component as any).userPreprocessingApplied).toBe(false); + expect(component.preprocessingStatus).toBe('applied'); + expect(component.pendingChangeCount).toBe(0); + }); + + it('builds processed histogram preview from describe counts without calling histogram_sql', () => { + const aspiration = { + code: 'aspiration', + label: 'Aspiration', + type: 'nominal', + enumerations: [{ code: '0', label: 'No' }, { code: '1', label: 'Yes' }], + }; + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue({ + missing_values_handler: { strategies: { aspiration: 'drop' } }, + }); + (mockExpService.selectedVariables as any).set([aspiration]); + fixture.detectChanges(); + component.preprocessingStatus = 'applied'; + component.processedSummary = (component as any).buildSummaryFromResponse({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'aspiration', + data: { + num_dtps: 21766, + num_na: 0, + num_total: 21766, + counts: { '0': 18000, '1': 3766 }, + }, + }, + ], + }, + }, 'processed'); + component.processedSummary.selectedStatisticKey = 'aspiration'; + fixture.detectChanges(); + + mockExpService.getAlgorithmResults.mockClear(); + component.setSummaryTab('processed', 'Histogram'); + fixture.detectChanges(); + + expect(mockExpService.getAlgorithmResults).not.toHaveBeenCalled(); + const block = component.selectedStatisticBlock('processed'); + expect(block).toBeTruthy(); + expect(component.selectedSummaryHistogramData('processed', block!)).toEqual(expect.objectContaining({ + bins: ['No', 'Yes'], + counts: [18000, 3766], + variableName: 'Aspiration', + })); + }); + + it('reloads the raw histogram preview after filters change while the Histogram tab is active', () => { + const aspiration = { + code: 'aspiration', + label: 'Aspiration', + type: 'nominal', + enumerations: [{ code: '0', label: 'No' }, { code: '1', label: 'Yes' }], + }; + configureRawSummary(); + (mockExpService.selectedVariables as any).set([aspiration]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'aspiration', + data: { + num_dtps: 21766, + num_na: 0, + num_total: 21766, + counts: { '0': 18000, '1': 3766 }, + }, + }, + ], + }, + })); + fixture.detectChanges(); + + component.setSummaryTab('raw', 'Histogram'); + fixture.detectChanges(); + expect(component.rawSummary.activeTab).toBe('Histogram'); + + const filteredLogic = { + condition: 'AND', + rules: [{ field: 'age', operator: 'greater', value: 65 }], + }; + (mockExpService.filterLogic as any).set(filteredLogic); + fixture.detectChanges(); + + const block = component.selectedStatisticBlock('raw'); + expect(component.selectedSummaryHistogramData('raw', block!)).toEqual(expect.objectContaining({ + bins: ['No', 'Yes'], + counts: [18000, 3766], + })); + }); + + it('requests raw histogram with the default NaN drop only', () => { + const dose = { code: 'dose', label: 'Dose', type: 'continuous' }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'dose', + data: { num_dtps: 100, num_na: 5, num_total: 105, mean: 12.5 }, + }, + ], + }, + })); + mockExpService.getAlgorithmResults.mockReturnValue(of({ + result: { + histogram: [{ + var: 'dose', + bins: [0, 5, 10, 15], + counts: [10, 40, 50], + }], + }, + })); + (mockExpService.selectedVariables as any).set([dose]); + fixture.detectChanges(); + + mockExpService.getAlgorithmResults.mockClear(); + component.setSummaryTab('raw', 'Histogram'); + fixture.detectChanges(); + + expect(mockExpService.getAlgorithmResults).toHaveBeenCalledWith( + 'histogram', + ['dose'], + null, + // histogram_sql needs a handler, so the raw preview adds the default + // drop for the plotted variable and no preprocessing of its own. + { missing_values_handler: { strategies: { dose: 'drop' } } }, + // No pending cohort preview is pinned, so the stored filter applies. + undefined, + null + ); + }); + + it('requests processed histogram with pending preprocessing when edits are unapplied', () => { + const aspiration = { + code: 'aspiration', + label: 'Aspiration', + type: 'nominal', + }; + const applied = { + missing_values_handler: { + strategies: { aspiration: 'mean' }, + }, + }; + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(applied); + (mockExpService.selectedVariables as any).set([aspiration]); + fixture.detectChanges(); + + component.onMissingActionChange(aspiration, 'drop'); + mockExpService.getAlgorithmResults.mockReturnValue(of({ + result: { + histogram: [{ + var: 'aspiration', + bins: ['0', '1'], + counts: [18000, 3766], + }], + }, + })); + component.processedSummary = (component as any).buildSummaryFromResponse({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'aspiration', + data: { num_dtps: 21766, num_na: 0, num_total: 21766 }, + }, + ], + }, + }, 'processed'); + component.processedSummary.selectedStatisticKey = 'aspiration'; + fixture.detectChanges(); + + mockExpService.getAlgorithmResults.mockClear(); + component.setSummaryTab('processed', 'Histogram'); + fixture.detectChanges(); + + expect(mockExpService.getAlgorithmResults).toHaveBeenCalledWith( + 'histogram', + ['aspiration'], + null, + { missing_values_handler: { strategies: { aspiration: 'drop' } } }, + undefined, + null + ); + }); + + it('plots the processed histogram from the pending rules, never the stored config', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const stored = { + missing_values_handler: { strategies: { age: 'drop' } }, + // A derived column is the output of this variable's pipeline, never an + // input of its histogram. + categorical_column_creator: [{ + code: 'group_a', + strategy: 'filter_rules', + rules: { a: categoryFilter() }, + }], + }; + (mockExpService.appliedPreprocessingConfig as any).set(stored); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(stored); + (mockExpService.selectedVariables as any).set([age]); + mockExpService.getAlgorithmResults.mockReturnValue(of({ + result: { + histogram: [{ + var: 'age', + bins: [0, 5, 10, 15], + counts: [10, 40, 50], + }], + }, + })); + fixture.detectChanges(); + component.preprocessingStatus = 'applied'; + + // The pending edit the stored config does not carry yet. + component.onMissingActionChange(age, 'mean'); + component.processedSummary = (component as any).buildSummaryFromResponse({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'age', + data: { num_dtps: 10, num_na: 1, num_total: 11, mean: 12.5 }, + }, + ], + }, + }, 'processed'); + component.processedSummary.selectedStatisticKey = 'age'; + fixture.detectChanges(); + + mockExpService.getAlgorithmResults.mockClear(); + component.setSummaryTab('processed', 'Histogram'); + fixture.detectChanges(); + + expect(mockExpService.getAlgorithmResults).toHaveBeenCalledWith( + 'histogram', + ['age'], + null, + { missing_values_handler: { strategies: { age: 'mean' } } }, + undefined, + null + ); + const preprocessingArg = mockExpService.getAlgorithmResults.mock.lastCall![3] as Record<string, unknown>; + expect(Object.keys(preprocessingArg ?? {})).not.toContain('categorical_column_creator'); + }); + + it('offers Dataset plus selected nominal variables as histogram splits, excluding the plotted variable', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [{ code: 'F', label: 'Female' }, { code: 'M', label: 'Male' }], + }; + const tooMany = { + code: 'site', + label: 'Site', + type: 'nominal', + enumerations: Array.from({ length: 9 }, (_, i) => ({ code: `s${i}`, label: `Site ${i}` })), + }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + ], + }, + })); + (mockExpService.selectedVariables as any).set([age, sex, tooMany]); + fixture.detectChanges(); + component.setSummaryTab('raw', 'Histogram'); + const block = component.selectedStatisticBlock('raw')!; + + expect(component.canSplitSummaryHistogram('raw', block)).toBe(true); + expect(component.histogramSplitOptions(block).map((option) => option.code)).toEqual(['dataset', 'sex', 'site']); + expect(component.histogramSplitOptions(block).find((option) => option.code === 'site')?.disabled).toBe(true); + expect(component.histogramSplitValue('raw', block)).toBe(''); + }); + + it('caps Dataset split by selected datasets, not the enumeration map', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + mockExpService.getDatasetLabelMap.mockReturnValue( + Object.fromEntries(Array.from({ length: 12 }, (_, i) => [`ds${i}`, `D${i}`])) + ); + (mockExpService.selectedDatasets as any).set(['ds0', 'ds1']); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + ], + }, + })); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + component.setSummaryTab('raw', 'Histogram'); + const block = component.selectedStatisticBlock('raw')!; + expect(component.histogramSplitOptions(block).find((option) => option.code === 'dataset')?.disabled).toBe(false); + }); + + it('hides histogram split controls for nominal variables', () => { + const aspiration = { + code: 'aspiration', + label: 'Aspiration', + type: 'nominal', + enumerations: [{ code: '0', label: 'No' }, { code: '1', label: 'Yes' }], + }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'aspiration', + data: { num_dtps: 10, num_na: 0, num_total: 10, counts: { '0': 6, '1': 4 } }, + }, + ], + }, + })); + (mockExpService.selectedVariables as any).set([aspiration]); + fixture.detectChanges(); + component.setSummaryTab('raw', 'Histogram'); + const block = component.selectedStatisticBlock('raw')!; + + expect(component.canSplitSummaryHistogram('raw', block)).toBe(false); + expect(mockExpService.getAlgorithmResults).not.toHaveBeenCalled(); + }); + + it('re-requests a stacked histogram when split changes and reuses the cache when switching back', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + ], + }, + })); + mockExpService.getAlgorithmResults.mockImplementation((_name: string, _y?: string[] | null, _bins?: number | null, _prep?: unknown, _filter?: unknown, grouping?: string[] | null) => { + if (grouping?.[0] === 'dataset') { + return of({ + result: { + histogram: [ + { var: 'age', grouping_var: null, grouping_enum: null, bins: [0, 5, 10], counts: [10, 40] }, + { var: 'age', grouping_var: 'dataset', grouping_enum: 'dataset-a', bins: [0, 5, 10], counts: [6, 20] }, + ], + }, + }); + } + return of({ + result: { histogram: [{ var: 'age', grouping_var: null, grouping_enum: null, bins: [0, 5, 10], counts: [10, 40] }] }, + }); + }); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + mockExpService.getAlgorithmResults.mockClear(); + component.setSummaryTab('raw', 'Histogram'); + fixture.detectChanges(); + const block = component.selectedStatisticBlock('raw')!; + expect(mockExpService.getAlgorithmResults).toHaveBeenCalledTimes(1); + expect(component.selectedSummaryHistogramData('raw', block)?.groups).toBeUndefined(); + + component.onHistogramSplitChange('raw', block, { target: { value: 'dataset' } } as unknown as Event); + fixture.detectChanges(); + expect(mockExpService.getAlgorithmResults).toHaveBeenCalledTimes(2); + expect(mockExpService.getAlgorithmResults).toHaveBeenLastCalledWith( + 'histogram', + ['age'], + null, + { missing_values_handler: { strategies: { age: 'drop' } } }, + undefined, + ['dataset'] + ); + expect(component.selectedSummaryHistogramData('raw', block)?.groups).toEqual([ + expect.objectContaining({ key: 'dataset-a', label: 'Dataset A', counts: [6, 20] }), + ]); + + component.onHistogramSplitChange('raw', block, { target: { value: '' } } as unknown as Event); + fixture.detectChanges(); + expect(mockExpService.getAlgorithmResults).toHaveBeenCalledTimes(2); + expect(component.selectedSummaryHistogramData('raw', block)?.groups).toBeUndefined(); + expect(component.selectedSummaryHistogramData('raw', block)?.counts).toEqual([10, 40]); + }); + + it('keeps privacy-null edge bins on Data Handling histograms', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + ], + }, + })); + mockExpService.getAlgorithmResults.mockReturnValue(of({ + result: { + histogram: [{ + var: 'age', + grouping_var: null, + bins: [0, 10, 20, 30], + counts: [null, 8, 12, null], + }], + }, + })); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + mockExpService.getAlgorithmResults.mockClear(); + component.setSummaryTab('raw', 'Histogram'); + fixture.detectChanges(); + const block = component.selectedStatisticBlock('raw')!; + expect(component.selectedSummaryHistogramData('raw', block)?.counts).toEqual([null, 8, 12, null]); + expect(component.summaryHistogramError('raw', block)).toBe(''); + }); + + it('loads the processed summary when saved preprocessing is hydrated', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const preprocessing = { + missing_values_handler: { + strategies: { + age: 'median', + }, + }, + }; + (mockExpService.appliedPreprocessingConfig as any).set(preprocessing); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(preprocessing); + mockExpService.loadDescriptiveOverview.mockImplementation((_codes: string[], preprocessingArg?: unknown) => { + if (!preprocessingArg) { + return of({ result: { featurewise: [] } }); + } + return of({ + result: { + featurewise: [ + { + dataset: 'all datasets', + variable: 'age', + data: { + num_dtps: 10, + num_na: 0, + num_total: 10, + mean: 71, + }, + }, + ], + }, + }); + }); + + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith(['age'], preprocessing); + expect(component.processedSummary.data.length).toBe(1); + expect(component.processedSummary.data[0].name).toBe('Age'); + }); + + it('scrolls to the transformation section when that sub-tab is selected', () => new Promise<void>((done) => { + configureRawSummary(); + const scrollSpy = vi.spyOn(Element.prototype, 'scrollIntoView').mockReturnValue(undefined); + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback): number => { + void Promise.resolve().then(() => callback(0)); + return 0; + }); + + component.goToSection('transformation'); + fixture.detectChanges(); + + setTimeout(() => { + expect(component.sectionOpen().transformation).toBe(true); + expect(scrollSpy).toHaveBeenCalledWith({ behavior: 'smooth', block: 'start' }); + done(); + }); + })); + + it('hydrates the transformation form from applied categorical_column_creator and keeps it on sync', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const filter = { + condition: 'AND' as const, + rules: [{ + id: 'age-gt-65', + field: 'age', + type: 'integer' as const, + input: 'number' as const, + operator: 'greater', + value: 65, + }], + }; + const transformation = { + new_column_names: ['mrs_good_outcome'], + strategy: 'filter_rules', + rules: { good: filter }, + default_enumeration: 'unknown', + }; + const preprocessing = { + missing_values_handler: { + strategies: { age: 'drop' }, + }, + categorical_column_creator: [transformation], + }; + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(preprocessing); + (mockExpService.appliedPreprocessingConfig as any).set(preprocessing); + // Column creators live in the ordered store; the panel hydrates its cards from there. + (mockExpService.appliedTransformations as any).set([transformation].map((parameters, index) => ({ + id: `cat-${index}`, name: 'categorical_column_creator', parameters, + }))); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + expect(component.transformationDrafts.length).toBe(1); + const draft = component.transformationDrafts[0]; + expect(draft.code).toBe('mrs_good_outcome'); + expect(draft.defaultEnumeration).toBe('unknown'); + expect(draft.rules).toEqual([{ value: 'good', filter }]); + expect(component.transformationStatusLabel).toBe('Applied'); + expect(component.cards().map((step) => step.parameters)).toEqual([transformation]); + }); + + it('hydrates a category rule stored as a bare condition, and keeps it through Apply', () => { + const clinical = { code: 'clinical_sdr', label: 'Clinical syndrome', type: 'nominal', enumerations: [ + { code: '1', label: 'ACS' }, + { code: '2', label: 'PACS' }, + { code: '4', label: 'POCS' }, + ] }; + // The shape a saved experiment holds: one rule per category, no wrapping group. + const acs: any = { id: 'clinical_sdr', field: 'clinical_sdr', operator: 'in', value: ['1', '2'], type: 'string' }; + const pcs: any = { id: 'clinical_sdr', field: 'clinical_sdr', operator: 'equal', value: '4', type: 'string' }; + const transformation = { + new_column_names: ['stroke_territory_cohort'], + strategy: 'filter_rules', + rules: { ACS: acs, PCS: pcs }, + default_enumeration: 'other_or_unknown', + }; + const preprocessing = { categorical_column_creator: [transformation] }; + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(preprocessing); + (mockExpService.appliedPreprocessingConfig as any).set(preprocessing); + // Column creators live in the ordered store; the panel hydrates its cards from there. + (mockExpService.appliedTransformations as any).set([transformation].map((parameters, index) => ({ + id: `cat-${index}`, name: 'categorical_column_creator', parameters, + }))); + (mockExpService.selectedVariables as any).set([clinical]); + fixture.detectChanges(); + + const draft = component.transformationDrafts[0]; + expect(draft.rules.map((rule) => rule.value)).toEqual(['ACS', 'PCS']); + expect(draft.rules.map((rule) => rule.filter)).toEqual([acs, pcs]); + expect(component.transformationDraftStatus(draft).label).toBe('Applied'); + + // Apply commits the rendered builders; a rule that loaded must survive untouched. + const modals: any = new QueryList(); + modals.reset([ + { + exportFilterLogic: () => ({ condition: 'AND', rules: [{ field: 'clinical_sdr', operator: 'in', value: ['1', '2'] }], valid: true }), + filterError: () => null, + }, + { + exportFilterLogic: () => ({ condition: 'AND', rules: [{ field: 'clinical_sdr', operator: 'equal', value: '4' }], valid: true }), + filterError: () => null, + }, + ]); + component.transformationRuleModals = modals; + + component.commitTransformationDraft(draft); + expect(draft.rules.map((rule) => (rule.filter as any)?.rules.length)).toEqual([1, 1]); + }); + + it('drops an untouched category on Apply and keeps the stage pending while one is half-filled', () => { + const filter = { condition: 'AND' as const, rules: [{ field: 'age', operator: 'greater', value: 65 }] } as any; + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'elderly'; + draft.rules = [{ value: 'old', filter }, { value: '', filter: null }]; + component.transformationRuleModals = new QueryList() as any; + + expect(component.transformationDraftStatus(draft).label).toBe('Not applied'); + expect(component.transformationHasPendingChange).toBe(true); + + component.commitTransformationDraft(draft); + expect(draft.rules.map((rule) => rule.value)).toEqual(['old']); + expect(component.transformationDraftStatus(draft).label).toBe('Applied'); + expect(component.transformationHasPendingChange).toBe(false); + + draft.rules.push({ value: 'named-only', filter: null }); + component.commitTransformationDraft(draft); + expect(draft.rules.length).toBe(2); + expect(component.transformationHasPendingChange).toBe(true); + }); + + it('drops loaded category counts once the column is edited', () => { + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'elderly'; + component.countsDraftId = draft.id; + component.transformationStatistics = [{ code: 'elderly', rows: [{ value: 'old', count: 12 }] }]; + component.transformationStatisticsError = 'Failed to load category counts.'; + + component.removeTransformationRule(draft, 0); + + expect(component.countsDraftId).toBeNull(); + expect(component.transformationStatistics).toEqual([]); + expect(component.transformationStatisticsError).toBe(''); + }); + + it('hydrates every saved categorical creator into its own card', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const filter = { + condition: 'AND' as const, + rules: [{ + id: 'age-gt-65', + field: 'age', + type: 'integer' as const, + input: 'number' as const, + operator: 'greater', + value: 65, + }], + }; + const creators = [ + { new_column_names: ['group_a'], strategy: 'filter_rules', rules: { a: filter } }, + { new_column_names: ['group_b'], strategy: 'filter_rules', rules: { b: filter }, default_enumeration: 'other' }, + ]; + const preprocessing = { categorical_column_creator: creators }; + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(preprocessing); + (mockExpService.appliedPreprocessingConfig as any).set(preprocessing); + // Column creators live in the ordered store; the panel hydrates its cards from there. + (mockExpService.appliedTransformations as any).set(creators.map((parameters, index) => ({ + id: `cat-${index}`, name: 'categorical_column_creator', parameters, + }))); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + // Both steps hydrate; the last one must not win. + expect(component.transformationDrafts.map((draft) => draft.code)).toEqual(['group_a', 'group_b']); + expect(component.transformationDrafts[1].defaultEnumeration).toBe('other'); + expect(component.transformationDraftStatus(component.transformationDrafts[0]).label).toBe('Applied'); + expect(component.transformationDraftStatus(component.transformationDrafts[1]).label).toBe('Applied'); + + // Re-persisting the same cards keeps them as an ordered array of two. + mockExpService.upsertTransformation.mockClear(); + component.onTransformationChange(); + expect(component.cards().map((step) => step.parameters)).toEqual([ + { new_column_names: ['group_a'], strategy: 'filter_rules', rules: { a: filter } }, + { new_column_names: ['group_b'], strategy: 'filter_rules', rules: { b: filter }, default_enumeration: 'other' }, + ]); + }); + + it('adds and removes extra categorical column cards, keeping at least one', () => { + // Scoped to the stage's graph: the stage header carries its own Remove Step. + const cards = () => workflowSection('Transformation').querySelectorAll('.transformation-graph app-station-card'); + const cardActions = () => + Array.from( + workflowSection('Transformation').querySelectorAll('.transformation-graph .btn-remove-step-node') + ) as HTMLButtonElement[]; + const chooserRows = () => + Array.from( + workflowSection('Transformation').querySelectorAll('.transformation-chooser .pipeline-subnode-content') + ) as HTMLButtonElement[]; + + openStation('transformation'); + // Nothing chosen yet: the stage is the type chooser, with no column card. + expect(cards().length).toBe(0); + expect(chooserRows().length).toBe(2); + + chooserRows()[0].click(); + fixture.detectChanges(); + + expect(component.transformationDrafts.length).toBe(1); + expect(cards().length).toBe(1); + // Every card offers its own remove action. + expect(cardActions().length).toBe(1); + expect(cardActions()[0].textContent).toContain('Remove column'); + + chooserRows()[0].click(); + fixture.detectChanges(); + + expect(component.transformationDrafts.length).toBe(2); + expect(cards().length).toBe(2); + expect(cardActions().length).toBe(2); + + cardActions()[1].click(); + fixture.detectChanges(); + + expect(component.transformationDrafts.length).toBe(1); + expect(cards().length).toBe(1); + expect(cardActions().length).toBe(1); + + // The last card is removed, so the chooser is alone. + cardActions()[0].click(); + fixture.detectChanges(); + + expect(component.transformationDrafts.length).toBe(0); + expect(cards().length).toBe(0); + expect(component.transformationChooserOnly).toBe(true); + }); + + it('clears the primary card instead of removing the last one', () => { + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'group_a'; + draft.rules = [{ value: 'a', filter: { condition: 'AND', rules: [] } }]; + + component.removeTransformationDraft(draft.id); + + expect(component.transformationDrafts.length).toBe(0); + expect(component.cards().length).toBe(0); + expect(component.transformationChooserOnly).toBe(true); + }); + + it('commits a column card without leaving the pipeline', () => { + const navigate = vi.spyOn(TestBed.inject(ExperimentStudioNavigationService), 'navigateToSection').mockReturnValue(undefined); + const filter = categoryFilter(); + openStation('transformation'); + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'mrs_good_outcome'; + draft.rules = [{ value: 'good', filter }]; + component.transformationRuleModals = { + toArray: () => [{ exportFilterLogic: () => filter, filterError: () => null }], + } as any; + expect(component.sectionOpen().transformation).toBe(true); + + component.commitTransformationDraft(draft); + fixture.detectChanges(); + + expect(mockExpService.upsertTransformation).toHaveBeenCalled(); + expect(navigate).not.toHaveBeenCalled(); + + // The card's own Apply folds just that card: the stage stays open for the next + // derived column. + expect(component.sectionOpen().transformation).toBe(true); + expect(component.transformationDrafts.every((card) => !card.open)).toBe(true); + const body = workflowSection('Transformation').querySelector('.workflow-section-body'); + expect(body?.classList.contains('open')).toBe(true); + + // The terminal card is the only control that advances to Algorithm. + component.finishDataHandling(); + expect(navigate).toHaveBeenCalledWith('algorithm-section'); + }); + + it('leaves the stage open when a card holding no derived column is closed', () => { + // Collapsing the stage would reveal an empty overview, and a card Close never folds + // the stage around it any more. + openStation('transformation'); + ensureCategoricalDrafts(1); + component.transformationRuleModals = { toArray: () => [] } as any; + + component.commitTransformationDraft(component.transformationDrafts[0]); + + expect(component.sectionOpen().transformation).toBe(true); + expect(component.transformationDrafts[0].open).toBe(false); + }); + + it('keeps the stored column and flags the card when an emptied card is read by a later step', () => { + openStation('transformation'); + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'grp'; + draft.rules = []; + // A later step reads 'grp'; an edit that writes no column would orphan it. + mockExpService.outputsInUse.mockImplementation((_id: string, next: string[]) => (next.length ? [] : ['grp'])); + mockExpService.upsertTransformation.mockClear(); + + component.onTransformationChange(); + + expect(mockExpService.upsertTransformation).not.toHaveBeenCalled(); + expect(component.transformationDraftStatus(draft).label).toBe('Column in use'); + }); + + it('applies the first card when two derived columns share a name', () => { + const navigate = vi.spyOn(TestBed.inject(ExperimentStudioNavigationService), 'navigateToSection').mockReturnValue(undefined); + const filter = categoryFilter(); + openStation('transformation'); + ensureCategoricalDrafts(2); + component.transformationDrafts[0].code = 'group'; + component.transformationDrafts[0].rules = [{ value: 'a', filter }]; + component.transformationDrafts[1].code = 'group'; + component.transformationDrafts[1].rules = [{ value: 'b', filter }]; + component.transformationRuleModals = { + toArray: () => [ + { exportFilterLogic: () => filter, filterError: () => null }, + { exportFilterLogic: () => filter, filterError: () => null }, + ], + } as any; + fixture.detectChanges(); + + component.commitTransformationDraft(component.transformationDrafts[0]); + component.commitTransformationDraft(component.transformationDrafts[1]); + fixture.detectChanges(); + + // Apply no longer refuses. The second card owns the collision chip; only the first + // occurrence of a shared code is persisted. + expect(component.transformationDraftStatus(component.transformationDrafts[0]).label).toBe('Applied'); + expect(component.transformationDraftStatus(component.transformationDrafts[1]).label).toBe('Duplicate name'); + expect(component.transformationStatusLabel).toBe('Not applied'); + expect(navigate).not.toHaveBeenCalled(); + expect(component.transformationDrafts[0].open).toBe(false); + expect(component.transformationDrafts[1].open).toBe(true); + expect(component.sectionOpen().transformation).toBe(true); + const persisted = component.cards() + .map((step) => step.parameters) + .filter((parameters) => Array.isArray(parameters['new_column_names']) && parameters['new_column_names'].length); + expect(persisted).toEqual([expect.objectContaining({ new_column_names: ['group'] })]); + }); + + it('copies live category filters onto rules before preview and apply', () => { + const filter = categoryFilter('age', 'greater', 65); + const secondFilter = categoryFilter('sex', 'equal', 1); + ensureCategoricalDrafts(2); + component.transformationDrafts[0].code = 'mrs_good_outcome'; + component.transformationDrafts[0].rules = [{ value: 'good', filter: null }]; + component.transformationDrafts[1].code = 'mrs_bad_outcome'; + component.transformationDrafts[1].rules = [{ value: 'bad', filter: null }]; + // One modal per rule, flattened in draft order, as the template emits them. + component.transformationRuleModals = { + toArray: () => [ + { exportFilterLogic: () => filter, filterError: () => null }, + { exportFilterLogic: () => secondFilter, filterError: () => null }, + ], + } as any; + + component.commitTransformationRuleFilters(); + expect(component.transformationDrafts[0].rules[0].filter).toBe(filter as any); + expect(component.transformationDrafts[1].rules[0].filter).toBe(secondFilter as any); + expect(component.cards().map((step) => step.parameters)).toEqual([ + expect.objectContaining({ new_column_names: ['mrs_good_outcome'] }), + expect.objectContaining({ new_column_names: ['mrs_bad_outcome'] }), + ]); + }); + + describe('preprocessing evidence', () => { + it('prices the selected missing-value action in real rows', () => { + const { sex } = configureRawSummary(); + component.fetchDescriptiveStatistics(); + fixture.detectChanges(); + + const coverage = component.coverageFor(sex); + expect(coverage?.total).toBe(11); + expect(coverage?.missing).toBe(1); + expect(component.coveragePercent(coverage!)).toBe('9.1%'); + // Below coverageSeverity's 10% warning onset, so it is deliberately + // uncoloured — the rail compares values, and 9.1% is the floor case. + expect(component.coverageSeverity(coverage!)).toBe('none'); + + component.onMissingActionChange(sex, 'drop'); + expect(component.missingActionConsequence(sex)).toBe('Drops up to 1 of 11 rows (9.1%).'); + }); + + it('reports category frequencies for a nominal variable only', () => { + const { age, sex } = configureRawSummary(); + component.fetchDescriptiveStatistics(); + fixture.detectChanges(); + + expect(component.categoryCountsFor(sex).map((row) => [row.label, row.value, row.share])) + .toEqual([['female', '6', 0.6], ['male', '4', 0.4]]); + // A numeric block's Mean/Std/Min/... rows are metrics, not categories. + expect(component.categoryCountsFor(age)).toEqual([]); + }); + + it('omits quantiles the payload does not carry instead of rendering N/A', () => { + const { age } = configureRawSummary(); + component.fetchDescriptiveStatistics(); + fixture.detectChanges(); + + expect(component.distributionFor(age)).toEqual([{ label: 'Mean', value: '71' }]); + }); + + it('shows no evidence at all before the descriptive stats arrive', () => { + // Default harness resolves an empty featurewise payload, so there is + // no block for this variable and the rail must render nothing. + expect(component.coverageFor({ code: 'age' })).toBeNull(); + expect(component.missingActionConsequence({ code: 'age' })).toBeNull(); + expect(component.distributionFor({ code: 'age' })).toEqual([]); + }); + + it('prices every picker row with that variable\'s own share missing', () => { + configureRawSummary(); + component.fetchDescriptiveStatistics(); + fixture.detectChanges(); + + openStation('setup'); + const rows = Array.from(fixture.nativeElement.querySelectorAll('.preprocessing-variable-btn')) as HTMLElement[]; + const sexRow = rows.find((row) => row.textContent?.includes('Sex')); + // sex: num_na 1 of num_total 11. age carries num_na 0 -> 0%. + expect(sexRow?.querySelector('.preprocessing-variable-coverage')?.textContent.trim()).toBe('9.1%'); + // 9.1% is under coverageSeverity's 10% onset, so it is not red. + expect(sexRow?.querySelector('.preprocessing-variable-coverage')?.getAttribute('data-severity')).not.toBe('high'); + }); + + it('states why the evidence pane is empty instead of rendering blank space', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age]); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + openStation('setup'); + component.selectPrepVariable('missing', age); + fixture.detectChanges(); + + expect(component.coverageFor(age)).toBeNull(); + const note = fixture.nativeElement.querySelector('.preprocessing-evidence .preprocessing-consequence') as HTMLElement; + expect(note?.textContent?.trim()).toContain('No dataset statistics are available'); + }); + }); + + describe('step documentation', () => { + it('renders prose with one list for every bullet marker', () => { + mockExpService.backendAlgorithms.set({ + describe: { + preprocessing: [ + { + name: 'missing_values_handler', + documentation: [ + 'Handles missing values using a selected strategy for each variable.', + 'Strategies:', + '- Drop removes rows with missing values for the variable.', + '\u2022 Mean fills missing numerical values with the local mean.', + 'Fold: the multiple of the IQR used by the winsorizer.', + ].join('\n'), + }, + ], + }, + } as any); + + const html = component.formatPreprocessingDocumentationHtml('missing_values_handler'); + + expect(html).toContain( + '<p class="preprocessing-doc-paragraph preprocessing-doc-intro">Handles missing values using a selected strategy for each variable.</p>' + ); + expect(html).toContain('<p class="preprocessing-doc-section-title">Strategies:</p>'); + // '-' and '\u2022' are the same list, and a 'Term: description' item keeps its colon + // because it is read as one line of prose rather than a table row. + expect(html.match(/<li>/g)).toHaveLength(3); + expect(html).toContain('<span class="preprocessing-doc-term">Fold:</span>'); + expect(html).toContain( + '<span class="preprocessing-doc-desc">Mean fills missing numerical values with the local mean.</span>' + ); + }); + }); + + describe('summary number formatting', () => { + it('keeps pivot values canonical for CSV and groups only at display time', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 1234.5 } }, + ], + }, + })); + (mockExpService.selectedVariables as any).set([age]); + component.fetchDescriptiveStatistics(); + fixture.detectChanges(); + + // Stored cell value is the same string the CSV export reads: fixed + // decimals, no locale grouping (1234.5 -> "1234.50", not "1,234.50"). + const meanRow = component.rawSummary.data + .find((block) => block.code === 'age') + ?.rows.find((row) => row.metric === 'Mean'); + expect(meanRow?.values['all datasets']).toBe('1234.50'); + + const csvSpy = vi.spyOn(TestBed.inject(CsvExportService), 'exportToCsv').mockReturnValue(undefined); + component.exportSummaryToCSV('raw'); + expect(csvSpy).toHaveBeenCalledWith( + expect.arrayContaining([expect.objectContaining({ Metric: 'Mean', Dataset: 'All datasets', Value: '1234.50' })]), + ['Variable', 'Metric', 'Dataset', 'Value'], + 'raw_data_summary.csv', + ); + + // Grouping happens only on the way to the table, parsed from the canonical string. + expect(component.displayNumber('1234.50')) + .toBe((1234.5).toLocaleString(undefined, { maximumFractionDigits: 2 })); + expect(component.displayNumber('1234.50')).not.toBe('1234.50'); + }); + + describe('summary tabs', () => { + it('switches tabs directly without toggle-off gymnastics', () => { + configureRawSummary(); + + component.setSummaryTab('raw', 'Charts'); + expect(component.rawSummary.activeTab).toBe('Charts'); + + // Re-clicking the same tab keeps it active; it does not fall back to Statistics. + component.setSummaryTab('raw', 'Charts'); + expect(component.rawSummary.activeTab).toBe('Charts'); + + component.setSummaryTab('raw', 'Statistics'); + expect(component.rawSummary.activeTab).toBe('Statistics'); + }); + + it('renders an explicit Table tab in the raw summary workspace', () => { + configureRawSummary(); + // The raw summary now lives inside the Filtering node, so it only + // renders once that step is added. + component.addStep('filters'); + fixture.detectChanges(); + const rawSection = workflowSection('Raw Data Summary'); + const tabs = Array.from(rawSection.querySelectorAll('.summary-tabs button')) as HTMLButtonElement[]; + expect(tabs.map((b) => b.textContent?.trim())).toEqual(['Table', 'Charts', 'Histogram']); + // Default tab is Statistics (Table). + expect(tabs[0].classList.contains('active')).toBe(true); + }); + + /** + * The switch only swaps the results pane, so the pane owns it. Rendered above the + * card it sat over the variable rail and read as a toolbar for the whole surface. + * The column wrapper is also what keeps it out of the pane's scroll area. + */ + it('renders the overlay switch inside the results pane it controls', () => { + configureRawSummary(); + component.addStep('filters'); + fixture.detectChanges(); + + const rawSection = workflowSection('Raw Data Summary'); + const column = rawSection.querySelector('.summary-detail-column') as HTMLElement; + const tabs = column.querySelector('.summary-tabs') as HTMLElement; + const panel = column.querySelector('.statistics-panel') as HTMLElement; + + expect(tabs).toBeTruthy(); + expect(panel).toBeTruthy(); + expect(tabs.compareDocumentPosition(panel) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy(); + // The rail lists variables, which the switch does not affect. + expect(rawSection.querySelector('.statistics-browser .summary-tabs')).toBeNull(); + }); + + /** + * A numerical histogram is a live federated run, not part of the summary payload. + * The wait has to say so, otherwise a couple of shimmer lines in a pane that + * reserves a full chart height reads as a surface that stopped responding. + */ + it('names the federated run while a numerical histogram loads', () => { + configureRawSummary(); + component.addStep('filters'); + fixture.detectChanges(); + + // Held open: this is the window the user actually stares at. + const pending = new Subject<any>(); + mockExpService.getAlgorithmResults.mockReturnValue(pending.asObservable()); + component.setSummaryTab('raw', 'Histogram'); + fixture.detectChanges(); + + const rawSection = workflowSection('Raw Data Summary'); + expect(mockExpService.getAlgorithmResults).toHaveBeenCalled(); + expect(rawSection.textContent).toContain('Calculated on'); + expect(rawSection.textContent).toContain('dataset node'); + expect(rawSection.querySelector('.studio-skeleton-chart')).toBeTruthy(); + expect(rawSection.querySelector('app-histogram')).toBeNull(); + + pending.complete(); + }); + }); + + describe('raw summary fetch', () => { + /** + * Raw is described with a null preprocessing config, so an Apply cannot move its + * numbers. It used to share the selection key with the processed summary and paid + * a second full federated describe on every apply. + */ + it('does not refetch the raw summary when only the applied preprocessing changes', () => { + configureRawSummary(); + const before = component.rawSummary; + mockExpService.loadDescriptiveOverview.mockClear(); + + (mockExpService.appliedPreprocessingConfig as any).set({ missing_values_handler: { age: 'mean' } }); + fixture.detectChanges(); + + expect(component.rawSummary).toBe(before); + const rawRuns = mockExpService.loadDescriptiveOverview.mock.calls.filter((args) => args[1] === null); + expect(rawRuns.length).toBe(0); + }); + + /** + * Adding variables in quick succession starts overlapping describes. Without a + * sequence guard the slowest response wins, so the card can show a stale cohort. + */ + it('drops a raw describe response that a newer run already superseded', () => { + configureRawSummary(); + component.addStep('filters'); + fixture.detectChanges(); + + const describePayload = (mean: number) => ({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean } }, + ], + }, + }); + + const stale = new Subject<any>(); + mockExpService.loadDescriptiveOverview.mockReturnValue(stale.asObservable()); + component.fetchDescriptiveStatistics(); + + const current = new Subject<any>(); + mockExpService.loadDescriptiveOverview.mockReturnValue(current.asObservable()); + component.fetchDescriptiveStatistics(); + + current.next(describePayload(71)); + stale.next(describePayload(99)); + fixture.detectChanges(); + + const table = workflowSection('Raw Data Summary').querySelector('.statistics-table'); + expect(table?.textContent).toContain('71'); + expect(table?.textContent).not.toContain('99'); + + stale.complete(); + current.complete(); + }); + }); + + describe('unapplied-changes guard at the pipeline terminal', () => { + it('advances straight to algorithm selection when nothing is pending', () => { + const navigate = vi.spyOn(TestBed.inject(ExperimentStudioNavigationService), 'navigateToSection').mockReturnValue(undefined); + configureRawSummary(); + + component.finishDataHandling(); + + expect(component.showUnappliedChangesWarning()).toBe(false); + expect(navigate).toHaveBeenCalledWith('algorithm-section'); + }); + + it('asks what to do with unapplied changes instead of advancing', () => { + const navigate = vi.spyOn(TestBed.inject(ExperimentStudioNavigationService), 'navigateToSection').mockReturnValue(undefined); + const { age } = configureRawSummary(); + component.onMissingActionChange(age, 'mean'); + expect(component.pendingChangeCount).toBeGreaterThan(0); + + component.finishDataHandling(); + fixture.detectChanges(); + + expect(navigate).not.toHaveBeenCalled(); + expect(component.showUnappliedChangesWarning()).toBe(true); + const card = fixture.nativeElement.querySelector('.unapplied-warning-card'); + expect(card?.textContent).toContain('You have unapplied changes'); + }); + + it('stays in the pipeline when the warning is dismissed', () => { + const navigate = vi.spyOn(TestBed.inject(ExperimentStudioNavigationService), 'navigateToSection').mockReturnValue(undefined); + const { age } = configureRawSummary(); + component.onMissingActionChange(age, 'mean'); + component.finishDataHandling(); + fixture.detectChanges(); + expect(component.showUnappliedChangesWarning()).toBe(true); + + (fixture.nativeElement.querySelector('.unapplied-warning-btn.ghost') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(component.showUnappliedChangesWarning()).toBe(false); + expect(navigate).not.toHaveBeenCalled(); + expect(component.pendingChangeCount).toBeGreaterThan(0); + }); + + it('discards pending changes and continues', () => { + const navigate = vi.spyOn(TestBed.inject(ExperimentStudioNavigationService), 'navigateToSection').mockReturnValue(undefined); + const { age } = configureRawSummary(); + component.onMissingActionChange(age, 'mean'); + expect(component.pendingChangeCount).toBeGreaterThan(0); + + component.discardAndContinue(); + fixture.detectChanges(); + + expect(component.showUnappliedChangesWarning()).toBe(false); + expect(component.pendingChangeCount).toBe(0); + expect(component.pendingPreprocessingRules['age']?.action).toBe('drop'); + expect(navigate).toHaveBeenCalledWith('algorithm-section'); + }); + + it('applies pending changes and continues once the describe lands', () => new Promise<void>((done) => { + const navigate = vi.spyOn(TestBed.inject(ExperimentStudioNavigationService), 'navigateToSection').mockReturnValue(undefined); + const { age } = configureRawSummary(); + const response$ = new Subject<unknown>(); + vi.spyOn(Element.prototype, 'scrollIntoView').mockReturnValue(undefined); + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback): number => { + void Promise.resolve().then(() => callback(0)); + return 0; + }); + mockExpService.loadDescriptiveOverview.mockReturnValue(response$.asObservable()); + + component.onMissingActionChange(age, 'mean'); + component.applyAndContinue(); + + expect(component.showUnappliedChangesWarning()).toBe(false); + expect(navigate).not.toHaveBeenCalled(); + + setTimeout(() => { + response$.next({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 10, num_na: 0, num_total: 10, mean: 71 } }, + ], + }, + }); + response$.complete(); + fixture.detectChanges(); + + expect(component.pendingChangeCount).toBe(0); + expect(navigate).toHaveBeenCalledWith('algorithm-section'); + done(); + }, 0); + })); + }); + + describe('batch preprocessing actions', () => { + it('applies a batch mean strategy to every numerical variable only', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const glucose = { code: 'glucose', label: 'Glucose', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, glucose, sex]); + fixture.detectChanges(); + + component.applyBatchMissingStrategy('mean'); + + expect(component.pendingPreprocessingRules['age']?.action).toBe('mean'); + expect(component.pendingPreprocessingRules['glucose']?.action).toBe('mean'); + // sex keeps the implicit NA-removal default; the batch only touched numerical. + expect(component.pendingPreprocessingRules['sex']?.action).toBe('drop'); + expect(component.pendingChangeCount).toBeGreaterThan(0); + }); + + it('resets every variable to drop via batch action', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + fixture.detectChanges(); + + // Move age off its default so the reset is observable. + component.onMissingActionChange(age, 'mean'); + expect(component.pendingPreprocessingRules['age']?.action).toBe('mean'); + + component.applyBatchMissingStrategy('drop'); + + expect(component.pendingPreprocessingRules['age']?.action).toBe('drop'); + expect(component.pendingPreprocessingRules['sex']?.action).toBe('drop'); + // Both back at the implicit default: nothing pending. + expect(component.pendingChangeCount).toBe(0); + }); + + it('enables the outlier winsorizer for all eligible variables via batch action', () => { + const age = { code: 'age', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex', label: 'Sex', type: 'nominal' }; + (mockExpService.selectedVariables as any).set([age, sex]); + fixture.detectChanges(); + + component.toggleAllOutliers(true); + + expect(component.pendingOutlierRules['age']?.enabled).toBe(true); + expect(component.pendingOutlierRules['sex']).toBeUndefined(); + }); + }); + + describe('cohort retention strip', () => { + it('shows the retention strip only when both raw and processed summaries are loaded', () => new Promise<void>((done) => { + const { age } = configureRawSummary(); + // Before any preprocessing is applied, the processed summary is empty. + expect(fixture.nativeElement.querySelector('.cohort-retention-strip')).toBeNull(); + + const response$ = new Subject<unknown>(); + vi.spyOn(Element.prototype, 'scrollIntoView').mockReturnValue(undefined); + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback: FrameRequestCallback): number => { + void Promise.resolve().then(() => callback(0)); + return 0; + }); + mockExpService.loadDescriptiveOverview.mockReturnValue(response$.asObservable()); + + component.onMissingActionChange(age, 'mean'); + component.applyPreprocessing(); + + setTimeout(() => { + response$.next({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'age', data: { num_dtps: 8, num_na: 0, num_total: 8, mean: 71 } }, + ], + }, + }); + response$.complete(); + fixture.detectChanges(); + + const strip = fixture.nativeElement.querySelector('.cohort-retention-strip'); + expect(strip).not.toBeNull(); + expect(strip.textContent).toContain('8 / 10'); + expect(strip.textContent).toContain('2 rows removed by preprocessing'); + done(); + }, 0); + })); + }); + + }); + + describe('Data Handling preview contracts', () => { + /** Stand-in for a category-rule QueryBuilder, as the template hands them over. */ + function ruleModal(filter: any, error: string | null = null) { + return { + exportFilterLogic: () => filter, + filterError: () => error, + }; + } + + /** Stand-in for the cohort builder: exports what the editor holds, unapplied. */ + function cohortModal(filter: any, error: string | null = null) { + return { exportFilterLogic: () => filter, filterError: () => error }; + } + + function stationButton(stage: string, selector: string): HTMLButtonElement { + return workflowSection(stage).querySelector(selector) as HTMLButtonElement; + } + + it('plots the step 0 histogram with the default drop and no cohort filter', () => { + const dose = { code: 'dose', label: 'Dose', type: 'continuous' }; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'dose', data: { num_dtps: 100, num_na: 5, num_total: 105, mean: 12.5 } }, + ], + }, + })); + mockExpService.getAlgorithmResults.mockReturnValue(of({ + result: { histogram: [{ var: 'dose', bins: [0, 5, 10, 15], counts: [10, 40, 50] }] }, + })); + (mockExpService.filterLogic as any).set({ + condition: 'AND', + rules: [{ field: 'site', operator: 'equal', value: 'A' }], + }); + (mockExpService.selectedVariables as any).set([dose]); + fixture.detectChanges(); + + component.toggleSourcePreview(); + fixture.detectChanges(); + mockExpService.getAlgorithmResults.mockClear(); + component.setSummaryTab('source', 'Histogram'); + fixture.detectChanges(); + + expect(mockExpService.getAlgorithmResults).toHaveBeenCalledWith( + 'histogram', + ['dose'], + null, + // Step 0 stays free of preprocessing: only the drop the histogram needs. + { missing_values_handler: { strategies: { dose: 'drop' } } }, + // A null override is the snapshot: the selection, not the filtered cohort. + null, + null + ); + }); + + it('previews the cohort with the pending rules and never applies them', () => { + configureRawSummary(); + component.goToSection('filters'); + fixture.detectChanges(); + const pending = categoryFilter(); + mockExpService.loadDescriptiveOverview.mockClear(); + + component.previewFilterData(cohortModal(pending) as any); + fixture.detectChanges(); + + expect(component.sectionOpen().raw).toBe(true); + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith( + ['age', 'sex'], + null, + null, + pending + ); + // Apply (`saveFilters`) stays the only path that writes the cohort. + expect(mockExpService.setFilterLogic).not.toHaveBeenCalled(); + expect(mockExpService.filterLogic()).toBeNull(); + + // The pending filter is part of the cache key, so the same rules are free. + mockExpService.loadDescriptiveOverview.mockClear(); + component.previewFilterData(cohortModal(pending) as any); + fixture.detectChanges(); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + }); + + it('keeps the Filtering editor open when the pending rules are invalid', () => { + configureRawSummary(); + component.goToSection('filters'); + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + component.previewFilterData(cohortModal(null, 'A filter value is required') as any); + fixture.detectChanges(); + + expect(component.sectionOpen().filters).toBe(true); + expect(component.sectionOpen().raw).toBe(false); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + }); + + it('keeps the pending cohort preview when the stored filter changes underneath it', () => { + configureRawSummary(); + const pending = categoryFilter(); + component.previewFilterData(cohortModal(pending) as any); + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + // The stored cohort moves underneath the preview. Falling back to it would swap + // the cohort the user asked to see for an older one, so the pinned rules keep the + // surface — and the numbers it already holds need no second describe. + const stored = { condition: 'AND', rules: [{ field: 'sex', operator: 'equal', value: 'female' }] } as any; + (mockExpService.filterLogic as any).set(stored); + fixture.detectChanges(); + + const cohorts = mockExpService.loadDescriptiveOverview.mock.calls.map((args) => args[3]); + expect(cohorts).not.toContain(stored); + }); + + it('loads counts for the complete cards on Done and drops the unfinished ones', () => { + const filter = categoryFilter(); + openStation('transformation'); + ensureCategoricalDrafts(4); + const [first, second, unfiltered, nameless] = component.transformationDrafts; + first.code = 'group_a'; + first.rules = [{ value: 'a', filter }]; + second.code = 'group_b'; + second.rules = [{ value: 'b', filter }]; + unfiltered.code = 'other'; + unfiltered.rules = [{ value: 'c', filter: null }]; + nameless.defaultEnumeration = 'unknown'; + component.transformationRuleModals = { + toArray: () => [ruleModal(filter), ruleModal(filter), ruleModal(null)], + } as any; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'group_a', data: { counts: { a: 3 } } }, + { dataset: 'all datasets', variable: 'group_b', data: { counts: { b: 4 } } }, + ], + }, + })); + fixture.detectChanges(); + + // The stage keeps no fix-list and no red row, and never dims the pair: the + // Pending chips on the two unfinished cards are the whole warning. + const transformation = workflowSection('Transformation'); + expect(transformation.querySelector('.row-error')).toBeNull(); + expect(transformation.querySelector('.glass-feedback-warning')).toBeNull(); + expect(stationButton('Transformation', '.station-action-apply').disabled).toBe(false); + expect(component.transformationDraftStatus(unfiltered).label).toBe('Not applied'); + expect(component.transformationStatusLabel).toBe('Not applied'); + + mockExpService.loadDescriptiveOverview.mockClear(); + component.finishTransformationRule(first, 0); + fixture.detectChanges(); + + // An incomplete card simply drops out of the describe; the complete ones + // still get their counts, which each card charts for its own column. + expect(component.countsDraftId).toBe(first.id); + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledTimes(1); + expect(component.transformationStatistics).toEqual([ + { code: 'group_a', rows: [{ value: 'a', count: 3 }] }, + { code: 'group_b', rows: [{ value: 'b', count: 4 }] }, + ]); + }); + + it('runs the applied K-means creator and shows the cluster column counts', () => { + const creator = { + new_column_names: ['kmeans_cluster'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { + enumeration_prefix: 'cluster_', + cluster_variables: ['age', 'sex'], + cluster_choices: [ + { cluster_id: 'cluster_0', label: 'Cluster 0' }, + { cluster_id: 'cluster_1', label: 'Cluster 1' }, + ], + }, + }; + mockExpService.transformationSourceCodesForInput.mockReturnValue(['age', 'sex']); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [{ + dataset: 'all datasets', + variable: 'kmeans_cluster', + data: { counts: { cluster_0: 12, cluster_1: 7 } }, + }], + }, + })); + openStation('transformation'); + mockExpService.upsertTransformation({ + id: 'kmeans-preview', + name: 'kmeans_cluster_creator', + parameters: creator, + }); + fixture.detectChanges(); + + component.previewTransformationCard('kmeans-preview'); + fixture.detectChanges(); + + // Ordered-step describe: K-means is a preprocessing step over its CDE sources. + const [columns, preprocessing, sourceCodes] = + mockExpService.loadDescriptiveOverview.mock.lastCall!; + expect(columns).toEqual(['kmeans_cluster']); + expect(Array.isArray(preprocessing)).toBe(true); + expect((preprocessing as any[]).some((step) => step.name === 'kmeans_cluster_creator')).toBe(true); + expect(mockExpService.buildTransformationDescribePreprocessing).toHaveBeenCalled(); + expect(sourceCodes).toContain('age'); + expect(sourceCodes).toContain('sex'); + expect(component.transformationStatistics).toEqual([{ + code: 'kmeans_cluster', + rows: [ + { value: 'cluster_0', count: 12 }, + { value: 'cluster_1', count: 7 }, + ], + }]); + + const preview = (fixture.nativeElement as HTMLElement).querySelector('.transformation-statistics'); + expect(preview?.textContent).toContain('Nominal column · kmeans_cluster'); + expect(preview?.textContent).toContain('Each record is assigned to the nearest of these clusters.'); + expect(preview?.textContent).toContain('Nearest cluster — Cluster 0'); + expect(preview?.textContent).toContain('2 clusters · covers every record'); + expect(preview?.querySelector('.transformation-stats-table')).toBeNull(); + expect(preview?.querySelectorAll('.transformation-rule-row').length).toBe(2); + }); + + it('previews a fit-mode K-means creator from variables + synthetic cluster_1..k', () => { + const creator = { + new_column_names: ['km_fit'], + variables: ['age', 'bmi'], + k: 2, + enumeration_prefix: 'cluster_', + }; + mockExpService.transformationSourceCodesForInput.mockReturnValue(['age', 'bmi']); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [{ + dataset: 'all datasets', + variable: 'km_fit', + data: { counts: { cluster_1: 9, cluster_2: 4 } }, + }], + }, + })); + openStation('transformation'); + mockExpService.upsertTransformation({ + id: 'kmeans-fit-preview', + name: 'kmeans_cluster_creator', + parameters: creator, + }); + fixture.detectChanges(); + + component.previewTransformationCard('kmeans-fit-preview'); + fixture.detectChanges(); + + const [columns, preprocessing, sourceCodes] = + mockExpService.loadDescriptiveOverview.mock.lastCall!; + expect(columns).toEqual(['km_fit']); + expect(Array.isArray(preprocessing)).toBe(true); + expect((preprocessing as any[]).some((step) => step.name === 'kmeans_cluster_creator')).toBe(true); + expect(sourceCodes).toContain('age'); + expect(sourceCodes).toContain('bmi'); + expect(component.transformationStatistics).toEqual([{ + code: 'km_fit', + rows: [ + { value: 'cluster_1', count: 9 }, + { value: 'cluster_2', count: 4 }, + ], + }]); + expect(component.kmeansColumnView('km_fit')?.rules).toEqual({ + cluster_1: 'Nearest cluster', + cluster_2: 'Nearest cluster', + }); + expect(component.transformationStatisticsError).not.toContain('no clustering variables'); + }); + + it('keeps a rejected category open on Done without loading counts', () => { + openStation('transformation'); + // The card is on screen only once its type is chosen; its own footer owns Preview. + component.chooseTransformation('categorical'); + const draft = component.transformationDrafts[0]; + draft.code = 'group_a'; + draft.rules = [{ value: 'a', filter: null }]; + component.transformationRuleModals = { + toArray: () => [ruleModal(null, 'A filter value is required')], + } as any; + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + draft.openRuleIndex = 0; + // detectChanges refreshes the @ViewChildren query; re-stub the rejecting builder. + component.transformationRuleModals = { + toArray: () => [ruleModal(null, 'A filter value is required')], + } as any; + component.finishTransformationRule(draft, 0); + fixture.detectChanges(); + + // The builder shows its own error; the rule stays open with its previous filter + // and no describe runs for a half-typed condition. + expect(draft.openRuleIndex).toBe(0); + expect(draft.rules[0].filter).toBeNull(); + expect(component.countsDraftId).toBeNull(); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + }); + + it('shows a preview error when the K-means card has no counts', () => { + openStation('transformation'); + ensureCategoricalDrafts(1); + component.chooseTransformation('kmeans_cluster_creator'); + component.transformationDrafts[0].code = 'pro'; + component.transformationRuleModals = { toArray: () => [] } as any; + fixture.detectChanges(); + + component.previewTransformationCard(); + fixture.detectChanges(); + + const statistics = workflowSection('Transformation').querySelector('.transformation-statistics'); + expect(statistics?.querySelector('.glass-feedback-warning')?.textContent?.trim()) + .toBe('Each derived column needs a named category before counts can be loaded.'); + expect(statistics?.textContent).not.toContain('give at least one category a rule'); + }); + + it('explains a named card instead of claiming counts that never ran', () => { + openStation('transformation'); + ensureCategoricalDrafts(1); + component.transformationDrafts[0].code = 'pro'; + component.transformationRuleModals = { toArray: () => [] } as any; + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + component.finishTransformationRule(component.transformationDrafts[0], 0); + fixture.detectChanges(); + + // A bare column name owns a heading above this table, so it has to own a reason + // too. Before, it rendered dashes under "counts from a describe run" — a describe + // that never happened. + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + expect(component.transformationStatistics.map((block) => block.code)).toEqual(['pro']); + const statistics = workflowSection('Transformation').querySelector('.transformation-statistics'); + expect(statistics?.querySelector('.glass-feedback-warning')?.textContent?.trim()) + .toBe('Each derived column needs a named category before counts can be loaded.'); + expect(statistics?.textContent).not.toContain('Category counts from a describe run'); + }); + + it('turns category counts into a bar chart and skips a chart when every count is missing', () => { + expect(component.transformationCountChart({ + code: 'aaa', + rows: [ + { value: 'a', count: 88426 }, + { value: 'b', count: 187902 }, + ], + })).toEqual({ + bins: ['a', 'b'], + counts: [88426, 187902], + variableName: 'aaa', + variableType: 'nominal', + }); + expect(component.transformationCountChart({ + code: 'aaa', + rows: [{ value: 'a', count: null }], + })).toBeNull(); + }); + + it('shows a load error when describe fails, and insufficient data only when it succeeds with no counts', () => { + (mockExpService.selectedVariables as any).set([{ code: 'age', label: 'Age', type: 'continuous' }]); + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'cohort'; + draft.rules = [{ value: 'ACS', filter: categoryFilter() }]; + + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + status: 'error', + result: { + data: 'Something went wrong. Please inform the system administrator or try again later.', + type: 'text/plain+error', + }, + })); + component.refreshTransformationStatistics(); + expect(component.transformationStatisticsError).toBe('Failed to load category counts.'); + expect(component.transformationStatisticsError).not.toContain('insufficient data'); + + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + status: 'success', + result: { + featurewise: [{ variable: 'cohort', dataset: 'all datasets', data: null }], + }, + })); + component.refreshTransformationStatistics(); + expect(component.transformationStatisticsError) + .toBe('No counts were returned (privacy threshold or insufficient data after preprocessing).'); + }); + + it('names every gap when no card can be described', () => { + openStation('transformation'); + ensureCategoricalDrafts(2); + const [unfiltered, nameless] = component.transformationDrafts; + unfiltered.code = 'group_a'; + unfiltered.rules = [{ value: 'a', filter: null }]; + nameless.code = 'group_b'; + component.transformationRuleModals = { toArray: () => [ruleModal(null), ruleModal(null)] } as any; + fixture.detectChanges(); + + component.finishTransformationRule(unfiltered, 0); + + // One line for the whole stage, covering each card's own shortcoming. + expect(component.transformationStatisticsError).toBe( + 'Each derived column needs a named category and a filter on each one before counts can be loaded.' + ); + }); + + it('keeps the card primary slot live while no column can be formed', () => { + openStation('transformation'); + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'pro'; + component.transformationRuleModals = { toArray: () => [] } as any; + component.onTransformationInput(); + fixture.detectChanges(); + + // A card that cannot form a column yet still answers the click: its own primary + // slot is an enabled Apply, never a dimmed one, and Preview stays live too — the + // read is what names the gap. + const apply = stationButton('Transformation', '.station-action-apply'); + expect(apply.disabled).toBe(false); + expect(apply.textContent?.trim()).toBe('Apply'); + expect(apply.classList.contains('is-quiet')).toBe(false); + + // Once the category holds a rule the card is Applied, and its primary slot + // becomes the quiet Close. + const filter = categoryFilter(); + draft.rules = [{ value: 'old', filter }]; + component.transformationRuleModals = { toArray: () => [ruleModal(filter)] } as any; + component.onTransformationInput(); + fixture.detectChanges(); + + expect(apply.textContent?.trim()).toBe('Close'); + expect(apply.classList.contains('is-quiet')).toBe(true); + expect(apply.disabled).toBe(false); + }); + + it('keeps the unfinished card open while its own Apply folds the complete one', () => { + const filter = categoryFilter(); + openStation('transformation'); + ensureCategoricalDrafts(2); + const [complete, unfinished] = component.transformationDrafts; + complete.code = 'group_a'; + complete.rules = [{ value: 'a', filter }]; + unfinished.code = 'group_b'; + unfinished.rules = [{ value: 'b', filter: null }]; + component.transformationRuleModals = { + toArray: () => [ruleModal(filter), ruleModal(null)], + } as any; + fixture.detectChanges(); + + component.commitTransformationDraft(complete); + component.commitTransformationDraft(unfinished); + fixture.detectChanges(); + + // The complete card is committed and folds itself. The card whose Pending chip is + // the only thing left saying that work remains keeps its editor — and its Apply. + expect(component.transformationStatusLabel).toBe('Not applied'); + expect(complete.open).toBe(false); + expect(unfinished.open).toBe(true); + expect(component.sectionOpen().transformation).toBe(true); + const body = workflowSection('Transformation').querySelector('.workflow-section-body'); + expect(body?.classList.contains('open')).toBe(true); + expect( + component.cards() + .map((step) => step.parameters) + .filter((parameters) => Array.isArray(parameters['new_column_names']) && parameters['new_column_names'].length), + ).toEqual([expect.objectContaining({ new_column_names: ['group_a'] })]); + }); + + it('loads category counts once every started card is complete', () => { + const filter = categoryFilter(); + openStation('transformation'); + ensureCategoricalDrafts(1); + const draft = component.transformationDrafts[0]; + draft.code = 'group_a'; + draft.rules = [{ value: 'a', filter }]; + component.transformationRuleModals = { toArray: () => [ruleModal(filter)] } as any; + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'all datasets', variable: 'group_a', data: { counts: { a: 7 } } }, + ], + }, + })); + fixture.detectChanges(); + mockExpService.loadDescriptiveOverview.mockClear(); + + component.finishTransformationRule(draft, 0); + fixture.detectChanges(); + + expect(component.countsDraftId).toBe(draft.id); + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith( + ['group_a'], + [ + expect.objectContaining({ + name: 'categorical_column_creator', + parameters: expect.objectContaining({ new_column_names: ['group_a'] }), + }), + ], + ['age'] + ); + expect(mockExpService.buildTransformationDescribePreprocessing).toHaveBeenCalled(); + expect(component.transformationStatistics).toEqual([ + { code: 'group_a', rows: [{ value: 'a', count: 7 }] }, + ]); + }); + }); + + describe('Task 4: repeated transformation cards', () => { + it('lets two K-means cards coexist', () => { + openStation('transformation'); + component.chooseTransformation('kmeans_cluster_creator'); + component.chooseTransformation('kmeans_cluster_creator'); + fixture.detectChanges(); + + const kmeans = component.cards().filter((step) => step.name === 'kmeans_cluster_creator'); + expect(kmeans).toHaveLength(2); + expect(kmeans[0].id).not.toBe(kmeans[1].id); + expect( + workflowSection('Transformation').querySelectorAll('.transformation-graph app-station-card').length, + ).toBe(2); + }); + + it('shows a duplicate name error and leaves the store unchanged', () => { + openStation('transformation'); + mockExpService.upsertTransformation({ + id: 'pca-1', + name: 'pca_column_creator', + parameters: { new_column_names: ['pca_pc1'] }, + }); + fixture.detectChanges(); + const before = structuredClone(mockExpService.appliedTransformations()); + + expect(component.columnNameError('pca-2', 'pca_pc1')).toBe('Name already used'); + expect(mockExpService.appliedTransformations()).toEqual(before); + }); + + it('keeps a nominal column a later step reads from being renamed', () => { + openStation('transformation'); + component.chooseTransformation('categorical_column_creator'); + const id = component.cards()[0].id; + mockExpService.outputsInUse.mockImplementation((stepId: string, next: string[]) => + stepId === id && !next.includes('grp') ? ['grp'] : [], + ); + const draft = component.draftForStep(id)!; + draft.code = 'grp_renamed'; + mockExpService.upsertTransformation.mockClear(); + + expect(component.columnNameError(id, 'grp_renamed')).toBe('Used by a later step: grp'); + component.onTransformationChange(); + expect(mockExpService.upsertTransformation).not.toHaveBeenCalled(); + expect(component.transformationDraftStatus(draft).label).not.toBe('Duplicate name'); + }); + + it('disables remove when a step has dependents', () => { + openStation('transformation'); + mockExpService.upsertTransformation({ + id: 'pcaA', + name: 'pca_column_creator', + parameters: { new_column_names: ['pca_a1'] }, + }); + mockExpService.upsertTransformation({ + id: 'kmeans', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km'], + reusable_preprocessing: { cluster_variables: ['pca_a1'] }, + }, + }); + mockExpService.transformationDependents.mockImplementation((id: string) => + id === 'pcaA' + ? [{ id: 'kmeans', name: 'kmeans_cluster_creator', parameters: { new_column_names: ['km'] } }] + : [], + ); + fixture.detectChanges(); + + expect(component.canRemoveTransformation('pcaA')).toBe(false); + const removeBtn = workflowSection('Transformation').querySelector( + '[data-step-id="pcaA"] .btn-remove-step-node', + ) as HTMLButtonElement | null; + expect(removeBtn?.disabled).toBe(true); + expect(removeBtn?.title).toContain('km'); + }); + + it('hides pca_column_creator in the chooser when absent from specs', () => { + mockExpService.availableCreatorNames.mockReturnValue([ + 'categorical_column_creator', + 'kmeans_cluster_creator', + ]); + openStation('transformation'); + fixture.detectChanges(); + + const titles = Array.from( + workflowSection('Transformation').querySelectorAll( + '.transformation-chooser .pipeline-subnode-title', + ), + ).map((el) => el.textContent?.trim()); + expect(titles.some((title) => /pca/i.test(title ?? ''))).toBe(false); + expect(titles).toContain('Nominal column from rules'); + expect(titles).toContain('K-means cluster column'); + }); }); }); diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.ts index 4640a518b..6a6351bf5 100644 --- a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.ts +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.component.ts @@ -1,351 +1,4870 @@ import { + ChangeDetectionStrategy, + ChangeDetectorRef, Component, - Input, - SimpleChanges, - OnChanges, + ElementRef, + computed, effect, - inject + inject, + QueryList, + signal, + untracked, + ViewChild, + ViewChildren, + output, + input, + OnDestroy } from '@angular/core'; -import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { NgTemplateOutlet } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { EChartsOption } from 'echarts'; +import { Subject, Subscription, takeUntil } from 'rxjs'; +import { ExperimentStudioService, PreprocessingConfig } from '../../../services/experiment-studio.service'; +import { escapeHtml } from '../../../core/html.utils'; import { ChartBuilderService } from '../visualisations/charts/chart-builder.service'; import { ChartRendererComponent } from '../visualisations/charts/charts-renderer/charts-renderer.component'; -import { EChartsOption } from 'echarts'; -import { ViewChildren, QueryList } from '@angular/core'; +import { HistogramComponent } from '../visualisations/histogram/histogram.component'; +import { + DATASET_VARIABLE_CODE, + HISTOGRAM_SPLIT_NONE, + HistogramGroupSeries, + HistogramSplitOption, + buildHistogramSplitOptions, + buildStackedHistogramSeries, + extractHistogramItems, + histogramCacheKey, +} from '../visualisations/histogram/histogram-data'; import { PdfExportService } from '../../../services/pdf-export.service'; -import { SpinnerComponent } from '../../shared/spinner/spinner.component'; import { buildGroupedBarChart } from '../visualisations/charts/renderers/grouped-bar-chart'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { + getFeaturewiseDescribeRows, + resolveDatasetDisplayLabel, +} from '../../../core/describe-result.utils'; +import { FilterConfigModalComponent } from '../shared/filter-config-modal/filter-config-modal.component'; +import { StationActionBarComponent } from '../shared/station-action-bar/station-action-bar.component'; +import { StationCardComponent, StationStatus } from '../shared/station-card/station-card.component'; +import { KMeansClusterSourceComponent } from './kmeans-cluster-source/kmeans-cluster-source.component'; +import { PcaColumnSourceComponent } from './pca-column-source/pca-column-source.component'; +import { + kmeansCreatorClusterChoices, + kmeansCreatorVariables, +} from '../../../core/kmeans-cluster-source.utils'; +import { BackendFilter, BackendRule } from '../../../models/filters.model'; +import { CsvExportService } from '../../../services/csv-export.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; +import { countFilterRules, formatFilterExpression } from '../../../core/filter-display.utils'; +import { ExperimentStudioGuideStateService } from '../guide/experiment-studio-guide-state.service'; +import { getAnalysisGuideLayout } from '../guide/experiment-studio-analysis-guide.util'; +import { AlgorithmNames } from '../../../core/constants/algorithm.constants'; +import { pluralize } from '../../../core/result-label.utils'; +import { filterOperatorPhrase } from '../../../core/filter-logic.utils'; +import { isCategoricalVariableType, isNumericalVariableType } from '../../../core/data-model.utils'; +import { + cloneOutlierRules, + createDefaultOutlierRule, + defaultFoldForStrategy, + hydrateOutlierRules, + isOutlierEligibleVariable, + OUTLIER_STRATEGIES, + OUTLIER_TAILS, + OutlierRule, + OutlierStrategy, + OutlierTail, + outlierStrategyLabel, + outlierTailLabel, + serializeOutlierRule, + serializeOutlierRules, + validateOutlierRule, +} from '../../../core/outlier-rules'; +import { collectVariableCodes } from '../../../core/variable-availability.utils'; +import { + CreatorName, + TransformationStep, + newStepId, + stepOutputNames as stepOutputNamesOf, +} from '../../../core/transformation-steps.utils'; + +type TabKey = 'Statistics' | 'Charts' | 'Histogram'; +type SummaryKind = 'source' | 'raw' | 'processed'; +type SectionKey = 'source' | 'raw' | 'setup' | 'filters' | 'processed' | 'transformation'; +type PipelineStep = 'source' | 'filters' | 'setup' | 'transformation'; +type PrepStep = 'missing' | 'outlier' | 'longitudinal'; +type DistributionSubTab = 'Numeric' | 'Nominal'; +type StatisticVariableType = 'numeric' | 'nominal'; +type PreprocessingStatus = 'none' | 'pending' | 'applied'; +type PrepKind = 'missing' | 'outlier' | 'longitudinal'; + +type SummaryAnchorKey = 'charts' | 'details' | 'export'; + +/** + * Where each summary surface wears a `data-guide` anchor. They are not uniform: the Raw + * surface owns the overlay-tab and export anchors, the Processed surface only its details + * panel, and step 0 is a read-only duplicate of the Raw surface, so it wears none — the + * same anchor on a hidden node would hand the tour or a QA selector the wrong surface. + */ +/** Backend step docs arrive as prose whose bullets are '-', '*' or '\u2022'; all of them + become real list items, so the block reads like the algorithm documentation. */ +const DOCUMENTATION_BULLET = /^[-*\u2022\u00b7]\s+/; + +const SUMMARY_GUIDE_ANCHORS: Record<SummaryKind, Record<SummaryAnchorKey, string | null>> = { + source: { charts: null, details: null, export: null }, + raw: { charts: 'analysis-charts', details: 'analysis-raw-details', export: 'analysis-export' }, + processed: { charts: null, details: 'analysis-processed-details', export: null }, +}; + +type MissingAction = 'no_action' | 'drop' | 'mean' | 'median' | 'constant'; +type LongitudinalStrategy = 'first' | 'second' | 'diff'; + +/** + * The record-level rule behind one category. exaflow takes either a group of rules or a + * single condition, and saved experiments hold both shapes - so the draft keeps whichever + * it was given instead of pretending a bare condition cannot exist. + */ +type CategoryFilter = BackendFilter | BackendRule; + +interface TransformationRule { + value: string; + filter: CategoryFilter | null; +} + +/** One "Create new categorical column" card. The stage holds an ordered list. */ +interface TransformationColumnDraft { + /** Matches the store step id so cards() and drafts stay aligned. */ + id: string; + code: string; + defaultEnumeration: string; + rules: TransformationRule[]; + open: boolean; + /** Rule index whose filter editor is expanded; at most one open per card. */ + openRuleIndex?: number | null; + /** Picked from the type chooser (or restored from the store). */ + chosen?: boolean; +} + +const CREATOR_CHOOSER_COPY: Record<CreatorName, { icon: string; title: string; subtitle: string }> = { + categorical_column_creator: { + icon: 'fas fa-tags', + title: 'Nominal column from rules', + subtitle: 'Name categories and pick each one\'s records with a filter — e.g. MMSE below 24 → ‘impaired’', + }, + kmeans_cluster_creator: { + icon: 'fas fa-diagram-project', + title: 'K-means cluster column', + subtitle: 'Group records by similarity and use the cluster as a nominal variable', + }, + pca_column_creator: { + icon: 'fas fa-chart-line', + title: 'PCA component columns', + subtitle: 'Project numerical variables onto principal components', + }, +}; + +/** A summary cell as a number: tolerates grouping commas and a trailing %; null when not numeric. */ +function parseNumber(value: unknown): number | null { + const text = String(value ?? '').replace(/[,%]/g, '').trim(); + const number = Number(text); + return text !== '' && Number.isFinite(number) ? number : null; +} + +function emptyTransformationDraft(id = newStepId()): TransformationColumnDraft { + return { id, code: '', defaultEnumeration: '', rules: [], open: true, openRuleIndex: null }; +} + +/** One sub-step of a pipeline stage, rendered on the pipeline-overview rail. */ +interface PipelineSubNode { + // Preprocessing stations use fixed ids; transformation sub-nodes carry a + // `transformation:<code>` id so the rail can list one card per derived column. + id: string; + icon: string; + title: string; + subtitle: string; + statusLabel: string; + statusTone: 'applied' | 'default' | 'pending'; + /** Optional consequence line (e.g. rows the default drop-NaN would remove). */ + impact?: string; +} + +type MetricKey = + | 'num_dtps' | 'num_na' | 'num_total' + | 'mean' | 'std' | 'min' | 'q1' | 'q2' | 'q3' | 'max'; + +interface SummaryView { + activeTab: TabKey; + data: PivotBlock[]; + featurewiseRows: any[]; + isLoading: boolean; + showDistributions: boolean; + distributionSubTab: DistributionSubTab; + nonNominalVariables: VariableRow[]; + nominalVariables: VariableRow[]; + chartsForBoxPlot: EChartsOption[][]; + chartsForNominal: EChartsOption[][]; + histogramDataByVariable: Record<string, HistogramPreviewData>; + histogramLoadingByVariable: Record<string, boolean>; + histogramErrorByVariable: Record<string, string>; + histogramSubscriptionsByVariable: Record<string, Subscription>; + histogramRequestIdByVariable: Record<string, number>; + histogramSplitByVariable: Record<string, string>; + activeBoxPlotIndex: number; + activeNominalIndex: number; + selectedStatisticKey: string | null; +} + +interface PivotBlock { + code?: string; + name: string; + columns: string[]; + rows: Array<{ metric: string; values: Record<string, string> }>; +} + +interface VariableRow { + code: string; + name?: string; + label?: string; + type?: string; + enumerations?: Array<{ code?: string; label?: string; name?: string }>; +} + +interface EnumOption { + code: string; + label: string; +} + +interface HistogramPreviewData { + bins: string[]; + counts: Array<number | null>; + variableName: string; + variableType?: string; + groups?: HistogramGroupSeries[]; + splitLabel?: string; +} + +interface PreprocessingRule { + variableCode: string; + action: MissingAction; + value: string; + enabled: boolean; +} + +interface PreprocessingGroup { + key: 'pending' | 'applied' | 'default' | 'not-applied'; + title: string; + variables: VariableRow[]; +} + +interface OutlierPreviewRow { + variable: string; + dataset: string; + strategy: string; + tail: string; + fold: string; + lowerBound: string; + upperBound: string; + lowerOutliers: string; + upperOutliers: string; + totalOutliers: string; + outlierPercentage: string; +} + +export interface DescriptiveProgressState { + pendingChangeCount: number; + preprocessingStatus: PreprocessingStatus; + transformationStatusLabel: string; +} + +@Component({ + selector: 'app-statistic-analysis-panel', + imports: [ChartRendererComponent, HistogramComponent, FormsModule, NgTemplateOutlet, FilterConfigModalComponent, StationActionBarComponent, StationCardComponent, KMeansClusterSourceComponent, PcaColumnSourceComponent], + templateUrl: './statistic-analysis-panel.component.html', + // Order is the cascade: shell + pipeline canvas, then preprocessing stations, + // then shared controls / result surfaces + responsive overrides. Concatenated in this order. + styleUrls: [ + './statistic-analysis-panel.component.css', + './statistic-analysis-panel.preprocessing.css', + './statistic-analysis-panel.results.css', + ], + host: { + '(document:click)': 'closeBatchMenuOnOutsideClick($event)', + }, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class StatisticAnalysisPanelComponent implements OnDestroy { + readonly processedDataInput = input<PivotBlock[]>([], { alias: 'processedData' }); + processedData: PivotBlock[] = []; + readonly variables = input<unknown[]>([]); + readonly filters = input<unknown[]>([]); + readonly progressStateChange = output<DescriptiveProgressState>(); + @ViewChild('sourceSection') + sourceSection?: ElementRef<HTMLElement>; + @ViewChild('rawSection') + rawSection?: ElementRef<HTMLElement>; + @ViewChild('setupSection') + setupSection?: ElementRef<HTMLElement>; + @ViewChild('filtersSection') + filtersSection?: ElementRef<HTMLElement>; + @ViewChild('processedSection') + processedSection?: ElementRef<HTMLElement>; + @ViewChild('transformationSection') + transformationSection?: ElementRef<HTMLElement>; + @ViewChildren('ruleModal') + transformationRuleModals?: QueryList<FilterConfigModalComponent>; + + expStudioService = inject(ExperimentStudioService); + private readonly destroyed = new Subject<void>(); + private studioNavigation = inject(ExperimentStudioNavigationService); + private chartBuilder = inject(ChartBuilderService); + private pdfExportService = inject(PdfExportService); + private csvExportService = inject(CsvExportService); + private cdr = inject(ChangeDetectorRef); + private runtimeEnvService = inject(RuntimeEnvService); + readonly mipVersion = this.runtimeEnvService.mipVersion; + + rawSummary = this.createEmptySummary(true); + processedSummary = this.createEmptySummary(false); + /** Step 0 snapshot: the same `SummaryView` the Raw and Processed surfaces use. */ + sourceSummary = this.createEmptySummary(true); + pendingPreprocessingRules: Record<string, PreprocessingRule> = {}; + appliedPreprocessingRules: Record<string, PreprocessingRule> = {}; + appliedLongitudinalEnabled = false; + longitudinalVisit1 = ''; + longitudinalVisit2 = ''; + appliedLongitudinalVisit1 = ''; + appliedLongitudinalVisit2 = ''; + pendingLongitudinalStrategies: Record<string, LongitudinalStrategy> = {}; + appliedLongitudinalStrategies: Record<string, LongitudinalStrategy> = {}; + pendingOutlierRules: Record<string, OutlierRule> = {}; + appliedOutlierRules: Record<string, OutlierRule> = {}; + preprocessingStatus: PreprocessingStatus = 'none'; + private userPreprocessingApplied = false; + isApplyingPreprocessing = false; + preprocessingValidationErrors: Record<string, string> = {}; + outlierValidationErrors: Record<string, string> = {}; + outlierPreviewRows: OutlierPreviewRow[] = []; + outlierPreviewError = ''; + /** A card's own Apply that failed: its message, shown in that card's footer. */ + preprocessingApplyError: Record<string, string | undefined> = {}; + isLoadingOutlierPreview = false; + prepSearch: Record<PrepKind, string> = { + missing: '', + outlier: '', + longitudinal: '', + }; + selectedPrepCode: Record<PrepKind, string | null> = { + missing: null, + outlier: null, + longitudinal: null, + }; + // Record<string, ..>: the shared summary template indexes it with its own kind. + statisticsSearch: Record<string, string> = { + source: '', + raw: '', + processed: '', + }; + isExporting = false; + isLoading = true; + readonly sectionOpen = signal<Record<SectionKey, boolean>>({ + source: false, + raw: false, + setup: false, + filters: false, + processed: false, + transformation: false, + }); + readonly summaryExpanded = signal<{ filters: boolean; setup: boolean }>({ + filters: false, + setup: false, + }); + preprocessingStepOpen: Record<'missing' | 'outlier' | 'longitudinal' | 'transformation', boolean> = { + missing: true, + outlier: true, + longitudinal: true, + transformation: true, + }; + + readonly addedSteps = signal<Record<'filters' | 'setup' | 'transformation', boolean>>({ + filters: false, + setup: false, + transformation: false, + }); + readonly showUnappliedChangesWarning = signal(false); + + isStepAdded(step: 'filters' | 'setup' | 'transformation'): boolean { + if (this.addedSteps()[step]) return true; + if (step === 'filters') return this.filterRuleCount() > 0; + if (step === 'transformation') { + // Prefer the ordered store: singleton-map writes after Apply must not hide derived columns. + if (this.cards().some((candidate) => stepOutputNamesOf(candidate).length > 0)) return true; + } + const applied = this.expStudioService.appliedPreprocessingConfig(); + if (!applied) return false; + if (step === 'transformation') { + return !!applied['categorical_column_creator'] || !!applied['kmeans_cluster_creator']; + } + return !!( + applied['missing_values_handler'] || + applied['outlier_winsorizer'] || + applied['longitudinal_transformer'] + ); + } + + /** Opening a stage is a read: it shows the editor, it never writes the request. */ + addStep(step: 'filters' | 'setup' | 'transformation'): void { + this.addedSteps.update((s) => ({ ...s, [step]: true })); + if (step === 'setup') this.ensureDefaultRulesForCurrentSelection(); + this.goToSection(step); + this.cdr.markForCheck(); + } + + removeStep(step: 'filters' | 'setup' | 'transformation'): void { + this.addedSteps.update((s) => ({ ...s, [step]: false })); + if (step === 'filters') { + this.rawPreviewFilter = undefined; + this.expStudioService.setFilterLogic(null); + this.sectionOpen.update((open) => ({ ...open, filters: false, raw: false })); + } else if (step === 'setup') { + this.appliedPreprocessingRules = {}; + this.pendingPreprocessingRules = {}; + this.pendingOutlierRules = {}; + this.appliedOutlierRules = {}; + this.userEnabledOutliers.set(false); + this.persistAppliedDescriptivePreprocessing(null); + this.preprocessingStatus = 'none'; + this.userPreprocessingApplied = false; + this.processedDescribeSeq++; + this.clearProcessedPreviewSubscription(); + this.clearHistogramSubscriptions(this.processedSummary); + this.processedSummary = this.createEmptySummary(false); + this.processedSummaryKey = ''; + this.sectionOpen.update((open) => ({ ...open, setup: false, processed: false })); + } else if (step === 'transformation') { + this.resetTransformation(); + this.sectionOpen.update((open) => ({ ...open, transformation: false })); + } + this.cdr.markForCheck(); + } + + /** + * The station footer's primary slot commits pending conditions. An empty builder has + * nothing to commit and nothing to keep — an optional step with no rules is exactly the + * default collapsed step — so the same slot closes the station and hands the pipeline back. + */ + commitOrCloseFilterStation(modal: FilterConfigModalComponent): void { + if (modal.activeRulesCount() > 0) { + modal.saveFilters(); + return; + } + this.removeStep('filters'); + this.requestSectionScroll('filters'); + } + + /** Folds the stage's editor and preview; nothing is removed or discarded. */ + collapseStep(step: 'filters' | 'setup' | 'transformation'): void { + if (step === 'filters') return this.foldFilterStation(); + if (step === 'setup') return this.foldPreprocessingStation(); + this.sectionOpen.update((open) => ({ ...open, transformation: false })); + this.cdr.markForCheck(); + this.requestSectionScroll('transformation'); + } + + /** A click anywhere on a step's header toggles it, except on the header's own buttons + * (the title, Remove, Revert, Customize) and at the end of a text selection. */ + onStepHeaderClick(step: PipelineStep, event: Event): void { + if ((event.target as Element).closest('button')) return; + if (window.getSelection()?.toString()) return; + this.toggleStep(step); + } + + /** Folds an open step and opens a folded one; opening an optional step adds it. */ + toggleStep(step: PipelineStep): void { + if (step === 'source') return this.toggleSourcePreview(); + if (this.isStepOpen(step)) this.collapseStep(step); + else this.addStep(step); + } + + /** A step outside the pipeline is never open, whatever its section flag says. */ + isStepOpen(step: PipelineStep): boolean { + const open = this.sectionOpen(); + if (step === 'source') return open.source; + if (!this.isStepAdded(step)) return false; + if (step === 'filters') return open.filters || open.raw; + if (step === 'setup') return open.setup || open.processed; + return open.transformation; + } + + /** Apply saved the rules; fold the editor. Preview data is what opens the tables. */ + private foldFilterStation(): void { + this.sectionOpen.update((open) => ({ ...open, filters: false, raw: false })); + this.cdr.markForCheck(); + this.requestSectionScroll('filters'); + } + + /** + * Cohort Filtering's "Preview data": describe the cohort under the rules the editor + * holds — unapplied, including "no rules yet" — and hand the result to the Raw + * summary. Nothing is written; Apply (`saveFilters`) stays the only path to the + * request, and an invalid builder keeps the user on the editor with its own error. + */ + previewFilterData(modal: FilterConfigModalComponent): void { + const pendingFilters = modal.exportFilterLogic() as BackendFilter | null; + if (modal.filterError()) { + this.cdr.markForCheck(); + return; + } + this.rawPreviewFilter = pendingFilters ?? null; + this.goToSection('raw'); + } + + /** + * Apply wrote the pending rules. Drop any preview pin so later Preview data follows + * the store; do not open the Raw tables — that is Preview data's job. + */ + onCohortFiltersApplied(): void { + this.rawPreviewFilter = undefined; + this.foldFilterStation(); + } + + /** Clear drops the stored cohort, which retires the preview pinned to it. */ + clearFilterRules(modal: FilterConfigModalComponent): void { + this.rawPreviewFilter = undefined; + modal.clearFilters(); + } + + get activeStagesCount(): number { + return (['filters', 'setup', 'transformation'] as const).filter((step) => this.isStepAdded(step)).length; + } + + /** Track if outlier section is explicitly enabled by user */ + readonly userEnabledOutliers = signal<boolean>(false); + + /** + * Which outlier card the editor shows: the station once the step is opened or holds a + * rule, the dashed add card while it is still dormant. The rail does not read this — + * see `outlierSubNodeApplied`. + */ + get showOutliersSection(): boolean { + if (this.userEnabledOutliers()) return true; + const hasApplied = Object.values(this.appliedOutlierRules).some((r) => r && r.enabled); + const hasPending = Object.values(this.pendingOutlierRules).some((r) => r && r.enabled); + return hasApplied || hasPending; + } + + enableOutlierHandling(): void { + this.userEnabledOutliers.set(true); + this.preprocessingStepOpen.outlier = true; + this.cdr.markForCheck(); + } + + removeOutlierHandling(): void { + this.userEnabledOutliers.set(false); + for (const code of Object.keys(this.pendingOutlierRules)) { + if (this.pendingOutlierRules[code]) { + this.pendingOutlierRules[code].enabled = false; + } + } + for (const code of Object.keys(this.appliedOutlierRules)) { + if (this.appliedOutlierRules[code]) { + this.appliedOutlierRules[code].enabled = false; + } + } + this.cdr.markForCheck(); + } + + finishDataHandling(): void { + if (this.pendingChangeCount > 0 || this.transformationHasPendingChange) { + this.showUnappliedChangesWarning.set(true); + this.cdr.markForCheck(); + return; + } + this.studioNavigation.navigateToSection('algorithm-section'); + } + + applyAndContinue(): void { + this.showUnappliedChangesWarning.set(false); + if (this.pendingChangeCount > 0) { + this.continueAfterApply = true; + this.applyPreprocessing(); + return; + } + this.studioNavigation.navigateToSection('algorithm-section'); + } + + discardAndContinue(): void { + this.showUnappliedChangesWarning.set(false); + this.resetChanges(); + this.discardPendingTransformation(); + this.studioNavigation.navigateToSection('algorithm-section'); + } + + /** Header chip tone; the label reads off it so copy and colour cannot drift. */ + get preprocessingBadgeTone(): 'applied' | 'pending' | 'default' { + if (this.pendingChangeCount > 0) return 'pending'; + return this.preprocessingStatus === 'applied' ? 'applied' : 'default'; + } + + get preprocessingStatusLabel(): string { + switch (this.preprocessingBadgeTone) { + case 'pending': + return `In run · ${pluralize(this.pendingChangeCount, 'change')} not applied`; + case 'applied': { + const count = this.appliedPreprocessingCount; + return count > 1 ? `${count} steps applied ✓` : 'Applied ✓'; + } + default: + return 'In run · Default'; + } + } + + /** Terminal card's preprocessing fact: default drop-NaN, or the applied step count. */ + get terminalPreprocessingLine(): string { + if (this.preprocessingBadgeTone === 'default') return 'Missing values: remove rows (default)'; + return `Preprocessing: ${pluralize(this.appliedPreprocessingCount, 'step')}`; + } + + /** + * Sub-steps the pipeline will actually run, read from the persisted request + * config so the rail can never disagree with the next run. Missing values is + * always present: default NA removal is an active step even when untouched. + */ + get appliedPreprocessingSubNodes(): PipelineSubNode[] { + const config = this.expStudioService.appliedPreprocessingConfig() ?? {}; + const nodes: PipelineSubNode[] = [this.missingValuesSubNode(config)]; + + const outlier = config['outlier_winsorizer'] as { strategies?: Record<string, unknown> } | undefined; + const outlierStrategies = Object.values(outlier?.strategies ?? {}); + if (outlierStrategies.length) { + nodes.push({ + id: 'outlier', + icon: 'fas fa-line-chart', + title: 'Outlier Winsorizer', + subtitle: `Extreme values capped on ${outlierStrategies.length} ${outlierStrategies.length === 1 ? 'variable' : 'numerical variables'} (${Array.from(new Set(outlierStrategies.map((s) => outlierStrategyLabel(String(s))))).join(', ')})`, + statusLabel: 'Applied', + statusTone: this.hasPendingOutlierChanges ? 'pending' : 'applied', + }); + } + + const longitudinal = config['longitudinal_transformer'] as + | { visit1?: unknown; visit2?: unknown; strategies?: Record<string, unknown> } + | undefined; + if (longitudinal && this.isLongitudinalModel) { + const count = Object.keys(longitudinal.strategies ?? {}).length; + nodes.push({ + id: 'longitudinal', + icon: 'fas fa-code-compare', + title: 'Longitudinal Comparison', + subtitle: `${this.visitLabel(longitudinal.visit1)} vs ${this.visitLabel(longitudinal.visit2)} strategy configured on ${count} ${count === 1 ? 'variable' : 'variables'}`, + statusLabel: 'Applied', + statusTone: this.pendingLongitudinalChangeCount > 0 ? 'pending' : 'applied', + }); + } + + return nodes; + } + + /** + * Rail counterpart of the outlier step: whether the rail already draws it as a node. + * The dashed row that creates a sub-step must answer to the same source as the rail's own + * rows — the persisted request — so a sub-step always has exactly one representation. The + * editor's `showOutliersSection` also turns true on a merely opened step, which used to + * leave an untouched step that was closed again with neither a node nor an add row. + */ + get outlierSubNodeApplied(): boolean { + return this.appliedPreprocessingSubNodes.some((node) => node.id === 'outlier'); + } + + get appliedPreprocessingCount(): number { + return this.appliedPreprocessingSubNodes.filter((node) => node.statusTone === 'applied').length; + } + + /** A derived column only exists once its config is persisted; otherwise the stage stays empty. */ + get appliedTransformationSubNodes(): PipelineSubNode[] { + const statusTone: PipelineSubNode['statusTone'] = + this.transformationStatusLabel === 'Applied' ? 'applied' : 'pending'; + const nodes: PipelineSubNode[] = []; + for (const step of this.cards()) { + const outputs = stepOutputNamesOf(step); + if (!outputs.length) continue; + if (step.name === 'categorical_column_creator') { + const rules = (step.parameters['rules'] as Record<string, unknown> | undefined) ?? {}; + const ruleCount = Object.keys(rules).length; + const fallback = String(step.parameters['default_enumeration'] ?? '').trim(); + nodes.push({ + id: `transformation:${step.id}`, + icon: 'fas fa-tag', + title: `Derived column: ${outputs[0]}`, + subtitle: `${ruleCount} ${ruleCount === 1 ? 'category rule' : 'category rules'} configured${fallback ? ` · default: ${fallback}` : ''}`, + statusLabel: 'Applied', + statusTone, + }); + continue; + } + if (step.name === 'kmeans_cluster_creator') { + const variables = kmeansCreatorVariables(step.parameters); + const clusterCount = kmeansCreatorClusterChoices(step.parameters).length; + nodes.push({ + id: `transformation:${step.id}`, + icon: 'fas fa-diagram-project', + title: `K-means clusters: ${outputs[0]}`, + subtitle: `${clusterCount} clusters from ${variables.join(', ')}`, + statusLabel: 'Applied', + statusTone: 'applied', + }); + continue; + } + nodes.push({ + id: `transformation:${step.id}`, + icon: CREATOR_CHOOSER_COPY[step.name]?.icon ?? 'fas fa-square-plus', + title: `${CREATOR_CHOOSER_COPY[step.name]?.title ?? step.name}: ${outputs.join(', ')}`, + subtitle: this.isCreatorAvailable(step.name) ? 'Configured' : 'Not available on this deployment', + statusLabel: this.isCreatorAvailable(step.name) ? 'Applied' : 'Unavailable', + statusTone: this.isCreatorAvailable(step.name) ? 'applied' : 'pending', + }); + } + return nodes; + } + + get appliedTransformationCount(): number { + return this.appliedTransformationSubNodes.length; + } + + /** Expands the owning stage and opens only the clicked station. */ + jumpToSubNode(stage: 'setup' | 'transformation', subNode: PipelineSubNode['id']): void { + this.goToSection(stage); + if (stage === 'transformation') { + this.preprocessingStepOpen.transformation = true; + return; + } + for (const step of ['missing', 'outlier', 'longitudinal'] as const) { + this.preprocessingStepOpen[step] = step === subNode; + } + if (subNode === 'outlier') this.userEnabledOutliers.set(true); + this.cdr.markForCheck(); + } + + /** A dashed rail row adds and opens its sub-step in one click, writing nothing. */ + addPreprocessingSubNode(subNode: 'outlier' | 'longitudinal'): void { + if (!this.isStepAdded('setup')) this.addStep('setup'); + this.jumpToSubNode('setup', subNode); + } + + /** Same for the Transformation stage: opens it on the type chooser at the end of its graph. */ + addTransformationSubNode(): void { + this.addStep('transformation'); + } + + isDraftShown(draft: TransformationColumnDraft): boolean { + return !!draft.chosen || this.draftHasWork(draft); + } + + /** Ordered cards from the store — one station per transformation step. */ + cards(): TransformationStep[] { + return this.expStudioService.appliedTransformations(); + } + + /** Template helper: output codes a step writes (`new_column_names`). */ + stepOutputNames(step: TransformationStep): string[] { + return stepOutputNamesOf(step); + } + + /** Creator types the deployment exposes in `/services/specifications/preprocessing`. */ + chooserCreators(): CreatorName[] { + return this.expStudioService.availableCreatorNames(); + } + + creatorChooserCopy(kind: CreatorName): { icon: string; title: string; subtitle: string } { + return CREATOR_CHOOSER_COPY[kind]; + } + + /** + * True when the editor should render for this creator. Empty specs before the catalog + * loads are not "unavailable" — only loaded && missing means the deployment placeholder. + */ + isCreatorAvailable(name: CreatorName): boolean { + if (!this.expStudioService.catalogLoaded()) return true; + return this.expStudioService.hasPreprocessingSpec(name); + } + + get kmeansCardShown(): boolean { + return this.cards().some((step) => step.name === 'kmeans_cluster_creator'); + } + + /** Nothing picked yet: the chooser is the whole stage, so it asks instead of offering. */ + get transformationChooserOnly(): boolean { + return this.cards().length === 0; + } + + /** + * Type chooser row: always appends a new store step (fresh id). Categorical cards also get a + * matching local draft for the rule editor. + */ + chooseTransformation(kind: CreatorName | 'categorical' | 'kmeans' | 'pca'): void { + const name = this.resolveCreatorName(kind); + const id = newStepId(); + this.expStudioService.upsertTransformation({ id, name, parameters: {} }); + if (name === 'categorical_column_creator') { + this.transformationDrafts = [ + ...this.transformationDrafts.filter((draft) => this.isDraftShown(draft)), + { ...emptyTransformationDraft(id), chosen: true, open: true }, + ]; + } + this.cardOpenState.update((state) => ({ ...state, [id]: true })); + this.cdr.markForCheck(); + } + + private resolveCreatorName(kind: CreatorName | 'categorical' | 'kmeans' | 'pca'): CreatorName { + if (kind === 'categorical') return 'categorical_column_creator'; + if (kind === 'kmeans') return 'kmeans_cluster_creator'; + if (kind === 'pca') return 'pca_column_creator'; + return kind; + } + + draftForStep(stepId: string): TransformationColumnDraft | undefined { + return this.transformationDrafts.find((draft) => draft.id === stepId); + } + + columnNameError(stepId: string, name: string): string | null { + const trimmed = name.trim(); + if (!trimmed) return null; + const cdeCodes = new Set(collectVariableCodes(this.expStudioService.selectedDataModel())); + if (cdeCodes.has(trimmed)) return 'Name already used'; + for (const step of this.cards()) { + if (step.id === stepId) continue; + // Ignore empty/incomplete placeholders — only real outputs collide. + if (stepOutputNamesOf(step).includes(trimmed)) return 'Name already used'; + } + // Among local categorical drafts, the first occurrence of a code wins (same as the + // old payload dedupe); later cards show the error and never overwrite the store. + for (const draft of this.transformationDrafts) { + if (draft.id === stepId) break; + if (draft.code.trim() === trimmed) return 'Name already used'; + } + // A nominal card writes one column: renaming it must not orphan a later step that reads it. + if (this.draftForStep(stepId)) { + const inUse = this.expStudioService.outputsInUse(stepId, [trimmed]); + if (inUse.length) return `Used by a later step: ${inUse.join(', ')}`; + } + return null; + } + + /** Stable callback for K-means / PCA cards (`[columnNameError]` input). */ + readonly kmeansColumnNameError = (stepId: string, name: string): string | null => + this.columnNameError(stepId, name); + + canRemoveTransformation(stepId: string): boolean { + return this.expStudioService.transformationDependents(stepId).length === 0; + } + + removeTransformationTooltip(stepId: string): string { + const dependents = this.expStudioService.transformationDependents(stepId); + if (!dependents.length) return 'Remove column'; + const names = dependents.flatMap((step) => stepOutputNamesOf(step)); + return names.length ? `Used by ${names.join(', ')}` : 'Used by a later step'; + } + + isCardOpen(stepId: string): boolean { + return this.cardOpenState()[stepId] ?? true; + } + + toggleCardOpen(stepId: string, open?: boolean): void { + this.cardOpenState.update((state) => ({ + ...state, + [stepId]: open ?? !this.isCardOpen(stepId), + })); + this.cdr.markForCheck(); + } + + stepStatus(step: TransformationStep): { label: string; tone: StationStatus } { + if (!this.isCreatorAvailable(step.name)) { + return { label: 'Unavailable', tone: 'pending' }; + } + if (step.name === 'categorical_column_creator') { + const draft = this.draftForStep(step.id); + return draft ? this.transformationDraftStatus(draft) : { label: 'Not set', tone: 'default' }; + } + if (stepOutputNamesOf(step).length) return { label: 'Applied', tone: 'applied' }; + return { label: 'Not set', tone: 'default' }; + } + + stepTitle(step: TransformationStep): string { + const copy = CREATOR_CHOOSER_COPY[step.name]; + const outputs = stepOutputNamesOf(step); + if (step.name === 'categorical_column_creator') { + const draft = this.draftForStep(step.id); + const code = draft?.code.trim() || outputs[0] || ''; + return code ? `Nominal column · ${code}` : 'Nominal column'; + } + if (outputs.length) return `${copy.title} · ${outputs[0]}`; + return copy.title; + } + + private missingValuesSubNode(config: Record<string, unknown>): PipelineSubNode { + const handler = config['missing_values_handler'] as + | { strategies?: Record<string, unknown> } + | undefined; + const entries = Object.entries(handler?.strategies ?? {}).filter(([, action]) => action !== 'no_action'); + const imputed = entries.filter(([, action]) => action !== 'drop'); + const pending = this.hasPendingMissingChanges ? 'pending' : 'applied'; + const droppedByDefault = this.rowsDroppedByDefault; + const impact = droppedByDefault != null + ? `−${droppedByDefault.toLocaleString()} records` + : undefined; + + if (imputed.length) { + const summary = imputed.slice(0, 3).map(([code, action]) => { + const variable = this.preprocessingVariables.find((v) => v.code === code); + return `${variable ? this.variableLabel(variable) : code}: ${this.missingStrategyLabel(String(action))}`; + }); + return { + id: 'missing', + icon: 'fas fa-eraser', + title: 'Missing Values Strategy', + subtitle: `Imputation configured for ${imputed.length} ${imputed.length === 1 ? 'variable' : 'variables'} (${summary.join(', ')}${imputed.length > 3 ? ', …' : ''})`, + statusLabel: 'Applied', + statusTone: pending, + }; + } + + if (entries.length) { + return { + id: 'missing', + icon: 'fas fa-eraser', + title: 'Missing Values Handler', + subtitle: `Rows with missing values removed for ${entries.length} ${entries.length === 1 ? 'variable' : 'variables'}`, + statusLabel: 'Applied', + statusTone: pending, + impact, + }; + } + + return { + id: 'missing', + icon: 'fas fa-eraser', + title: 'Missing values', + subtitle: `All ${pluralize(this.preprocessingVariables.length, 'variable')}: remove rows with a missing value (default)`, + statusLabel: 'In run · Default', + statusTone: 'default', + impact, + }; + } + + missingStrategyLabel(action: string): string { + return this.missingActions.find((item) => item.value === action)?.label ?? action; + } + + private visitLabel(code: unknown): string { + const value = String(code ?? ''); + return this.visitOptions.find((option) => option.code === value)?.label ?? value; + } + + get filteringStatusLabel(): string { + const count = this.filterRuleCount(); + if (count > 0) return `In run · ${pluralize(count, 'rule')}`; + return 'Not in run yet'; + } + + /** Builder match chip after Preview: filtered rows (raw summary) out of the unfiltered + * source snapshot. The processed summary is excluded: its drop is preprocessing, not the filter. */ + get filterPreviewStatus(): string { + const matched = this.getSummaryTotalRows(this.rawSummary); + const source = this.getSummaryTotalRows(this.sourceSummary); + if (matched === null || source === null) return ''; + // Never round a partial match to 0% or 100%: those read as "none" and "the filter does nothing". + const share = matched === source ? 100 + : matched === 0 || source === 0 ? 0 + : Math.min(99, Math.max(1, Math.round((matched / source) * 100))); + return `Matches ${matched.toLocaleString()} of ${source.toLocaleString()} records · ${share}%`; + } + + /** Header "Records out" per stage, read off the summaries already loaded; null hides it. + * With no stored rule the Raw summary is the unfiltered cohort, so it stands in for the + * source snapshot, which is only fetched when Raw data is previewed. A Raw summary + * pinned to an unapplied preview stands in for neither. */ + get recordsOut(): { source: number | null; filtered: number | null; processed: number | null } { + // A Raw summary pinned to unsaved preview rules does not describe the stored cohort. + const rawIsStoredCohort = this.rawSummaryKey === this.rawSummaryKeyFor(); + const filtered = rawIsStoredCohort ? this.getSummaryTotalRows(this.rawSummary) : null; + const source = this.getSummaryTotalRows(this.sourceSummary) + ?? (this.filterRuleCount() === 0 ? filtered : null); + return { source, filtered, processed: this.getSummaryTotalRows(this.processedSummary) }; + } + + /** Collapsed Cohort filtering: one row per top-level rule of the stored filter. */ + get cohortRuleRows(): Array<{ title: string; subtitle: string }> { + return (this.expStudioService.filterLogic()?.rules ?? []).map((node) => { + if ('rules' in node) { + const joiner = node.condition === 'OR' ? ' or ' : ' and '; + const title = node.rules + .map((child) => ('rules' in child ? '(…)' : this.filterConditionSentence(child))) + .join(joiner); + const scope = node.condition === 'OR' ? 'any' : 'all'; + return { title, subtitle: `Group · matches ${scope} of ${pluralize(countFilterRules(node), 'condition')}` }; + } + return { title: this.filterConditionSentence(node), subtitle: node.type === 'string' ? 'Categorical' : 'Numerical' }; + }); + } + + get cohortMatchesAny(): boolean { + return this.expStudioService.filterLogic()?.condition === 'OR'; + } + + /** "How to use" strip copy per expanded stage; K-means is projected into its card. */ + readonly howTo: Record<'source' | 'filters' | 'setup' | 'transformation' | 'kmeans' | 'pca', { steps: string[]; note: string }> = { + source: { + steps: ['Pick a variable on the left', 'Switch between Table, Charts and Histogram', 'Note gaps or odd ranges — fix them in the steps below'], + note: 'Read only — this preview never changes the data or the run.', + }, + filters: { + steps: ['Add a condition: variable, operator, value', 'Preview to see who matches', 'Apply to use the filter in the run'], + note: 'Records that match stay in the run. Everything else is left out — the raw data itself is never changed.', + }, + setup: { + steps: ['Pick a variable', 'Choose what happens to its missing values', 'Apply — the run uses your rules'], + note: "Variables you don't touch keep the default: rows with a missing value are removed.", + }, + transformation: { + steps: ['Choose the kind of column to add', 'Define it — category rules, or a K-means or PCA run', 'Done on a rule loads its counts, then Apply'], + note: 'The new column joins your variable list, so you can pick it as an outcome or predictor in Algorithm Selection.', + }, + kmeans: { + steps: ['Choose the variables to group by', 'Run the K-means report and check the clusters', 'Name the column and use it'], + note: 'Rows missing any chosen variable are left out. A finished K-means run can be reused if it used the same data model, datasets and filters.', + }, + pca: { + steps: ['Choose the variables to combine', 'Run the PCA report and pick the components', 'Name the columns and add them'], + note: 'No saved PCA experiment is needed. Rows missing any chosen variable are left out. A finished PCA run can be reused if it used the same data model, datasets and filters.', + }, + }; + + readonly missingActions: Array<{ value: MissingAction; label: string }> = [ + { value: 'drop', label: 'Remove rows' }, + { value: 'mean', label: 'Mean imputation' }, + { value: 'median', label: 'Median imputation' }, + { value: 'constant', label: 'Constant value' }, + ]; + + /** The card whose category counts replace its editor ('kmeans' = the cluster card). */ + previewDraftId: string | null = null; + /** The categorical card whose Done loaded the current counts; it shows their loading/error state. */ + countsDraftId: string | null = null; + /** Ordered cards; the stage always keeps at least one (possibly empty) draft. */ + transformationDrafts: TransformationColumnDraft[] = []; + /** Collapsed/expanded state per store step id (categorical drafts also keep `open`). */ + readonly cardOpenState = signal<Record<string, boolean>>({}); + transformationStatistics: Array<{ code: string; rows: Array<{ value: string; count: number | null }> }> = []; + isTransformationStatsLoading = false; + transformationStatisticsError = ''; + private transformationStatsRequestId = 0; + + readonly longitudinalStrategies: Array<{ value: LongitudinalStrategy; label: string }> = [ + { value: 'diff', label: 'Diff (Visit 2 - Visit 1)' }, + { value: 'first', label: 'Use visit 1' }, + { value: 'second', label: 'Use visit 2' }, + ]; + readonly outlierStrategies = OUTLIER_STRATEGIES; + readonly outlierTails = OUTLIER_TAILS; + + readonly metricOrder: Array<{ key: string; label: string }> = [ + { key: 'num_datapoints', label: 'Datapoints' }, + { key: 'num_missing', label: 'Missing' }, + { key: 'num_total', label: 'Total' }, + { key: 'mean', label: 'Mean' }, + { key: 'std', label: 'Standard Deviation' }, + { key: 'min', label: 'Min' }, + { key: 'q1', label: 'Q1' }, + { key: 'q2', label: 'Median' }, + { key: 'q3', label: 'Q3' }, + { key: 'max', label: 'Max' }, + ]; + + readonly metricLabel: Record<MetricKey, string> = { + num_dtps: 'Datapoints', + num_na: 'Missing', + num_total: 'Total', + mean: 'Mean', + std: 'Standard Deviation', + min: 'Min', + q1: 'Q1', + q2: 'Median', + q3: 'Q3', + max: 'Max', + }; + + private selectionKey = ''; + /** Cohort half of `selectionKey`, so a preprocessing write can be ignored on its own. */ + private cohortSelectionKey = ''; + /** + * JSON of the applied config written by this component. The selection effect + * rehydrates every card from that signal; this write must not, or a card Apply + * wipes the other cards' pending edits. + */ + private appliedConfigWrite = ''; + /** Card preview describes that card's pending rules over the applied rest. */ + private processedPreviewScope: PrepStep | 'all' = 'all'; + private rawSummaryKey = ''; + /** + * The cohort rules the Raw surface was last fetched with when they are unsaved + * editor state. `undefined` means the surface follows the stored `filterLogic()`, + * so the selection effect may refresh it; a value — including `null` for "no rules + * at all" — pins the Raw summary to what the modal holds, so Preview shows the + * cohort the user is composing rather than the older stored one. + */ + private rawPreviewFilter: BackendFilter | null | undefined = undefined; + /** Supersedes an in-flight raw describe so a late response cannot win. */ + private rawDescribeSeq = 0; + private processedSummaryKey = ''; + private sourceSummaryKey = ''; + private processedPreviewSubscription: Subscription | null = null; + private sourcePreviewSubscription: Subscription | null = null; + /** Supersedes in-flight processed/source describes so a late response cannot win. */ + private processedDescribeSeq = 0; + private sourceDescribeSeq = 0; + private scrollRequestId = 0; + private continueAfterApply = false; + private readonly scrollSectionRequest = signal<{ section: SectionKey; requestId: number } | null>(null); + private readonly guideState = inject(ExperimentStudioGuideStateService); + + constructor() { + effect(() => { + const layout = getAnalysisGuideLayout(this.guideState.activeStepId()); + if (!layout) return; + + if (layout.expandSection === 'none') { + this.collapseAllWorkflowSections(); + } else { + this.goToSection(layout.expandSection); + } + + if (layout.summaryKind && layout.summaryTab) { + this.setSummaryTab(layout.summaryKind, layout.summaryTab); + } + + if (layout.preprocessingStep) { + this.preprocessingStepOpen = { + missing: layout.preprocessingStep === 'missing', + outlier: layout.preprocessingStep === 'outlier', + longitudinal: layout.preprocessingStep === 'longitudinal', + transformation: false, + }; + } + this.cdr.markForCheck(); + }); + + effect(() => { + const incomingProcessedData = this.processedDataInput(); + this.processedData = incomingProcessedData; + this.isLoading = incomingProcessedData.length === 0; + this.cdr.markForCheck(); + }); + + effect(() => { + const variables = this.expStudioService.selectedVariables(); + const filters = this.expStudioService.selectedFilters(); + const filterLogic = this.expStudioService.filterLogic(); + const appliedPreprocessing = this.expStudioService.appliedPreprocessingConfig(); + const cohortKey = this.buildSelectionKey(variables, filters, filterLogic); + const nextSelectionKey = this.buildSelectionKey(variables, filters, filterLogic, appliedPreprocessing); + const echoed = this.appliedConfigWrite; + if ( + echoed + && echoed === JSON.stringify(appliedPreprocessing ?? null) + && cohortKey === this.cohortSelectionKey + ) { + this.appliedConfigWrite = ''; + this.selectionKey = nextSelectionKey; + return; + } + this.appliedConfigWrite = ''; + + if (nextSelectionKey === this.selectionKey) return; + this.cohortSelectionKey = cohortKey; + this.selectionKey = nextSelectionKey; + this.reconcilePreprocessingForSelection(); + + if (!variables.length && !filters.length) { + this.rawDescribeSeq++; + this.processedDescribeSeq++; + this.sourceDescribeSeq++; + this.clearProcessedPreviewSubscription(); + this.clearSourcePreviewSubscription(); + this.clearHistogramSubscriptions(this.rawSummary); + this.clearHistogramSubscriptions(this.sourceSummary); + this.clearHistogramSubscriptions(this.processedSummary); + this.rawSummary = this.createEmptySummary(false); + this.processedSummary = this.createEmptySummary(false); + this.sourceSummary = this.createEmptySummary(false); + this.rawSummaryKey = ''; + this.sourceSummaryKey = ''; + this.processedSummaryKey = ''; + this.processedData = []; + this.isLoading = false; + this.expStudioService.clearDataExclusionWarnings(); + this.cdr.markForCheck(); + return; + } + + // Raw is "the variables as stored": it is requested with a null preprocessing + // config, so applying preprocessing cannot change its numbers and must not + // spend a second full federated describe on it. A Cohort Filtering preview owns + // the surface instead, and ensureRawSummary re-runs the describe with those + // unsaved rules rather than falling back to the stored cohort read above. + this.ensureRawSummary(); + this.fetchProcessedPreview(); + // untracked: the snapshot refreshes with the selection, but sectionOpen must + // not become a dependency of this effect or every section switch refetches raw. + if (untracked(this.sectionOpen).source) this.ensureSourceSummary(); + }); + + effect(() => { + const request = this.scrollSectionRequest(); + if (!request) return; + + setTimeout(() => { + if (this.scrollSectionRequest()?.requestId !== request.requestId) return; + this.scrollSectionIntoView(request.section); + if (this.scrollSectionRequest()?.requestId === request.requestId) { + this.scrollSectionRequest.set(null); + } + }); + }); + } + + ngOnDestroy(): void { + this.destroyed.next(); + this.destroyed.complete(); + this.clearProcessedPreviewSubscription(); + this.clearSourcePreviewSubscription(); + this.clearHistogramSubscriptions(this.rawSummary); + this.clearHistogramSubscriptions(this.sourceSummary); + this.clearHistogramSubscriptions(this.processedSummary); + } + + get pendingChangeCount(): number { + return this.pendingMissingChangeCount + this.pendingOutlierChangeCount + this.pendingLongitudinalChangeCount; + } + + readonly filterRuleCount = computed(() => + countFilterRules(this.expStudioService.filterLogic()) + ); + + /** Drafts that already form a valid column config (code + at least one filtered rule). */ + private transformationConfigs(): Record<string, unknown>[] { + return this.transformationDrafts + .map((draft) => this.buildTransformationConfigForDraft(draft)) + .filter((config): config is Record<string, unknown> => !!config); + } + + /** A draft counts as touched once the user typed any column name, default, or rule. */ + private draftHasWork(draft: TransformationColumnDraft): boolean { + return ( + draft.code.trim().length > 0 || + draft.defaultEnumeration.trim().length > 0 || + draft.rules.some((rule) => rule.value.trim().length > 0 || !!rule.filter) + ); + } + + /** Two columns cannot share a name: exaflow errors when `code` already exists. */ + get transformationHasDuplicateCodes(): boolean { + const codes = this.transformationDrafts.map((draft) => draft.code.trim()).filter((code) => code.length > 0); + return new Set(codes).size !== codes.length; + } + + get transformationHasPendingChange(): boolean { + if (this.transformationHasDuplicateCodes) return true; + return this.transformationDrafts.some( + (draft) => this.draftHasWork(draft) && (this.hasUnfinishedCategory(draft) || !this.buildTransformationConfigForDraft(draft)) + ); + } + + /** A category without a name or a rule is left out of the saved column. */ + private hasUnfinishedCategory(draft: TransformationColumnDraft): boolean { + return draft.rules.some((rule) => !rule.value.trim() || !rule.filter); + } + + get transformationStatusLabel(): string { + const validCount = this.transformationConfigs().length; + const hasAppliedColumn = validCount > 0 + || this.cards().some((step) => step.name !== 'categorical_column_creator' && stepOutputNamesOf(step).length > 0); + if (hasAppliedColumn && !this.transformationHasPendingChange) return 'Applied'; + if ( + hasAppliedColumn + || this.transformationHasPendingChange + || this.transformationDrafts.some((draft) => this.draftHasWork(draft)) + || this.cards().length > 0 + ) { + return 'Not applied'; + } + return 'Not set'; + } + + /** Pipeline-header badge: shows the derived-column count once applied. */ + get transformationBadgeLabel(): string { + const count = this.appliedTransformationCount; + if (this.transformationStatusLabel === 'Applied' && count > 0) { + return `${count} ${count === 1 ? 'Transformation' : 'Transformations'} active`; + } + return this.transformationStatusLabel === 'Applied' ? 'Applied' : 'Not in run yet'; + } + + + /** Status chip for the K-means cluster column card on the Transformation step. */ + get kmeansClusterStatusLabel(): string { + return this.cards().some( + (step) => step.name === 'kmeans_cluster_creator' && stepOutputNamesOf(step).length > 0, + ) + ? 'Applied' + : 'Not set'; + } + + /** True when a named category on any card is still missing an applied filter. */ + get transformationRulesNeedFilters(): boolean { + return this.transformationDrafts.some((draft) => { + if (!draft.code.trim()) return false; + const named = draft.rules.filter((rule) => rule.value.trim().length > 0); + return named.length > 0 && named.some((rule) => !rule.filter); + }); + } + + get pendingMissingChangeCount(): number { + return this.hasPendingMissingChanges ? 1 : 0; + } + + get hasPendingMissingChanges(): boolean { + const currentCodes = this.currentPreprocessingCodeSet(); + const codes = new Set( + [ + ...Array.from(currentCodes), + ...Object.keys(this.pendingPreprocessingRules), + ...Object.keys(this.appliedPreprocessingRules), + ].filter((code) => currentCodes.has(code)) + ); + for (const code of codes) { + const appliedRule = this.appliedPreprocessingRules[code]; + const pendingRule = this.pendingPreprocessingRules[code] + ?? (appliedRule ? undefined : this.defaultRule(code)); + // Newly added variables sit on the implicit NA-removal default; that + // doesn't count as a pending change because no user action is required. + if (this.isDefaultMissingRule(pendingRule) && !this.hasAppliedPreprocessing(code)) continue; + if (this.serializeRule(pendingRule) !== this.serializeRule(appliedRule)) { + return true; + } + } + return false; + } + + get pendingOutlierChangeCount(): number { + return this.hasPendingOutlierChanges ? 1 : 0; + } + + get hasPendingOutlierChanges(): boolean { + const currentCodes = this.currentOutlierPreprocessingCodeSet(); + const codes = new Set( + [ + ...Array.from(currentCodes), + ...Object.keys(this.pendingOutlierRules), + ...Object.keys(this.appliedOutlierRules), + ].filter((code) => currentCodes.has(code)) + ); + for (const code of codes) { + const pendingRule = this.pendingOutlierRules[code] + ?? (this.appliedOutlierRules[code] ? undefined : this.defaultOutlierRule(code)); + if (serializeOutlierRule(pendingRule) !== serializeOutlierRule(this.appliedOutlierRules[code])) { + return true; + } + } + return false; + } + + get pendingLongitudinalChangeCount(): number { + if (!this.isLongitudinalModel) return 0; + return this.serializeLongitudinalState(false) === this.serializeLongitudinalState(true) ? 0 : 1; + } + + get preprocessingVariables(): VariableRow[] { + const selectedVars = this.expStudioService.selectedVariables() as VariableRow[]; + return Array.from( + new Map(selectedVars.map((variable) => [variable.code, variable])).values() + ).filter((variable) => !!variable?.code); + } + + filteredPrepVariables(kind: PrepKind): VariableRow[] { + const query = this.prepSearch[kind].trim().toLowerCase(); + const pool = kind === 'outlier' ? this.outlierPreprocessingVariables : this.preprocessingVariables; + if (!query) return pool; + return pool.filter((variable) => { + const label = this.variableLabel(variable).toLowerCase(); + return label.includes(query) || String(variable.code).toLowerCase().includes(query); + }); + } + + get outlierPreprocessingVariables(): VariableRow[] { + return this.preprocessingVariables.filter((variable) => isOutlierEligibleVariable(variable)); + } + + prepGroups(kind: PrepKind): PreprocessingGroup[] { + return this.buildPreprocessingGroups( + this.filteredPrepVariables(kind), + (variable) => this.prepVariableHasPendingChange(kind, variable), + (variable) => this.prepVariableIsApplied(kind, variable), + kind === 'missing' ? (variable) => this.isPreprocessingVariableUsingDefault(variable) : () => false, + ); + } + + private buildPreprocessingGroups( + variables: VariableRow[], + isPending: (variable: VariableRow) => boolean, + isApplied: (variable: VariableRow) => boolean, + isDefault: (variable: VariableRow) => boolean = () => false + ): PreprocessingGroup[] { + const pending: VariableRow[] = []; + const applied: VariableRow[] = []; + const defaults: VariableRow[] = []; + const notApplied: VariableRow[] = []; + + variables.forEach((variable) => { + if (isPending(variable)) pending.push(variable); + else if (isApplied(variable)) applied.push(variable); + else if (isDefault(variable)) defaults.push(variable); + else notApplied.push(variable); + }); + + const groups: PreprocessingGroup[] = [ + { + key: 'pending', + title: 'Pending', + variables: pending, + }, + { + key: 'applied', + title: 'Applied preprocessing', + variables: applied, + }, + { + key: 'default', + title: 'Default (NA removal)', + variables: defaults, + }, + { + key: 'not-applied', + title: 'Not applied', + variables: notApplied, + }, + ]; + return groups.filter((group) => group.variables.length > 0); + } + + selectedPrepVariable(kind: PrepKind): VariableRow | null { + const filtered = this.filteredPrepVariables(kind); + if (!filtered.length) return null; + return filtered.find((variable) => variable.code === this.selectedPrepCode[kind]) ?? filtered[0]; + } + + selectPrepVariable(kind: PrepKind, variable: VariableRow): void { + this.selectedPrepCode[kind] = variable.code; + this.cdr.markForCheck(); + } + + isPrepVariableSelected(kind: PrepKind, variable: VariableRow): boolean { + return this.selectedPrepVariable(kind)?.code === variable.code; + } + + prepVariableStateLabel(kind: PrepKind, variable: VariableRow): string { + if (this.prepVariableHasPendingChange(kind, variable)) return 'Pending'; + if (this.prepVariableIsApplied(kind, variable)) return 'Applied'; + if (kind === 'missing' && this.isPreprocessingVariableUsingDefault(variable)) return 'Default'; + return 'Not applied'; + } + + prepVariableHasPendingChange(kind: PrepKind, variable: VariableRow): boolean { + switch (kind) { + case 'missing': { + const appliedRule = this.appliedPreprocessingRules[variable.code]; + const pendingRule = this.pendingPreprocessingRules[variable.code] + ?? (appliedRule ? undefined : this.defaultRule(variable.code)); + if (this.isDefaultMissingRule(pendingRule) && !this.hasAppliedPreprocessing(variable.code)) { + return false; + } + return this.serializeRule(pendingRule) !== this.serializeRule(appliedRule); + } + case 'outlier': { + const appliedRule = this.appliedOutlierRules[variable.code]; + const pendingRule = this.pendingOutlierRules[variable.code] + ?? (appliedRule ? undefined : this.defaultOutlierRule(variable.code)); + return serializeOutlierRule(pendingRule) !== serializeOutlierRule(appliedRule); + } + case 'longitudinal': { + if (!this.isLongitudinalModel) return false; + const pendingStrategy = this.pendingLongitudinalStrategies[variable.code] ?? this.defaultLongitudinalStrategy(variable); + const appliedStrategy = this.appliedLongitudinalStrategies[variable.code] ?? null; + return pendingStrategy !== appliedStrategy || this.longitudinalVisitPairHasPendingChange; + } + default: { + const _exhaustive: never = kind; + return _exhaustive; + } + } + } + + private prepVariableIsApplied(kind: PrepKind, variable: VariableRow): boolean { + switch (kind) { + case 'missing': + return this.hasAppliedPreprocessing(variable.code); + case 'outlier': + return this.hasAppliedOutlierPreprocessing(variable.code); + case 'longitudinal': + return this.hasAppliedLongitudinalPreprocessing(variable.code); + default: { + const _exhaustive: never = kind; + return _exhaustive; + } + } + } + + isPreprocessingVariableUsingDefault(variable: VariableRow): boolean { + if (this.hasAppliedPreprocessing(variable.code)) return false; + const pendingRule = this.pendingPreprocessingRules[variable.code] + ?? this.defaultRule(variable.code); + return this.isDefaultMissingRule(pendingRule); + } + + private isDefaultMissingRule(rule: PreprocessingRule | undefined): boolean { + if (!rule) return false; + return rule.enabled === true + && rule.action === 'drop' + && (rule.value === undefined || rule.value === ''); + } + + get longitudinalVisitPairHasPendingChange(): boolean { + return this.longitudinalVisit1 !== this.appliedLongitudinalVisit1 || this.longitudinalVisit2 !== this.appliedLongitudinalVisit2; + } + + get missingPreprocessingStatusLabel(): string { + if (this.hasPendingMissingChanges) { + return `${pluralize(this.pendingMissingChangeCount, 'change')} · not applied`; + } + if (Object.values(this.appliedPreprocessingRules).some((rule) => rule.enabled && rule.action !== 'no_action')) return 'Applied'; + if (this.preprocessingVariables.some((variable) => this.isPreprocessingVariableUsingDefault(variable))) return 'Default · in run'; + return 'Required'; + } + + get outlierPreprocessingStatusLabel(): string { + if (!this.outlierPreprocessingVariables.length) return 'Not available'; + if (this.hasPendingOutlierChanges) { + return `${pluralize(this.pendingOutlierChangeCount, 'change')} · not applied`; + } + if (Object.values(this.appliedOutlierRules).some((rule) => rule.enabled)) return 'Applied'; + return 'Optional'; + } + + get hasOutlierRulesForPreview(): boolean { + return !!this.buildOutlierPreprocessingConfig(this.pendingOutlierRules); + } + + get longitudinalPreprocessingStatusLabel(): string { + if (!this.isLongitudinalModel) return 'Not available'; + if (this.pendingLongitudinalChangeCount > 0) { + return `${pluralize(this.pendingLongitudinalChangeCount, 'change')} · not applied`; + } + if (this.appliedLongitudinalEnabled) return 'Applied'; + return 'Required'; + } + + get selectedFilters(): VariableRow[] { + return this.expStudioService.selectedFilters() as VariableRow[]; + } + + get isLongitudinalModel(): boolean { + return !!this.expStudioService.selectedDataModel()?.longitudinal; + } + + get visitOptions(): Array<{ code: string; label: string }> { + const visitVariable = this.findModelVariable('visitid'); + return (visitVariable?.enumerations ?? []) + .map((item) => { + const code = item?.code === undefined || item?.code === null ? '' : String(item.code); + return { + code, + label: String(item?.label ?? item?.name ?? item?.code ?? ''), + }; + }) + .filter((item) => item.code); + } + + setSummaryTab(kind: SummaryKind, tab: TabKey): void { + const summary = this.getSummary(kind); + summary.activeTab = tab; + this.summaryExportMenuKind.set(null); + if (tab === 'Histogram') { + const block = this.selectedStatisticBlock(kind); + if (block) this.ensureHistogramForBlock(kind, block); + } + this.cdr.markForCheck(); + } + + filteredStatisticBlocks(kind: SummaryKind): PivotBlock[] { + const summary = this.getSummary(kind); + const query = this.statisticsSearch[kind].trim().toLowerCase(); + if (!query) return summary.data; + return summary.data.filter((block) => { + const variable = this.variableForBlock(block); + return [ + block.name, + variable?.code, + variable?.label, + variable?.name, + ].some((value) => String(value ?? '').toLowerCase().includes(query)); + }); + } + + statisticBlocks(kind: SummaryKind, type: StatisticVariableType): PivotBlock[] { + return this.filteredStatisticBlocks(kind).filter((block) => this.statisticBlockType(kind, block) === type); + } + + selectStatisticBlock(kind: SummaryKind, block: PivotBlock): void { + const summary = this.getSummary(kind); + summary.selectedStatisticKey = this.statisticBlockKey(block); + if (summary.activeTab === 'Histogram') { + this.ensureHistogramForBlock(kind, block); + } + } + + selectedStatisticBlock(kind: SummaryKind): PivotBlock | null { + const filtered = this.filteredStatisticBlocks(kind); + if (!filtered.length) return null; + const selectedKey = this.getSummary(kind).selectedStatisticKey; + return filtered.find((block) => this.statisticBlockKey(block) === selectedKey) ?? filtered[0]; + } + + isStatisticBlockSelected(kind: SummaryKind, block: PivotBlock): boolean { + const selected = this.selectedStatisticBlock(kind); + return !!selected && this.statisticBlockKey(selected) === this.statisticBlockKey(block); + } + + statisticBlockVariable(block: PivotBlock): VariableRow | null { + return this.variableForBlock(block); + } + + statisticBlockTypeLabel(kind: SummaryKind, block: PivotBlock): string { + return this.statisticBlockType(kind, block) === 'numeric' ? 'Numerical' : 'Categorical'; + } + + selectedSummaryChartType(kind: SummaryKind, block: PivotBlock): 'numeric' | 'nominal' { + return this.statisticBlockType(kind, block); + } + + selectedSummaryChartTypeLabel(kind: SummaryKind, block: PivotBlock): string { + return this.selectedSummaryChartType(kind, block) === 'numeric' ? 'Numerical' : 'Categorical'; + } + + selectedSummaryChartOptions(kind: SummaryKind, block: PivotBlock): EChartsOption[] { + const variable = this.statisticBlockVariable(block); + if (!variable) return []; + + const summary = this.getSummary(kind); + if (this.selectedSummaryChartType(kind, block) === 'numeric') { + const index = summary.nonNominalVariables.findIndex((item) => item.code === variable.code); + return index >= 0 ? summary.chartsForBoxPlot[index] ?? [] : []; + } + + const index = summary.nominalVariables.findIndex((item) => item.code === variable.code); + return index >= 0 ? summary.chartsForNominal[index] ?? [] : []; + } + + selectedSummaryHistogramData(kind: SummaryKind, block: PivotBlock): HistogramPreviewData | null { + const code = this.variableCodeForBlock(block); + if (!code) return null; + return this.getSummary(kind).histogramDataByVariable[this.histogramLookupKey(kind, block, code)] ?? null; + } + + summaryHistogramLoading(kind: SummaryKind, block: PivotBlock): boolean { + const code = this.variableCodeForBlock(block); + if (!code) return false; + return !!this.getSummary(kind).histogramLoadingByVariable[this.histogramLookupKey(kind, block, code)]; + } + + /** What a histogram run covers, spelled out for the loading state. The pooled + "all datasets" rollup is a result column, not a node, so it is not counted. */ + summaryHistogramRunLabel(block: PivotBlock): string { + return this.federatedRunLabel(block.columns.filter((column) => !this.isRollupColumn(column)).length); + } + + /** Fan-out wording for a run that has not returned yet, read from the live scope. */ + summaryScopeRunLabel(): string { + const excluded = this.expStudioService.excludedDatasets(); + const nodes = this.expStudioService.selectedDatasets().filter((ds) => !excluded.includes(ds)).length; + return this.federatedRunLabel(nodes); + } + + private federatedRunLabel(nodes: number): string { + return `Calculated on ${nodes} dataset ${nodes === 1 ? 'node' : 'nodes'}`; + } + + summaryHistogramError(kind: SummaryKind, block: PivotBlock): string { + const code = this.variableCodeForBlock(block); + if (!code) return ''; + return this.getSummary(kind).histogramErrorByVariable[this.histogramLookupKey(kind, block, code)] ?? ''; + } + + canSplitSummaryHistogram(kind: SummaryKind, block: PivotBlock): boolean { + return this.selectedSummaryChartType(kind, block) === 'numeric'; + } + + histogramSplitOptions(block: PivotBlock): HistogramSplitOption[] { + return buildHistogramSplitOptions({ + plottedCode: this.variableCodeForBlock(block), + selectedVariables: this.expStudioService.selectedVariables(), + datasetLevelCount: this.expStudioService.selectedDatasets().length, + }); + } + + onHistogramSplitChange(kind: SummaryKind, block: PivotBlock, event: Event): void { + const code = this.variableCodeForBlock(block); + if (!code) return; + const selected = (event.target as HTMLSelectElement).value; + const option = this.histogramSplitOptions(block).find((item) => item.code === selected); + const split = option && !option.disabled ? selected : HISTOGRAM_SPLIT_NONE; + const summary = this.getSummary(kind); + summary.histogramSplitByVariable = { + ...summary.histogramSplitByVariable, + [code]: split, + }; + this.ensureHistogramForBlock(kind, block); + this.cdr.markForCheck(); + } + + /** The block's split, or none once it is no longer offered or enabled. */ + histogramSplitValue(kind: SummaryKind, block: PivotBlock): string { + if (!this.canSplitSummaryHistogram(kind, block)) return HISTOGRAM_SPLIT_NONE; + const code = this.variableCodeForBlock(block); + if (!code) return HISTOGRAM_SPLIT_NONE; + const split = this.getSummary(kind).histogramSplitByVariable[code] ?? HISTOGRAM_SPLIT_NONE; + if (!split) return HISTOGRAM_SPLIT_NONE; + const option = this.histogramSplitOptions(block).find((item) => item.code === split); + return option && !option.disabled ? split : HISTOGRAM_SPLIT_NONE; + } + + private histogramLookupKey(kind: SummaryKind, block: PivotBlock, code: string): string { + return histogramCacheKey(code, this.histogramSplitValue(kind, block)); + } + + goToSection(section: SectionKey): void { + const filtersStation = section === 'filters' || section === 'raw'; + const setupStation = section === 'setup' || section === 'processed'; + const previewFilters = section === 'raw'; + const previewSetup = section === 'processed'; + if (filtersStation) { + this.addedSteps.update((s) => ({ ...s, filters: true })); + } + if (setupStation) { + this.addedSteps.update((s) => ({ ...s, setup: true })); + } + if (section === 'transformation') { + this.addedSteps.update((s) => ({ ...s, transformation: true })); + } + this.summaryExpanded.set({ + filters: previewFilters, + setup: previewSetup, + }); + this.sectionOpen.set({ + source: section === 'source', + filters: filtersStation && !previewFilters, + raw: previewFilters, + setup: setupStation && !previewSetup, + processed: previewSetup, + transformation: section === 'transformation', + }); + if (section === 'raw') { + // The Raw summary must show the cohort it claims to: the stored rules, or the + // unsaved ones a Cohort Filtering preview pinned to it. + this.ensureRawSummary(); + } + if (section === 'processed') { + // Pipeline consistency: the processed preview always mirrors the current + // pipeline state (cohort filter + configured preprocessing, pending edits + // included), not just the last persisted snapshot. + this.fetchProcessedPreview(); + } + this.cdr.markForCheck(); + this.requestSectionScroll(section); + } + + /** + * Scrolling is a request, not a call: the section is measured only after the template + * has reacted to whatever just opened or closed. Shared by goToSection and the stations' + * Close action, which folds an editor away and must not leave the viewport stranded. + */ + private requestSectionScroll(section: SectionKey): void { + this.scrollSectionRequest.set({ section, requestId: ++this.scrollRequestId }); + } + + /** + * Step 0 is a read: it shows the selection as stored and can never write the + * experiment request, so it toggles a section instead of adding a stage. + */ + toggleSourcePreview(): void { + if (this.sectionOpen().source) { + this.collapseAllWorkflowSections(); + this.cdr.markForCheck(); + return; + } + this.goToSection('source'); + this.ensureSourceSummary(); + } + + /** + * The snapshot's own cache key: the variable and dataset scope it describes. The + * shared `selectionKey` also folds in the cohort filter and the applied preprocessing, + * neither of which this request carries, so keying on it would refetch an identical + * snapshot on every committed filter rule while the preview happens to be open. + */ + private get sourceScopeKey(): string { + return JSON.stringify({ + variables: this.preprocessingVariables.map((variable) => variable.code).sort(), + datasets: this.expStudioService.selectedDatasets(), + }); + } + + /** The snapshot follows the selection, so reopening it after a change refetches. */ + ensureSourceSummary(): void { + if (this.sourceSummaryKey === this.sourceScopeKey && !this.sourceSummary.isLoading) return; + this.fetchSourceSummary(); + } + + /** + * The same describe run that feeds the Raw summary with the cohort filter and + * the preprocessing steps left off. It fills a `SummaryView`, so step 0 reuses + * the shared summary workspace - browser, statistics, charts, histograms, + * export - instead of a second presentation of its own. + */ + fetchSourceSummary(): void { + const variableCodes = this.preprocessingVariables.map((item) => item.code); + const hasScope = !!this.expStudioService.selectedDataModel() && this.expStudioService.selectedDatasets().length > 0; + if (!hasScope || !variableCodes.length) { + this.sourceSummary = this.createEmptySummary(false); + this.sourceSummaryKey = ''; + this.cdr.markForCheck(); + return; + } + + const requestedKey = this.sourceScopeKey; + const requestSeq = ++this.sourceDescribeSeq; + this.clearHistogramSubscriptions(this.sourceSummary); + this.sourceSummary = { ...this.sourceSummary, isLoading: true }; + this.cdr.markForCheck(); + + // No filter payload, no preprocessing steps: the data exactly as selected. + this.clearSourcePreviewSubscription(); + this.sourcePreviewSubscription = this.expStudioService + .loadDescriptiveOverview(variableCodes, null, null, null) + .pipe(takeUntil(this.destroyed)) + .subscribe({ + next: (response) => { + if (requestSeq !== this.sourceDescribeSeq) return; + this.clearSourcePreviewSubscription(); + this.sourceSummary = this.buildSummaryFromResponse(response, 'source'); + this.sourceSummaryKey = requestedKey; + this.refreshActiveHistogramPreview('source'); + this.cdr.markForCheck(); + }, + error: (err) => { + if (requestSeq !== this.sourceDescribeSeq) return; + this.clearSourcePreviewSubscription(); + console.error(err); + this.sourceSummary = { ...this.sourceSummary, isLoading: false }; + this.cdr.markForCheck(); + }, + }); + } + + /** Heading and aria text, so one summary template reads correctly for all three kinds. */ + summaryKindLabel(kind: SummaryKind): string { + return kind === 'source' ? 'Source data' : kind === 'raw' ? 'Raw data' : 'Processed data'; + } + + /** Anchor name for one slot of the shared summary template; see `SUMMARY_GUIDE_ANCHORS`. */ + summaryGuideAnchor(kind: SummaryKind, anchor: SummaryAnchorKey): string | null { + return SUMMARY_GUIDE_ANCHORS[kind][anchor]; + } + + /** Close on a data preview returns to the station editor (filters or preprocessing). */ + toggleSummaryWorkspace(kind: SummaryKind): void { + if (kind === 'raw') { + this.goToSection(this.sectionOpen().raw ? 'filters' : 'raw'); + return; + } + this.updatePreprocessingStatus(); + this.goToSection(this.sectionOpen().processed ? 'setup' : 'processed'); + } + + collapseAllWorkflowSections(): void { + this.sectionOpen.set({ + source: false, + raw: false, + setup: false, + filters: false, + processed: false, + transformation: false, + }); + this.cdr.markForCheck(); + } + + togglePreprocessingStep(step: 'missing' | 'outlier' | 'longitudinal' | 'transformation'): void { + this.preprocessingStepOpen[step] = !this.preprocessingStepOpen[step]; + } + + /** "How this works" disclosures, moved from the body summary into the card header. */ + readonly preprocessingDocOpen = signal<Record<'missing' | 'outlier' | 'longitudinal', boolean>>({ + missing: false, + outlier: false, + longitudinal: false, + }); + + togglePreprocessingDoc(step: 'missing' | 'outlier' | 'longitudinal'): void { + this.preprocessingDocOpen.update((open) => ({ ...open, [step]: !open[step] })); + this.cdr.markForCheck(); + } + + /** Maps a station status label onto the shared chip states (Default / Pending / Applied). */ + statusTone(label: string, pending = false): StationStatus { + if (pending) return 'pending'; + return label === 'Applied' ? 'applied' : 'default'; + } + + /** + * Persist live category-filter builders onto every rule (across all cards) without + * touching cohort filters. `#ruleModal` renders one modal per rule in DOM order, so a + * single running index walks drafts and their rules in the same order the template emits. + * A rule is rewritten only when its builder can speak for it: a builder that rejects its + * own half-typed condition, or that could not read what the store holds, leaves that rule + * as it is rather than exporting an empty filter and erasing a configured category. + */ + commitTransformationRuleFilters(only?: TransformationColumnDraft): void { + const modals = this.transformationRuleModals?.toArray() ?? []; + let modalIndex = 0; + for (const draft of this.transformationDrafts) { + for (const rule of draft.rules) { + const modal = modals[modalIndex++]; + if (!modal || (only && draft !== only)) { + continue; + } + const logic = modal.exportFilterLogic(); + if (modal.filterError()) { + // Keep walking the remaining cards: one broken builder must not stop the + // others from committing what they hold. The builder renders its own error. + continue; + } + rule.filter = logic; + } + } + this.onTransformationChange(); + } + + /** The K-means card's Preview: cluster counts, shown in place of its editor. */ + previewTransformationCard(stepId?: string): void { + this.previewDraftId = stepId + ?? this.cards().find((step) => step.name === 'kmeans_cluster_creator')?.id + ?? null; + this.refreshTransformationStatistics(); + this.cdr.markForCheck(); + } + + closeTransformationPreview(): void { + this.previewDraftId = null; + this.cdr.markForCheck(); + } + + toggleTransformationPreview(): void { + if (this.previewDraftId) this.closeTransformationPreview(); + else this.previewTransformationCard(); + } + + transformationStatsFor(code: string): Array<{ code: string; rows: Array<{ value: string; count: number | null }> }> { + const target = code.trim(); + return this.transformationStatistics.filter((block) => block.code === target); + } + + /** Card is incomplete or clashes: its primary slot says Apply, otherwise Close. */ + draftHasNewWork(draft: TransformationColumnDraft): boolean { + const { label } = this.transformationDraftStatus(draft); + return label === 'Not applied' || label === 'Duplicate name'; + } + + /** A card's own Apply/Close: commit its rule builders, then fold it once it is settled. + * Valid cards are persisted as they are typed, so this never writes other cards. */ + commitTransformationDraft(draft: TransformationColumnDraft): void { + this.commitTransformationRuleFilters(draft); + // An untouched category row (no name, no rule) is dropped, not left to block Apply. + for (let index = draft.rules.length - 1; index >= 0; index--) { + const rule = draft.rules[index]; + if (!rule.value.trim() && !rule.filter) this.removeTransformationRule(draft, index); + } + if (!this.draftHasNewWork(draft)) draft.open = false; + this.cdr.markForCheck(); + } + + /** Clear the transformation station: pending edits and the committed config alike. */ + resetTransformation(): void { + this.expStudioService.clearTransformations(); + this.transformationDrafts = []; + this.cardOpenState.set({}); + this.previewDraftId = null; + this.transformationStatistics = []; + this.transformationStatisticsError = ''; + this.invalidateCategoryCounts(); + this.emitProgressState(); + this.cdr.markForCheck(); + } + + /** A new category opens straight onto its filter, with the first condition ready. */ + addTransformationRule(draft: TransformationColumnDraft): void { + draft.rules.push({ value: '', filter: null }); + const index = draft.rules.length - 1; + draft.openRuleIndex = index; + this.onTransformationChange(); + this.cdr.detectChanges(); + this.ruleModalAt(draft, index)?.ensureFirstCondition(); + } + + /** `#ruleModal` renders one builder per rule in card order, so earlier cards' rules come first. */ + private ruleModalAt(draft: TransformationColumnDraft, index: number) { + const offset = this.transformationDrafts + .slice(0, this.transformationDrafts.indexOf(draft)) + .reduce((count, earlier) => count + earlier.rules.length, 0); + return this.transformationRuleModals?.toArray()[offset + index]; + } + + /** + * Done on the rule being edited: save what its builder holds and fold back to the + * category list. A builder with an error stays open and shows its own message. + */ + finishTransformationRule(draft: TransformationColumnDraft, index: number): void { + const modal = this.ruleModalAt(draft, index); + if (modal) { + const logic = modal.exportFilterLogic(); + if (modal.filterError()) { + this.cdr.markForCheck(); + return; + } + draft.rules[index].filter = logic; + } + draft.openRuleIndex = null; + this.onTransformationChange(); + this.countsDraftId = draft.id; + this.refreshTransformationStatistics(); + } + + removeTransformationRule(draft: TransformationColumnDraft, index: number): void { + draft.rules.splice(index, 1); + if (draft.openRuleIndex === index) draft.openRuleIndex = null; + else if (draft.openRuleIndex != null && draft.openRuleIndex > index) draft.openRuleIndex -= 1; + this.onTransformationChange(); + } + + toggleTransformationRule(draft: TransformationColumnDraft, index: number): void { + draft.openRuleIndex = draft.openRuleIndex === index ? null : index; + this.cdr.markForCheck(); + } + + private firstFilterCondition(filter: unknown): { field: string; operator: string; value: unknown } | null { + if (!filter || typeof filter !== 'object') return null; + const node = filter as { field?: unknown; operator?: unknown; value?: unknown; rules?: unknown[] }; + if (node.field) { + return { field: String(node.field), operator: String(node.operator ?? ''), value: node.value }; + } + for (const rule of Array.isArray(node.rules) ? node.rules : []) { + const found = this.firstFilterCondition(rule); + if (found) return found; + } + return null; + } + + /** A one-line rule summary for the collapsed row. */ + transformationRuleSummary(rule: TransformationRule): string { + const condition = this.firstFilterCondition(rule.filter); + return condition ? this.filterConditionSentence(condition) : 'No rule yet'; + } + + /** `Age is at least 60`: one condition in words, labelled from the selected variables. */ + private filterConditionSentence(condition: { field: string; operator: string; value: unknown }): string { + const variable = this.expStudioService.selectedVariables() + .find((item) => String(item?.code ?? '') === condition.field); + const label = String(variable?.label ?? variable?.name ?? condition.field); + const enumLabel = (code: string) => + variable?.enumerations?.find((item: { code?: unknown }) => String(item?.code) === code)?.label || code; + const values = (Array.isArray(condition.value) ? condition.value : [condition.value ?? '']) + .map((value) => enumLabel(String(value))) + .join(', '); + const phrase = filterOperatorPhrase(condition.operator); + return values ? `${label} ${phrase} ${values}` : `${label} ${phrase}`; + } + + /** Loaded record count for one enumeration of a card's column. */ + transformationCount(draft: TransformationColumnDraft, value: string): string { + const block = this.transformationStatistics.find((item) => item.code === draft.code.trim()); + const row = block?.rows.find((item) => item.value === value); + return row?.count != null ? row.count.toLocaleString() : '—'; + } + + /** Per-card chip: Applied only when every category is complete and the name is unique. + * An unfinished row is omitted from the saved column, so it must not leave the chip on Applied. */ + transformationDraftStatus(draft: TransformationColumnDraft): { label: string; tone: 'applied' | 'pending' | 'default' } { + const problem = this.draftWriteProblem(draft); + if (problem) return { label: problem === 'Name already used' ? 'Duplicate name' : 'Column in use', tone: 'pending' }; + if (!this.hasUnfinishedCategory(draft) && this.buildTransformationConfigForDraft(draft)) { + return { label: 'Applied', tone: 'applied' }; + } + if (this.draftHasWork(draft)) return { label: 'Not applied', tone: 'pending' }; + return { label: 'Not set', tone: 'default' }; + } + + /** + * Why a categorical card cannot be written to the store: a name collision, or a later step + * still reads the column the edit would drop (renamed, or left incomplete). Mirrors the + * refusal in `upsertTransformation`, so a refused edit is shown on the card instead of lost. + */ + private draftWriteProblem(draft: TransformationColumnDraft): string | null { + const code = draft.code.trim(); + const nameError = code ? this.columnNameError(draft.id, code) : null; + if (nameError || this.buildTransformationConfigForDraft(draft)) return nameError; + const inUse = this.expStudioService.outputsInUse(draft.id, []); + return inUse.length ? `Used by a later step: ${inUse.join(', ')}` : null; + } + + toggleTransformationDraft(draft: TransformationColumnDraft): void { + draft.open = !draft.open; + this.cdr.markForCheck(); + } + + /** Append another empty categorical card through the store-backed chooser path. */ + addTransformationDraft(): void { + this.chooseTransformation('categorical_column_creator'); + } + + /** + * Remove a store step (and its categorical draft). Disabled while dependents remain — + * callers should check `canRemoveTransformation` first. + */ + removeTransformationDraft(id: string): void { + if (!this.canRemoveTransformation(id)) return; + this.expStudioService.removeTransformation(id); + this.transformationDrafts = this.transformationDrafts.filter((draft) => draft.id !== id); + this.cardOpenState.update((state) => { + const next = { ...state }; + delete next[id]; + return next; + }); + this.invalidateCategoryCounts(); + this.emitProgressState(); + this.cdr.markForCheck(); + } + + onTransformationInput(): void { + this.onTransformationChange(); + } + + preprocessingStepDocumentation(stepName: string): string { + const algorithms = Object.values(this.expStudioService.backendAlgorithms?.() ?? {}) as Array<{ + preprocessing?: Array<{ name: string; documentation?: string }>; + }>; + const step = algorithms + .flatMap((algorithm) => algorithm.preprocessing ?? []) + .find((candidate) => candidate.name === stepName); + return step?.documentation?.trim() ?? ''; + } + + formatPreprocessingDocumentationHtml(stepName: string): string { + return this.buildPreprocessingDocumentationHtml(this.preprocessingStepDocumentation(stepName)); + } + + private buildPreprocessingDocumentationHtml(text: string): string { + const trimmed = text.trim(); + if (!trimmed) { + return ''; + } + + const lines = trimmed.includes('\n') + ? trimmed.split(/\r?\n/) + : trimmed.split(/\s+(?:[-*\u2022\u00b7])\s+(?=[A-Za-z])/); + + const parts: string[] = []; + let listItems: string[] = []; + let isIntroParagraph = true; + + const flushList = (): void => { + if (!listItems.length) { + return; + } + parts.push('<ul class="preprocessing-doc-list">' + listItems.join('') + '</ul>'); + listItems = []; + }; + + const pushParagraph = (line: string): void => { + const escaped = escapeHtml(line); + if (line.endsWith(':') && line.length <= 120) { + parts.push('<p class="preprocessing-doc-section-title">' + escaped + '</p>'); + isIntroParagraph = false; + return; + } + + const classes = isIntroParagraph + ? 'preprocessing-doc-paragraph preprocessing-doc-intro' + : 'preprocessing-doc-paragraph'; + parts.push('<p class="' + classes + '">' + escaped + '</p>'); + isIntroParagraph = false; + }; + + for (const rawLine of lines) { + const line = rawLine.trim(); + if (!line) { + continue; + } + + const isBullet = DOCUMENTATION_BULLET.test(line); + let itemText = isBullet ? line.replace(DOCUMENTATION_BULLET, '').trim() : line; + const colonIndex = itemText.indexOf(':'); + const looksLikeListItem = + isBullet || + (colonIndex > 0 && colonIndex < 40 && itemText.slice(colonIndex + 1).trim().length > 0); + + if (looksLikeListItem && colonIndex > 0) { + const term = escapeHtml(itemText.slice(0, colonIndex).trim()); + const description = escapeHtml(itemText.slice(colonIndex + 1).trim()); + // The term keeps its colon: the item is read as one line of prose, not a table. + listItems.push( + '<li><span class="preprocessing-doc-term">' + term + ':</span> ' + + '<span class="preprocessing-doc-desc">' + description + '</span></li>' + ); + continue; + } + + if (isBullet) { + listItems.push('<li><span class="preprocessing-doc-desc">' + escapeHtml(itemText) + '</span></li>'); + continue; + } + + flushList(); + pushParagraph(line); + } + + flushList(); + return parts.join(''); + } + + // ── Preprocessing evidence ──────────────────────────────────────────────── + // A preprocessing form states a decision but never priced it. These read the + // descriptive stats already loaded for the Raw summary, so "Remove rows" can + // say how many rows it removes. Every helper returns null/empty when the + // stats have not arrived; the editor then shows no evidence at all rather + // than an estimate — an unpriced decision is honest, a made-up price is not. + summaryBlockFor(variable: VariableRow): PivotBlock | null { + return this.rawSummary.data.find((block) => this.variableCodeForBlock(block) === variable.code) ?? null; + } + + coverageFor(variable: VariableRow): { total: number; missing: number; share: number } | null { + return this.blockCoverage(this.summaryBlockFor(variable)); + } + + /** Block-based coverage. Both rails price a variable the same way, so the + * lookup lives here and coverageFor() stays the variable-based entry. */ + blockCoverage(block: PivotBlock | null | undefined): { total: number; missing: number; share: number } | null { + if (!block) return null; + const total = this.statisticMetricNumber(block, 'Total'); + if (total <= 0) return null; + const missing = Math.min(this.statisticMissingValue(block), total); + return { total, missing, share: missing / total }; + } + + /** Display form of a canonical pivot value: parse the stored string, then + * locale-group it. Storage and CSV export use `formatNumber`, never this. */ + displayNumber(value: string | undefined | null): string { + if (value === null || value === undefined || value === '') return value ?? ''; + const n = Number(String(value).replace(/,/g, '')); + if (!Number.isFinite(n)) return value; + return n.toLocaleString(undefined, { maximumFractionDigits: 2 }); + } + + missingShareNote(block: PivotBlock, row: PivotBlock['rows'][number], dataset: string): string | null { + if (row.metric !== 'Missing') return null; + const parse = (metric: string) => + Number((block.rows.find((item) => item.metric === metric)?.values[dataset] ?? '').replace(/,/g, '')); + const missing = parse('Missing'); + const total = parse('Total'); + if (!Number.isFinite(missing) || !Number.isFinite(total) || total <= 0) return null; + return this.coveragePercent({ share: Math.min(missing / total, 1) }); + } + + /** The rail's foot: coverage re-pooled over every variable currently shown. + * Per-variable shares are not additive, so this sums the counts and divides + * once — averaging 0.7% and 100% would state a number that describes no + * dataset. Null when no shown block has statistics, so the foot disappears + * rather than reporting 0%. */ + selectionCoverage(kind: SummaryKind): { total: number; missing: number; share: number; variables: number } | null { + let total = 0; + let missing = 0; + let variables = 0; + for (const block of this.filteredStatisticBlocks(kind)) { + const coverage = this.blockCoverage(block); + if (!coverage) continue; + total += coverage.total; + missing += coverage.missing; + variables += 1; + } + if (variables === 0 || total <= 0) return null; + return { total, missing, share: Math.min(missing / total, 1), variables }; + } + + /** The pooled column is a rollup, not another dataset. Normalised through the + * same label lookup the header uses, because the pivot columns are already + * labelled and the header labels them again — this holds either way. */ + isRollupColumn(column: string): boolean { + return this.datasetLabel(column) === this.datasetLabel('all datasets'); + } + + private isCountMetric(metric: string | undefined): boolean { + return metric === 'Datapoints' || metric === 'Missing' || metric === 'Total'; + } + + /** Group heading rendered above rows[index], or null when the row continues + * the group above it. A group starts where count-ness changes. */ + metricGroupLabel(rows: PivotBlock['rows'], index: number): string | null { + const here = this.isCountMetric(rows[index]?.metric); + if (index === 0 || this.isCountMetric(rows[index - 1]?.metric) !== here) { + return here ? 'Coverage' : 'Distribution'; + } + return null; + } + + coveragePercent(coverage: { share: number }): string { + return this.formatPercent(coverage.share * 100); + } + + /** Bar length for a share scaled up so small shares stay visible (×1000: 5% is a half + * bar), floored at 2% so a clean variable still shows the track. */ + barWidth(share: unknown, scale: number): number { + return Math.max(2, Math.min(100, (parseNumber(share) ?? 0) * scale)); + } + + /** The full count belongs in its tooltip; the row only carries the share. */ + coverageTitle(coverage: { total: number; missing: number }): string { + return `${coverage.missing.toLocaleString()} of ${coverage.total.toLocaleString()} values missing`; + } + + /** An unpriced decision must say why it is unpriced, not render blank. + Shared by both preprocessing evidence panes. */ + summaryUnavailableNote(): string { + return this.rawSummary.isLoading + ? 'Loading dataset statistics\u2026' + : 'No dataset statistics are available for this variable, so neither the distribution nor the row cost can be shown.'; + } + + /** Three bands. At the old single 10% floor 30% and 100% rendered in the same + * red, so across a rail of variables the colour ranked nothing. 10% keeps the + * studio's existing "worth noticing" onset; 50% is where a variable stops + * being usable. The exact figure always sits beside the colour. */ + coverageSeverity(coverage: { share: number }): 'none' | 'warning' | 'high' { + if (coverage.share >= 0.5) return 'high'; + if (coverage.share >= 0.1) return 'warning'; + return 'none'; + } + + distributionFor(variable: VariableRow): Array<{ label: string; value: string }> { + const block = this.summaryBlockFor(variable); + if (!block) return []; + // A nominal block carries no quantile rows at all, so the lookups come back + // 'N/A'; those are absent data, not values, and must not render as a row of N/A. + return (['mean', 'std', 'min', 'q1', 'q2', 'q3', 'max'] as const) + .map((key) => ({ + label: this.metricLabel[key], + value: this.displayNumber(this.statisticMetricText(block, this.metricLabel[key])), + })) + .filter((point) => point.value !== 'N/A'); + } + + /** Category frequencies. In the pivot these are the rows after the three + * count metrics, so anything that is not a count metric is a category — but + * only for a nominal block. A numeric block's remaining rows are Mean/Std/ + * Min/Q1/Q2/Q3/Max, which are not categories and were being relabelled. */ + categoryCountsFor(variable: VariableRow, limit = 6): Array<{ label: string; value: string; share: number }> { + if (!this.isCategoricalVariable(variable)) return []; + const block = this.summaryBlockFor(variable); + if (!block) return []; + const base = new Set(['Datapoints', 'Missing', 'Total']); + const dataset = block.columns.includes('all datasets') ? 'all datasets' : block.columns[0]; + const datapoints = Number((block.rows.find((row) => row.metric === 'Datapoints')?.values[dataset] ?? '').replace(/,/g, '')); + return block.rows + .filter((row) => !base.has(row.metric)) + .map((row) => { + const value = row.values[dataset] ?? ''; + const count = Number(value.replace(/,/g, '')); + return { + label: row.metric, + value: this.displayNumber(value || '0'), + share: Number.isFinite(count) && datapoints > 0 ? Math.min(count / datapoints, 1) : 0, + }; + }) + .slice(0, limit); + } + + /** The selected missing-value action restated in rows, so it can be judged. */ + missingActionConsequence(variable: VariableRow): string | null { + const coverage = this.coverageFor(variable); + if (!coverage) return null; + const total = coverage.total.toLocaleString(); + const missing = coverage.missing.toLocaleString(); + const share = this.coveragePercent(coverage); + const action = this.ruleFor(variable).action; + if (action === 'drop') { + return coverage.missing > 0 + ? `Drops up to ${missing} of ${total} rows (${share}).` + : `No missing values, so no rows are dropped.`; + } + if (action === 'no_action') return `${missing} of ${total} values (${share}) stay missing.`; + const fill = action === 'constant' ? 'a fixed value' : `the ${action}`; + return `Fills ${missing} of ${total} values (${share}) with ${fill}. Rows kept.`; + } + + /** + * What each preprocessing card does for a scoped Apply / Discard. A scope of 'all' runs + * every card's op in this order, so a new card is added here and nowhere else. + */ + private readonly prepStepOps: Record<PrepStep, { + pending: () => number; + validate: () => Record<string, string>; + commit: () => void; + reset: () => void; + }> = { + missing: { + pending: () => this.pendingMissingChangeCount, + validate: () => this.validatePendingMissingRules(), + commit: () => { + this.appliedPreprocessingRules = this.mergeRulesForCurrentSelection( + this.appliedPreprocessingRules, + this.pendingPreprocessingRules + ); + }, + reset: () => { + this.pendingPreprocessingRules = this.mergeRulesForCurrentSelection( + this.pendingPreprocessingRules, + this.appliedPreprocessingRules + ); + }, + }, + outlier: { + pending: () => this.pendingOutlierChangeCount, + validate: () => this.validatePendingOutlierRules(), + commit: () => { + this.appliedOutlierRules = this.mergeOutlierRulesForCurrentSelection( + this.appliedOutlierRules, + this.pendingOutlierRules + ); + }, + reset: () => { + this.pendingOutlierRules = this.mergeOutlierRulesForCurrentSelection( + this.pendingOutlierRules, + this.appliedOutlierRules + ); + }, + }, + longitudinal: { + pending: () => this.pendingLongitudinalChangeCount, + validate: () => this.validatePendingLongitudinalRules(), + commit: () => { + this.appliedLongitudinalEnabled = this.isLongitudinalModel; + this.appliedLongitudinalVisit1 = this.longitudinalVisit1; + this.appliedLongitudinalVisit2 = this.longitudinalVisit2; + this.appliedLongitudinalStrategies = { ...this.pendingLongitudinalStrategies }; + }, + reset: () => { + this.longitudinalVisit1 = this.appliedLongitudinalVisit1; + this.longitudinalVisit2 = this.appliedLongitudinalVisit2; + this.pendingLongitudinalStrategies = { ...this.appliedLongitudinalStrategies }; + this.ensureLongitudinalDefaults(); + }, + }, + }; + + private stepsIn(scope: PrepStep | 'all'): PrepStep[] { + return scope === 'all' ? ['missing', 'outlier', 'longitudinal'] : [scope]; + } + + /** Pending edits of one preprocessing card; the stage count sums these. */ + stepPendingCount(step: PrepStep): number { + return this.prepStepOps[step].pending(); + } + + stepApplyLabel(step: PrepStep): string { + if (this.isApplyingPreprocessing) return 'Applying…'; + return this.stepPendingCount(step) > 0 ? 'Apply' : 'Close'; + } + + /** Card footer primary: apply only this card's edits, or fold the card when it has none. */ + commitOrCloseStep(step: PrepStep): void { + if (this.stepPendingCount(step) > 0) { + this.applyPreprocessing(step); + return; + } + this.preprocessingStepOpen[step] = false; + this.cdr.markForCheck(); + } + + /** Choice-card effect line. Empty when the summary has not loaded: the label alone + * is still better than the old select. */ + missingActionEffect(variable: VariableRow, action: MissingAction): string { + const coverage = this.coverageFor(variable); + if (!coverage) return ''; + const n = coverage.missing.toLocaleString(); + switch (action) { + case 'drop': + return `Removes ${n} records (${this.coveragePercent(coverage)}) from the run`; + case 'mean': + case 'median': { + const value = this.distributionMapValue(variable, action); + return `Fills ${n} values with the ${action}${value ? `, ${value}` : ''}`; + } + case 'constant': + return `Fills ${n} values with a value you set`; + default: + return ''; + } + } + + distributionMapValue(variable: VariableRow, label: string): string { + return this.distributionFor(variable) + .find((point) => point.label.toLowerCase() === label.toLowerCase())?.value ?? ''; + } + + /** Per-dataset coverage for the missing-values evidence tiles. Derives the same + * Missing/Total rows the table uses, split by dataset, minus the rollup column. */ + coverageByDataset(variable: VariableRow): Array<{ label: string; missing: number; total: number; share: number }> { + const block = this.summaryBlockFor(variable); + if (!block) return []; + const totalRow = block.rows.find((row) => row.metric === 'Total'); + const missingRow = block.rows.find((row) => row.metric === 'Missing'); + if (!totalRow || !missingRow) return []; + return block.columns + .filter((dataset) => !this.isRollupColumn(dataset)) + .map((dataset) => { + const total = parseNumber(totalRow.values[dataset]) ?? 0; + if (total <= 0) return null; + const missing = Math.min(parseNumber(missingRow.values[dataset]) ?? 0, total); + return { + label: this.datasetLabel(dataset), + missing, + total, + share: missing / total, + }; + }) + .filter((tile): tile is { label: string; missing: number; total: number; share: number } => !!tile); + } + + ruleFor(variable: VariableRow): PreprocessingRule { + if (!this.pendingPreprocessingRules[variable.code]) { + this.pendingPreprocessingRules[variable.code] = this.defaultRule(variable.code); + } + return this.pendingPreprocessingRules[variable.code]; + } + + onMissingActionChange(variable: VariableRow, action: MissingAction): void { + const next = { ...this.ruleFor(variable), action }; + next.enabled = true; + if (action !== 'constant') next.value = ''; + if (action === 'constant' && this.isCategoricalVariable(variable)) { + const enumValues = this.enumOptions(variable).map((item) => item.code); + if (!enumValues.includes(next.value)) next.value = ''; + } + this.pendingPreprocessingRules = { + ...this.pendingPreprocessingRules, + [variable.code]: next, + }; + this.preprocessingValidationErrors = { + ...this.preprocessingValidationErrors, + [variable.code]: '', + }; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + onMissingValueChange(variable: VariableRow, value: string): void { + this.pendingPreprocessingRules = { + ...this.pendingPreprocessingRules, + [variable.code]: { ...this.ruleFor(variable), value }, + }; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + onLongitudinalVisitChange(which: 'visit1' | 'visit2', value: string): void { + if (which === 'visit1') this.longitudinalVisit1 = value; + else this.longitudinalVisit2 = value; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + onLongitudinalStrategyChange(variable: VariableRow, strategy: LongitudinalStrategy): void { + this.pendingLongitudinalStrategies = { + ...this.pendingLongitudinalStrategies, + [variable.code]: strategy, + }; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + outlierStrategyDisplay(strategy: OutlierStrategy): string { + return this.outlierStrategies.find((item) => item.value === strategy)?.label ?? strategy; + } + + outlierTailDisplay(tail: OutlierTail): string { + if (tail === 'left') return 'Low only'; + if (tail === 'right') return 'High only'; + return 'Both'; + } + + outlierFoldDisplay(strategy: OutlierStrategy): string { + switch (strategy) { + case 'gaussian': return '× std. dev.'; + case 'iqr': return '× IQR'; + case 'mad': return '× MAD'; + case 'quantile': return 'Quantile'; + } + } + + outlierRuleSummary(variable: VariableRow): string { + const rule = this.outlierRuleFor(variable); + if (!rule.enabled) return 'Off'; + return `${this.outlierStrategyDisplay(rule.strategy)} × ${rule.fold ?? '—'} · ${this.outlierTailDisplay(rule.tail)}`; + } + + outlierPreviewRowsFor(variable: VariableRow): OutlierPreviewRow[] { + const label = this.variableLabel(variable); + return this.outlierPreviewRows.filter((row) => row.variable === label || row.variable === variable.code); + } + + private outlierDistributionNumbers(variable: VariableRow): { + min: number; q1: number; median: number; q3: number; max: number; mean: number | null; std: number | null; + } | null { + const points = this.distributionFor(variable); + if (!points.length) return null; + const byLabel = new Map(points.map((point) => [point.label.toLowerCase(), point.value])); + const number = (label: string) => parseNumber(byLabel.get(label)); + const min = number('min'); + const q1 = number('q1'); + const median = number('median'); + const q3 = number('q3'); + const max = number('max'); + if (min === null || q1 === null || median === null || q3 === null || max === null) return null; + return { min, q1, median, q3, max, mean: number('mean'), std: number('standard deviation') }; + } + + /** Client-side bounds for the strategies the summary can price. MAD/quantile and + * Gaussian without mean/std return null; the strip then says so. */ + private outlierBounds(variable: VariableRow): { lo: number | null; hi: number | null } | null { + const numbers = this.outlierDistributionNumbers(variable); + if (!numbers) return null; + const rule = this.outlierRuleFor(variable); + const k = Number(rule.fold); + if (!Number.isFinite(k)) return null; + let lo: number; + let hi: number; + switch (rule.strategy) { + case 'iqr': { + const iqr = numbers.q3 - numbers.q1; + lo = numbers.q1 - k * iqr; + hi = numbers.q3 + k * iqr; + break; + } + case 'gaussian': { + if (numbers.mean === null || numbers.std === null) return this.reportBounds(variable); + lo = numbers.mean - k * numbers.std; + hi = numbers.mean + k * numbers.std; + break; + } + default: + // MAD and arbitrary quantiles are not in the summary; the report's own + // bounds are the fallback once a report has run. + return this.reportBounds(variable); + } + return { + lo: rule.tail === 'right' ? null : lo, + hi: rule.tail === 'left' ? null : hi, + }; + } + + private reportBounds(variable: VariableRow): { lo: number | null; hi: number | null } | null { + const row = this.outlierPreviewRowsFor(variable)[0]; + if (!row) return null; + const lo = parseNumber(row.lowerBound); + const hi = parseNumber(row.upperBound); + if (lo === null && hi === null) return null; + return { lo, hi }; + } + + outlierBoundsView(variable: VariableRow): { + min: number; iqrLeft: number; iqrWidth: number; medianLeft: number; + loPct: number | null; hiPct: number | null; cappedLeft: number | null; cappedRight: number | null; + loLabel: string | null; hiLabel: string | null; note: string; + } | null { + const numbers = this.outlierDistributionNumbers(variable); + if (!numbers || numbers.max <= numbers.min) return null; + const span = numbers.max - numbers.min; + const pct = (value: number) => Math.max(0, Math.min(100, ((value - numbers.min) / span) * 100)); + const bounds = this.outlierBounds(variable); + const loPct = bounds?.lo != null ? pct(bounds.lo) : null; + const hiPct = bounds?.hi != null ? pct(bounds.hi) : null; + const reportRows = this.outlierPreviewRowsFor(variable); + const cappedCount = reportRows.reduce( + (sum, row) => sum + (parseNumber(row.lowerOutliers) ?? 0) + (parseNumber(row.upperOutliers) ?? 0), 0); + const note = bounds + ? `Below ${bounds.lo?.toLocaleString() ?? '—'} or above ${bounds.hi?.toLocaleString() ?? '—'} → capped` + + (reportRows.length && cappedCount > 0 ? ` · ${cappedCount.toLocaleString()} values` : '') + : 'Bounds shown after Refresh report'; + + return { + min: numbers.min, + iqrLeft: pct(numbers.q1), + iqrWidth: Math.max(2, pct(numbers.q3) - pct(numbers.q1)), + medianLeft: pct(numbers.median), + loPct, + hiPct, + cappedLeft: loPct, + cappedRight: hiPct !== null ? 100 - hiPct : null, + loLabel: bounds?.lo != null ? bounds.lo.toLocaleString() : null, + hiLabel: bounds?.hi != null ? bounds.hi.toLocaleString() : null, + note, + }; + } + + outlierRuleFor(variable: VariableRow): OutlierRule { + if (!this.pendingOutlierRules[variable.code]) { + this.pendingOutlierRules = { + ...this.pendingOutlierRules, + [variable.code]: this.defaultOutlierRule(variable.code), + }; + } + return this.pendingOutlierRules[variable.code]; + } + + onOutlierEnabledChange(variable: VariableRow, enabled: boolean): void { + const existing = this.outlierRuleFor(variable); + this.pendingOutlierRules = { + ...this.pendingOutlierRules, + [variable.code]: { ...existing, enabled }, + }; + this.outlierValidationErrors = { + ...this.outlierValidationErrors, + [variable.code]: '', + }; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + onOutlierStrategyChange(variable: VariableRow, strategy: OutlierStrategy): void { + const existing = this.outlierRuleFor(variable); + this.pendingOutlierRules = { + ...this.pendingOutlierRules, + [variable.code]: { + ...existing, + enabled: true, + strategy, + fold: defaultFoldForStrategy(strategy), + }, + }; + this.outlierValidationErrors = { + ...this.outlierValidationErrors, + [variable.code]: '', + }; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + onOutlierTailChange(variable: VariableRow, tail: OutlierTail): void { + const existing = this.outlierRuleFor(variable); + this.pendingOutlierRules = { + ...this.pendingOutlierRules, + [variable.code]: { ...existing, enabled: true, tail }, + }; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + onOutlierFoldChange(variable: VariableRow, rawValue: string | number): void { + const existing = this.outlierRuleFor(variable); + const value = String(rawValue ?? '').trim(); + const fold = value === '' ? null : Number(value); + const next: OutlierRule = { + ...existing, + enabled: true, + fold: typeof fold === 'number' && Number.isFinite(fold) ? fold : null, + }; + this.pendingOutlierRules = { + ...this.pendingOutlierRules, + [variable.code]: next, + }; + this.outlierValidationErrors = { + ...this.outlierValidationErrors, + [variable.code]: validateOutlierRule(next) ?? '', + }; + this.updatePreprocessingStatus(); + this.emitProgressState(); + } + + batchMenuOpen = signal<'missing' | 'outlier' | null>(null); + summaryExportMenuKind = signal<SummaryKind | null>(null); + + toggleBatchMenu(kind: 'missing' | 'outlier'): void { + this.batchMenuOpen.update((open) => (open === kind ? null : kind)); + this.cdr.markForCheck(); + } + + toggleSummaryExportMenu(kind: SummaryKind): void { + this.summaryExportMenuKind.update((open) => (open === kind ? null : kind)); + this.cdr.markForCheck(); + } + + closeBatchMenuOnOutsideClick(event: MouseEvent): void { + const target = event.target; + if ( + this.batchMenuOpen() !== null + && !(target instanceof Element && target.closest('.batch-menu')) + ) { + this.batchMenuOpen.set(null); + this.cdr.markForCheck(); + } + if ( + this.summaryExportMenuKind() !== null + && !(target instanceof Element && target.closest('.summary-export-group')) + ) { + this.summaryExportMenuKind.set(null); + this.cdr.markForCheck(); + } + } + + applyBatchMissingStrategy(strategy: 'drop' | 'mean' | 'median'): void { + const variables = + strategy === 'drop' + ? this.preprocessingVariables + : this.preprocessingVariables.filter((variable) => this.isNumericVariable(variable)); + variables.forEach((variable) => this.onMissingActionChange(variable, strategy)); + this.batchMenuOpen.set(null); + this.cdr.markForCheck(); + } + + toggleAllOutliers(enabled: boolean): void { + this.outlierPreprocessingVariables.forEach((variable) => this.onOutlierEnabledChange(variable, enabled)); + this.batchMenuOpen.set(null); + this.cdr.markForCheck(); + } + + outlierRuleError(variable: VariableRow): string { + const rule = this.outlierRuleFor(variable); + return this.outlierValidationErrors[variable.code] || validateOutlierRule(rule) || ''; + } + + longitudinalStrategyFor(variable: VariableRow): LongitudinalStrategy { + if (!this.pendingLongitudinalStrategies[variable.code]) { + this.pendingLongitudinalStrategies[variable.code] = this.defaultLongitudinalStrategy(variable); + } + return this.pendingLongitudinalStrategies[variable.code]; + } + + isMissingActionDisabled(variable: VariableRow, action: MissingAction): boolean { + return (action === 'mean' || action === 'median') && !this.isNumericVariable(variable); + } + + isLongitudinalStrategyDisabled(variable: VariableRow, strategy: LongitudinalStrategy): boolean { + return strategy === 'diff' && !this.isNumericVariable(variable); + } + + /** Discard pending edits: one card's (its footer) or every card's (leaving the stage). */ + resetChanges(scope: PrepStep | 'all' = 'all'): void { + this.stepsIn(scope).forEach((step) => this.prepStepOps[step].reset()); + this.preprocessingApplyError = {}; + this.preprocessingValidationErrors = {}; + this.outlierValidationErrors = {}; + this.updatePreprocessingStatus(); + this.emitProgressState(); + this.cdr.markForCheck(); + } + + previewOutlierReport(): void { + this.ensureDefaultRulesForCurrentSelection(); + this.ensureOutlierDefaultsForCurrentSelection(); + if ( + this.reportPendingValidationErrors({ + ...this.validatePendingMissingRules(), + ...this.validatePendingOutlierRules(), + }) + ) { + this.outlierPreviewRows = []; + this.outlierPreviewError = 'Fix the outlier preprocessing rules before previewing.'; + return; + } + + const outlierParameters = this.buildOutlierPreprocessingConfig(this.pendingOutlierRules); + if (!outlierParameters) { + this.outlierPreviewRows = []; + this.outlierPreviewError = 'Enable at least one outlier rule before previewing.'; + this.cdr.markForCheck(); + return; + } + + const variableCodes = Object.keys((outlierParameters['strategies'] as Record<string, string>) ?? {}); + const missingValuesHandler = this.buildMissingPreprocessingConfig( + this.pendingPreprocessingRules, + new Set(variableCodes) + ); + const preprocessing = missingValuesHandler + ? { missing_values_handler: missingValuesHandler } + : null; + + this.isLoadingOutlierPreview = true; + this.outlierPreviewRows = []; + this.outlierPreviewError = ''; + this.preprocessingValidationErrors = {}; + this.outlierValidationErrors = {}; + this.cdr.markForCheck(); + + this.expStudioService.loadOutlierReportPreview(variableCodes, outlierParameters, preprocessing).subscribe({ + next: (response) => { + this.outlierPreviewRows = this.buildOutlierPreviewRows(response); + this.outlierPreviewError = this.outlierPreviewRows.length + ? '' + : 'No outlier report rows were returned for the enabled rules.'; + this.isLoadingOutlierPreview = false; + this.cdr.markForCheck(); + }, + error: (err) => { + console.error(err); + this.outlierPreviewRows = []; + this.outlierPreviewError = 'Outlier report preview failed.'; + this.isLoadingOutlierPreview = false; + this.cdr.markForCheck(); + }, + }); + } + + /** + * Fold the station: editor and processed workspace both close, nothing is reverted. + * Used by Close (nothing to commit) and by Apply once the describe has landed. + * The node keeps its rail of applied sub-steps, and that rail is the way back into the editor. + */ + private foldPreprocessingStation(): void { + this.sectionOpen.update((open) => ({ ...open, setup: false, processed: false })); + this.cdr.markForCheck(); + this.requestSectionScroll('setup'); + } + + /** + * The Preprocessing station's "Preview data" action: describe the processed cohort + * under the current (pending) config without writing it into the request. It fills + * the shared processed SummaryView, so the preview shows data instead of an empty + * card; Apply remains the only path that persists the config. + */ + previewProcessedData(scope: PrepStep | 'all' = 'all'): void { + this.ensureDefaultRulesForCurrentSelection(); + this.ensureOutlierDefaultsForCurrentSelection(); + this.ensureLongitudinalDefaults(); + if (this.reportPendingValidationErrors(this.validatePendingRulesFor(scope), scope)) return; + this.processedPreviewScope = scope; + // Navigating to the processed section performs the transient describe; + // it must not run twice for one click. + this.goToSection('processed'); + } + + /** + * Transient processed describe for the pending config. It shares the processed + * summary's cache key with the applied fetch, so an Apply (or an applied refresh) + * with identical inputs reuses the data instead of refetching. + */ + private fetchProcessedPreview(): void { + const scope = this.processedPreviewScope; + this.processedPreviewScope = 'all'; + if (Object.keys(this.validatePendingRulesFor(scope)).length > 0) return; + const variableCodes = this.preprocessingVariables.map((variable) => variable.code); + const hasScope = + !!this.expStudioService.selectedDataModel() && this.expStudioService.selectedDatasets().length > 0; + if (!hasScope || !variableCodes.length) { + this.clearProcessedPreviewSubscription(); + this.processedSummary = this.createEmptySummary(false); + this.processedSummaryKey = ''; + this.cdr.markForCheck(); + return; + } + const preprocessing = scope === 'all' + ? this.buildPreprocessingConfig(this.pendingPreprocessingRules, this.currentPreprocessingCodeSet()) + : this.buildScopedPreprocessingConfig(scope); + const nextKey = this.buildProcessedSummaryKey(variableCodes, preprocessing ?? {}); + // The key is claimed before the request goes out, so an in-flight or already + // settled fetch for the same config answers this call; re-requesting it would + // both spend a second federated describe and supersede the pending response. + if (nextKey === this.processedSummaryKey) return; + const previous = this.processedSummary; + const requestSeq = ++this.processedDescribeSeq; + this.clearHistogramSubscriptions(this.processedSummary); + this.clearProcessedPreviewSubscription(); + this.processedSummaryKey = nextKey; + this.processedSummary = { + ...this.createEmptySummary(true), + activeTab: previous.activeTab, + selectedStatisticKey: previous.selectedStatisticKey, + histogramSplitByVariable: previous.histogramSplitByVariable, + }; + this.cdr.markForCheck(); + this.processedPreviewSubscription = this.expStudioService + .loadDescriptiveOverview(variableCodes, preprocessing) + .pipe(takeUntil(this.destroyed)) + .subscribe({ + next: (response) => { + if (requestSeq !== this.processedDescribeSeq) return; + this.clearProcessedPreviewSubscription(); + this.processedSummary = this.buildSummaryFromResponse(response, 'processed'); + this.refreshActiveHistogramPreview('processed'); + this.emitProgressState(); + this.cdr.markForCheck(); + }, + error: (err) => { + if (requestSeq !== this.processedDescribeSeq) return; + this.clearProcessedPreviewSubscription(); + console.error(err); + // Drop the claimed key so reopening the preview retries instead of + // trusting a summary that never arrived. + this.processedSummaryKey = ''; + this.processedSummary = this.createEmptySummary(false); + this.cdr.markForCheck(); + }, + }); + } + + /** + * Apply pending edits: every card's (Apply & Continue) or one card's (its footer). A + * scoped apply writes that card's pending rules over the applied rest, leaves the other + * cards' edits pending, and folds only its own card. + */ + applyPreprocessing(scope: PrepStep | 'all' = 'all'): void { + this.ensureDefaultRulesForCurrentSelection(); + this.ensureOutlierDefaultsForCurrentSelection(); + this.ensureLongitudinalDefaults(); + if (this.stepsIn(scope).every((step) => this.stepPendingCount(step) === 0)) return; + this.preprocessingApplyError = {}; + if (this.reportPendingValidationErrors(this.validatePendingRulesFor(scope))) return; + this.clearOutlierPreview(); + + const currentCodes = this.currentPreprocessingCodeSet(); + const preprocessing = scope === 'all' + ? this.buildPreprocessingConfig(this.pendingPreprocessingRules, currentCodes) + : this.buildScopedPreprocessingConfig(scope); + if (!preprocessing) { + this.commitPendingToApplied(scope); + this.processedSummary = this.createEmptySummary(false); + this.persistAppliedDescriptivePreprocessing( + this.buildPreprocessingConfig(this.appliedPreprocessingRules) + ); + this.preprocessingStatus = this.pendingChangeCount > 0 + ? 'pending' + : this.buildPreprocessingConfig(this.appliedPreprocessingRules) ? 'applied' : 'none'; + if (scope !== 'all') this.preprocessingStepOpen[scope] = false; + this.sectionOpen.update((open) => ({ ...open, processed: false })); + this.emitProgressState(); + this.cdr.markForCheck(); + return; + } + + const variableCodes = this.preprocessingVariables.map((variable) => variable.code); + const requestSeq = ++this.processedDescribeSeq; + this.isApplyingPreprocessing = true; + this.clearHistogramSubscriptions(this.processedSummary); + this.clearProcessedPreviewSubscription(); + this.processedSummary = this.createEmptySummary(true); + // Claim the summary key up front so the navigate-to-preview fetch is + // deduped against the apply request below (identical payload). + this.processedSummaryKey = this.buildProcessedSummaryKey(variableCodes, preprocessing); + if (scope === 'all') { + this.summaryExpanded.update((expanded) => ({ ...expanded, setup: true })); + this.goToSection('processed'); + } + this.preprocessingValidationErrors = {}; + this.outlierValidationErrors = {}; + this.cdr.markForCheck(); + + this.processedPreviewSubscription = this.expStudioService + .loadDescriptiveOverview(variableCodes, preprocessing) + .pipe(takeUntil(this.destroyed)) + .subscribe({ + next: (response) => { + if (requestSeq !== this.processedDescribeSeq) return; + this.clearProcessedPreviewSubscription(); + this.processedSummary = this.buildSummaryFromResponse(response, 'processed'); + this.processedSummaryKey = this.buildProcessedSummaryKey(variableCodes, preprocessing); + this.commitPendingToApplied(scope); + // The describe above included the other cards' applied rules (and implicit + // defaults). Save only what is actually applied, so those defaults are not + // stored as an explicit missing-values handler. + this.persistAppliedDescriptivePreprocessing( + this.buildPreprocessingConfig(this.appliedPreprocessingRules) + ); + this.userPreprocessingApplied = true; + this.preprocessingStatus = this.pendingChangeCount > 0 ? 'pending' : 'applied'; + this.isApplyingPreprocessing = false; + this.refreshActiveHistogramPreview('processed'); + this.emitProgressState(); + // Apply saves and closes: fold the card (or, for Apply & Continue, the station), + // and do not open the next one. The summary just computed stays reachable + // through Preview; moving on belongs to "Continue to Algorithm Selection". + if (scope === 'all') this.foldPreprocessingStation(); + else this.preprocessingStepOpen[scope] = false; + if (this.continueAfterApply) { + this.continueAfterApply = false; + this.studioNavigation.navigateToSection('algorithm-section'); + } + this.cdr.markForCheck(); + }, + error: (err) => { + if (requestSeq !== this.processedDescribeSeq) return; + this.clearProcessedPreviewSubscription(); + console.error(err); + this.isApplyingPreprocessing = false; + // Nothing was applied, so the key claimed for this request is a lie; + // clear it so the next preview or apply refetches. + this.processedSummaryKey = ''; + this.processedSummary = this.createEmptySummary(false); + if (scope === 'all') { + this.sectionOpen.update((open) => ({ ...open, processed: true })); + } else { + // A card's own Apply never left the card: keep it open and say why nothing changed. + this.preprocessingApplyError = { [scope]: 'Could not apply these changes. They are still pending — try Apply again.' }; + } + this.cdr.markForCheck(); + }, + }); + } + + /** + * The Raw summary describe. `filterOverride` runs it against unsaved Cohort + * Filtering rules; the cache key always carries the filter that was actually sent, + * so a pending preview and the stored cohort never claim each other's numbers. + */ + fetchDescriptiveStatistics(filterOverride?: BackendFilter | null): void { + if (!this.expStudioService.selectedDataModel() || this.expStudioService.selectedDatasets().length === 0) { + this.rawDescribeSeq++; + this.rawSummaryKey = ''; + this.expStudioService.clearDataExclusionWarnings(); + this.rawSummary = this.createEmptySummary(false); + this.processedData = []; + this.isLoading = false; + this.cdr.markForCheck(); + return; + } + + this.rawSummary = { ...this.rawSummary, isLoading: true }; + this.isLoading = true; + this.expStudioService.clearDataExclusionWarnings(); + this.cdr.markForCheck(); + + const items = this.preprocessingVariables; + if (!items.length) { + this.rawSummaryKey = ''; + this.rawDescribeSeq++; + this.expStudioService.clearDataExclusionWarnings(); + this.rawSummary = this.createEmptySummary(false); + this.processedData = []; + this.isLoading = false; + this.cdr.markForCheck(); + return; + } + + const variableCodes = items.map((item) => item.code); + const requestSeq = ++this.rawDescribeSeq; + this.rawSummaryKey = this.rawSummaryKeyFor(filterOverride); + this.expStudioService + .loadDescriptiveOverview(variableCodes, null, null, filterOverride) + .subscribe({ + next: (response) => { + // Selection changed while this was in flight; the newer run owns the summary. + if (requestSeq !== this.rawDescribeSeq) return; + this.expStudioService.setDataExclusionWarnings([], []); + this.rawSummary = this.buildSummaryFromResponse(response, 'raw'); + this.processedData = this.rawSummary.data; + this.isLoading = false; + this.refreshActiveHistogramPreview('raw'); + this.cdr.markForCheck(); + }, + error: (err) => { + if (requestSeq !== this.rawDescribeSeq) return; + console.error(err); + this.expStudioService.clearDataExclusionWarnings(); + this.rawSummary = { ...this.rawSummary, isLoading: false }; + this.isLoading = false; + this.cdr.markForCheck(); + }, + }); + } + + /** + * Cache key of the Raw summary for one cohort. The stored `filterLogic()` is the + * default; a pending preview keys on the rules it is showing instead, so the two + * states cannot reuse each other's response. + */ + private rawSummaryKeyFor(filterOverride?: BackendFilter | null): string { + const effectiveFilter = filterOverride === undefined + ? this.expStudioService.filterLogic() + : filterOverride; + return this.buildSelectionKey( + this.expStudioService.selectedVariables(), + this.expStudioService.selectedFilters(), + effectiveFilter, + ); + } + + /** + * Show the Raw summary for the cohort it is supposed to describe, spending at most + * one describe: an identical cohort (same rules, same selection) is already cached. + */ + private ensureRawSummary(): void { + if (this.rawSummaryKey === this.rawSummaryKeyFor(this.rawPreviewFilter)) return; + this.fetchDescriptiveStatistics(this.rawPreviewFilter); + } + + variableLabel(variable: VariableRow): string { + return variable.name ?? variable.label ?? variable.code; + } + + datasetLabel(datasetCode: string): string { + if (!datasetCode) return datasetCode; + const map = this.expStudioService.getDatasetLabelMap(); + return resolveDatasetDisplayLabel(String(datasetCode), map); + } + + variableTypeLabel(variable: VariableRow): string { + if (this.isNumericVariable(variable)) return 'Numeric'; + if (this.isCategoricalVariable(variable)) return 'Categorical'; + return variable.type ?? 'Unknown'; + } + + enumOptions(variable: VariableRow): EnumOption[] { + return (variable.enumerations ?? []) + .map((item) => { + const raw = item.code ?? item.label ?? item.name; + if (raw === undefined || raw === null) return null; + return { + code: String(raw), + label: String(item.label ?? item.name ?? raw), + }; + }) + .filter((item): item is EnumOption => !!item); + } + + async exportAllDescriptiveToPDF(kind: SummaryKind = 'raw'): Promise<void> { + this.isExporting = true; + this.cdr.markForCheck(); + try { + const numericCharts = document.querySelectorAll( + `.hidden-charts-for-export.${kind}-export .numeric-charts-export app-chart-renderer` + ) as NodeListOf<HTMLElement>; + + const nominalCharts = document.querySelectorAll( + `.hidden-charts-for-export.${kind}-export .nominal-charts-export app-chart-renderer` + ) as NodeListOf<HTMLElement>; + + const dataModel = this.expStudioService.selectedDataModel(); + const pathologyName = dataModel?.label || dataModel?.code || ''; + const summary = this.getSummary(kind); + + const labelMap = this.expStudioService.variableLabelMap(); + const enumMaps = this.expStudioService.getCategoricalEnumMaps(); + const filterLogic = this.expStudioService.filterLogic(); + await this.pdfExportService.exportDescriptiveStatisticsPdf({ + title: this.summaryKindLabel(kind), + pathologyName, + context: [ + ['Datasets', this.expStudioService.selectedDatasets().map((code) => this.datasetLabel(code))], + // Source data is the selection before any cohort rule or preprocessing. + ['Filters', kind !== 'source' && filterLogic ? formatFilterExpression(filterLogic, { labelMap, enumMaps }) : null], + ['Preprocessing', kind === 'processed' + ? this.expStudioService.formatPreprocessingConfig(this.expStudioService.getAppliedDescriptivePreprocessing(), labelMap, enumMaps).split('\n') + : null], + ], + variables: this.pivotBlocksWithDatasetLabels(summary.data), + charts: numericCharts, + nonNominalVariables: summary.nonNominalVariables, + nominalCharts, + nominalVariables: summary.nominalVariables, + mipVersion: this.mipVersion, + }); + } catch (err) { + console.error('PDF export failed:', err); + } finally { + this.isExporting = false; + this.cdr.markForCheck(); + } + } + + exportSummaryToCSV(kind: SummaryKind): void { + const summary = this.getSummary(kind); + if (!summary.data.length) return; + + const rows = summary.data.flatMap((variable) => + variable.rows.flatMap((row) => + variable.columns.map((dataset) => ({ + Variable: variable.name, + Metric: row.metric, + Dataset: this.datasetLabel(dataset), + Value: row.values[dataset] ?? '', + })) + ) + ); + + const filename = `${kind}_data_summary.csv`; + this.csvExportService.exportToCsv(rows, ['Variable', 'Metric', 'Dataset', 'Value'], filename); + } + + private createEmptySummary(isLoading: boolean): SummaryView { + return { + activeTab: 'Statistics', + data: [], + featurewiseRows: [], + isLoading, + showDistributions: false, + distributionSubTab: 'Numeric', + nonNominalVariables: [], + nominalVariables: [], + chartsForBoxPlot: [], + chartsForNominal: [], + histogramDataByVariable: {}, + histogramLoadingByVariable: {}, + histogramErrorByVariable: {}, + histogramSubscriptionsByVariable: {}, + histogramRequestIdByVariable: {}, + histogramSplitByVariable: {}, + activeBoxPlotIndex: 0, + activeNominalIndex: 0, + selectedStatisticKey: null, + }; + } + + /** Every kind with a summary surface, in pipeline order. */ + readonly summaryKinds: SummaryKind[] = ['source', 'raw', 'processed']; + + getSummary(kind: SummaryKind): SummaryView { + if (kind === 'raw') return this.rawSummary; + if (kind === 'source') return this.sourceSummary; + return this.processedSummary; + } + + private sectionElement(section: SectionKey): ElementRef<HTMLElement> | undefined { + switch (section) { + case 'source': + return this.sourceSection; + case 'raw': + return this.rawSection; + case 'setup': + return this.setupSection; + case 'filters': + return this.filtersSection; + case 'processed': + return this.processedSection; + case 'transformation': + return this.transformationSection; + default: { + const _exhaustive: never = section; + return _exhaustive; + } + } + } + + private scrollSectionIntoView(section: SectionKey): void { + const element = this.sectionElement(section)?.nativeElement; + if (!element) return; + + requestAnimationFrame(() => { + element.scrollIntoView({ behavior: 'smooth', block: 'start' }); + }); + } + + private buildSummaryFromResponse(response: unknown, kind: SummaryKind): SummaryView { + const current = this.getSummary(kind); + const featurewise = getFeaturewiseDescribeRows(response); + const dsFromPayload = Array.from( + new Set((featurewise ?? []).map((item: any) => String(item.dataset))) + ) as string[]; + const datasetOrder = dsFromPayload + .filter((dataset) => dataset && dataset !== 'all datasets') + .concat('all datasets'); + const variableList = this.preprocessingVariables; + const data = this.pivotByDataset(featurewise, variableList, datasetOrder); + const distributionState = this.buildDistributionState(featurewise, response); + const summary: SummaryView = { + ...current, + data, + featurewiseRows: featurewise, + isLoading: false, + activeTab: current.activeTab, + selectedStatisticKey: this.nextStatisticSelection(data, current.selectedStatisticKey), + histogramDataByVariable: {}, + histogramLoadingByVariable: {}, + histogramErrorByVariable: {}, + histogramSubscriptionsByVariable: {}, + histogramRequestIdByVariable: {}, + ...distributionState, + }; + return summary; + } + + private refreshActiveHistogramPreview(kind: SummaryKind): void { + const summary = this.getSummary(kind); + if (summary.activeTab !== 'Histogram') return; + const block = this.selectedStatisticBlock(kind); + if (!block) return; + this.ensureHistogramForBlock(kind, block); + } + + private buildProcessedSummaryKey( + variableCodes: string[], + preprocessing: PreprocessingConfig + ): string { + return JSON.stringify({ + variableCodes, + preprocessing, + filterLogic: this.expStudioService.filterLogic(), + }); + } + + private ensureHistogramForBlock(kind: SummaryKind, block: PivotBlock): void { + const code = this.variableCodeForBlock(block); + if (!code) return; + + const split = this.histogramSplitValue(kind, block); + const cacheKey = histogramCacheKey(code, split); + const summary = this.getSummary(kind); + if (summary.histogramSubscriptionsByVariable[cacheKey]) return; + if (summary.histogramLoadingByVariable[cacheKey]) return; + if (summary.histogramDataByVariable[cacheKey]) return; + + if (!split) { + const fromDescribe = this.buildHistogramFromDescribeCounts(kind, code, block); + if (fromDescribe) { + summary.histogramDataByVariable = { + ...summary.histogramDataByVariable, + [cacheKey]: fromDescribe, + }; + summary.histogramErrorByVariable = { + ...summary.histogramErrorByVariable, + [cacheKey]: '', + }; + this.cdr.markForCheck(); + return; + } + } + + summary.histogramLoadingByVariable = { + ...summary.histogramLoadingByVariable, + [cacheKey]: true, + }; + summary.histogramErrorByVariable = { + ...summary.histogramErrorByVariable, + [cacheKey]: '', + }; + this.cdr.markForCheck(); + + // `histogram_sql` cannot run without a missing-value strategy, so even the + // unprocessed surfaces send the default drop for the plotted variable and nothing + // else. The processed surface sends its pending rules — never the derived columns, + // which are this variable's output, not its input. Grouping variables are not given + // a drop strategy: that would remove rows from the overall total. + const preprocessingOverride = this.histogramPreprocessingFor(kind, code); + + // Step 0 previews the unfiltered cohort, so its histogram must drop the + // filter too - otherwise the tab would show the filtered cohort under a + // "no filters applied" heading. + const requestSeq = (summary.histogramRequestIdByVariable[cacheKey] ?? 0) + 1; + summary.histogramRequestIdByVariable = { + ...summary.histogramRequestIdByVariable, + [cacheKey]: requestSeq, + }; + // Step 0 describes the unfiltered source; a Cohort Filtering preview pins the raw + // histograms to the same unsaved cohort its table came from; the processed surface + // keeps the applied cohort (undefined = the stored rules). + const filterOverride = kind === 'source' ? null : kind === 'raw' ? this.rawPreviewFilter : undefined; + this.expStudioService + .getAlgorithmResults( + AlgorithmNames.HISTOGRAM, + [code], + null, + preprocessingOverride, + filterOverride, + split ? [split] : null, + ) + .pipe(takeUntil(this.destroyed)) + .subscribe({ + next: (response) => { + const nextSummary = this.getSummary(kind); + if (nextSummary.histogramRequestIdByVariable[cacheKey] !== requestSeq) return; + const parsed = this.parseHistogramResponse(response, code, block, kind, split); + if (parsed.data) { + nextSummary.histogramDataByVariable = { + ...nextSummary.histogramDataByVariable, + [cacheKey]: parsed.data, + }; + nextSummary.histogramErrorByVariable = { + ...nextSummary.histogramErrorByVariable, + [cacheKey]: '', + }; + } else { + nextSummary.histogramErrorByVariable = { + ...nextSummary.histogramErrorByVariable, + [cacheKey]: parsed.error, + }; + } + nextSummary.histogramLoadingByVariable = { + ...nextSummary.histogramLoadingByVariable, + [cacheKey]: false, + }; + nextSummary.histogramSubscriptionsByVariable = { + ...nextSummary.histogramSubscriptionsByVariable, + [cacheKey]: Subscription.EMPTY, + }; + this.cdr.markForCheck(); + }, + error: () => { + const nextSummary = this.getSummary(kind); + if (nextSummary.histogramRequestIdByVariable[cacheKey] !== requestSeq) return; + nextSummary.histogramLoadingByVariable = { + ...nextSummary.histogramLoadingByVariable, + [cacheKey]: false, + }; + nextSummary.histogramErrorByVariable = { + ...nextSummary.histogramErrorByVariable, + [cacheKey]: 'Failed to load histogram preview.', + }; + nextSummary.histogramSubscriptionsByVariable = { + ...nextSummary.histogramSubscriptionsByVariable, + [cacheKey]: Subscription.EMPTY, + }; + this.cdr.markForCheck(); + }, + }); + } + + /** + * What one numerical histogram is plotted under. Raw and step 0 only ever add the + * default drop-NaN for the plotted variable: neither surface may show preprocessing + * or transformations it does not describe. The processed surface follows the pending + * editor state, and a variable those rules leave alone still gets the default drop, + * because a histogram without a handler is a failed request, not an unhandled cohort. + */ + private histogramPreprocessingFor(kind: SummaryKind, code: string): PreprocessingConfig { + if (kind === 'processed') { + const pending = this.buildPreprocessingConfig( + this.pendingPreprocessingRules, + this.currentPreprocessingCodeSet(), + ); + return this.withDefaultDropFor(pending, code); + } + return this.withDefaultDropFor(null, code); + } + + /** Add `drop` for `code` unless the config already carries a strategy for it. */ + private withDefaultDropFor( + preprocessing: PreprocessingConfig | null, + code: string + ): PreprocessingConfig { + const config: PreprocessingConfig = { ...(preprocessing ?? {}) }; + const handler = { ...((config['missing_values_handler'] as Record<string, unknown>) ?? {}) }; + const strategies = { ...((handler['strategies'] as Record<string, unknown>) ?? {}) }; + if (strategies[code]) return config; + strategies[code] = 'drop'; + handler['strategies'] = strategies; + config['missing_values_handler'] = handler; + return config; + } + + private clearProcessedPreviewSubscription(): void { + this.processedPreviewSubscription?.unsubscribe(); + this.processedPreviewSubscription = null; + } + + private clearSourcePreviewSubscription(): void { + this.sourcePreviewSubscription?.unsubscribe(); + this.sourcePreviewSubscription = null; + } + + private clearHistogramSubscriptions(summary: SummaryView): void { + Object.values(summary.histogramSubscriptionsByVariable).forEach((subscription) => subscription.unsubscribe()); + summary.histogramSubscriptionsByVariable = {}; + } + + private buildHistogramFromDescribeCounts( + kind: SummaryKind, + code: string, + block: PivotBlock + ): HistogramPreviewData | null { + const featurewise = this.getSummary(kind).featurewiseRows; + const row = featurewise.find( + (item: { variable?: string; dataset?: string }) => + item?.variable === code && String(item?.dataset ?? '') === 'all datasets' + ) ?? featurewise.find((item: { variable?: string }) => item?.variable === code); + const counts = (row as { data?: { counts?: Record<string, number> } })?.data?.counts; + if (!counts || typeof counts !== 'object' || !Object.keys(counts).length) { + return null; + } + + const variable = this.statisticBlockVariable(block); + const binCodes = Object.keys(counts); + const binsWithLabels = this.mapBinsToEnumLabels(binCodes, variable?.enumerations); + const mappedCounts = binCodes.map((key) => { + const value = counts[key]; + return typeof value === 'number' && Number.isFinite(value) ? value : null; + }); + if (!mappedCounts.some((count) => count !== null && count > 0)) { + return null; + } + + return { + bins: binsWithLabels.map((bin) => String(bin)), + counts: mappedCounts, + variableName: block.name, + variableType: this.statisticBlockType(kind, block), + }; + } + + private extractTransientHistogramError(response: unknown, payload: unknown): string | null { + const candidates: unknown[] = [payload, response]; + if (response && typeof response === 'object' && 'result' in (response as object)) { + candidates.push((response as { result?: unknown }).result); + } + + for (const candidate of candidates) { + if (typeof candidate === 'string' && candidate.trim()) { + return candidate.trim(); + } + if (!candidate || typeof candidate !== 'object' || Array.isArray(candidate)) { + continue; + } + const record = candidate as Record<string, unknown>; + const data = record['data']; + if (typeof data === 'string' && data.trim()) { + return data.trim(); + } + const message = record['message']; + if (typeof message === 'string' && message.trim()) { + return message.trim(); + } + } + + return null; + } + + private parseHistogramResponse( + response: unknown, + code: string, + block: PivotBlock, + kind: SummaryKind, + groupingCode: string | null = null + ): { data: HistogramPreviewData | null; error: string } { + if (!response) { + return { data: null, error: 'Failed to load histogram preview.' }; + } + + const payload = (response as { result?: unknown })?.result ?? response; + const histList = extractHistogramItems(response); + const stacked = buildStackedHistogramSeries( + histList, + code, + groupingCode, + (groupingVar, groupingEnum) => this.groupingEnumLabel(groupingVar, groupingEnum), + this.expStudioService.selectedDatasets() + ); + if (!stacked) { + const backendError = this.extractTransientHistogramError(response, payload); + if (backendError) { + return { data: null, error: backendError }; + } + const fallback = kind === 'processed' + ? 'No histogram data available for this variable after preprocessing. The federated histogram preview returned no bins—often because preprocessing removed too many rows or the execution engine reported an error.' + : 'No histogram data available for this variable.'; + return { data: null, error: fallback }; + } + + const { bins, counts: numericCounts } = stacked; + if (!numericCounts.some((count) => count !== null && count > 0)) { + return { + data: null, + error: 'Histogram counts are unavailable (privacy threshold or insufficient data after preprocessing).', + }; + } + + const variable = this.statisticBlockVariable(block); + const binsWithLabels = this.mapBinsToEnumLabels(bins, variable?.enumerations); + const binLabels = binsWithLabels.map((bin) => String(bin)); + const splitLabel = groupingCode + ? (this.histogramSplitOptions(block).find((option) => option.code === groupingCode)?.label ?? groupingCode) + : undefined; + + return { + data: { + bins: binLabels, + counts: numericCounts, + groups: stacked.groups, + variableName: block.name, + variableType: this.statisticBlockType(kind, block), + splitLabel, + }, + error: '', + }; + } + + private groupingEnumLabel(groupingVar: string, groupingEnum: string): string { + if (groupingVar.toLowerCase() === DATASET_VARIABLE_CODE) { + return this.expStudioService.getDatasetLabelMap()[groupingEnum] ?? groupingEnum; + } + const selected = (this.expStudioService.selectedVariables() as VariableRow[]) + .find((variable) => variable.code === groupingVar); + const mapped = this.getEnumLabelMap(selected ?? this.findModelVariable(groupingVar) ?? undefined) + .get(groupingEnum); + return mapped ?? groupingEnum; + } + + private buildDistributionState(featurewise: any[], response: unknown): Pick<SummaryView, + 'showDistributions' | 'distributionSubTab' | 'nonNominalVariables' | 'nominalVariables' | + 'chartsForBoxPlot' | 'chartsForNominal' | 'activeBoxPlotIndex' | 'activeNominalIndex' + > { + const selectedVars = this.expStudioService.selectedVariables() as VariableRow[]; + const unique = Array.from(new Map(selectedVars.map((v) => [v.code, v])).values()); + + const varsWithCounts = new Set<string>(); + for (const item of featurewise) { + if (item.data && item.data.counts && Object.keys(item.data.counts).length > 0) { + varsWithCounts.add(item.variable); + } + } + + const nominalVariables = unique.filter( + (v) => isCategoricalVariableType(v?.type) || varsWithCounts.has(v.code) + ); + const nominalCodes = new Set(nominalVariables.map((v) => v.code)); + const nonNominalVariables = unique.filter((v) => !nominalCodes.has(v.code)); + const showDistributions = nonNominalVariables.length > 0 || nominalVariables.length > 0; + const distributionSubTab = nonNominalVariables.length > 0 ? 'Numeric' : 'Nominal'; + + const datasetLabels = this.expStudioService.getDatasetLabelMap(); + const chartsForBoxPlot = nonNominalVariables.map((v) => { + const perVarResp = { + ...(response as Record<string, unknown>), + result: { + ...((response as { result?: Record<string, unknown> })?.result ?? {}), + featurewise: featurewise.filter( + (row: any) => row.variable === v.code && row.dataset !== 'all datasets' + ), + dataset_labels: datasetLabels, + }, + }; + return this.chartBuilder.getChartsForAlgorithm('describe', perVarResp); + }); + + const chartsForNominal = nominalVariables.map((v) => { + const varData = featurewise.filter((row: any) => row.variable === v.code); + const varLabel = this.variableLabel(v); + const enumMap = this.getEnumLabelMap(v); + return buildGroupedBarChart(varData, varLabel, enumMap); + }); + + return { + showDistributions, + distributionSubTab, + nonNominalVariables, + nominalVariables, + chartsForBoxPlot, + chartsForNominal, + activeBoxPlotIndex: 0, + activeNominalIndex: 0, + }; + } + + private buildOutlierPreviewRows(response: unknown): OutlierPreviewRow[] { + const payload = (response as { result?: unknown })?.result ?? response; + const featurewise = Array.isArray((payload as { featurewise?: unknown })?.featurewise) + ? ((payload as { featurewise: unknown[] }).featurewise) + : []; + + return featurewise + .filter((row) => { + const item = row as { dataset?: unknown }; + return String(item.dataset ?? '').trim().toLowerCase() !== 'all datasets'; + }) + .map((row) => { + const item = row as { variable?: unknown; dataset?: unknown; data?: Record<string, unknown> }; + const data = item.data ?? {}; + const variableCode = String(item.variable ?? ''); + return { + variable: this.variableLabelForCode(variableCode), + dataset: String(item.dataset ?? '').trim() || '-', + strategy: outlierStrategyLabel(String(data['strategy'] ?? '')), + tail: outlierTailLabel(String(data['tail'] ?? '')), + fold: this.formatOutlierBound(data['fold']), + lowerBound: this.formatOutlierBound(data['lower_bound']), + upperBound: this.formatOutlierBound(data['upper_bound']), + lowerOutliers: this.formatInteger(data['lower_outlier_count'], '-'), + upperOutliers: this.formatInteger(data['upper_outlier_count'], '-'), + totalOutliers: this.formatInteger(data['total_outlier_count'], '-'), + outlierPercentage: this.formatOutlierPercentage(data['total_outlier_percentage']), + }; + }); + } + + private variableLabelForCode(code: string): string { + const variable = this.preprocessingVariables.find((item) => item.code === code); + return variable ? this.variableLabel(variable) : code; + } + + /** Canonical (painted + exported) pivot cell: fixed 2 decimals, no grouping. */ + private formatNumber(value: unknown): string { + if (value === null || value === undefined || value === '') return 'N/A'; + const n = typeof value === 'number' ? value : Number(String(value).replace(/,/g, '')); + if (!Number.isFinite(n)) return String(value); + return n.toFixed(2); + } + + /** Outlier preview bounds keep the old 6-decimal preview precision. */ + private formatOutlierBound(value: unknown): string { + if (value === null || value === undefined || value === '') return '-'; + const n = typeof value === 'number' ? value : Number(String(value).replace(/,/g, '')); + if (!Number.isFinite(n)) return String(value); + return n.toLocaleString(undefined, { maximumFractionDigits: 6 }); + } + + private formatInteger(value: unknown, empty = 'N/A'): string { + if (value === null || value === undefined || value === '') return empty; + const n = typeof value === 'number' ? value : Number(value); + if (!Number.isFinite(n)) return String(value); + return String(Math.round(n)); + } + private formatPercent(pct: number, digits?: number): string { + const d = digits ?? (pct >= 10 || pct === 0 ? 0 : 1); + return `${pct.toFixed(d)}%`; + } -type TabKey = 'Variables' | 'Model' | 'Distributions'; -type MetricKey = - | 'num_dtps' | 'num_na' | 'num_total' - | 'mean' | 'std' | 'min' | 'q1' | 'q2' | 'q3' | 'max'; + private formatOutlierPercentage(value: unknown): string { + if (value === null || value === undefined || value === '') return '-'; + const num = typeof value === 'number' ? value : parseFloat(String(value)); + if (!Number.isFinite(num)) return '-'; + return this.formatPercent(num, 2); + } -interface ModelTableBlock { - variableName: string; - datasets: string[]; - rows: Array<{ metric: MetricKey; values: Record<string, number | string | null> }>; -} + /** Copy pending → applied for one card or all of them. */ + private commitPendingToApplied(scope: PrepStep | 'all'): void { + this.stepsIn(scope).forEach((step) => this.prepStepOps[step].commit()); + } -@Component({ - selector: 'app-statistic-analysis-panel', - imports: [ChartRendererComponent, SpinnerComponent], - templateUrl: './statistic-analysis-panel.component.html', - styleUrls: ['./statistic-analysis-panel.component.css'] -}) -export class StatisticAnalysisPanelComponent implements OnChanges { - @Input() processedData: any[] = []; - @Input() variables: any[] = []; - @Input() covariates: any[] = []; - @Input() filters: any[] = []; - @ViewChildren(ChartRendererComponent) - chartRenderers!: QueryList<ChartRendererComponent>; - isExporting = false; - readonly mipVersion = (window as any).__env?.MIP_VERSION || '9.0.0'; + /** The run config one card's Apply writes: its pending rules over the applied rest + * (untouched variables on their defaults, exactly as a full apply would send them). */ + private buildScopedPreprocessingConfig(step: PrepStep): PreprocessingConfig | null { + const config: PreprocessingConfig = {}; + const missing = this.buildMissingPreprocessingConfig(step === 'missing' + ? this.pendingPreprocessingRules + : this.mergeRulesForCurrentSelection({}, this.appliedPreprocessingRules)); + if (missing) config['missing_values_handler'] = missing; + const outlier = this.buildOutlierPreprocessingConfig(step === 'outlier' + ? this.pendingOutlierRules + : this.mergeOutlierRulesForCurrentSelection({}, this.appliedOutlierRules)); + if (outlier) config['outlier_winsorizer'] = outlier; + const longitudinal = this.buildLongitudinalPreprocessingConfig(step !== 'longitudinal'); + if (longitudinal) config['longitudinal_transformer'] = longitudinal; + return Object.keys(config).length ? config : null; + } - private expStudioService = inject(ExperimentStudioService); - private chartBuilder = inject(ChartBuilderService); - private pdfExportService = inject(PdfExportService); + private buildPreprocessingConfig( + rules: Record<string, PreprocessingRule>, + allowedCodes = this.currentPreprocessingCodeSet() + ): PreprocessingConfig | null { + const config: PreprocessingConfig = {}; + const missingValuesHandler = this.buildMissingPreprocessingConfig(rules, allowedCodes); + if (missingValuesHandler) config['missing_values_handler'] = missingValuesHandler; - openAccordions: Record<string, boolean> = {}; - isLoading = true; + const outlier = this.buildOutlierPreprocessingConfig( + rules === this.appliedPreprocessingRules ? this.appliedOutlierRules : this.pendingOutlierRules, + allowedCodes + ); + if (outlier) config['outlier_winsorizer'] = outlier; - activeTab: TabKey = 'Variables'; - showBoxPlots = false; - distributionSubTab: 'Numeric' | 'Nominal' = 'Numeric'; + const longitudinal = this.buildLongitudinalPreprocessingConfig(rules === this.appliedPreprocessingRules, allowedCodes); + if (longitudinal) config['longitudinal_transformer'] = longitudinal; - nonNominalVariables: Array<{ code: string; name?: string; label?: string; type?: string; enumerations?: any[] }> = []; - nominalVariables: Array<{ code: string; name?: string; label?: string; type?: string; enumerations?: any[] }> = []; - chartsForBoxPlot: EChartsOption[][] = []; - chartsForNominal: EChartsOption[][] = []; - activeBoxPlotIndex = 0; - activeNominalIndex = 0; - modelTables: ModelTableBlock[] = []; + return Object.keys(config).length ? config : null; + } - modelData: Array<{ - name: string; - columns: string[]; - rows: Array<{ metric: string; values: Record<string, string> }>; - }> = []; + /** Build the exaflow config for one card, or null when it is not yet complete. */ + buildTransformationConfigForDraft(draft: TransformationColumnDraft): Record<string, unknown> | null { + const code = draft.code.trim(); + if (!code) return null; + const rules: Record<string, CategoryFilter> = {}; + for (const r of draft.rules) { + const value = r.value.trim(); + if (value && r.filter) rules[value] = r.filter; + } + if (!Object.keys(rules).length) return null; + // Exaflow names the created column with `new_column_names`; the draft keeps `code` as the + // editable UI name only. The emitted creator parameters never carry a `code` key. + const config: Record<string, unknown> = { + new_column_names: [code], + strategy: 'filter_rules', + rules, + }; + const def = draft.defaultEnumeration.trim(); + if (def) config['default_enumeration'] = def; + return config; + } - constructor() { - effect(() => { - const variables = this.expStudioService.selectedVariables(); - const covariates = this.expStudioService.selectedCovariates(); - const filters = this.expStudioService.selectedFilters(); + onTransformationChange(): void { + // Each categorical draft upserts by id. An incomplete draft clears its outputs so the + // request skips it. A draft with a write problem leaves the prior parameters untouched and + // shows the problem on its status chip. + for (const draft of this.transformationDrafts) { + if (this.draftWriteProblem(draft)) continue; + const config = this.buildTransformationConfigForDraft(draft); + this.expStudioService.upsertTransformation({ + id: draft.id, + name: 'categorical_column_creator', + parameters: config ?? {}, + }); + } + this.invalidateCategoryCounts(); + this.emitProgressState(); + this.cdr.markForCheck(); + } - if (!variables.length && !covariates.length && !filters.length) { - this.processedData = []; - this.modelData = []; - this.showBoxPlots = false; - this.isLoading = false; - return; - } + /** Applied K-means creators from store cards (every card with an output name). */ + private appliedKMeansCreators(): Array<{ code: string; creator: Record<string, unknown> }> { + return this.cards() + .filter((step) => step.name === 'kmeans_cluster_creator') + .map((step) => { + const code = String((step.parameters['new_column_names'] as unknown[] | undefined)?.[0] ?? '').trim(); + return { code, creator: step.parameters }; + }) + .filter((entry) => !!entry.code); + } - this.fetchDescriptiveStatistics(); - }); + /** Category counts describe the config at the last Done; any later edit drops them. */ + private invalidateCategoryCounts(): void { + if (this.countsDraftId === null) return; + this.countsDraftId = null; + // ponytail: also cancels an in-flight K-means preview; scope per card if both overlap in practice. + this.transformationStatsRequestId++; + this.isTransformationStatsLoading = false; + this.transformationStatisticsError = ''; + const clusterCodes = new Set(this.appliedKMeansCreators().map((entry) => entry.code)); + this.transformationStatistics = this.transformationStatistics.filter((block) => clusterCodes.has(block.code)); } - ngOnChanges(changes: SimpleChanges): void { - if (changes['processedData']) { - this.isLoading = this.processedData.length === 0; + /** Placeholder (null-count) rows for one card so Preview shows the category shape. */ + private placeholderRowsForDraft(draft: TransformationColumnDraft): Array<{ value: string; count: number | null }> { + const configuredValues = draft.rules.map((r) => r.value.trim()).filter((v) => v.length > 0); + const rows: Array<{ value: string; count: number | null }> = configuredValues.map((value) => ({ value, count: null })); + const def = draft.defaultEnumeration.trim(); + if (def && !configuredValues.includes(def)) { + rows.push({ value: def, count: null }); } + return rows; } - setTab(tab: TabKey) { - this.activeTab = tab; + /** + * Why no card reached a complete config, phrased once for every named card that is short + * of one: a card with no named category needs a category, a card whose categories are + * unnamed by a filter needs the filter. A stage with no named cards at all has nothing to + * explain — the empty state already says so. + */ + private transformationStatsGapNotice(): string { + const gaps: string[] = []; + if (this.transformationDrafts.some((draft) => draft.code.trim() + && !draft.rules.some((rule) => rule.value.trim().length > 0))) { + gaps.push('a named category'); + } + if (this.transformationRulesNeedFilters) { + gaps.push('a filter on each one'); + } + return gaps.length + ? `Each derived column needs ${gaps.join(' and ')} before counts can be loaded.` + : ''; } - private computeDistributionVariables(variable_based: any[] = []): void { - const selectedVars = this.expStudioService.selectedVariables(); - const selectedCovars = this.expStudioService.selectedCovariates(); - const all = [...selectedVars, ...selectedCovars]; - const dedupMap = new Map(all.map(v => [v.code, v])); - const unique = Array.from(dedupMap.values()); + /** + * The counts note belongs to rule-built columns. A K-means column carries its own + * sentence, in the same shape as the categorical column card. + */ + showsRuleColumnCounts(): boolean { + const clusterCodes = new Set(this.appliedKMeansCreators().map((entry) => entry.code)); + return this.transformationStatistics.some((block) => !clusterCodes.has(block.code)); + } - // Identify variables that have counts in the response - const varsWithCounts = new Set<string>(); - for (const item of variable_based) { - if (item.data && item.data.counts && Object.keys(item.data.counts).length > 0) { - varsWithCounts.add(item.variable); - } + /** + * Present an applied K-means column as a categorical column: a name, one assignment + * rule per cluster, and the variables that placed each record. Null for any other column. + */ + kmeansColumnView(code: string): { + code: string; + lead: string; + foot: string; + rules: Record<string, string>; + } | null { + const target = code.trim(); + const match = this.appliedKMeansCreators().find((entry) => entry.code === target); + if (!match) return null; + + const sources = kmeansCreatorVariables(match.creator); + const variables = sources + .map((variable) => this.variableLabelForCode(variable)) + .filter((label, index, all) => !!label && label !== sources[index] && all.indexOf(label) === index); + const choices = kmeansCreatorClusterChoices(match.creator); + const rules: Record<string, string> = {}; + for (const choice of choices) { + const label = choice.label.trim(); + rules[choice.cluster_id] = label && label !== choice.cluster_id + ? `Nearest cluster — ${label}` + : 'Nearest cluster'; + } + const count = choices.length; + const clusterWord = count === 1 ? 'cluster' : 'clusters'; + return { + code: target, + lead: variables.length + ? `Each record is assigned to the nearest of these clusters, using ${variables.join(', ')}.` + : 'Each record is assigned to the nearest of these clusters.', + foot: variables.length + ? `${count} ${clusterWord} from ${variables.join(', ')} · covers every record that has those variables` + : `${count} ${clusterWord} · covers every record`, + rules, + }; + } + + refreshTransformationStatistics(): void { + // One stats table per derived column (categorical cards and every applied K-means + // cluster column); counts come from a single describe over all of them. + const kmeansEntries = this.appliedKMeansCreators(); + const kmeansPlaceholders = kmeansEntries.map((entry) => { + const values = kmeansCreatorClusterChoices(entry.creator).map((choice) => choice.cluster_id); + return { code: entry.code, rows: values.map((value) => ({ value, count: null as number | null })) }; + }); + const placeholderBlocks = [ + ...this.transformationDrafts + .filter((draft) => draft.code.trim().length > 0) + .map((draft) => ({ code: draft.code.trim(), rows: this.placeholderRowsForDraft(draft) })), + ...kmeansPlaceholders, + ]; + const configs = this.transformationConfigs(); + + if (!configs.length && !kmeansEntries.length) { + this.transformationStatistics = placeholderBlocks; + // Every named card gets a heading over this table, so a named card always needs a + // reason beside it: falling through to the "counts from a describe run" line would + // describe a describe that never ran. + this.transformationStatisticsError = this.transformationStatsGapNotice(); + this.isTransformationStatsLoading = false; + this.cdr.markForCheck(); + return; + } + + const columnCodes = [ + ...configs.map((config) => String((config['new_column_names'] as unknown[] | undefined)?.[0] ?? '').trim()).filter(Boolean), + ...kmeansEntries.map((entry) => entry.code), + ]; + const columnSet = new Set(columnCodes); + // Same CDE set a run would send: filter fields + selected variables + K-means/PCA sources + // that are not earlier outputs. Created columns (pca_pc*, cluster codes) stay out. + const sourceCodes = Array.from( + new Set( + [ + ...configs.flatMap((config) => + Object.values((config['rules'] as Record<string, BackendFilter>) ?? {}).flatMap((filter) => + this.expStudioService.filterVariableCodes(filter) + ) + ), + ...this.expStudioService.transformationSourceCodesForInput(), + ...this.expStudioService.selectedVariables().map((v) => String(v?.code ?? '')), + ] + .map((code) => String(code).trim()) + .filter((code) => !!code && !columnSet.has(code)) + ) + ); + + if (!sourceCodes.length) { + this.transformationStatistics = placeholderBlocks; + this.transformationStatisticsError = configs.length + ? 'Category filters must reference at least one data-model variable before counts can be loaded.' + : 'The K-means cluster column has no clustering variables to read.'; + this.isTransformationStatsLoading = false; + this.cdr.markForCheck(); + return; } - // Nominal variables: Explicitly nominal OR found to have counts - this.nominalVariables = unique.filter( - (v) => v?.type === 'nominal' || varsWithCounts.has(v.code) + const requestId = ++this.transformationStatsRequestId; + this.isTransformationStatsLoading = true; + this.transformationStatisticsError = ''; + this.transformationStatistics = placeholderBlocks; + this.cdr.markForCheck(); + + // Pipeline consistency: the preview uses the same ordered-step builder as a run + // (singletons, then PCA/categorical/K-means in store order with CDE-only drops). + if (Object.keys(this.validatePendingRules()).length > 0) { + this.transformationStatistics = placeholderBlocks; + this.transformationStatisticsError = 'Fix the preprocessing rules before loading category counts.'; + this.isTransformationStatsLoading = false; + this.cdr.markForCheck(); + return; + } + const preprocessing = this.expStudioService.buildTransformationDescribePreprocessing( + this.buildPreprocessingConfig(this.pendingPreprocessingRules), ); + this.expStudioService + .loadDescriptiveOverview(columnCodes, preprocessing, sourceCodes) + .subscribe({ + next: (response) => { + if (requestId !== this.transformationStatsRequestId) return; + if (!response) { + this.transformationStatisticsError = 'Failed to load category counts.'; + this.isTransformationStatsLoading = false; + this.cdr.markForCheck(); + return; + } + this.transformationStatistics = [ + ...configs.map((config) => { + const code = String((config['new_column_names'] as unknown[] | undefined)?.[0] ?? '').trim(); + const rules = (config['rules'] as Record<string, BackendFilter>) ?? {}; + const def = String(config['default_enumeration'] ?? '').trim(); + return { + code, + rows: this.buildTransformationStatisticsFromDescribe(response, code, Object.keys(rules), def), + }; + }), + ...kmeansEntries.map((entry) => { + const values = kmeansCreatorClusterChoices(entry.creator).map((choice) => choice.cluster_id); + return { + code: entry.code, + rows: this.buildTransformationStatisticsFromDescribe(response, entry.code, values, ''), + }; + }), + ]; + const hasAnyCount = this.transformationStatistics.some((block) => + block.rows.some((row) => typeof row.count === 'number' && Number.isFinite(row.count)) + ); + this.transformationStatisticsError = hasAnyCount + ? '' + : this.isErrorResponse(response) + ? 'Failed to load category counts.' + : 'No counts were returned (privacy threshold or insufficient data after preprocessing).'; + this.isTransformationStatsLoading = false; + this.cdr.markForCheck(); + }, + error: () => { + if (requestId !== this.transformationStatsRequestId) return; + this.transformationStatisticsError = 'Failed to load category counts.'; + this.isTransformationStatsLoading = false; + this.cdr.markForCheck(); + }, + }); + } + + private isErrorResponse(response: unknown): boolean { + const r = response as { status?: unknown; result?: { type?: unknown; data?: unknown } } | null; + return r?.status === 'error' || + (typeof r?.result?.type === 'string' && r.result.type.includes('error')) || + typeof r?.result?.data === 'string'; + } - // Non-nominal variables: everything else - // We exclude those already identified as nominal to avoid duplication - const nominalCodes = new Set(this.nominalVariables.map(v => v.code)); - this.nonNominalVariables = unique.filter( - (v) => !nominalCodes.has(v.code) && v?.type !== 'text' + /** Category counts as the same horizontal bar chart the summary uses for groups. */ + transformationCountChart(block: { code: string; rows: Array<{ value: string; count: number | null }> }): HistogramPreviewData | null { + if (!block.rows.some((row) => typeof row.count === 'number')) return null; + return { + bins: block.rows.map((row) => row.value), + counts: block.rows.map((row) => row.count), + variableName: block.code, + variableType: 'nominal', + }; + } + + private buildTransformationStatisticsFromDescribe( + response: unknown, + columnCode: string, + configuredValues: string[], + defaultEnumeration: string + ): Array<{ value: string; count: number | null }> { + const featurewise = getFeaturewiseDescribeRows(response); + const row = + featurewise.find( + (item: { variable?: string; dataset?: string }) => + item?.variable === columnCode && String(item?.dataset ?? '') === 'all datasets' + ) ?? + featurewise.find((item: { variable?: string }) => item?.variable === columnCode); + const counts = (row as { data?: { counts?: Record<string, number> } })?.data?.counts ?? {}; + const ordered = [...configuredValues]; + if (defaultEnumeration && !ordered.includes(defaultEnumeration)) { + ordered.push(defaultEnumeration); + } + Object.keys(counts).forEach((key) => { + if (!ordered.includes(key)) ordered.push(key); + }); + return ordered.map((value) => { + const raw = counts[value]; + const count = typeof raw === 'number' && Number.isFinite(raw) ? raw : null; + return { value, count }; + }); + } + + private buildMissingPreprocessingConfig( + rules: Record<string, PreprocessingRule>, + allowedCodes = this.currentPreprocessingCodeSet() + ): Record<string, unknown> | null { + const strategies: Record<string, string> = {}; + const fillValues: Record<string, string> = {}; + + Object.values(rules).forEach((rule) => { + if (!allowedCodes.has(rule.variableCode)) return; + if (!rule.enabled || rule.action === 'no_action') return; + strategies[rule.variableCode] = rule.action; + if (rule.action === 'constant') fillValues[rule.variableCode] = rule.value; + }); + + if (!Object.keys(strategies).length) return null; + const missingValuesHandler: Record<string, unknown> = { strategies }; + if (Object.keys(fillValues).length) missingValuesHandler['fill_values'] = fillValues; + return missingValuesHandler; + } + + private buildOutlierPreprocessingConfig( + rules: Record<string, OutlierRule>, + allowedCodes = this.currentPreprocessingCodeSet() + ): Record<string, unknown> | null { + const outlierCodes = new Set( + Array.from(this.currentOutlierPreprocessingCodeSet()).filter((code) => allowedCodes.has(code)) ); + const serialized = serializeOutlierRules(rules, outlierCodes); + return serialized ? { ...serialized } : null; + } + + private validatePendingRulesFor(scope: PrepStep | 'all'): Record<string, string> { + return Object.assign({}, ...this.stepsIn(scope).map((step) => this.prepStepOps[step].validate())); + } - this.showBoxPlots = this.nonNominalVariables.length > 0 || this.nominalVariables.length > 0; + private validatePendingRules(): Record<string, string> { + return this.validatePendingRulesFor('all'); + } - // Auto-select the appropriate sub-tab based on available variable types - if (this.nonNominalVariables.length > 0 && this.nominalVariables.length === 0) { - this.distributionSubTab = 'Numeric'; - } else if (this.nominalVariables.length > 0 && this.nonNominalVariables.length === 0) { - this.distributionSubTab = 'Nominal'; - } else if (this.nonNominalVariables.length > 0) { - this.distributionSubTab = 'Numeric'; // Default to Numeric when both exist + /** + * Records what blocks a run (splitting off the codes the outlier step owns) and says + * whether the caller has to stop. Clears both maps when the rules are valid, so a + * fixed rule never leaves stale red text behind. + */ + private reportPendingValidationErrors( + validationErrors: Record<string, string>, + scope: PrepStep | 'all' = 'all', + ): boolean { + const outlierCodes = this.currentOutlierPreprocessingCodeSet(); + const inScope = (code: string): boolean => { + if (scope === 'all') return true; + if (scope === 'outlier') return outlierCodes.has(code); + if (scope === 'longitudinal') return code === '__longitudinal__'; + return !outlierCodes.has(code) && code !== '__longitudinal__'; + }; + const kept = Object.fromEntries( + Object.entries(this.preprocessingValidationErrors).filter(([code]) => !inScope(code)) + ); + if (Object.keys(validationErrors).length === 0) { + this.preprocessingValidationErrors = kept; + if (scope === 'all' || scope === 'outlier') this.outlierValidationErrors = {}; + return false; + } + this.preprocessingValidationErrors = { ...kept, ...validationErrors }; + if (scope === 'all' || scope === 'outlier') { + this.outlierValidationErrors = Object.fromEntries( + Object.entries(validationErrors).filter(([code]) => outlierCodes.has(code)) + ); } + this.cdr.markForCheck(); + return true; } - private buildBoxPlotCharts(response: any) { - this.chartsForBoxPlot = this.nonNominalVariables.map((v) => { - const perVarResp = { - ...response, - result: { - ...response.result, - variable_based: (response?.result?.variable_based ?? []).filter( - (r: any) => r.variable === v.code - ) + private validatePendingMissingRules(): Record<string, string> { + const errors: Record<string, string> = {}; + const currentCodes = this.currentPreprocessingCodeSet(); + Object.values(this.pendingPreprocessingRules).forEach((rule) => { + if (!currentCodes.has(rule.variableCode)) return; + if (!rule.enabled || rule.action === 'no_action') return; + const variable = this.preprocessingVariables.find((row) => row.code === rule.variableCode); + if (!variable) return; + if ((rule.action === 'mean' || rule.action === 'median') && !this.isNumericVariable(variable)) { + errors[rule.variableCode] = 'Mean and median imputation are only available for numeric variables.'; + } + if (rule.action === 'constant') { + const value = String(rule.value ?? '').trim(); + if (!value) { + errors[rule.variableCode] = 'A constant value is required.'; + return; } - }; - return this.chartBuilder.getChartsForAlgorithm('describe', perVarResp); + const enumCodes = this.enumCodes(variable); + if (enumCodes.length && !enumCodes.includes(value)) { + errors[rule.variableCode] = `Use one of the categorical enum codes: ${enumCodes.join(', ')}.`; + } + } }); - this.activeBoxPlotIndex = 0; + return errors; } - private buildNominalCharts(response: any) { - const variable_based = response?.result?.variable_based ?? []; + private validatePendingOutlierRules(): Record<string, string> { + const errors: Record<string, string> = {}; + const outlierCodes = this.currentOutlierPreprocessingCodeSet(); + Object.values(this.pendingOutlierRules).forEach((rule) => { + if (!outlierCodes.has(rule.variableCode)) return; + const error = validateOutlierRule(rule); + if (error) errors[rule.variableCode] = error; + }); + return errors; + } - this.chartsForNominal = this.nominalVariables.map((v) => { - const varData = variable_based.filter((r: any) => r.variable === v.code); - const varLabel = v.name || v.label || v.code; - const enumMap = this.getEnumLabelMap(v); - return buildGroupedBarChart(varData, varLabel, enumMap); + private validatePendingLongitudinalRules(): Record<string, string> { + const errors: Record<string, string> = {}; + if (this.isLongitudinalModel) { + if (!this.longitudinalVisit1 || !this.longitudinalVisit2) { + errors['__longitudinal__'] = 'Both longitudinal visits are required.'; + } else if (this.longitudinalVisit1 === this.longitudinalVisit2) { + errors['__longitudinal__'] = 'Select two different longitudinal visits.'; + } + + this.preprocessingVariables.forEach((variable) => { + const strategy = this.longitudinalStrategyFor(variable); + if (strategy === 'diff' && !this.isNumericVariable(variable)) { + errors[variable.code] = 'Difference strategy is only available for numeric variables.'; + } + }); + } + return errors; + } + + private updatePreprocessingStatus(): void { + if (this.pendingChangeCount > 0) this.preprocessingStatus = 'pending'; + else if (this.buildPreprocessingConfig(this.appliedPreprocessingRules)) this.preprocessingStatus = 'applied'; + else this.preprocessingStatus = 'none'; + this.clearOutlierPreview(); + if (this.sectionOpen().processed) this.fetchProcessedPreview(); + } + + private clearOutlierPreview(): void { + this.outlierPreviewRows = []; + this.outlierPreviewError = ''; + this.isLoadingOutlierPreview = false; + } + + private reconcilePreprocessingForSelection(): void { + const currentCodes = this.currentPreprocessingCodeSet(); + const appliedPreprocessing = this.expStudioService.getAppliedDescriptivePreprocessing(); + if (appliedPreprocessing) { + this.hydrateAppliedPreprocessing(appliedPreprocessing, currentCodes); + // Edit-mode hydration: the saved experiment already had preprocessing + // applied, so its step is genuinely complete (not a fresh auto-applied + // default). Create mode must stay out: auto-applied defaults there are + // persisted into the applied config without a user apply. + if (this.expStudioService.editingExistingExperiment()) { + this.userPreprocessingApplied = true; + } + } else { + this.ensureDefaultRulesForCurrentSelection(currentCodes); + this.ensureOutlierDefaultsForCurrentSelection(); + // No persisted config means this session has no explicit apply yet. + this.userPreprocessingApplied = false; + } + this.preprocessingValidationErrors = Object.fromEntries( + Object.entries(this.preprocessingValidationErrors).filter(([code]) => currentCodes.has(code)) + ); + const outlierCodes = this.currentOutlierPreprocessingCodeSet(); + this.outlierValidationErrors = Object.fromEntries( + Object.entries(this.outlierValidationErrors).filter(([code]) => outlierCodes.has(code)) + ); + this.sectionOpen.update((open) => ({ ...open, processed: false })); + this.ensureLongitudinalDefaults(); + if (!this.isApplyingPreprocessing) { + this.clearProcessedPreviewSubscription(); + } + this.updatePreprocessingStatus(); + this.syncAppliedPreprocessingForCurrentSelection(); + this.fetchProcessedPreview(); + } + + private hydrateAppliedPreprocessing( + preprocessing: PreprocessingConfig, + currentCodes = this.currentPreprocessingCodeSet() + ): void { + const missingValues = preprocessing['missing_values_handler'] as { + strategies?: Record<string, unknown>; + fill_values?: Record<string, unknown>; + } | undefined; + const strategies = missingValues?.strategies ?? {}; + const fillValues = missingValues?.fill_values ?? {}; + const nextApplied = this.cloneRules(this.appliedPreprocessingRules); + const nextPending = this.cloneRules(this.pendingPreprocessingRules); + + currentCodes.forEach((code) => { + const strategy = String(strategies[code] ?? ''); + const action = this.isMissingAction(strategy) ? strategy : 'no_action'; + const rule: PreprocessingRule = action === 'no_action' + ? this.emptyRule(code) + : { + variableCode: code, + action, + value: fillValues[code] === undefined || fillValues[code] === null ? '' : String(fillValues[code]), + enabled: true, + }; + nextApplied[code] = rule; + const existingPending = nextPending[code]; + nextPending[code] = action === 'no_action' + ? { + ...(existingPending && this.serializeRule(existingPending) !== this.serializeRule(rule) + ? existingPending + : this.defaultRule(code)), + } + : { ...rule }; + }); + + this.appliedPreprocessingRules = nextApplied; + this.pendingPreprocessingRules = nextPending; + + const outlier = preprocessing['outlier_winsorizer']; + const outlierCodes = this.currentOutlierPreprocessingCodeSet(); + const hydratedOutlier = hydrateOutlierRules(outlier, outlierCodes); + const nextAppliedOutlier = cloneOutlierRules(this.appliedOutlierRules); + const nextPendingOutlier = cloneOutlierRules(this.pendingOutlierRules); + outlierCodes.forEach((code) => { + const rule = hydratedOutlier[code] ?? this.defaultOutlierRule(code); + nextAppliedOutlier[code] = { ...rule }; + nextPendingOutlier[code] = { ...rule }; }); - this.activeNominalIndex = 0; + this.appliedOutlierRules = nextAppliedOutlier; + this.pendingOutlierRules = nextPendingOutlier; + + const longitudinal = preprocessing['longitudinal_transformer'] as { + visit1?: unknown; + visit2?: unknown; + strategies?: Record<string, unknown>; + } | undefined; + + if (!longitudinal || !this.isLongitudinalModel) { + this.appliedLongitudinalEnabled = false; + this.appliedLongitudinalVisit1 = ''; + this.appliedLongitudinalVisit2 = ''; + this.appliedLongitudinalStrategies = {}; + } else { + this.appliedLongitudinalEnabled = true; + this.appliedLongitudinalVisit1 = longitudinal.visit1 === undefined || longitudinal.visit1 === null ? '' : String(longitudinal.visit1); + this.appliedLongitudinalVisit2 = longitudinal.visit2 === undefined || longitudinal.visit2 === null ? '' : String(longitudinal.visit2); + this.longitudinalVisit1 = this.appliedLongitudinalVisit1; + this.longitudinalVisit2 = this.appliedLongitudinalVisit2; + const longitudinalStrategies = longitudinal.strategies ?? {}; + const appliedStrategies: Record<string, LongitudinalStrategy> = {}; + currentCodes.forEach((code) => { + const strategy = String(longitudinalStrategies[code] ?? ''); + if (this.isLongitudinalStrategy(strategy)) appliedStrategies[code] = strategy; + }); + this.appliedLongitudinalStrategies = appliedStrategies; + this.pendingLongitudinalStrategies = { ...appliedStrategies }; + } + + this.hydrateAppliedTransformation(); } - fetchDescriptiveStatistics(): void { - this.isLoading = true; + /** One editable card per stored categorical step, keyed by the step id. */ + private hydrateAppliedTransformation(): void { + this.transformationDrafts = this.cards() + .filter((step) => step.name === 'categorical_column_creator') + .map((step) => ({ + ...this.draftFromCreatorConfig(step.parameters as { + new_column_names?: unknown; + rules?: Record<string, CategoryFilter>; + default_enumeration?: unknown; + }, step.id), + chosen: true, + })); + } + + /** Rebuild one editable card from a persisted categorical_column_creator config. */ + private draftFromCreatorConfig( + creator: { + new_column_names?: unknown; + rules?: Record<string, CategoryFilter>; + default_enumeration?: unknown; + }, + id: string, + ): TransformationColumnDraft { + const rules = creator.rules ?? {}; + const code = Array.isArray(creator.new_column_names) ? String(creator.new_column_names[0] ?? '').trim() : ''; + return { + id, + code, + defaultEnumeration: creator.default_enumeration == null ? '' : String(creator.default_enumeration), + rules: Object.entries(rules).map(([value, filter]) => ({ + value, + filter: filter ?? null, + })), + open: true, + }; + } - const variables = this.expStudioService.selectedVariables(); - const covariates = this.expStudioService.selectedCovariates(); - const filters = this.expStudioService.selectedFilters(); + private isMissingAction(value: string): value is MissingAction { + return ['drop', 'mean', 'median', 'constant'].includes(value); + } - const filterCodes = new Set(filters.map(f => f.code)); - const uniqueVariables = Array.from(new Map(variables.map(v => [v.code, v])).values()); - const uniqueCovariates = Array.from(new Map(covariates.map(c => [c.code, c])).values()); + private isLongitudinalStrategy(value: string): value is LongitudinalStrategy { + return ['first', 'second', 'diff'].includes(value); + } - // merged variables + covariates - const merged = [ - ...uniqueVariables, - ...uniqueCovariates - ]; + private ensureDefaultRulesForCurrentSelection(currentCodes = this.currentPreprocessingCodeSet()): void { + const nextPending = this.cloneRules(this.pendingPreprocessingRules); + let changed = false; + currentCodes.forEach((code) => { + if (!nextPending[code] && !this.appliedPreprocessingRules[code]) { + nextPending[code] = this.defaultRule(code); + changed = true; + } + }); + if (changed) this.pendingPreprocessingRules = nextPending; + } - const items = Array.from(new Map(merged.map(v => [v.code, v])).values()); - if (!items.length) { this.isLoading = false; return; } + private ensureOutlierDefaultsForCurrentSelection(currentCodes = this.currentOutlierPreprocessingCodeSet()): void { + const nextPending = cloneOutlierRules(this.pendingOutlierRules); + let changed = false; + currentCodes.forEach((code) => { + if (!nextPending[code] && !this.appliedOutlierRules[code]) { + nextPending[code] = this.defaultOutlierRule(code); + changed = true; + } + }); + if (changed) this.pendingOutlierRules = nextPending; + } - const variableCodes = items.map(i => i.code); + private emitProgressState(): void { + this.progressStateChange.emit({ + pendingChangeCount: this.pendingChangeCount, + // Report 'applied' only for an explicit user apply; auto-applied and + // hydrated defaults stay out of the emitted status so the studio stepper + // does not mark Data review Done without user work. + preprocessingStatus: + this.preprocessingStatus === 'applied' && !this.userPreprocessingApplied + ? 'none' + : this.preprocessingStatus, + transformationStatusLabel: this.transformationStatusLabel, + }); + } - this.expStudioService.loadDescriptiveOverview(variableCodes).subscribe({ - next: (response) => { - const res = response?.result ?? response ?? {}; - const variable_based = res.variable_based ?? []; - const model_based = res.model_based ?? []; + private persistAppliedDescriptivePreprocessing(preprocessing: PreprocessingConfig | null): void { + // Singletons only — column creators live in appliedTransformations and must keep order/ids. + this.expStudioService.setAppliedDescriptivePreprocessing(preprocessing); + this.appliedConfigWrite = JSON.stringify(this.expStudioService.appliedPreprocessingConfig() ?? null); + } - const dsFromPayload = Array.from( - new Set((variable_based ?? []).map((x: any) => String(x.dataset))) - ) as string[]; + private syncAppliedPreprocessingForCurrentSelection(): void { + this.persistAppliedDescriptivePreprocessing( + this.buildPreprocessingConfig(this.appliedPreprocessingRules) + ); + } - const allLast: string[] = dsFromPayload - .filter((d: string) => d && d !== 'all datasets') - .concat('all datasets'); + private currentPreprocessingCodeSet(): Set<string> { + return new Set(this.preprocessingVariables.map((variable) => variable.code)); + } - const varList = items; + private currentOutlierPreprocessingCodeSet(): Set<string> { + return new Set(this.outlierPreprocessingVariables.map((variable) => variable.code)); + } - // Variables tab - this.processedData = this.pivotByDataset(variable_based, varList, allLast); + private buildLongitudinalPreprocessingConfig( + applied: boolean, + allowedCodes = this.currentPreprocessingCodeSet() + ): Record<string, unknown> | null { + if (!this.isLongitudinalModel) return null; + const enabled = applied ? this.appliedLongitudinalEnabled : true; + if (!enabled) return null; - // Model tab - filter out "Missing" row as model-based uses complete data only - this.modelData = this.pivotByDataset(model_based, varList, allLast).map(v => ({ - ...v, - rows: v.rows.filter(r => r.metric !== 'Missing') - })); + const visit1 = applied ? this.appliedLongitudinalVisit1 : this.longitudinalVisit1; + const visit2 = applied ? this.appliedLongitudinalVisit2 : this.longitudinalVisit2; + const sourceStrategies = applied ? this.appliedLongitudinalStrategies : this.pendingLongitudinalStrategies; + const strategies: Record<string, string> = {}; - // Box Plots - this.computeDistributionVariables(variable_based); - if (this.nonNominalVariables.length > 0) this.buildBoxPlotCharts(response); - if (this.nominalVariables.length > 0) this.buildNominalCharts(response); - this.isLoading = false; - }, - error: (err) => { console.error(err); this.isLoading = false; } + this.preprocessingVariables.forEach((variable) => { + if (!allowedCodes.has(variable.code)) return; + const strategy = sourceStrategies[variable.code] ?? (applied ? null : this.defaultLongitudinalStrategy(variable)); + if (strategy) strategies[variable.code] = strategy; }); + + if (!visit1 || !visit2 || !Object.keys(strategies).length) return null; + return { visit1, visit2, strategies }; } - processDescriptiveStatsResults(response: any) { - if (!response?.result?.variable_based) { - this.processedData = []; + private ensureLongitudinalDefaults(): void { + if (!this.isLongitudinalModel) { + this.appliedLongitudinalEnabled = false; return; } - const variableList = this.expStudioService.selectedVariables(); - - const grouped = response.result.variable_based.reduce((acc: any, v: any) => { - const match = variableList.find((vv) => vv.code === v.variable); - const name = match ? (match.name ?? match.label ?? match.code) : v.variable; - const d = v.data || {}; - - if (!acc[name]) acc[name] = { name, data: [] }; - - acc[name].data.push({ - dataset: v.dataset ?? 'all datasets', - stats: { - num_datapoints: d.num_dtps ?? 0, - num_missing: d.num_na ?? 0, - total: d.num_total ?? 0, - mean: d.mean ?? null, - std_dev: d.std ?? null, - min: d.min ?? null, - q1: d.q1 ?? null, - median: d.q2 ?? null, - q3: d.q3 ?? null, - max: d.max ?? null, - } - }); + const visits = this.visitOptions; + if (!this.longitudinalVisit1 && visits[0]) this.longitudinalVisit1 = visits[0].code; + if (!this.longitudinalVisit2 && visits[1]) this.longitudinalVisit2 = visits[1].code; - return acc; - }, {}); + const next = { ...this.pendingLongitudinalStrategies }; + this.preprocessingVariables.forEach((variable) => { + if (!next[variable.code]) next[variable.code] = this.defaultLongitudinalStrategy(variable); + }); + this.pendingLongitudinalStrategies = next; + } - Object.values(grouped).forEach((g: any) => { - g.data.sort((a: any, b: any) => { - const A = a.dataset === 'all datasets' ? 'zzzz' : a.dataset.toLowerCase(); - const B = b.dataset === 'all datasets' ? 'zzzz' : b.dataset.toLowerCase(); - return A.localeCompare(B); - }); + private defaultLongitudinalStrategy(variable: VariableRow): LongitudinalStrategy { + return this.isNumericVariable(variable) ? 'diff' : 'first'; + } + + private serializeLongitudinalState(applied: boolean): string { + const enabled = applied ? this.appliedLongitudinalEnabled : this.isLongitudinalModel; + const visit1 = applied ? this.appliedLongitudinalVisit1 : this.longitudinalVisit1; + const visit2 = applied ? this.appliedLongitudinalVisit2 : this.longitudinalVisit2; + const sourceStrategies = applied ? this.appliedLongitudinalStrategies : this.pendingLongitudinalStrategies; + const allowedCodes = this.currentPreprocessingCodeSet(); + const strategies = Object.fromEntries( + this.preprocessingVariables + .filter((variable) => allowedCodes.has(variable.code)) + .map((variable) => [ + variable.code, + sourceStrategies[variable.code] ?? (applied ? null : this.defaultLongitudinalStrategy(variable)), + ]) + ); + return JSON.stringify({ enabled, visit1, visit2, strategies }); + } + + private hasAppliedPreprocessing(variableCode: string): boolean { + const rule = this.appliedPreprocessingRules[variableCode]; + return !!rule?.enabled && rule.action !== 'no_action'; + } + + private hasAppliedLongitudinalPreprocessing(variableCode: string): boolean { + if (!this.isLongitudinalModel || !this.appliedLongitudinalEnabled) return false; + if (!this.appliedLongitudinalVisit1 || !this.appliedLongitudinalVisit2) return false; + return !!this.appliedLongitudinalStrategies[variableCode]; + } + + private hasAppliedOutlierPreprocessing(variableCode: string): boolean { + return !!this.appliedOutlierRules[variableCode]?.enabled; + } + + private mergeRulesForCurrentSelection( + base: Record<string, PreprocessingRule>, + source: Record<string, PreprocessingRule> + ): Record<string, PreprocessingRule> { + const next = this.cloneRules(base); + this.currentPreprocessingCodeSet().forEach((code) => { + next[code] = source[code] ? { ...source[code] } : this.defaultRule(code); }); + return next; + } - this.processedData = Object.values(grouped); + private mergeOutlierRulesForCurrentSelection( + base: Record<string, OutlierRule>, + source: Record<string, OutlierRule> + ): Record<string, OutlierRule> { + const next = cloneOutlierRules(base); + this.currentOutlierPreprocessingCodeSet().forEach((code) => { + next[code] = source[code] ? { ...source[code] } : this.defaultOutlierRule(code); + }); + return next; } - METRIC_LABEL: Record<MetricKey, string> = { - num_dtps: 'Datapoints', - num_na: 'Missing', - num_total: 'Total', - mean: 'Mean', - std: 'Standard Deviation', - min: 'Min', - q1: 'Q1', - q2: 'Median', - q3: 'Q3', - max: 'Max', - }; + // For Missing Values the default action is `drop` (NA removal): rows with + // missing values are removed. Adding a variable does not create a pending + // change — the default is implicit, and the user can opt into a different + // imputation strategy (mean, median, constant) when needed. + private defaultRule(variableCode: string): PreprocessingRule { + return { variableCode, action: 'drop', value: '', enabled: true }; + } - // helper: metrics + label - METRIC_ORDER: Array<{ key: string; label: string }> = [ - { key: 'num_datapoints', label: 'Datapoints' }, - { key: 'num_missing', label: 'Missing' }, - { key: 'num_total', label: 'Total' }, - { key: 'mean', label: 'Mean' }, - { key: 'std', label: 'Standard Deviation' }, - { key: 'min', label: 'Min' }, - { key: 'q1', label: 'Q1' }, - { key: 'q2', label: 'Median' }, - { key: 'q3', label: 'Q3' }, - { key: 'max', label: 'Max' }, - ]; + private defaultOutlierRule(variableCode: string): OutlierRule { + return createDefaultOutlierRule(variableCode, false); + } + + private emptyRule(variableCode: string): PreprocessingRule { + return { variableCode, action: 'no_action', value: '', enabled: false }; + } + + private cloneRules(rules: Record<string, PreprocessingRule>): Record<string, PreprocessingRule> { + return Object.fromEntries( + Object.entries(rules).map(([code, rule]) => [code, { ...rule }]) + ); + } + + private serializeRule(rule: PreprocessingRule | undefined): string { + const normalized = rule ?? this.emptyRule(''); + return JSON.stringify({ + action: normalized.action, + value: normalized.value, + enabled: normalized.enabled, + }); + } + + private buildSelectionKey( + variables: VariableRow[], + filters: VariableRow[], + filterLogic: unknown, + // Raw's own dependency set omits applied preprocessing on purpose: the raw describe + // is requested with a null config, so applying preprocessing cannot change its numbers. + appliedPreprocessing?: unknown + ): string { + return JSON.stringify({ + variables: variables.map((variable) => variable.code).sort(), + filters: filters.map((variable) => variable.code).sort(), + filterLogic, + appliedPreprocessing, + }); + } + + private discardPendingTransformation(): void { + this.hydrateAppliedTransformation(); + if (!this.transformationDrafts.length) { + this.transformationStatistics = []; + this.transformationStatisticsError = ''; + } + this.onTransformationChange(); + } + + private getSummaryTotalRows(summary: SummaryView): number | null { + const all = (summary.featurewiseRows ?? []).find((item: any) => item.dataset === 'all datasets'); + if (!all) return null; + const total = Number(all.data?.num_total); + return Number.isFinite(total) ? total : null; + } + + get cohortRetention(): { raw: number; processed: number; dropped: number; percentage: number } | null { + const raw = this.getSummaryTotalRows(this.rawSummary); + const processed = this.getSummaryTotalRows(this.processedSummary); + if (raw === null || processed === null) return null; + return { + raw, + processed, + dropped: Math.max(0, raw - processed), + percentage: raw > 0 ? (processed / raw) * 100 : 0, + }; + } + + /** Rows the default drop-NaN rule would remove. Only priced when both raw and + * processed summaries have loaded; otherwise the rail stays honest and the + * row falls back to its unpriced layout. */ + private get rowsDroppedByDefault(): number | null { + const retention = this.cohortRetention; + return retention && retention.dropped > 0 ? retention.dropped : null; + } - // 2-decimals formatter - private fmt(v: any): string { - if (v === null || v === undefined) return 'N/A'; - if (typeof v === 'number') return v.toFixed(2); + private isNumericVariable(variable: VariableRow): boolean { + return isNumericalVariableType(variable.type); + } + + isCategoricalVariable(variable: VariableRow): boolean { + return isCategoricalVariableType(variable.type) || !!variable.enumerations?.length; + } + + private enumCodes(variable: VariableRow): string[] { + return (variable.enumerations ?? []) + .map((item) => item.code ?? item.label ?? item.name) + .filter((value): value is string => value !== undefined && value !== null) + .map((value) => String(value)); + } + + private findModelVariable(code: string): VariableRow | null { + const model = this.expStudioService.selectedDataModel(); + const target = code.toLowerCase(); + const visit = this.flattenModelVariables(model?.variables ?? [], model?.groups ?? []) + .find((variable) => String(variable.code ?? '').toLowerCase() === target); + return visit ?? null; + } + + private flattenModelVariables(variables: any[], groups: Array<{ variables?: any[]; groups?: any[] }>): VariableRow[] { + return [ + ...variables, + ...groups.flatMap((group) => this.flattenModelVariables(group.variables ?? [], group.groups ?? [])), + ].filter((variable): variable is VariableRow => !!variable?.code); + } + + private variableForBlock(block: PivotBlock): VariableRow | null { + const target = block.name.toLowerCase(); + const code = block.code?.toLowerCase(); + return this.preprocessingVariables.find((variable) => + (code !== undefined && variable.code.toLowerCase() === code) || + this.variableLabel(variable).toLowerCase() === target || + variable.code.toLowerCase() === target + ) ?? null; + } - const n = Number(v); - return Number.isFinite(n) ? n.toFixed(2) : String(v); + private variableCodeForBlock(block: PivotBlock): string | null { + if (block.code) return block.code; + return this.variableForBlock(block)?.code ?? null; } - private fmtCount(v: any): string { - if (v === null || v === undefined) return 'N/A'; - if (typeof v === 'number' && Number.isFinite(v)) return String(Math.round(v)); - const n = Number(v); - return Number.isFinite(n) ? String(Math.round(n)) : String(v); + private mapBinsToEnumLabels( + bins: unknown[], + enumerations?: Array<{ code?: unknown; label?: string; name?: string }> + ): unknown[] { + if (!Array.isArray(bins) || !enumerations?.length) return bins; + + const codeToLabel = new Map( + enumerations.map((entry) => [ + String(entry?.code ?? entry?.label ?? entry?.name ?? ''), + entry?.label ?? entry?.name ?? String(entry?.code ?? ''), + ]) + ); + + let mapped = 0; + const mappedBins = bins.map((bin) => { + const label = codeToLabel.get(String(bin)); + if (label !== undefined) { + mapped += 1; + return label; + } + return bin; + }); + + return mapped > 0 ? mappedBins : bins; } - private getEnumLabelMap(variable: any): Map<string, string> { + private getEnumLabelMap(variable: VariableRow | undefined): Map<string, string> { const enums = Array.isArray(variable?.enumerations) ? variable.enumerations : []; const map = new Map<string, string>(); - enums.forEach((e: any) => { - const raw = e?.code ?? e?.label ?? e?.name; + enums.forEach((item) => { + const raw = item?.code ?? item?.label ?? item?.name; if (raw === null || raw === undefined) return; const key = String(raw); - const label = String(e?.label ?? e?.name ?? raw); + const label = String(item?.label ?? item?.name ?? raw); map.set(key, label); }); return map; } - // pivot variable_based or model_based + private pivotBlocksWithDatasetLabels(blocks: PivotBlock[]): PivotBlock[] { + return blocks.map((block) => { + const labeledColumns = block.columns.map((code) => this.datasetLabel(code)); + return { + ...block, + columns: labeledColumns, + rows: block.rows.map((row) => ({ + metric: row.metric, + values: Object.fromEntries( + block.columns.map((code, index) => [labeledColumns[index], row.values[code]]) + ), + })), + }; + }); + } + private pivotByDataset( items: any[], - variableList: any[], + variableList: VariableRow[], datasetOrder: string[] - ) { - // group per variable + ): PivotBlock[] { const byVar: Record<string, any[]> = {}; - for (const it of items || []) { - const arr = byVar[it.variable] || (byVar[it.variable] = []); - arr.push(it); + for (const item of items || []) { + const arr = byVar[item.variable] || (byVar[item.variable] = []); + arr.push(item); } - // Change -> [{ name, columns, rows:[{metric, values:Record<dataset,string>}] }] - const result: Array<{ - name: string; - columns: string[]; - rows: Array<{ metric: string; values: Record<string, string> }>; - }> = []; - + const result: PivotBlock[] = []; for (const [varCode, arr] of Object.entries(byVar)) { - const matched = variableList.find(v => v.code === varCode); - const varName = matched?.name ?? matched?.label ?? varCode; + const matched = variableList.find((v) => v.code === varCode); + const varName = matched ? this.variableLabel(matched) : varCode; const enumMap = this.getEnumLabelMap(matched); - const byDataset: Record<string, any> = {}; for (const entry of arr) byDataset[entry.dataset] = entry.data || {}; @@ -356,113 +4875,93 @@ export class StatisticAnalysisPanelComponent implements OnChanges { } let rows: Array<{ metric: string; values: Record<string, string> }> = []; - if (countsKeys.size > 0) { const baseMetrics = [ { key: 'num_datapoints', label: 'Datapoints' }, { key: 'num_missing', label: 'Missing' }, { key: 'num_total', label: 'Total' }, ]; - - rows = baseMetrics.map((m) => { + rows = baseMetrics.map((metric) => { const values: Record<string, string> = {}; for (const ds of datasetOrder) { const raw = - m.key === 'num_datapoints' ? byDataset[ds]?.num_dtps : - m.key === 'num_missing' ? byDataset[ds]?.num_na : + metric.key === 'num_datapoints' ? byDataset[ds]?.num_dtps : + metric.key === 'num_missing' ? byDataset[ds]?.num_na : byDataset[ds]?.num_total; - values[ds] = this.fmtCount(raw); + values[ds] = this.formatInteger(raw); } - return { metric: m.label, values }; + return { metric: metric.label, values }; }); const orderedKeys = Array.from(enumMap.keys()).filter((key) => countsKeys.has(key)); const remainingKeys = Array.from(countsKeys).filter((key) => !enumMap.has(key)); - const countOrder = orderedKeys.concat(remainingKeys); - - countOrder.forEach((key) => { + orderedKeys.concat(remainingKeys).forEach((key) => { const label = enumMap.get(key) ?? key; const values: Record<string, string> = {}; for (const ds of datasetOrder) { - const raw = byDataset[ds]?.counts?.[key]; - values[ds] = this.fmtCount(raw); + values[ds] = this.formatInteger(byDataset[ds]?.counts?.[key]); } rows.push({ metric: label, values }); }); } else { + const isCategorical = !!matched && this.isCategoricalVariable(matched); const countKeys = new Set(['num_datapoints', 'num_missing', 'num_total']); - rows = this.METRIC_ORDER.map(m => { + const metrics = isCategorical + ? this.metricOrder.filter((metric) => countKeys.has(metric.key)) + : this.metricOrder; + rows = metrics.map((metric) => { const values: Record<string, string> = {}; for (const ds of datasetOrder) { const raw = - m.key === 'num_datapoints' ? byDataset[ds]?.num_dtps : - m.key === 'num_missing' ? byDataset[ds]?.num_na : - m.key === 'num_total' ? byDataset[ds]?.num_total : - m.key === 'std' ? byDataset[ds]?.std : - m.key === 'q2' ? byDataset[ds]?.q2 : - byDataset[ds]?.[m.key]; - - values[ds] = countKeys.has(m.key) ? this.fmtCount(raw) : this.fmt(raw); + metric.key === 'num_datapoints' ? byDataset[ds]?.num_dtps : + metric.key === 'num_missing' ? byDataset[ds]?.num_na : + metric.key === 'num_total' ? byDataset[ds]?.num_total : + metric.key === 'std' ? byDataset[ds]?.std : + metric.key === 'q2' ? byDataset[ds]?.q2 : + byDataset[ds]?.[metric.key]; + values[ds] = countKeys.has(metric.key) ? this.formatInteger(raw) : this.formatNumber(raw); } - return { metric: m.label, values }; + return { metric: metric.label, values }; }); } - - result.push({ name: varName, columns: datasetOrder, rows }); + result.push({ code: varCode, name: varName, columns: datasetOrder, rows }); } - return result; } - async exportAllDescriptiveToPDF(): Promise<void> { - this.isExporting = true; - try { - const numericCharts = document.querySelectorAll( - '.hidden-charts-for-export .numeric-charts-export app-chart-renderer' - ) as NodeListOf<HTMLElement>; - - const nominalCharts = document.querySelectorAll( - '.hidden-charts-for-export .nominal-charts-export app-chart-renderer' - ) as NodeListOf<HTMLElement>; - - const dataModel = this.expStudioService.selectedDataModel(); - const pathologyName = dataModel?.label || dataModel?.code || ''; + private statisticBlockKey(block: PivotBlock): string { + return block.code ?? block.name; + } - await this.pdfExportService.exportDescriptiveStatisticsPdf({ - pathologyName, - variables: this.processedData, - models: this.modelData, - charts: numericCharts, - nonNominalVariables: this.nonNominalVariables, - nominalCharts: nominalCharts, - nominalVariables: this.nominalVariables, - mipVersion: this.mipVersion - }); - } catch (err) { - console.error('PDF export failed:', err); - } finally { - this.isExporting = false; - } + private statisticBlockType(kind: SummaryKind, block: PivotBlock): StatisticVariableType { + const summary = this.getSummary(kind); + const variable = this.statisticBlockVariable(block); + if (variable && summary.nominalVariables.some((item) => item.code === variable.code)) return 'nominal'; + if (variable && this.isCategoricalVariable(variable)) return 'nominal'; + const numericMetrics = new Set(['Mean', 'Standard Deviation', 'Minimum', 'Q1', 'Median', 'Q3', 'Maximum']); + return block.rows.some((row) => numericMetrics.has(row.metric)) ? 'numeric' : 'nominal'; } - toggleAccordion(name: string): void { - this.openAccordions[name] = !this.openAccordions[name]; + private statisticMetricText(block: PivotBlock, metric: string): string { + const row = block.rows.find((item) => item.metric === metric); + if (!row) return 'N/A'; + const dataset = block.columns.includes('all datasets') ? 'all datasets' : block.columns[0]; + return row.values[dataset] ?? 'N/A'; } - isAccordionOpen(name: string): boolean { - return !!this.openAccordions[name]; + private statisticMetricNumber(block: PivotBlock, metric: string): number { + const value = Number(this.statisticMetricText(block, metric).replace(/,/g, '')); + return Number.isFinite(value) ? value : 0; } - expandAll(): void { - const data = this.activeTab === 'Variables' ? this.processedData : this.modelData; - if (!data?.length) return; - data.forEach((v: any) => (this.openAccordions[v.name] = true)); + private statisticMissingValue(block: PivotBlock): number { + return Math.round(this.statisticMetricNumber(block, 'Missing')); } - collapseAll(): void { - const data = this.activeTab === 'Variables' ? this.processedData : this.modelData; - if (!data?.length) return; - data.forEach((v: any) => (this.openAccordions[v.name] = false)); + private nextStatisticSelection(data: PivotBlock[], currentKey: string | null): string | null { + if (!data.length) return null; + if (currentKey && data.some((block) => this.statisticBlockKey(block) === currentKey)) return currentKey; + return this.statisticBlockKey(data[0]); } } diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.pipeline.spec.ts b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.pipeline.spec.ts new file mode 100644 index 000000000..c2b23de9f --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.pipeline.spec.ts @@ -0,0 +1,1164 @@ +import type { MockedObject } from 'vitest'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { of, throwError } from 'rxjs'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { provideEchartsCore } from 'ngx-echarts'; +import { StatisticAnalysisPanelComponent } from './statistic-analysis-panel.component'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; +import { ChartBuilderService } from '../visualisations/charts/chart-builder.service'; +import { PdfExportService } from '../../../services/pdf-export.service'; +import { createSpyObj } from '../../../testing/spy-obj.testing'; + +/** + * Contract: a stage counts as added only when its work is either opened this + * visit or present in the persisted request config. Opening a stage must never + * write that config. + */ +describe('StatisticAnalysisPanelComponent pipeline presence', () => { + let component: StatisticAnalysisPanelComponent; + let fixture: ComponentFixture<StatisticAnalysisPanelComponent>; + let mockExpService: MockedObject<ExperimentStudioService>; + + const age = { code: 'age', label: 'Age', type: 'continuous' }; + + /** Round-trips the applied config the way the real service does. */ + function seedAppliedConfig(config: Record<string, unknown> | null): void { + (mockExpService.appliedPreprocessingConfig as any).set(config); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(config as never); + // Mirror creators into the ordered store when the seed carries them. Singleton-only + // writes (Apply preprocessing) must not wipe transformation steps already in the store. + const hasCreators = !!config && ( + 'categorical_column_creator' in config + || 'kmeans_cluster_creator' in config + || 'pca_column_creator' in config + ); + if (!hasCreators) return; + const steps: { id: string; name: string; parameters: Record<string, unknown> }[] = []; + const categorical = config?.['categorical_column_creator']; + for (const parameters of Array.isArray(categorical) ? categorical : categorical ? [categorical] : []) { + steps.push({ id: crypto.randomUUID(), name: 'categorical_column_creator', parameters: parameters as Record<string, unknown> }); + } + const kmeans = config?.['kmeans_cluster_creator']; + if (kmeans && typeof kmeans === 'object') { + steps.push({ id: 'kmeans-seed', name: 'kmeans_cluster_creator', parameters: kmeans as Record<string, unknown> }); + } + const pca = config?.['pca_column_creator']; + if (pca && typeof pca === 'object') { + steps.push({ id: crypto.randomUUID(), name: 'pca_column_creator', parameters: pca as Record<string, unknown> }); + } + (mockExpService.appliedTransformations as any).set(steps); + } + + /** A stage card of the pipeline canvas, as rendered. */ + function pipelineCard(stage: 'analysis-preprocessing' | 'analysis-transformation'): HTMLElement { + return (fixture.nativeElement as HTMLElement).querySelector(`[data-guide="${stage}"]`) as HTMLElement; + } + + /** The collapsed rail rows, split into what runs and what the rail offers to add. */ + function railRows(card: HTMLElement): { live: HTMLElement[]; ghost: HTMLElement[] } { + // Scoped to the rail: the opened Transformation stage keeps its own ghost rows in the + // type chooser, which is part of the stage graph rather than of the overview. + const rows = Array.from(card.querySelectorAll('.pipeline-subnodes-tree .pipeline-subnode-item')) as HTMLElement[]; + return { + live: rows.filter((row) => !row.classList.contains('is-ghost')), + ghost: rows.filter((row) => row.classList.contains('is-ghost')), + }; + } + + beforeEach(async () => { + mockExpService = createSpyObj('ExperimentStudioService', [ + 'loadDescriptiveOverview', + 'loadOutlierReportPreview', + 'getAlgorithmResults', + 'getDatasetLabelMap', + 'setDataExclusionWarnings', + 'clearDataExclusionWarnings', + 'setAppliedDescriptivePreprocessing', + 'getAppliedDescriptivePreprocessing', + 'setFilters', + 'setFilterLogic', + 'filterVariableCodes', + 'buildTransformationDescribePreprocessing', + 'transformationSourceCodesForInput', + 'requestDatasets', + 'requestFilters', + 'getActiveDataModelCode', + 'upsertTransformation', + 'removeTransformation', + 'clearTransformations', + 'transformationDependents', + 'outputsInUse', + 'availableCreatorNames', + 'hasPreprocessingSpec', + 'catalogLoaded', + ], { + selectedVariables: signal([]), + selectedFilters: signal([]), + selectedDatasets: signal(['dataset-a']), + selectedDataModel: signal({ code: 'Stroke', version: '3.7', variables: [] }), + unavailableVariables: signal(new Map()), + filterLogic: signal(null), + editingExistingExperiment: () => false, + appliedPreprocessingConfig: signal<Record<string, unknown> | null>(null), + appliedTransformations: signal([] as { id: string; name: string; parameters: Record<string, unknown> }[]), + backendAlgorithms: signal({}) + }); + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ result: { featurewise: [] } })); + mockExpService.loadOutlierReportPreview.mockReturnValue(of({ result: { featurewise: [] } })); + mockExpService.getAlgorithmResults.mockReturnValue(of({ result: { histogram: [] } })); + mockExpService.getAppliedDescriptivePreprocessing.mockReturnValue(null); + mockExpService.getDatasetLabelMap.mockReturnValue({ 'dataset-a': 'Dataset A' }); + mockExpService.availableCreatorNames.mockReturnValue([ + 'categorical_column_creator', + 'kmeans_cluster_creator', + ]); + mockExpService.hasPreprocessingSpec.mockImplementation((name: string) => + ['categorical_column_creator', 'kmeans_cluster_creator'].includes(name), + ); + mockExpService.catalogLoaded.mockReturnValue(true); + mockExpService.transformationDependents.mockReturnValue([]); + mockExpService.outputsInUse.mockReturnValue([]); + mockExpService.upsertTransformation.mockImplementation((step: any) => { + const current = [...(mockExpService.appliedTransformations() as any[])]; + const index = current.findIndex((existing) => existing.id === step.id); + const next = index === -1 + ? [...current, step] + : current.map((existing, i) => (i === index ? step : existing)); + (mockExpService.appliedTransformations as any).set(next); + return true; + }); + mockExpService.clearTransformations.mockImplementation(() => + (mockExpService.appliedTransformations as any).set([])); + mockExpService.removeTransformation.mockImplementation((id: string) => { + (mockExpService.appliedTransformations as any).set( + (mockExpService.appliedTransformations() as any[]).filter((step) => step.id !== id), + ); + }); + mockExpService.requestDatasets.mockReturnValue(['dataset-a']); + mockExpService.requestFilters.mockReturnValue(null); + mockExpService.getActiveDataModelCode.mockReturnValue('Stroke:3.7'); + mockExpService.buildTransformationDescribePreprocessing.mockImplementation((singletons: any) => { + const steps: { name: string; parameters: Record<string, unknown> }[] = []; + if (singletons && typeof singletons === 'object') { + for (const [name, parameters] of Object.entries(singletons)) { + if (parameters && typeof parameters === 'object') { + steps.push({ name, parameters: parameters as Record<string, unknown> }); + } + } + } + for (const step of mockExpService.appliedTransformations() as any[]) { + if (Array.isArray(step.parameters?.new_column_names) + && step.parameters.new_column_names.some((n: unknown) => String(n ?? '').trim())) { + steps.push({ name: step.name, parameters: step.parameters }); + } + } + return steps.length ? steps : null; + }); + mockExpService.transformationSourceCodesForInput.mockReturnValue([]); + // Mirrors the real collector: a group walks its rules, a condition is its field. + mockExpService.filterVariableCodes.mockImplementation((logic: any) => { + const codes = new Set<string>(); + const walk = (node: any) => { + if (!node) return; + if (Array.isArray(node.rules)) { + node.rules.forEach(walk); + } else if (node.field || node.id) { + codes.add(String(node.field ?? node.id)); + } + }; + walk(logic); + return [...codes]; + }); + mockExpService.setAppliedDescriptivePreprocessing.mockImplementation((config: unknown) => + seedAppliedConfig(config as Record<string, unknown> | null)); + + await TestBed.configureTestingModule({ + imports: [StatisticAnalysisPanelComponent], + providers: [ + provideZonelessChangeDetection(), + provideEchartsCore({ echarts: () => import('echarts') }), + { provide: ExperimentStudioService, useValue: mockExpService }, + { provide: ExperimentsDashboardService, useValue: { listKMeansExperiments: () => of([]), listPcaExperiments: () => of([]) } }, + { provide: ChartBuilderService, useValue: (() => { const s = createSpyObj('ChartBuilderService', ['getChartsForAlgorithm']); s.getChartsForAlgorithm.mockReturnValue([]); return s; })() }, + { provide: PdfExportService, useValue: createSpyObj('PdfExportService', ['exportDescriptiveStatisticsPdf']) } + ] + }).compileComponents(); + + fixture = TestBed.createComponent(StatisticAnalysisPanelComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('marks Filtering active from persisted filter rules, without opening it', () => { + const logic: any = { condition: 'AND', rules: [{ field: 'age', operator: '>', value: 40 }] }; + (mockExpService.filterLogic as any).set(logic); + fixture.detectChanges(); + + expect(component.isStepAdded('filters')).toBe(true); + expect(component.activeStagesCount).toBe(1); + expect(mockExpService.filterLogic()).toBe(logic); + expect(mockExpService.setFilterLogic).not.toHaveBeenCalled(); + }); + + it('marks Preprocessing active from a persisted applied config', () => { + seedAppliedConfig({ missing_values_handler: { strategies: { age: 'drop' } } }); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + expect(component.isStepAdded('setup')).toBe(true); + expect(component.activeStagesCount).toBe(1); + }); + + it('counts transformation-only work as Transformation, not Preprocessing', () => { + seedAppliedConfig({ + categorical_column_creator: [{ + new_column_names: ['group'], + strategy: 'filter_rules', + rules: { mild: { field: 'age', operator: '<', value: 50 } }, + }], + }); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + expect(component.isStepAdded('transformation')).toBe(true); + expect(component.isStepAdded('setup')).toBe(false); + }); + + it('does not persist when Preprocessing is opened', () => { + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + mockExpService.setAppliedDescriptivePreprocessing.mockClear(); + mockExpService.upsertTransformation.mockClear(); + component.addStep('setup'); + component.goToSection('setup'); + fixture.detectChanges(); + + expect(mockExpService.setAppliedDescriptivePreprocessing).not.toHaveBeenCalled(); + expect(mockExpService.upsertTransformation).not.toHaveBeenCalled(); + expect((component as any).userPreprocessingApplied).toBe(false); + }); + + it('persists on Apply and clears the persisted config on Remove', () => { + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + component.onMissingActionChange(age, 'mean'); + component.applyPreprocessing(); + + expect(mockExpService.setAppliedDescriptivePreprocessing).toHaveBeenCalledWith( + expect.objectContaining({ missing_values_handler: { strategies: { age: 'mean' } } }), + ); + expect(component.isStepAdded('setup')).toBe(true); + + mockExpService.setAppliedDescriptivePreprocessing.mockClear(); + component.removeStep('setup'); + + expect(mockExpService.setAppliedDescriptivePreprocessing).toHaveBeenCalledWith(null); + expect(component.isStepAdded('setup')).toBe(false); + }); + + it('shows what the next run will use in the terminal card', async () => { + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + await fixture.whenStable(); + + component.rawSummary = { + ...component.rawSummary, + featurewiseRows: [{ dataset: 'all datasets', data: { num_total: 20 } }], + } as any; + component.processedSummary = { + ...component.processedSummary, + featurewiseRows: [{ dataset: 'all datasets', data: { num_total: 17 } }], + } as any; + // Toggling a signal marks the OnPush view for the summary mutation above; + // production paths call markForCheck when a summary lands instead. + component.showUnappliedChangesWarning.set(true); + fixture.detectChanges(); + + const terminal = fixture.nativeElement.querySelector('.pipeline-terminal-card') as HTMLElement; + const text = terminal.textContent ?? ''; + expect(text).toContain('The next run will use'); + expect(text).toContain('17 of 20 records'); + expect(text).toContain('1 variables'); + expect(text).toContain('1 datasets'); + expect(text).toContain('Missing values: remove rows (default)'); + expect(terminal.querySelector('.unapplied-warning-card')).toBeTruthy(); + }); + + it('shows the default NA-removal sub-node before any customization', () => { + const nodes = component.appliedPreprocessingSubNodes; + + expect(nodes.length).toBe(1); + expect(nodes[0].id).toBe('missing'); + expect(nodes[0].statusLabel).toBe('In run · Default'); + expect(component.appliedPreprocessingCount).toBe(0); + expect(component.appliedTransformationSubNodes).toEqual([]); + }); + + /** + * Default NaN removal is in the request from the first render, so the stage's rail says + * Default before anyone opens it. + */ + it('draws the untouched stage as a solid node tagged Default', () => { + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + mockExpService.setAppliedDescriptivePreprocessing.mockClear(); + + const preprocessing = pipelineCard('analysis-preprocessing'); + expect(preprocessing.querySelector('.pipeline-node-header h3')?.textContent?.trim()).toBe('Preprocessing'); + expect(preprocessing.querySelector('.pipeline-node-subtitle')?.textContent).toContain('Decide what happens to missing values'); + + const badge = preprocessing.querySelector('.pipeline-status-badge') as HTMLElement; + expect(badge.textContent?.trim()).toBe('In run · Default'); + expect(badge.classList.contains('default')).toBe(true); + expect(badge.classList.contains('applied')).toBe(false); + + const rows = railRows(preprocessing); + expect(rows.live.length).toBe(1); + expect(rows.live[0].textContent).toContain('Missing values'); + expect(rows.live[0].textContent).toContain('All 1 variable: remove rows with a missing value (default)'); + const chip = rows.live[0].querySelector('.pipeline-subnode-status') as HTMLElement; + expect(chip.getAttribute('data-tone')).toBe('default'); + expect(chip.textContent?.trim()).toBe('In run · Default'); + + expect(component.activeStagesCount).toBe(0); + expect(mockExpService.setAppliedDescriptivePreprocessing).not.toHaveBeenCalled(); + expect(mockExpService.appliedPreprocessingConfig()).toBeNull(); + }); + + /** + * The dashed rail row is the whole add flow: it opens the station already narrowed to that + * sub-step, so a stage never has to be expanded just to reach a control. + */ + it('adds a preprocessing sub-step from the collapsed rail without writing the request', () => { + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + expect(pipelineCard('analysis-preprocessing').querySelector('.pipeline-node-body')).toBeNull(); + const rows = railRows(pipelineCard('analysis-preprocessing')); + expect(rows.ghost.map((row) => row.textContent?.trim())).toEqual([expect.stringMatching('Add outlier clipping')]); + + mockExpService.setAppliedDescriptivePreprocessing.mockClear(); + (rows.ghost[0].querySelector('button') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(component.sectionOpen().setup).toBe(true); + expect(component.userEnabledOutliers()).toBe(true); + expect(component.preprocessingStepOpen.outlier).toBe(true); + expect(component.preprocessingStepOpen.missing).toBe(false); + expect(pipelineCard('analysis-preprocessing').querySelector('.pipeline-node-body')).toBeTruthy(); + expect(mockExpService.setAppliedDescriptivePreprocessing).not.toHaveBeenCalled(); + expect(railRows(pipelineCard('analysis-preprocessing')).ghost).toEqual([]); + }); + + /** + * An opened step that was never configured contributes nothing to the request, so closing + * it hands the stage back exactly as it was: the rail must keep offering the step, or the + * one control that creates it is gone until the user finds the editor by another route. + */ + it('offers the outlier step from the rail again after an untouched step is closed', () => { + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + const rail = railRows(pipelineCard('analysis-preprocessing')); + (rail.ghost[0].querySelector('button') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(component.pendingChangeCount).toBe(0); + // The card's own footer primary folds just that card; the stage keeps its shape. + component.commitOrCloseStep('outlier'); + fixture.detectChanges(); + + expect(component.preprocessingStepOpen.outlier).toBe(false); + expect(component.sectionOpen().setup).toBe(true); + expect(mockExpService.appliedPreprocessingConfig()).toBeNull(); + + // Collapsed, the stage hands the step back to its rail: closing an untouched card + // never consumed the one control that adds the step back. + component.collapseStep('setup'); + fixture.detectChanges(); + const rows = railRows(pipelineCard('analysis-preprocessing')); + expect(rows.live.length).toBe(1); + expect(rows.ghost.map((row) => row.textContent?.trim())).toEqual([expect.stringMatching('Add outlier clipping')]); + + (rows.ghost[0].querySelector('button') as HTMLButtonElement).click(); + fixture.detectChanges(); + expect(component.sectionOpen().setup).toBe(true); + expect(component.preprocessingStepOpen.outlier).toBe(true); + }); + + it('opens the transformation stage from the collapsed rail without inventing a column', () => { + seedAppliedConfig({ + categorical_column_creator: [{ + new_column_names: ['mrs_good_outcome'], + strategy: 'filter_rules', + rules: { good: { field: 'age', operator: '<', value: 50 } }, + }], + }); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + const rows = railRows(pipelineCard('analysis-transformation')); + expect(rows.live.map((row) => row.textContent)).toEqual([expect.stringMatching('mrs_good_outcome')]); + expect(rows.ghost.map((row) => row.textContent?.trim())).toEqual([expect.stringMatching('Add derived column')]); + + mockExpService.upsertTransformation.mockClear(); + (rows.ghost[0].querySelector('button') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(component.sectionOpen().transformation).toBe(true); + // The dashed row only opens the stage now: the graph holds the stored column, and the + // stage's own blank draft stays off screen until a type is chosen. + const graphCards = () => (fixture.nativeElement as HTMLElement) + .querySelectorAll('[data-guide="analysis-transformation"] .transformation-graph app-station-card'); + const blankCards = () => component.transformationDrafts.filter((draft) => !draft.code.trim() && !draft.rules.length).length; + expect(graphCards().length).toBe(1); + expect(blankCards()).toBe(0); + + component.addTransformationSubNode(); + fixture.detectChanges(); + expect(component.transformationDrafts.length).toBe(1); + expect(graphCards().length).toBe(1); + expect(blankCards()).toBe(0); + expect(mockExpService.upsertTransformation).not.toHaveBeenCalled(); + }); + + it('lists one sub-node per applied preprocessing step with its summary', () => { + const mmse = { code: 'mmse', label: 'MMSE', type: 'continuous' }; + (mockExpService.selectedVariables as any).set([age, mmse]); + seedAppliedConfig({ + missing_values_handler: { strategies: { age: 'mean', mmse: 'median' } }, + outlier_winsorizer: { strategies: { age: 'iqr' }, tails: { age: 'both' }, folds: { age: 1.5 } }, + }); + fixture.detectChanges(); + + const nodes = component.appliedPreprocessingSubNodes; + expect(nodes.map((n) => n.id)).toEqual(['missing', 'outlier']); + expect(nodes[0].subtitle).toContain('2 variables'); + expect(nodes[1].subtitle).toContain('IQR'); + expect(component.appliedPreprocessingCount).toBe(2); + expect(component.preprocessingStatusLabel).toBe('2 steps applied ✓'); + }); + + it('surfaces the applied derived column as a transformation sub-node', () => { + seedAppliedConfig({ + categorical_column_creator: [{ + new_column_names: ['mrs_good_outcome'], + strategy: 'filter_rules', + rules: { + good: { field: 'age', operator: '<', value: 50 }, + bad: { field: 'age', operator: '>=', value: 50 }, + }, + default_enumeration: 'unknown', + }], + }); + fixture.detectChanges(); + + const nodes = component.appliedTransformationSubNodes; + expect(nodes.length).toBe(1); + expect(nodes[0].title).toBe('Derived column: mrs_good_outcome'); + expect(nodes[0].subtitle).toContain('2 category rules'); + expect(nodes[0].subtitle).toContain('default: unknown'); + expect(component.appliedTransformationCount).toBe(1); + expect(component.transformationBadgeLabel).toBe('1 Transformation active'); + }); + + it('lists one sub-node per applied derived column and counts them in the badge', () => { + seedAppliedConfig({ + categorical_column_creator: [ + { + new_column_names: ['mrs_good_outcome'], + strategy: 'filter_rules', + rules: { good: { field: 'age', operator: '<', value: 50 } }, + }, + { + new_column_names: ['mrs_bad_outcome'], + strategy: 'filter_rules', + rules: { + bad: { field: 'age', operator: '>=', value: 50 }, + interim: { field: 'age', operator: '=', value: 50 }, + }, + default_enumeration: 'unknown', + }, + ], + }); + fixture.detectChanges(); + + const nodes = component.appliedTransformationSubNodes; + expect(nodes.length).toBe(2); + expect(nodes.map((node) => node.title)).toEqual([ + 'Derived column: mrs_good_outcome', + 'Derived column: mrs_bad_outcome', + ]); + expect(nodes[1].subtitle).toContain('2 category rules'); + expect(component.appliedTransformationCount).toBe(2); + expect(component.transformationBadgeLabel).toBe('2 Transformations active'); + }); + + it('surfaces the applied K-means cluster column as a transformation sub-node', () => { + seedAppliedConfig({ + kmeans_cluster_creator: { + new_column_names: ['kmeans_cluster'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { + enumeration_prefix: 'cluster_', + cluster_choices: [ + { cluster_id: 'c1', label: 'Cluster 1' }, + { cluster_id: 'c2', label: 'Cluster 2' }, + ], + cluster_variables: ['a', 'b'], + }, + }, + }); + fixture.detectChanges(); + + const nodes = component.appliedTransformationSubNodes; + const clusterNode = nodes.find((node) => node.title === 'K-means clusters: kmeans_cluster'); + expect(clusterNode?.id).toBe('transformation:kmeans-seed'); + expect(clusterNode?.subtitle).toBe('2 clusters from a, b'); + expect(clusterNode?.statusLabel).toBe('Applied'); + expect(clusterNode?.statusTone).toBe('applied'); + }); + + it('counts the cluster column as the stage status on its own', () => { + seedAppliedConfig({ + kmeans_cluster_creator: { + new_column_names: ['kmeans_cluster'], + k: 1, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { + enumeration_prefix: 'cluster_', + cluster_choices: [{ cluster_id: 'c1', label: 'Cluster 1' }], + cluster_variables: ['a'], + }, + }, + }); + fixture.detectChanges(); + + // No categorical card is configured, but the stage does hold a derived column. + expect(component.transformationStatusLabel).toBe('Applied'); + expect(component.transformationBadgeLabel).toBe('1 Transformation active'); + }); + + it('folds every derived column card on Apply so the overview can list them', () => { + seedAppliedConfig({ + categorical_column_creator: [ + { new_column_names: ['mrs_good_outcome'], strategy: 'filter_rules', rules: { good: { field: 'age', operator: '<', value: 50 } } }, + { new_column_names: ['mrs_bad_outcome'], strategy: 'filter_rules', rules: { bad: { field: 'age', operator: '>=', value: 50 } } }, + ], + }); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + component.addStep('transformation'); + fixture.detectChanges(); + expect(component.sectionOpen().transformation).toBe(true); + + // Echo the hydrated filters back so committing does not blank them. + const committed = component.transformationDrafts.flatMap((draft) => draft.rules.map((rule) => rule.filter)); + component.transformationRuleModals = { + toArray: () => committed.map((filter) => ({ exportFilterLogic: () => filter, filterError: () => null })), + } as any; + + // Every card answers for itself: its own Apply commits it and folds it, and the + // stage stays open until a click on its header folds it to the overview. + component.transformationDrafts.forEach((draft) => component.commitTransformationDraft(draft)); + fixture.detectChanges(); + expect(component.transformationDrafts.every((draft) => !draft.open)).toBe(true); + expect(component.sectionOpen().transformation).toBe(true); + + component.collapseStep('transformation'); + fixture.detectChanges(); + + expect(component.sectionOpen().transformation).toBe(false); + const overview = Array.from( + fixture.nativeElement.querySelectorAll('[data-guide="analysis-transformation"] .pipeline-subnodes-tree .pipeline-subnode-item') + ) as HTMLElement[]; + expect(overview.map((item) => item.textContent)).toEqual([ + expect.stringMatching('mrs_good_outcome'), + expect.stringMatching('mrs_bad_outcome'), + expect.stringMatching('Add derived column'), + ]); + }); + + it('expands the stage and opens only the clicked station on sub-node click', () => { + seedAppliedConfig({ missing_values_handler: { strategies: { age: 'mean' } } }); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + component.addStep('setup'); + component.preprocessingStepOpen.missing = true; + component.jumpToSubNode('setup', 'outlier'); + + expect(component.sectionOpen().setup).toBe(true); + expect(component.preprocessingStepOpen.outlier).toBe(true); + expect(component.preprocessingStepOpen.missing).toBe(false); + expect(component.preprocessingStepOpen.longitudinal).toBe(false); + + component.jumpToSubNode('transformation', 'transformation'); + expect(component.sectionOpen().transformation).toBe(true); + expect(component.preprocessingStepOpen.transformation).toBe(true); + }); + + it('opens step 0 as a read that adds no stage and writes no config', () => { + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + mockExpService.setFilterLogic.mockClear(); + mockExpService.setAppliedDescriptivePreprocessing.mockClear(); + component.toggleSourcePreview(); + fixture.detectChanges(); + + expect(component.sectionOpen().source).toBe(true); + expect(component.sectionOpen().filters).toBe(false); + expect(component.isStepAdded('filters')).toBe(false); + expect(component.activeStagesCount).toBe(0); + expect(mockExpService.setFilterLogic).not.toHaveBeenCalled(); + expect(mockExpService.setAppliedDescriptivePreprocessing).not.toHaveBeenCalled(); + + component.toggleSourcePreview(); + expect(component.sectionOpen().source).toBe(false); + }); + + it('renders un-added optional steps collapsed, with no editor mounted', () => { + (mockExpService.selectedVariables as any).set([age]); + (mockExpService.filterLogic as any).set(null); + fixture.detectChanges(); + const root = fixture.nativeElement as HTMLElement; + + for (const guide of ['analysis-filtering', 'analysis-transformation']) { + const section = root.querySelector(`[data-guide="${guide}"]`)!; + expect(section.classList.contains('is-compact'), guide).toBe(true); + expect(section.querySelector('.btn-remove-step-node'), guide).toBeNull(); + expect(section.querySelector('.pipeline-node-body'), guide).toBeNull(); + expect(section.querySelector('.pipeline-node-toggle')?.getAttribute('aria-expanded'), guide).toBe('false'); + } + expect(root.querySelector('[data-guide="analysis-filtering"]')?.textContent).toContain('Add condition'); + expect(root.querySelector('[data-guide="analysis-transformation"]')?.textContent).toContain('Add derived column'); + }); + + it('opens and folds every step from its title button', () => { + (mockExpService.selectedVariables as any).set([age]); + (mockExpService.filterLogic as any).set(null); + fixture.detectChanges(); + const toggle = (guide: string) => (fixture.nativeElement as HTMLElement) + .querySelector(`[data-guide="${guide}"] .pipeline-node-toggle`) as HTMLButtonElement; + + for (const [guide, isOpen] of [ + ['analysis-source-data', () => component.sectionOpen().source], + ['analysis-filtering', () => component.sectionOpen().filters], + ['analysis-preprocessing', () => component.sectionOpen().setup], + ['analysis-transformation', () => component.sectionOpen().transformation], + ] as const) { + toggle(guide).click(); + fixture.detectChanges(); + expect(isOpen(), guide).toBe(true); + expect(toggle(guide).getAttribute('aria-expanded'), guide).toBe('true'); + + toggle(guide).click(); + fixture.detectChanges(); + expect(isOpen(), guide).toBe(false); + } + expect(component.isStepAdded('filters')).toBe(true); + expect(component.isStepAdded('transformation')).toBe(true); + }); + + it('folds a step from its header, but not from its buttons or a text selection', () => { + (mockExpService.selectedVariables as any).set([age]); + component.goToSection('filters'); + fixture.detectChanges(); + const section = (fixture.nativeElement as HTMLElement).querySelector('[data-guide="analysis-filtering"]')!; + const subtitle = section.querySelector('.pipeline-node-header .pipeline-node-subtitle') as HTMLElement; + + vi.spyOn(component, 'removeStep').mockImplementation(() => undefined); + (section.querySelector('.btn-remove-step-node') as HTMLButtonElement).click(); + expect(component.removeStep).toHaveBeenCalledWith('filters'); + expect(component.sectionOpen().filters).toBe(true); + + const selection = vi.spyOn(window, 'getSelection').mockReturnValue({ toString: () => 'Keep only' } as Selection); + subtitle.click(); + expect(component.sectionOpen().filters).toBe(true); + + selection.mockRestore(); + subtitle.click(); + expect(component.sectionOpen().filters).toBe(false); + }); + + it('drops unapplied filter conditions with the removed step', () => { + (mockExpService.selectedVariables as any).set([age]); + (mockExpService.filterLogic as any).set(null); + component.goToSection('filters'); + fixture.detectChanges(); + const section = (fixture.nativeElement as HTMLElement).querySelector('[data-guide="analysis-filtering"]')!; + expect(section.querySelector('app-filter-config-modal')).not.toBeNull(); + + component.removeStep('filters'); + fixture.detectChanges(); + + // The builder is destroyed, so pending rules cannot survive into the next open. + expect(section.querySelector('app-filter-config-modal')).toBeNull(); + }); + + it('describes step 0 without the cohort filter and refetches only on a new selection', () => { + (mockExpService.selectedVariables as any).set([age]); + (mockExpService.filterLogic as any).set({ condition: 'AND', rules: [{ field: 'age', operator: '>', value: 40 }] }); + fixture.detectChanges(); + + mockExpService.loadDescriptiveOverview.mockClear(); + component.toggleSourcePreview(); + + // Filters are dropped from the request: the snapshot is the data as selected. + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith(['age'], null, null, null); + + // Close and reopen: the snapshot still matches the selection, so it is free. + mockExpService.loadDescriptiveOverview.mockClear(); + component.toggleSourcePreview(); + component.toggleSourcePreview(); + expect(mockExpService.loadDescriptiveOverview).not.toHaveBeenCalled(); + + // A new selection invalidates it, and an open snapshot refreshes at once. + mockExpService.loadDescriptiveOverview.mockClear(); + (mockExpService.selectedVariables as any).set([age, { code: 'mmse', label: 'MMSE', type: 'continuous' }]); + fixture.detectChanges(); + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalledWith(['age', 'mmse'], null, null, null); + }); + + it('renders the step 0 snapshot in the shared summary workspace', () => { + mockExpService.loadDescriptiveOverview.mockReturnValue(of({ + result: { + featurewise: [ + { dataset: 'dataset-a', variable: 'age', data: { num_dtps: 10, num_na: 2, num_total: 12, mean: 61 } }, + ], + }, + })); + (mockExpService.selectedVariables as any).set([age]); + fixture.detectChanges(); + + component.toggleSourcePreview(); + fixture.detectChanges(); + + const snapshot = (fixture.nativeElement as HTMLElement).querySelector('[data-guide="analysis-source-summary"]'); + // Same surface as the Raw preview: overlay tabs, variable browser, statistics table. + expect(snapshot?.querySelectorAll('.summary-tabs button').length).toBe(3); + expect(snapshot?.querySelector('.statistics-browser')).toBeTruthy(); + expect(snapshot?.querySelector('.summary-detail-toolbar h4')?.textContent).toContain('Age'); + expect(snapshot?.querySelector('.statistics-table')?.textContent).toContain('Dataset A'); + expect(snapshot?.querySelector('.summary-detail-toolbar .statistics-panel-kicker')?.textContent).toContain('Numerical'); + // Step 0 is read-only: no rule editor, and the tour anchors stay on the raw surface. + expect(snapshot?.querySelector('app-filter-config-modal')).toBeNull(); + expect(snapshot?.querySelector('[data-guide]')).toBeNull(); + }); + + /** + * The collapsed stage header is the only copy the overview shows, so it must read the + * stored request rather than the editor, and folding must close a stage without ever + * taking it out of the run. + */ + describe('stage header copy and collapse', () => { + it('lists one collapsed row per top-level cohort rule', () => { + (mockExpService.filterLogic as any).set({ + condition: 'AND', + rules: [ + { id: 'age', field: 'age', type: 'integer', input: 'number', operator: 'greater_or_equal', value: 60 }, + { + condition: 'OR', + rules: [ + { id: 'sex', field: 'sex', type: 'string', input: 'select', operator: 'equal', value: 'F' }, + { id: 'sex', field: 'sex', type: 'string', input: 'select', operator: 'equal', value: 'M' }, + ], + }, + ], + }); + fixture.detectChanges(); + + const rows = component.cohortRuleRows; + expect(rows.length).toBe(2); + expect(rows[0].subtitle).toBe('Numerical'); + expect(rows[0].title).toContain('60'); + expect(rows[1].subtitle).toBe('Group · matches any of 2 conditions'); + expect(rows[1].title).toContain(' or '); + expect(component.cohortMatchesAny).toBe(false); + expect(component.filteringStatusLabel).toBe('In run · 3 rules'); + }); + + it('reads Not in run yet without stored rules', () => { + (mockExpService.filterLogic as any).set(null); + fixture.detectChanges(); + + expect(component.filteringStatusLabel).toBe('Not in run yet'); + }); + + it('collapses the filter station without removing the step', () => { + component.goToSection('filters'); + component.collapseStep('filters'); + + expect(component.sectionOpen().filters).toBe(false); + expect(component.sectionOpen().raw).toBe(false); + expect(component.isStepAdded('filters')).toBe(true); + }); + + it('collapses transformations without removing the step', () => { + component.goToSection('transformation'); + component.collapseStep('transformation'); + + expect(component.sectionOpen().transformation).toBe(false); + expect(component.isStepAdded('transformation')).toBe(true); + }); + + it('closes a settled column card from its own footer without removing the step', () => { + component.addStep('transformation'); + // The stage opens on its type chooser; the footer bar belongs to the column card. + component.chooseTransformation('categorical'); + fixture.detectChanges(); + + const apply = (fixture.nativeElement as HTMLElement).querySelector( + '[data-guide="analysis-transformation"] .station-card-footer .station-action-apply') as HTMLButtonElement; + + expect(apply.textContent?.trim()).toBe('Close'); + expect(apply.disabled).toBe(false); + expect(apply.classList.contains('is-quiet')).toBe(true); + + apply.click(); + fixture.detectChanges(); + + // Close folds its own card and leaves the stage exactly where it was. + expect(component.transformationDrafts[0].open).toBe(false); + expect(component.sectionOpen().transformation).toBe(true); + expect(component.isStepAdded('transformation')).toBe(true); + }); + + it('opens a new transformation stage on the type chooser', () => { + component.addStep('transformation'); + fixture.detectChanges(); + + const graphCards = () => (fixture.nativeElement as HTMLElement).querySelectorAll( + '[data-guide="analysis-transformation"] .transformation-graph app-station-card'); + const chooserRows = () => Array.from( + (fixture.nativeElement as HTMLElement).querySelectorAll( + '[data-guide="analysis-transformation"] .transformation-chooser .pipeline-subnode-content') + ) as HTMLButtonElement[]; + + expect(component.transformationChooserOnly).toBe(true); + expect(graphCards().length).toBe(0); + expect(chooserRows().length).toBe(2); + + chooserRows()[0].click(); + fixture.detectChanges(); + + expect(graphCards().length).toBe(1); + expect(component.transformationChooserOnly).toBe(false); + + const kmeansRow = chooserRows().find((row) => row.textContent?.includes('K-means')) as HTMLButtonElement; + kmeansRow.click(); + fixture.detectChanges(); + + expect(graphCards().length).toBe(2); + // Chooser always lists every creator in the specs (repeated cards are allowed). + expect(chooserRows().length).toBe(2); + }); + + it('counts pending preprocessing changes in the badge and apply label', () => { + const pending = vi.spyOn(component, 'pendingChangeCount', 'get').mockReturnValue(2); + const missingPending = vi.spyOn(component, 'pendingMissingChangeCount', 'get').mockReturnValue(2); + + expect(component.preprocessingStatusLabel).toBe('In run · 2 changes not applied'); + expect(component.stepApplyLabel('missing')).toBe('Apply'); + + pending.mockReturnValue(1); + missingPending.mockReturnValue(0); + expect(component.preprocessingStatusLabel).toBe('In run · 1 change not applied'); + // A card with nothing pending offers Close in its own footer instead of an Apply. + expect(component.stepApplyLabel('missing')).toBe('Close'); + }); + + /** + * Every preprocessing card owns its Discard / Preview / Apply footer: the stage no + * longer carries a bar of its own, so an action can never reach across cards. + */ + it('gives each preprocessing card its own footer actions', () => { + (mockExpService.selectedVariables as any).set([age]); + component.addStep('setup'); + component.enableOutlierHandling(); + fixture.detectChanges(); + + const openCards = (Array.from( + (fixture.nativeElement as HTMLElement).querySelectorAll( + '[data-guide="analysis-preprocessing"] app-station-card') + ) as HTMLElement[]) + .filter((card) => card.querySelector('.station-card')?.classList.contains('is-open')); + + // Missing Values plus the optional Outlier Handling card, each with one primary. + expect(openCards.length).toBe(2); + for (const card of openCards) { + expect(card.querySelectorAll('.station-card-footer .station-action-apply').length).toBe(1); + } + + // Nothing floats outside a card footer: no stage-level action bar in the station. + const shell = (fixture.nativeElement as HTMLElement).querySelector( + '[data-guide="analysis-preprocessing"] .preprocessing-shell') as HTMLElement; + const strayBars = (Array.from(shell.querySelectorAll('.station-action-bar')) as HTMLElement[]) + .filter((bar) => !bar.closest('.station-card-footer')); + expect(strayBars).toEqual([]); + }); + + it('shows category counts on the rule rows and has no preview button', () => { + component.addStep('transformation'); + component.chooseTransformation('categorical'); + fixture.detectChanges(); + + const footer = (fixture.nativeElement as HTMLElement).querySelector( + '[data-guide="analysis-transformation"] .station-card-footer') as HTMLElement; + expect(footer.querySelector('.station-action-preview')).toBeNull(); + expect(footer.querySelector('.station-action-apply')).toBeTruthy(); + }); + + it('folds the How to use strip by default', () => { + component.goToSection('transformation'); + fixture.detectChanges(); + + const section = (fixture.nativeElement as HTMLElement).querySelector('[data-guide="analysis-transformation"]'); + const strip = section?.querySelector('details.how-to-strip') as HTMLDetailsElement | null; + expect(strip).toBeTruthy(); + expect(strip?.open).toBe(false); + expect(strip?.querySelector('summary .how-to-label')?.textContent?.trim()).toBe('How to use'); + + (strip as HTMLDetailsElement).open = true; + expect(strip?.querySelectorAll('.how-to-number').length).toBe(3); + }); + + it('numbers the column editor and adds categories from the ghost row', () => { + component.goToSection('transformation'); + // The stage opens on the type chooser; the editor belongs to the column card. + component.chooseTransformation('categorical'); + fixture.detectChanges(); + + const section = (fixture.nativeElement as HTMLElement).querySelector('[data-guide="analysis-transformation"]'); + const create = section?.querySelector('.transformation-create'); + expect(create?.querySelectorAll('.column-step').length).toBe(2); + expect(Array.from(create?.querySelectorAll('.column-step-number') ?? []) + .map((number) => number.textContent?.trim())) + .toEqual(['1', '2']); + + const ghost = section?.querySelector('.transformation-rules-list .transformation-add-category') as HTMLButtonElement | null; + expect(ghost?.nextElementSibling?.classList.contains('transformation-rule-fallback')).toBe(true); + + const before = (component as any).transformationDrafts[0].rules.length as number; + (ghost as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect((component as any).transformationDrafts[0].rules.length).toBe(before + 1); + expect((ghost as HTMLButtonElement).textContent).toContain('Add category'); + }); + }); + + describe('category rule focus', () => { + function rulesBlock(): HTMLElement { + return (fixture.nativeElement as HTMLElement).querySelector( + '[data-guide="analysis-transformation"] .transformation-rules') as HTMLElement; + } + + function openCardWithTwoCategories() { + // The rule builder renders its conditions only once there are variables to filter on. + (mockExpService.selectedVariables as any).set([age]); + component.goToSection('transformation'); + component.chooseTransformation('categorical'); + const draft = component.transformationDrafts[0]; + component.addTransformationRule(draft); + component.addTransformationRule(draft); + fixture.detectChanges(); + return draft; + } + + it('opens the new category on a started filter', () => { + const draft = openCardWithTwoCategories(); + + expect(draft.openRuleIndex).toBe(1); + const editing = rulesBlock().querySelector('.transformation-rule-editor.is-open'); + expect(editing?.querySelector('.empty-filter-group')).toBeNull(); + expect(editing?.querySelector('.condition-row')).not.toBeNull(); + expect(rulesBlock().querySelector('.transformation-rule-edit-link')?.textContent?.trim()).toBe('Set rule'); + }); + + it('focuses the table on the rule being edited, and Done folds back to the list', () => { + const draft = openCardWithTwoCategories(); + + expect(rulesBlock().classList.contains('is-focused')).toBe(true); + const editing = rulesBlock().querySelectorAll('.transformation-rule-row.is-editing'); + expect(editing.length).toBe(1); + expect(editing[0].querySelector('.transformation-rule-number')?.textContent?.trim()).toBe('2'); + // Its own Edit button gives way to Done inside the editor. + expect(editing[0].querySelector('.transformation-rule-edit-link')).toBeNull(); + expect(rulesBlock().closest('.column-step')?.querySelector('.column-step-meta')?.textContent?.trim()) + .toBe('Editing category 2 of 2'); + + const filter = { condition: 'AND', rules: [{ id: 'age', field: 'age', operator: 'greater', value: 60 }] }; + const modal = component.transformationRuleModals!.toArray()[1]; + vi.spyOn(modal, 'exportFilterLogic').mockReturnValue(filter as any); + (editing[0].querySelector('.transformation-rule-done') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(draft.rules[1].filter).toEqual(filter as any); + expect(draft.openRuleIndex).toBeNull(); + expect(rulesBlock().classList.contains('is-focused')).toBe(false); + expect(rulesBlock().querySelectorAll('.transformation-rule-edit-link')[1].textContent?.trim()).toBe('Edit rule'); + }); + + it('keeps a rule open when its builder reports an error on Done', () => { + const draft = openCardWithTwoCategories(); + component.toggleTransformationRule(draft, 0); + fixture.detectChanges(); + + const modal = component.transformationRuleModals!.toArray()[0]; + vi.spyOn(modal, 'exportFilterLogic').mockReturnValue(null); + vi.spyOn(modal, 'filterError').mockReturnValue('Pick a value'); + component.finishTransformationRule(draft, 0); + + expect(draft.openRuleIndex).toBe(0); + expect(draft.rules[0].filter).toBeNull(); + }); + }); + + describe('review regressions', () => { + // Built on the component's own empty summary so teardown finds its histogram maps. + const summaryOf = (total: number) => ({ + ...(component as any).createEmptySummary(false), + featurewiseRows: [{ dataset: 'all datasets', data: { num_total: total } }], + }); + + it('keeps a chosen K-means card in the graph when it is folded', () => { + component.addStep('transformation'); + component.chooseTransformation('kmeans'); + component.toggleCardOpen(component.cards()[0].id, false); + + expect(component.kmeansCardShown).toBe(true); + expect(component.transformationChooserOnly).toBe(false); + }); + + it('Clear drops the K-means card and its preview, back to the chooser', () => { + component.addStep('transformation'); + component.chooseTransformation('kmeans'); + const stepId = component.cards().find((step) => step.name === 'kmeans_cluster_creator')!.id; + mockExpService.upsertTransformation({ + id: stepId, + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['cluster'], + k: 1, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { + enumeration_prefix: 'cluster_', + cluster_choices: [{ cluster_id: 'c1', label: 'Cluster 1' }], + cluster_variables: ['a'], + }, + }, + }); + component.previewTransformationCard(stepId); + + component.resetTransformation(); + + expect(component.previewDraftId).toBeNull(); + expect(component.kmeansCardShown).toBe(false); + expect(component.transformationChooserOnly).toBe(true); + }); + + it('previewing the K-means card commits no categorical card', () => { + component.addStep('transformation'); + component.chooseTransformation('kmeans'); + const stepId = component.cards().find((step) => step.name === 'kmeans_cluster_creator')!.id; + const commit = vi.spyOn(component, 'commitTransformationRuleFilters').mockReturnValue(undefined); + vi.spyOn(component, 'refreshTransformationStatistics').mockReturnValue(undefined); + + component.previewTransformationCard(stepId); + + expect(commit).not.toHaveBeenCalled(); + expect(component.previewDraftId).toBe(stepId); + }); + + it('hides Records out while the Raw summary is pinned to an unapplied preview', () => { + (mockExpService.filterLogic as any).set(null); + (component as any).rawSummaryKey = (component as any).rawSummaryKeyFor(); + (component as any).rawSummary = summaryOf(400); + expect(component.recordsOut.source).toBe(400); + expect(component.recordsOut.filtered).toBe(400); + + // Pinned to unsaved "age >= 60": 120 rows are not the source cohort. + const pinned = { condition: 'AND', rules: [{ id: 'age', field: 'age', operator: 'greater_or_equal', value: 60 }] } as any; + (component as any).rawSummaryKey = (component as any).rawSummaryKeyFor(pinned); + (component as any).rawSummary = summaryOf(120); + expect(component.recordsOut.source).toBeNull(); + expect(component.recordsOut.filtered).toBeNull(); + }); + + it('never rounds a partial filter match to 0% or 100%', () => { + (component as any).sourceSummary = summaryOf(1000); + (component as any).rawSummary = summaryOf(998); + expect(component.filterPreviewStatus).toContain('· 99%'); + (component as any).rawSummary = summaryOf(2); + expect(component.filterPreviewStatus).toContain('· 1%'); + (component as any).rawSummary = summaryOf(1000); + expect(component.filterPreviewStatus).toContain('· 100%'); + }); + + it('keeps a card open with a message when its own Apply fails', () => { + (mockExpService.selectedVariables as any).set([age]); + component.addStep('setup'); + fixture.detectChanges(); + component.pendingPreprocessingRules = { + age: { variableCode: 'age', action: 'mean', value: '', enabled: true }, + } as any; + mockExpService.loadDescriptiveOverview.mockReturnValue(throwError(() => new Error('boom'))); + vi.spyOn(console, 'error').mockReturnValue(undefined); + + component.commitOrCloseStep('missing'); + + expect(component.preprocessingStepOpen.missing).toBe(true); + expect(component.sectionOpen().processed).toBe(false); + expect(component.preprocessingApplyError['missing']).toContain('Could not apply'); + expect(component.stepPendingCount('missing')).toBe(1); + + component.resetChanges('missing'); + expect(component.preprocessingApplyError['missing']).toBeUndefined(); + }); + + it('applying one card leaves the other card pending and unsaved', () => { + (mockExpService.selectedVariables as any).set([age]); + component.addStep('setup'); + fixture.detectChanges(); + component.onMissingActionChange(age, 'mean'); + component.pendingOutlierRules = { + age: { variableCode: 'age', enabled: true, strategy: 'iqr', tail: 'both', fold: 1.5 }, + }; + + component.commitOrCloseStep('missing'); + TestBed.flushEffects(); + + expect(component.pendingOutlierRules['age'].enabled).toBe(true); + expect(component.stepPendingCount('outlier')).toBe(1); + expect(component.stepPendingCount('missing')).toBe(0); + const saved = mockExpService.setAppliedDescriptivePreprocessing.mock.lastCall![0] as Record<string, unknown>; + expect(saved['missing_values_handler']).toEqual(expect.objectContaining({ strategies: { age: 'mean' } })); + expect(saved['outlier_winsorizer']).toBeUndefined(); + }); + + it('previews one card while another card is invalid', () => { + (mockExpService.selectedVariables as any).set([age]); + component.addStep('setup'); + fixture.detectChanges(); + component.onMissingActionChange(age, 'mean'); + component.pendingOutlierRules = { + age: { variableCode: 'age', enabled: true, strategy: 'iqr', tail: 'both', fold: null }, + }; + mockExpService.loadDescriptiveOverview.mockClear(); + + component.previewProcessedData('missing'); + + expect(mockExpService.loadDescriptiveOverview).toHaveBeenCalled(); + const config = mockExpService.loadDescriptiveOverview.mock.lastCall![1] as Record<string, unknown>; + expect(config['missing_values_handler']).toEqual(expect.objectContaining({ strategies: { age: 'mean' } })); + expect(config['outlier_winsorizer']).toBeUndefined(); + }); + }); +}); diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.preprocessing.css b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.preprocessing.css new file mode 100644 index 000000000..edc9a2a19 --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.preprocessing.css @@ -0,0 +1,1131 @@ +/* Preprocessing stations of the Data review step: station shell, preview bar, + glass feedback cards, the documentation panel, the missing/outlier/longitudinal + controls and the preprocessing variable browser. + Split from statistic-analysis-panel.component.css to keep every component + stylesheet inside the anyComponentStyle budget. Angular concatenates the + component's styleUrl array IN ORDER, and this file is the second of three — + the order below is the cascade, so do not reorder or move rules across files: + 1. statistic-analysis-panel.component.css panel shell + node-pipeline canvas + 2. statistic-analysis-panel.preprocessing.css (this file) + 3. statistic-analysis-panel.results.css shared controls + result surfaces + Shared/one-off overrides that must win over styles.css live in the file that + owns the rule's section, never duplicated. */ + +.workflow-section-body.open { + display: block; +} + +.station-preview-bar { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + margin: 0 0 10px; +} + +/* Stations sit in a gapped shell: the preview bar needs no extra bottom margin. */ +.preprocessing-shell .station-preview-bar { + margin-bottom: 0; +} + +.station-preview-toggle { + appearance: none; + display: inline-flex; + align-items: center; + gap: 8px; + margin: 0 0 0 auto; + padding: 8px 16px; + border: 1px solid rgba(127, 156, 232, 0.38); + border-radius: 999px; + background: rgba(255, 255, 255, 0.96); + color: #334155; + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + line-height: 1.2; + white-space: nowrap; + cursor: pointer; + transition: background 0.2s ease, border-color 0.2s ease, opacity 0.2s ease; +} + +.station-preview-toggle.station-preview-close { + margin-left: auto; +} + +.station-preview-toggle i { + font-size: 0.76rem; + color: var(--primary-color); + line-height: 1; +} + +.station-preview-toggle:hover:not(:disabled) { + background: rgba(43, 51, 233, 0.06); + border-color: rgba(43, 51, 233, 0.45); + color: var(--primary-color); +} + +.station-preview-toggle:disabled { + color: var(--primary-color); + opacity: 0.45; + cursor: not-allowed; +} + +.station-preview-toggle:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +/* Cohort-retention strip above Close and the processed summary workspace. */ +.cohort-retention-strip { + display: flex; + align-items: center; + gap: 8px; + margin: 0 0 8px; + padding: 8px 14px; + font-size: 0.78rem; + font-weight: 600; + color: var(--text-main); + background: color-mix(in srgb, var(--primary-color) 5%, white); + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: var(--radius-md, 8px); +} + +.cohort-retention-strip i { + color: var(--primary-color); + font-size: 0.9rem; +} + +/* Batch-action menu in the preprocessing variable-browser header. */ +.batch-menu { + position: relative; + margin-left: auto; +} + +/* The Batch trigger is the app's studio pill (styles.css) with a rounder frame; the open + state comes from [aria-expanded], which .studio-export-btn already paints. */ +.batch-menu-trigger { + border-radius: 999px; + min-height: 26px; + height: 26px; +} + +.batch-menu-list { + position: absolute; + top: calc(100% + 6px); + right: 0; + z-index: 30; + min-width: 240px; + padding: 6px; + background: var(--card-bg, #ffffff); + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: var(--radius-md, 8px); + box-shadow: 0 8px 24px rgba(43, 51, 233, 0.15); + display: flex; + flex-direction: column; + gap: 2px; + pointer-events: none; +} + +.batch-menu-list button { + appearance: none; + border: 0; + background: transparent; + padding: 8px 10px; + border-radius: 6px; + font-family: inherit; + font-size: 0.8rem; + font-weight: 500; + color: var(--text-main); + text-align: left; + cursor: pointer; + pointer-events: auto; + white-space: normal; +} + +.batch-menu-list button:hover { + background: color-mix(in srgb, var(--primary-color) 6%, white); +} + +.batch-menu-list button:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: -2px; +} + +/* The overlay switch changes only the results pane, so it lives inside that pane + instead of above the card, where it read as a toolbar for the variable rail. + The column keeps it outside the pane's scroller, so it holds in place while a + long metrics table scrolls under it, and it owns the switch's spacing: padding + on .summary-tabs would override the .modal-tabs padding and stretch the pill's + own background and border past the labels. */ +.summary-detail-column { + display: flex; + flex-direction: column; + min-width: 0; + min-height: 0; +} + +.summary-detail-column .statistics-panel { + flex: 1 1 auto; +} + +/* Centered because it is the pane's own view control: the pane is much wider than + the pill, and hugging one gutter left it stranded above the heading. Centering + the pill itself, not a row, keeps its capsule sized to the labels. */ +.summary-tabs { + flex: 0 0 auto; + align-self: center; +} + +@media (max-width: 768px) { + .summary-detail-column { + padding-top: 12px; + } +} + +.empty-state, +.empty-state-block { + margin: 12px 0; + text-align: center; + font-size: 0.9rem; + color: var(--text-muted); + padding: 24px; + background: var(--studio-card-header-bg); + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + box-shadow: none; + border-radius: var(--radius-lg, 12px); + display: flex; + flex-direction: column; + align-items: center; + gap: 12px; + min-height: 160px; + justify-content: center; +} + +/* The icon is the only cue that a station is idle, so it must stay above 3:1. */ +.empty-state-block i { + font-size: 2.25rem; + color: var(--primary-color); + background: color-mix(in srgb, var(--primary-light) 18%, white 82%); + padding: 18px; + border-radius: 50%; + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + box-shadow: none; +} + +/* One notch below .station-card-title so the panel heading still leads. */ +.empty-state-block p { margin: 0; font-weight: 600; color: var(--text-main); } + +.empty-state-block span { font-size: 0.85rem; color: var(--text-muted); } + +/* Glassmorphic feedback cards */ +.glass-feedback { + margin: 0 0 12px; + padding: 14px 18px; + font-size: 0.82rem; + background: var(--card-bg); + border: 1px solid rgba(148, 163, 184, 0.28); + border-radius: var(--radius-lg, 12px); + box-shadow: 0 6px 20px rgba(15, 23, 42, 0.04); + color: var(--text-main); + animation: feedbackSlideIn 0.3s cubic-bezier(0.16, 1, 0.3, 1); +} + +.glass-feedback-warning { + background: rgba(255, 186, 8, 0.12); + border-color: rgba(255, 186, 8, 0.35); + color: #92400e; +} + +@keyframes feedbackSlideIn { + from { opacity: 0; transform: translateY(-8px); } + to { opacity: 1; transform: translateY(0); } +} + +/* The preview card reads as the informational feedback variant the same way its + siblings do - through its own fill and edge, not a side stripe. */ +.search-inline { + position: relative; + max-width: 280px; + width: 100%; +} + +.preprocessing-search, +.table-input, +.table-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; +} + +.preprocessing-search:focus-visible, +.table-input:focus-visible, +.table-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); +} + +/* Rail search: pill with a leading glyph, matching the tray/button vocabulary. */ +.search-inline .preprocessing-search { + border-radius: 999px; + padding-left: 30px; +} + +.search-inline .search-inline-icon { + position: absolute; + left: 11px; + top: 50%; + transform: translateY(-50%); + color: var(--text-muted); + font-size: 0.72rem; + pointer-events: none; +} + +/* Documentation: the algorithm configuration panel's disclosure, copied. A plain + label with a CSS chevron that turns, and the text carried by a left rule instead of + a card. The prefix stays because these styles are component-scoped, so this file + cannot reach the algorithm panel's classes. */ +.preprocessing-documentation-panel { + margin: 0 0 14px; + padding: 0 8px; +} + +/* "How this works" lives in the station header's action slot; the body is the + same documentation panel, toggled by the header link. */ +.preprocessing-doc-toggle { + display: inline-flex; + align-items: center; + gap: 6px; + padding: 0; + border: 0; + border-radius: var(--radius-sm); + background: transparent; + color: var(--text-muted); + font-family: inherit; + font-size: 0.76rem; + font-weight: 600; + white-space: nowrap; + cursor: pointer; +} + +.preprocessing-doc-toggle i { + color: var(--primary-color); + font-size: 0.72rem; +} + +.preprocessing-doc-toggle:hover { + color: var(--primary-color); +} + +.preprocessing-doc-toggle:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +.preprocessing-documentation-content { + width: 100%; + box-sizing: border-box; + margin-top: 0.5rem; + padding-left: 10px; + border-left: 2px solid color-mix(in srgb, var(--primary-color) 26%, white); + font-size: 0.82rem; + line-height: 1.45; + color: rgba(20, 22, 31, 0.72); + overflow-wrap: anywhere; +} + +.preprocessing-documentation-content .preprocessing-doc-intro, +.preprocessing-documentation-content .preprocessing-doc-paragraph { + margin: 0 0 10px; + font-size: inherit; + line-height: inherit; + color: inherit; +} + +.preprocessing-documentation-content .preprocessing-doc-paragraph:last-child, +.preprocessing-documentation-content .preprocessing-doc-intro:last-child { + margin-bottom: 0; +} + +.preprocessing-documentation-content .preprocessing-doc-section-title { + margin: 12px 0 6px; + font-size: inherit; + font-weight: 600; + line-height: inherit; + color: var(--text-main); +} + +.preprocessing-documentation-content .preprocessing-doc-section-title:first-child { + margin-top: 0; +} + +.preprocessing-documentation-content .preprocessing-doc-list { + display: grid; + gap: 3px; + margin: 0 0 10px; + padding-left: 1.15em; + list-style: disc; +} + +.preprocessing-documentation-content .preprocessing-doc-list:last-child { + margin-bottom: 0; +} + +.preprocessing-documentation-content .preprocessing-doc-list li { + display: list-item; + font-size: inherit; + line-height: inherit; + color: inherit; +} + +.preprocessing-documentation-content .preprocessing-doc-term { + font-weight: 600; + color: var(--text-main); +} + +.preprocessing-documentation-content .preprocessing-doc-desc { + font-size: inherit; + line-height: inherit; + color: inherit; +} + +.longitudinal-controls { + display: flex; + align-items: center; + justify-content: flex-start; + gap: 18px; + margin-bottom: 16px; + flex-wrap: wrap; +} + +.visit-controls, +.visit-controls label { + display: inline-flex; + align-items: center; + gap: 8px; +} + +.visit-controls { + gap: 16px; + flex-wrap: wrap; +} + +.visit-controls label { + color: var(--text-muted); + font-size: 0.84rem; + font-weight: 700; +} + +.visit-controls label span { + min-width: 48px; +} + +.longitudinal-error { + margin: -4px 0 10px; +} + +.outlier-preview-skeleton { + margin: 0; +} + +/* Preprocessing stations are ONE card, not two. styles.css owns the card, the + rail/panel split and the scroll contract; both summary panels use + .statistics-browser as a left rail, so the pill toolbar that used to live here + made this the only surface in the step that rendered the shared class + differently — the adjacent-panel test in DESIGN.md. This file owns the row + internals only. */ + +.preprocessing-browser .chart-search { + margin-top: 2px; +} + +.preprocessing-browser .preprocessing-search { + width: 100%; + min-width: 0; +} + +/* A 240px rail has no room for a count justified to the far edge. */ +.preprocessing-browser .statistics-browser-header { + justify-content: flex-start; + gap: 6px; +} + +.preprocessing-browser .preprocessing-variable-group { + gap: 2px; + margin-top: 12px; +} + +/* The group heading already names the state, so repeating it on every row said + the same thing up to 9 times per station; the state dot carries it instead. */ +.preprocessing-browser .preprocessing-state-mini { + display: none; +} + +.preprocessing-browser .empty-state-block { + margin-top: 8px; +} + +/* The shared .statistics-browser row owns layout, padding and selection. + Only the narrower rail's type size is a local concern. */ +.preprocessing-browser .statistics-variable-btn > span:first-of-type { + font-size: 0.8rem; +} + +/* The global narrow rule re-shows the state chip inside a column-direction + header, where it stretched into a full-width pill. */ +.preprocessing-detail-panel .preprocessing-state-chip { + width: fit-content; +} + +/* Two-pane card: the editor owns a column sized to its controls and the + evidence rail takes the rest of the card, bleeding to the edges so it reads + as one designed surface rather than a box floating inside a box. No border or + shadow here — the workspace is the card, and re-declaring them at this + specificity is what drew a second frame inside the first. */ +.preprocessing-detail-panel { + display: flex; + flex-direction: column; + gap: 16px; + padding: 16px 20px 18px; +} + +/* The winsorizer carries four controls, so it needs the wider editor column. */ +.outlier-preprocessing-workspace .preprocessing-detail-panel { + grid-template-columns: minmax(260px, 560px) minmax(0, 1fr); +} + +/* The header names the variable, so it owns the full card width; the editor and + the evidence rail sit side by side beneath it. */ +.preprocessing-detail-panel .statistics-panel-header { + grid-column: 1 / -1; + min-width: 0; +} + +.preprocessing-detail-panel .preprocessing-detail-section { + grid-column: 1; + min-width: 0; +} + +.preprocessing-detail-panel > .empty-state-block { + grid-column: 1 / -1; +} + +.preprocessing-detail-panel h4 { + margin: 2px 0 0; + font-size: 1.1rem; + font-weight: 700; + display: flex; + align-items: center; + gap: 8px; + letter-spacing: -0.01em; + line-height: 1.25; + text-wrap: balance; +} + +.preprocessing-header-coverage { + color: var(--text-muted); + font-size: 0.76rem; + font-variant-numeric: tabular-nums; + white-space: nowrap; +} + +/* The evidence now sits under the choices as one flat band, not a second pane. */ +.preprocessing-evidence { + display: flex; + flex-wrap: wrap; + align-items: flex-start; + gap: 12px 28px; + margin: 0; + padding: 12px 0 0; + border: 0; + border-top: 1px solid var(--studio-card-header-border); + background: none; + font-size: 0.78rem; + color: var(--text-muted); +} + +.preprocessing-evidence-group { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 8px; + min-width: 0; +} + +.preprocessing-evidence-title { + margin: 0; + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; +} + +.preprocessing-stat-grid { + display: grid; + gap: 8px 14px; + margin: 0; +} + +.preprocessing-stat-strip { + display: contents; +} + +.preprocessing-stat-grid { + grid-template-columns: repeat(3, minmax(0, 1fr)); +} + +/* Flex-wrap, not a fixed column count: these are real measured values and a + MAX of 47334358.00 is far wider than a MEDIAN of 155.00. A fixed 5-column + grid clipped the widest number against the card edge; wrapping moves the + whole stat to the next line so a figure is never partially shown. */ +.preprocessing-stat-strip { + display: flex; + flex-wrap: wrap; + gap: 10px 22px; +} + +.preprocessing-stat-strip .preprocessing-stat { + flex: 0 0 auto; +} + +.preprocessing-stat { + display: inline-flex; + gap: 4px; + min-width: 0; + align-items: baseline; +} + +.preprocessing-stat dt { + color: var(--text-muted); + font-size: inherit; + font-weight: 400; + letter-spacing: normal; + text-transform: lowercase; +} + +.preprocessing-stat dd { + margin: 0; + color: var(--text-main); + font-size: inherit; + font-weight: 700; + font-variant-numeric: tabular-nums; + white-space: nowrap; +} + +/* Filled coverage meter — an outline-only gauge would not encode the value. + --success is only 2.07:1 against this track, so a 0%-missing row renders an + empty track (which is the correct reading) rather than a pale green nub. */ +.preprocessing-meter { + display: block; + height: 6px; + border-radius: 999px; + background: color-mix(in srgb, var(--text-muted) 14%, white); + overflow: hidden; +} + +.preprocessing-meter-fill { + display: block; + height: 100%; + border-radius: 999px; +} + +/* Keep a non-zero share visible even when it is tiny. */ +.preprocessing-evidence[data-severity='warning'] .preprocessing-meter-fill, +.preprocessing-evidence[data-severity='high'] .preprocessing-meter-fill { + min-width: 6px; +} + +.preprocessing-evidence[data-severity='warning'] .preprocessing-meter-fill { + /* --accent-color alone is 1.9:1 on the track; deepened with the brand's own + filter brown so the fill clears 3:1 without inventing a new hex. */ + background: color-mix(in srgb, var(--accent-color) 42%, var(--filter-color)); +} + +.preprocessing-evidence[data-severity='high'] .preprocessing-meter-fill { + background: var(--danger); +} + +.preprocessing-consequence { + margin: 0; + color: var(--text-main); + font-size: 0.8125rem; + line-height: 1.5; + text-wrap: pretty; +} + +.preprocessing-evidence-group { + display: grid; + grid-template-columns: minmax(0, 1fr); + align-content: start; + gap: 8px; + min-width: 0; +} + +.preprocessing-evidence-wide { + grid-column: 1 / -1; +} + +.preprocessing-frequency-list { + display: grid; + gap: 6px; + margin: 0; + padding: 0; + list-style: none; +} + +.preprocessing-frequency { + display: grid; + grid-template-columns: minmax(0, 1fr) minmax(48px, 2fr) auto; + align-items: center; + gap: 10px; +} + +.preprocessing-frequency-label { + color: var(--text-main); + font-size: 0.8125rem; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.preprocessing-frequency-track { + height: 6px; + border-radius: 999px; + background: color-mix(in srgb, var(--text-muted) 14%, white); + overflow: hidden; +} + +.preprocessing-frequency-bar { + display: block; + height: 100%; + min-width: 4px; + border-radius: 999px; + background: var(--primary-color); +} + +.preprocessing-frequency-value { + color: var(--text-main); + font-size: 0.8125rem; + font-weight: 600; + font-variant-numeric: tabular-nums; +} + +/* Below the two columns the rail can no longer sit beside the editor; it becomes + a banded footer of the same card instead of a detached box. */ +@media (max-width: 900px) { + .preprocessing-detail-panel, + .outlier-preprocessing-workspace .preprocessing-detail-panel { + grid-template-columns: minmax(0, 1fr); + } + + .preprocessing-detail-panel .statistics-panel-header, + .preprocessing-detail-panel .preprocessing-detail-section, + .preprocessing-detail-panel .preprocessing-evidence { + grid-column: 1; + grid-row: auto; + } + + .preprocessing-evidence { + grid-template-columns: minmax(0, 1fr); + margin: 0 -16px -14px; + border-top: 1px solid var(--studio-card-header-border); + border-left: 0; + } + + .preprocessing-stat-grid { + grid-template-columns: repeat(3, minmax(0, 1fr)); + } +} + + +/* The boxed row and its hover/active border are gone: the shared rail row is + flat with a leading accent edge, so a per-surface border here was the only + thing still making this rail look different from the summary rails. */ + +/* The per-pill state chip was removed because it repeated its own group heading + up to 9 times. The state still has to scan at a glance, so it moves to a dot + that inherits from the group. Colours are deepened toward --text-main because + --success (2.5:1) and --accent-color (1.9:1) both fail the 3:1 graphic floor. */ +.preprocessing-variable-btn::before { + content: ''; + flex: 0 0 auto; + width: 7px; + height: 7px; + border-radius: 50%; + background: transparent; + /* 55% hit 2.59:1 against the pill; 75% is 4.2:1 and matches the toggle track border. */ + box-shadow: inset 0 0 0 1.5px color-mix(in srgb, var(--text-muted) 75%, transparent); +} + +.preprocessing-variable-group[data-state='applied'] .preprocessing-variable-btn::before { + background: color-mix(in srgb, var(--success) 55%, var(--text-main)); + box-shadow: none; +} + +.preprocessing-variable-group[data-state='pending'] .preprocessing-variable-btn::before { + background: color-mix(in srgb, var(--accent-color) 42%, var(--filter-color)); + box-shadow: none; +} + +.preprocessing-variable-group[data-state='default'] .preprocessing-variable-btn::before { + background: var(--primary-color); + box-shadow: none; +} + +.preprocessing-variable-btn { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + row-gap: 1px; + column-gap: 8px; +} + +.preprocessing-variable-btn::before { + display: none; +} + +.preprocessing-variable-btn .statistics-variable-meta { + align-items: flex-start; + gap: 6px; +} + +.prep-rail-action { + grid-column: 1 / -1; + display: flex; + align-items: center; + gap: 6px; + font-size: 0.72rem; + font-weight: 600; + color: var(--text-muted); +} + +.prep-rail-action::before { + content: ''; + width: 6px; + height: 6px; + border-radius: 999px; + background: var(--primary-color); + flex-shrink: 0; +} + +.prep-rail-action[data-state='pending'] { + color: #78350f; +} + +.prep-rail-action[data-state='pending']::before { + background: color-mix(in srgb, var(--accent-color) 42%, var(--filter-color)); +} +.preprocessing-detail-section { + padding: 0; + border: 0; + background: transparent; + box-shadow: none; +} + +/* Missing-values choice cards (replaces the action select). */ +/* 2×2 so each card's effect sentence stays readable. */ +.missing-choice-grid { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 8px; + margin: 0; + padding: 0; + border: 0; +} + +.missing-choice-grid legend { + grid-column: 1 / -1; + margin-bottom: 6px; + padding: 0; +} + +.missing-choice { + position: relative; + display: grid; + gap: 4px; + padding: 10px 12px 10px 34px; + border-radius: var(--radius-md); + background: var(--card-bg); + box-shadow: inset 0 0 0 1px var(--studio-card-border); + cursor: pointer; +} + +.missing-choice:hover { + background: #f7f8fe; +} + +.missing-choice.is-selected { + background: color-mix(in srgb, var(--primary-color) 4%, white); + box-shadow: inset 0 0 0 1.5px var(--primary-color); +} + +.missing-choice.is-disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.missing-choice input[type='radio'] { + position: absolute; + left: 12px; + top: 13px; +} + +.missing-choice-label { + font-size: 0.84rem; + font-weight: 600; + color: var(--text-main); +} + +.missing-choice-effect { + font-size: 0.74rem; + line-height: 1.4; + color: var(--text-muted); + text-wrap: pretty; +} + +/* Per-dataset coverage tiles. */ +.coverage-tiles { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); + gap: 10px; + width: 100%; +} + +.coverage-tile { + display: grid; + gap: 6px; + padding: 10px 12px; + border-radius: var(--radius-md); + background: var(--bg-neutral); +} + +.coverage-tile-head { + display: flex; + justify-content: space-between; + gap: 8px; + font-size: 0.76rem; +} + +.coverage-tile-head strong { + font-weight: 700; +} + +.coverage-tile-head span { + color: var(--text-muted); +} + +/* Mini variant of the studio switch, used in the outlier rail rows. */ +.toggle-switch.is-mini { + width: 30px; + height: 18px; +} + +.toggle-switch.is-mini .toggle-slider:before { + width: 14px; + height: 14px; + left: 1px; + top: 1px; +} + +.toggle-switch.is-mini input:checked + .toggle-slider:before { + transform: translateX(12px); +} + +/* Outlier bounds strip. Pooled over all datasets; the report below is per dataset. */ +.bounds-strip { + display: grid; + gap: 8px; + padding: 14px 16px 10px; + border-radius: var(--radius-md); + background: var(--bg-neutral); +} + +.bounds-labels, +.bounds-scale { + position: relative; + height: 18px; + font-size: 0.72rem; + color: var(--text-muted); +} + +.bounds-labels span { + position: absolute; + top: 0; + font-weight: 700; + color: var(--text-main); + white-space: nowrap; +} + +.bounds-track { + position: relative; + height: 44px; +} + +.bounds-track::before { + content: ''; + position: absolute; + inset: 20px 0 auto; + height: 4px; + border-radius: 999px; + background: #e5e7eb; +} + +.bounds-iqr { + position: absolute; + top: 12px; + height: 20px; + border-radius: var(--radius-sm); + background: rgba(43, 51, 233, 0.14); + box-shadow: inset 0 0 0 1px rgba(43, 51, 233, 0.35); +} + +.bounds-median { + position: absolute; + top: 12px; + width: 2px; + height: 20px; + background: var(--primary-color); +} + +.bounds-capped { + position: absolute; + top: 20px; + height: 4px; + background: color-mix(in srgb, var(--danger) 75%, var(--text-main)); +} + +.bounds-marker { + position: absolute; + top: 4px; + width: 2px; + height: 36px; + background: var(--text-main); +} + +.bounds-scale span { + position: absolute; + top: 0; + transform: translateX(-50%); + white-space: nowrap; +} + +.bounds-scale .is-min { + left: 0; + transform: none; +} + +.bounds-scale .is-max { + right: 0; + left: auto; + transform: none; +} + +.outlier-report-section { + display: grid; + gap: 10px; + margin-top: 4px; +} + +.outlier-report-header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; +} + +.outlier-report-table .share-cell { + display: flex; + align-items: center; + justify-content: flex-end; + gap: 8px; +} + +.share-bar { + display: inline-block; + width: 74px; + height: 4px; + border-radius: 999px; + background: rgba(43, 51, 233, 0.08); + overflow: hidden; +} + +.share-bar > span { + display: block; + height: 100%; + background: color-mix(in srgb, var(--danger) 75%, var(--text-main)); +} + +.preprocessing-detail-heading { + display: flex; + align-items: center; + gap: 8px; + margin-bottom: 10px; + color: var(--text-muted); + font-size: 0.78rem; + font-weight: 700; + letter-spacing: 0.04em; + text-transform: uppercase; +} + +.preprocessing-detail-heading i { + font-size: 0.82rem; + opacity: 0.68; +} + +.preprocessing-control-grid { + display: grid; + gap: 12px 18px; + /* auto-fit + 1fr gave a 200px select two 602px tracks. Tracks stop at 300px + and the row stops at its content instead of spanning the panel. */ + grid-template-columns: repeat(auto-fit, minmax(200px, 300px)); + justify-content: start; +} + +.outlier-control-grid { + display: flex; + flex-wrap: wrap; + align-items: flex-end; + gap: 14px 22px; +} + +.preprocessing-control > span { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; +} + +.outlier-fold-input { + width: 84px; +} + +/* Four tracks need ~420px; below that the Fold control was clipped out of the + panel entirely (no page overflow, so it failed silently). Declared after the + base rule so it actually wins. */ +@media (max-width: 720px) { + .outlier-control-grid { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } +} + +.preprocessing-control { + display: grid; + gap: 6px; + color: #1e293b; + font-size: 0.78rem; + font-weight: 700; + letter-spacing: normal; + text-transform: none; +} diff --git a/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.results.css b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.results.css new file mode 100644 index 000000000..e86a620c4 --- /dev/null +++ b/src/app/pages/experiment-studio/statistic-analysis-panel/statistic-analysis-panel.results.css @@ -0,0 +1,875 @@ +/* Shared controls and result surfaces of the Data review step: the toggle + switch, the federated-data note, the descriptive/data tables, the statistics + browser, the chart-export-only containers, the transformation station and the + responsive overrides for all three files. + Second split of statistic-analysis-panel.component.css (see + statistic-analysis-panel.preprocessing.css for the reason and the cascade + order). This file is LAST in the styleUrl array, which is why the responsive + overrides live here. */ + +.toggle-switch { + position: relative; + display: inline-block; + width: 44px; + height: 24px; +} +.toggle-switch input { + opacity: 0; + width: 0; + height: 0; +} +/* The input is invisible and zero-sized, and styles.css excludes checkboxes from + the studio focus rule, so nothing else can draw the ring. Put it on the slider + (the element the user actually sees) with a solid token-coloured outline. */ +.toggle-switch input:focus-visible + .toggle-slider { + outline: 2px solid var(--primary-color); + outline-offset: 2px; +} +.toggle-slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + /* OFF must read as a control and as "not on": the old near-white fill hit 1.2:1. */ + background: color-mix(in srgb, var(--text-muted) 22%, white); + transition: + background 0.22s ease, + border-color 0.22s ease, + box-shadow 0.22s ease; + border-radius: 999px; + border: 1px solid color-mix(in srgb, var(--text-muted) 75%, transparent); +} +.toggle-slider:before { + content: ""; + position: absolute; + height: 18px; + width: 18px; + left: 3px; + top: 2.5px; + background: var(--card-bg); + border-radius: 50%; + transition: + transform 0.22s var(--ease-standard), + box-shadow 0.22s ease; + /* Ring keeps the knob legible on both the OFF and ON track. */ + box-shadow: 0 2px 6px rgba(15, 23, 42, 0.18), inset 0 0 0 1px rgba(15, 23, 42, 0.12); +} +.toggle-switch input:checked + .toggle-slider { + /* Flat primary: every other studio gradient is a near-white surface wash, and + a two-stop brand ramp is invisible on a 44x24 track anyway. */ + background: var(--primary-color); + border-color: rgba(43,51,233,0.24); + box-shadow: 0 5px 14px color-mix(in srgb, var(--primary-color) 22%, transparent); +} +.toggle-switch input:checked + .toggle-slider:before { + transform: translateX(20px); +} + +.row-error { + margin-top: 5px; + color: #b91c1c; + font-size: 0.75rem; + font-weight: 600; + text-align: left; +} + +.table-input:disabled, +.table-select:disabled, +button:disabled { + opacity: 0.55; + cursor: not-allowed; + pointer-events: none; +} + +.table-input:disabled, +.table-select:disabled { + background: color-mix(in srgb, var(--text-muted) 6%, white 94%); + border-color: color-mix(in srgb, var(--text-muted) 12%, transparent); + color: color-mix(in srgb, var(--text-muted) 74%, white 26%); +} + + +/* A quiet callout: secondary text with an accent edge, the same edge the + statistics table uses for its section bands. */ +.federated-info-note { + display: flex; + align-items: flex-start; + gap: 10px; + margin-bottom: 12px; + padding: 9px 14px 9px 12px; + background: color-mix(in srgb, var(--primary-color) 4%, var(--card-bg)); + border: 1px solid color-mix(in srgb, var(--primary-color) 12%, transparent); + border-left: 3px solid var(--primary-color); + border-radius: var(--radius-md, 8px); + color: var(--text-muted); + font-size: 0.82rem; + line-height: 1.45; +} + +.federated-info-note strong { + color: var(--text-main); + font-weight: 600; +} + +/* Icon height matches one line of text, so it centres on the first line + when the note wraps instead of floating between the two. */ +.federated-info-note i { + flex: 0 0 auto; + color: var(--primary-color); + font-size: 0.95rem; + line-height: 1.45; +} + +.table-wrapper { + padding: 10px 12px 12px; +} + +.data-table { + width: 100%; + border-collapse: separate; + border-spacing: 0; + background: var(--card-bg); + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: 8px; + overflow: hidden; +} + +.data-table th { + position: sticky; + top: 0; + z-index: 3; + background: var(--studio-card-header-bg); + color: var(--text-muted); + text-align: center; + padding: 9px 14px; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; + border-bottom: 1px solid var(--studio-card-header-border); + box-shadow: none; +} + +.data-table td { + text-align: center; + padding: 9px 14px; + font-size: 0.85rem; + color: var(--text-main); + border-bottom: 1px solid color-mix(in srgb, var(--primary-color) 6%, transparent); + font-variant-numeric: tabular-nums; +} + +.data-table tbody tr:last-child td { + border-bottom: 0; +} + +.data-table tbody tr:hover { + background-color: rgba(43, 51, 233, 0.03); + transition: background-color 0.2s ease; +} + +.data-table td:first-child, +.metric-cell { + font-weight: 600; + color: var(--text-muted); + text-align: left; + padding-left: 16px; +} + +.statistics-table th:not(:first-child), +.statistics-table td:not(:first-child) { + text-align: right; +} + +.statistics-table td:not(:first-child) { + color: var(--text-main); + /* Twelve rows of semibold digits emphasise nothing. Values read at 400 so the + pooled column and the metric labels are what carry weight. */ + font-weight: 400; +} + +/* The rollup is the column a reader is scanning for; it keeps the emphasis the + per-dataset columns just gave up. :not() keeps it above the rule above. */ +.statistics-table td.statistics-col-rollup:not(:first-child) { + font-weight: 700; +} + +/* Targets .metric-cell, not td:first-child: the group heading row is a + colspan first child too, and a :first-child rule outranks the group's own + 700 and flattens COVERAGE / DISTRIBUTION into the row labels. */ +.statistics-table td.metric-cell { + font-weight: 500; +} + +/* The global rule for this row loses to .data-table td:first-child (the group + row is a colspan first child), so 700 was declared here and never applied. + Declared in this file so it actually wins. */ +.statistics-table tr.statistics-metric-group td { + font-weight: 700; +} + +.statistics-table-wrapper.statistics-table-wrapper--numeric { + max-height: none; + overflow-x: auto; + overflow-y: visible; +} + +.statistics-table-wrapper--numeric .statistics-table th { + padding: 7px 12px; + line-height: 1.2; +} + +.statistics-table-wrapper--numeric .statistics-table td { + padding: 9px 12px; + line-height: 1.25; +} + +.statistics-table-wrapper--numeric .statistics-table td:first-child, +.statistics-table-wrapper--numeric .metric-cell { + padding-left: 14px; + white-space: nowrap; +} + +.statistics-table-wrapper--numeric .metric-cell:hover { + padding-left: 11px; +} + +/* Rules below are prefixed with .statistics-table-wrapper so they outrank the + --numeric overrides above: emulated encapsulation adds one attribute per + compound selector, so a shorter selector loses regardless of order. */ + +/* The wrapper owns the card edge; the .data-table frame drew a second box inside it. */ +.statistics-table-wrapper { + padding: 0; +} + +.statistics-table-wrapper .statistics-table { + border: 0; + border-radius: 0; +} + +/* Explicit tints, not --studio-card-header-bg: the studio sets that token to + white, which flattened the header and section rows into the body. */ +.statistics-table-wrapper .statistics-table th { + padding-top: 10px; + padding-bottom: 10px; + background: color-mix(in srgb, var(--primary-color) 5%, var(--card-bg)); + border-bottom: 1px solid color-mix(in srgb, var(--primary-color) 14%, transparent); + color: var(--primary-dark); + letter-spacing: 0.06em; +} + +.statistics-table-wrapper .statistics-table th:first-child { + text-align: left; + padding-left: 16px; + color: var(--text-muted); +} + +/* Section bands: tinted, with an accent edge, so COVERAGE / DISTRIBUTION read + as headings and not as two more rows. */ +.statistics-table-wrapper .statistics-table tr.statistics-metric-group td { + padding: 7px 16px; + background: color-mix(in srgb, var(--primary-color) 3%, var(--card-bg)); + border-top: 1px solid color-mix(in srgb, var(--primary-color) 10%, transparent); + border-bottom: 1px solid color-mix(in srgb, var(--primary-color) 10%, transparent); + box-shadow: inset 3px 0 0 var(--primary-color); + color: var(--primary-color); + font-size: 0.68rem; + font-weight: 700; + letter-spacing: 0.08em; + text-transform: uppercase; +} + +.statistics-table-wrapper .statistics-table tbody tr.statistics-metric-group:first-child td { + border-top: 0; +} + +.statistics-table-wrapper .statistics-table td.metric-cell { + padding-left: 16px; + color: var(--text-main); +} + +/* Faint banding keeps a row's value lined up with its label across wide tables. */ +.statistics-table-wrapper .statistics-table tbody tr:not(.statistics-metric-group):nth-child(even) td { + background: var(--bg-neutral); +} + +/* The rollup column is a band of its own: tinted top to bottom, blue edge. */ +.statistics-table-wrapper .statistics-table th.statistics-col-rollup { + background: color-mix(in srgb, var(--primary-color) 10%, var(--card-bg)); + border-left: 2px solid color-mix(in srgb, var(--primary-color) 30%, var(--card-bg)); +} + +.statistics-table-wrapper .statistics-table td.statistics-col-rollup, +.statistics-table-wrapper .statistics-table tbody tr:not(.statistics-metric-group):nth-child(even) td.statistics-col-rollup { + background: color-mix(in srgb, var(--primary-color) 5%, var(--card-bg)); + border-left: 2px solid color-mix(in srgb, var(--primary-color) 30%, var(--card-bg)); + color: var(--primary-dark); +} + +/* Cells carry their own background (sticky first column, banding, rollup + tint), so the hover tint goes on the cells or it only shows in some columns. */ +.statistics-table-wrapper .statistics-table tbody tr:not(.statistics-metric-group):hover td { + background: color-mix(in srgb, var(--primary-color) 6%, var(--card-bg)); +} + +.statistics-table-wrapper .statistics-table tbody tr:not(.statistics-metric-group):hover td.statistics-col-rollup { + background: color-mix(in srgb, var(--primary-color) 10%, var(--card-bg)); +} + +.statistics-table-wrapper .statistics-cell-note { + display: inline-block; + margin-left: 6px; + padding: 1px 6px; + border-radius: 999px; + background: color-mix(in srgb, var(--primary-color) 7%, transparent); + color: var(--text-muted); + font-size: 0.68rem; + font-weight: 600; + vertical-align: 1px; +} + +.chart-container { + width: 100%; + max-width: none; + min-height: 360px; + background: rgba(255, 255, 255, 0.6); + border: 1px solid var(--studio-card-border, rgba(43, 51, 233, 0.1)); + border-radius: 8px; + padding: 10px 12px; + box-shadow: 0 4px 24px rgba(15, 23, 42, 0.04); +} + +.hidden-charts-for-export { + position: fixed; + top: -9999px; + left: -9999px; + width: 900px; + height: auto; + opacity: 1; + pointer-events: none; + background: var(--card-bg); +} + +.hidden-charts-for-export app-chart-renderer { + width: 800px; + height: 500px; + display: block; +} + +.pdf-overlay { + position: absolute; + inset: 0; + background: rgba(255, 255, 255, 0.8); + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + z-index: 999; + font-size: 1.2rem; + color: #333; + font-weight: 500; +} + + +@media (max-width: 768px) { + .modal-content { + border-radius: var(--radius-lg, 12px); + } + + .workflow-section-body { + padding: 12px; + } + + + .search-inline { + max-width: none; + } + + .longitudinal-controls { + flex-direction: column; + align-items: stretch; + } + + .visit-controls { + flex-direction: column; + gap: 8px; + } + + .data-table th, + .data-table td { + padding: 6px 8px; + font-size: 12px; + } + +} + +/* ===== TRANSFORMATION: CREATE CATEGORICAL COLUMN ===== + Chrome comes from app-station-card / app-station-list-row / app-station-action-bar; + only the field and table styling is local. */ +.transformation-create { + display: flex; + flex-direction: column; + gap: 16px; +} + +/* One numbered step of the column editor (1 name · 2 categories · 3 preview/apply). */ +.column-step { + display: grid; + gap: 8px; + min-width: 0; +} + +.column-step + .column-step { + padding-top: 16px; + border-top: 1px solid color-mix(in srgb, var(--primary-color) 6%, transparent); +} + +.column-step-head { + display: flex; + align-items: center; + gap: 8px; + color: var(--text-main); + font-size: 0.84rem; + font-weight: 700; +} + +.column-step-number { + display: inline-flex; + align-items: center; + justify-content: center; + flex: none; + width: 18px; + height: 18px; + border-radius: 50%; + background: var(--primary-color); + color: #ffffff; + font-size: 0.68rem; +} + +.column-step-meta { + margin-left: auto; + color: var(--text-muted); + font-size: 0.74rem; + font-weight: 500; +} + +.transformation-name-row { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 10px; +} + +.transformation-code-input { + width: 260px; + max-width: 100%; + box-sizing: border-box; + min-height: 38px; + border: 1px solid rgba(43, 51, 233, 0.15); + border-radius: var(--radius-md, 8px); + background: rgba(255, 255, 255, 0.9); + color: var(--text-main); + padding: 6px 10px; + font-family: var(--font-mono); + font-size: 0.84rem; +} + +.transformation-hint { + color: var(--text-muted); + font-size: 0.72rem; +} + +.transformation-hint-error { + color: var(--danger-color, #c0392b); + font-size: 0.72rem; +} + +.transformation-rules { + display: grid; + gap: 8px; +} + +.transformation-rule-head, +.transformation-rule-row { + display: grid; + grid-template-columns: 36px 200px minmax(0, 1fr) 110px 36px; + gap: 10px; + align-items: center; + padding: 8px 12px; +} + +.transformation-rule-head { + background: var(--studio-card-header-bg); + border-radius: var(--radius-md) var(--radius-md) 0 0; + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; +} + +.transformation-rules-list { + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-md); + overflow: hidden; +} + +.transformation-rule-row { + border-top: 1px solid color-mix(in srgb, var(--primary-color) 6%, transparent); +} + +.transformation-rules-list .transformation-rule-row:first-child { + border-top: 0; +} + +.transformation-rule-number { + display: inline-flex; + align-items: center; + justify-content: center; + width: 22px; + height: 22px; + border-radius: 999px; + box-shadow: inset 0 0 0 1px var(--studio-card-border); + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 600; +} + +.transformation-rule-value.input { + width: 100%; + min-height: 34px; + box-sizing: border-box; +} + +.transformation-rule-summary { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 8px; + min-width: 0; + color: var(--text-main); + font-size: 0.8rem; +} + +.transformation-rule-edit-link { + display: inline-flex; + align-items: center; + gap: 5px; + height: 24px; + padding: 0 10px; + border: 0; + border-radius: 999px; + background: transparent; + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 30%, transparent); + color: var(--primary-color); + font: inherit; + font-size: 0.74rem; + font-weight: 600; + cursor: pointer; +} + +.transformation-rule-edit-link:hover { + background: color-mix(in srgb, var(--primary-color) 6%, white); +} + +.transformation-rule-edit-link i { + font-size: 0.62rem; +} + +.transformation-rule-record-count { + color: var(--text-muted); + font-size: 0.78rem; + font-variant-numeric: tabular-nums; + text-align: right; +} + +.transformation-rule-remove { + width: 28px; + height: 28px; + display: inline-flex; + align-items: center; + justify-content: center; + border: 0; + border-radius: 999px; + background: transparent; + color: var(--text-muted); + cursor: pointer; +} + +.transformation-rule-remove:hover { + background: color-mix(in srgb, var(--danger) 8%, white); + color: #991b1b; +} + +.transformation-rule-editor { + grid-column: 1 / -1; + display: none; + padding: 10px 0 4px 46px; +} + +.transformation-rule-editor.is-open { + display: block; +} + +/* Focus mode: while one rule is edited, the rest of the table folds away (display only — + every builder stays mounted) and the list wears a primary ring, so the open rule is the + one thing on screen. Done restores the collapsed list. */ +.transformation-rules.is-focused .transformation-rule-head, +.transformation-rules.is-focused .transformation-rule-row:not(.is-editing), +.transformation-rules.is-focused .transformation-add-category, +.transformation-rules.is-focused .transformation-rules-foot { + display: none; +} + +.transformation-rules.is-focused .transformation-rules-list { + border-color: color-mix(in srgb, var(--primary-color) 35%, transparent); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-color) 8%, transparent); +} + +.transformation-rule-row.is-editing { + border-top: 0; + background: color-mix(in srgb, var(--primary-color) 3%, white); +} + +.transformation-rule-row.is-editing .transformation-rule-number { + background: var(--primary-color); + box-shadow: none; + color: #ffffff; +} + +.transformation-rule-sentence.is-empty { + color: var(--text-muted); +} + +.transformation-rule-editor-lead { + margin: 0 0 10px; + color: var(--text-muted); + font-size: 0.78rem; +} + +.transformation-rule-editor-lead strong { + color: var(--text-main); +} + +.transformation-rule-editor-foot { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 8px 12px; + margin-top: 12px; + padding-top: 12px; + border-top: 1px solid color-mix(in srgb, var(--primary-color) 8%, transparent); + color: var(--text-muted); + font-size: 0.74rem; +} + +.transformation-rule-done { + display: inline-flex; + align-items: center; + gap: 6px; + height: 32px; + padding: 0 16px; + border: 0; + border-radius: 999px; + background: var(--primary-color); + color: #ffffff; + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + cursor: pointer; +} + +.transformation-rule-fallback { + background: var(--bg-neutral); +} + +.transformation-rule-fallback .transformation-rule-value { + border-style: dashed; + color: var(--text-muted); +} + +/* Ghost row inside the rules table, above the fallback: where the next category goes. */ +.transformation-add-category { + display: flex; + align-items: center; + gap: 10px; + width: 100%; + padding: 10px 12px 10px 20px; + border: 0; + border-top: 1px dashed color-mix(in srgb, var(--primary-color) 25%, transparent); + background: transparent; + color: var(--primary-color); + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + text-align: left; + cursor: pointer; +} + +.transformation-rules-list .transformation-add-category:first-child { + border-top: 0; +} + +.transformation-add-category:hover { + background: color-mix(in srgb, var(--primary-color) 5%, white); +} + +.transformation-add-category > i { + font-size: 0.72rem; +} + +/* The K-means card previewing its counts hides its report without destroying it. */ +app-kmeans-cluster-source.is-previewing { + display: none; +} + +/* Derived columns as a vertical graph: cards joined by the pipeline connector, ending + in the type chooser. The connector sits shorter here than between stages. */ +.transformation-graph > .pipeline-connector { + height: 28px; +} + +.transformation-rules-foot { + margin: 0; + color: var(--text-muted); + font-size: 0.74rem; +} + +.transformation-code-input:focus-visible { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-color) 18%, transparent); +} + +.transformation-rule-value { + width: 100%; + box-sizing: border-box; + min-height: 38px; + border: 1px solid rgba(43, 51, 233, 0.15); + border-radius: var(--radius-md, 8px); + background: rgba(255, 255, 255, 0.9); + color: var(--text-main); + padding: 6px 10px; + font-family: inherit; + font-size: 0.84rem; +} + +.transformation-rule-value:focus-visible { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-color) 18%, transparent); +} + +/* Statistics preview: the card owns the frame, the table owns the rows. + A K-means column reuses the categorical rule rows instead of that table. */ +.transformation-statistics { + display: grid; + gap: 12px; +} + +.transformation-count-chart { + width: 100%; + background: rgba(255, 255, 255, 0.6); + border: 1px solid rgba(43, 51, 233, 0.1); + border-radius: 8px; + padding: 8px 12px 4px; + box-sizing: border-box; +} + +.transformation-stats-title { + margin: 0; + color: var(--text-main); + font-size: 0.95rem; + font-weight: 600; +} + +.transformation-stats-lead { + margin: 0; + color: var(--text-muted); + font-size: 0.82rem; +} + +.transformation-rule-value--fixed { + display: flex; + align-items: center; + min-height: 34px; + border-color: transparent; + background: transparent; + padding-left: 0; + font-weight: 600; +} + +.transformation-stats-table { + width: 100%; + border-collapse: collapse; + background: rgba(255, 255, 255, 0.6); + border: 1px solid rgba(43, 51, 233, 0.1); + border-radius: 8px; + overflow: hidden; +} + +.transformation-stats-table th, +.transformation-stats-table td { + text-align: left; + padding: 9px 12px; + font-size: 0.82rem; + border-bottom: 1px solid var(--border-color, rgba(43, 51, 233, 0.1)); + font-variant-numeric: tabular-nums; +} + +.transformation-stats-table th { + font-weight: 700; + color: var(--text-main); + background: rgba(248, 250, 252, 0.9); +} + +.transformation-stats-table tr:last-child td { + border-bottom: none; +} + +.transformation-stats-table td { + color: var(--chart-label-color); +} + +@media (max-width: 900px) { + .transformation-rule-head, + .transformation-rule-row { + grid-template-columns: 36px minmax(0, 1fr) 90px 36px; + grid-template-areas: + 'number value count remove' + 'summary summary count remove'; + row-gap: 4px; + } + + .transformation-rule-head { + display: none; + } + + .transformation-rule-number { + grid-area: number; + } + + .transformation-rule-value { + grid-area: value; + } + + .transformation-rule-summary { + grid-area: summary; + } + + .transformation-rule-record-count { + grid-area: count; + } + + .transformation-rule-remove { + grid-area: remove; + } + + .transformation-rule-editor { + grid-column: 1 / -1; + padding-left: 0; + } +} diff --git a/src/app/pages/experiment-studio/stepper/studio-stepper.component.css b/src/app/pages/experiment-studio/stepper/studio-stepper.component.css new file mode 100644 index 000000000..76ee1d65a --- /dev/null +++ b/src/app/pages/experiment-studio/stepper/studio-stepper.component.css @@ -0,0 +1,159 @@ +:host { + display: flex; + align-items: center; + min-width: 0; + flex: 1; + height: 100%; +} + +.studio-stepper-header { + display: flex; + align-items: center; + justify-content: center; + width: 100%; + min-width: 0; +} + +/* Steps track */ +.stepper-track { + display: flex; + align-items: center; + list-style: none; + margin: 0; + padding: 0; + gap: 8px; + flex: 1; + justify-content: center; + min-width: 0; +} + +.stepper-track-item { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +} + +.stepper-connector { + width: 24px; + height: 2px; + background-color: var(--border-color, #e5e7eb); + /* First thing to give when the four step labels and the CTA compete for the bar. */ + flex: 0 1 24px; + min-width: 6px; + transition: background-color 0.2s ease; +} + +.stepper-connector.is-complete { + background-color: var(--primary-color); +} + +/* Parent step button */ +.stepper-step-btn { + display: inline-flex; + align-items: center; + gap: 8px; + min-width: 0; + background: transparent; + border: none; + padding: 4px 8px; + border-radius: var(--radius-md, 6px); + cursor: pointer; + color: var(--text-muted); + font-family: inherit; + font-size: 0.875rem; + font-weight: 500; + transition: all 0.15s ease; + white-space: nowrap; +} + +/* Last resort after the connectors have shrunk: clip rather than sit under the CTA. */ +.stepper-step-label { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; +} + +.stepper-step-btn:hover:not(:disabled) { + color: var(--text-main); + background-color: color-mix(in srgb, var(--primary-light) 25%, transparent); +} + +.stepper-step-btn:focus-visible { + outline: 2px solid var(--primary-color); + outline-offset: 2px; +} + +.stepper-step-btn.is-active { + color: var(--primary-color); + font-weight: 700; +} + +.stepper-step-btn.is-complete { + color: var(--text-main); +} + +.stepper-step-btn.is-locked { + cursor: not-allowed; + opacity: 0.55; +} + +/* Step node (circle) */ +.stepper-node { + display: inline-flex; + align-items: center; + justify-content: center; + width: 22px; + height: 22px; + border-radius: 50%; + font-size: 0.75rem; + font-weight: 700; + border: 1.5px solid var(--border-color, #cbd5e1); + background: #ffffff; + color: var(--text-muted); + flex-shrink: 0; + transition: all 0.2s ease; +} + +.stepper-node.is-active { + background: var(--primary-color); + border-color: var(--primary-color); + color: #ffffff; +} + +.stepper-node.is-complete { + background: color-mix(in srgb, var(--primary-color) 15%, #ffffff); + border-color: var(--primary-color); + color: var(--primary-color); +} + +/* Responsive adjustments */ +@media (max-width: 1100px) { + /* Four long labels cannot share this bar with the CTA. Keep the step you are on + named, let the rest read as numbered nodes (full name stays in [title]). + Without this, flex shrink ellipsises all four labels at once. + 1100px is the measured crossover for the widest CTA ("Continue with N variables"). + ponytail: viewport-derived, so a longer CTA string can clip the labels for a ~30px + band above this. Upgrade path is a ResizeObserver on .studio-sub-header. */ + .stepper-step-btn:not(.is-active) .stepper-step-label { + display: none; + } + .stepper-connector { + flex-basis: 20px; + } +} + +@media (max-width: 1024px) { + .stepper-connector { + flex-basis: 12px; /* flex-basis, not width: the item is a flex item */ + } +} + +@media (max-width: 768px) { + .stepper-step-label { + display: none; + } + .stepper-connector { + flex-basis: 8px; + } +} diff --git a/src/app/pages/experiment-studio/stepper/studio-stepper.component.html b/src/app/pages/experiment-studio/stepper/studio-stepper.component.html new file mode 100644 index 000000000..9d94e3ea9 --- /dev/null +++ b/src/app/pages/experiment-studio/stepper/studio-stepper.component.html @@ -0,0 +1,30 @@ +<nav class="studio-stepper-header" aria-label="Experiment Studio wizard" data-guide="studio-navigation"> + <ol class="stepper-track"> + @for (step of parentSteps; track step.id; let isLast = $last) { + <li class="stepper-track-item"> + <button + type="button" + class="stepper-step-btn" + [class.is-active]="activeSection() === step.id" + [class.is-complete]="stepState(step.id) === 'complete'" + [class.is-locked]="stepState(step.id) === 'locked'" + [disabled]="!isStepClickable(step.id)" + [attr.aria-current]="activeSection() === step.id ? 'step' : null" + [attr.aria-disabled]="!isStepClickable(step.id)" + [attr.data-guide]="step.guide" + [title]="stepTitle(step)" + (click)="onStepClick(step.id)" + > + <span class="stepper-node" [class.is-complete]="stepState(step.id) === 'complete'" [class.is-active]="activeSection() === step.id"> + <span class="stepper-node-index">{{ step.index }}</span> + </span> + <span class="stepper-step-label">{{ step.label }}</span> + </button> + + @if (!isLast) { + <span class="stepper-connector" [class.is-complete]="stepState(step.id) === 'complete'" aria-hidden="true"></span> + } + </li> + } + </ol> +</nav> diff --git a/src/app/pages/experiment-studio/stepper/studio-stepper.component.spec.ts b/src/app/pages/experiment-studio/stepper/studio-stepper.component.spec.ts new file mode 100644 index 000000000..2b07a0cc0 --- /dev/null +++ b/src/app/pages/experiment-studio/stepper/studio-stepper.component.spec.ts @@ -0,0 +1,115 @@ +import type { MockedObject } from 'vitest'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { StudioStepperComponent } from './studio-stepper.component'; +import { + ExperimentStudioNavigationHost, + ExperimentStudioNavigationService, + ExperimentStudioSection, + StepState, +} from '../../../services/experiment-studio-navigation.service'; +import { createSpyObj } from '../../../testing/spy-obj.testing'; + +describe('StudioStepperComponent', () => { + let fixture: ComponentFixture<StudioStepperComponent>; + let component: StudioStepperComponent; + let navService: ExperimentStudioNavigationService; + let host: MockedObject<ExperimentStudioNavigationHost>; + + /** railStatus is a full Record, so every test states all four steps explicitly. */ + function rail(overrides: Partial<Record<ExperimentStudioSection, StepState>> = {}): Record<ExperimentStudioSection, StepState> { + return { + 'variables-top': 'complete', + 'statistics-section': 'complete', + 'algorithm-section': 'available', + 'execution-section': 'locked', + ...overrides, + }; + } + + beforeEach(async () => { + // The service decides clickability, the registered host receives the navigation. + host = createSpyObj<ExperimentStudioNavigationHost>('ExperimentStudioNavigationHost', [ + 'navigateToSection', + 'navigateToDescriptiveStep', + 'runExperiment', + 'backToDashboard', + ]); + + await TestBed.configureTestingModule({ + imports: [StudioStepperComponent], + providers: [ExperimentStudioNavigationService], + }) + .compileComponents(); + + navService = TestBed.inject(ExperimentStudioNavigationService); + navService.register(host); + fixture = TestBed.createComponent(StudioStepperComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + afterEach(() => navService.unregister(host)); + + function publish(partial: Parameters<ExperimentStudioNavigationService['publishState']>[0]): void { + navService.publishState({ railStatus: rail(), ...partial }); + fixture.detectChanges(); + } + + it('renders the four parent steps by name, with no station sub-steps', () => { + const labels = [...(fixture.nativeElement as HTMLElement).querySelectorAll('.stepper-step-label')]; + + expect(labels.map((l) => l.textContent?.trim())).toEqual([ + 'Data Exploration', + 'Data Handling', + 'Algorithm Selection', + 'Execution Results', + ]); + expect((fixture.nativeElement as HTMLElement).querySelector('.stepper-substep-group')).toBeNull(); + expect(component.parentSteps.length).toBe(4); + }); + + it('marks completed steps with a check and lights the connector behind them', () => { + publish({ activeSection: 'algorithm-section' }); + + const nodes = (fixture.nativeElement as HTMLElement).querySelectorAll('.stepper-node'); + expect(nodes[0].classList.contains('is-complete')).toBe(true); + expect(nodes[0].textContent?.trim()).toBe('1'); + expect(nodes[2].classList.contains('is-active')).toBe(true); + + const connectors = (fixture.nativeElement as HTMLElement).querySelectorAll('.stepper-connector'); + expect(connectors[0].classList.contains('is-complete')).toBe(true); + expect(connectors[2].classList.contains('is-complete')).toBe(false); + }); + + it('keeps a locked step inert and explains it, preferring the per-step reason', () => { + publish({ + activeSection: 'algorithm-section', + lockedStepReason: 'Run the experiment to unlock', + lockedStepReasons: { 'execution-section': 'Select an algorithm first' }, + }); + + const locked = (fixture.nativeElement as HTMLElement).querySelectorAll<HTMLButtonElement>('.stepper-step-btn')[3]; + expect(locked.disabled).toBe(true); + expect(locked.getAttribute('aria-disabled')).toBe('true'); + expect(locked.title).toBe('Select an algorithm first'); + + locked.click(); + expect(host.navigateToSection).not.toHaveBeenCalled(); + }); + + it('navigates to a step that is available', () => { + publish({ activeSection: 'variables-top' }); + + (fixture.nativeElement as HTMLElement) + .querySelectorAll<HTMLButtonElement>('.stepper-step-btn')[2] + .click(); + + expect(host.navigateToSection).toHaveBeenCalledWith('algorithm-section', undefined); + }); + + it('never renders a run control here — running belongs to the studio action bar', () => { + publish({ activeSection: 'algorithm-section' }); + + expect((fixture.nativeElement as HTMLElement).querySelector('.stepper-run-btn')).toBeNull(); + }); +}); diff --git a/src/app/pages/experiment-studio/stepper/studio-stepper.component.ts b/src/app/pages/experiment-studio/stepper/studio-stepper.component.ts new file mode 100644 index 000000000..04989b6e9 --- /dev/null +++ b/src/app/pages/experiment-studio/stepper/studio-stepper.component.ts @@ -0,0 +1,57 @@ +import { + ChangeDetectionStrategy, + Component, + computed, + inject, +} from '@angular/core'; +import { + ExperimentStudioNavigationService, + ExperimentStudioSection, + StepState, +} from '../../../services/experiment-studio-navigation.service'; + +interface ParentStep { + id: ExperimentStudioSection; + index: number; + label: string; + guide: string; +} + +@Component({ + selector: 'app-studio-stepper', + templateUrl: './studio-stepper.component.html', + styleUrl: './studio-stepper.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class StudioStepperComponent { + private nav = inject(ExperimentStudioNavigationService); + readonly state = this.nav.state; + + readonly parentSteps: ParentStep[] = [ + { id: 'variables-top', index: 1, label: 'Data Exploration', guide: 'studio-step-variables' }, + { id: 'statistics-section', index: 2, label: 'Data Handling', guide: 'studio-step-statistics' }, + { id: 'algorithm-section', index: 3, label: 'Algorithm Selection', guide: 'studio-step-algorithm' }, + { id: 'execution-section', index: 4, label: 'Execution Results', guide: 'studio-step-execution' }, + ]; + + readonly activeSection = computed(() => this.state().activeSection); + + stepState(id: ExperimentStudioSection): StepState { + return this.state().railStatus[id] ?? 'available'; + } + + isStepClickable(id: ExperimentStudioSection): boolean { + return this.stepState(id) !== 'locked'; + } + + /** Locked steps explain themselves; the generic reason is the fallback. */ + stepTitle(step: ParentStep): string { + if (this.isStepClickable(step.id)) return step.label; + return this.state().lockedStepReasons[step.id] ?? this.state().lockedStepReason; + } + + onStepClick(id: ExperimentStudioSection): void { + if (!this.isStepClickable(id)) return; + this.nav.navigateToSection(id); + } +} diff --git a/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.css b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.css index 7a76c2312..c8b677f42 100644 --- a/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.css +++ b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.css @@ -1,70 +1,109 @@ -.data-model-selector { - display: inline-flex; +:host { + display: block; + width: 100%; +} + +.data-model-selector-group { + display: flex; flex-direction: column; - align-items: flex-start; - gap: 4px; - font-family: "Roboto", system-ui, -apple-system, BlinkMacSystemFont, - "Segoe UI", sans-serif; + gap: 10px; + width: 100%; } -.control-label { - font-size: 0.72rem; - font-weight: 700; - letter-spacing: 0.08em; - text-transform: uppercase; - color: #0f172a; +.data-model-chip-row { + display: flex; + flex-wrap: wrap; + align-items: stretch; + gap: 8px; + width: 100%; } -/* Styled select, more in line with dashboard pills */ -.data-model-select { - width: 170px; - padding: 6px 30px 6px 10px; +.studio-chip.data-model-chip { + position: relative; + display: inline-flex; + align-items: center; + gap: 8px; + min-height: 32px; + padding: 6px 12px; + border: 1px solid rgba(148, 163, 184, 0.3); + border-radius: 10px; + background: var(--bg-neutral); + color: var(--chart-label-color); font-size: 0.82rem; font-weight: 600; - color: #0f172a; - background: #ffffff; - border-radius: 999px; - border: 1px solid #d1e4ea; - box-shadow: 0 3px 8px rgba(15, 23, 42, 0.08); - outline: none; + line-height: 1.25; cursor: pointer; + box-sizing: border-box; + transition: + background-color 0.15s ease, + border-color 0.15s ease, + color 0.15s ease, + box-shadow 0.15s ease, + transform 0.12s ease; + user-select: none; +} + +.studio-chip.data-model-chip input { + position: absolute; + width: 1px; + height: 1px; + margin: 0; + opacity: 0; + pointer-events: none; +} - /* Native select cleanup */ - -webkit-appearance: none; - -moz-appearance: none; - appearance: none; +.model-radio-indicator { + display: inline-flex; + align-items: center; + justify-content: center; + width: 14px; + height: 14px; + border-radius: 50%; + border: 1.5px solid rgba(148, 163, 184, 0.6); + background: #ffffff; + flex: 0 0 auto; + box-sizing: border-box; + transition: + background-color 0.15s ease, + border-color 0.15s ease, + box-shadow 0.15s ease; +} - /* Custom arrow */ - background-image: url("data:image/svg+xml,%3Csvg width='16' height='16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M4 6l4 4 4-4' stroke='%232b33e9' stroke-width='1.6' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); - background-repeat: no-repeat; - background-position: right 12px center; - background-size: 12px; - transition: border-color 0.16s ease, box-shadow 0.16s ease, - background-color 0.16s ease, transform 0.12s ease; +.model-label { + white-space: nowrap; } -.data-model-select:hover { - background-color: #f7fcfd; +.studio-chip.data-model-chip:hover { + background: #ffffff; + border-color: rgba(43, 51, 233, 0.35); + color: var(--text-main); + transform: translateY(-1px); + box-shadow: 0 2px 6px rgba(43, 51, 233, 0.06); +} + +.studio-chip.data-model-chip:hover .model-radio-indicator { + border-color: rgba(43, 51, 233, 0.4); +} + +.studio-chip.data-model-chip.is-selected { + background: rgba(43, 51, 233, 0.08); border-color: var(--primary-color); - box-shadow: 0 6px 14px rgba(43, 51, 233, 0.12); - transform: translateY(-0.5px); + color: var(--primary-color); + font-weight: 700; + box-shadow: 0 1px 4px rgba(43, 51, 233, 0.12); } -.data-model-select:focus { +.studio-chip.data-model-chip.is-selected .model-radio-indicator { + background: var(--primary-color); border-color: var(--primary-color); - box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.35), 0 6px 14px rgba(15, 23, 42, 0.12); + box-shadow: inset 0 0 0 2.5px #ffffff; } -/* Just to keep optgroup labels readable */ -.data-model-select optgroup[label] { - font-weight: 600; - color: var(--primary-color); +.studio-chip.data-model-chip:has(input:focus-visible), +.studio-chip.data-model-chip:focus-visible { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.18); } -/* Mobile tweaks */ -@media (max-width: 960px) { - .data-model-select { - min-width: 150px; - font-size: 0.8rem; - } -} \ No newline at end of file + diff --git a/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.html b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.html index 6eea0dde8..03857abee 100644 --- a/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.html +++ b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.html @@ -1,27 +1,17 @@ -<div class="data-model-selector"> - <label class="control-label" for="dataModel">Pathology</label> - <select class="data-model-select" id="dataModel" [(ngModel)]="selectedDataModel" (change)="onDataModelChange()" - aria-labelledby="dataModel"> - <!-- Cross-Sectional Data Models Group --> - @if (crossSectionalModels.length > 0) { - <optgroup label="Cross-Sectional"> - @for (model of crossSectionalModels; track model) { - <option [ngValue]="model"> - {{ model.label }} - </option> - } - </optgroup> +<div class="studio-chip-group data-model-selector-group" data-guide="data-model-selector"> + <span class="studio-chip-label" id="dataModel">Pathology</span> + <div class="studio-chip-row data-model-chip-row" role="group" aria-labelledby="dataModel"> + @for (model of models(); track model) { + <label class="studio-chip data-model-chip" [class.is-selected]="isOptionSelected(model)"> + <input + type="radio" + name="dataModel" + [checked]="isOptionSelected(model)" + (change)="selectModel(model)" + /> + <span class="model-radio-indicator" aria-hidden="true"></span> + <span class="model-label">{{ model.label }}</span> + </label> } - - <!-- Longitudinal Data Models Group --> - @if (longitudinalModels.length > 0) { - <optgroup label="Longitudinal"> - @for (model of longitudinalModels; track model) { - <option [ngValue]="model"> - {{ model.label }} - </option> - } - </optgroup> - } - </select> -</div> \ No newline at end of file + </div> +</div> diff --git a/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.spec.ts b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.spec.ts new file mode 100644 index 000000000..0af3b948d --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.spec.ts @@ -0,0 +1,91 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideZonelessChangeDetection } from '@angular/core'; +import { DataModelSelectorComponent } from './data-model-selector.component'; +import { DataModel } from '../../../../models/data-model.interface'; + +describe('DataModelSelectorComponent', () => { + let fixture: ComponentFixture<DataModelSelectorComponent>; + let component: DataModelSelectorComponent; + + const persistedModel: DataModel = { + uuid: 'persisted-stroke', + code: 'stroke', + version: '3.7', + label: 'Stroke 3.7', + released: true, + }; + + const catalogModel: DataModel = { + uuid: 'catalog-stroke', + code: 'stroke', + version: '3.7', + label: 'Stroke 3.7', + released: true, + }; + + const tbiModel: DataModel = { + uuid: 'tbi-model', + code: 'tbi', + version: '2.0', + label: 'Traumatic Brain Injury', + released: true, + }; + + const longitudinalModel: DataModel = { + uuid: 'long-model', + code: 'long', + version: '1.0', + label: 'Longitudinal Example', + released: true, + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [DataModelSelectorComponent], + providers: [provideZonelessChangeDetection()], + }); + + fixture = TestBed.createComponent(DataModelSelectorComponent); + component = fixture.componentInstance; + }); + + it('replaces an equivalent persisted model with the catalog option instance without re-emitting', () => { + vi.spyOn(component.dataModelChange, 'emit').mockReturnValue(undefined); + component.selectedDataModel = persistedModel; + fixture.componentRef.setInput('crossSectionalModels', [catalogModel]); + fixture.componentRef.setInput('defaultModel', catalogModel); + + fixture.detectChanges(); + + expect(component.selectedDataModel).toBe(catalogModel); + expect(component.dataModelChange.emit).not.toHaveBeenCalled(); + }); + + it('renders one chip per model and marks the selected chip', () => { + fixture.componentRef.setInput('crossSectionalModels', [catalogModel, tbiModel]); + fixture.componentRef.setInput('longitudinalModels', [longitudinalModel]); + fixture.componentRef.setInput('defaultModel', catalogModel); + fixture.detectChanges(); + + const chips = fixture.nativeElement.querySelectorAll('.studio-chip'); + expect(chips.length).toBe(3); + expect(chips[0].textContent).toContain('Stroke 3.7'); + expect(chips[0].classList.contains('is-selected')).toBe(true); + expect(chips[0].querySelector('input')!.checked).toBe(true); + expect(chips[1].querySelector('input')!.checked).toBe(false); + }); + + it('selects a model via its chip and emits the change', () => { + const emitSpy = vi.spyOn(component.dataModelChange, 'emit').mockReturnValue(undefined); + fixture.componentRef.setInput('crossSectionalModels', [catalogModel, tbiModel]); + fixture.componentRef.setInput('defaultModel', catalogModel); + fixture.detectChanges(); + + const chips = fixture.nativeElement.querySelectorAll('.studio-chip'); + (chips[1] as HTMLElement).click(); + fixture.detectChanges(); + + expect(component.selectedDataModel).toBe(tbiModel); + expect(emitSpy).toHaveBeenCalledWith(tbiModel); + }); +}); diff --git a/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.ts b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.ts index 87c328ed1..0d574fc70 100644 --- a/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.ts +++ b/src/app/pages/experiment-studio/variables-panel/data-model-selector/data-model-selector.component.ts @@ -1,24 +1,24 @@ -import { Component, Input, Output, EventEmitter, SimpleChanges, OnChanges, OnInit } from '@angular/core'; -import { FormsModule } from "@angular/forms"; +import { ChangeDetectionStrategy, Component, computed, input, OnChanges, OnInit, output, SimpleChanges } from '@angular/core'; import { DataModel } from '../../../../models/data-model.interface'; @Component({ - selector: 'app-data-model-selector', - templateUrl: './data-model-selector.component.html', - styleUrls: ['./data-model-selector.component.css'], - imports: [ - FormsModule, - ] + selector: 'app-data-model-selector', + templateUrl: './data-model-selector.component.html', + styleUrl: './data-model-selector.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class DataModelSelectorComponent implements OnChanges, OnInit { - @Input() crossSectionalModels: DataModel[] = []; - @Input() longitudinalModels: DataModel[] = []; - @Input() defaultModel: DataModel | null = null; + readonly crossSectionalModels = input<DataModel[]>([]); + readonly longitudinalModels = input<DataModel[]>([]); + readonly defaultModel = input<DataModel | null>(null); - @Output() dataModelChange = new EventEmitter<DataModel | null>(); + readonly dataModelChange = output<DataModel | null>(); selectedDataModel: DataModel | null = null; + /** Single flat list for the chip loop (cross-sectional first). */ + readonly models = computed(() => [...this.crossSectionalModels(), ...this.longitudinalModels()]); + ngOnInit(): void { this.updateSelectedModel(); } @@ -30,11 +30,30 @@ export class DataModelSelectorComponent implements OnChanges, OnInit { } updateSelectedModel(): void { - this.selectedDataModel = this.defaultModel; - this.dataModelChange.emit(this.selectedDataModel); + const defaultModel = this.defaultModel(); + if (this.selectedDataModel === defaultModel) { + return; + } + + const shouldEmit = !this.isSameModel(this.selectedDataModel, defaultModel); + this.selectedDataModel = defaultModel; + if (shouldEmit) { + this.dataModelChange.emit(this.selectedDataModel); + } } - onDataModelChange(): void { + selectModel(model: DataModel): void { + this.selectedDataModel = model; this.dataModelChange.emit(this.selectedDataModel); } + + isOptionSelected(model: DataModel): boolean { + return this.isSameModel(this.selectedDataModel, model); + } + + private isSameModel(a: DataModel | null, b: DataModel | null): boolean { + if (a === b) return true; + if (!a || !b) return false; + return a.code === b.code && String(a.version) === String(b.version); + } } diff --git a/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.css b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.css index 73faecc95..162296228 100644 --- a/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.css +++ b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.css @@ -1,163 +1,183 @@ -.dataset-selector { - position: relative; - display: inline-flex; - flex-direction: column; - align-items: flex-start; - gap: 4px; - font-family: "Roboto", system-ui, -apple-system, BlinkMacSystemFont, - "Segoe UI", sans-serif; +:host { + display: block; + width: 100%; } -.control-label { - font-size: 0.72rem; - font-weight: 700; - letter-spacing: 0.08em; - text-transform: uppercase; - color: #0f172a; -} - -.dataset-control { - position: relative; - display: inline-flex; - align-items: center; +.dataset-selector-group { + display: flex; + flex-direction: column; + gap: 10px; + width: 100%; } -/* Main button */ -.dataset-btn { - display: inline-flex; +.selector-section-header { + display: flex; align-items: center; - gap: 8px; - padding: 6px 10px; - border-radius: 999px; - border: 1px solid #d1e4ea; - background: #ffffff; - cursor: pointer; - font-size: 0.82rem; - font-weight: 600; - color: #0f172a; - box-shadow: 0 3px 8px rgba(15, 23, 42, 0.08); - transition: background 0.16s ease, box-shadow 0.16s ease, - border-color 0.16s ease, transform 0.12s ease; - min-width: 170px; justify-content: space-between; + width: 100%; } -.dataset-btn:hover { - background: linear-gradient(90deg, var(--bg-color, #f8fafc), rgba(43, 51, 233, 0.06)); - border-color: var(--primary-color); - box-shadow: 0 6px 14px rgba(43, 51, 233, 0.12); - transform: translateY(-0.5px); -} - -.dataset-btn:active { - transform: translateY(0); - box-shadow: 0 2px 6px rgba(15, 23, 42, 0.12); -} - -.dataset-btn-label { - white-space: nowrap; +.selector-title-wrapper { + display: flex; + align-items: center; + gap: 6px; } -/* Count pill inside button */ -.dataset-count { +.dataset-counter-badge { display: inline-flex; align-items: center; justify-content: center; - min-width: 20px; - height: 20px; + min-width: 18px; + height: 18px; padding: 0 6px; border-radius: 999px; - background-color: var(--primary-color); - color: #ffffff; + background: rgba(43, 51, 233, 0.08); + color: var(--primary-color); font-size: 0.68rem; font-weight: 700; - line-height: 1; - box-shadow: 0 3px 8px rgba(43, 51, 233, 0.2); -} - -/* Dropdown menu */ -.dataset-dropdown-menu { - position: absolute; - top: calc(100% + 6px); - left: 0; - width: 230px; - background-color: #ffffff; - border: 1px solid #d1e4ea; - box-shadow: 0 10px 24px rgba(15, 23, 42, 0.18); - z-index: 1000; - list-style: none; - padding: 4px 0; - margin: 0; - max-height: 260px; - overflow-y: auto; - border-radius: 10px; - animation: datasetFadeIn 0.16s ease-in-out; + font-variant-numeric: tabular-nums; } -@keyframes datasetFadeIn { - from { - opacity: 0; - transform: translateY(-4px); - } - - to { - opacity: 1; - transform: translateY(0); - } +.dataset-quick-actions { + display: flex; + align-items: center; + gap: 8px; } -.dataset-dropdown-menu li { +.dataset-action-link { + display: inline-flex; + align-items: center; padding: 0; - font-size: 0.86rem; + border: none; + background: transparent; + color: var(--primary-color); + font-size: 0.72rem; + font-weight: 600; + cursor: pointer; + text-decoration: underline; + text-underline-offset: 2px; + transition: color 0.15s ease, opacity 0.15s ease; } -.dataset-dropdown-menu li+li { - border-top: 1px solid #f3f5f7; +.dataset-action-link:hover { + opacity: 0.8; } -.dataset-dropdown-menu label { +.dataset-action-link--clear { + color: var(--text-muted); +} + +.dataset-action-link--clear:hover { + color: var(--danger); +} + +.dataset-list-container { display: flex; - align-items: center; + flex-direction: column; gap: 6px; - cursor: pointer; + width: 100%; + max-height: 220px; + overflow-y: auto; + padding-right: 2px; +} + +.studio-chip.dataset-item-btn { + display: flex; + align-items: center; + gap: 10px; + width: 100%; + min-height: 34px; padding: 6px 10px; - color: #111827; - transition: background-color 0.18s ease; + border: 1px solid rgba(148, 163, 184, 0.25); + border-radius: 8px; + background: var(--bg-neutral); + color: #1e293b; + font-family: inherit; + font-size: 0.8rem; + font-weight: 550; + line-height: 1.3; + text-align: left; + cursor: pointer; + box-sizing: border-box; + white-space: normal; + overflow: visible; + transition: + background-color 0.15s ease, + border-color 0.15s ease, + color 0.15s ease, + box-shadow 0.15s ease, + transform 0.12s ease; + user-select: none; +} + +.dataset-checkbox-box { + display: inline-flex; + align-items: center; + justify-content: center; + width: 15px; + height: 15px; + border-radius: 4px; + border: 1.5px solid rgba(148, 163, 184, 0.6); + background: #ffffff; + flex: 0 0 auto; + box-sizing: border-box; + color: #ffffff; + font-size: 0.55rem; + transition: + background-color 0.15s ease, + border-color 0.15s ease; } -.dataset-dropdown-menu label:hover { - background-color: rgba(43, 51, 233, 0.06); +.dataset-item-label { + flex: 1 1 auto; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + word-break: break-word; } -/* Checkboxes */ -.dataset-dropdown-menu input[type="checkbox"] { - margin-right: 4px; - accent-color: var(--primary-color); +.studio-chip.dataset-item-btn:hover { + background: #ffffff; + border-color: rgba(43, 51, 233, 0.35); + color: var(--text-main); + transform: translateY(-1px); + box-shadow: 0 2px 6px rgba(43, 51, 233, 0.05); } -/* Scrollbar */ -.dataset-dropdown-menu::-webkit-scrollbar { - width: 6px; +.studio-chip.dataset-item-btn:hover .dataset-checkbox-box { + border-color: rgba(43, 51, 233, 0.5); } -.dataset-dropdown-menu::-webkit-scrollbar-track { - background: #f9fafb; +.studio-chip.dataset-item-btn.is-selected { + background: rgba(43, 51, 233, 0.05); + border-color: rgba(43, 51, 233, 0.45); + color: var(--text-main); + font-weight: 600; + box-shadow: 0 1px 3px rgba(43, 51, 233, 0.06); } -.dataset-dropdown-menu::-webkit-scrollbar-thumb { - background-color: var(--primary-light); - border-radius: 999px; +.studio-chip.dataset-item-btn.is-selected .dataset-checkbox-box { + background: var(--primary-color); + border-color: var(--primary-color); +} + +.studio-chip.dataset-item-btn:focus-visible { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.18); } -/* Mobile: slightly bigger hit area */ -@media (max-width: 960px) { - .dataset-btn { - padding: 5px 10px; - font-size: 0.8rem; - min-width: 150px; - } +.dataset-empty-state { + display: flex; + align-items: center; + justify-content: center; + padding: 16px 12px; + border-radius: 8px; + background: var(--bg-neutral); + border: 1px dashed rgba(148, 163, 184, 0.3); + color: #64748b; + font-size: 0.78rem; + font-weight: 500; + text-align: center; +} - .dataset-dropdown-menu { - width: 220px; - } -} \ No newline at end of file diff --git a/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.html b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.html index fa8ab8a1f..4a79a4bc2 100644 --- a/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.html +++ b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.html @@ -1,27 +1,61 @@ -<div class="dataset-selector"> - <label class="control-label">Datasets</label> - <div class="dataset-control"> - <button class="dataset-btn" (click)="toggleDropdown()"> - <span class="dataset-btn-label">Datasets</span> - <span class="dataset-count">{{ selectedDatasets.size }}</span> - </button> - </div> +<div class="studio-chip-group dataset-selector-group" data-guide="dataset-selector"> + <div class="selector-section-header"> + <div class="selector-title-wrapper"> + <span class="studio-chip-label" id="datasetsLabel">Datasets</span> + <span class="dataset-counter-badge">{{ selectedCount() }}/{{ totalCount() }}</span> + </div> - <!-- Dropdown menu --> - @if (isDropdownOpen) { - <ul class="dataset-dropdown-menu"> - @for (dataset of datasets; track dataset) { - <li> - <label> - <input - type="checkbox" - [checked]="isDatasetSelected(dataset.code)" - (change)="toggleDataset(dataset.code)" - /> - {{ dataset.label }} - </label> - </li> + @if (totalCount() > 0) { + <div class="dataset-quick-actions"> + @if (selectedCount() < totalCount()) { + <button + type="button" + class="dataset-action-link" + (click)="selectAll()" + title="Select all datasets" + > + Select all + </button> } - </ul> - } + @if (selectedCount() > 0) { + <button + type="button" + class="dataset-action-link dataset-action-link--clear" + (click)="clearAll()" + title="Clear selected datasets" + > + Clear + </button> + } + </div> + } + </div> + + <div + class="studio-chip-row dataset-list-container" + role="group" + aria-labelledby="datasetsLabel" + > + @for (dataset of datasets(); track dataset.code) { + <button + type="button" + class="studio-chip dataset-item-btn" + [class.is-selected]="isDatasetSelected(dataset.code)" + [attr.aria-pressed]="isDatasetSelected(dataset.code)" + (click)="toggleDataset(dataset.code)" + [title]="dataset.label" + > + <span class="dataset-checkbox-box" aria-hidden="true"> + @if (isDatasetSelected(dataset.code)) { + <i class="fas fa-check"></i> + } + </span> + <span class="dataset-item-label">{{ dataset.label }}</span> + </button> + } @empty { + <div class="dataset-empty-state"> + <span>No datasets available for this pathology</span> + </div> + } + </div> </div> diff --git a/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.spec.ts b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.spec.ts new file mode 100644 index 000000000..31070c055 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.spec.ts @@ -0,0 +1,127 @@ +import type { MockedObject } from 'vitest'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideZonelessChangeDetection } from '@angular/core'; +import { DatasetSelectorComponent } from './dataset-selector.component'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { createSpyObj } from '../../../../testing/spy-obj.testing'; + +describe('DatasetSelectorComponent', () => { + let fixture: ComponentFixture<DatasetSelectorComponent>; + let component: DatasetSelectorComponent; + let expStudioService: MockedObject<ExperimentStudioService>; + + const setInputs = (inputs: { + datasets?: { code: string; label: string }[]; + selectedDatasetCodes?: string[]; + }): void => { + Object.entries(inputs).forEach(([name, value]) => { + fixture.componentRef.setInput(name, value); + }); + fixture.detectChanges(); + }; + + beforeEach(async () => { + expStudioService = createSpyObj('ExperimentStudioService', ['setSelectedDatasets']); + + await TestBed.configureTestingModule({ + imports: [DatasetSelectorComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: expStudioService }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(DatasetSelectorComponent); + component = fixture.componentInstance; + }); + + it('does not select datasets the service has not asked for', () => { + setInputs({ + datasets: [ + { code: 'ds1', label: 'Dataset 1' }, + { code: 'ds2', label: 'Dataset 2' }, + ], + }); + + expect(component.selectedDatasets().size).toBe(0); + expect(expStudioService.setSelectedDatasets).not.toHaveBeenCalled(); + }); + + it('uses hydrated dataset codes instead of replacing them with all datasets', () => { + setInputs({ + selectedDatasetCodes: ['ds2'], + datasets: [ + { code: 'ds1', label: 'Dataset 1' }, + { code: 'ds2', label: 'Dataset 2' }, + ], + }); + + expect(component.isDatasetSelected('ds1')).toBe(false); + expect(component.isDatasetSelected('ds2')).toBe(true); + expect(expStudioService.setSelectedDatasets).not.toHaveBeenCalled(); + }); + + it('renders a chip per dataset and toggles the selection on click', () => { + expStudioService.setSelectedDatasets.mockClear(); + // Mirror the real service: writing the selection is what feeds the input back. + expStudioService.setSelectedDatasets.mockImplementation((codes: string[]) => + fixture.componentRef.setInput('selectedDatasetCodes', codes) + ); + setInputs({ + selectedDatasetCodes: ['ds1'], + datasets: [ + { code: 'ds1', label: 'Dataset 1' }, + { code: 'ds2', label: 'Dataset 2' }, + ], + }); + + const chips = fixture.nativeElement.querySelectorAll('.studio-chip') as NodeListOf<HTMLElement>; + expect(chips.length).toBe(2); + expect(chips[0].classList.contains('is-selected')).toBe(true); + expect(chips[1].classList.contains('is-selected')).toBe(false); + + chips[1].click(); + fixture.detectChanges(); + + expect(component.isDatasetSelected('ds2')).toBe(true); + expect(expStudioService.setSelectedDatasets).toHaveBeenCalledWith(['ds1', 'ds2']); + }); + + it('selects all datasets when selectAll is called', () => { + setInputs({ + selectedDatasetCodes: ['ds1'], + datasets: [ + { code: 'ds1', label: 'Dataset 1' }, + { code: 'ds2', label: 'Dataset 2' }, + ], + }); + + component.selectAll(); + expect(expStudioService.setSelectedDatasets).toHaveBeenCalledWith(['ds1', 'ds2']); + }); + + it('clears all datasets when clearAll is called', () => { + setInputs({ + selectedDatasetCodes: ['ds1', 'ds2'], + datasets: [ + { code: 'ds1', label: 'Dataset 1' }, + { code: 'ds2', label: 'Dataset 2' }, + ], + }); + + component.clearAll(); + expect(expStudioService.setSelectedDatasets).toHaveBeenCalledWith([]); + }); + + it('shows empty state when datasets array is empty', () => { + setInputs({ + datasets: [], + selectedDatasetCodes: [], + }); + + const emptyState = fixture.nativeElement.querySelector('.dataset-empty-state'); + expect(emptyState).toBeTruthy(); + expect(emptyState.textContent).toContain('No datasets available'); + }); +}); + diff --git a/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.ts b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.ts index 9f4daf388..9b6eaaf9a 100644 --- a/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.ts +++ b/src/app/pages/experiment-studio/variables-panel/dataset-selector/dataset-selector.component.ts @@ -1,85 +1,46 @@ -import { Component, Input, Output, EventEmitter, OnChanges, SimpleChanges, HostListener, ElementRef } from '@angular/core'; +import { ChangeDetectionStrategy, Component, computed, inject, input } from '@angular/core'; import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; @Component({ selector: 'app-dataset-selector', - standalone: true, templateUrl: './dataset-selector.component.html', - styleUrls: ['./dataset-selector.component.css'], + styleUrl: './dataset-selector.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class DatasetSelectorComponent implements OnChanges { - @Input() datasets: { code: string; label: string }[] = []; - @Output() selectedDatasetsChange = new EventEmitter<string[]>(); - isDropdownOpen = false; +export class DatasetSelectorComponent { + private expStudioService = inject(ExperimentStudioService); + readonly datasets = input<{ code: string; label: string }[]>([]); + readonly selectedDatasetCodes = input<string[]>([]); - constructor(private elementRef: ElementRef, private expStudioService: ExperimentStudioService) { } + /** Selection lives in the service; this is it filtered to the live datasets. */ + readonly selectedDatasets = computed(() => { + const available = new Set(this.datasets().map((dataset) => dataset.code)); + return new Set(this.selectedDatasetCodes().filter((code) => available.has(code))); + }); - - selectedDatasets = new Set<string>(); // Store selected datasets - - ngOnChanges(changes: SimpleChanges): void { - if (changes['datasets'] && this.datasets?.length > 0) { - const prevSelection = new Set(this.selectedDatasets); - const currentCodes = this.datasets.map(d => d.code); - - // keep only valid selections that still exist - this.selectedDatasets = new Set( - [...prevSelection].filter(code => currentCodes.includes(code)) - ); - - // if none left, preselect all (initial mount case) - if (this.selectedDatasets.size === 0) { - this.preselectAllDatasets(); - } else { - this.emitSelectedDatasets(); - } - } - } - - onDatasetSelectionChange(event: Event): void { - const selectedOptions = (event.target as HTMLSelectElement).selectedOptions; - this.selectedDatasets = new Set( - Array.from(selectedOptions).map((option) => option.value) - ); - this.emitSelectedDatasets(); - } + readonly totalCount = computed(() => this.datasets().length); + readonly selectedCount = computed(() => this.selectedDatasets().size); toggleDataset(datasetCode: string): void { - // Add or remove dataset based on selection - if (this.selectedDatasets.has(datasetCode)) { - this.selectedDatasets.delete(datasetCode); + const selected = this.selectedDatasets(); + if (selected.has(datasetCode)) { + selected.delete(datasetCode); } else { - this.selectedDatasets.add(datasetCode); + selected.add(datasetCode); } - this.emitSelectedDatasets(); + this.expStudioService.setSelectedDatasets(Array.from(selected)); } - isDatasetSelected(datasetCode: string): boolean { - return this.selectedDatasets.has(datasetCode); + selectAll(): void { + const allCodes = this.datasets().map((dataset) => dataset.code); + this.expStudioService.setSelectedDatasets(allCodes); } - emitSelectedDatasets(): void { - const selected = Array.from(this.selectedDatasets); - this.selectedDatasetsChange.emit(selected); - - // Notify the global service - this.expStudioService.setSelectedDatasets(selected); - } - - private preselectAllDatasets(): void { - this.selectedDatasets = new Set(this.datasets.map((dataset) => dataset.code)); - this.emitSelectedDatasets(); // Emit the preselected datasets - } - - toggleDropdown(): void { - this.isDropdownOpen = !this.isDropdownOpen; + clearAll(): void { + this.expStudioService.setSelectedDatasets([]); } - @HostListener('document:click', ['$event']) - onOutsideClick(event: Event): void { - if (!this.elementRef.nativeElement.contains(event.target)) { - this.isDropdownOpen = false; - } + isDatasetSelected(datasetCode: string): boolean { + return this.selectedDatasets().has(datasetCode); } - } diff --git a/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.css b/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.css deleted file mode 100644 index e25ba9be0..000000000 --- a/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.css +++ /dev/null @@ -1,28 +0,0 @@ -:host { - display: block; - height: 100%; - min-height: 500px; -} - -.distribution-graph-expanded { - position: relative; - flex-grow: 1; - height: 100%; - min-height: 450px; - overflow-x: hidden; - overflow-y: auto; -} - -.distribution-graph-full { - display: flex; - flex-direction: column; - justify-content: center; - align-items: stretch; - width: 100%; - height: 100%; - min-height: 0; - min-width: 0; - padding: 10px; - box-sizing: border-box; - overflow: hidden; -} \ No newline at end of file diff --git a/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.html b/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.html deleted file mode 100644 index acf0e21af..000000000 --- a/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.html +++ /dev/null @@ -1,8 +0,0 @@ -<div class="distribution-graph-expanded"> - - <div class="distribution-graph-full" id="histogram-chart-container"> - @if (data && data.bins && data.counts) { - <app-histogram [data]="data" [config]="{ color: '#2b33e9' }"></app-histogram> - } - </div> -</div> \ No newline at end of file diff --git a/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.ts b/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.ts deleted file mode 100644 index 83d249dc5..000000000 --- a/src/app/pages/experiment-studio/variables-panel/distribution-graph/distribution-graph.component.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Component, Input, inject } from '@angular/core'; -import { HistogramComponent } from '../../visualisations/histogram/histogram.component'; -@Component({ - selector: 'app-distribution-graph', - imports: [HistogramComponent], - templateUrl: './distribution-graph.component.html', - styleUrl: './distribution-graph.component.css' -}) -export class DistributionGraphComponent { - @Input() data: { bins: string[]; counts: number[]; variableName: string } | null = null; - - constructor() { } -} diff --git a/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.css b/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.css deleted file mode 100644 index fadab3139..000000000 --- a/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.css +++ /dev/null @@ -1,413 +0,0 @@ -/* ===== MODAL WRAPPER & OVERLAY ===== */ -.modal-wrapper { - position: fixed; - inset: 0; - z-index: 9500; - display: flex; - justify-content: center; - align-items: center; - animation: fadeIn 0.25s cubic-bezier(0.16, 1, 0.3, 1); -} - -@keyframes fadeIn { - from { - opacity: 0; - } - - to { - opacity: 1; - } -} - -.modal-overlay { - position: absolute; - inset: 0; - background-color: rgba(15, 23, 42, 0.45); - backdrop-filter: blur(6px); - -webkit-backdrop-filter: blur(6px); - z-index: 1; -} - -/* ===== MODAL SHELL ===== */ -.filter-modal { - position: relative; - z-index: 2; - width: 900px; - max-width: 95vw; - max-height: 88vh; - background: #ffffff; - border-radius: 20px; - /* Complex shadow for depth */ - box-shadow: - 0 4px 6px -1px rgba(0, 0, 0, 0.1), - 0 20px 50px -12px rgba(15, 23, 42, 0.3), - 0 0 0 1px rgba(15, 23, 42, 0.05); - display: flex; - flex-direction: column; - overflow: hidden; - max-height: 90vh; - /* Ensure modal never exceeds viewport */ - animation: modalSlide 0.35s cubic-bezier(0.34, 1.56, 0.64, 1); -} - -@keyframes modalSlide { - from { - opacity: 0; - transform: translateY(20px) scale(0.98); - } - - to { - opacity: 1; - transform: translateY(0) scale(1); - } -} - -/* ===== HEADER ===== */ -.filter-modal-header { - display: flex; - justify-content: space-between; - align-items: center; - padding: 18px 24px; - background: #ffffff; - border-bottom: 1px solid #f1f5f9; -} - -.header-left { - display: flex; - align-items: center; - gap: 12px; -} - -.header-icon { - font-size: 1.1rem; - color: var(--primary-color); - background: #f0f4ff; - width: 32px; - height: 32px; - display: flex; - align-items: center; - justify-content: center; - border-radius: 8px; -} - -.filter-modal-header h3 { - margin: 0; - font-size: 1.15rem; - font-weight: 700; - color: #1e293b; - letter-spacing: -0.01em; -} - -.close-top-btn { - background: #f1f5f9; - border: none; - color: #64748b; - width: 32px; - height: 32px; - display: flex; - align-items: center; - justify-content: center; - border-radius: 8px; - cursor: pointer; - transition: all 0.2s ease; -} - -.close-top-btn:hover { - background: #e2e8f0; - color: #0f172a; - transform: rotate(90deg); -} - -/* ===== HELPER TEXT ===== */ -.filter-helper { - background: #f8fafc; - padding: 12px 24px; - border-bottom: 1px solid #f1f5f9; -} - -.helper-content { - display: flex; - align-items: center; - gap: 10px; -} - -.info-icon { - color: var(--primary-color); - font-size: 0.9rem; - opacity: 0.7; -} - -.filter-helper p { - margin: 0; - font-size: 0.88rem; - color: #64748b; - line-height: 1.5; -} - -/* ===== BODY ===== */ -.filter-modal-body { - padding: 24px; - background: #ffffff; - overflow-y: auto; - flex: 1; - /* Take up remaining space */ - min-height: 200px; -} - -/* ===== FOOTER ===== */ -.filter-modal-footer { - display: flex; - justify-content: space-between; - align-items: center; - padding: 16px 24px; - background: #ffffff; - border-top: 1px solid #f1f5f9; -} - -.status-badge { - display: flex; - align-items: center; - gap: 8px; - padding: 4px 12px; - background: #f0fdf4; - color: #166534; - border-radius: 999px; - font-size: 0.82rem; - font-weight: 600; - border: 1px solid rgba(22, 101, 52, 0.1); -} - -.pulse-dot { - width: 6px; - height: 6px; - background: #22c55e; - border-radius: 50%; - animation: pulse-dot 2s infinite; -} - -@keyframes pulse-dot { - 0% { - transform: scale(0.95); - box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7); - } - - 70% { - transform: scale(1); - box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); - } - - 100% { - transform: scale(0.95); - box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); - } -} - -.status-text.muted { - font-size: 0.85rem; - color: #94a3b8; -} - -.filter-actions { - display: flex; - gap: 12px; -} - -.btn-secondary { - background: #f1f5f9; - border: none; - color: #475569; - padding: 10px 20px; - border-radius: 10px; - font-size: 0.9rem; - font-weight: 600; - cursor: pointer; - transition: all 0.2s ease; -} - -.btn-secondary:hover { - background: #e2e8f0; - color: #0f172a; -} - -.btn-primary-gradient { - display: inline-flex; - align-items: center; - gap: 8px; - background: linear-gradient(135deg, var(--primary-color), var(--primary-dark)); - border: none; - color: #ffffff; - padding: 10px 24px; - border-radius: 10px; - font-size: 0.9rem; - font-weight: 600; - cursor: pointer; - box-shadow: 0 4px 12px rgba(43, 51, 233, 0.25); - transition: all 0.2s ease; -} - -.btn-primary-gradient:hover { - transform: translateY(-1px); - box-shadow: 0 6px 16px rgba(43, 51, 233, 0.35); - filter: brightness(1.05); -} - -.icon-left { - font-size: 0.85rem; -} - -/* ===== NGX QUERY BUILDER CUSTOM THEME ===== */ - -:host ::ng-deep .query-builder { - background: transparent !important; - padding: 0 !important; - border: none !important; - box-shadow: none !important; -} - -/* Group Container */ -:host ::ng-deep .query-builder .rules-group-container { - background: #f8fafc !important; - border: 1px solid #e2e8f0 !important; - border-radius: 16px !important; - padding: 20px !important; - margin-bottom: 20px !important; - position: relative; -} - -/* Group Header */ -:host ::ng-deep .query-builder .rules-group-header { - margin-bottom: 16px !important; - display: flex !important; - align-items: center !important; - gap: 12px !important; -} - -/* AND/OR Segmented Control */ -:host ::ng-deep .query-builder .btn-group { - display: flex !important; - background: #e2e8f0 !important; - padding: 3px !important; - border-radius: 10px !important; - gap: 2px !important; -} - -:host ::ng-deep .query-builder .btn-primary { - background: transparent !important; - border: none !important; - color: #64748b !important; - font-size: 0.8rem !important; - font-weight: 700 !important; - text-transform: uppercase !important; - letter-spacing: 0.05em !important; - padding: 6px 14px !important; - border-radius: 8px !important; - margin: 0 !important; - transition: all 0.2s ease !important; -} - -:host ::ng-deep .query-builder .btn-primary.active { - background: #ffffff !important; - color: var(--primary-color) !important; - box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important; -} - -/* Rule Actions (Add buttons) */ -:host ::ng-deep .query-builder .btn-success { - background: #ffffff !important; - border: 1px solid #e2e8f0 !important; - color: #475569 !important; - font-size: 0.85rem !important; - font-weight: 600 !important; - padding: 6px 14px !important; - border-radius: 10px !important; - cursor: pointer !important; - transition: all 0.2s ease !important; - box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05) !important; -} - -:host ::ng-deep .query-builder .btn-success:hover { - background: #f1f5f9 !important; - border-color: #cbd5e1 !important; - color: #0f172a !important; -} - -/* Rule Container */ -:host ::ng-deep .query-builder .rule-container { - background: #ffffff !important; - border: 1px solid #f1f5f9 !important; - border-radius: 12px !important; - padding: 12px 16px !important; - margin-bottom: 10px !important; - display: flex !important; - align-items: center !important; - gap: 12px !important; - box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02) !important; -} - -/* Form Elements */ -:host ::ng-deep .query-builder select, -:host ::ng-deep .query-builder input { - background: #ffffff !important; - border: 1px solid #e2e8f0 !important; - border-radius: 8px !important; - padding: 8px 12px !important; - font-size: 0.9rem !important; - color: #1e293b !important; - transition: all 0.2s ease !important; -} - -:host ::ng-deep .query-builder select:hover, -:host ::ng-deep .query-builder input:hover { - border-color: #cbd5e1 !important; -} - -:host ::ng-deep .query-builder select:focus, -:host ::ng-deep .query-builder input:focus { - outline: none !important; - border-color: var(--primary-color) !important; - box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.1) !important; -} - -/* Remove Button */ -:host ::ng-deep .query-builder .btn-danger { - background: #fee2e2 !important; - color: #dc2626 !important; - width: 28px !important; - height: 28px !important; - display: flex !important; - align-items: center !important; - justify-content: center !important; - border-radius: 8px !important; - font-size: 0.8rem !important; - border: none !important; - cursor: pointer !important; - transition: all 0.2s ease !important; -} - -:host ::ng-deep .query-builder .btn-danger:hover { - background: #fecaca !important; - transform: scale(1.05) !important; -} - -/* Numeric Range Inputs */ -.numeric-input-container { - display: flex; - align-items: center; -} - -.between-inputs { - display: flex; - align-items: center; - gap: 8px; -} - -.between-inputs input { - width: 80px !important; -} - -.and-separator { - font-size: 0.85rem; - color: #64748b; - font-weight: 500; - text-transform: lowercase; -} \ No newline at end of file diff --git a/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.html b/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.html deleted file mode 100644 index 911b9172e..000000000 --- a/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.html +++ /dev/null @@ -1,77 +0,0 @@ -<div class="modal-wrapper"> - <div class="modal-overlay" (click)="cancel()"></div> - - <div class="filter-modal"> - <div class="filter-modal-header"> - <div class="header-left"> - <i class="fas fa-filter header-icon"></i> - <h3>Filters & Formula</h3> - </div> - <button class="close-top-btn" (click)="cancel()" aria-label="Close modal"> - <i class="fas fa-times"></i> - </button> - </div> - - <div class="filter-helper"> - <div class="helper-content"> - <i class="fas fa-info-circle info-icon"></i> - <p>Define rules to filter your dataset before running the experiment. Combined rules form the final Cohort - Selection.</p> - </div> - </div> - - <div class="filter-modal-body"> - @if(config().fields && Object.keys(config().fields).length) { - <ngx-query-builder [config]="config()" [ngModel]="filterLogicModel()" - (ngModelChange)="filterLogicModel.set($event)" [attr.key]="configKey()"> - <ng-container *queryOperator="let rule; let operators=operators"> - <select [(ngModel)]="rule.operator" (ngModelChange)="filterLogicModel.set(filterLogicModel())" - [class.q-control]="true" [style.min-width.px]="120"> - <option *ngFor="let value of operators" [value]="value"> - {{ getOperatorLabel(value) }} - </option> - </select> - </ng-container> - - <ng-container *queryInput="let rule; type: 'number'"> - <div class="numeric-input-container"> - @if (rule.operator === 'between' || rule.operator === 'not_between') { - <div class="between-inputs"> - <input type="number" [(ngModel)]="rule.value[0]" - (ngModelChange)="filterLogicModel.set(filterLogicModel())"> - <span class="and-separator">and</span> - <input type="number" [(ngModel)]="rule.value[1]" - (ngModelChange)="filterLogicModel.set(filterLogicModel())"> - </div> - } @else { - <input type="number" [(ngModel)]="rule.value" (ngModelChange)="filterLogicModel.set(filterLogicModel())"> - } - </div> - </ng-container> - </ngx-query-builder> - - } - </div> - - <div class="filter-modal-footer"> - <div class="footer-left"> - @if (activeRulesCount() > 0) { - <div class="status-badge"> - <span class="pulse-dot"></span> - {{ activeRulesCount() }} Active Rule{{ activeRulesCount() === 1 ? "" : "s" }} - </div> - } @else { - <span class="status-text muted">No filters applied</span> - } - </div> - - <div class="filter-actions"> - <button class="btn-secondary" (click)="cancel()">Cancel</button> - <button class="btn-primary-gradient" (click)="saveFilters()"> - <i class="fas fa-check icon-left"></i> - Apply Filters - </button> - </div> - </div> - </div> -</div> diff --git a/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.ts b/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.ts deleted file mode 100644 index 878a434fe..000000000 --- a/src/app/pages/experiment-studio/variables-panel/filter-config-modal/filter-config-modal.component.ts +++ /dev/null @@ -1,247 +0,0 @@ -import { EnumValue } from '../../../../models/data-model.interface'; -import { Component, Output, EventEmitter, OnInit, effect, signal, computed, Input, SimpleChanges, OnChanges } from '@angular/core'; -import { QueryBuilderConfig, QueryBuilderModule } from "@kerwin612/ngx-query-builder"; -import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; -import { CommonModule } from '@angular/common'; -import { FormsModule } from '@angular/forms'; - -type SupportedType = 'real' | 'integer' | 'nominal'; - -@Component({ - selector: 'app-filter-config-modal', - imports: [CommonModule, FormsModule, QueryBuilderModule], - templateUrl: './filter-config-modal.component.html', - styleUrls: ['./filter-config-modal.component.css'] -}) -export class FilterConfigModalComponent implements OnInit, OnChanges { - @Input() filterLogic: any | null = null; - @Output() closeModal = new EventEmitter<void>(); - - // Variables to be filtered - filters = signal<any[]>([]); - - // query builder - filterLogicModel = signal<{ condition: string; rules: any[] }>({ condition: 'AND', rules: [] }); - activeRulesCount = computed(() => this.countRules(this.filterLogicModel())); - - private countRules(node: any): number { - if (!node || !Array.isArray(node.rules)) return 0; - - let count = 0; - for (const r of node.rules) { - if (r?.condition && Array.isArray(r.rules)) { - // group - count += this.countRules(r); - } else { - // leaf rule - count += 1; - } - } - return count; - } - - // Config for QueryBuilder object - config = signal<QueryBuilderConfig>({ fields: {} }); - - // Operator sets per variable type - private operatorOptions: Record<SupportedType, string[]> = { - real: ['equal', 'not_equal', 'less', 'less_or_equal', 'greater', 'greater_or_equal', 'between', 'not_between'], - integer: ['equal', 'not_equal', 'less', 'less_or_equal', 'greater', 'greater_or_equal', 'between', 'not_between'], - nominal: ['equal', 'not_equal'], - }; - - // Human readable labels - operatorLabels: Record<string, string> = { - equal: 'Equal', - not_equal: 'Not Equal', - less: 'Less', - less_or_equal: 'Less or Equal', - greater: 'Greater', - greater_or_equal: 'Greater or Equal', - between: 'Between', - not_between: 'Not Between', - in: 'In', - not_in: 'Not In', - contains: 'Contains', - not_contains: 'Not Contains', - begins_with: 'Begins With', - ends_with: 'Ends With', - is_empty: 'Is Empty', - is_not_empty: 'Is Not Empty', - is_null: 'Is Null', - is_not_null: 'Is Not Null' - }; - - Object = Object; - - configKey = computed(() => Object.keys(this.config().fields || {}).join('-')); - - constructor(private expStudio: ExperimentStudioService) { - effect(() => { - const available = this.expStudio.selectedFilters(); - - // If no filter variables cleans up UI and service - if (!available || available.length === 0) { - this.filters.set([]); - this.config.set({ fields: {} }); - this.filterLogicModel.set({ condition: 'AND', rules: [] }); // reset UI state - this.expStudio.setFilterLogic(null); // reset service - return; - } - - // If variables exist build config - const builtConfig = this.buildFieldsConfig(available); - this.filters.set(available); - this.config.set({ - fields: builtConfig, - allowEmptyRulesets: true, - coerceValueForOperator: (operator, value, rule) => { - if (operator === 'between' || operator === 'not_between') { - if (!Array.isArray(value)) { - return [value || 0, value || 0]; - } - if (value.length !== 2) { - return [value[0] || 0, value[1] || 0]; - } - } - return value; - } - }); - - - }, { allowSignalWrites: true }); - } - - ngOnChanges(changes: SimpleChanges): void { - if (changes['filterLogic']) { - const incoming = changes['filterLogic'].currentValue; - if (incoming && Array.isArray(incoming.rules)) { - this.filterLogicModel.set(structuredClone(incoming)); - } else { - this.filterLogicModel.set({ condition: 'AND', rules: [] }); - } - } - } - - getOperatorLabel(op: string): string { - if (this.operatorLabels[op]) { - return this.operatorLabels[op]; - } - // Fallback: title case replacing underscores - return op.split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' '); - } - - ngOnInit() { - } - - // Turns enums to options - private toOptions(values: EnumValue[] | string[]): { name: string; value: any }[] { - if (!values || values.length === 0) return []; - if (typeof values[0] === 'string') { - return (values as string[]).map(v => ({ name: v, value: v })); - } - return (values as EnumValue[]).map(v => ({ name: v.label, value: v.code })); - } - - // Creates for Query Builder - private buildFieldsConfig(filters: any[]): Record<string, any> { - const fields: Record<string, any> = {}; - if (!filters?.length) return fields; - - for (const f of filters) { - const ops = this.operatorOptions[f.type as SupportedType] ?? ['equal', 'not_equal']; - const enums = f.type === 'nominal' ? f.enumerations ?? [] : undefined; - - fields[f.code] = { - name: f.label ?? f.name ?? f.code, - type: (f.type === 'real' || f.type === 'integer') ? 'number' : 'category', - operators: ops, - options: - f.type === 'nominal' && enums?.length - ? this.toOptions(enums) - : undefined, - }; - } - return fields; - } - - private formatFiltersForBackend(rawLogic: { condition: string; rules: any[] }): any { - if (!rawLogic || !Array.isArray(rawLogic.rules) || rawLogic.rules.length === 0) { - return null; - } - - const normalizeRule = (r: any): any => { - // Group - if (r?.condition && Array.isArray(r.rules)) { - return { - condition: String(r.condition).toUpperCase(), // Upper case - rules: r.rules.map(normalizeRule), - }; - } - - // Leaf - const fieldKey = r.field ?? r.id; - const op = r.operator; - let val = r.value; - - if ((op === 'in' || op === 'not_in') && !Array.isArray(val)) val = [val]; - - return { - id: fieldKey, - field: fieldKey, - type: this.detectType(fieldKey), // 'string' | 'integer' | 'real' - input: this.detectInput(fieldKey), // 'number' | 'select' | 'text' - operator: op, - value: val, - entity: undefined, - }; - }; - - // Root to upper case - const formatted = { - condition: String(rawLogic.condition || 'AND').toUpperCase(), - rules: rawLogic.rules.map(normalizeRule), - valid: true, - }; - - return formatted; - } - - // turns internal UI types to backend types - private detectType(field: string): 'string' | 'integer' | 'real' { - const f = this.filters().find(v => v.code === field); - if (!f) return 'real'; - if (f.type === 'integer') return 'integer'; - if (f.type === 'real') return 'real'; - // nominal/text -> backend "string" - return 'string'; - } - - private detectInput(field: string): 'text' | 'number' | 'select' { - const f = this.filters().find(v => v.code === field); - if (!f) return 'text'; - if (f.type === 'integer' || f.type === 'real') return 'number'; - if (f.type === 'nominal') return 'select'; - return 'text'; - } - - // Saves logic in service - saveFilters() { - const raw = this.filterLogicModel(); - const formatted = this.formatFiltersForBackend(raw); - - const toStore = - formatted && Array.isArray(formatted.rules) && formatted.rules.length > 0 - ? formatted - : null; - - this.expStudio.setFilterLogic(toStore); - this.closeModal.emit(); - } - - - // Closes modal without saving edits - cancel() { - this.closeModal.emit(); - } -} diff --git a/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.css b/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.css new file mode 100644 index 000000000..7c252cb2d --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.css @@ -0,0 +1,16 @@ +:host { + display: block; +} + +.histogram-graph-expanded { + position: relative; + overflow: visible; +} + +.histogram-graph-full { + width: 100%; + min-width: 0; + padding: 4px 4px 0; + box-sizing: border-box; + overflow: visible; +} diff --git a/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.html b/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.html new file mode 100644 index 000000000..a52e70d8c --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.html @@ -0,0 +1,10 @@ +<div class="histogram-graph-expanded"> + + <div class="histogram-graph-full" id="histogram-chart-container"> + @if (data(); as histogramData) { + @if (histogramData.bins && histogramData.counts) { + <app-histogram [data]="histogramData" [config]="{ color: '#2b33e9', orientation: horizontal() ? 'horizontal' : 'vertical' }"></app-histogram> + } + } + </div> +</div> diff --git a/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.ts b/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.ts new file mode 100644 index 000000000..5104bd4b9 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/histogram-graph/histogram-graph.component.ts @@ -0,0 +1,14 @@ +import { ChangeDetectionStrategy, Component, input } from '@angular/core'; +import { HistogramComponent } from '../../visualisations/histogram/histogram.component'; +import { HistogramChartData } from '../../visualisations/histogram/histogram-chart'; +@Component({ + selector: 'app-histogram-graph', + imports: [HistogramComponent], + templateUrl: './histogram-graph.component.html', + styleUrl: './histogram-graph.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class HistogramGraphComponent { + readonly data = input<HistogramChartData | null>(null); + readonly horizontal = input(false); +} diff --git a/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.css b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.css new file mode 100644 index 000000000..063c65d68 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.css @@ -0,0 +1,232 @@ +:host { + display: block; + min-width: 0; +} + +.metadata-info-panel { + display: flex; + flex-direction: column; + gap: 10px; + min-width: 0; + padding: 2px 2px 4px; +} + +.metadata-empty { + display: grid; + gap: 10px; + place-items: center; + min-height: 180px; + color: #64748b; + text-align: center; +} + +.metadata-empty i { + color: var(--primary-color); + font-size: 1.4rem; +} + +.metadata-info-header { + display: flex; + flex-direction: column; + gap: 6px; + min-width: 0; + padding-bottom: 12px; + border-bottom: 1px solid rgba(148, 163, 184, 0.18); +} + +.metadata-info-header h3 { + margin: 0; + color: var(--text-main); + font-size: 1.05rem; + font-weight: 800; + line-height: 1.25; +} + +.metadata-description, +.metadata-long-text { + margin: 0; + color: var(--chart-label-color); + font-size: 0.82rem; + line-height: 1.5; +} + +.metadata-kind { + width: fit-content; + padding: 2px 7px; + border-radius: 6px; + font-size: 0.68rem; + font-weight: 800; + letter-spacing: 0.04em; + text-transform: uppercase; + background: #eef2ff; + border: 1px solid rgba(43, 51, 233, 0.18); + color: var(--primary-color); +} + +.metadata-section { + display: flex; + flex-direction: column; + min-width: 0; +} + +.metadata-block { + display: flex; + flex-direction: column; + gap: 8px; + min-width: 0; + padding: 10px 12px; + border-radius: var(--radius-md, 8px); + background: var(--studio-card-header-bg); + border: 1px solid var(--studio-card-header-border, rgba(43, 51, 233, 0.08)); +} + +.metadata-block h4 { + margin: 0; + color: #64748b; + font-size: 0.68rem; + font-weight: 800; + letter-spacing: 0.05em; + text-transform: uppercase; +} + +.metadata-path { + display: flex; + flex-wrap: wrap; + align-items: baseline; + gap: 2px 4px; + min-width: 0; +} + +.metadata-path span { + display: inline; + max-width: 100%; + color: var(--text-muted); + font-size: 0.76rem; + font-weight: 500; + line-height: 1.45; + word-break: break-word; +} + +.metadata-path span.current { + color: var(--primary-color); + font-weight: 700; +} + +.metadata-path i { + flex-shrink: 0; + align-self: center; + color: var(--primary-light); + font-size: 0.5rem; + opacity: 0.85; + margin: 0 1px; +} + +.metadata-field-grid, +.metadata-metric-grid { + display: grid; + grid-template-columns: minmax(88px, 34%) 1fr; + gap: 0; + min-width: 0; + margin: 0; + padding: 0; + border: none; + border-radius: 0; + background: transparent; +} + +.metadata-field-grid dt, +.metadata-field-grid dd, +.metadata-metric-grid dt, +.metadata-metric-grid dd { + margin: 0; + min-width: 0; + padding: 8px 0; + border-bottom: 1px solid rgba(226, 232, 240, 0.75); +} + +.metadata-field-grid dt:last-of-type, +.metadata-field-grid dd:last-of-type, +.metadata-metric-grid dt:last-of-type, +.metadata-metric-grid dd:last-of-type { + border-bottom: none; + padding-bottom: 0; +} + +.metadata-field-grid dt:first-of-type, +.metadata-field-grid dd:first-of-type, +.metadata-metric-grid dt:first-of-type, +.metadata-metric-grid dd:first-of-type { + padding-top: 0; +} + +.metadata-field-grid dt, +.metadata-metric-grid dt { + padding-right: 12px; + color: #64748b; + font-size: 0.66rem; + font-weight: 700; + letter-spacing: 0.04em; + text-transform: uppercase; + line-height: 1.45; +} + +.metadata-field-grid dd, +.metadata-metric-grid dd { + overflow-wrap: anywhere; + color: var(--text-main); + font-size: 0.8rem; + font-weight: 600; + line-height: 1.45; +} + +.metadata-metric-grid dd { + font-variant-numeric: tabular-nums; + color: var(--primary-color); + font-weight: 700; +} + +.enumeration-list { + display: flex; + flex-direction: column; + gap: 0; + padding: 0; + margin: 0; + list-style: none; +} + +.enumeration-list li { + min-width: 0; + padding: 6px 0 6px 14px; + color: var(--chart-label-color); + font-size: 0.8rem; + font-weight: 500; + line-height: 1.4; + overflow-wrap: anywhere; + position: relative; + border-bottom: 1px solid rgba(226, 232, 240, 0.85); +} + +.enumeration-list li::before { + content: ''; + position: absolute; + left: 0; + top: 0.85em; + width: 5px; + height: 5px; + border-radius: 50%; + background: var(--primary-color); + opacity: 0.55; +} + +.enumeration-list li:last-child { + border-bottom: none; + padding-bottom: 0; +} + +.enumeration-list li:first-child { + padding-top: 0; +} + +.enumeration-list li:first-child::before { + top: 0.55em; +} diff --git a/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.html b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.html new file mode 100644 index 000000000..2ada729c6 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.html @@ -0,0 +1,90 @@ +<div class="metadata-info-panel"> + @if (!selectedNode()) { + <div class="metadata-empty"> + <i class="fas fa-info-circle" aria-hidden="true"></i> + <p>Select a group or variable to inspect its metadata.</p> + </div> + } @else { + @if (!hideHeader()) { + <header class="metadata-info-header"> + <span class="metadata-kind">{{ isGroup() ? 'Group' : 'Variable' }}</span> + <h3>{{ selectedNode()?.label }}</h3> + @if (selectedNode()?.description) { + <p class="metadata-description">{{ selectedNode()?.description }}</p> + } + </header> + } + + @if (pathNodes().length) { + <section class="metadata-section" aria-label="Hierarchy path"> + <div class="metadata-block"> + <h4>Path</h4> + <div class="metadata-path"> + @for (node of pathNodes(); track node.code + ':' + node.label; let last = $last) { + <span [class.current]="last">{{ node.label }}</span> + @if (!last) { + <i class="fas fa-chevron-right" aria-hidden="true"></i> + } + } + </div> + </div> + </section> + } + + @if (isGroup()) { + <section class="metadata-section" aria-label="Group summary"> + <div class="metadata-block"> + <h4>Summary</h4> + <dl class="metadata-metric-grid"> + <dt>Child groups</dt> + <dd>{{ directGroupCount() }}</dd> + <dt>Direct variables</dt> + <dd>{{ directVariableCount() }}</dd> + <dt>Total variables</dt> + <dd>{{ totalVariableCount() }}</dd> + @if (groupInfo()?.hasGroups) { + <dt>Chart groups</dt> + <dd>{{ groupInfo()?.groupCount }}</dd> + } + </dl> + </div> + </section> + } @else { + @if (fields().length) { + <section class="metadata-section" aria-label="Variable metadata"> + <div class="metadata-block"> + <h4>Metadata</h4> + <dl class="metadata-field-grid"> + @for (field of fields(); track field.label) { + <dt>{{ field.label }}</dt> + <dd>{{ field.value }}</dd> + } + </dl> + </div> + </section> + } + + @if (selectedNode()?.methodology) { + <section class="metadata-section" aria-label="Methodology"> + <div class="metadata-block"> + <h4>Methodology</h4> + <p class="metadata-long-text">{{ selectedNode()?.methodology }}</p> + </div> + </section> + } + + @if (enumerations().length) { + <section class="metadata-section" aria-label="Enumerations"> + <div class="metadata-block"> + <h4>Enumerations</h4> + <ul class="enumeration-list"> + @for (entry of enumerations(); track entry.code ?? entry.label) { + <li>{{ entry.label }}</li> + } + </ul> + </div> + </section> + } + } + } +</div> diff --git a/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.spec.ts b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.spec.ts new file mode 100644 index 000000000..61422abf0 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.spec.ts @@ -0,0 +1,119 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { MetadataInfoPanelComponent } from './metadata-info-panel.component'; + +describe('MetadataInfoPanelComponent', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [MetadataInfoPanelComponent], + providers: [provideZonelessChangeDetection()], + }); + }); + + it('renders enumeration labels instead of raw codes', () => { + const fixture = TestBed.createComponent(MetadataInfoPanelComponent); + fixture.componentRef.setInput('selectedNode', { + label: 'Hemisphere', + code: 'hemisphere', + type: 'nominal', + enumerations: [ + { code: 'right', label: 'Right' }, + { code: 'left', label: 'Left' }, + { code: 'bilateral', label: 'Bilateral' }, + { code: 'unknown', label: 'Unknown' }, + ], + }); + fixture.detectChanges(); + + const items = Array.from<HTMLElement>( + fixture.nativeElement.querySelectorAll('.enumeration-list li') + ).map((el) => el.textContent?.trim() ?? ''); + + expect(items).toEqual(['Right', 'Left', 'Bilateral', 'Unknown']); + }); + + it('humanizes enumeration codes when labels are missing', () => { + const fixture = TestBed.createComponent(MetadataInfoPanelComponent); + fixture.componentRef.setInput('selectedNode', { + label: 'Hemisphere', + code: 'hemisphere', + type: 'nominal', + enumerations: [ + { code: 'right' }, + { code: 'left' }, + { code: 'bilateral' }, + { code: 'unknown' }, + ], + }); + fixture.detectChanges(); + + const items = Array.from<HTMLElement>( + fixture.nativeElement.querySelectorAll('.enumeration-list li') + ).map((el) => el.textContent?.trim() ?? ''); + + expect(items).toEqual(['Right', 'Left', 'Bilateral', 'Unknown']); + }); + + it('renders variable metadata and enumeration labels without raw variable codes', () => { + const fixture = TestBed.createComponent(MetadataInfoPanelComponent); + fixture.componentRef.setInput('selectedNode', { + label: 'Anaesthesia type', + code: 'acute_treat_evt_anaesth', + description: 'Type of anesthesia', + type: 'nominal', + units: 'N/A', + methodology: 'Clinical abstraction', + enumerations: [ + { code: 'secret-general-code', label: 'general anesthesia' }, + ], + }); + fixture.componentRef.setInput('pathNodes', [ + { code: 'stroke', label: 'Stroke 3.7' }, + { code: 'acute', label: 'Acute treatment' }, + { code: 'anaesthesia', label: 'Anaesthesia type' }, + ]); + fixture.detectChanges(); + + const text = fixture.nativeElement.textContent; + expect(text).toContain('Anaesthesia type'); + expect(text).toContain('Type of anesthesia'); + expect(text).toContain('nominal'); + expect(text).toContain('N/A'); + expect(text).toContain('Clinical abstraction'); + expect(text).toContain('general anesthesia'); + expect(text).not.toContain('Value'); + expect(text).not.toContain('SQL type'); + expect(text).not.toContain('Categorical'); + expect(text).not.toContain('text'); + expect(text).not.toContain('acute_treat_evt_anaesth'); + expect(text).not.toContain('secret-general-code'); + }); + + it('renders selected group summary metadata', () => { + const fixture = TestBed.createComponent(MetadataInfoPanelComponent); + fixture.componentRef.setInput('selectedNode', { + label: 'Vitals', + code: 'vitals', + children: [ + { label: 'Blood pressure', code: 'bp', type: 'continuous' }, + { + label: 'Metabolic', + code: 'metabolic', + children: [ + { label: 'Glucose', code: 'glucose', type: 'continuous' }, + ], + }, + ], + }); + fixture.componentRef.setInput('groupInfo', { groupCount: 2, hasGroups: true }); + fixture.detectChanges(); + + const text = fixture.nativeElement.textContent; + expect(text).toContain('Group'); + expect(text).toContain('Vitals'); + expect(text).toContain('Child groups'); + expect(text).toContain('Direct variables'); + expect(text).toContain('Total variables'); + expect(text).toContain('Chart groups'); + }); +}); diff --git a/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.ts b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.ts new file mode 100644 index 000000000..5f44ab494 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/metadata-info-panel/metadata-info-panel.component.ts @@ -0,0 +1,159 @@ +import { ChangeDetectionStrategy, Component, computed, input } from '@angular/core'; +import { D3HierarchyNode } from '../../../../models/data-model.interface'; +import { countLeafNodes } from '../../../../core/data-model.utils'; + +interface MetadataPathNode { + code: string; + label: string; +} + +interface MetadataGroupInfo { + groupCount: number; + hasGroups: boolean; +} + +interface MetadataField { + label: string; + value: string; +} + +interface EnumerationEntry { + label: string; + code?: string; +} + +@Component({ + selector: 'app-metadata-info-panel', + templateUrl: './metadata-info-panel.component.html', + styleUrl: './metadata-info-panel.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class MetadataInfoPanelComponent { + readonly selectedNode = input<D3HierarchyNode | null>(null); + readonly pathNodes = input<MetadataPathNode[]>([]); + readonly groupInfo = input<MetadataGroupInfo | null>(null); + readonly hideHeader = input(false); + + readonly isGroup = computed(() => !!this.selectedNode()?.children?.length); + readonly directGroupCount = computed(() => this.children().filter((child) => !!child.children?.length).length); + readonly directVariableCount = computed(() => this.children().filter((child) => !child.children?.length).length); + readonly totalVariableCount = computed(() => this.children().reduce((total, child) => total + countLeafNodes(child), 0)); + readonly enumerations = computed(() => this.resolveEnumerationEntries(this.selectedNode()?.enumerations)); + readonly fields = computed<MetadataField[]>(() => { + const node = this.selectedNode(); + if (!node || this.isGroup()) return []; + + return [ + { label: 'Type', value: this.normalizeString(node.type) }, + { label: 'Units', value: this.normalizeString(node.units) }, + { label: 'Minimum', value: this.normalizeString(node.minValue) }, + { label: 'Maximum', value: this.normalizeString(node.maxValue) }, + ].filter((field) => field.value); + }); + + private children(): D3HierarchyNode[] { + return this.selectedNode()?.children ?? []; + } + + + private resolveEnumerationEntries(value: unknown): EnumerationEntry[] { + if (!Array.isArray(value) || !value.length) { + return []; + } + + const codeToLabel = this.buildEnumerationLabelMap(value); + const seen = new Set<string>(); + + return value + .map((entry) => this.resolveEnumerationEntry(entry, codeToLabel)) + .filter((entry): entry is EnumerationEntry => !!entry?.label) + .filter((entry) => { + const key = `${entry.code ?? ''}:${entry.label}`; + if (seen.has(key)) { + return false; + } + seen.add(key); + return true; + }); + } + + private buildEnumerationLabelMap(enumerations: unknown[]): Map<string, string> { + const map = new Map<string, string>(); + + enumerations.forEach((entry) => { + if (!entry || typeof entry !== 'object') { + return; + } + + const record = entry as Record<string, unknown>; + const code = this.normalizeString(record['code'] ?? record['value']); + const label = this.normalizeString(record['label'] ?? record['name']); + if (!code && !label) { + return; + } + + const displayLabel = this.preferredEnumerationLabel(label, code); + if (code) { + map.set(code, displayLabel); + } + if (label && label !== code) { + map.set(label, displayLabel); + } + }); + + return map; + } + + private resolveEnumerationEntry( + entry: unknown, + codeToLabel: Map<string, string> + ): EnumerationEntry | null { + if (entry && typeof entry === 'object') { + const record = entry as Record<string, unknown>; + const code = this.normalizeString(record['code'] ?? record['value']); + const rawLabel = this.normalizeString(record['label'] ?? record['name']); + const label = rawLabel + ? this.preferredEnumerationLabel(rawLabel, code) + : code + ? codeToLabel.get(code) ?? this.humanizeEnumCode(code) + : ''; + + if (!label) { + return null; + } + + return { label, code: code || undefined }; + } + + const code = this.normalizeString(entry); + if (!code) { + return null; + } + + const label = codeToLabel.get(code) ?? this.humanizeEnumCode(code); + return { label, code }; + } + + private preferredEnumerationLabel(label: string, code: string): string { + if (!label) { + return code ? this.humanizeEnumCode(code) : ''; + } + if (!code || label.toLowerCase() !== code.toLowerCase()) { + return label; + } + return this.humanizeEnumCode(code); + } + + private humanizeEnumCode(code: string): string { + const normalized = code.replace(/[_-]+/g, ' ').trim(); + if (!normalized) { + return ''; + } + return normalized.charAt(0).toUpperCase() + normalized.slice(1); + } + + private normalizeString(value: unknown): string { + if (value === null || value === undefined) return ''; + return String(value).trim(); + } +} diff --git a/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.css b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.css index ee881384d..f5812ed94 100644 --- a/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.css +++ b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.css @@ -1,14 +1,15 @@ :host { --sb-bg: #ffffff; - --sb-border: #d1e4ea; - --sb-shadow: 0 4px 12px rgba(15, 23, 42, 0.08); - --sb-shadow-focus: 0 8px 20px rgba(15, 23, 42, 0.12); - --sb-text: #0f172a; - --sb-text-muted: #64748b; - --sb-accent: var(--primary-color, #2b33e9); - --sb-accent-faint: rgba(43, 51, 233, 0.05); - --sb-radius: 999px; + --sb-text: var(--text-main); + --sb-text-muted: var(--text-muted); + --sb-accent: var(--primary-color); + --sb-accent-light: var(--primary-light); + --sb-radius-sm: var(--radius-sm, 4px); + --sb-radius-md: var(--radius-md, 8px); + --sb-radius-lg: var(--radius-lg, 12px); --sb-transition: 0.15s cubic-bezier(0.4, 0, 0.2, 1); + display: block; + width: 100%; } .search-container { @@ -17,239 +18,439 @@ margin: 0; display: flex; flex-direction: column; - gap: 2px; z-index: 100; - overflow: visible; + background: var(--sb-bg); } -/* Main pill */ -.search-input-container { +/* Top Search Input Header */ +.search-header-row { display: flex; align-items: center; gap: 8px; - border-radius: var(--sb-radius); - border: 1px solid var(--sb-border); - padding: 2px 10px; - background: var(--sb-bg); - box-shadow: var(--sb-shadow); - transition: all var(--sb-transition); - width: 100%; - min-height: 32px; + min-width: 0; + margin: 6px; + width: calc(100% - 12px); + padding: 7px 12px; + box-sizing: border-box; + background: var(--bg-neutral); + border: 1.5px solid rgba(43, 51, 233, 0.28); + border-radius: var(--sb-radius-lg); + box-shadow: 0 1px 4px rgba(43, 51, 233, 0.08); + transition: border-color var(--sb-transition), box-shadow var(--sb-transition), background var(--sb-transition); } -.search-input-container:focus-within { +.search-header-row:focus-within { + background: #ffffff; border-color: var(--sb-accent); - box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.15), var(--sb-shadow-focus); + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.14); } -/* Text input */ -#search-bar { - flex: 1; - padding: 2px 0; - font-size: 0.82rem; - border: none; - outline: none; - color: var(--sb-text); - background: transparent; +.search-icon { + flex-shrink: 0; + font-size: 0.85rem; + color: var(--sb-accent); +} + +:host .search-input, +.search-input { + flex: 1 1 auto; min-width: 0; + width: 100%; + min-height: unset !important; + height: auto !important; + padding: 3px 0 !important; + margin: 0 !important; + border: none !important; + border-radius: 0 !important; + outline: none !important; + background: transparent !important; + box-shadow: none !important; + font-family: inherit; + font-size: 0.92rem; + font-weight: 500; + color: var(--sb-text); + line-height: 1.2; +} + +:host .search-input:hover, +:host .search-input:focus, +.search-input:hover, +.search-input:focus { + border: none !important; + outline: none !important; + box-shadow: none !important; + background: transparent !important; } -#search-bar::placeholder { - color: var(--sb-text-muted); - opacity: 0.6; +.search-input::placeholder { + color: #94a3b8; + font-weight: 400; } -/* Clear button */ +/* Clear Button */ .clear-btn { - background: none; + display: inline-flex; + align-items: center; + justify-content: center; + width: 18px; + height: 18px; + padding: 0; border: none; - font-size: 1.1rem; - color: var(--sb-text-muted); + border-radius: 50%; + background: #f1f5f9; + color: #64748b; + font-size: 0.65rem; cursor: pointer; - padding: 0 2px; + flex-shrink: 0; + transition: all var(--sb-transition); +} + +.clear-btn:hover, +.clear-btn:focus-visible { + background: #fee2e2; + color: #dc2626; + outline: none; +} + +/* Edge-to-Edge Divider */ +.search-divider { + height: 1px; + width: 100%; + background: rgba(226, 232, 240, 0.9); + flex-shrink: 0; +} + +/* Suggestions Container */ +.suggestions-container { + display: flex; + flex-direction: column; + width: 100%; +} + +/* Toolbar & Filters */ +.search-toolbar { + display: flex; + flex-direction: column; + gap: 6px; + padding: 8px 12px 6px; + border-bottom: 1px solid rgba(226, 232, 240, 0.85); + background: #ffffff; +} + +.search-toolbar-main { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; +} + +/* Segmented Control for Variables vs Groups */ +.search-kind-segment { + --segment-active-index: 0; + --segment-count: 2; + --segment-pad: 2px; + --segment-gap: 2px; + box-sizing: border-box; + position: relative; + display: grid; + grid-template-columns: repeat(var(--segment-count), minmax(min-content, 1fr)); + gap: var(--segment-gap); + flex: 0 0 auto; + padding: var(--segment-pad); + isolation: isolate; + border: 1px solid rgba(148, 163, 184, 0.25); + border-radius: 999px; + background: #f1f5f9; +} + +.search-segment-track { + position: absolute; + inset: var(--segment-pad); + z-index: 0; + pointer-events: none; + overflow: hidden; + border-radius: 999px; +} + +.search-segment-indicator { + box-sizing: border-box; + position: absolute; + top: 0; + bottom: 0; + left: 0; + width: calc((100% - (var(--segment-count) - 1) * var(--segment-gap)) / var(--segment-count)); + border-radius: 999px; + background: #ffffff; + border: 1px solid rgba(43, 51, 233, 0.18); + box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08); + transform: translate3d( + calc(var(--segment-active-index) * (100% + var(--segment-gap))), + 0, + 0 + ); + transition: transform 0.25s var(--ease-standard); +} + +.search-segment-btn { + position: relative; + z-index: 1; display: inline-flex; align-items: center; justify-content: center; + gap: 5px; + min-height: 26px; + margin: 0; + padding: 3px 12px; + border: 0; + border-radius: 999px; + background: transparent; + color: #64748b; + cursor: pointer; + font-family: inherit; + font-size: 0.74rem; + font-weight: 600; + letter-spacing: 0.01em; + line-height: 1.2; transition: color var(--sb-transition); } -.clear-btn:hover { +.search-segment-btn i { + font-size: 0.7rem; +} + +.search-segment-btn:hover, +.search-segment-btn:focus-visible { color: var(--sb-accent); + outline: none; } -/* Inline filters */ -.inline-filters { +.search-segment-btn.active { + color: var(--sb-accent); + font-weight: 700; +} + +.search-results-counter { + font-size: 0.72rem; + font-weight: 600; + color: #64748b; + font-variant-numeric: tabular-nums; + white-space: nowrap; +} + +/* Variable Type Filter Chips */ +.search-type-chips { display: flex; + flex-wrap: wrap; align-items: center; - gap: 6px; - padding: 2px; - background-color: var(--sb-accent-faint); - border-radius: var(--sb-radius); + gap: 5px; } -.inline-filters select, -.additional-filter select { - padding: 1px 6px; - font-size: 0.68rem; +.search-type-chip { + display: inline-flex; + align-items: center; + padding: 3px 9px; + border: 1px solid rgba(43, 51, 233, 0.14); + border-radius: 999px; + background: var(--bg-neutral); + color: var(--text-muted); + font-family: inherit; + font-size: 0.7rem; font-weight: 600; - color: var(--sb-text-muted); - border: 1px solid var(--sb-border); - border-radius: var(--sb-radius); - background-color: var(--sb-bg); + line-height: 1.2; cursor: pointer; - height: 22px; + text-transform: lowercase; transition: all var(--sb-transition); - outline: none; } -.inline-filters select:hover, -.additional-filter select:hover { - border-color: var(--sb-accent); +.search-type-chip:hover, +.search-type-chip:focus-visible { + border-color: rgba(43, 51, 233, 0.35); + background: #ffffff; color: var(--sb-accent); + outline: none; } -.inline-filters select:focus, -.additional-filter select:focus { +.search-type-chip.is-active { + background: var(--sb-accent); border-color: var(--sb-accent); - box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.1); + color: #ffffff; + box-shadow: 0 1px 3px rgba(43, 51, 233, 0.25); } -/* Suggestions dropdown */ -.suggestions-container { - position: absolute; - top: calc(100% + 8px); - left: 0; - width: 100%; - max-height: 400px; - overflow-y: auto; - background: var(--sb-bg); - box-shadow: 0 12px 32px rgba(15, 23, 42, 0.2); - border-radius: 12px; - z-index: 2000; - /* High z-index to stay above charts */ - border: 1px solid var(--sb-border); -} - -.suggestions-container ul { +/* Results List */ +.search-results-list { list-style: none; margin: 0; - padding: 0; + padding: 4px 6px; + max-height: 250px; + overflow-y: auto; + display: flex; + flex-direction: column; + gap: 2px; + background: #ffffff; } -.suggestions-container li { - padding: 8px 12px; - font-size: 0.85rem; - color: var(--sb-text); +.search-result-item { + display: flex; + align-items: flex-start; + gap: 8px; + padding: 6px 8px; + border: 1px solid transparent; + border-radius: var(--sb-radius-md); cursor: pointer; - transition: background-color var(--sb-transition), color var(--sb-transition); + transition: background-color var(--sb-transition), border-color var(--sb-transition); } -.suggestions-container li:hover { - background-color: var(--sb-accent-faint); - color: var(--sb-accent); +.search-result-item:hover, +.search-result-item:focus-visible, +.search-result-item.is-active { + background: #f0f4ff; + border-color: rgba(43, 51, 233, 0.16); + outline: none; } -.no-results { - padding: 16px; - text-align: center; - color: var(--sb-text-muted); - font-size: 0.85rem; +/* Not present in every selected dataset: listed grayed out, cannot be picked. */ +.search-result-item.is-unavailable { + cursor: not-allowed; + opacity: var(--unavailable-opacity); } -/* Section headers */ -.section-header { - font-size: 0.72rem; +.search-result-item.is-unavailable .search-result-label { + color: var(--unavailable-text); +} + +.search-result-content { + flex: 1 1 auto; + min-width: 0; + display: flex; + flex-direction: column; + gap: 2px; +} + +.search-result-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + min-width: 0; +} + +.search-result-label { + flex: 1 1 auto; + min-width: 0; + font-size: 0.83rem; + font-weight: 600; + line-height: 1.25; + color: var(--sb-text); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.search-result-label mark { + background: color-mix(in srgb, var(--accent-color) 32%, white); + color: var(--text-main); font-weight: 700; - text-transform: uppercase; - letter-spacing: 0.05em; - color: var(--sb-text-muted); - background: #f8fafc; - padding: 6px 12px; - border-top: 1px solid var(--sb-border); - border-bottom: 1px solid var(--sb-border); - position: sticky; - top: 0; - z-index: 10; + border-radius: 3px; + padding: 0 2px; } -.section-header:first-child { - border-top: none; +.search-result-badge { + flex-shrink: 0; + padding: 2px 7px; + border-radius: 999px; + font-size: 0.68rem; + font-weight: 600; + line-height: 1.2; + letter-spacing: 0.01em; + font-variant-numeric: tabular-nums; } -/* Variable result items */ -.variable-item { - padding: 6px 8px; - font-size: 0.8rem; - cursor: pointer; - transition: background-color 0.2s ease; - border-bottom: 1px solid #f4f4f4; +.search-result-badge.variable-badge { + text-transform: lowercase; + background: #eef2ff; + color: #3730a3; + border: 1px solid rgba(43, 51, 233, 0.12); } -.variable-item:hover { - background-color: #ecfbff; +.search-result-badge.group-badge { + background: #f1f5f9; + color: var(--text-muted); + border: 1px solid rgba(148, 163, 184, 0.2); } -.item-top { +.search-result-breadcrumb { + font-size: 0.71rem; + font-weight: 500; + line-height: 1.3; + color: #64748b; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +/* Empty State */ +.no-results { display: flex; - justify-content: space-between; + flex-direction: column; align-items: center; - font-weight: 500; - color: #0f172a; + justify-content: center; + gap: 6px; + padding: 24px 16px; + text-align: center; + background: #ffffff; } -.result-group { - font-size: 0.7rem; - color: #6b7280; - font-style: italic; +.no-results-title { + margin: 0; + font-size: 0.83rem; + font-weight: 600; + color: var(--chart-label-color); } -.item-bottom { - font-size: 0.75rem; - color: #4b5563; - margin-top: 2px; - padding-left: 4px; - display: flex; - align-items: center; - gap: 4px; +.no-results-hint { + margin: 0; + font-size: 0.73rem; + color: #94a3b8; } -.result-type { - background: #f5f8ff; - border-radius: 999px; - padding: 1px 8px; - display: inline-flex; +/* Keyboard Shortcut Footer Hint */ +.search-footer-hint { + display: flex; align-items: center; - color: #1d4ed8; - font-style: italic; + justify-content: center; + gap: 6px; + padding: 8px 14px; + background: var(--bg-neutral); + border-top: 1px solid rgba(226, 232, 240, 0.85); font-size: 0.7rem; + color: #94a3b8; } -.type-icon { - font-size: 0.75rem; - color: #1d4ed8; - opacity: 0.8; - margin-right: 3px; +.search-footer-hint kbd { + display: inline-block; + padding: 1px 4px; + border-radius: 4px; + background: #ffffff; + border: 1px solid #cbd5e1; + font-family: inherit; + font-size: 0.66rem; + font-weight: 600; + color: var(--text-muted); } -/* Highlight inside result-name */ -.result-name mark { - background: #ffec99; - color: inherit; - padding: 0; +.hint-sep { + color: #cbd5e1; } -/* Mobile: a bit wider and looser */ +/* Mobile Responsiveness */ @media (max-width: 960px) { .search-container { - margin: 8px 0 0; - width: 100%; - } - - .search-input-container { + margin: 0; width: 100%; } - #search-bar { - font-size: 0.85rem; + .search-results-list { + max-height: 240px; } } \ No newline at end of file diff --git a/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.html b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.html index 0fec20838..923b02011 100644 --- a/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.html +++ b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.html @@ -1,65 +1,156 @@ <div class="search-container"> - <div class="search-input-container"> - <input type="text" id="search-bar" placeholder="Search variables or groups" aria-label="Search variables or groups" - [(ngModel)]="searchQuery" (focus)="onSearchFocus()" (input)="handleSearch(searchQuery)" /> + <!-- Top Search Input Header --> + <div class="search-header-row"> + <i class="fas fa-search search-icon" aria-hidden="true"></i> + <input + type="text" + id="search-bar" + class="search-input" + placeholder="Search variables or groups..." + aria-label="Search variables or groups" + role="combobox" + [attr.aria-expanded]="searchSuggestionsVisible" + [attr.aria-controls]="searchSuggestionsVisible ? 'search-results-listbox' : null" + [attr.aria-activedescendant]="activeDescendantId()" + [(ngModel)]="searchQuery" + (focus)="onSearchFocus()" + (input)="handleSearch(searchQuery)" + (keydown)="onSearchKeydown($event)" + autocomplete="off" + spellcheck="false" /> @if (searchQuery) { - <button class="clear-btn" (click)="closeSearch()">×</button> + <button + type="button" + class="clear-btn" + aria-label="Clear search" + title="Clear search" + (click)="clearSearch()"> + <i class="fas fa-times" aria-hidden="true"></i> + </button> } + </div> + + @if (searchSuggestionsVisible) { + <div class="search-divider" aria-hidden="true"></div> + } + + <!-- Suggestions / Results Area --> + @if (searchSuggestionsVisible) { + <div class="suggestions-container"> + <div class="search-toolbar"> + <div class="search-toolbar-main"> + <!-- Variables vs Groups toggle --> + <div + class="search-kind-segment" + role="group" + aria-label="Search kind" + style="--segment-count: 2" + [style.--segment-active-index]="kindIndex"> + <span class="search-segment-track" aria-hidden="true"> + <span class="search-segment-indicator"></span> + </span> + <button + type="button" + class="search-segment-btn" + [class.active]="filterType === 'variables'" + [attr.aria-pressed]="filterType === 'variables'" + (click)="setFilterType('variables')"> + <span>Variables</span> + </button> + <button + type="button" + class="search-segment-btn" + [class.active]="filterType === 'groups'" + [attr.aria-pressed]="filterType === 'groups'" + (click)="setFilterType('groups')"> + <span>Groups</span> + </button> + </div> - <div class="inline-filters"> - <select [(ngModel)]="filterType" (change)="applyFilter(filterType)"> - <option value="variables">Variables</option> - <option value="groups">Groups</option> - </select> + <span class="search-results-counter"> + {{ filteredItems.length }} {{ filterType === 'variables' ? (filteredItems.length === 1 ? 'variable' : 'variables') : (filteredItems.length === 1 ? 'group' : 'groups') }} + </span> + </div> - @if (filterType === 'variables') { - <div class="additional-filter"> - <select [(ngModel)]="variableTypeFilter" (change)="applyVariableTypeFilter(variableTypeFilter)"> - <option value="">All Types</option> - @for (type of variableTypes; track type) { - <option [value]="type">{{ type }}</option> - } - </select> + <!-- Variable Type Filter Chips --> + @if (filterType === 'variables' && variableTypes.length > 0) { + <div class="search-type-chips" role="group" aria-label="Filter by variable type"> + <button + type="button" + class="search-type-chip" + [class.is-active]="!variableTypeFilter" + [attr.aria-pressed]="!variableTypeFilter" + (click)="setVariableTypeFilter('')"> + All + </button> + @for (type of variableTypes; track type) { + <button + type="button" + class="search-type-chip" + [class.is-active]="variableTypeFilter === type" + [attr.aria-pressed]="variableTypeFilter === type" + (click)="setVariableTypeFilter(type)"> + {{ type }} + </button> + } </div> } </div> - </div> - <!-- Suggestions Results --> - @if (searchSuggestionsVisible) { - <div class="suggestions-container"> @if (filteredItems.length > 0) { - <div class="section-header"> - {{ filterType === 'variables' ? 'Variables' : 'Groups' }} - </div> - <ul> - @for (item of filteredItems; track item.code || item.label) { - <li class="variable-item" (click)="onItemClick(item)" [title]="generateTooltip(item)"> - <div class="item-top"> - <span class="result-name" [innerHTML]="highlight(item.label)"></span> - @if (item.path && filterType === 'variables') { - <span class="result-group"> - {{ item.path.split(" > ").slice(-2, -1)[0] }} - </span> - } - </div> - <div class="item-bottom"> - <span class="result-type"> - @if (filterType === 'variables') { - <span class="type-icon">ⓘ</span> - type: {{ item.type }} + <ul class="search-results-list" id="search-results-listbox" role="listbox" aria-label="Search results"> + @for (item of filteredItems; track item.id) { + <li + class="search-result-item" + role="option" + [attr.id]="'search-result-' + $index" + [class.is-group]="item.kind === 'group'" + [class.is-active]="$index === activeIndex" + [class.is-unavailable]="isUnavailable(item)" + [attr.aria-selected]="$index === activeIndex" + [attr.aria-disabled]="isUnavailable(item) ? true : null" + (click)="onItemClick(item)" + [title]="generateTooltip(item)"> + @let groupCount = groupVariableCount(item); + <div class="search-result-content"> + <div class="search-result-head"> + <span class="search-result-label" [innerHTML]="highlight(item.label)"></span> + @if (item.kind === 'variable') { + <span class="search-result-badge variable-badge">{{ variableType(item) }}</span> } @else { - Group + <span class="search-result-badge group-badge"> + {{ groupCount }} {{ groupCount === 1 ? 'var' : 'vars' }} + </span> } - </span> + </div> + <div class="search-result-breadcrumb" [title]="item.path"> + <span>{{ breadcrumbPath(item) }}</span> + </div> </div> </li> } </ul> } @else { - <div class="no-results">No results found for "{{ searchQuery }}"</div> + <div class="no-results"> + <p class="no-results-title"> + @if (searchQuery.trim()) { + No results found for "{{ searchQuery }}" + } @else { + No {{ filterType === 'variables' ? 'variables' : 'groups' }} available + } + </p> + <p class="no-results-hint">Try adjusting your query or switching filter options.</p> + </div> } + + <div class="search-footer-hint"> + <span class="hint-item"><kbd>↑</kbd><kbd>↓</kbd> navigate</span> + <span class="hint-sep">·</span> + <span class="hint-item"><kbd>↵</kbd> select</span> + <span class="hint-sep">·</span> + <span class="hint-item"><kbd>esc</kbd> close</span> + </div> </div> } -</div> \ No newline at end of file +</div> diff --git a/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.spec.ts b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.spec.ts new file mode 100644 index 000000000..083d80f2d --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.spec.ts @@ -0,0 +1,240 @@ +import { provideZonelessChangeDetection, SimpleChange } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { SearchBarComponent } from './search-bar.component'; +import { MetadataSearchResult } from '../../visualisations/metadata-browser/metadata-browser.model'; + +describe('SearchBarComponent', () => { + const mockHierarchy = { + code: 'root', + label: 'Stroke 3.7', + children: [ + { + code: 'demographics', + label: 'Demographics', + children: [ + { + code: 'age', + label: 'Age at onset', + type: 'discrete', + }, + { + code: 'gender', + label: 'Gender', + type: 'nominal', + }, + ], + }, + { + code: 'vitals', + label: 'Vitals', + children: [ + { + code: 'blood_pressure', + label: 'Blood Pressure', + type: 'continuous', + }, + ], + }, + ], + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [SearchBarComponent], + providers: [provideZonelessChangeDetection()], + }); + }); + + function setupComponent(): { + fixture: ReturnType<typeof TestBed.createComponent<SearchBarComponent>>; + component: SearchBarComponent; + } { + const fixture = TestBed.createComponent(SearchBarComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('dataModelHierarchy', mockHierarchy); + component.ngOnChanges({ + dataModelHierarchy: new SimpleChange(null, mockHierarchy, true), + }); + fixture.detectChanges(); + return { fixture, component }; + } + + it('indexes hierarchy and discovers variable types', () => { + const { component } = setupComponent(); + expect(component.variableTypes).toEqual(['continuous', 'discrete', 'nominal']); + }); + + it('filters variables by search query', () => { + const { component } = setupComponent(); + component.onSearchFocus(); + component.handleSearch('age'); + + expect(component.filteredItems.length).toBe(1); + expect(component.filteredItems[0].label).toBe('Age at onset'); + expect(component.filteredItems[0].kind).toBe('variable'); + }); + + it('filters groups when filterType is groups', () => { + const { component } = setupComponent(); + component.onSearchFocus(); + component.setFilterType('groups'); + + expect(component.filteredItems.length).toBeGreaterThan(0); + expect(component.filteredItems.every((item) => item.kind === 'group')).toBe(true); + const groupLabels = component.filteredItems.map((item) => item.label); + expect(groupLabels).toContain('Demographics'); + }); + + it('filters variables by type filter', () => { + const { component } = setupComponent(); + component.onSearchFocus(); + component.handleSearch(''); + component.setVariableTypeFilter('nominal'); + + expect(component.filteredItems.length).toBe(1); + expect(component.filteredItems[0].label).toBe('Gender'); + + // Toggle off / clear type filter + component.setVariableTypeFilter('nominal'); + expect(component.filteredItems.length).toBe(3); + }); + + it('emits searchResultSelected when an item is clicked', () => { + const { component } = setupComponent(); + let selected: MetadataSearchResult | undefined; + component.searchResultSelected.subscribe((item) => (selected = item)); + + component.onSearchFocus(); + component.handleSearch('Gender'); + expect(component.filteredItems.length).toBe(1); + + component.onItemClick(component.filteredItems[0]); + + expect(selected).toBeDefined(); + expect(selected?.label).toBe('Gender'); + expect(component.searchSuggestionsVisible).toBe(false); + }); + + it('supports keyboard navigation with arrow keys and Enter', () => { + const { component } = setupComponent(); + let selected: MetadataSearchResult | undefined; + component.searchResultSelected.subscribe((item) => (selected = item)); + + component.onSearchFocus(); + component.handleSearch(''); + expect(component.filteredItems.length).toBe(3); + expect(component.activeIndex).toBe(0); + + // Arrow down moves selection + component.onSearchKeydown(new KeyboardEvent('keydown', { key: 'ArrowDown' })); + expect(component.activeIndex).toBe(1); + + // Enter selects the active item + component.onSearchKeydown(new KeyboardEvent('keydown', { key: 'Enter' })); + expect(selected).toBeDefined(); + expect(selected?.label).toBe(component.filteredItems[1].label); + }); + + it('closes suggestions on Escape key', () => { + const { component } = setupComponent(); + component.onSearchFocus(); + expect(component.searchSuggestionsVisible).toBe(true); + + component.onSearchKeydown(new KeyboardEvent('keydown', { key: 'Escape' })); + expect(component.searchSuggestionsVisible).toBe(false); + }); + + it('highlights matched substring within label', () => { + const { component } = setupComponent(); + component.searchQuery = 'Age'; + const highlighted = component.highlight('Age at onset'); + expect(highlighted).toContain('<mark>Age</mark>'); + }); + + it('escapes HTML in labels because the result is bound via innerHTML', () => { + const { component } = setupComponent(); + // Even with no query, the raw label must not reach [innerHTML]. + const plain = component.highlight('Age <script>'); + expect(plain).toContain('<script>'); + expect(plain).not.toContain('<script'); + + component.searchQuery = 'Age'; + const highlighted = component.highlight('Age <script>'); + expect(highlighted).toContain('<mark>Age</mark>'); + expect(highlighted).toContain('<script>'); + expect(highlighted).not.toContain('<script'); + }); + + it('formats breadcrumbs properly for variable results', () => { + const { component } = setupComponent(); + const item: MetadataSearchResult = { + id: 'variable:age', + kind: 'variable', + label: 'Age at onset', + path: 'Stroke 3.7 > Demographics > Age at onset', + pathLabels: ['Stroke 3.7', 'Demographics', 'Age at onset'], + matchText: 'Age at onset', + parentGroupId: 'demographics', + }; + + const breadcrumb = component.breadcrumbPath(item); + expect(breadcrumb).toBe('Stroke 3.7 › Demographics'); + }); + + it('clears search input and updates results on clearSearch', () => { + const { component } = setupComponent(); + component.onSearchFocus(); + component.handleSearch('Age'); + expect(component.searchQuery).toBe('Age'); + + component.clearSearch(); + expect(component.searchQuery).toBe(''); + expect(component.filteredItems.length).toBe(3); + }); + + it('wires the combobox contract so the active option is announced', () => { + // Opened before the first render: the zoneless harness does not reliably + // re-render attribute bindings on a second detectChanges(). + const fixture = TestBed.createComponent(SearchBarComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('dataModelHierarchy', mockHierarchy); + component.ngOnChanges({ + dataModelHierarchy: new SimpleChange(null, mockHierarchy, true), + }); + component.onSearchFocus(); + component.handleSearch('age'); + fixture.detectChanges(); + + const input = fixture.nativeElement.querySelector('#search-bar'); + expect(input.getAttribute('role')).toBe('combobox'); + expect(input.getAttribute('aria-expanded')).toBe('true'); + expect(input.getAttribute('aria-controls')).toBe('search-results-listbox'); + expect(input.getAttribute('aria-activedescendant')).toBe('search-result-0'); + + const active = fixture.nativeElement.querySelector('#search-result-0'); + expect(active.getAttribute('role')).toBe('option'); + expect(active.getAttribute('aria-selected')).toBe('true'); + }); + + it('grays an unavailable variable result, explains why and refuses to pick it', () => { + const { fixture, component } = setupComponent(); + fixture.componentRef.setInput('unavailableVariables', new Map([['gender', ['Dataset B']]])); + let selected: MetadataSearchResult | undefined; + component.searchResultSelected.subscribe((item) => (selected = item)); + + component.onSearchFocus(); + component.handleSearch('Gender'); + fixture.detectChanges(); + + const item = component.filteredItems[0]; + expect(component.isUnavailable(item)).toBe(true); + expect(component.generateTooltip(item)).toContain('Not available in: Dataset B'); + const row = fixture.nativeElement.querySelector('.search-result-item') as HTMLElement; + expect(row.classList).toContain('is-unavailable'); + expect(row.getAttribute('aria-disabled')).toBe('true'); + + row.click(); + component.onSearchKeydown(new KeyboardEvent('keydown', { key: 'Enter' })); + expect(selected).toBeUndefined(); + }); +}); diff --git a/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.ts b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.ts index 873f289a2..58ed256f3 100644 --- a/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.ts +++ b/src/app/pages/experiment-studio/variables-panel/search-bar/search-bar.component.ts @@ -1,160 +1,264 @@ -import { Component, EventEmitter, Input, Output, OnInit, ElementRef, HostListener, SimpleChanges, OnChanges, inject, ChangeDetectionStrategy } from '@angular/core'; -import { FormsModule } from "@angular/forms"; +import { + Component, + ElementRef, + SimpleChanges, + OnChanges, + inject, + ChangeDetectionStrategy, + output, + input, +} from '@angular/core'; +import { FormsModule } from '@angular/forms'; +import { + MetadataSearchResult, + NormalizedMetadataIndex, +} from '../../visualisations/metadata-browser/metadata-browser.model'; +import { + normalizeMetadataTree, + searchMetadataIndex, +} from '../../visualisations/metadata-browser/metadata-browser-normalizer'; +import { + EMPTY_UNAVAILABLE_VARIABLES, + UnavailableVariablesMap, + unavailableTooltipFor, +} from '../../../../core/variable-availability.utils'; @Component({ selector: 'app-search-bar', templateUrl: './search-bar.component.html', - imports: [ - FormsModule, - ], - styleUrls: ['./search-bar.component.css'], - changeDetection: ChangeDetectionStrategy.OnPush + imports: [FormsModule], + styleUrl: './search-bar.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, + host: { + '(document:click)': 'onOutsideClick($event)', + }, }) -export class SearchBarComponent implements OnInit, OnChanges { +export class SearchBarComponent implements OnChanges { private eRef = inject(ElementRef); - @Input() dataModelHierarchy: any; - @Output() searchResultSelected = new EventEmitter<string>(); + readonly dataModelHierarchy = input<any>(); + /** Variable results listed grayed out that cannot be picked for the current datasets. */ + readonly unavailableVariables = input<UnavailableVariablesMap>(EMPTY_UNAVAILABLE_VARIABLES); + readonly searchResultSelected = output<MetadataSearchResult>(); - searchQuery: string = ''; - variables: { label: string; code: string; type: string; path: string }[] = []; - groups: { label: string; code: string; path: string }[] = []; - filteredItems: any[] = []; + searchQuery = ''; + filteredItems: MetadataSearchResult[] = []; searchSuggestionsVisible = false; - isSearchExpanded = false; - filterType: string = 'variables'; // Default filter type - variableTypeFilter: string = ''; // Additional variable type filter - variableTypes: string[] = []; // List of available variable types + filterType: 'variables' | 'groups' = 'variables'; + variableTypeFilter = ''; + variableTypes: string[] = []; + activeIndex = -1; - constructor() { } - - ngOnInit(): void { - if (this.dataModelHierarchy) { - this.extractVariablesAndGroups(this.dataModelHierarchy); - } + get kindIndex(): number { + return this.filterType === 'groups' ? 1 : 0; } - ngOnChanges(changes: SimpleChanges): void { - if (changes['dataModelHierarchy']?.currentValue) { + /** The option the input's aria-activedescendant must point at, or null when nothing is active. */ + activeDescendantId(): string | null { + return this.searchSuggestionsVisible && this.activeIndex >= 0 ? `search-result-${this.activeIndex}` : null; + } - // Ensure variables and groups are cleared before reloading - this.variables = []; - this.groups = []; - this.variableTypes = []; + get typeIndex(): number { + return this.variableTypes.indexOf(this.variableTypeFilter); + } - // Extract variables and groups - this.extractVariablesAndGroups(this.dataModelHierarchy); + private metadataIndex: NormalizedMetadataIndex | null = null; + ngOnChanges(changes: SimpleChanges): void { + if (changes['dataModelHierarchy']?.currentValue) { + this.rebuildIndex(this.dataModelHierarchy()); } } - expandSearch(event: MouseEvent): void { - event.stopPropagation(); - this.isSearchExpanded = true; - } - closeSearch(): void { - this.isSearchExpanded = false; + clearSearch(): void { this.searchQuery = ''; + this.handleSearch(''); + this.focusInput(); + } + + hideSuggestions(): void { this.searchSuggestionsVisible = false; + this.activeIndex = -1; } + /** Result is bound via [innerHTML]; escape first, then add <mark> tags. */ highlight(name: string): string { - if (!this.searchQuery) return name; - const re = new RegExp(`(${this.searchQuery})`, 'gi'); - return name.replace(re, '<mark>$1</mark>'); + const safe = String(name ?? '') + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + if (!this.searchQuery) return safe; + const re = new RegExp(`(${this.escapeRegExp(this.searchQuery)})`, 'gi'); + return safe.replace(re, '<mark>$1</mark>'); } - - @HostListener('document:click', ['$event']) onOutsideClick(event: Event): void { if (!this.eRef.nativeElement.contains(event.target)) { - this.closeSearch(); + this.hideSuggestions(); } } - // Handles the search query input. - extractVariablesAndGroups(hierarchy: any): void { - this.variables = []; - this.groups = []; - this.variableTypes = []; - - const traverse = (node: any, path: string) => { - if (!node) return; - const currentPath = path ? `${path} > ${node.label}` : node.label; - - // GROUP - if (Array.isArray(node.children) && node.children.length > 0) { - this.groups.push({ - label: node.label, - code: node.code ?? node.label, // fallback safe - path: currentPath - }); - node.children.forEach((child: any) => traverse(child, currentPath)); - return; + handleSearch(query: string): void { + this.searchQuery = query; + this.applyFilter(this.filterType); + } + + applyFilter(type: 'variables' | 'groups'): void { + this.filterType = type; + const index = this.metadataIndex; + if (!index) { + this.filteredItems = []; + this.activeIndex = -1; + return; + } + + const results = searchMetadataIndex(index, this.searchQuery); + this.filteredItems = results.filter((result) => { + if (type === 'groups') { + return result.kind === 'group'; + } + if (result.kind !== 'variable') { + return false; } + if (!this.variableTypeFilter) { + return true; + } + return index.variablesById[result.id]?.type === this.variableTypeFilter; + }); + this.activeIndex = this.filteredItems.length ? 0 : -1; + } - // LEAF (variable) - if (typeof node.type === 'string' || Array.isArray(node.type)) { - this.variables.push({ - label: node.label, - code: node.code ?? node.label, // fallback safe - type: node.type as string, - path: currentPath - }); - - const t = node.type as string; - if (!this.variableTypes.includes(t)) this.variableTypes.push(t); + onSearchKeydown(event: KeyboardEvent): void { + if (event.key === 'Escape') { + event.preventDefault(); + this.hideSuggestions(); + return; + } + if (!this.searchSuggestionsVisible || !this.filteredItems.length) { + if (event.key === 'ArrowDown') { + this.searchSuggestionsVisible = true; + this.applyFilter(this.filterType); } - }; + return; + } + if (event.key === 'ArrowDown') { + event.preventDefault(); + this.activeIndex = (this.activeIndex + 1) % this.filteredItems.length; + this.scrollActiveIntoView(); + return; + } + if (event.key === 'ArrowUp') { + event.preventDefault(); + this.activeIndex = this.activeIndex <= 0 + ? this.filteredItems.length - 1 + : this.activeIndex - 1; + this.scrollActiveIntoView(); + return; + } + if (event.key === 'Enter' && this.activeIndex >= 0) { + event.preventDefault(); + this.onItemClick(this.filteredItems[this.activeIndex]); + } + } - traverse(hierarchy, ''); + setFilterType(type: 'variables' | 'groups'): void { + this.variableTypeFilter = ''; + this.applyFilter(type); } - handleSearch(query: string): void { - this.searchQuery = query.toLowerCase(); - this.applyFilter(this.filterType); - this.searchSuggestionsVisible = this.filteredItems.length > 0; - } - - // Applies the filter for either variables or groups. - applyFilter(type: string): void { - if (type === 'variables') { - this.filteredItems = this.variables.filter( - (v) => - v.label.toLowerCase().includes(this.searchQuery) && - (this.variableTypeFilter ? v.type === this.variableTypeFilter : true) - ); - } else if (type === 'groups') { - this.filteredItems = this.groups.filter((g) => - g.label.toLowerCase().includes(this.searchQuery) - ); + setVariableTypeFilter(type: string): void { + this.variableTypeFilter = this.variableTypeFilter === type ? '' : type; + this.applyFilter('variables'); + } + + onItemClick(item: MetadataSearchResult): void { + if (this.isUnavailable(item)) return; + this.searchQuery = item.label; + this.hideSuggestions(); + this.searchResultSelected.emit(item); + } + + breadcrumbPath(item: MetadataSearchResult): string { + if (!item.pathLabels || item.pathLabels.length <= 1) { + return item.path || 'Root'; } + // Return ancestor hierarchy without the item label itself + return item.pathLabels.slice(0, -1).join(' › '); } - // Applies the variable type filter. - applyVariableTypeFilter(type: string): void { - this.variableTypeFilter = type; - this.applyFilter(this.filterType); + groupVariableCount(item: MetadataSearchResult): number { + return this.metadataIndex?.groupsById[item.id]?.totalVariableCount ?? 0; } - onItemClick(item: any): void { - this.searchQuery = item.label || item; - this.searchSuggestionsVisible = false; + variableType(item: MetadataSearchResult): string { + return this.metadataIndex?.variablesById[item.id]?.type ?? ''; + } + + generateTooltip(item: MetadataSearchResult): string { + if (item.kind === 'group') { + const count = this.groupVariableCount(item); + return `Group: ${item.label}\nPath: ${item.path}\nVariables: ${count}`; + } + const variable = this.metadataIndex?.variablesById[item.id]; + const base = `Variable: ${item.label}\nPath: ${item.path}\nType: ${variable?.type ?? 'unknown'}`; + const unavailable = this.unavailableTooltip(item); + return unavailable ? `${base}\n${unavailable}` : base; + } - const code = item.code ?? item.label ?? item; - this.searchResultSelected.emit(code); + isUnavailable(item: MetadataSearchResult): boolean { + return this.unavailableTooltip(item) !== ''; } - // Generates tooltip text for search results. - generateTooltip(item: any): string { - const parent = item.path.split(' > ').slice(-2, -1)[0] || 'Root'; - return `Group: ${parent}\nPath: ${item.path}\nType: ${item.type}`; + /** "Not available in: …" for a variable result missing from a selected dataset, '' otherwise. */ + private unavailableTooltip(item: MetadataSearchResult): string { + if (item.kind !== 'variable') return ''; + const code = this.metadataIndex?.variablesById[item.id]?.code; + return unavailableTooltipFor(this.unavailableVariables(), { code }); } - // Handles focus event on the search bar. onSearchFocus(): void { this.searchSuggestionsVisible = true; this.handleSearch(this.searchQuery); } + + private scrollActiveIntoView(): void { + setTimeout(() => { + const activeEl = this.eRef.nativeElement.querySelector('.search-result-item.is-active'); + if (activeEl && typeof activeEl.scrollIntoView === 'function') { + activeEl.scrollIntoView({ block: 'nearest', behavior: 'smooth' }); + } + }, 0); + } + + private focusInput(): void { + setTimeout(() => { + const inputEl = this.eRef.nativeElement.querySelector('#search-bar') as HTMLInputElement | null; + inputEl?.focus(); + }, 0); + } + + private rebuildIndex(hierarchy: any): void { + if (!hierarchy) { + this.metadataIndex = null; + this.variableTypes = []; + this.filteredItems = []; + return; + } + + this.metadataIndex = normalizeMetadataTree(hierarchy); + this.variableTypes = [ + ...new Set( + this.metadataIndex.variableIds + .map((id) => this.metadataIndex!.variablesById[id]?.type) + .filter((type): type is string => !!type) + ), + ].sort((a, b) => a.localeCompare(b)); + } + + private escapeRegExp(value: string): string { + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } } diff --git a/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.css b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.css new file mode 100644 index 000000000..2ea3542ed --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.css @@ -0,0 +1,306 @@ +:host { + position: relative; + display: inline-flex; + align-items: stretch; +} + +.selected-variables-trigger { + position: relative; + display: inline-flex; + align-items: center; + justify-content: center; + min-width: 32px; + min-height: 28px; + padding: 0 10px; + border: 0; + border-radius: 999px; + background: transparent; + color: var(--primary-color); + font-family: inherit; + font-size: 0.76rem; + font-weight: 800; + line-height: 1; + cursor: pointer; + outline: none; + box-shadow: none; +} + +.selected-variables-trigger::before { + content: ''; + position: absolute; + left: 0; + top: 6px; + bottom: 6px; + width: 1px; + background: rgba(127, 156, 232, 0.35); +} + +.selected-variables-trigger:hover, +.selected-variables-trigger:focus, +.selected-variables-trigger:focus-visible, +.selected-variables-trigger[aria-expanded="true"] { + background: color-mix(in srgb, var(--primary-light) 18%, white); + outline: none; + box-shadow: none; +} + +.selected-variables-trigger-count { + color: inherit; + font-size: 0.76rem; + font-weight: 800; + line-height: 1; +} + +.selected-variables-popover { + position: absolute; + top: calc(100% + 8px); + right: 0; + z-index: 40; + width: min(360px, 78vw); + max-height: 18rem; + overflow-y: auto; + padding: 12px; + box-sizing: border-box; + background: var(--studio-card-bg); + border-radius: var(--radius-lg, 12px); + border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.35)); + box-shadow: 0 10px 26px rgba(15, 23, 42, 0.12); +} + +.selected-variables-popover-header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + margin-bottom: 10px; +} + +.selected-variables-popover-title { + font-size: 0.8rem; + font-weight: 800; + color: var(--chart-label-color); +} + +.parameter-clear-btn { + display: inline-flex; + align-items: center; + gap: 6px; + min-height: 28px; + padding: 0 8px; + border: 0; + border-radius: 8px; + background: transparent; + color: var(--danger); + font-family: inherit; + font-size: 0.72rem; + font-weight: 700; + cursor: pointer; +} + +.parameter-clear-btn:hover, +.parameter-clear-btn:focus-visible { + background: color-mix(in srgb, var(--danger) 10%, white); +} + +.parameter-empty { + padding: 10px; + border-radius: var(--radius-md, 8px); + background: var(--bg-neutral); + color: var(--text-muted); + font-size: 0.74rem; + line-height: 1.35; + text-align: center; +} + +/* Selected variables table */ +.selected-vars-table { + width: 100%; + border-collapse: collapse; + table-layout: fixed; + font-size: 0.78rem; +} + +.selected-vars-table th { + padding: 0 6px 6px; + border-bottom: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.35)); + color: var(--text-muted); + font-size: 0.62rem; + font-weight: 700; + letter-spacing: 0.04em; + text-align: left; + text-transform: uppercase; +} + +.selected-vars-table th:nth-child(1) { + width: 62%; +} + +.selected-vars-table th:nth-child(2) { + width: 20%; +} + +.selected-vars-th-actions { + width: 28px; +} + +.selected-vars-table td { + padding: 6px; + border-bottom: 1px solid color-mix(in srgb, var(--studio-card-border, rgba(148, 163, 184, 0.35)) 55%, transparent); + vertical-align: middle; +} + +.selected-vars-row { + cursor: pointer; + transition: background-color 0.16s ease, box-shadow 0.16s ease; +} + +.selected-vars-row:hover td { + background: color-mix(in srgb, var(--primary-light) 6%, var(--card-bg)); +} + +.selected-vars-row.is-selected td { + background: color-mix(in srgb, var(--variable-color) 10%, var(--card-bg)); +} + +.selected-vars-row.is-selected td:first-child { + box-shadow: inset 3px 0 0 color-mix(in srgb, var(--variable-color) 60%, transparent); +} + +.selected-var-name { + min-width: 0; +} + +.selected-var-label { + display: block; + font-weight: 700; + color: var(--text-main); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.selected-var-code { + display: block; + color: var(--text-muted); + font-size: 0.64rem; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.selected-var-type { + color: var(--text-muted); + font-size: 0.7rem; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.selected-var-td-actions { + width: 28px; + text-align: right; +} + +.parameter-remove-btn { + display: inline-flex; + align-items: center; + justify-content: center; + width: 22px; + height: 22px; + flex: 0 0 auto; + border: none; + border-radius: var(--radius-sm, 6px); + background: transparent; + color: var(--text-muted); + font-size: 0.68rem; + cursor: pointer; + transition: background-color 0.16s ease, color 0.16s ease; +} + +.parameter-remove-btn:hover, +.parameter-remove-btn:focus-visible { + background: color-mix(in srgb, var(--danger) 10%, white); + color: var(--danger); +} + +:host:has(.pool-card) { + display: block; +} + +.pool-card { + background: var(--studio-card-bg); + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-lg); + box-shadow: var(--studio-card-shadow); + overflow: hidden; +} + +.pool-card-header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + padding: 12px 16px; + background: var(--studio-card-header-bg); + border-bottom: 1px solid var(--studio-card-header-border); +} + +.pool-card-title { + display: flex; + align-items: center; + gap: 8px; + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; +} + +.pool-card-body { + padding: 12px 16px 14px; +} + +.pool-chip { + gap: 8px; + height: 30px; + padding: 0 4px 0 10px; + overflow: visible; +} + +.pool-chip.is-current { + border-color: color-mix(in srgb, var(--variable-color) 60%, transparent); + background: color-mix(in srgb, var(--variable-color) 10%, var(--card-bg)); +} + +.pool-chip-dot { + width: 8px; + height: 8px; + border-radius: 999px; + background: var(--variable-color); + flex-shrink: 0; +} + +.pool-chip-type { + color: var(--text-muted); + font-size: 0.68rem; + font-weight: 500; +} + +.pool-chip .parameter-remove-btn { + border-radius: 999px; +} + +/* Pooled before a dataset change made it unavailable: grayed out, still removable. */ +.pool-chip.is-unavailable, +.selected-vars-row.is-unavailable { + cursor: not-allowed; + opacity: var(--unavailable-opacity); +} + +.pool-chip.is-unavailable .pool-chip-dot { + background: var(--unavailable-border); +} + +.pool-chip .parameter-remove-btn:hover { + color: #991b1b; /* dark ink, DESIGN.md "text on a state fill" */ +} diff --git a/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.html b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.html new file mode 100644 index 000000000..c0e8a8803 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.html @@ -0,0 +1,115 @@ +@if (mode() === 'inline') { +<section class="pool-card" aria-label="Variables in this experiment"> + <header class="pool-card-header"> + <div class="pool-card-title"> + <span>Variables in this experiment</span> + <span class="count-badge">{{ variables().length }}</span> + </div> + @if (variables().length > 0) { + <button type="button" class="parameter-clear-btn" (click)="clearList()"> + <i class="fas fa-trash-alt" aria-hidden="true"></i><span>Clear all</span> + </button> + } + </header> + <div class="pool-card-body"> + @if (variables().length === 0) { + <p class="parameter-empty">No variables yet. Pick one in the map and choose <strong>Add</strong>.</p> + } @else { + <div class="studio-chip-row"> + @for (variable of variables(); track variable.code ?? variable.label) { + @let unavailable = unavailableTooltip(variable); + <span class="studio-chip pool-chip" + [class.is-current]="selectedNode()?.code === variable.code" + [class.is-unavailable]="!!unavailable" + [attr.title]="unavailable || null" + (click)="onVariableClick(variable)"> + <span class="pool-chip-dot" aria-hidden="true"></span> + <span>{{ variable.label }}</span> + <span class="pool-chip-type">{{ variable.type }}</span> + <button type="button" class="parameter-remove-btn" + [attr.aria-label]="'Remove ' + variable.label" + (click)="removeItem(variable); $event.stopPropagation()"> + <i class="fas fa-times" aria-hidden="true"></i> + </button> + </span> + } + </div> + } + </div> +</section> +} @else { +<button + type="button" + class="selected-variables-trigger" + [attr.aria-expanded]="isOpen()" + aria-controls="selected-variables-popover" + [attr.aria-label]="'Selected variables: ' + variables().length" + (click)="toggle(); $event.stopPropagation()" +> + <span class="selected-variables-trigger-count">{{ variables().length }}</span> +</button> + +@if (isOpen()) { +<div class="selected-variables-popover" id="selected-variables-popover" role="dialog" aria-label="Selected variables"> + <div class="selected-variables-popover-header"> + <span class="selected-variables-popover-title">Selected variables</span> + @if (variables().length > 0) { + <button + type="button" + class="panel-secondary-btn parameter-clear-btn" + aria-label="Clear all variables" + (click)="clearList()" + > + <i class="fas fa-trash-alt" aria-hidden="true"></i> + <span>Clear all</span> + </button> + } + </div> + + <table class="selected-vars-table"> + <thead> + <tr> + <th scope="col">Variable</th> + <th scope="col">Type</th> + <th scope="col" class="selected-vars-th-actions" aria-label="Actions"></th> + </tr> + </thead> + <tbody> + @if (variables().length === 0) { + <tr> + <td class="parameter-empty" colspan="3">No variables in this experiment yet.</td> + </tr> + } + @for (variable of variables(); track variable.code ?? variable.label) { + @let rowUnavailable = unavailableTooltip(variable); + <tr + class="selected-vars-row" + [class.is-selected]="selectedNode()?.code === variable.code" + [class.is-unavailable]="!!rowUnavailable" + [attr.title]="rowUnavailable || null" + (click)="onVariableClick(variable)" + > + <td class="selected-var-name"> + <span class="selected-var-label">{{ variable.label }}</span> + @if (variable.code) { + <span class="selected-var-code">{{ variable.code }}</span> + } + </td> + <td class="selected-var-type">{{ variable.type }}</td> + <td class="selected-var-td-actions"> + <button + type="button" + class="parameter-remove-btn" + [attr.aria-label]="'Remove ' + variable.label" + (click)="removeItem(variable); $event.stopPropagation()" + > + <i class="fas fa-times" aria-hidden="true"></i> + </button> + </td> + </tr> + } + </tbody> + </table> +</div> +} +} diff --git a/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.spec.ts b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.spec.ts new file mode 100644 index 000000000..1a617e975 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.spec.ts @@ -0,0 +1,239 @@ +import type { Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { SelectedVariablesComponent } from './selected-variables.component'; + +describe('SelectedVariablesComponent', () => { + let experimentStudioService: { + selectedVariables: ReturnType<typeof signal<any[]>>; + selectedDatasets: ReturnType<typeof signal<string[]>>; + setVariables: Mock; + addVariableAndEnrich: Mock; + unavailableVariables: ReturnType<typeof signal<ReadonlyMap<string, readonly string[]>>>; + }; + + beforeEach(() => { + experimentStudioService = { + selectedVariables: signal<any[]>([]), + selectedDatasets: signal<string[]>([]), + setVariables: vi.fn().mockImplementation((vars: any[]) => + experimentStudioService.selectedVariables.set(vars), + ), + addVariableAndEnrich: vi.fn(), + unavailableVariables: signal<ReadonlyMap<string, readonly string[]>>(new Map()), + }; + + TestBed.configureTestingModule({ + imports: [SelectedVariablesComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: experimentStudioService }, + ], + }); + }); + + function openPopover(fixture: ReturnType<typeof TestBed.createComponent<SelectedVariablesComponent>>): void { + fixture.nativeElement.querySelector('.selected-variables-trigger').click(); + fixture.detectChanges(); + } + + it('toggles the popover from the count trigger', () => { + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + + expect(fixture.componentInstance.isOpen()).toBe(false); + expect(fixture.nativeElement.querySelector('.selected-variables-popover')).toBeNull(); + + openPopover(fixture); + + expect(fixture.componentInstance.isOpen()).toBe(true); + expect(fixture.nativeElement.querySelector('.selected-variables-popover')).not.toBeNull(); + }); + + it('shows an empty hint when the pool is empty', () => { + experimentStudioService.selectedVariables.set([]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + openPopover(fixture); + + const emptyEl: HTMLElement | null = fixture.nativeElement.querySelector('.parameter-empty'); + expect(emptyEl).not.toBeNull(); + expect(emptyEl?.textContent).toContain('No variables in this experiment yet'); + }); + + it('reports the selected chip count', () => { + experimentStudioService.selectedVariables.set([ + { code: 'age_value', label: 'Age', type: 'continuous' }, + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + + expect(fixture.componentInstance.variables().length).toBe(2); + const countEl: HTMLElement | null = fixture.nativeElement.querySelector('.selected-variables-trigger-count'); + expect(countEl?.textContent).toBe('2'); + + openPopover(fixture); + const rows = fixture.nativeElement.querySelectorAll('.selected-vars-row'); + expect(rows.length).toBe(2); + }); + + it('lists every selected variable in the popover', () => { + experimentStudioService.selectedVariables.set(Array.from({ length: 7 }, (_, i) => ({ code: `v${i}`, label: `Var ${i}`, type: 'continuous' }))); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + openPopover(fixture); + + expect(fixture.nativeElement.querySelectorAll('.selected-vars-row').length).toBe(7); + expect(fixture.nativeElement.querySelector('.selected-vars-pager')).toBeNull(); + }); + + it('renders no Values column in the table', () => { + experimentStudioService.selectedVariables.set([ + { code: 'sex_value', label: 'Sex', type: 'string', enumerations: [{ code: '1', label: 'Male' }] }, + { code: 'age_value', label: 'Age', type: 'continuous', minValue: 0, maxValue: 90 }, + ]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + openPopover(fixture); + + const headers = [...fixture.nativeElement.querySelectorAll('.selected-vars-table th')].map((el: any) => el.textContent?.trim()); + expect(headers.filter(Boolean)).toEqual(['Variable', 'Type']); + expect(fixture.nativeElement.querySelector('.selected-var-value-chip')).toBeNull(); + }); + + it('removes a single variable from the pool', () => { + experimentStudioService.selectedVariables.set([ + { code: 'age_value', label: 'Age', type: 'continuous' }, + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + + fixture.componentInstance.removeItem({ code: 'age_value' }); + + expect(experimentStudioService.setVariables).toHaveBeenCalledWith([ + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + }); + + it('clears the whole pool', () => { + experimentStudioService.selectedVariables.set([ + { code: 'age_value', label: 'Age', type: 'continuous' }, + ]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + + fixture.componentInstance.clearList(); + + expect(experimentStudioService.setVariables).toHaveBeenCalledWith([]); + }); + + it('renders the pool card inline instead of the count trigger', () => { + experimentStudioService.selectedVariables.set([ + { code: 'age_value', label: 'Age', type: 'continuous' }, + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.componentRef.setInput('mode', 'inline'); + fixture.detectChanges(); + + expect(fixture.nativeElement.querySelector('.pool-card')).not.toBeNull(); + expect(fixture.nativeElement.querySelector('.selected-variables-trigger')).toBeNull(); + expect(fixture.nativeElement.querySelectorAll('.pool-chip').length).toBe(2); + const count = fixture.nativeElement.querySelector('.count-badge') as HTMLElement; + expect(count?.textContent).toBe('2'); + }); + + it('guides the user to Add when the inline pool is empty', () => { + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.componentRef.setInput('mode', 'inline'); + fixture.detectChanges(); + + const empty = fixture.nativeElement.querySelector('.parameter-empty') as HTMLElement; + expect(empty.textContent).toContain('No variables yet'); + expect(fixture.nativeElement.querySelector('.pool-card .parameter-clear-btn')).toBeNull(); + }); + + it('marks the selected node chip in the inline pool', () => { + experimentStudioService.selectedVariables.set([{ code: 'age_value', label: 'Age', type: 'continuous' }]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.componentRef.setInput('mode', 'inline'); + fixture.componentRef.setInput('selectedNode', { code: 'age_value', label: 'Age', type: 'continuous' }); + fixture.detectChanges(); + + expect(fixture.nativeElement.querySelector('.pool-chip')?.classList).toContain('is-current'); + }); + + it('emits the clicked inline chip and removes one from the pool', () => { + experimentStudioService.selectedVariables.set([ + { code: 'age_value', label: 'Age', type: 'continuous' }, + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.componentRef.setInput('mode', 'inline'); + fixture.detectChanges(); + + const clicks: any[] = []; + fixture.componentInstance.variableClicked.subscribe((node) => clicks.push(node)); + (fixture.nativeElement.querySelector('.pool-chip') as HTMLElement).click(); + expect(clicks[0]?.code).toBe('age_value'); + + const remove = fixture.nativeElement.querySelector('.pool-chip .parameter-remove-btn') as HTMLElement; + remove.click(); + fixture.detectChanges(); + expect(experimentStudioService.setVariables).toHaveBeenCalledWith([ + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + }); + + it('grays a pooled variable that became unavailable, ignores its clicks and keeps it removable', () => { + experimentStudioService.selectedVariables.set([{ code: 'age_value', label: 'Age', type: 'continuous' }]); + experimentStudioService.unavailableVariables.set(new Map([['age_value', ['Dataset B']]])); + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.componentRef.setInput('mode', 'inline'); + fixture.detectChanges(); + + const chip = fixture.nativeElement.querySelector('.pool-chip') as HTMLElement; + expect(chip.classList).toContain('is-unavailable'); + expect(chip.getAttribute('title')).toBe('Not available in: Dataset B'); + + const clicks: any[] = []; + fixture.componentInstance.variableClicked.subscribe((node) => clicks.push(node)); + chip.click(); + expect(clicks).toEqual([]); + + (chip.querySelector('.parameter-remove-btn') as HTMLElement).click(); + expect(experimentStudioService.setVariables).toHaveBeenCalledWith([]); + }); + + it('closes the popover on Escape', () => { + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + openPopover(fixture); + expect(fixture.componentInstance.isOpen()).toBe(true); + + document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); + fixture.detectChanges(); + + expect(fixture.componentInstance.isOpen()).toBe(false); + expect(fixture.nativeElement.querySelector('.selected-variables-popover')).toBeNull(); + }); + + it('closes on an outside click but stays open on an inside click', () => { + const fixture = TestBed.createComponent(SelectedVariablesComponent); + fixture.detectChanges(); + openPopover(fixture); + + // A click on the popover's own content must not close it. + const popover = fixture.nativeElement.querySelector('.selected-variables-popover') as HTMLElement; + popover.dispatchEvent(new MouseEvent('click', { bubbles: true })); + fixture.detectChanges(); + expect(fixture.componentInstance.isOpen()).toBe(true); + + document.body.dispatchEvent(new MouseEvent('click', { bubbles: true })); + fixture.detectChanges(); + expect(fixture.componentInstance.isOpen()).toBe(false); + }); +}); diff --git a/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.ts b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.ts new file mode 100644 index 000000000..aeb3fbc5f --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/selected-variables/selected-variables.component.ts @@ -0,0 +1,64 @@ +import { ChangeDetectionStrategy, Component, computed, output, inject, input, signal, ElementRef } from '@angular/core'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; +import { D3HierarchyNode } from '../../../../models/data-model.interface'; +import { unavailableTooltipFor } from '../../../../core/variable-availability.utils'; + + +@Component({ + selector: 'app-selected-variables', + templateUrl: './selected-variables.component.html', + styleUrl: './selected-variables.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, + host: { + '(document:click)': 'onOutsideClick($event)', + '(document:keydown.escape)': 'close()', + }, +}) +export class SelectedVariablesComponent { + private expStudioService = inject(ExperimentStudioService); + private elementRef = inject(ElementRef); + + readonly selectedNode = input<any>(); + readonly mode = input<'popover' | 'inline'>('popover'); + readonly variableClicked = output<any>(); + + readonly isOpen = signal(false); + + /** The single assignable pool (data-model variables only). */ + readonly variables = computed(() => this.expStudioService.selectedVariables()); + + onVariableClick(node: D3HierarchyNode): void { + // Picked before a dataset change made it unavailable: kept in the pool, but inert. + if (this.unavailableTooltip(node)) return; + this.variableClicked.emit(node); + } + + /** "Not available in: …" for a pooled variable missing from a selected dataset, '' otherwise. */ + unavailableTooltip(node: D3HierarchyNode): string { + return unavailableTooltipFor(this.expStudioService.unavailableVariables(), { code: node?.code }); + } + + toggle(): void { + this.isOpen.update((open) => !open); + } + + close(): void { + this.isOpen.set(false); + } + + onOutsideClick(event: Event): void { + if (!this.elementRef.nativeElement.contains(event.target)) { + this.close(); + } + } + + removeItem(item: any): void { + this.expStudioService.setVariables( + this.variables().filter((v) => v.code !== item.code) + ); + } + + clearList(): void { + this.expStudioService.setVariables([]); + } +} diff --git a/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.css b/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.css deleted file mode 100644 index c7c5f8d33..000000000 --- a/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.css +++ /dev/null @@ -1,512 +0,0 @@ -/* ===== PARAMETERS WRAPPER ===== */ - -.parameters-section { - display: flex; - flex-direction: column; - gap: 0.35rem; -} - -.parameters-header { - font-size: 0.92rem; - color: var(--primary-color); - margin: -10px -12px 10px; - font-weight: 600; - letter-spacing: 0.01em; - padding: 8px 12px; - border-bottom: 1px solid var(--studio-card-header-border, rgba(226, 232, 240, 0.9)); - background: var(--studio-card-header-bg, rgba(241, 245, 249, 0.85)); - border-top-left-radius: 12px; - border-top-right-radius: 12px; -} - -/* Main layout: three equal columns */ -.parameters-content { - display: grid; - grid-template-columns: repeat(3, minmax(0, 1fr)); - gap: 0.6rem; - padding: 0.4rem 0.15rem 0.4rem; -} - -/* ===== COLUMN / SECTION ===== */ - -.section-container { - display: flex; - flex-direction: column; - gap: 0.45rem; - width: 100%; - min-width: 0; -} - -/* Top row for button + bin */ -.panel-controls { - display: flex; - align-items: center; - justify-content: space-between; - gap: 0.6rem; - flex-wrap: nowrap; - width: 100%; - margin-left: 0; - position: relative; - z-index: 2; - pointer-events: auto; -} - -.panel-title { - display: inline-flex; - align-items: center; - gap: 8px; - font-size: 0.82rem; - font-weight: 700; - color: #ffffff; - padding: 4px 10px; - border-radius: 999px; - border: none; - cursor: pointer !important; -} - -.panel-title:disabled { - cursor: not-allowed !important; - opacity: 0.6; -} - -.panel-title * { - cursor: inherit; -} - -.panel-title--variable { - background: var(--variable-color, #2b33e9); -} - -.panel-title--covariate { - background: var(--covariate-color, #ccb692); - color: #ffffff; -} - -.panel-title--filter { - background: var(--filter-color, #94a3b8); - color: #ffffff; -} - -/* ===== BUTTONS & COUNTERS ===== */ -.button-container { - display: inline-flex; - align-items: center; - position: relative; -} - -/* Pill buttons */ -.variable-btn, -.covariate-btn, -.filter-btn { - position: relative; - color: #fff; - background: var(--variable-color, #2b33e9); - border: none; - padding: 0.45rem 0.9rem; - border-radius: 999px; - font-size: 0.82rem; - font-weight: 600; - cursor: pointer; - transition: transform 0.2s ease, box-shadow 0.2s ease, filter 0.2s ease; - width: 135px; - text-align: center; - white-space: nowrap; -} - -.covariate-btn { - background: var(--covariate-color, #ccb692); - color: #ffffff; -} - -.filter-btn { - background: var(--filter-color, #94a3b8); - color: #ffffff; -} - -.variable-btn:hover, -.covariate-btn:hover, -.filter-btn:hover { - transform: translateY(-1px); - filter: brightness(1.04); - box-shadow: 0 8px 18px rgba(15, 23, 42, 0.25); -} - -.variable-btn:active, -.covariate-btn:active, -.filter-btn:active { - transform: translateY(0); - box-shadow: 0 4px 10px rgba(15, 23, 42, 0.18); -} - -.variable-btn:disabled, -.covariate-btn:disabled, -.filter-btn:disabled { - background: #e5e7eb !important; - color: #9ca3af !important; - cursor: not-allowed; - box-shadow: none; - filter: none; -} - -/* Counter */ -.counter { - position: absolute; - top: -7px; - right: -8px; - width: 22px; - height: 22px; - border-radius: 999px; - background: var(--primary-color); - color: #f9fafb; - font-size: 0.72rem; - font-weight: 700; - display: flex; - justify-content: center; - align-items: center; - box-shadow: 0 4px 10px rgba(43, 51, 233, 0.55); -} - -.counter--inline { - position: static; - width: auto; - height: auto; - padding: 2px 8px; - font-size: 0.7rem; - border-radius: 999px; - background: rgba(255, 255, 255, 0.25); - color: #ffffff; - font-weight: 700; - box-shadow: none; -} - -.counter--variable, -.counter--covariate, -.counter--filter { - background: rgba(255, 255, 255, 0.2); -} - -/* Clear bin */ -.bin-btn { - width: 30px; - height: 30px; - border-radius: 999px; - border: none; - background: #fee2e2; - color: #dc2626; - display: flex; - justify-content: center; - align-items: center; - font-size: 0.9rem; - box-shadow: 0 3px 8px rgba(248, 113, 113, 0.45); - transition: transform 0.2s ease, background 0.2s ease; - margin-left: 0; -} - -.bin-btn:hover { - background: #fecaca; - transform: translateY(-1px) scale(1.06); -} - -.bin-btn i { - margin: 0; -} - -/* ===== PANEL / LIST ===== */ -.panel-container { - position: relative; - border-radius: 10px; - min-height: 9.2rem; - padding: 4px 8px; - box-sizing: border-box; - background: var(--studio-card-bg, rgba(255, 255, 255, 0.4)); - border: 1px solid rgba(226, 232, 240, 0.6); - box-shadow: 0 2px 6px rgba(15, 23, 42, 0.04); - overflow: visible; - transition: all 0.3s ease; -} - -.panel { - width: 100%; - min-height: 8.5rem; - max-height: 20rem; - overflow-y: auto; - position: relative; -} - -.panel::-webkit-scrollbar { - width: 6px; -} - -.panel::-webkit-scrollbar-track { - background: transparent; -} - -.panel::-webkit-scrollbar-thumb { - background: rgba(148, 163, 184, 0.7); - border-radius: 999px; -} - -/* List */ -.horizontal-list { - list-style-type: none; - padding: 0; - margin: 0; - min-height: 40px; -} - -/* Drag preview */ -.horizontal-item.cdk-drag-preview { - background-color: rgba(255, 255, 255, 0.98); - border-radius: 10px; - box-shadow: 0 8px 18px rgba(15, 23, 42, 0.35); - transform: scale(1.03); - opacity: 0.98; - z-index: 1001; - pointer-events: none; -} - -/* Item row */ -.horizontal-item { - display: flex; - justify-content: space-between; - align-items: center; - padding: 0.35rem 0; - border-bottom: 1px solid rgba(226, 232, 240, 0.85); - transition: background-color 0.15s ease, transform 0.15s ease; - cursor: pointer; -} - -.horizontal-item:last-child { - border-bottom: none; -} - -.horizontal-item:hover { - background: rgba(15, 118, 110, 0.03); -} - -/* Info block */ -.variable-info { - display: flex; - flex-direction: column; - flex-grow: 1; - min-width: 0; -} - -.variable-name { - font-weight: 500; - font-size: 0.9rem; - margin: 0; - color: #0f172a; - white-space: nowrap; - text-overflow: ellipsis; - overflow: hidden; -} - -.variable-type { - font-size: 0.76rem; - color: #6b7280; -} - -/* Remove icon */ -.remove-btn { - font-size: 1rem; - color: #ef4444; - background: none; - border: none; - cursor: pointer; - transition: transform 0.15s ease, color 0.15s ease; - padding: 2px 4px; -} - -.remove-btn:hover { - transform: scale(1.15); - color: #b91c1c; -} - -/* ===== HIGHLIGHT STATES ===== */ - -.highlighted-variable, -.highlighted-covariate, -.highlighted-filter { - border-radius: 10px; - box-shadow: 0 3px 10px rgba(34, 197, 235, 0.18); - transform: translateY(-1px); -} - -.highlighted-variable { - background: linear-gradient(90deg, #f0f7ff, #f8fbff); - border-left: 3px solid var(--variable-color, #2b33e9); -} - -.highlighted-covariate { - background: linear-gradient(90deg, #faf6ef, #fdfbf7); - border-left: 3px solid var(--covariate-color, #ccb692); -} - -.highlighted-filter { - background: linear-gradient(90deg, #f1f5f9, #f8fafc); - border-left: 3px solid var(--filter-color, #94a3b8); -} - -/* ===== COG BUTTON ===== */ - -.cog-btn { - position: absolute; - bottom: -14px; - right: 8px; - background: #ffffff; - color: #64748b; - border: 1px solid #e2e8f0; - border-radius: 10px; - width: 32px; - height: 32px; - display: flex; - justify-content: center; - align-items: center; - box-shadow: 0 4px 12px rgba(15, 23, 42, 0.08); - cursor: pointer; - font-size: 0.95rem; - transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1); - z-index: 10; -} - -.cog-btn:hover { - background: var(--primary-color); - color: #ffffff; - border-color: var(--primary-color); - transform: translateY(-2px); - box-shadow: 0 8px 16px rgba(43, 51, 233, 0.25); -} - -.cog-btn:active { - transform: translateY(0) scale(0.95); -} - -.cog-btn:disabled { - background: #f1f5f9; - color: #94a3b8; - opacity: 0.6; - cursor: not-allowed; - pointer-events: none; - box-shadow: none; - border-color: #f1f5f9; -} - -/* ===== RESPONSIVE ===== */ - -@media (max-width: 1440px) { - - .variable-btn, - .covariate-btn, - .filter-btn { - width: 125px; - padding: 0.4rem 0.75rem; - font-size: 0.8rem; - } - - .counter { - width: 20px; - height: 20px; - font-size: 0.7rem; - } - - .panel-controls { - gap: 0.6rem; - } - - .bin-btn { - width: 28px; - height: 28px; - font-size: 0.8rem; - } -} - -@media (max-width: 1024px) { - .parameters-content { - grid-template-columns: minmax(0, 1fr); - /* stack columns */ - } - - .panel-controls { - justify-content: flex-start; - } -} - -@media (max-width: 768px) { - .parameters-content { - padding: 0.25rem 0; - gap: 0.6rem; - } - - .panel { - max-height: 9.5rem; - } - - .panel-controls { - align-items: flex-start; - gap: 0.45rem; - } -} - -@keyframes pulse-blue { - 0% { - box-shadow: 0 0 0 0 rgba(8, 144, 166, 0.7); - transform: scale(1); - } - - 70% { - box-shadow: 0 0 0 10px rgba(8, 144, 166, 0); - transform: scale(1.05); - } - - 100% { - box-shadow: 0 0 0 0 rgba(8, 144, 166, 0); - transform: scale(1); - } -} - -@keyframes pulse-gold { - 0% { - box-shadow: 0 0 0 0 rgba(200, 141, 0, 0.7); - transform: scale(1); - } - - 70% { - box-shadow: 0 0 0 10px rgba(200, 141, 0, 0); - transform: scale(1.05); - } - - 100% { - box-shadow: 0 0 0 0 rgba(200, 141, 0, 0); - transform: scale(1); - } -} - -@keyframes pulse-green { - 0% { - box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.7); - transform: scale(1); - } - - 70% { - box-shadow: 0 0 0 10px rgba(22, 163, 74, 0); - transform: scale(1.05); - } - - 100% { - box-shadow: 0 0 0 0 rgba(22, 163, 74, 0); - transform: scale(1); - } -} - -.pulse-hint--variable { - animation: pulse-blue 2s infinite; -} - -.pulse-hint--covariate { - animation: pulse-gold 2s infinite; -} - -.pulse-hint--filter { - animation: pulse-green 2s infinite; -} \ No newline at end of file diff --git a/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.html b/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.html deleted file mode 100644 index d8f2ecd8e..000000000 --- a/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.html +++ /dev/null @@ -1,137 +0,0 @@ -<div class="parameters-section"> - <!-- Header --> - <div class="parameters-header"> - <label>Parameters</label> - </div> - - <!-- Content --> - <div class="parameters-content"> - <!-- Variables Section --> - <div class="section-container"> - <div class="panel-controls"> - <button type="button" class="panel-title panel-title--variable" - [class.pulse-hint--variable]="isNodeSelectedAndNotInList('variables')" [disabled]="!hasSelectedDatasets" - (click)="addVariable()"> - <span>+ Variables</span> - <span class="counter counter--inline counter--variable">{{ variables.length }}</span> - </button> - @if (variables.length > 0) { - <button class="bin-btn" (click)="clearList('variables')"> - <i class="fa fa-trash"></i> - </button> - } - </div> - - <div class="panel-container"> - <div class="panel"> - <ul class="horizontal-list" cdkDropList #varList="cdkDropList" [cdkDropListData]="variables" - [cdkDropListConnectedTo]="[covList, filtList]" (cdkDropListDropped)="drop($event, 'variables')"> - @for (variable of variables; track variable) { - <li class="horizontal-item" [ngClass]="{ - 'highlighted-variable': selectedNode?.code === variable.code - }" cdkDrag (click)="onVariableClick(variable)"> - <div class="variable-info"> - <span class="variable-name">{{ variable.label }}</span> - <span class="variable-type">type: {{ variable.type }}</span> - </div> - <button class="remove-btn" (click)=" - removeItem(variable, 'variables'); $event.stopPropagation() - "> - ✖ - </button> - </li> - } - </ul> - </div> - </div> - </div> - - <!-- Covariates Section --> - <div class="section-container"> - <div class="panel-controls"> - <button type="button" class="panel-title panel-title--covariate" - [class.pulse-hint--covariate]="isNodeSelectedAndNotInList('covariates')" [disabled]="!hasSelectedDatasets" - (click)="addCovariate()"> - <span>+ Covariates</span> - <span class="counter counter--inline counter--covariate">{{ covariates.length }}</span> - </button> - @if (covariates.length > 0) { - <button class="bin-btn" (click)="clearList('covariates')"> - <i class="fa fa-trash"></i> - </button> - } - </div> - - <div class="panel-container"> - <div class="panel"> - <ul class="horizontal-list" cdkDropList #covList="cdkDropList" [cdkDropListData]="covariates" - [cdkDropListConnectedTo]="[varList, filtList]" (cdkDropListDropped)="drop($event, 'covariates')"> - @for (covariate of covariates; track covariate) { - <li class="horizontal-item" [ngClass]="{ - 'highlighted-variable highlighted-covariate': - selectedNode?.code === covariate.code - }" cdkDrag (click)="onVariableClick(covariate)"> - <div class="variable-info"> - <span class="variable-name">{{ covariate.label }}</span> - <span class="variable-type">type: {{ covariate.type }}</span> - </div> - <button class="remove-btn" (click)=" - removeItem(covariate, 'covariates'); $event.stopPropagation() - "> - ✖ - </button> - </li> - } - </ul> - </div> - </div> - </div> - - <!-- Filters Section --> - <div class="section-container"> - <div class="panel-controls"> - <button type="button" class="panel-title panel-title--filter" - [class.pulse-hint--filter]="isNodeSelectedAndNotInList('filters')" [disabled]="!hasSelectedDatasets" - (click)="addFilter()"> - <span>+ Filters</span> - <span class="counter counter--inline counter--filter">{{ filters.length }}</span> - </button> - @if (filters.length > 0) { - <button class="bin-btn" (click)="clearList('filters')"> - <i class="fa fa-trash"></i> - </button> - } - </div> - - <div class="panel-container"> - <div class="panel"> - <ul class="horizontal-list" cdkDropList #filtList="cdkDropList" [cdkDropListData]="filters" - [cdkDropListConnectedTo]="[varList, covList]" (cdkDropListDropped)="drop($event, 'filters')"> - @for (filter of filters; track filter) { - <li class="horizontal-item" [ngClass]="{ - 'highlighted-variable highlighted-filter': - selectedNode?.code === filter.code - }" cdkDrag (click)="onVariableClick(filter)"> - <div class="variable-info"> - <span class="variable-name">{{ filter.label }}</span> - <span class="variable-type">type: {{ filter.type }}</span> - </div> - <button class="remove-btn" (click)=" - removeItem(filter, 'filters'); $event.stopPropagation() - "> - ✖ - </button> - </li> - } - </ul> - </div> - - <button class="cog-btn" (click)="openFilterConfig()" [disabled]="filters.length === 0" - [ngClass]="{ disabled: filters.length === 0 }"> - <i class="fa fa-cog"></i> - </button> - </div> - - </div> - </div> -</div> \ No newline at end of file diff --git a/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.ts b/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.ts deleted file mode 100644 index 73d0b58c1..000000000 --- a/src/app/pages/experiment-studio/variables-panel/variable-filter-selection/variable-filter-selection.component.ts +++ /dev/null @@ -1,223 +0,0 @@ -import { Component, Input, Output, EventEmitter, OnInit, effect } from '@angular/core'; -import { CommonModule } from '@angular/common'; -import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; -import { CdkDragDrop, DragDropModule, transferArrayItem } from '@angular/cdk/drag-drop' -import { D3HierarchyNode } from '../../../../models/data-model.interface'; - - -@Component({ - selector: 'app-variable-filter-selection', - templateUrl: './variable-filter-selection.component.html', - styleUrls: ['./variable-filter-selection.component.css'], - imports: [CommonModule, DragDropModule] -}) -export class VariableFilterSelectionComponent implements OnInit { - @Input() selectedNode: any; // Selected node from the bubble chart - @Input() groupVariables: any[] = []; - @Output() filtersChange = new EventEmitter<any[]>(); - @Output() variableClicked = new EventEmitter<any>(); - - filterLogic: any; - variables: any[] = []; - covariates: any[] = []; - filters: any[] = []; - - constructor(private expStudioService: ExperimentStudioService) { - effect(() => { - // todo: check and remove if not bugs appear - // activates availableGroupedAlgorithms to recalculate available algorithms - this.expStudioService.availableGroupedAlgorithms(); - }); - - effect(() => { - this.variables = this.expStudioService.selectedVariables(); - }); - - effect(() => { - this.covariates = this.expStudioService.selectedCovariates(); - }); - - effect(() => { - this.filters = this.expStudioService.selectedFilters(); - }); - } - - ngOnInit(): void { - - } - - onVariableClick(node: D3HierarchyNode): void { - this.variableClicked.emit(node); - } - - - private getLeafNodes(node: any): any[] { - const leaves: any[] = []; - - function collectLeaves(n: any) { - if (!n.children || n.children.length === 0) { - leaves.push(n); - } else { - n.children.forEach(collectLeaves); - } - } - - collectLeaves(node); - return leaves; - } - - get hasSelectedDatasets(): boolean { - return (this.expStudioService.selectedDatasets() || []).length > 0; - } - - addItem(listName: 'variables' | 'covariates' | 'filters'): void { - const datasets = this.expStudioService.selectedDatasets(); - if (!datasets || datasets.length === 0) { - return; - } - if (!this.selectedNode) return; - - // Select all leaves from selected node - let itemsToAdd = this.selectedNode.children ? this.getLeafNodes(this.selectedNode) : [this.selectedNode]; - - // Safety: don't add hundreds of filters at once if a group is selected by mistake - if (listName === 'filters' && itemsToAdd.length > 5) { - console.warn(`[VariableFilterSelection] Blocking bulk filter addition (${itemsToAdd.length} items). Please select individual variables.`); - return; - } - - const list = this[listName]; - const updated = [ - ...list, - ...itemsToAdd.filter(item => !list.some(existing => existing.code === item.code)) - ]; - - this[listName] = updated; - - if (listName === 'variables') { - itemsToAdd.forEach(item => this.expStudioService.addVariableAndEnrich(item)); - } else { - this.updateService(listName, updated); - } - } - - drop(event: CdkDragDrop<any[]>, listName: 'variables' | 'covariates' | 'filters') { - if (event.previousContainer === event.container) return; - - const item = event.previousContainer.data[event.previousIndex]; - const targetList = event.container.data; - if (targetList.some(v => v.code === item.code)) { - return; - } - - transferArrayItem( - event.previousContainer.data, - event.container.data, - event.previousIndex, - event.currentIndex - ); - - // Sync all lists after move so availability recalculates correctly - this.variables = [...this.variables]; - this.covariates = [...this.covariates]; - this.filters = [...this.filters]; - this.expStudioService.setVariables(this.variables); - this.expStudioService.setCovariates(this.covariates); - this.expStudioService.setFilters(this.filters); - } - - addVariable(): void { - this.addItem('variables'); - } - - addCovariate(): void { - this.addItem('covariates'); - } - - addFilter(): void { - this.addItem('filters'); - } - - isNodeSelectedAndNotInList(listName: 'variables' | 'covariates' | 'filters'): boolean { - if (!this.selectedNode || this.selectedNode.children) return false; - const list = this[listName]; - return !list.some(item => item.code === this.selectedNode.code); - } - - removeItem(item: any, listName: string): void { - switch (listName) { - case 'variables': - this.variables = this.variables.filter((v) => v.label !== item.label); - this.updateService('variables', [...this.variables]); - break; - case 'covariates': - this.covariates = this.covariates.filter((c) => c.label !== item.label); - this.updateService('covariates', [...this.covariates]); - break; - case 'filters': - this.filters = this.filters.filter((f) => f.label !== item.label); - this.updateFilters([...this.filters]); - this.updateService('filters', [...this.filters]); - break; - default: - throw new Error(`Invalid listName: ${listName}`); - } - } - - clearList(listName: string): void { - switch (listName) { - case 'variables': - this.variables = []; - this.updateService('variables', []); - break; - case 'covariates': - this.covariates = []; - this.updateService('covariates', []); - break; - case 'filters': - this.filters = []; - this.updateService('filters', []); - break; - default: - throw new Error(`Invalid listName: ${listName}`); - } - } - - openFilterConfig(): void { - this.expStudioService.toggleFilterConfigModal(true); - } - - - // Check for redundant code - updateFilters(updatedFilters: any[]): void { - this.filters = updatedFilters; - this.updateService('filters', updatedFilters); - } - - onFiltersChange(updatedFilters: any[]): void { - if (Array.isArray(updatedFilters)) { - this.filters = [...updatedFilters]; - this.filtersChange.emit(this.filters); // this emits an event if a filter exists - this.updateService('filters', this.filters); - } - } - - onModalClose(): void { - this.expStudioService.toggleFilterConfigModal(false); - } - - // Helper service for VariabeHandlingService - updateService(listName: string, updatedList: any[]): void { - switch (listName) { - case 'variables': - this.expStudioService.setVariables(updatedList); - break; - case 'covariates': - this.expStudioService.setCovariates(updatedList); - break; - case 'filters': - this.expStudioService.setFilters(updatedList); - break; - } - } -} diff --git a/src/app/pages/experiment-studio/variables-panel/variables-panel.component.css b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.css index 712f1a1fd..9ad8b813f 100644 --- a/src/app/pages/experiment-studio/variables-panel/variables-panel.component.css +++ b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.css @@ -12,44 +12,254 @@ .variables-main { display: flex; flex-direction: column; - gap: 24px; - padding: 12px 0 16px; + gap: 16px; + padding: 12px 0 0; align-items: stretch; } -.visualization-header { +/* 1fr / auto / 1fr keeps the view switcher on the card's own center line, + independent of how wide the context chip and search button are. */ +.visualization-toolbar { + display: grid; + grid-template-columns: 1fr auto 1fr; + align-items: center; + column-gap: 12px; + flex: 0 0 auto; + width: 100%; + min-width: 0; + box-sizing: border-box; + padding: 12px 14px; + border: 0; + border-bottom: 1px solid var(--studio-card-header-border); + border-radius: var(--radius-lg, 12px) var(--radius-lg, 12px) 0 0; + background: var(--studio-card-header-bg); + box-shadow: none; +} + +.visualization-toolbar-section { display: flex; - flex-wrap: wrap; - justify-content: space-between; - align-items: flex-end; + align-items: center; gap: 12px; - padding: 2px 0; min-width: 0; - width: 100%; } -.visualization-header-left, -.visualization-header-right { +.visualization-toolbar-context { + position: relative; + flex: 0 1 auto; +} + +.study-context-chip { + display: inline-flex; + align-items: center; + gap: 8px; + min-height: 34px; + max-width: 100%; + padding: 0 12px; + border: 1px solid rgba(127, 156, 232, 0.28); + border-radius: 999px; + background: #ffffff; + color: var(--text-main); + cursor: pointer; + font-family: inherit; + font-size: 0.82rem; + font-weight: 600; + line-height: 1.2; + transition: + border-color 0.15s ease, + box-shadow 0.15s ease, + background-color 0.15s ease; +} + +.study-context-chip:hover { + border-color: rgba(43, 51, 233, 0.35); + box-shadow: 0 2px 8px rgba(43, 51, 233, 0.08); + background: var(--studio-card-header-bg); +} + +.study-context-chip[aria-expanded="true"] { + border-color: var(--primary-color); + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.12); + background: #ffffff; +} + +.study-context-chip:focus-visible { + outline: none; + border-color: var(--primary-color); + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.18); +} + +.study-context-value { + white-space: nowrap; + font-weight: 650; + color: var(--text-main); +} + +.study-context-sep { + color: #cbd5e1; + font-weight: 400; +} + +.study-context-count { + display: inline-flex; + align-items: center; + gap: 5px; + color: var(--primary-color); + font-size: 0.74rem; + font-weight: 700; + font-variant-numeric: tabular-nums; + white-space: nowrap; +} + +.study-context-count i { + font-size: 0.7rem; + color: var(--primary-color); +} + +.study-context-chevron { + font-size: 0.65rem; + color: var(--primary-color); + opacity: 0.8; + transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1); +} + +.study-context-chip[aria-expanded="true"] .study-context-chevron { + transform: rotate(180deg); +} + +.study-context-panel { + position: absolute; + top: calc(100% + 8px); + left: 0; + z-index: 1200; display: flex; - align-items: flex-end; + flex-direction: column; + gap: 14px; + width: 380px; + max-width: calc(100vw - 32px); + padding: 16px 18px; + border: 1px solid rgba(43, 51, 233, 0.14); + border-radius: 16px; + background: #ffffff; + box-shadow: + 0 14px 34px -4px rgba(15, 23, 42, 0.12), + 0 4px 12px -2px rgba(43, 51, 233, 0.06), + 0 0 0 1px rgba(43, 51, 233, 0.04); + animation: study-context-pop 0.18s cubic-bezier(0.16, 1, 0.3, 1); +} + +.study-context-divider { + height: 1px; + background: rgba(43, 51, 233, 0.08); + margin: 0 -2px; +} + +.study-context-panel app-data-model-selector, +.study-context-panel app-dataset-selector { + width: 100%; +} + +@keyframes study-context-pop { + from { + opacity: 0; + transform: translateY(-4px); + } + + to { + opacity: 1; + transform: translateY(0); + } +} + +.visualization-toolbar-modes { + justify-self: center; min-width: 0; + display: flex; } -.visualization-header-right { - flex: 0 1 400px; - max-width: 400px; - margin-right: 20px; - /* Pull slightly left to prevent overflow */ +.visualization-toolbar-modes .metadata-browser-switcher { + flex: 0 0 auto; } +.visualization-toolbar-search { + position: relative; + justify-self: end; + flex: 0 0 auto; +} -.visualization-header-right app-search-bar { - width: 100%; +.search-popover { + position: absolute; + top: calc(100% + 6px); + right: 0; + z-index: 1200; + width: 340px; + max-width: calc(100vw - 32px); + padding: 0; + border: 1px solid rgba(43, 51, 233, 0.14); + border-radius: 12px; + background: #ffffff; + box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12), 0 2px 6px rgba(43, 51, 233, 0.04); + overflow: hidden; + animation: study-context-pop 0.16s ease-out; +} + +.search-popover app-search-bar { + display: block; +} + +.search-toggle-btn { + display: inline-flex; + align-items: center; + justify-content: center; + width: auto; + height: 34px; + gap: 8px; + padding: 0 12px; + border: 1px solid rgba(127, 156, 232, 0.28); + border-radius: 999px; + background: #ffffff; + color: var(--text-muted); + cursor: pointer; + font-size: 0.8rem; + font-weight: 600; + transition: + color 0.15s ease, + border-color 0.15s ease, + box-shadow 0.15s ease, + background-color 0.15s ease; +} + +.search-toggle-btn:hover, +.search-toggle-btn:focus-visible { + color: var(--primary-color); + border-color: rgba(43, 51, 233, 0.35); + background: var(--studio-card-header-bg); + outline: none; + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.1); +} + +.study-context-chip, +.search-toggle-btn { + border-color: rgba(43, 51, 233, 0.12); +} + +.search-toggle-btn.is-active { + color: var(--primary-color); + border-color: rgba(43, 51, 233, 0.35); + background: color-mix(in srgb, var(--primary-light) 18%, white); + box-shadow: none; +} + +.metadata-mode-btn { + padding: 5px 10px; +} + +.metadata-mode-btn span { + display: none; } .visualization-parameters-row { display: grid; - grid-template-columns: 1.1fr 0.9fr; + grid-template-columns: minmax(0, 55fr) minmax(0, 45fr); gap: 24px; align-items: start; } @@ -57,42 +267,46 @@ .bubble-column { display: flex; flex-direction: column; - gap: 12px; + gap: 0; min-width: 0; width: 100%; + height: auto; align-items: stretch; + align-self: start; + container-type: inline-size; + container-name: bubble-column; } .right-column { display: flex; flex-direction: column; gap: 16px; - height: 100%; + min-width: 0; + width: 100%; + align-self: start; } /* Left: bubble chart “canvas” */ +/* Stacking context: keeps toolbar popovers (context sheet, search) above the + card's own content but below the sticky stepper and studio overlays. */ .bubble-chart-section { + position: relative; + z-index: 1; width: 100%; max-width: 100%; - /* Removed max-height calc to allow filling the container */ - /* max-height: calc(100vh - 300px); */ - /* aspect-ratio: 1 / 1; REMOVED square constraint */ - aspect-ratio: 1 / 1; flex-grow: 0; + min-height: 0; + height: auto; display: flex; flex-direction: column; - background: var(--studio-card-bg, rgba(255, 255, 255, 0.72)); - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - border-radius: 16px; + align-items: stretch; + background: var(--studio-card-bg); + border-radius: var(--radius-lg, 12px); border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.3)); padding: 0; box-shadow: var(--studio-card-shadow, 0 8px 32px rgba(15, 23, 42, 0.08)); box-sizing: border-box; - overflow: visible; - display: flex; - align-items: stretch; - justify-content: stretch; + overflow: hidden; scroll-margin-top: 90px; } @@ -101,295 +315,562 @@ height: 100%; } -.bubble-actions { +.metadata-browser-shell { display: flex; - flex-wrap: wrap; - gap: 8px; - align-items: center; -} - -.bubble-search { + flex: 1 1 auto; + flex-direction: column; + min-height: 520px; width: 100%; + box-sizing: border-box; + position: relative; } -.bubble-controls { - display: flex; - align-items: center; +.metadata-browser-shell.is-map { + aspect-ratio: auto; + min-height: 0; } -.bubble-selectors { - display: flex; - align-items: flex-end; - gap: 16px; +.metadata-browser-switcher { + --segment-active-index: 0; + --segment-pad: 3px; + --segment-gap: 2px; + position: relative; + display: grid; + grid-template-columns: repeat(3, 36px); + gap: var(--segment-gap); + flex: 0 0 auto; + padding: var(--segment-pad); + overflow: hidden; + border: 0; + border-radius: 999px; + background: rgba(148, 163, 184, 0.12); + box-shadow: inset 0 0 0 1px var(--border-color); + isolation: isolate; } -.bubble-selectors app-data-model-selector :deep(.data-model-select), -.bubble-selectors app-dataset-selector :deep(.dataset-btn) { - height: 36px; +.metadata-segment-track { + position: absolute; + inset: var(--segment-pad); + z-index: 0; + pointer-events: none; + overflow: hidden; + border-radius: 999px; } -.bubble-action { +.metadata-segment-indicator { + box-sizing: border-box; + position: absolute; + top: 0; + bottom: 0; + left: 0; + width: calc((100% - 2 * var(--segment-gap)) / 3); + border-radius: 999px; + background: var(--card-bg); + border: 0; + box-shadow: var(--shadow-sm); + transform: translate3d( + calc(var(--segment-active-index) * (100% + var(--segment-gap))), + 0, + 0 + ); + transition: transform 0.28s ease; +} + +.metadata-mode-btn { position: relative; - color: #fff; - border: none; - padding: 8px 16px; - border-radius: var(--radius-sm); - font-size: 0.78rem; - font-weight: 600; + z-index: 1; + display: inline-flex; + align-items: center; + justify-content: center; + width: 36px; + height: 28px; + padding: 0; + border: 0; + border-radius: 999px; + background: transparent; + color: var(--text-muted); cursor: pointer; - transition: all 0.2s ease; - white-space: nowrap; - box-shadow: var(--shadow-md); + opacity: 1; + transition: + color 0.28s ease, + opacity 0.28s ease, + transform 0.28s var(--ease-standard); } -.bubble-action--variable { - background: var(--primary-color); - border-radius: var(--radius-sm); +.metadata-mode-btn i { + display: inline-flex; + align-items: center; + justify-content: center; + width: 1.1rem; + height: 1.1rem; + font-size: 0.8rem; + border-radius: 999px; + background: transparent; + color: inherit; + transition: + transform 0.32s var(--ease-standard), + background 0.28s ease, + color 0.28s ease, + box-shadow 0.28s ease; } -.bubble-action--covariate { - background: #ffeeba; - /* Muted pastel orange */ - color: #0f172a; - border-radius: var(--radius-sm); +.metadata-mode-btn:hover, +.metadata-mode-btn:focus-visible { + color: var(--primary-color); + opacity: 1; + outline: none; } -.bubble-action--filter { - background: #c5d4f0; - /* Muted pastel blue */ - color: #0f172a; - border-radius: var(--radius-sm); +.metadata-mode-btn:active { + transform: scale(0.98); } -.bubble-action:hover { - transform: translateY(-1px); - box-shadow: var(--shadow-lg); - filter: brightness(1.1); +.metadata-mode-btn.active { + color: var(--primary-color); + opacity: 1; } -.bubble-action:disabled { - background: #e5e7eb !important; - color: #9ca3af !important; - cursor: not-allowed; - box-shadow: none; - filter: none; +.metadata-mode-btn.active i { + color: var(--primary-color); } -.bubble-action-count { - margin-left: 8px; - padding: 1px 7px; - border-radius: var(--radius-sm); - background: rgba(255, 255, 255, 0.85); - color: #0f172a; - font-size: 0.72rem; - font-weight: 700; +.metadata-browser-content { + flex: 1; + min-height: 0; + overflow: hidden; +} + +.metadata-browser-content app-bubble-chart, +.metadata-browser-content app-ontology-tree-browser, +.metadata-browser-content app-collapsible-tree-browser { + display: block; + height: 100%; } -/* Right column: parameters + distribution */ +/* Right column: parameters + histogram */ /* Common block styling */ .panel-block { - background: var(--studio-card-bg, rgba(255, 255, 255, 0.88)); - backdrop-filter: blur(10px); - -webkit-backdrop-filter: blur(10px); - border-radius: 16px; + background: var(--studio-card-bg); + border-radius: var(--radius-lg, 12px); border: 1px solid var(--studio-card-border, rgba(148, 163, 184, 0.35)); padding: 16px 20px; - box-shadow: var(--studio-card-shadow, 0 8px 32px rgba(15, 23, 42, 0.08)); + box-shadow: var(--studio-card-shadow); box-sizing: border-box; } -/* Variable / covariate / filter panel */ -.variable-filter-container { - min-height: 140px; +.histogram-panel { flex: 0 0 auto; + position: relative; + display: flex; + flex-direction: column; + align-self: stretch; + min-width: 0; + min-height: 0; + overflow: hidden; + padding: 0; } -.parameters-panel { +.details-panel-header { + position: sticky; + top: 0; + z-index: 20; + margin: 0; + padding: 14px 16px 12px; + border-bottom: 1px solid var(--studio-card-header-border, rgba(226, 232, 240, 0.9)); + border-radius: 0; + background: var(--studio-card-header-bg); display: flex; flex-direction: column; - gap: 16px; + align-items: stretch; + gap: 12px; + overflow: visible; +} + +.details-panel-header-main { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 12px; + min-width: 0; +} + +.details-panel-header-main { + padding-right: 0; +} + +.selection-toggle-btn { + display: inline-flex; + align-items: center; + justify-content: center; + flex-shrink: 0; + gap: 8px; + height: 34px; + min-height: 0; + padding: 0 14px; + border: 1px solid var(--primary-color); + border-radius: 999px; + background: var(--primary-color); + color: #ffffff; + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + letter-spacing: 0.01em; + line-height: 1; + cursor: pointer; + transition: + background-color 0.18s ease, + color 0.18s ease, + box-shadow 0.18s ease, + opacity 0.16s ease; +} + +.selection-toggle-btn i { + font-size: 0.68rem; +} + +.selection-toggle-btn:disabled { + cursor: not-allowed; + opacity: 0.45; +} + +.selection-toggle-btn:not(:disabled):hover, +.selection-toggle-btn:not(:disabled):focus-visible { + background: var(--primary-dark); + outline: none; + box-shadow: 0 0 0 2px rgba(43, 51, 233, 0.22); +} + +.selection-toggle-btn.is-remove { + background: var(--card-bg); + border-color: rgba(127, 156, 232, 0.38); + color: var(--primary-color); +} + +.selection-toggle-btn.is-remove:not(:disabled):hover, +.selection-toggle-btn.is-remove:not(:disabled):focus-visible { + background: rgba(43, 51, 233, 0.06); +} + +.pulse-hint--variable { + box-shadow: 0 0 0 2px rgba(255, 186, 8, 0.55); +} + +.histogram-graph { position: relative; - z-index: 10; + display: block; + min-width: 0; + overflow: visible; } -@media (max-width: 1200px) { - .visualization-parameters-row { - grid-template-columns: 1fr; - } +.histogram-graph app-histogram-graph { + display: block; +} + +.details-panel-title-block { + display: flex; + flex-direction: column; + gap: 4px; + min-width: 0; + flex: 1 1 auto; +} + +.details-panel-identity { + min-width: 0; +} + +.details-panel-heading { + display: flex; + flex-direction: column; + gap: 3px; + min-width: 0; + flex: 1 1 auto; +} + +.details-panel-eyebrow { + font-size: 0.72rem; + font-weight: 700; + line-height: 1.2; + color: var(--primary-color); + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; +} + +.details-panel-identity.is-group .details-panel-eyebrow { + color: #6b5a2e; +} + +.details-panel-title { + margin: 0; + color: var(--text-main); + font-size: 1.05rem; + font-weight: 700; + line-height: 1.25; + overflow: hidden; + white-space: normal; + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; +} + +.details-panel-subtitle { + margin: 0; + color: var(--text-muted); + font-size: 0.78rem; + line-height: 1.35; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +@property --segment-active-index { + syntax: '<number>'; + inherits: true; + initial-value: 0; +} + +.details-segment { + --segment-active-index: 0; + --segment-pad: 3px; + --segment-gap: 2px; + position: relative; + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: var(--segment-gap); + padding: var(--segment-pad); + border: 0; + border-radius: 999px; + background: rgba(148, 163, 184, 0.12); + box-shadow: inset 0 0 0 1px var(--border-color); + isolation: isolate; + transition: --segment-active-index var(--motion-enter) var(--ease-standard); +} + +.details-segment-track { + position: absolute; + inset: var(--segment-pad); + z-index: 0; + pointer-events: none; + border-radius: 999px; +} + +.details-segment-indicator { + position: absolute; + inset: 0 auto 0 0; + width: calc((100% - var(--segment-gap)) / 2); + border-radius: 999px; + background: var(--card-bg); + box-shadow: var(--shadow-sm); + transform: translate3d(calc(var(--segment-active-index) * (100% + var(--segment-gap))), 0, 0); + transition: transform var(--motion-enter) var(--ease-standard); +} + +.details-segment-btn { + position: relative; + z-index: 1; + display: inline-flex; + align-items: center; + justify-content: center; + gap: 7px; + height: 30px; + padding: 0 14px; + border: 0; + border-radius: 999px; + background: transparent; + color: var(--text-muted); + font: inherit; + font-size: 0.78rem; + font-weight: 600; + cursor: pointer; + transition: color var(--motion-exit) var(--ease-standard); +} + +.details-segment-btn i { + font-size: 0.72rem; +} - .bubble-chart-section { - height: 600px; - } +.details-segment-btn:hover, +.details-segment-btn.active { + color: var(--primary-color); } -/* Distribution graph card */ -.distribution-graph { - position: relative; - min-height: 240px; - margin: 0; - flex: 1 1 auto; - display: flex; - flex-direction: column; - width: 100%; - min-width: 0; +.details-segment-btn:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; } -.distribution-panel { - position: relative; - display: flex; - flex-direction: column; - min-height: 0; - min-width: 0; - max-height: 800px; - overflow-y: auto; +.details-tab-panel { + animation: details-tab-enter 0.36s var(--ease-standard) both; } -.distribution-header { - font-size: 0.92rem; - color: var(--primary-color); - margin: -16px -20px 10px; - /* Counteract panel default padding */ - font-weight: 600; - letter-spacing: 0.01em; - padding: 8px 20px; - background: var(--studio-card-header-bg, rgba(241, 245, 249, 0.85)); - border-bottom: 1px solid var(--studio-card-header-border, rgba(226, 232, 240, 0.9)); - border-top-left-radius: 16px; - border-top-right-radius: 16px; - /* Make sure header stays at top if sticky is desired */ - position: sticky; - top: 0; - z-index: 10; +@keyframes details-tab-enter { + from { + opacity: 0; + transform: translateY(8px) scale(0.992); + } + + to { + opacity: 1; + transform: translateY(0) scale(1); + } } +@media (prefers-reduced-motion: reduce) { + .details-segment, + .metadata-browser-switcher { + transition: none; + } + .study-context-panel, + .search-popover { + animation: none; + } -@media (max-width: 1100px) { - .bubble-selectors { - flex-wrap: wrap; + .details-segment-indicator { + transition: transform 0.01ms linear; } -} -@media (max-width: 720px) { - .bubble-selectors { - width: 100%; + .details-segment-btn, + .details-segment-btn i, + .metadata-mode-btn, + .metadata-mode-btn i, + .details-tab-panel { + transition: none; + animation: none; } -} -.distribution-loader { - display: flex; - align-items: center; - justify-content: center; - min-height: 180px; + .details-segment-btn:active, + .details-segment-btn.active i, + .metadata-mode-btn:active, + .metadata-mode-btn.active i { + transform: none; + } } -.histogram-placeholder { - width: 100%; - height: 250px; - background: linear-gradient(90deg, #f0f0f0, #e0e0e0, #f0f0f0); - background-size: 200% 100%; - animation: shimmer 1.5s infinite; - border-radius: 0px; +.histogram-tab-content { + display: flex; + flex-direction: column; + gap: 10px; + min-width: 0; + flex: 1; + min-height: 0; + padding: 10px 16px 10px; } -.group-components-vertical { - display: block; - width: 60%; +.metadata-tab-content { + display: flex; + flex-direction: column; + gap: 10px; + min-width: 0; + flex: 0 0 auto; + padding: 10px 16px 10px; } -/* Adjust "No Data Available" Message */ -.no-data-message { - min-height: 180px; +.metadata-tab-content { + max-height: min(700px, calc(85vh - 120px)); + overflow-y: auto; } -/* Wrapper for the floating button */ -.histogam-placeholder { +.histogram-graph-area { position: relative; - width: 100%; - padding-bottom: 8px; - flex: 1 1 auto; display: flex; + flex-direction: column; + min-width: 0; + flex: 1; + min-height: 0; } -/* ---------- DISTRIBUTION ACTIONS BAR ---------- */ - -.distribution-actions-bar { - position: absolute; - top: 16px; - right: 20px; - z-index: 10; +.histogram-privacy-note { display: flex; - justify-content: flex-end; + align-items: flex-start; + gap: 8px; + margin: 4px 4px 2px; + padding: 0 0 0 8px; + border: 0; + border-left: 2px solid color-mix(in srgb, var(--accent-color) 50%, var(--text-muted)); + border-radius: 0; + background: none; + color: var(--text-muted); + font-size: 0.75rem; + line-height: 1.4; } -.distribution-actions-group { - display: flex; - gap: 10px; +.histogram-privacy-note i { + flex: 0 0 auto; + color: color-mix(in srgb, var(--accent-color) 45%, var(--text-muted)); + font-size: 0.75rem; + line-height: 1.4; } -.distribution-action-btn { - display: inline-flex; +.histogram-toolbar { + display: flex; align-items: center; - justify-content: center; - gap: 8px; - padding: 8px; - width: 40px; - height: 40px; - border-radius: 8px; - font-size: 0.85rem; - font-weight: 600; - cursor: pointer; - transition: all 0.2s ease; - border: 1px solid transparent; - background: #ffffff; - box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05); + justify-content: space-between; + gap: 12px; + min-width: 0; + min-height: 34px; } -.distribution-action-btn i { - font-size: 1.15rem; +/* Relative so the menu still anchors to the button and floats over the chart. */ +.histogram-export-group { + position: relative; + z-index: 3; + display: flex; + align-items: center; + gap: 6px; + width: fit-content; + margin-left: auto; + flex: 0 0 auto; } -.csv-btn { - color: #059669; - border-color: rgba(5, 150, 105, 0.2); +/* Same pill as the summary toolbar Export in statistic-analysis-panel. */ +.histogram-export-group .studio-export-btn { + min-height: 34px; + padding: 0 14px; + border: 0; + border-radius: 999px; + background: var(--card-bg); + box-shadow: inset 0 0 0 1px var(--border-color), var(--shadow-sm); + font-size: 0.78rem; + letter-spacing: 0.01em; } -.csv-btn:hover:not(:disabled) { - background: #ecfdf5; - border-color: #059669; - transform: translateY(-1px); - box-shadow: 0 4px 12px rgba(5, 150, 105, 0.15); +.histogram-export-group .studio-export-btn:hover:not(:disabled), +.histogram-export-group .studio-export-btn[aria-expanded="true"]:not(:disabled) { + background: color-mix(in srgb, var(--primary-color) 6%, var(--card-bg)); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 35%, transparent), var(--shadow-sm); } -.pdf-btn { - color: #dc2626; - border-color: rgba(220, 38, 38, 0.2); +.histogram-export-caret { + font-size: 0.58rem; + transition: transform 0.18s ease; } -.pdf-btn:hover:not(:disabled) { - background: #fef2f2; - border-color: #dc2626; - transform: translateY(-1px); - box-shadow: 0 4px 12px rgba(220, 38, 38, 0.15); +.histogram-export-caret.open { + transform: rotate(180deg); } -.distribution-action-btn:disabled { - opacity: 0.5; - cursor: not-allowed; - filter: grayscale(1); - background: #f1f5f9; - border-color: #e2e8f0; - color: #94a3b8; +.histogram-loader { + display: flex; + align-items: center; + justify-content: center; + min-height: 180px; } .bin-selector { - display: flex; + display: inline-flex; align-items: center; gap: 8px; - font-size: 0.85rem; - color: #64748b; + min-width: 0; + min-height: 34px; + font-size: 0.78rem; font-weight: 600; - margin-right: 8px; + letter-spacing: 0.01em; + color: var(--text-muted); +} + +/* Split variable missing from a selected dataset: grayed out and disabled. */ +.bin-selector option.is-unavailable { + color: var(--unavailable-text); } .bin-selector label { @@ -399,51 +880,75 @@ .bin-selector input { width: 50px; padding: 6px 8px; - border: 1px solid #e2e8f0; - border-radius: 8px; + border: 1px solid var(--border-color); + border-radius: var(--radius-md, 8px); font-size: 0.85rem; - color: #0f172a; + color: var(--text-main); outline: none; transition: all 0.2s ease; - background: #ffffff; - box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05); + background: var(--card-bg); + box-shadow: var(--shadow-sm); text-align: center; } .bin-selector select { - min-width: 180px; - padding: 6px 8px; - border: 1px solid #e2e8f0; - border-radius: 8px; - font-size: 0.85rem; - color: #0f172a; + appearance: none; + -webkit-appearance: none; + width: auto; + min-width: 7.5rem; + max-width: 180px; + min-height: 34px; + padding: 0 34px 0 14px; + border: 0; + border-radius: 999px; + background-color: var(--card-bg); + background-image: + linear-gradient(45deg, transparent 50%, var(--primary-color) 50%), + linear-gradient(135deg, var(--primary-color) 50%, transparent 50%); + background-position: + calc(100% - 17px) 50%, + calc(100% - 12px) 50%; + background-size: 5px 5px, 5px 5px; + background-repeat: no-repeat; + box-shadow: inset 0 0 0 1px var(--border-color), var(--shadow-sm); + color: var(--text-main); + font: inherit; + font-size: 0.78rem; + font-weight: 600; outline: none; - transition: all 0.2s ease; - background: #ffffff; - box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05); } .bin-selector input:focus { border-color: var(--primary-color); - box-shadow: 0 0 0 2px rgba(43, 51, 233, 0.1); + box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary-light) 40%, transparent); +} + +.bin-selector select:hover:not(:disabled) { + background-color: color-mix(in srgb, var(--primary-color) 6%, var(--card-bg)); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 35%, transparent), var(--shadow-sm); } .bin-selector select:focus { - border-color: var(--primary-color); - box-shadow: 0 0 0 2px rgba(43, 51, 233, 0.1); + outline: none; + box-shadow: inset 0 0 0 1px var(--border-color), var(--shadow-sm); +} + +.bin-selector select:focus-visible { + outline: 2px solid var(--primary-color); + outline-offset: 2px; } .bin-selector input:disabled, .bin-selector input.disabled { - background: #f1f5f9; - color: #94a3b8; + background: var(--bg-neutral); + color: var(--text-muted); cursor: not-allowed; opacity: 0.7; } .bin-selector select:disabled { - background: #f1f5f9; - color: #94a3b8; + background-color: var(--bg-neutral); + color: var(--text-muted); cursor: not-allowed; opacity: 0.7; } @@ -461,177 +966,23 @@ -moz-appearance: textfield; } -.pdf-exporting .distribution-actions-bar { - display: none; -} - .pdf-export-overlay { position: absolute; inset: 6px; - background: rgba(255, 255, 255, 0.75); + background: rgba(255, 255, 255, 0.92); border-radius: 0px; display: flex; align-items: center; justify-content: center; z-index: 9; - backdrop-filter: blur(1px); + backdrop-filter: var(--glass-blur); } -#distribution-graph, -#parameters-listing, -#variables-top, +#histogram-graph, #dataset-selection { scroll-margin-top: 90px; } -/* Group summary for selected nodes with children */ -.group-summary { - display: flex; - flex-direction: column; - gap: 10px; - padding: 10px 6px 6px; - min-width: 0; -} - -.group-summary-header { - display: flex; - flex-direction: column; - gap: 4px; - font-size: 0.95rem; - color: #0f172a; - font-weight: 600; - min-width: 0; -} - -.group-summary-header p { - margin: 0; -} - -.group-summary-label { - text-transform: uppercase; - font-size: 0.72rem; - letter-spacing: 0.06em; - color: #64748b; - font-weight: 700; -} - -.group-summary-value { - font-weight: 700; - color: #0f172a; - display: inline-flex; - flex-wrap: wrap; - align-items: center; - gap: 4px; - min-width: 0; -} - -.group-summary-count-row { - display: flex; - flex-wrap: wrap; - align-items: center; - gap: 6px; - min-width: 0; -} - -.group-summary-count { - background: rgba(43, 51, 233, 0.12); - color: var(--primary-color); - padding: 2px 8px; - border-radius: 999px; - font-weight: 700; -} - -.group-summary-empty { - font-size: 0.85rem; - color: #64748b; - margin-top: 4px; -} - -.group-summary-path { - display: flex; - flex-wrap: wrap; - align-items: center; - gap: 4px; - min-width: 0; -} - -.group-summary-separator { - display: none; -} - -.group-summary-separator.is-hidden { - display: none; -} - -.group-summary-link { - background: none; - border: none; - padding: 0; - margin: 0; - color: var(--primary-color); - font-weight: 600; - cursor: pointer; - text-decoration: underline; -} - -.group-summary-path-link { - color: #0f172a; - text-decoration: none; - background: rgba(43, 51, 233, 0.06); - border: 1px solid rgba(43, 51, 233, 0.15); - border-radius: 999px; - padding: 4px 12px; - display: inline-flex; - align-items: center; - gap: 6px; - max-width: 100%; - white-space: normal; - word-break: break-word; - font-size: 0.82rem; - transition: all 0.2s ease; -} - -.group-summary-path-link:hover { - color: var(--primary-color); - background: rgba(43, 51, 233, 0.18); -} - -.group-summary-list-link { - text-decoration: none; -} - -.group-summary-list-link:hover { - text-decoration: underline; -} - -.group-summary-link:hover { - color: var(--primary-dark, #1b21a3); -} - -.group-summary-list { - list-style: none; - padding: 0; - margin: 0; - display: grid; - grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); - gap: 6px 12px; - color: #0f172a; - font-size: 0.92rem; -} - -.group-summary-list li { - background: rgba(43, 51, 233, 0.06); - border: 1px solid rgba(43, 51, 233, 0.12); - border-radius: 12px; - padding: 8px 12px; - transition: transform 0.2s ease, background 0.2s ease; -} - -.group-summary-list li:hover { - transform: translateY(-2px); - background: rgba(43, 51, 233, 0.1); -} - /* ---------- MESSAGES (no variable / error) ---------- */ .message-container { @@ -641,7 +992,7 @@ justify-content: center; gap: 8px; padding: 24px; - border-radius: 0px; + border-radius: 12px; text-align: center; font-size: 1rem; font-weight: 500; @@ -662,9 +1013,11 @@ border: 1px solid rgba(255, 0, 0, 0.2); } -.message-icon { - font-size: 36px; - opacity: 0.85; +.message-container.empty { + background: transparent; + border: none; + color: #64748b; + font-size: 0.88rem; } @keyframes fadeIn { @@ -679,71 +1032,29 @@ } } -/* Welcome Empty State */ -.empty-state-welcome { - padding: 40px 20px; +/* Clinical Empty State (no variable selected) */ +.empty-state-clinical { + padding: 32px 20px; background: transparent; border: none; max-width: 100%; + margin: 14px auto 6px; + gap: 6px; } -.empty-state-icon { - font-size: 48px; - margin-bottom: 20px; - background: linear-gradient(135deg, var(--primary-color), var(--primary-light, #7f9ce8)); - -webkit-background-clip: text; - background-clip: text; - -webkit-text-fill-color: transparent; - filter: drop-shadow(0 4px 12px rgba(43, 51, 233, 0.2)); -} - -.empty-state-welcome h3 { - font-size: 1.4rem; +.empty-state-clinical h3 { + font-size: 1.05rem; font-weight: 700; - color: #0f172a; - margin-bottom: 12px; + color: var(--text-main); + margin: 0; } -.empty-state-welcome p { +.empty-state-clinical p { color: #64748b; - line-height: 1.6; - max-width: 400px; - margin: 0 auto 30px; -} - -.empty-state-actions { - display: flex; - flex-direction: column; - gap: 12px; - width: 100%; - max-width: 320px; -} - -.hint-item { - display: flex; - align-items: center; - gap: 14px; - padding: 12px 16px; - background: rgba(255, 255, 255, 0.6); - border: 1px solid rgba(148, 163, 184, 0.2); - border-radius: 12px; font-size: 0.88rem; - color: #334155; - text-align: left; - transition: all 0.2s ease; -} - -.hint-item:hover { - background: #ffffff; - border-color: rgba(43, 51, 233, 0.3); - transform: translateX(4px); - box-shadow: 0 4px 12px rgba(15, 23, 42, 0.05); -} - -.hint-item i { - font-size: 1.1rem; - color: var(--primary-color); - width: 20px; + line-height: 1.55; + max-width: 280px; + margin: 0 auto; } /* ---------- RESPONSIVE ---------- */ @@ -753,22 +1064,22 @@ flex-wrap: wrap; } + .visualization-parameters-row { + grid-template-columns: 1fr; + min-height: 0; + } + .variables-main { grid-template-columns: minmax(0, 1fr); } .bubble-chart-section { - min-height: 280px; + min-height: 480px; + height: auto; } - } @media (max-width: 768px) { - .variables-panel-shell { - padding: 8px; - border-radius: 0px; - } - .selectors-row { gap: 10px; padding: 6px 0 8px; @@ -776,23 +1087,17 @@ .variables-main { gap: 12px; + padding: 16px 0 0; } .bubble-chart-section { min-height: 250px; } - - .bubble-actions { - position: static; - justify-content: flex-start; - margin-bottom: 6px; - } - .bubble-chart-section { padding-top: 12px; } - .distribution-graph { + .histogram-graph { min-height: 210px; } } diff --git a/src/app/pages/experiment-studio/variables-panel/variables-panel.component.html b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.html index be00c9822..270380657 100644 --- a/src/app/pages/experiment-studio/variables-panel/variables-panel.component.html +++ b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.html @@ -1,177 +1,341 @@ -<div id="variables-top"> +<div> <div class="selectors-row" id="dataset-selection"></div> <!-- Main area --> <div class="variables-main"> - <div class="visualization-parameters-row"> - <div class="bubble-column"> - <div class="visualization-header"> - <div class="visualization-header-left"> - <div class="bubble-selectors"> - <app-data-model-selector [crossSectionalModels]="crossSectionalModels" - [longitudinalModels]="longitudinalModels" [defaultModel]="selectedDataModel()" - (dataModelChange)="onSelectedDataModelChange($event)"> - </app-data-model-selector> + <div class="visualization-parameters-row" data-guide="variables-section"> + <div class="bubble-column" data-guide="variable-browser"> + <section class="bubble-chart-section" id="data-model-visualization" data-guide="variable-selection"> + <div class="visualization-toolbar"> + <div + class="visualization-toolbar-section visualization-toolbar-context" + #contextSection + data-guide="study-context"> + <button + type="button" + class="study-context-chip" + [attr.aria-expanded]="contextOpen()" + [attr.title]="contextChipTitle" + (click)="toggleContext()"> + <span class="study-context-value"> + {{ selectedDataModel()?.label ?? 'Select pathology' }} + </span> + @if (selectedDataModel()) { + <span class="study-context-sep" aria-hidden="true">·</span> + <span class="study-context-count" [title]="contextChipTitle"> + <i class="fas fa-hospital" aria-hidden="true"></i> + <span>{{ selectedDatasetCount() }} dataset{{ selectedDatasetCount() === 1 ? '' : 's' }}</span> + </span> + } + <i class="fas fa-chevron-down study-context-chevron" aria-hidden="true"></i> + </button> + @if (contextOpen()) { + <div class="study-context-panel" role="region" aria-label="Study context settings"> + <app-data-model-selector [crossSectionalModels]="crossSectionalModels" + [longitudinalModels]="longitudinalModels" [defaultModel]="selectedDataModel()" + (dataModelChange)="onSelectedDataModelChange($event)"> + </app-data-model-selector> - <app-dataset-selector [datasets]="availableDatasets"> - </app-dataset-selector> + <div class="study-context-divider" aria-hidden="true"></div> + + <app-dataset-selector + [datasets]="availableDatasets" + [selectedDatasetCodes]="experimentStudioService.selectedDatasets()"> + </app-dataset-selector> + </div> + } </div> - </div> - <div class="visualization-header-right"> + @if (d3Data) { - <app-search-bar [dataModelHierarchy]="d3Data" (searchResultSelected)="onSearchSelected($event)"> - </app-search-bar> + <div class="visualization-toolbar-modes"> + <div + class="metadata-browser-switcher" + role="group" + aria-label="Metadata browser view" + [style.--segment-active-index]="metadataBrowserModeIndex()"> + <span class="metadata-segment-track" aria-hidden="true"> + <span class="metadata-segment-indicator"></span> + </span> + @for (mode of metadataBrowserModes; track mode.value) { + <button + type="button" + class="metadata-mode-btn" + [class.active]="metadataBrowserMode() === mode.value" + [attr.aria-pressed]="metadataBrowserMode() === mode.value" + [attr.aria-label]="mode.label" + [title]="mode.title" + (click)="setMetadataBrowserMode(mode.value)" + > + <i [class]="mode.icon" aria-hidden="true"></i> + <span>{{ mode.label }}</span> + </button> + } + </div> + </div> + + <div + class="visualization-toolbar-section visualization-toolbar-search" + #searchSection + data-guide="search-bar" + [class.is-open]="searchExpanded()"> + <button + type="button" + class="search-toggle-btn" + [class.is-active]="searchExpanded()" + [attr.aria-expanded]="searchExpanded()" + [title]="searchExpanded() ? 'Close search' : 'Search variables or groups'" + aria-label="Search variables or groups" + (click)="toggleSearch()"> + <i class="fas fa-search" aria-hidden="true"></i> + <span>Search variables</span> + </button> + @if (searchExpanded()) { + <div class="search-popover"> + <app-search-bar [dataModelHierarchy]="d3Data" [unavailableVariables]="unavailableVariables()" (searchResultSelected)="onSearchSelected($event)"> + </app-search-bar> + </div> + } + </div> } </div> - </div> - <!-- Data model visualization --> - <section class="bubble-chart-section" id="data-model-visualization"> - @if (selectedDataModel()) { - <app-bubble-chart [d3Data]="d3Data" [highlightNode]="highlightNode" [selectedVariables]="selectedVariables" - [selectedCovariates]="selectedCovariates" [selectedFilters]="selectedFilters" - (selectedNodeChange)="onSelectedNodeChange($event)" (nodeDoubleClicked)="onNodeDoubleClicked($event)"> - </app-bubble-chart> + @if (selectedDataModel() && d3Data) { + <div + class="metadata-browser-shell" + [class.is-map]="metadataBrowserMode() === 'bubble'" + data-guide="variable-visualizations"> + <div class="metadata-browser-content"> + @switch (metadataBrowserMode()) { + @case ('ontology') { + <app-ontology-tree-browser + [data]="d3Data" + [highlightNode]="highlightNode" + [poolVariables]="selectedVariables" + [unavailableVariables]="unavailableVariables()" + (selectedNodeChange)="onMetadataBrowserNodeSelected($event)" + (nodeDoubleClicked)="onNodeDoubleClicked($event)" + (variablePoolToggle)="toggleSelectedNodeInPool()"> + </app-ontology-tree-browser> + } + @case ('collapsible') { + <app-collapsible-tree-browser + [data]="d3Data" + [highlightNode]="highlightNode" + [selectedVariables]="selectedVariables" + [unavailableVariables]="unavailableVariables()" + (selectedNodeChange)="onMetadataBrowserNodeSelected($event)" + (nodeDoubleClicked)="onNodeDoubleClicked($event)"> + </app-collapsible-tree-browser> + } + @default { + <app-bubble-chart + [d3Data]="d3Data" + [highlightNode]="highlightNode" + [selectedVariables]="selectedVariables" + [unavailableVariables]="unavailableVariables()" + (selectedNodeChange)="onBubbleNodeSelected($event)" + (nodeDoubleClicked)="onNodeDoubleClicked($event)"> + </app-bubble-chart> + } + } + </div> + </div> } </section> </div> <aside class="right-column"> - <!-- Parameters listing --> - <section class="panel-block variable-filter-container parameters-panel" id="parameters-listing"> - <app-variable-filter-selection #variableFilterSelection [selectedNode]="selectedNode" - (filtersChange)="onFilterChange($event)" - (variableClicked)="onVariableClicked($event)"></app-variable-filter-selection> - </section> - - <!-- Distribution graph --> - <section class="panel-block distribution-panel" id="distribution-graph"> - <div class="distribution-header"> - {{ groupHistogramData() ? 'Group Information' : 'Distribution graph' }} - </div> - <!-- Distribution Actions Bar --> - <div class="distribution-actions-bar"> - <div class="distribution-actions-group"> - @if (showBinSelector()) { - <div class="bin-selector"> - <label for="bins-input">Bins:</label> - <input id="bins-input" type="number" min="1" max="100" [class.disabled]="isLoadingHistogram()" - [disabled]="isLoadingHistogram()" [value]="histogramBins() || ''" (change)="onBinsChange($event)" - placeholder="Auto"> - </div> - } - @if (!groupHistogramData() && histogramVariants().length > 1) { - <div class="bin-selector"> - <label for="histogram-variant-select">View:</label> - <select id="histogram-variant-select" [value]="selectedHistogramVariantKey() || ''" - (change)="onHistogramVariantChange($event)"> - @for (variant of histogramVariants(); track variant.key) { - <option [value]="variant.key">{{ variant.label }}</option> - } - </select> + <!-- Node details: histogram + metadata --> + <section class="panel-block histogram-panel" id="histogram-graph" data-guide="variable-details"> + <div class="details-panel-header"> + <div class="details-panel-header-main"> + <div class="details-panel-title-block"> + @if (selectedNode) { + <div class="details-panel-identity" [class.is-group]="isSelectedNodeGroup()"> + <div class="details-panel-heading"> + <span class="details-panel-eyebrow">{{ isSelectedNodeGroup() ? 'Group' : 'Variable' }}</span> + <h2 class="details-panel-title" [title]="detailsPanelTitle()">{{ detailsPanelTitle() }}</h2> + @if (detailsPanelSubtitle()) { + <p class="details-panel-subtitle" [title]="detailsPanelSubtitle()">{{ detailsPanelSubtitle() }}</p> + } + </div> + </div> + } @else { + <h2 class="details-panel-title">Select a variable</h2> + <p class="details-panel-subtitle">Click a variable or group to see its chart and metadata.</p> + } </div> - } - <button class="distribution-action-btn csv-btn" [disabled]="isExportDisabled() || isExporting()" - (click)="exportDistributionCsv()" title="Export CSV"> - <i class="fas fa-file-csv"></i> + <button + type="button" + class="selection-toggle-btn" + data-guide="guide-add-variable" + [class.pulse-hint--variable]="!isSelectedNodeInPool() && !!selectedNode" + [class.is-remove]="isSelectedNodeInPool()" + [disabled]="selectionToggleDisabled()" + [title]="selectionToggleTitle()" + (click)="toggleSelectedNodeInPool()"> + <i class="fas" [class.fa-plus]="!isSelectedNodeInPool()" [class.fa-minus]="isSelectedNodeInPool()" aria-hidden="true"></i> + <span>{{ selectionToggleLabel() }}</span> + </button> + </div> + <div + class="details-segment" + role="tablist" + aria-label="Selected node views" + [style.--segment-active-index]="activeDetailsTab() === 'info' ? 1 : 0"> + <span class="details-segment-track" aria-hidden="true"> + <span class="details-segment-indicator"></span> + </span> + <button + type="button" + class="details-segment-btn" + role="tab" + id="details-tab-chart" + [class.active]="activeDetailsTab() === 'histogram'" + [attr.aria-selected]="activeDetailsTab() === 'histogram'" + aria-controls="histogram-tab-panel" + (click)="setActiveDetailsTab('histogram')"> + <i class="fas fa-chart-bar" aria-hidden="true"></i> + <span>Chart</span> </button> - <button class="distribution-action-btn pdf-btn" [disabled]="isExportDisabled() || isExporting()" - (click)="exportDistributionPdf()" title="Export PDF"> - <i class="fas fa-file-pdf"></i> + <button + type="button" + class="details-segment-btn" + role="tab" + id="details-tab-details" + [class.active]="activeDetailsTab() === 'info'" + [attr.aria-selected]="activeDetailsTab() === 'info'" + aria-controls="metadata-tab-panel" + (click)="setActiveDetailsTab('info')"> + <i class="fas fa-list-ul" aria-hidden="true"></i> + <span>Details</span> </button> </div> </div> - <div class="distribution-graph" #distributionExport> - @if (isLoadingHistogram()) { - <div class="distribution-loader"> - <app-spinner text="Loading Histogram" [overlay]="false" /> - </div> - } @else if (errorMessage()) { - <div class="message-container error"> - <p>{{ errorMessage() }}</p> - </div> - } @else if (!selectedNode) { - <div class="message-container empty-state-welcome"> - <div class="empty-state-icon"> - <i class="fas fa-magic"></i> + @if (activeDetailsTab() === 'histogram') { + <div class="histogram-tab-content details-tab-panel" id="histogram-tab-panel" role="tabpanel" aria-labelledby="details-tab-chart"> + @if (canSplitHistogram() || histogramData() || groupHistogramData()) { + <div class="histogram-toolbar"> + @if (!groupHistogramData() && canSplitHistogram()) { + <div class="bin-selector"> + <label for="histogram-split-select">Split by</label> + <select + id="histogram-split-select" + [value]="histogramSplit()" + (change)="onHistogramSplitChange($event)" + aria-label="Split histogram by category"> + <option value="">None</option> + @for (option of histogramSplitOptions(); track option.code) { + <option + [value]="option.code" + [disabled]="option.disabled" + [title]="option.title" + [class.is-unavailable]="!!option.unavailable">{{ option.label }}</option> + } + </select> </div> - <h3>Welcome to Experiment Studio</h3> - <p>Explore your data model by clicking the bubbles in the chart or use the search bar to find specific - variables.</p> - <div class="empty-state-actions"> - <div class="hint-item"> - <i class="fas fa-mouse-pointer"></i> - <span><b>Single-click</b> to view details & distribution</span> - </div> - <div class="hint-item"> - <i class="fas fa-bolt"></i> - <span><b>Double-click</b> to add to Variables list</span> + } + @if (histogramData() || groupHistogramData()) { + <div class="histogram-export-group" aria-label="Export histogram"> + <button type="button" class="studio-export-btn" + [disabled]="isExportDisabled() || isExporting()" + [attr.aria-expanded]="exportMenuOpen()" + aria-haspopup="menu" + (click)="toggleExportMenu()" title="Export histogram"> + <i class="fa fa-download" aria-hidden="true"></i> + <span>Export</span> + <i class="fa fa-chevron-down histogram-export-caret" [class.open]="exportMenuOpen()" + aria-hidden="true"></i> + </button> + @if (exportMenuOpen()) { + <div class="studio-export-menu" role="menu" aria-label="Export histogram"> + <button type="button" class="studio-export-menu-item" role="menuitem" + (click)="exportHistogramCsv()" title="Export CSV"> + <i class="fa fa-file-csv" aria-hidden="true"></i> + <span>CSV</span> + </button> + <button type="button" class="studio-export-menu-item" role="menuitem" + (click)="exportHistogramPdf()" title="Export PDF"> + <i class="fa fa-file-pdf" aria-hidden="true"></i> + <span>PDF</span> + </button> </div> + } </div> + } </div> - } @else if (groupHistogramData()) { - <div class="group-summary"> - <div class="group-summary-header"> - <p class="group-summary-path"> - <span class="group-summary-label">Path</span> - <span class="group-summary-value"> - @for (node of groupHistogramMeta()?.pathNodes; track node.code; let i = $index) { - <button type="button" class="group-summary-link group-summary-path-link" - (click)="onGroupSummaryClick(node)"> - @if (i === 0) { <i class="fas fa-home" style="font-size: 0.75rem; opacity: 0.7;"></i> } - {{ node.label }} - </button> - } - </span> - </p> - <p class="group-summary-count-row"> - <span class="group-summary-label"> - {{ groupHistogramMeta()?.hasGroups ? 'Number of groups in' : 'Number of variables in' }} - </span> - <span class="group-summary-value">{{ selectedNode?.label }}</span> - <span class="group-summary-count">{{ groupHistogramMeta()?.groupCount }}</span> - </p> - </div> - </div> - <app-distribution-graph [data]="groupHistogramData()"></app-distribution-graph> - } @else if (distributionData()) { - <div class="group-summary"> - <div class="group-summary-header"> - @if (selectedNode?.description) { - <div class="group-summary-path" style="margin-bottom: 8px;"> - <span class="group-summary-label">Description</span> - <span class="group-summary-value" style="font-weight: 400;"> - {{ selectedNode?.description }} - </span> + } + <div class="histogram-graph-area"> + <div class="histogram-graph" #histogramExport> + @if (isLoadingHistogram()) { + <div class="histogram-loader"> + <div class="studio-skeleton-card studio-skeleton-card--chart" role="status" aria-label="Loading histogram"> + <span class="studio-skeleton-line medium"></span> + <div class="studio-skeleton-chart" aria-hidden="true"> + <span class="studio-skeleton-bar" style="height: 42%"></span> + <span class="studio-skeleton-bar" style="height: 68%"></span> + <span class="studio-skeleton-bar" style="height: 56%"></span> + <span class="studio-skeleton-bar" style="height: 84%"></span> + <span class="studio-skeleton-bar" style="height: 48%"></span> + <span class="studio-skeleton-bar" style="height: 64%"></span> + </div> + </div> + </div> + } @else if (errorMessage()) { + <div class="message-container error"> + <p>{{ errorMessage() }}</p> + </div> + } @else if (emptyChartMessage()) { + <div class="message-container empty"> + <p>{{ emptyChartMessage() }}</p> </div> + } @else if (!selectedNode) { + <div class="message-container empty-state-clinical"> + <h3>No variable selected</h3> + <p>Click a variable in the browser to see its chart and details.</p> + </div> + } @else if (groupHistogramData()) { + <app-histogram-graph [data]="groupHistogramData()"></app-histogram-graph> + } @else if (histogramData()) { + <app-histogram-graph [data]="histogramData()"></app-histogram-graph> } - <p class="group-summary-path"> - <span class="group-summary-label">Path</span> - <span class="group-summary-value"> - @for (node of getPathNodes(selectedNode); track node.code; let i = $index) { - <button type="button" class="group-summary-link group-summary-path-link" - (click)="onGroupSummaryClick(node)"> - @if (i === 0) { <i class="fas fa-home" style="font-size: 0.75rem; opacity: 0.7;"></i> } - {{ node.label }} - </button> - } - </span> - </p> </div> + @if (histogramPrivacyTrimNote()) { + <div class="histogram-privacy-note" role="note"> + <i class="fas fa-info-circle" aria-hidden="true"></i> + <span>{{ histogramPrivacyTrimNote() }}</span> + </div> + } </div> - <app-distribution-graph [data]="distributionData()"></app-distribution-graph> - } </div> - - + } @else { + <div class="metadata-tab-content details-tab-panel" id="metadata-tab-panel" role="tabpanel" aria-labelledby="details-tab-details"> + <app-metadata-info-panel + [selectedNode]="selectedNode" + [pathNodes]="getPathNodes(selectedNode)" + [groupInfo]="groupHistogramMeta()" + [hideHeader]="true"> + </app-metadata-info-panel> + </div> + } @if (isExporting()) { <div class="pdf-export-overlay"> - <app-spinner text="Generating PDF..." [overlay]="false" /> + <div class="studio-skeleton-card" role="status" aria-label="Generating PDF"> + <span class="studio-skeleton-line medium"></span> + <span class="studio-skeleton-line short"></span> + </div> </div> } </section> + + <app-selected-variables + mode="inline" + data-guide="variable-containers" + [selectedNode]="selectedNode" + (variableClicked)="onVariableClicked($event)"> + </app-selected-variables> </aside> </div> diff --git a/src/app/pages/experiment-studio/variables-panel/variables-panel.component.spec.ts b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.spec.ts new file mode 100644 index 000000000..ef83f6658 --- /dev/null +++ b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.spec.ts @@ -0,0 +1,772 @@ +import type { Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal, ElementRef } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { of, throwError } from 'rxjs'; +import { ErrorService } from '../../../services/error.service'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { CsvExportService } from '../../../services/csv-export.service'; +import { PdfExportService } from '../../../services/pdf-export.service'; +import { VariablesPanelComponent } from './variables-panel.component'; +import { ExperimentStudioGuideStateService } from '../guide/experiment-studio-guide-state.service'; +describe('VariablesPanelComponent bubble selection', () => { + let experimentStudioService: { + selectedDataModel: ReturnType<typeof signal<any>>; + selectedVariables: ReturnType<typeof signal<any[]>>; + selectedDatasets: ReturnType<typeof signal<string[]>>; + availableGroupedAlgorithms: ReturnType<typeof signal<any[]>>; + getAllDataModels: Mock; + categorizeDataModels: Mock; + getAlgorithmResults: Mock; + getDatasetLabelMap: Mock; + setVariables: Mock; + setSelectedDatasets: Mock; + addVariableAndEnrich: Mock; + clearSelectionsForDataModelChange: Mock; + editingExistingExperiment: () => boolean; + unavailableVariables: ReturnType<typeof signal<ReadonlyMap<string, readonly string[]>>>; + }; + + beforeEach(() => { + experimentStudioService = { + selectedDataModel: signal<any>(null), + selectedVariables: signal<any[]>([]), + selectedDatasets: signal<string[]>(['dataset-a']), + availableGroupedAlgorithms: signal<any[]>([]), + getAllDataModels: vi.fn().mockReturnValue(of([])), + categorizeDataModels: vi.fn().mockReturnValue({ + crossSectional: [], + longitudinal: [], + }), + getAlgorithmResults: vi.fn().mockReturnValue(of({ result: { histogram: [] } })), + getDatasetLabelMap: vi.fn().mockReturnValue({ 'dataset-a': 'Dataset A' }), + setVariables: vi.fn(), + setSelectedDatasets: vi.fn(), + addVariableAndEnrich: vi.fn(), + clearSelectionsForDataModelChange: vi.fn(), + editingExistingExperiment: () => false, + unavailableVariables: signal<ReadonlyMap<string, readonly string[]>>(new Map()), + }; + + TestBed.configureTestingModule({ + imports: [VariablesPanelComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioService, useValue: experimentStudioService }, + { + provide: ExperimentStudioGuideStateService, + useValue: { + activeStepId: signal<string | null>(null), + setSelectedHierarchyNode: vi.fn(), + }, + }, + { provide: ErrorService, useValue: {} }, + { provide: PdfExportService, useValue: { exportHistogramPdf: vi.fn() } }, + { provide: CsvExportService, useValue: { exportHistogramCsv: vi.fn() } }, + ], + }); + + TestBed.overrideComponent(VariablesPanelComponent, { + set: { template: '' }, + }); + localStorage.removeItem('metadata_browser_mode'); + }); + + it('does not auto-add a variable on single bubble click', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const node = { code: 'age_value', label: 'Age', type: 'continuous' }; + + const selectSpy = vi.spyOn(component, 'onSelectedNodeChange').mockReturnValue(undefined); + const addSpy = vi.spyOn(component, 'addVariableFromBubble').mockReturnValue(undefined); + + component.onBubbleNodeSelected(node); + + expect(selectSpy).toHaveBeenCalledWith(node); + expect(addSpy).not.toHaveBeenCalled(); + expect(experimentStudioService.addVariableAndEnrich).not.toHaveBeenCalled(); + }); + + it('keeps double click as the explicit add action', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const node = { code: 'age_value', label: 'Age', type: 'continuous' }; + + const selectSpy = vi.spyOn(component, 'onSelectedNodeChange').mockReturnValue(undefined); + const addSpy = vi.spyOn(component, 'addVariableFromBubble').mockReturnValue(undefined); + + component.onNodeDoubleClicked(node); + + expect(selectSpy).toHaveBeenCalledWith(node); + expect(addSpy).toHaveBeenCalled(); + }); + + it('adds the selected node when it is not in the pool', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.selectedNode = { code: 'age_value', label: 'Age', type: 'continuous' }; + + component.toggleSelectedNodeInPool(); + + expect(experimentStudioService.addVariableAndEnrich).toHaveBeenCalledWith({ + code: 'age_value', + label: 'Age', + type: 'continuous', + }); + expect(experimentStudioService.setVariables).not.toHaveBeenCalled(); + }); + + it('removes the selected node when it is already in the pool', () => { + experimentStudioService.selectedVariables.set([ + { code: 'age_value', label: 'Age', type: 'continuous' }, + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.selectedNode = { code: 'age_value', label: 'Age', type: 'continuous' }; + + component.toggleSelectedNodeInPool(); + + expect(experimentStudioService.setVariables).toHaveBeenCalledWith([ + { code: 'sex_value', label: 'Sex', type: 'string' }, + ]); + expect(experimentStudioService.addVariableAndEnrich).not.toHaveBeenCalled(); + }); + + it('ignores clicks, double clicks and search picks on an unavailable variable', () => { + experimentStudioService.unavailableVariables.set(new Map([['age_value', ['Dataset A']]])); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const node = { code: 'age_value', label: 'Age', type: 'continuous' }; + const selectSpy = vi.spyOn(component, 'onSelectedNodeChange').mockReturnValue(undefined); + + component.onBubbleNodeSelected(node); + component.onMetadataBrowserNodeSelected(node); + component.onNodeDoubleClicked(node); + + expect(selectSpy).not.toHaveBeenCalled(); + expect(component.highlightNode).toBeNull(); + expect(experimentStudioService.addVariableAndEnrich).not.toHaveBeenCalled(); + }); + + it('adds only the available variables of a group', () => { + experimentStudioService.unavailableVariables.set(new Map([['sex_value', ['Dataset A']]])); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const age = { code: 'age_value', label: 'Age', type: 'continuous' }; + const sex = { code: 'sex_value', label: 'Sex', type: 'string' }; + component.selectedNode = { code: 'demo', label: 'Demographics', children: [age, sex] }; + + component.toggleSelectedNodeInPool(); + + expect(experimentStudioService.addVariableAndEnrich).toHaveBeenCalledTimes(1); + expect(experimentStudioService.addVariableAndEnrich).toHaveBeenCalledWith(age); + }); + + it('disables Add when every variable of the selection is unavailable', () => { + experimentStudioService.unavailableVariables.set(new Map([['age_value', ['Dataset A']]])); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.selectedNode = { code: 'age_value', label: 'Age', type: 'continuous' }; + + expect(component.selectionToggleDisabled()).toBe(true); + expect(component.selectionToggleTitle()).toBe('Not available in the selected datasets'); + component.toggleSelectedNodeInPool(); + expect(experimentStudioService.addVariableAndEnrich).not.toHaveBeenCalled(); + }); + + it('still removes a pooled variable that became unavailable', () => { + experimentStudioService.selectedVariables.set([{ code: 'age_value', label: 'Age', type: 'continuous' }]); + experimentStudioService.unavailableVariables.set(new Map([['age_value', ['Dataset A']]])); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.selectedNode = { code: 'age_value', label: 'Age', type: 'continuous' }; + + expect(component.selectionToggleDisabled()).toBe(false); + component.toggleSelectedNodeInPool(); + expect(experimentStudioService.setVariables).toHaveBeenCalledWith([]); + }); + + it('ignores the pool toggle when no datasets are selected', () => { + experimentStudioService.selectedDatasets.set([]); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.selectedNode = { code: 'age_value', label: 'Age', type: 'continuous' }; + + component.toggleSelectedNodeInPool(); + + expect(experimentStudioService.addVariableAndEnrich).not.toHaveBeenCalled(); + expect(experimentStudioService.setVariables).not.toHaveBeenCalled(); + }); + + it('auto-selects all available datasets on a fresh session with no valid selection', () => { + experimentStudioService.selectedDatasets.set([]); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.availableDatasets = [ + { code: 'ds-a', label: 'Dataset A' }, + { code: 'ds-b', label: 'Dataset B' }, + ]; + + component.ensureDatasetsSelected(); + + expect(experimentStudioService.setSelectedDatasets).toHaveBeenCalledWith(['ds-a', 'ds-b']); + }); + + it('keeps a valid existing selection instead of re-selecting all', () => { + experimentStudioService.selectedDatasets.set(['ds-b']); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.availableDatasets = [ + { code: 'ds-a', label: 'Dataset A' }, + { code: 'ds-b', label: 'Dataset B' }, + ]; + + component.ensureDatasetsSelected(); + + expect(experimentStudioService.setSelectedDatasets).not.toHaveBeenCalled(); + }); + + it('does not auto-select datasets when editing an existing experiment', () => { + experimentStudioService.selectedDatasets.set([]); + experimentStudioService.editingExistingExperiment = () => true; + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.availableDatasets = [{ code: 'ds-a', label: 'Dataset A' }]; + + component.ensureDatasetsSelected(); + + expect(experimentStudioService.setSelectedDatasets).not.toHaveBeenCalled(); + }); + + it('reports a dataset count in the context chip title', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.selectedDataModel.set({ code: 'stroke', label: 'Stroke 3.7' } as any); + experimentStudioService.selectedDatasets.set(['ds-a', 'ds-b']); + + expect(component.contextChipTitle).toBe('Stroke 3.7 · 2 datasets'); + }); + + it('points to the dataset gate in the context chip title when none are selected', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.selectedDataModel.set({ code: 'stroke', label: 'Stroke 3.7' } as any); + experimentStudioService.selectedDatasets.set([]); + + expect(component.contextChipTitle).toBe('Stroke 3.7 · Select datasets'); + }); + + it('toggles the context popover open and closed', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + expect(component.contextOpen()).toBe(false); + + component.toggleContext(); + expect(component.contextOpen()).toBe(true); + + component.toggleContext(); + expect(component.contextOpen()).toBe(false); + }); + + it('reports the selected dataset count from the studio service', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + expect(component.selectedDatasetCount()).toBe(1); + + experimentStudioService.selectedDatasets.set(['dataset-a', 'dataset-b']); + expect(component.selectedDatasetCount()).toBe(2); + }); + + it('closes the context sheet on a click inside the host but outside the context section', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleContext(); + expect(component.contextOpen()).toBe(true); + + const contextSection = document.createElement('div'); + const sibling = document.createElement('div'); + document.body.appendChild(contextSection); + document.body.appendChild(sibling); + try { + component.contextSection = { nativeElement: contextSection } as unknown as ElementRef; + component.onDocumentClickForContext({ target: sibling } as unknown as MouseEvent); + expect(component.contextOpen()).toBe(false); + } finally { + contextSection.remove(); + sibling.remove(); + } + }); + + it('keeps the context sheet open on a click inside the context section', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleContext(); + + const contextSection = document.createElement('div'); + const inner = document.createElement('span'); + contextSection.appendChild(inner); + document.body.appendChild(contextSection); + try { + component.contextSection = { nativeElement: contextSection } as unknown as ElementRef; + component.onDocumentClickForContext({ target: inner } as unknown as MouseEvent); + expect(component.contextOpen()).toBe(true); + } finally { + contextSection.remove(); + } + }); + + it('closes the context popover on Escape', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleContext(); + + component.onDocumentKeydownForContext(new KeyboardEvent('keydown', { key: 'Escape' })); + expect(component.contextOpen()).toBe(false); + }); + + it('toggles the search popover', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + expect(component.searchExpanded()).toBe(false); + + component.toggleSearch(); + expect(component.searchExpanded()).toBe(true); + + component.toggleSearch(); + expect(component.searchExpanded()).toBe(false); + }); + + it('collapses the search bar on an outside document click', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleSearch(); + expect(component.searchExpanded()).toBe(true); + + const searchEl = document.createElement('div'); + document.body.appendChild(searchEl); + const outside = document.createElement('span'); + document.body.appendChild(outside); + try { + component.searchSection = { nativeElement: searchEl } as unknown as ElementRef; + component.onDocumentClickForContext({ target: outside } as unknown as MouseEvent); + expect(component.searchExpanded()).toBe(false); + } finally { + searchEl.remove(); + outside.remove(); + } + }); + + it('keeps the search bar open on an inside click', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleSearch(); + + const searchEl = document.createElement('div'); + const inner = document.createElement('input'); + searchEl.appendChild(inner); + document.body.appendChild(searchEl); + try { + component.searchSection = { nativeElement: searchEl } as unknown as ElementRef; + component.onDocumentClickForContext({ target: inner } as unknown as MouseEvent); + expect(component.searchExpanded()).toBe(true); + } finally { + searchEl.remove(); + } + }); + + it('collapses the search bar on Escape', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleSearch(); + + component.onDocumentKeydownForContext(new KeyboardEvent('keydown', { key: 'Escape' })); + expect(component.searchExpanded()).toBe(false); + }); + + it('toggles the histogram export menu open and closed', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + expect(component.exportMenuOpen()).toBe(false); + + component.toggleExportMenu(); + expect(component.exportMenuOpen()).toBe(true); + + component.toggleExportMenu(); + expect(component.exportMenuOpen()).toBe(false); + }); + + it('closes the export menu when a format is chosen', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleExportMenu(); + + component.exportHistogramCsv(); + expect(component.exportMenuOpen()).toBe(false); + + component.toggleExportMenu(); + component.exportHistogramPdf(); + expect(component.exportMenuOpen()).toBe(false); + }); + + it('closes the export menu on an outside document click', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleExportMenu(); + + const group = document.createElement('div'); + group.className = 'histogram-export-group'; + document.body.appendChild(group); + const outside = document.createElement('span'); + document.body.appendChild(outside); + try { + component.onDocumentClickForContext({ target: outside } as unknown as MouseEvent); + expect(component.exportMenuOpen()).toBe(false); + } finally { + group.remove(); + outside.remove(); + } + }); + + it('keeps the export menu open on a click inside the export group', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleExportMenu(); + + const group = document.createElement('div'); + group.className = 'histogram-export-group'; + const item = document.createElement('button'); + group.appendChild(item); + document.body.appendChild(group); + try { + component.onDocumentClickForContext({ target: item } as unknown as MouseEvent); + expect(component.exportMenuOpen()).toBe(true); + } finally { + group.remove(); + } + }); + + it('closes the export menu on Escape', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.toggleExportMenu(); + + component.onDocumentKeydownForContext(new KeyboardEvent('keydown', { key: 'Escape' })); + expect(component.exportMenuOpen()).toBe(false); + }); + + it('defaults the metadata browser mode to bubble map', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + expect(component.metadataBrowserMode()).toBe('bubble'); + }); + + it('persists metadata browser mode changes', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + component.setMetadataBrowserMode('collapsible'); + + expect(component.metadataBrowserMode()).toBe('collapsible'); + expect(component.metadataBrowserModeIndex()).toBe(2); + expect(localStorage.getItem('metadata_browser_mode')).toBe('collapsible'); + }); + + it('falls back to the bubble map for an invalid saved metadata browser mode', () => { + localStorage.setItem('metadata_browser_mode', 'columns'); + + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + expect(component.metadataBrowserMode()).toBe('bubble'); + }); + + it('includes collapsible tree as an alternative metadata browser mode', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + expect(component.metadataBrowserModes.map((mode) => mode.value)).toEqual(['bubble', 'ontology', 'collapsible']); + }); + + it('resets selected-node details to the histogram tab on variable selection', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const node = { code: 'age_value', label: 'Age', type: 'continuous' }; + + component.setActiveDetailsTab('info'); + component.onSelectedNodeChange(node); + + expect(component.activeDetailsTab()).toBe('histogram'); + }); + + it('defaults a group selection to the Chart tab', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const group = { + code: 'acute_treatment', + label: 'Acute treatment', + children: [{ code: 'age_value', label: 'Age', type: 'continuous' }], + }; + + component.setActiveDetailsTab('info'); + component.onSelectedNodeChange(group); + + expect(component.activeDetailsTab()).toBe('histogram'); + }); + + it('offers Dataset plus selected nominal variables as splits and hides them for nominal plots', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + experimentStudioService.selectedVariables.set([ + { code: 'age', label: 'Age', type: 'continuous' }, + { code: 'sex', label: 'Sex', type: 'nominal', enumerations: [{ code: 'F' }, { code: 'M' }] }, + ]); + + component.onSelectedNodeChange({ code: 'age', label: 'Age', type: 'continuous' }); + expect(component.canSplitHistogram()).toBe(true); + expect(component.histogramSplitOptions().map((option) => option.code)).toEqual(['dataset', 'sex']); + + component.onSelectedNodeChange({ code: 'sex', label: 'Sex', type: 'nominal', enumerations: [{ code: 'F' }, { code: 'M' }] }); + expect(component.canSplitHistogram()).toBe(false); + }); + + it('offers an unavailable split grayed out and disabled, and keeps an active one', () => { + const sex = { code: 'sex', label: 'Sex', type: 'nominal', enumerations: [{ code: 'F' }, { code: 'M' }] }; + experimentStudioService.selectedVariables.set([{ code: 'age', label: 'Age', type: 'continuous' }, sex]); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.onSelectedNodeChange({ code: 'age', label: 'Age', type: 'continuous' }); + component.histogramSplit.set('sex'); + TestBed.tick(); + + experimentStudioService.unavailableVariables.set(new Map([['sex', ['Dataset B']]])); + TestBed.tick(); + + const option = component.histogramSplitOptions().find((item) => item.code === 'sex'); + expect(option).toEqual(expect.objectContaining({ disabled: true, unavailable: true, title: 'Not available in: Dataset B' })); + expect(component.histogramSplit()).toBe('sex'); + + const requests = experimentStudioService.getAlgorithmResults.mock.calls.length; + const select = document.createElement('select'); + select.innerHTML = '<option value="sex">Sex</option>'; + select.value = 'sex'; + component.histogramSplit.set(''); + component.onHistogramSplitChange({ target: select } as unknown as Event); + expect(component.histogramSplit()).toBe(''); + expect(experimentStudioService.getAlgorithmResults.mock.calls.length).toBe(requests + 1); + expect(experimentStudioService.getAlgorithmResults.mock.calls.at(-1)?.[5]).toBeUndefined(); + }); + + it('caps Dataset split by the selected dataset count, not the enumeration map', () => { + experimentStudioService.getDatasetLabelMap.mockReturnValue( + Object.fromEntries(Array.from({ length: 12 }, (_, i) => [`ds${i}`, `D${i}`])) + ); + experimentStudioService.selectedDatasets.set(['ds0', 'ds1']); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.onSelectedNodeChange({ code: 'age', label: 'Age', type: 'continuous' }); + expect(component.histogramSplitOptions().find((option) => option.code === 'dataset')?.disabled).toBe(false); + + experimentStudioService.selectedDatasets.set(Array.from({ length: 9 }, (_, i) => `ds${i}`)); + expect(component.histogramSplitOptions().find((option) => option.code === 'dataset')?.disabled).toBe(true); + }); + + it('re-requests the histogram with grouping when split by dataset', async () => { + experimentStudioService.getAlgorithmResults.mockImplementation(() => of({ + result: { + histogram: [ + { var: 'age_value', grouping_var: null, grouping_enum: null, bins: ['0', '10'], counts: [12, 8] }, + { var: 'age_value', grouping_var: 'dataset', grouping_enum: 'dataset-a', bins: ['0', '10'], counts: [7, 5] }, + ], + }, + })); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.d3Data = { code: 'stroke', label: 'Stroke 3.7' }; + component.onSelectedNodeChange({ code: 'age_value', label: 'Age', type: 'continuous' }); + await new Promise((resolve) => setTimeout(resolve, 0)); + component.onHistogramSplitChange({ target: { value: 'dataset' } } as unknown as Event); + await new Promise((resolve) => setTimeout(resolve, 0)); + + expect(experimentStudioService.getAlgorithmResults).toHaveBeenLastCalledWith( + 'histogram', + ['age_value'], + null, + undefined, + undefined, + ['dataset'] + ); + expect( + component.histogramData(), + `data=${JSON.stringify(component.histogramData())} split=${component.histogramSplit()} empty=${component.emptyChartMessage()} err=${component.errorMessage()}` + ).toEqual(expect.objectContaining({ + counts: [12, 8], + splitLabel: 'Dataset', + groups: [expect.objectContaining({ key: 'dataset-a', label: 'Dataset A', counts: [7, 5] })], + })); + }); + + it('drops a split whose variable leaves the pool and re-requests the unsplit histogram', async () => { + experimentStudioService.getAlgorithmResults.mockImplementation(() => of({ result: { histogram: [] } })); + experimentStudioService.selectedVariables.set([ + { code: 'sex', label: 'Sex', type: 'nominal', enumerations: [{ code: 'F' }, { code: 'M' }] }, + ]); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.onSelectedNodeChange({ code: 'age_value', label: 'Age', type: 'continuous' }); + component.onHistogramSplitChange({ target: { value: 'sex' } } as unknown as Event); + expect(component.histogramSplit()).toBe('sex'); + + experimentStudioService.selectedVariables.set([]); + TestBed.tick(); + + expect(component.histogramSplit()).toBe(''); + expect(experimentStudioService.getAlgorithmResults).toHaveBeenLastCalledWith( + 'histogram', + ['age_value'], + null, + undefined, + undefined, + undefined + ); + }); + + it('warns when privacy-null edge bins were trimmed on a numeric histogram', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.plottedVariable.set({ code: 'age_value', label: 'Age', type: 'continuous' }); + component.histogramData.set({ + bins: ['0', '10', '20', '30'], + counts: [null, 8, 12, null], + variableName: 'Age', + }); + + expect(component.histogramPrivacyTrimNote()).toContain('Values below 10 / above 30 hidden'); + }); + + it('does not warn about trimmed edges on a nominal histogram', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.plottedVariable.set({ + code: 'sex', + label: 'Sex', + type: 'nominal', + enumerations: [{ code: 'F' }, { code: 'M' }], + }); + component.histogramData.set({ + bins: ['F', 'M'], + counts: [null, 8], + variableName: 'Sex', + }); + + expect(component.histogramPrivacyTrimNote()).toBeNull(); + }); + + it('collapses consecutive duplicate breadcrumb labels', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + component.d3Data = { + code: 'stroke', + label: 'Stroke 3.7', + children: [ + { code: 'inr', label: 'INR', type: 'continuous' }, + ], + }; + + const path = component.getPathNodes({ code: 'stroke', label: 'Stroke 3.7' }); + expect(path.map((node) => node.label)).toEqual(['Stroke 3.7']); + }); + + it('builds a group census chart for the pathology root', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const root = { + code: 'stroke', + label: 'Stroke 3.7', + children: [ + { code: 'g1', label: 'Acute treatment', children: [{ code: 'a', label: 'A', value: 1 }] }, + { code: 'g2', label: 'Hospitalization', children: [{ code: 'b', label: 'B', value: 1 }, { code: 'c', label: 'C', value: 1 }] }, + ], + }; + component.d3Data = root; + component.onSelectedNodeChange(root); + + expect(component.groupHistogramData()).toEqual({ + bins: ['Hospitalization', 'Acute treatment'], + counts: [2, 1], + variableName: 'Stroke 3.7', + }); + expect(component.groupHistogramMeta()?.groupCount).toBe(2); + expect(component.detailsPanelSubtitle()).toBe('2 groups in this view'); + }); + + it('prompts to pick a variable when a group holds only variables', () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + const group = { + code: 'g1', + label: 'Acute treatment', + children: [ + { code: 'a', label: 'A', type: 'continuous' }, + { code: 'b', label: 'B', type: 'continuous' }, + ], + }; + component.d3Data = { code: 'stroke', label: 'Stroke 3.7', children: [group] }; + + component.onSelectedNodeChange(group); + + expect(component.groupHistogramData()).toBeNull(); + expect(component.emptyChartMessage()).toBe( + 'Please select one of the variables in the representation on the left to see its histogram in the selected centers.', + ); + expect(component.errorMessage()).toBeNull(); + expect(component.isLoadingHistogram()).toBe(false); + expect(component.detailsPanelSubtitle()).toBe('2 variables in this group'); + expect(component.isExportDisabled()).toBe(true); + }); + + function flushLeafHistogram(component: VariablesPanelComponent): Promise<void> { + component.d3Data = { code: 'stroke', label: 'Stroke 3.7' }; + component.onSelectedNodeChange({ code: 'age_value', label: 'Age', type: 'continuous' }); + return new Promise((resolve) => setTimeout(resolve, 0)); + } + + it('shows a neutral empty message when a leaf has no distribution data', async () => { + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + await flushLeafHistogram(component); + + expect(component.emptyChartMessage()).toBe('No distribution for this selection.'); + expect(component.errorMessage()).toBeNull(); + expect(component.isLoadingHistogram()).toBe(false); + }); + + it('shows the backend insufficient-data message neutrally instead of as an error', async () => { + experimentStudioService.getAlgorithmResults + .mockReturnValue(of({ result: { data: 'insufficient data' } })); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + await flushLeafHistogram(component); + + expect(component.emptyChartMessage()).toBe('This variable does not have sufficient data.'); + expect(component.errorMessage()).toBeNull(); + }); + + it('keeps a red error message when the histogram request fails', async () => { + experimentStudioService.getAlgorithmResults + .mockReturnValue(throwError(() => new Error('network'))); + const fixture = TestBed.createComponent(VariablesPanelComponent); + const component = fixture.componentInstance; + + await flushLeafHistogram(component); + + expect(component.errorMessage()).toBe('Error loading histogram. Please try again.'); + expect(component.emptyChartMessage()).toBeNull(); + }); +}); diff --git a/src/app/pages/experiment-studio/variables-panel/variables-panel.component.ts b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.ts index 1a83a9216..7628d97bf 100644 --- a/src/app/pages/experiment-studio/variables-panel/variables-panel.component.ts +++ b/src/app/pages/experiment-studio/variables-panel/variables-panel.component.ts @@ -1,58 +1,133 @@ import { BubbleChartComponent } from './../visualisations/bubble-chart/bubble-chart.component'; import { ErrorService } from '../../../services/error.service'; import { ExperimentStudioService } from '../../../services/experiment-studio.service'; -import { Component, signal, inject, Input, WritableSignal, OnDestroy, ElementRef, ViewChild, effect, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core'; -import { CommonModule } from '@angular/common'; -import { MatIconModule } from '@angular/material/icon'; -import { MatChipsModule } from '@angular/material/chips'; +import { Component, HostListener, signal, inject, WritableSignal, OnDestroy, ElementRef, ViewChild, effect, computed, ChangeDetectionStrategy, ChangeDetectorRef, untracked } from '@angular/core'; import { DataModel } from '../../../models/data-model.interface'; import { DataModelSelectorComponent } from './data-model-selector/data-model-selector.component'; import { DatasetSelectorComponent } from './dataset-selector/dataset-selector.component'; import { SearchBarComponent } from './search-bar/search-bar.component'; -import { VariableFilterSelectionComponent } from './variable-filter-selection/variable-filter-selection.component'; -import { DistributionGraphComponent } from './distribution-graph/distribution-graph.component'; -import { SpinnerComponent } from '../../shared/spinner/spinner.component'; +import { SelectedVariablesComponent } from './selected-variables/selected-variables.component'; +import { HistogramGraphComponent } from './histogram-graph/histogram-graph.component'; +import { MetadataInfoPanelComponent } from './metadata-info-panel/metadata-info-panel.component'; import { catchError, map, of, Subject, switchMap, takeUntil } from 'rxjs'; import { PdfExportService } from '../../../services/pdf-export.service'; import { CsvExportService } from '../../../services/csv-export.service'; +import { ExperimentStudioGuideStateService } from '../guide/experiment-studio-guide-state.service'; +import { AlgorithmNames } from '../../../core/constants/algorithm.constants'; +import { OntologyTreeBrowserComponent } from '../visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component'; +import { CollapsibleTreeBrowserComponent } from '../visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component'; +import { MetadataBrowserMode, MetadataSearchResult } from '../visualisations/metadata-browser/metadata-browser.model'; +import { + normalizeMetadataTree, + selectionFromSearchResult, +} from '../visualisations/metadata-browser/metadata-browser-normalizer'; +import { countLeafNodes, isFreeTextCde } from '../../../core/data-model.utils'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { isVariableUnavailable } from '../../../core/variable-availability.utils'; +import { + describeClippedNullEdges, + formatClippedNullEdgesNote, +} from '../visualisations/histogram/histogram-chart'; +import { + DATASET_VARIABLE_CODE, + HISTOGRAM_SPLIT_NONE, + HistogramChartData, + HistogramSplitOption, + buildHistogramSplitOptions, + buildStackedHistogramSeries, + extractHistogramItems, + isNominalHistogramNode, +} from '../visualisations/histogram/histogram-data'; + +type DetailsPanelTab = 'histogram' | 'info'; + +/** Shown when the selected node groups variables only, so there is nothing to census. */ +const GROUP_OF_VARIABLES_ONLY_MESSAGE = + 'Please select one of the variables in the representation on the left to see its histogram in the selected centers.'; @Component({ selector: 'app-variables-panel', templateUrl: './variables-panel.component.html', - styleUrls: ['./variables-panel.component.css'], + styleUrl: './variables-panel.component.css', changeDetection: ChangeDetectionStrategy.OnPush, imports: [ - CommonModule, - MatChipsModule, - MatIconModule, BubbleChartComponent, - DistributionGraphComponent, + OntologyTreeBrowserComponent, + CollapsibleTreeBrowserComponent, + HistogramGraphComponent, + MetadataInfoPanelComponent, DataModelSelectorComponent, DatasetSelectorComponent, SearchBarComponent, - VariableFilterSelectionComponent, - SpinnerComponent, + SelectedVariablesComponent, ] }) export class VariablesPanelComponent implements OnDestroy { - @Input() defaultModel: DataModel | null = null; - @Input() dataModelHierarchy: any; - @ViewChild('distributionExport') distributionExport?: ElementRef<HTMLElement>; - @ViewChild(VariableFilterSelectionComponent) variableFilterSelection?: VariableFilterSelectionComponent; + private readonly metadataBrowserModeStorageKey = 'metadata_browser_mode'; + readonly metadataBrowserModes: Array<{ + value: MetadataBrowserMode; + label: string; + icon: string; + title: string; + }> = [ + { + value: 'bubble', + label: 'Map', + icon: 'fas fa-circle-nodes', + title: 'Bubble overview of the metadata structure', + }, + { + value: 'ontology', + label: 'List', + icon: 'fas fa-sitemap', + title: 'Expandable hierarchy list of groups and variables', + }, + { + value: 'collapsible', + label: 'Graph', + icon: 'fas fa-project-diagram', + title: 'Interactive collapsible hierarchy diagram', + }, + ]; + + @ViewChild('histogramExport') histogramExport?: ElementRef<HTMLElement>; + @ViewChild('searchSection') searchSection?: ElementRef<HTMLElement>; + @ViewChild('contextSection') contextSection?: ElementRef<HTMLElement>; highlightNode: any = null; experimentStudioService = inject(ExperimentStudioService); pdfExportService = inject(PdfExportService); csvExportService = inject(CsvExportService); + private readonly mipVersion = inject(RuntimeEnvService).mipVersion; + guideState = inject(ExperimentStudioGuideStateService); private cdr = inject(ChangeDetectorRef); errorService = inject(ErrorService); filteredVariables: WritableSignal<any[]> = signal([]); filteredGroups: WritableSignal<any[]> = signal([]); - distributionData = signal<any | null>(null); + histogramData = signal<HistogramChartData | null>(null); groupHistogramData = signal<{ bins: string[]; counts: number[]; variableName: string } | null>(null); - histogramVariants = signal<Array<{ key: string; label: string; data: any }>>([]); - selectedHistogramVariantKey = signal<string | null>(null); + histogramSplit = signal(HISTOGRAM_SPLIT_NONE); + plottedVariable = signal<any | null>(null); + readonly histogramSplitOptions = computed((): HistogramSplitOption[] => + buildHistogramSplitOptions({ + plottedCode: this.plottedVariable()?.code, + selectedVariables: this.experimentStudioService.selectedVariables(), + datasetLevelCount: this.experimentStudioService.selectedDatasets().length, + unavailableVariables: this.experimentStudioService.unavailableVariables(), + }) + ); + readonly canSplitHistogram = computed(() => { + const node = this.plottedVariable(); + return !!node && !node.children?.length && !isNominalHistogramNode(node); + }); + readonly histogramPrivacyTrimNote = computed(() => { + const data = this.histogramData(); + if (!data?.bins?.length || isNominalHistogramNode(this.plottedVariable())) { + return null; + } + return formatClippedNullEdgesNote(describeClippedNullEdges(data.bins, data.counts)); + }); groupHistogramMeta = signal<{ pathNodes: Array<{ code: string; label: string }>; groupCount: number; @@ -65,25 +140,57 @@ export class VariablesPanelComponent implements OnDestroy { longitudinalModels: DataModel[] = []; availableDatasets: { code: string; label: string }[] = []; error: string | null = null; - filteredData: any; // Filtered variables and groups - searchQuery = ''; // Search input - dataWithName: any; - groupVariables: any[] = []; isLoadingHistogram = signal(false); errorMessage = signal<string | null>(null); + emptyChartMessage = signal<string | null>(null); isExporting = signal(false); - refreshKey = signal(0); - histogramBins = signal<number | null>(null); + exportMenuOpen = signal(false); + metadataBrowserMode = signal<MetadataBrowserMode>(this.loadMetadataBrowserMode()); + readonly metadataBrowserModeIndex = computed(() => { + const index = this.metadataBrowserModes.findIndex( + (mode) => mode.value === this.metadataBrowserMode() + ); + return index >= 0 ? index : 0; + }); + contextOpen = signal(false); + searchExpanded = signal(false); + /** Variables missing from a selected dataset: grayed out in every view and never selected. */ + readonly unavailableVariables = this.experimentStudioService.unavailableVariables; + readonly selectedDatasetCount = computed(() => (this.experimentStudioService.selectedDatasets() ?? []).length); + activeDetailsTab = signal<DetailsPanelTab>('histogram'); private destroy$ = new Subject<void>(); - private histogramRequest$ = new Subject<{ codes: string[]; label?: string; bins?: number | null }>(); + private histogramRequest$ = new Subject<{ + codes: string[]; + label?: string; + bins?: number | null; + grouping?: string | null; + }>(); private lastModelKey: string | null = null; constructor() { this.setupHistogramPipeline(); + // A split whose option was removed from the pool or became disabled (too many + // datasets) must not stay drawn under a select that no longer offers it. + effect(() => { + const split = this.histogramSplit(); + if (!split) return; + const option = this.histogramSplitOptions().find((item) => item.code === split); + // A split that became unavailable after a dataset change is kept: nothing chosen is auto-removed. + if (option && (!option.disabled || option.unavailable)) return; + untracked(() => this.applyHistogramSplit(HISTOGRAM_SPLIT_NONE)); + }); + effect(() => { const model = this.selectedDataModel(); - if (!model) return; + if (!model) { + // A studio reset (e.g. a saved experiment whose pathology is gone) must not leave the old chart up. + if (this.lastModelKey !== null) { + this.lastModelKey = null; + untracked(() => this.clearModelView()); + } + return; + } const key = `${model.code}:${model.version}`; if (this.lastModelKey === key) return; this.lastModelKey = key; @@ -91,43 +198,28 @@ export class VariablesPanelComponent implements OnDestroy { if (this.d3Data) { this.onSelectedNodeChange(this.d3Data); } - }, { allowSignalWrites: true }); + }); } ngOnInit(): void { - this.selectedDataModel.set(this.defaultModel); this.loadDataModels(); } - onSearchResult(selectedName: string) { - const found = this.filteredVariables().find(v => v.label === selectedName); - if (found) { - this.highlightNode = found; - this.onSelectedNodeChange(this.highlightNode); - } else { - console.warn('No variable "', selectedName); + onSearchSelected(result: MetadataSearchResult): void { + if (!this.d3Data) { + console.warn('[Search] No data model loaded'); return; } - } - onSearchSelected(code: string) { - const foundVar = this.filteredVariables().find(v => v.code === code); - if (foundVar) { - this.highlightNode = foundVar; // zoom + highlight leaf - this.onSelectedNodeChange(foundVar); // histogram for leaf - return; - } - - const foundGroup = this.findNodeByCode(this.d3Data, code); - if (foundGroup) { - this.highlightNode = { code }; - this.onSelectedNodeChange(foundGroup); - return; - } - - console.warn('[Search] No node found for code:', code); + const index = normalizeMetadataTree(this.d3Data); + const selection = selectionFromSearchResult(index, result); + const node = selection.originalNode; + if (this.isUnavailable(node)) return; + this.highlightNode = { ...node, path: result.path }; + this.onSelectedNodeChange(node); + this.searchExpanded.set(false); } private findNodeByCode(node: any, code: string): any | null { @@ -149,7 +241,19 @@ export class VariablesPanelComponent implements OnDestroy { this.onSelectedNodeChange(variable); } + onBubbleNodeSelected(node: any): void { + if (this.isUnavailable(node)) return; + this.onSelectedNodeChange(node); + } + + onMetadataBrowserNodeSelected(node: any): void { + if (this.isUnavailable(node)) return; + this.highlightNode = node; + this.onSelectedNodeChange(node); + } + onNodeDoubleClicked(node: any): void { + if (this.isUnavailable(node)) return; this.onSelectedNodeChange(node); this.addVariableFromBubble(); this.cdr.detectChanges(); @@ -159,59 +263,174 @@ export class VariablesPanelComponent implements OnDestroy { return this.experimentStudioService.selectedVariables(); } - get selectedCovariates(): any[] { - return this.experimentStudioService.selectedCovariates(); + + setMetadataBrowserMode(mode: MetadataBrowserMode): void { + this.metadataBrowserMode.set(mode); + try { + localStorage.setItem(this.metadataBrowserModeStorageKey, mode); + } catch { + // Ignore private-mode or quota failures; the UI can still use the in-memory signal. + } + } + + setActiveDetailsTab(tab: DetailsPanelTab): void { + this.activeDetailsTab.set(tab); + } + + toggleContext(): void { + this.contextOpen.update(open => !open); + } + + get contextChipTitle(): string { + const label = this.selectedDataModel()?.label; + if (!label) { + return 'Select a pathology'; + } + const count = this.selectedDatasetCount(); + if (count > 0) { + return `${label} · ${count} dataset${count === 1 ? '' : 's'}`; + } + return `${label} · Select datasets`; + } + + toggleSearch(): void { + this.searchExpanded.update(expanded => { + if (!expanded) { + setTimeout(() => { + this.searchSection?.nativeElement?.querySelector('input')?.focus(); + }, 0); + } + return !expanded; + }); } - get selectedFilters(): any[] { - return this.experimentStudioService.selectedFilters(); + @HostListener('document:click', ['$event']) + onDocumentClickForContext(event: MouseEvent): void { + const target = event.target as Node; + if (this.contextOpen() && this.contextSection && !this.contextSection.nativeElement.contains(target)) { + this.contextOpen.set(false); + } + if ( + this.searchExpanded() + && this.searchSection + && !this.searchSection.nativeElement.contains(target) + ) { + this.searchExpanded.set(false); + } + if (this.exportMenuOpen() && !(target as HTMLElement)?.closest?.('.histogram-export-group')) { + this.exportMenuOpen.set(false); + } + } + + @HostListener('document:keydown', ['$event']) + onDocumentKeydownForContext(event: KeyboardEvent): void { + if (event.key === 'Escape') { + this.contextOpen.set(false); + this.searchExpanded.set(false); + this.exportMenuOpen.set(false); + } } get hasSelectedDatasets(): boolean { return (this.experimentStudioService.selectedDatasets() || []).length > 0; } - addVariableFromBubble(): void { - this.variableFilterSelection?.addVariable(); + private isUnavailable(node: any): boolean { + return isVariableUnavailable(this.unavailableVariables(), node); + } + + isSelectedNodeInPool(): boolean { + // Unavailable variables can never be added, so they do not count against "all added"; + // a node with only unavailable variables still reads as pooled when they were added earlier. + const addable = this.addableTargets(); + const targets = addable.length ? addable : this.selectionTargets(); + if (!targets.length) { + return false; + } + const selected = this.experimentStudioService.selectedVariables(); + return targets.every((item) => selected.some((variable) => variable.code === item.code)); } - addCovariateFromBubble(): void { - this.variableFilterSelection?.addCovariate(); + selectionToggleDisabled(): boolean { + return !this.hasSelectedDatasets + || !this.selectedNode + || (this.selectedNodeOnlyUnavailable() && !this.isSelectedNodeInPool()); } - addFilterFromBubble(): void { - this.variableFilterSelection?.addFilter(); + /** The selected node offers nothing to add: every variable under it is unavailable. */ + private selectedNodeOnlyUnavailable(): boolean { + return this.selectionTargets().length > 0 && this.addableTargets().length === 0; } - findParentNode(currentNode: any, targetNode: any, parent: any = null): any { - if (currentNode === targetNode) return parent; - for (const child of currentNode.children || []) { - const foundParent = this.findParentNode(child, targetNode, currentNode); - if (foundParent) return foundParent; + selectionToggleLabel(): string { + return this.isSelectedNodeInPool() ? 'Remove' : 'Add'; + } + + selectionToggleTitle(): string { + if (!this.hasSelectedDatasets) { + return 'Select datasets first'; } - return null; + if (!this.selectedNode) { + return 'Select a variable or group on the map'; + } + if (this.selectedNodeOnlyUnavailable() && !this.isSelectedNodeInPool()) { + return 'Not available in the selected datasets'; + } + return this.isSelectedNodeInPool() + ? 'Remove the selected variable from this experiment' + : 'Add the selected variable to this experiment'; } - // Recursively find a node by name - findNodeByName(node: any, name: string): any { - if (node.label === name) return node; - for (const child of node.children || []) { - const found = this.findNodeByName(child, name); - if (found) return found; + toggleSelectedNodeInPool(): void { + if (this.selectionToggleDisabled()) { + return; } - return null; + if (!this.selectionTargets().length) { + return; + } + if (this.isSelectedNodeInPool()) { + const removeCodes = new Set(this.selectionTargets().map((item) => item.code)); + this.experimentStudioService.setVariables( + this.experimentStudioService.selectedVariables().filter((variable) => !removeCodes.has(variable.code)) + ); + return; + } + this.addableTargets().forEach((item) => this.experimentStudioService.addVariableAndEnrich(item)); + } + + addVariableFromBubble(): void { + if (!this.hasSelectedDatasets || !this.selectedNode) { + return; + } + this.addableTargets().forEach((item) => this.experimentStudioService.addVariableAndEnrich(item)); } - onVariableChange(updatedVariables: any[]): void { - this.experimentStudioService.setVariables(updatedVariables); + /** Selection targets minus unavailable variables, so adding a group never pools one. */ + private addableTargets(): any[] { + return this.selectionTargets().filter((item) => !this.isUnavailable(item)); } - onCovariateChange(updatedCovariates: any[]): void { - this.experimentStudioService.setCovariates(updatedCovariates); + private selectionTargets(): any[] { + const selectedNode = this.selectedNode; + if (!selectedNode) { + return []; + } + return selectedNode.children ? this.getLeafNodes(selectedNode) : [selectedNode]; } - onFilterChange(updatedFilters: any[]): void { - this.experimentStudioService.setFilters(updatedFilters); + private getLeafNodes(node: any): any[] { + const leaves: any[] = []; + + function collectLeaves(n: any) { + if (!n.children || n.children.length === 0) { + leaves.push(n); + } else { + n.children.forEach(collectLeaves); + } + } + + collectLeaves(node); + return leaves; } loadDataModels(): void { @@ -237,9 +456,30 @@ export class VariablesPanelComponent implements OnDestroy { && String(model.version) === String(current.version) ) ?? null : null; - this.selectedDataModel.set(next ?? models[0] ?? null); + const fallback = this.experimentStudioService.editingExistingExperiment() + ? null + : models[0] ?? null; + this.selectedDataModel.set(next ?? fallback); this.experimentStudioService.selectedDataModel.set(this.selectedDataModel() ?? null); + return; } + + this.selectedDataModel.set(null); + this.experimentStudioService.selectedDataModel.set(null); + this.clearModelView(); + } + + private clearModelView(): void { + this.availableDatasets = []; + this.filteredVariables.set([]); + this.filteredGroups.set([]); + this.d3Data = null; + this.selectedNode = null; + this.guideState.setSelectedHierarchyNode(null); + this.histogramData.set(null); + this.groupHistogramData.set(null); + this.groupHistogramMeta.set(null); + this.activeDetailsTab.set('histogram'); } loadVisualizationData(): void { @@ -274,23 +514,30 @@ export class VariablesPanelComponent implements OnDestroy { code: String(dataset?.code ?? ''), label: String(dataset?.label ?? dataset?.name ?? dataset?.code ?? ''), })); + this.experimentStudioService.availableDatasets.set(this.availableDatasets); + this.ensureDatasetsSelected(); } - fetchFederationHistogram(): void { - const federation = this.selectedDataModel(); - if (!federation) { - console.warn('No federation selected.'); + /** + * A new session (or a fresh pathology pick) should land with a working + * dataset gate, not a lone "0". Auto-select every available dataset unless + * the user has a valid selection or is opening an existing experiment. + */ + ensureDatasetsSelected(): void { + if (this.experimentStudioService.editingExistingExperiment()) { return; } - - const federationGroups = this.d3Data.children || []; - const groupCodes = federationGroups.map((g: any) => g.code); - - const algorithmName = "histogram"; - - this.queueHistogramRequest(groupCodes, 'Federation'); + const selected = new Set(this.experimentStudioService.selectedDatasets()); + const hasValidSelection = this.availableDatasets.some((dataset) => selected.has(dataset.code)); + if (hasValidSelection) { + return; + } + this.experimentStudioService.setSelectedDatasets( + this.availableDatasets.map((dataset) => dataset.code) + ); } + // end of services functions onSelectedDataModelChange(selectedDataModel: DataModel | null): void { if (!selectedDataModel) return; @@ -306,72 +553,34 @@ export class VariablesPanelComponent implements OnDestroy { this.experimentStudioService.selectedDataModel.set(selectedDataModel); // clean up selections - this.experimentStudioService.setVariables([]); - this.experimentStudioService.setCovariates([]); - this.experimentStudioService.setFilters([]); + this.experimentStudioService.clearSelectionsForDataModelChange(); this.filteredVariables.set([]); this.filteredGroups.set([]); - this.distributionData.set(null); + this.histogramData.set(null); this.groupHistogramData.set(null); this.groupHistogramMeta.set(null); + this.activeDetailsTab.set('histogram'); + this.searchExpanded.set(false); // reload new model data this.loadVisualizationData(); } - // search bar functions - onSearchQueryChange(query: string): void { - this.searchQuery = query.toLowerCase(); - this.filterData(); - } - - filterData(): void { - const filterNodes = (node: any) => { - if (node.label.toLowerCase().includes(this.searchQuery)) { - return { ...node }; - } - if (node.children) { - const filteredChildren = node.children.map(filterNodes).filter(Boolean); - if (filteredChildren.length > 0) { - return { ...node, children: filteredChildren }; - } - } - return null; - }; - this.filteredData = filterNodes(this.d3Data) || { name: 'No Results', children: [] }; - } - - getAllLeafNodes(node: any): any[] { - if (!node.children || node.children.length === 0) { - return []; - } - - const leaves: any[] = []; - - function collectLeaves(n: any) { - if (!n.children || n.children.length === 0) { - leaves.push(n); - } else { - n.children.forEach(collectLeaves); - } - } - collectLeaves(node); - return leaves; - } onSelectedNodeChange(node: any): void { if (!node) { this.selectedNode = null; + this.guideState.setSelectedHierarchyNode(null); this.cdr.detectChanges(); - this.errorMessage.set(null); - this.distributionData.set(null); + this.histogramData.set(null); this.groupHistogramData.set(null); - this.histogramVariants.set([]); - this.selectedHistogramVariantKey.set(null); + this.histogramSplit.set(HISTOGRAM_SPLIT_NONE); + this.plottedVariable.set(null); this.groupHistogramMeta.set(null); this.isLoadingHistogram.set(false); this.errorMessage.set('No variable selected.'); + this.activeDetailsTab.set('histogram'); return; } @@ -381,15 +590,15 @@ export class VariablesPanelComponent implements OnDestroy { } this.selectedNode = { ...node }; + this.activeDetailsTab.set('histogram'); + this.guideState.setSelectedHierarchyNode(this.selectedNode); this.cdr.detectChanges(); - this.errorMessage.set(null); - this.distributionData.set(null); // clear previous histogram + this.clearChartNotices(); + this.histogramData.set(null); // clear previous histogram this.groupHistogramData.set(null); - this.histogramVariants.set([]); - this.selectedHistogramVariantKey.set(null); + this.histogramSplit.set(HISTOGRAM_SPLIT_NONE); + this.plottedVariable.set(node); this.groupHistogramMeta.set(null); - this.histogramBins.set(null); // Reset bins on new selection - if (!node) { this.isLoadingHistogram.set(false); this.errorMessage.set('No variable selected.'); @@ -409,21 +618,39 @@ export class VariablesPanelComponent implements OnDestroy { return; } - const bins = items.map((child: any) => String(child?.label ?? child?.name ?? child?.code ?? '')); - const counts = items.map((child: any) => this.countLeafNodes(child)); const pathNodes = this.getPathNodes(node); - this.isLoadingHistogram.set(false); - this.groupHistogramData.set({ - bins, - counts, - variableName: String(node?.label ?? 'Groups'), - }); this.groupHistogramMeta.set({ pathNodes, groupCount: items.length, hasGroups, }); + + // A group that holds only variables has nothing to census: every bar would be a + // single variable. Ask the user to pick a variable from the browser instead. + if (!hasGroups) { + this.emptyChartMessage.set(GROUP_OF_VARIABLES_ONLY_MESSAGE); + return; + } + + const rows = items + .map((child: any) => ({ + bin: String(child?.label ?? child?.name ?? child?.code ?? ''), + count: countLeafNodes(child), + })) + .sort((a: { bin: string; count: number }, b: { bin: string; count: number }) => b.count - a.count); + + this.groupHistogramData.set({ + bins: rows.map((row: { bin: string; count: number }) => row.bin), + counts: rows.map((row: { bin: string; count: number }) => row.count), + variableName: String(node?.label ?? 'Groups'), + }); + return; + } + + if (isFreeTextCde(node)) { + this.isLoadingHistogram.set(false); + this.emptyChartMessage.set('Free-text variable: no histogram available.'); return; } @@ -431,19 +658,6 @@ export class VariablesPanelComponent implements OnDestroy { this.queueHistogramRequest(codes, node.label); } - addGroupVariables(): void { - if (!this.groupVariables.length) return; - - const existingCodes = new Set(this.selectedVariables.map(v => v.code)); - const newVariables = this.groupVariables.filter((v: any) => !existingCodes.has(v.code)); - - newVariables.forEach((variable: any) => { - this.experimentStudioService.addVariableAndEnrich(variable); - }); - - // Update signal if it needs to refresh manually - this.onVariableChange([...this.selectedVariables, ...newVariables]); - } ngOnDestroy(): void { this.destroy$.next(); @@ -454,10 +668,16 @@ export class VariablesPanelComponent implements OnDestroy { this.histogramRequest$ .pipe( takeUntil(this.destroy$), - switchMap(({ codes, label, bins }) => { - const algoName = 'histogram'; + switchMap(({ codes, label, bins, grouping }) => { return this.experimentStudioService - .getAlgorithmResults(algoName, codes, bins) + .getAlgorithmResults( + AlgorithmNames.HISTOGRAM, + codes, + bins ?? null, + undefined, + undefined, + grouping ? [grouping] : undefined + ) .pipe( catchError((error) => { this.isLoadingHistogram.set(false); @@ -465,94 +685,114 @@ export class VariablesPanelComponent implements OnDestroy { this.errorMessage.set('Error loading histogram. Please try again.'); return of(null); }), - map((response) => ({ response, label, codes })) + map((response) => ({ response, label, codes, grouping: grouping ?? null })) ); }) ) - .subscribe(({ response, label, codes }) => { - this.isLoadingHistogram.set(false); - - if (!response) return; - - const histList = response?.result?.histogram ?? response?.histogram ?? []; - if (histList.length) { - const variableCode = histList[0]?.variable ?? codes?.[0]; - const variableNode = variableCode ? this.findNodeByCode(this.d3Data, variableCode) : null; - - const variants = histList.map((hist: any, idx: number) => { - const enrichedHistogram = this.mapBinsToEnumLabels(hist, variableNode?.enumerations); - const groupingVarCode = hist?.grouping_var; - const groupingVarNode = groupingVarCode ? this.findNodeByCode(this.d3Data, groupingVarCode) : null; - const groupingVarLabel = groupingVarNode?.label ?? groupingVarCode; - const groupingEnumLabel = this.mapEnumValueLabel(hist?.grouping_enum, groupingVarNode?.enumerations); - - const variantLabel = groupingVarCode - ? `${groupingVarLabel}: ${groupingEnumLabel ?? hist?.grouping_enum ?? 'N/A'}` - : 'Overall'; - - const dataWithName = { - ...enrichedHistogram, - variableName: label ?? variableNode?.label ?? enrichedHistogram.variable ?? enrichedHistogram.variableName, - variableType: variableNode?.type - }; - - return { - key: groupingVarCode ? `${groupingVarCode}:${String(hist?.grouping_enum ?? idx)}` : 'overall', - label: variantLabel, - data: dataWithName, - isOverall: !groupingVarCode, - }; - }); - - const sortedVariants = [ - ...variants.filter((v: { isOverall: boolean }) => v.isOverall), - ...variants.filter((v: { isOverall: boolean }) => !v.isOverall), - ].map(({ key, label: variantLabel, data }) => ({ key, label: variantLabel, data })); - - this.histogramVariants.set(sortedVariants); - const selectedKey = sortedVariants[0]?.key ?? null; - this.selectedHistogramVariantKey.set(selectedKey); - this.distributionData.set(sortedVariants[0]?.data ?? null); - this.errorMessage.set(null); - } else { - this.histogramVariants.set([]); - this.selectedHistogramVariantKey.set(null); - const resultData = response?.result?.data || response?.data; - if (typeof resultData === 'string' && resultData.includes('insufficient data')) { - this.errorMessage.set(resultData); - } else { - this.errorMessage.set('No histogram data found for this selection.'); + .subscribe({ + next: ({ response, label, codes, grouping }) => { + this.isLoadingHistogram.set(false); + + if (!response) return; + + try { + const histList = extractHistogramItems(response); + if (histList.length) { + const variableCode = String(histList[0]?.var ?? histList[0]?.variable ?? codes?.[0] ?? ''); + const variableNode = variableCode ? this.findNodeByCode(this.d3Data, variableCode) : null; + const stacked = buildStackedHistogramSeries( + histList, + variableCode, + grouping, + (groupingVar, groupingEnum) => this.groupingEnumLabel(groupingVar, groupingEnum), + this.experimentStudioService.selectedDatasets() + ); + if (!stacked) { + this.histogramData.set(null); + this.emptyChartMessage.set('No distribution for this selection.'); + return; + } + const enriched = this.mapBinsToEnumLabels( + { ...stacked, variable: variableCode }, + variableNode?.enumerations + ); + const resolvedVariable = this.findNodeByCode(this.d3Data, variableCode); + const binLabels = (enriched.bins ?? stacked.bins).map(String); + const splitLabel = grouping + ? (this.histogramSplitOptions().find((option) => option.code === grouping)?.label ?? grouping) + : undefined; + this.histogramData.set({ + bins: binLabels, + counts: stacked.counts, + groups: stacked.groups, + variableName: label ?? variableNode?.label ?? resolvedVariable?.label ?? '', + variableType: variableNode?.type, + splitLabel, + }); + this.clearChartNotices(); + } else { + this.histogramData.set(null); + const resultData = response?.result?.data || response?.data; + if (typeof resultData === 'string' && resultData.includes('insufficient data')) { + this.emptyChartMessage.set('This variable does not have sufficient data.'); + } else { + this.emptyChartMessage.set('No distribution for this selection.'); + } + } + } catch (error) { + console.error('Error parsing histogram:', error); + this.histogramData.set(null); + this.errorMessage.set('Error loading histogram. Please try again.'); } - } + }, + error: (error) => { + this.isLoadingHistogram.set(false); + console.error('Error fetching histogram:', error); + this.errorMessage.set('Error loading histogram. Please try again.'); + }, }); } - private queueHistogramRequest(codes: string[], label?: string, bins: number | null = null) { - this.isLoadingHistogram.set(true); + private clearChartNotices(): void { this.errorMessage.set(null); - this.distributionData.set(null); - this.histogramVariants.set([]); - this.selectedHistogramVariantKey.set(null); - this.histogramRequest$.next({ codes, label, bins }); + this.emptyChartMessage.set(null); } - onHistogramVariantChange(event: Event): void { - const key = (event.target as HTMLSelectElement).value; - this.selectedHistogramVariantKey.set(key); - const variant = this.histogramVariants().find((item) => item.key === key); - if (variant) { - this.distributionData.set(variant.data); - } + private queueHistogramRequest( + codes: string[], + label?: string, + bins: number | null = null, + grouping: string | null = null + ) { + this.isLoadingHistogram.set(true); + this.clearChartNotices(); + this.histogramData.set(null); + this.histogramRequest$.next({ codes, label, bins, grouping }); } - onBinsChange(event: Event): void { - const value = (event.target as HTMLInputElement).value; - const bins = value ? parseInt(value, 10) : null; - this.histogramBins.set(bins); + onHistogramSplitChange(event: Event): void { + const split = (event.target as HTMLSelectElement).value; + const option = this.histogramSplitOptions().find((item) => item.code === split); + this.applyHistogramSplit(option && !option.disabled ? split : HISTOGRAM_SPLIT_NONE); + } + + private applyHistogramSplit(resolved: string): void { + this.histogramSplit.set(resolved); + const node = this.selectedNode ?? this.plottedVariable(); + if (!node?.code || node.children?.length) return; + this.queueHistogramRequest([node.code], node.label, null, resolved || null); + } - if (this.selectedNode) { - this.queueHistogramRequest([this.selectedNode.code], this.selectedNode.label, bins); + private groupingEnumLabel(groupingVar: string, groupingEnum: string): string { + if (groupingVar.toLowerCase() === DATASET_VARIABLE_CODE) { + const fromMap = this.experimentStudioService.getDatasetLabelMap()[groupingEnum]; + if (fromMap) return fromMap; + const fromList = this.availableDatasets.find((dataset) => dataset.code === groupingEnum)?.label; + if (fromList) return fromList; } + const node = this.findNodeByCode(this.d3Data, groupingVar) + ?? this.experimentStudioService.selectedVariables().find((variable: { code?: string }) => variable.code === groupingVar); + return this.mapEnumValueLabel(groupingEnum, node?.enumerations) ?? groupingEnum; } getPathNodes(node: any): Array<{ code: string; label: string }> { @@ -566,7 +806,14 @@ export class VariablesPanelComponent implements OnDestroy { if (!found) { return [{ code: String(code), label: String(node?.label ?? code) }]; } - return pathNodes; + const collapsed: Array<{ code: string; label: string }> = []; + for (const pathNode of pathNodes) { + if (collapsed.length && collapsed[collapsed.length - 1].label === pathNode.label) { + continue; + } + collapsed.push(pathNode); + } + return collapsed; } private collectPathNodes( @@ -588,79 +835,96 @@ export class VariablesPanelComponent implements OnDestroy { return false; } - onGroupSummaryClick(node: { code: string }): void { - if (!node?.code) return; - const target = this.findNodeByCode(this.d3Data, node.code); - if (!target) return; - this.highlightNode = target; - this.onSelectedNodeChange(target); - } - isExportDisabled(): boolean { if (this.isLoadingHistogram()) return true; if (this.errorMessage()) return true; if (!this.selectedNode) return true; - return !this.distributionData() && !this.groupHistogramData(); + return !this.histogramData() && !this.groupHistogramData(); + } + + isSelectedNodeGroup(): boolean { + return !!this.selectedNode?.children?.length; + } + + detailsPanelTitle(): string { + if (!this.selectedNode) { + return 'Select a variable'; + } + return String(this.selectedNode.label ?? '').trim(); + } + + detailsPanelSubtitle(): string { + if (!this.selectedNode) { + return ''; + } + if (this.isSelectedNodeGroup()) { + const meta = this.groupHistogramMeta(); + if (meta) { + return meta.hasGroups + ? `${meta.groupCount} groups in this view` + : `${meta.groupCount} variables in this group`; + } + } + return String(this.selectedNode.description ?? '').trim(); } - showBinSelector(): boolean { - if (!this.selectedNode || !this.selectedNode.type) return false; - const type = String(this.selectedNode.type).toLowerCase(); - return ['real', 'integer', 'int'].includes(type); + toggleExportMenu(): void { + this.exportMenuOpen.update(open => !open); } - async exportDistributionPdf(): Promise<void> { + closeExportMenu(): void { + this.exportMenuOpen.set(false); + } + + async exportHistogramPdf(): Promise<void> { + this.closeExportMenu(); if (this.isExportDisabled()) return; this.isExporting.set(true); try { - const exportTarget = this.distributionExport?.nativeElement; + const exportTarget = this.histogramExport?.nativeElement; if (!exportTarget) { - console.warn('Distribution export target not found.'); + console.warn('Histogram export target not found.'); return; } - await this.pdfExportService.exportDistributionPdf(exportTarget, { - title: this.groupHistogramData() ? 'Group Description' : 'Distribution Graph', - nodeLabel: String(this.selectedNode?.label ?? this.distributionData()?.variableName ?? ''), + await this.pdfExportService.exportHistogramPdf(exportTarget, { + title: this.groupHistogramData() ? 'Group Description' : 'Histogram', + nodeLabel: String(this.selectedNode?.label ?? this.histogramData()?.variableName ?? ''), modelLabel: String(this.selectedDataModel()?.label ?? this.selectedDataModel()?.code ?? ''), datasetLabels: this.experimentStudioService.selectedDatasets() .map(code => this.availableDatasets.find(d => d.code === code)?.label ?? code) .filter(label => !!label), description: String(this.selectedNode?.description ?? ''), meta: this.groupHistogramMeta() ?? undefined, - isGroupView: !!this.groupHistogramData() + isGroupView: !!this.groupHistogramData(), + mipVersion: this.mipVersion, }); } catch (err) { - console.error('Distribution PDF export failed:', err); + console.error('Histogram PDF export failed:', err); } finally { this.isExporting.set(false); } } - exportDistributionCsv(): void { + exportHistogramCsv(): void { + this.closeExportMenu(); if (this.isExportDisabled()) return; - const data = this.groupHistogramData() || this.distributionData(); + const data = this.groupHistogramData() || this.histogramData(); if (!data) return; try { - this.csvExportService.exportDistributionCsv( - { bins: data.bins, counts: data.counts }, - String(this.selectedNode?.label ?? data.variableName ?? 'distribution') + this.csvExportService.exportHistogramCsv( + { bins: data.bins, counts: data.counts, groups: 'groups' in data ? data.groups : undefined }, + String(this.selectedNode?.label ?? data.variableName ?? 'histogram') ); } catch (err) { - console.error('Distribution CSV export failed:', err); + console.error('Histogram CSV export failed:', err); } } - private countLeafNodes(node: any): number { - if (!node?.children || node.children.length === 0) { - return 1; - } - return node.children.reduce((total: number, child: any) => total + this.countLeafNodes(child), 0); - } /** * Replace histogram bin codes with enumeration labels when available. @@ -693,4 +957,15 @@ export class VariablesPanelComponent implements OnDestroy { return String(match?.label ?? match?.name ?? value); } + private loadMetadataBrowserMode(): MetadataBrowserMode { + try { + const saved = localStorage.getItem(this.metadataBrowserModeStorageKey); + if (saved === 'ontology' || saved === 'collapsible' || saved === 'bubble') { + return saved; + } + } catch { + // Fall through to the Map default. + } + return 'bubble'; + } } diff --git a/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.spec.ts b/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.spec.ts index abd46611b..45e254265 100644 --- a/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.spec.ts +++ b/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.spec.ts @@ -1,4 +1,7 @@ -import { AlgorithmTableRegistry } from './algorithm-table-registry'; +import { cellText, exportTable, trimTable, AlgorithmTableRegistry } from './algorithm-table-registry'; + +/** The rows as the screen prints them. */ +const shown = (rows: unknown[][]) => rows.map((row) => row.map(cellText)); describe('AlgorithmTableRegistry', () => { it('supports linear_regression_cv metrics provided as mean/std objects', () => { @@ -6,7 +9,7 @@ describe('AlgorithmTableRegistry', () => { dependent_var: 'y', indep_vars: ['x1', 'x2'], n_obs: [100, 101, 99], - mean_sq_error: { mean: 1.25, std: 0.2 }, + root_mean_sq_error: { mean: 1.25, std: 0.2 }, r_squared: { mean: 0.91, std: 0.03 }, mean_abs_error: { mean: 0.72, std: 0.11 }, f_stat: { mean: 12.3, std: 1.1 }, @@ -19,6 +22,39 @@ describe('AlgorithmTableRegistry', () => { expect(tables[1].rows[0][2]).toBe(0.2); }); + it('shows undefined linear_regression_cv summaries from Exaflow [mean, std] arrays', () => { + const tables = AlgorithmTableRegistry['linear_regression_cv']({ + dependent_var: 'y', + indep_vars: ['Intercept'], + n_obs: [80, 80], + root_mean_sq_error: [1.25, 0.2], + r_squared: [null, null], + mean_abs_error: [0.72, 0.11], + f_stat: [null, null], + }); + + expect(tables[1].rows).toEqual([ + ['Root mean squared error', 1.25, 0.2], + ['R-squared', 'Undefined', 'Undefined'], + ['Mean absolute error', 0.72, 0.11], + ['F diagnostic', 'Undefined', 'Undefined'], + ]); + }); + + it('reads RMSE from the legacy mean_sq_error field of stored linear_regression_cv results', () => { + const tables = AlgorithmTableRegistry['linear_regression_cv']({ + dependent_var: 'y', + indep_vars: ['x1'], + n_obs: [100, 101], + mean_sq_error: [1.5, 0.3], + r_squared: [0.9, 0.02], + mean_abs_error: [0.8, 0.1], + f_stat: [10, 1], + } as any); + + expect(tables[1].rows[0]).toEqual(['Root mean squared error', 1.5, 0.3]); + }); + it('supports logistic_regression stderr field mapping', () => { const tables = AlgorithmTableRegistry['logistic_regression']({ dependent_var: 'outcome', @@ -40,6 +76,29 @@ describe('AlgorithmTableRegistry', () => { expect(tables[0].rows[0][2]).toBe('0.05'); }); + it('uses Exaflow F-stat display fields in linear_regression summary', () => { + const tables = AlgorithmTableRegistry['linear_regression']({ + dependent_var: 'target', + indep_vars: ['Intercept', 'x1'], + coefficients: [1.5, -0.2], + std_err: [0.1, 0.04], + t_stats: [15, -5], + pvalues: [0.0001, 0.0002], + lower_ci: [1.2, -0.28], + upper_ci: [1.8, -0.12], + f_stat: Number.POSITIVE_INFINITY, + f_pvalue: 0, + f_stat_display: 'Perfect fit', + f_pvalue_display: '<0.001', + f_stat_note: 'Residual sum of squares is zero.', + } as any); + + const summary = tables[1].rows; + expect(summary).toContainEqual(['F-statistic', 'Perfect fit']); + expect(summary).toContainEqual(['p-value (F-stat)', '<0.001']); + expect(summary).toContainEqual(['F-statistic note', 'Residual sum of squares is zero.']); + }); + it('includes ll/aic/bic and dependent variable in linear_regression summary', () => { const tables = AlgorithmTableRegistry['linear_regression']({ dependent_var: 'target', @@ -98,7 +157,7 @@ describe('AlgorithmTableRegistry', () => { }, }); - expect(tables.some((t) => t.title === 'Group Min/Max')).toBeTrue(); + expect(tables.some((t) => t.title === 'Group Min/Max')).toBe(true); }); it('renders linear_svm via canonical key', () => { @@ -111,10 +170,27 @@ describe('AlgorithmTableRegistry', () => { const canonical = AlgorithmTableRegistry['linear_svm'](payload); expect(canonical.length).toBeGreaterThan(0); - expect(canonical[0].rows.some((row) => row[0] === 'Intercept')).toBeTrue(); + expect(canonical[0].rows.some((row) => row[0] === 'Intercept')).toBe(true); + }); + + it('renders categorical naive Bayes counts with class-conditional shares', () => { + const tables = AlgorithmTableRegistry['naive_bayes_categorical']({ + classes: ['0', '1'], + class_count: [50, 60], + class_log_prior: [-0.7, -0.6], + feature_names: ['sex'], + categories: { sex: ['0', '1'] }, + category_count: { sex: [[25, 25], [30, 30]] }, + category_log_prob: { sex: [[-0.69, -0.69], [-0.65, -0.65]] }, + }); + + const sex = tables.find((t) => t.title === 'sex by class')!; + expect(sex.columns).toEqual(['Category', '0', '1']); + expect(sex.rows[0]).toEqual(['0', '25 · 50.2%', '30 · 52.2%']); + expect(shown(tables[0].rows)[0]).toEqual(['0', '50', '49.7%']); }); - it('renders category_log_prob tables for naive_bayes_categorical', () => { + it('exports categorical naive Bayes counts, P(category | class) and priors exactly, one value per column', () => { const tables = AlgorithmTableRegistry['naive_bayes_categorical']({ classes: ['0', '1'], class_count: [50, 60], @@ -125,30 +201,160 @@ describe('AlgorithmTableRegistry', () => { category_log_prob: { sex: [[-0.69, -0.69], [-0.65, -0.65]] }, }); - expect(tables.some((t) => t.title?.startsWith('Category Log Probabilities'))).toBeTrue(); + const sex = exportTable(tables.find((t) => t.title === 'sex by class')!); + expect(sex.columns).toEqual(['Category', '0 count', '0 P(category | class)', '1 count', '1 P(category | class)']); + expect(sex.rows[0]).toEqual(['0', 25, Math.exp(-0.69), 30, Math.exp(-0.65)]); + expect(exportTable(tables[0]).rows[0]).toEqual(['0', '50', Math.exp(-0.7)]); }); - it('renders pca summary and keeps pca_with_transformation alias', () => { + it('renders no pca summary for a result without components', () => { const payload = { title: 'Eigenvalues and Eigenvectors', n_obs: 120, eigenvalues: [3.1, 1.2, 0.7], }; - const base = AlgorithmTableRegistry['pca'](payload); - const transformed = AlgorithmTableRegistry['pca_with_transformation'](payload); - expect(base.length).toBe(1); - expect(base[0].title).toBe('PCA Summary'); - expect(transformed).toEqual(base); + expect(AlgorithmTableRegistry['pca'](payload as any)).toEqual([]); + }); + + it('names pca weight rows with the labels a caller supplies over the raw codes', () => { + const tables = AlgorithmTableRegistry['pca']({ + title: 'Eigenvalues and Eigenvectors', + n_obs: 120, + eigenvalues: [1.2, 0.8], + eigenvectors: [ + [0.7, 0.2], + [0.1, 0.9], + ], + variables: ['age', 'bmi'], + variable_names: ['Age', 'BMI'], + } as any); + + const weights = tables.find((table) => table.title === 'Component weights')!; + expect(weights.rows.map((row) => row[0])).toEqual(['Age', 'BMI']); + }); + + it('renders pca summary percentages from explained_variance_ratio', () => { + const tables = AlgorithmTableRegistry['pca']({ + title: 'Eigenvalues and Eigenvectors', + n_obs: 120, + eigenvalues: [3.42, 0.58], + eigenvectors: [ + [0.7, 0.2], + [0.1, 0.9], + ], + variables: ['age', 'bmi'], + components: [ + { + component_id: 'PC1', + explained_variance: 3.42, + explained_variance_ratio: 0.855, + cumulative_explained_variance: 0.855, + loadings: { age: 0.7, bmi: 0.2 }, + }, + { + component_id: 'PC2', + explained_variance: 0.58, + explained_variance_ratio: 0.145, + cumulative_explained_variance: 1, + loadings: { age: 0.1, bmi: 0.9 }, + }, + ], + reusable_preprocessing: null, + }); + + expect(tables[0].columns).toEqual([ + 'Component', + 'Eigenvalue', + 'Variance explained (%)', + 'Cumulative (%)', + ]); + expect(tables[0].rows[0]).toEqual(['PC1', '3.42', '85.5%', '85.5%']); + expect(tables[0].rows[1]).toEqual(['PC2', '0.58', '14.5%', '100.0%']); + }); + + it('renders quartiles tables and preserves unavailable quantile values', () => { + const tables = AlgorithmTableRegistry['quartiles']({ + quantiles: [ + { q: 0.25, value: 2.8124, actual_q: 0.2535211267605634 }, + { q: 0.5, value: null, actual_q: null }, + { q: 0.75, value: 3.3067, actual_q: 0.7549295774647887 }, + ], + }); + + expect(tables.length).toBe(1); + expect(tables[0].columns).toEqual(['Quantile', 'Value', 'Actual quantile']); + expect(tables[0].rows.length).toBe(3); + expect(tables[0].rows[1]).toEqual(['0.5', '', '']); + }); + + it('renders binned Mann-Whitney U test metrics', () => { + const tables = AlgorithmTableRegistry['binned_mann_whitney_u_test']({ + u_stat: 163.5, + p_value: 8.436804884099734e-6, + z_score: 4.4537889144455, + n1: 15, + n2: 80, + }); + + expect(tables.length).toBe(1); + const rows = shown(tables[0].rows); + expect(rows).toContainEqual(['U statistic', '163.5']); + expect(rows).toContainEqual(['p-value', '< 0.001']); + expect(rows).toContainEqual(['z-score', '4.454']); + expect(rows).toContainEqual(['P(A > B)', '0.14']); + expect(rows).toContainEqual(['Group A sample size', '15']); + expect(rows).toContainEqual(['Group B sample size', '80']); + // Exports carry what the screen rounds. + const exported = exportTable(tables[0]).rows; + expect(exported).toContainEqual(['p-value', 8.436804884099734e-6]); + expect(exported).toContainEqual(['P(A > B)', 163.5 / (15 * 80)]); + }); + + it('prints p = 0 as < 0.001 and leaves P(A > B) out without group sizes', () => { + const tables = AlgorithmTableRegistry['binned_mann_whitney_u_test']({ u_stat: 70, p_value: 0, z_score: 9 } as any); + + expect(shown(tables[0].rows)).toContainEqual(['p-value', '< 0.001']); + expect(tables[0].rows.map((row) => row[0])).not.toContain('P(A > B)'); + }); + + it('renders standardized mean difference comparisons', () => { + const tables = AlgorithmTableRegistry['standardized_mean_difference']({ + comparisons: [ + { group1: 'edsd0', group2: 'ppmi3', smd: -1.502242948685167 }, + { group1: 1, group2: 2, smd: 0.25 }, + ], + }); + + expect(tables.length).toBe(1); + expect(tables[0].title).toBe('Standardized Mean Difference'); + expect(tables[0].columns).toEqual(['Group 1', 'Group 2', 'SMD']); + expect(tables[0].rows.length).toBe(2); + expect(tables[0].rows[0][0]).toBe('edsd0'); + expect(tables[0].rows[0][1]).toBe('ppmi3'); + expect(tables[0].rows[0][2]).toBe('-1.502'); + expect(tables[0].rows[1][0]).toBe('1'); + }); + + it('renders an empty standardized mean difference table when no pair is eligible', () => { + const tables = AlgorithmTableRegistry['standardized_mean_difference']({ comparisons: [] }); + + expect(tables.length).toBe(1); + expect(tables[0].rows.length).toBe(0); }); it('renders describe tables for both numeric and nominal entries', () => { const tables = AlgorithmTableRegistry['describe']({ - variable_based: [ + featurewise: [ { variable: 'age', + dataset: 'all datasets', + data: { num_dtps: 10, num_na: 1, num_total: 11, mean: 52.1, std: 4.2, min: 40, q1: 49, q2: 52, q3: 55, median: 52.5, max: 60 }, + }, + { + variable: 'height', dataset: 'ds1', - data: { num_dtps: 10, num_na: 1, num_total: 11, mean: 52.1, std: 4.2, min: 40, q1: 49, q2: 52, q3: 55, max: 60 }, + data: { num_dtps: 8, num_na: 0, num_total: 8, mean: 170, std: 5, min: 160, q1: 166, q2: 171, q3: 174, max: 180 }, }, { variable: 'sex', @@ -156,11 +362,66 @@ describe('AlgorithmTableRegistry', () => { data: { num_dtps: 10, num_na: 1, num_total: 11, counts: { M: 6, F: 4 } }, }, ], - model_based: [], }); - expect(tables.some((t) => t.title === 'Variable-based Summary — Numeric')).toBeTrue(); - expect(tables.some((t) => t.title === 'Variable-based Summary — Nominal')).toBeTrue(); + expect(tables.some((t) => t.title === 'Featurewise Summary — Numeric')).toBe(true); + expect(tables.some((t) => t.title === 'Featurewise Summary — Nominal')).toBe(true); + expect(tables.some((t) => t.title?.includes('Model-based'))).toBe(false); + const numericRows = tables.find((t) => t.title === 'Featurewise Summary — Numeric')?.rows ?? []; + expect(numericRows[0][9]).toBe('52.5'); + expect(numericRows[1][9]).toBe('171'); + }); + + it('renders outlier_report and preserves zero counts separately from null values', () => { + const tables = AlgorithmTableRegistry['outlier_report']({ + featurewise: [ + { + variable: 'age', + dataset: 'ds1', + data: { + strategy: 'iqr', + tail: 'both', + fold: 1.5, + lower_bound: null, + upper_bound: 90, + lower_outlier_count: 0, + upper_outlier_count: null, + total_outlier_count: 0, + total_outlier_percentage: 0, + }, + }, + ], + }); + + expect(tables.length).toBe(1); + expect(tables[0].columns).toEqual(['Variable', 'Dataset', 'Rule', 'Kept range', 'Below', 'Above', 'Outliers', 'Share']); + expect(tables[0].rows[0][2]).toBe('IQR × 1.5, both tails'); + expect(tables[0].rows[0][3]).toBe('— to 90'); + expect(tables[0].rows[0][4]).toBe('0'); + expect(tables[0].rows[0][5]).toBe('Unavailable'); + expect(tables[0].rows[0][6]).toBe('0'); + expect(tables[0].rows[0][7]).toBe('0.0%'); + }); + + it('exports the outlier report with separate exact bounds and share', () => { + const tables = AlgorithmTableRegistry['outlier_report']({ + featurewise: [ + { + variable: 'age', + dataset: 'ds1', + data: { + strategy: 'gaussian', tail: 'both', fold: 3, + lower_bound: 12.345678, upper_bound: 90.123456, + lower_outlier_count: 2, upper_outlier_count: 7, total_outlier_count: 9, total_outlier_percentage: 8.91734, + }, + }, + ], + }); + + expect(tables[0].rows[0][3]).toBe('12.346 to 90.123'); + const exported = exportTable(tables[0]); + expect(exported.columns).toEqual(['Variable', 'Dataset', 'Rule', 'Lower bound', 'Upper bound', 'Below', 'Above', 'Outliers', 'Share (%)']); + expect(exported.rows[0]).toEqual(['age', 'ds1', 'Mean ± SD × 3, both tails', 12.345678, 90.123456, 2, 7, 9, 8.91734]); }); it('filters frontend metadata keys from ttest results', () => { @@ -190,4 +451,314 @@ describe('AlgorithmTableRegistry', () => { expect(rowNames).not.toContain('enumMaps'); expect(rowNames).not.toContain('yVar'); }); + + it('formats paired t-test metrics with three digits', () => { + const tables = AlgorithmTableRegistry['ttest_paired']({ + t_stat: 3.888283695725533, + df: 12, + p: 1.017999869046271e-4, + mean_diff: 0.06754973386270802, + se_diff: 0.01737263511326778, + ci_upper: 0.10160479913374154, + ci_lower: 0.033494668591674485, + }); + + const rows = shown(tables[0].rows); + expect(rows).toContainEqual(['T-statistic', '3.888']); + expect(rows).toContainEqual(['Degrees of Freedom', '12.000']); + expect(rows).toContainEqual(['p-value', '< 0.001']); + expect(rows).toContainEqual(['Mean Difference', '0.068']); + expect(exportTable(tables[0]).rows).toContainEqual(['p-value', 1.017999869046271e-4]); + }); + + it('renders classical Cox regression coefficients and summary', () => { + const tables = AlgorithmTableRegistry['cox_regression_classical']({ + dependent_var: 'Days to follow-up', + event_var: 'Death', + indep_vars: ['Intercept', 'Age'], + summary: { + n_obs: 200, + n_events: 80, + n_covariates: 1, + n_unique_event_times: 45, + coefficients: [0.1, -0.3], + hazard_ratios: [1.105, 0.741], + std_err: [0.05, 0.1], + lower_ci: [0.002, -0.496], + upper_ci: [0.198, -0.104], + hr_lower_ci: [1.002, 0.609], + hr_upper_ci: [1.219, 0.901], + z_scores: [2, -3], + pvalues: [0.045, 0.002], + df_model: 1, + df_resid: 198, + ll: -120.5, + ties: 'breslow', + n_iter: 6, + converged: true, + score_norm: 0.001, + step_norm: 0.02, + method: 'classical_cox_partial_likelihood', + }, + }); + + expect(tables.length).toBe(2); + expect(tables[0].title).toBe('Factor estimates (table)'); + expect(tables[0].columns).toEqual(['Factor', 'Hazard ratio', '95% CI', 'p-value']); + expect(tables[0].rows.length).toBe(1); + expect(tables[0].rows[0][0]).toBe('Age'); + expect(tables[0].rows[0][1]).toBe('0.741'); + expect(cellText(tables[0].rows[0][3])).toBe('0.002'); + expect(exportTable(tables[0]).rows[0][3]).toBe(0.002); + expect(tables[1].rows).toContainEqual(['Follow-up time', 'Days to follow-up']); + expect(tables[1].rows).toContainEqual(['Event indicator', 'Death']); + expect(tables[1].rows).toContainEqual(['Tied events handling', 'breslow']); + expect(tables[1].rows).toContainEqual(['Model converged', 'Yes']); + }); + + it('renders stacked Cox regression summary metrics', () => { + const tables = AlgorithmTableRegistry['cox_regression_stacked']({ + dependent_var: 'Follow-up time', + event_var: 'Diagnosis', + indep_vars: ['Intercept', 'Gender[Male]'], + summary: { + n_obs: 150, + n_events: 60, + n_stacked_rows: 900, + n_covariates: 1, + coefficients: [0.2, 0.4], + hazard_ratios: [1.221, 1.492], + std_err: [0.08, 0.12], + lower_ci: [0.04, 0.16], + upper_ci: [0.36, 0.64], + hr_lower_ci: [1.041, 1.174], + hr_upper_ci: [1.433, 1.896], + z_scores: [2.5, 3.3], + pvalues: [0.01, 0.001], + df_model: 1, + df_resid: 148, + ll: -95, + ll0: -110, + r_squared_cs: 0.12, + r_squared_mcf: 0.09, + aic: 194, + bic: 205, + time_grid_strategy: 'distinct_event_times', + n_time_bins_used: 40, + method: 'stacked_cox', + }, + }); + + expect(tables.length).toBe(2); + expect(tables[0].rows.length).toBe(1); + expect(tables[0].rows[0][0]).toBe('Gender[Male]'); + expect(tables[1].rows).toContainEqual(['Analysis rows', '900']); + expect(tables[1].rows).toContainEqual(['AIC', '194']); + }); + + it('renders LMM fixed effects and model summary', () => { + const tables = AlgorithmTableRegistry['lmm']({ + dependent_var: 'Right hippocampus', + grouping_var: 'Dataset', + indep_vars: ['Intercept', 'Age'], + coefficients: [1.2, -0.4], + std_err: [0.1, 0.05], + t_stats: [12, -8], + pvalue_label: 'P(>|t|)', + pvalues: [0.0001, 0.002], + pvalues_display: ['<0.001', '0.002'], + lower_ci: [1.0, -0.5], + upper_ci: [1.4, -0.3], + n_obs: 100, + n_groups: 3, + df_model: 2, + df_resid: 97, + sigma2: 0.8, + sigma_u2: 0.2, + ll_reml: -120, + aic: 250, + bic: 260, + converged: true, + n_iter: 8, + }); + + expect(tables.length).toBeGreaterThan(1); + expect(tables[0].title).toBe('Fixed Effects'); + expect(tables[0].columns).toContain('P(>|t|)'); + expect(tables[0].rows[0][0]).toBe('Intercept'); + expect(tables[0].rows[0][4]).toBe('<0.001'); + expect(tables[0].rows[1][4]).toBe('0.002'); + expect(tables.some(table => table.title === 'Model Summary')).toBe(true); + }); + + it('falls back to formatted LMM p-values when display values are absent', () => { + const tables = AlgorithmTableRegistry['lmm']({ + dependent_var: 'age', + grouping_var: 'dataset', + indep_vars: ['Intercept'], + coefficients: [1], + std_err: [0.1], + t_stats: [10], + pvalues: [0.045], + lower_ci: [0.8], + upper_ci: [1.2], + n_obs: 50, + n_groups: 2, + df_model: 1, + df_resid: 48, + sigma2: 1, + sigma_u2: 0.1, + ll_reml: -60, + aic: 120, + bic: 125, + converged: true, + n_iter: 5, + } as any); + + expect(tables[0].rows[0][4]).toBe('0.045'); + }); + + it('renders binary and ordinal GLMM result contracts', () => { + const binary = AlgorithmTableRegistry['glmm_binary']({ + dependent_var: 'Gender', + grouping_var: 'Dataset', + indep_vars: ['Intercept', 'Age'], + coefficients: [0.3, -0.2], + n_obs: 100, + n_groups: 3, + sigma_u2: 0.4, + converged: true, + }); + + const ordinal = AlgorithmTableRegistry['glmm_ordinal']({ + dependent_var: 'Age group', + grouping_var: 'Dataset', + indep_vars: ['Intercept', 'Gender[Male]'], + coefficients: [0.5, 0.1], + category_order: ['Under 50', '50-59', '60-69'], + cutpoints: [-1.2, 0.8], + n_obs: 90, + n_groups: 3, + sigma_u2: 0.3, + converged: true, + }); + + expect(binary.some(table => table.title === 'Fixed Effects')).toBe(true); + expect(ordinal.some(table => table.title === 'Ordinal Category Order')).toBe(true); + expect(ordinal.some(table => table.title === 'Cutpoints')).toBe(true); + }); + + it('renders chi-squared and fisher exact tables', () => { + const chiSquared = AlgorithmTableRegistry['chi_squared']({ + chi2: 3.14, + p_value: 0.04, + dof: 1, + expected: [[5.5, 4.5], [3.5, 6.5]], + x_labels: ['AD', 'Other'], + y_labels: ['Female', 'Male'], + }); + + const fisher = AlgorithmTableRegistry['fisher_exact']({ + odds_ratio: 1.48, + p_value: 0.56, + x_labels: ['no', 'yes'], + y_labels: ['no', 'yes'], + __xVar__: 'intra_arterial_urokinase', + __yVar__: 'aspiration', + __labelMap__: { + intra_arterial_urokinase: 'Intra-arterial urokinase', + aspiration: 'Aspiration', + }, + }); + + expect(chiSquared.some(table => table.title === 'Expected Frequencies')).toBe(true); + expect(chiSquared.some(table => table.title === 'Contingency Table Categories')).toBe(true); + expect(fisher.some(table => table.title === "Fisher's Exact Test Summary")).toBe(true); + + const fisherCategories = fisher.find(table => table.title === 'Contingency Table Categories'); + expect(fisherCategories?.columns).toEqual(['Role', 'Variable', 'Categories']); + expect(fisherCategories?.rows).toEqual([ + ['Factor', 'Intra-arterial urokinase', 'no, yes'], + ['Outcome', 'Aspiration', 'no, yes'], + ]); + }); + + it('renders kmeans summary, centers, profiles, and notes', () => { + const tables = AlgorithmTableRegistry['kmeans']({ + title: 'K-Means', + variables: ['a', 'b'], + k_selection: 'manual', + selected_k: 2, + initialization_method: 'k-means++', + n_init: 10, + n_obs_interval: '100-200', + center_definition: 'Centers are cluster means.', + intended_use: ['u1'], + privacy_note: 'Cluster values are binned.', + clusters: [ + { + cluster_id: '0', + label: 'Cluster 0', + size_interval: '10-20', + center: { a: 1, b: 2 }, + profile: ['p0'], + interpretation: 'i0', + quality: { compactness: null }, + }, + { + cluster_id: '1', + label: 'Cluster 1', + size_interval: '20-30', + center: { a: 3, b: 4 }, + profile: ['p1'], + interpretation: 'i1', + quality: { compactness: 'high' }, + }, + ], + elbow: null, + converged: true, + n_iter: 5, + warnings: ['w1'], + limitations: ['l1'], + }); + + expect(tables.map(table => table.title)).toEqual([ + 'K-Means Summary', + 'Cluster Centers', + 'Cluster Profiles', + 'Notes', + ]); + expect(tables[1].columns).toEqual(['Cluster', 'Size', 'a', 'b']); + expect(tables[2].columns).toEqual(['Cluster', 'Compactness', 'Profile']); + expect(tables[0].rows).toContainEqual(['Observations', '100-200']); + expect(tables[3].rows).toContainEqual(['Warning', 'w1']); + expect(tables[3].rows).toContainEqual(['Limitation', 'l1']); + }); + + it('returns no kmeans tables without clusters', () => { + expect(AlgorithmTableRegistry['kmeans']({ clusters: [] })).toEqual([]); + }); + + it('formats every table p-value APA style and marks the Tukey clip bounds', () => { + const anova = AlgorithmTableRegistry['anova_oneway']({ + anova_table: { df_explained: 1, df_residual: 98, ss_explained: 10, ss_residual: 90, ms_explained: 10, ms_residual: 0.9, f_stat: 10.9, p_value: 1.1102230246251565e-16 }, + tuckey_test: [{ groupA: 'a', groupB: 'b', meanA: 1, meanB: 2, diff: -1, se: 0.1, t_stat: -10, p_tuckey: 0.001 }], + min_max_per_group: { categories: [], min: [], max: [] }, + } as any); + + expect(cellText(anova[0].rows[0][5])).toBe('< 0.001'); + expect(cellText(anova[1].rows[0][7])).toBe('≤ 0.001'); + expect(exportTable(anova[0]).rows[0][5]).toBe(1.1102230246251565e-16); + expect(exportTable(anova[1]).rows[0][7]).toBe(0.001); + }); + + it('trims covered columns and rows, and drops a table with nothing left', () => { + const table = { title: 'T', columns: ['Source', 'df', 'Sum Sq', 'F'], rows: [['A', 1, 10, 5], ['Residual', 98, 90, ''], ['average', 0, 0, 0]] }; + + expect(trimTable(table, { title: 'T', columns: ['df', 'F'], rows: ['average'] })).toEqual({ + title: 'T', columns: ['Source', 'Sum Sq'], rows: [['A', 10], ['Residual', 90]], + }); + expect(trimTable(table, { title: 'T' })).toBeNull(); + expect(trimTable(table, { title: 'T', columns: ['df', 'Sum Sq', 'F'] })).toBeNull(); + }); }); diff --git a/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts b/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts index 21873f557..aa733ec50 100644 --- a/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts +++ b/src/app/pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry.ts @@ -1,36 +1,103 @@ import { AnovaResult, AnovaTwoWayResult, TTestResult, - LinearRegressionResult, LinearRegressionCVResult, + LinearRegressionResult, LinearRegressionCVResult, LMMResult, LogisticRegressionResult, CVLogisticRegressionResult, + CoxRegressionResult, ClassicalCoxRegressionSummary, StackedCoxRegressionSummary, NaiveBayesGaussianResult, NaiveBayesCategoricalResult, NaiveBayesCVResult, KMeansResult, PCAResult, PearsonResult, HistogramResult, DescriptiveStatsResult, - VariableStats, NominalDescriptiveStats, NumericalDescriptiveStats + VariableStats, NominalDescriptiveStats, NumericalDescriptiveStats, + QuartilesResult, BinnedMannWhitneyUTestResult, StandardizedMeanDifferenceResult } from '../../../../models/algorithm-results.model'; +import { getFeaturewiseDescribeRows } from '../../../../core/describe-result.utils'; +import { prettifyLabel } from '../../../../core/algorithm-mappers'; +import { formatInteger, formatPValue, readable, toFiniteNumber } from '../result-format'; +import { readConfusionMatrix } from '../charts/renderers/confusion-matrix-chart'; +import { readCorrelations } from '../charts/renderers/matrix-chart'; export interface TableSpec { title?: string; columns: string[]; rows: any[][]; layout?: 'compact' | 'full'; + /** The table as exported, when the screen merges values into one cell (a count and its share, + * a range): one exact value per column instead. Exports use it; the screen never does. */ + export?: { columns: string[]; rows: unknown[][] }; } -export type TableBuilder = (result: any) => TableSpec[]; +/** A cell shown rounded (`< 0.001`, `8.9%`) and exported exactly: CSV, ZIP and PDF carry `value`. */ +export interface DisplayCell { + text: string; + value: number | string | null; +} + +function displayCell(text: string, value: number | string | null): DisplayCell { + return { text, value }; +} + +/** A number shown through `format` and exported as sent; blank when it is not a finite number. */ +function exactCell(value: unknown, format: (value: number) => string): DisplayCell | '' { + const numeric = toFiniteNumber(value); + return numeric === null ? '' : displayCell(format(numeric), numeric); +} -function capitalize(s: string): string { - return s.charAt(0).toUpperCase() + s.slice(1); +function isDisplayCell(cell: unknown): cell is DisplayCell { + return !!cell && typeof cell === 'object' && 'text' in cell && 'value' in cell; } +/** What a cell shows on screen. */ +export function cellText(cell: unknown): unknown { + return isDisplayCell(cell) ? cell.text : cell; +} + +/** A table's exact values for CSV, ZIP and PDF: rounded cells give their value, merged tables their export. */ +export function exportTable(table: TableSpec): { title?: string; columns: string[]; rows: unknown[][] } { + if (table.export) return { title: table.title, ...table.export }; + return { + title: table.title, + columns: table.columns, + rows: (table.rows ?? []).map((row) => row.map((cell) => (isDisplayCell(cell) ? cell.value : cell))), + }; +} + +/** + * What a summary above a result already shows of one of its tables: the whole table, or some of + * its columns (by header) or rows (by first cell). Used on screen only; exports keep every table. + */ +export interface TableTrim { + title: string; + columns?: string[]; + rows?: string[]; +} + +/** The table without what the summary above shows; null when nothing of it is left to read. */ +export function trimTable(table: TableSpec, trim: TableTrim): TableSpec | null { + if (!trim.columns?.length && !trim.rows?.length) return null; + const dropColumns = new Set(trim.columns ?? []); + const dropRows = new Set(trim.rows ?? []); + const keep = table.columns.map((_, i) => i).filter((i) => !dropColumns.has(table.columns[i])); + const rows = table.rows + .filter((row) => !dropRows.has(String(cellText(row?.[0]) ?? ''))) + .map((row) => keep.map((i) => row[i])); + if (keep.length <= 1 || !rows.length) return null; + return { ...table, columns: keep.map((i) => table.columns[i]), rows }; +} + +type TableBuilder = (result: any) => TableSpec[]; + function formatDecimal(value: any): string { if (typeof value !== 'number' || isNaN(value)) return value ?? ''; const abs = Math.abs(value); if (abs === 0) return '0'; - // Too small or too big numbers -> scientific - if (abs < 1e-4 || abs >= 1_000_000) { + // Too small numbers -> scientific. Large ones stay whole: 1065587, not 1.066e+6 (four digits lost). + if (abs < 1e-4) { return value.toExponential(3); } + if (abs >= 1_000_000) { + return value.toFixed(0); + } const decimals = abs < 1 ? 4 : 3; let formatted = value.toFixed(decimals); @@ -44,14 +111,35 @@ function formatDecimal(value: any): string { return formatted; } -function formatTTestKey(key: string): string { +function formatAnovaMetric(value: any): string { + if (typeof value !== 'number' || isNaN(value)) return value ?? ''; + if (!Number.isFinite(value)) return String(value); + return value.toFixed(3); +} + + +function formatNullableOutlierValue(value: any): string { + if (value === null || value === undefined) return 'Unavailable'; + return formatDecimal(value); +} + +/** One p-value style across every result table: `< 0.001` below that, three decimals above — + * 0, 1.11e-16 and 3.9e-238 are all float noise at that size. Blank when missing. */ +function formatTableP(value: unknown): DisplayCell | '' { + const numeric = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN; + return Number.isFinite(numeric) ? displayCell(formatPValue(numeric), numeric) : ''; +} + +/** The row label a t-test table gives one result key. */ +export function formatTTestKey(key: string): string { const map: Record<string, string> = { mean_diff: 'Mean Difference', se_diff: 'Std. Error of Difference', se_difference: 'Std. Error of Difference', std_err_diff: 'Std. Error of Difference', - ci_upper: '95% CI Upper', - ci_lower: '95% CI Lower', + // The level is (1 − alpha) of the run, which the table does not know. + ci_upper: 'CI Upper', + ci_lower: 'CI Lower', t_stat: 'T-statistic', p: 'p-value', p_value: 'p-value', @@ -63,12 +151,13 @@ function formatTTestKey(key: string): string { if (map[key]) return map[key]; // Fallback for unknown keys - return key - .replace(/_/g, ' ') - .replace(/\b\w/g, (c) => c.toUpperCase()); + return prettifyLabel(key); } -function buildTTestRows(result: Record<string, any>): any[][] { +function buildTTestRows( + result: Record<string, any>, + valueFormatter: (value: any) => string = formatDecimal +): any[][] { const ignoredKeys = new Set([ 'title', 'labelMap', @@ -88,17 +177,336 @@ function buildTTestRows(result: Record<string, any>): any[][] { if (value !== null && typeof value === 'object') return false; return true; }) - .map(([k, v]) => [formatTTestKey(k), formatDecimal(v)]); + .map(([k, v]) => [formatTTestKey(k), k === 'p' || k === 'p_value' ? formatTableP(v) : valueFormatter(v)]); +} + +function buildMetricRows(result: Record<string, any>, labels: Record<string, string>, keys: string[]): any[][] { + return keys + .filter((key) => result?.[key] !== undefined && result?.[key] !== null) + .map((key) => [labels[key] ?? key, key === 'p_value' ? formatTableP(result[key]) : formatDecimal(result[key])]); +} + +interface ContingencyContext { + __labelMap__?: Record<string, string>; + __xVar__?: string | null; + __yVar__?: string | null; +} + +function resolveVariableDisplayLabel( + code: string | null | undefined, + labelMap?: Record<string, string> | null, +): string { + if (!code) return ''; + const trimmed = String(code).trim(); + if (!trimmed) return ''; + // A created column has no data-model label; its code still reads as words. + return labelMap?.[trimmed] ?? readable(trimmed); +} + +function buildContingencyCategoryTable( + xLabels: string[], + yLabels: string[], + ctx: ContingencyContext, +): TableSpec | null { + if (!xLabels.length && !yLabels.length) return null; + + const factorVariable = resolveVariableDisplayLabel(ctx.__xVar__, ctx.__labelMap__) || '—'; + const outcomeVariable = resolveVariableDisplayLabel(ctx.__yVar__, ctx.__labelMap__) || '—'; + + return { + title: 'Contingency Table Categories', + columns: ['Role', 'Variable', 'Categories'], + rows: [ + ['Factor', factorVariable, xLabels.join(', ')], + ['Outcome', outcomeVariable, yLabels.join(', ')], + ], + layout: 'full', + }; +} + +function buildContingencyCornerLabel(ctx: ContingencyContext): string { + const factor = resolveVariableDisplayLabel(ctx.__xVar__, ctx.__labelMap__); + const outcome = resolveVariableDisplayLabel(ctx.__yVar__, ctx.__labelMap__); + if (factor && outcome) { + return `${factor} \\ ${outcome}`; + } + return 'Factor \\ Outcome'; +} + +interface CoefficientColumnSpec { + key: string; + label: string; + displayKey?: string; + format?: 'decimal' | 'text'; +} + +function formatCoefficientCell( + result: Record<string, unknown>, + column: CoefficientColumnSpec, + index: number +): string { + const displayKey = column.displayKey; + if (displayKey) { + const displaySource = Array.isArray(result[displayKey]) ? result[displayKey] : []; + const display = displaySource[index]; + if (display !== null && display !== undefined && String(display).trim() !== '') { + return String(display); + } + } + + const source: unknown[] = Array.isArray(result[column.key]) + ? (result[column.key] as unknown[]) + : []; + const value = source[index]; + if (column.format === 'text') { + return value === null || value === undefined ? '' : String(value); + } + return formatDecimal(value); +} + +function buildCoefficientRows( + result: Record<string, unknown>, + values: CoefficientColumnSpec[] +): unknown[][] { + const variables = Array.isArray(result['indep_vars']) ? result['indep_vars'] : []; + if (!variables.length) return []; + + return variables.map((variable, index) => [ + variable, + ...values.map((column) => formatCoefficientCell(result, column, index)), + ]); +} + +function formatLinearRegressionSummaryMetric(key: string, result: LinearRegressionResult): string { + if (key === 'f_stat') { + const display = result.f_stat_display; + if (typeof display === 'string' && display.trim()) return display; + } + if (key === 'f_pvalue') { + const display = result.f_pvalue_display; + if (typeof display === 'string' && display.trim()) return display; + } + return formatDecimal(result[key as keyof LinearRegressionResult]); +} + +function isClassicalCoxSummary( + summary: ClassicalCoxRegressionSummary | StackedCoxRegressionSummary +): summary is ClassicalCoxRegressionSummary { + return typeof (summary as ClassicalCoxRegressionSummary).n_unique_event_times === 'number'; +} + +function isCoxInterceptTerm(name: string): boolean { + return String(name).trim().toLowerCase() === 'intercept'; +} + +function formatCoxHazardRatio(value: number | undefined): string { + const numeric = Number(value); + if (!Number.isFinite(numeric)) return '—'; + if (numeric >= 100) return numeric.toFixed(1); + if (numeric >= 10) return numeric.toFixed(2); + return numeric.toFixed(3); +} + +/** Tukey p comes from statsmodels' psturng, which clips to [0.001, 0.9]: the ends are bounds, not values. */ +function formatTukeyP(value: unknown): DisplayCell | '' { + const numeric = Number(value); + if (!Number.isFinite(numeric)) return ''; + if (numeric <= 0.001) return displayCell('≤ 0.001', numeric); + if (numeric >= 0.9) return displayCell('≥ 0.900', numeric); + return displayCell(numeric.toFixed(3), numeric); +} + +function formatCoxPValue(value: number | undefined): DisplayCell | string { + return formatTableP(value) || '—'; +} + +function formatCoxHazardRatioCi(lower: number | undefined, upper: number | undefined): string { + const low = Number(lower); + const high = Number(upper); + if (!Number.isFinite(low) || !Number.isFinite(high)) return '—'; + return `${formatCoxHazardRatio(low)} – ${formatCoxHazardRatio(high)}`; +} + +function buildCoxRegressionTables( + result: CoxRegressionResult, + options: { coefficientsTitle: string; variant: 'classical' | 'stacked' } +): TableSpec[] { + const summary = result?.summary; + if (!summary) return []; + + const indepVars = result.indep_vars ?? []; + const coefficients = summary.coefficients ?? []; + if (!indepVars.length || !coefficients.length) return []; + + const coefRows = indepVars + .map((variable, index) => { + if (isCoxInterceptTerm(variable)) return null; + return [ + variable, + formatCoxHazardRatio(summary.hazard_ratios?.[index]), + formatCoxHazardRatioCi(summary.hr_lower_ci?.[index], summary.hr_upper_ci?.[index]), + formatCoxPValue(summary.pvalues?.[index]), + ]; + }) + .filter((row): row is string[] => !!row); + + const tables: TableSpec[] = [{ + title: options.coefficientsTitle, + columns: [ + 'Factor', + 'Hazard ratio', + '95% CI', + 'p-value', + ], + rows: coefRows, + layout: 'full', + }]; + + const summaryRows: any[][] = [ + // Exaflow puts the follow-up time in y (dependent_var) and the event indicator in event_var. + ['Follow-up time', result.dependent_var], + ['Event indicator', result.event_var], + ['Participants', formatDecimal(summary.n_obs)], + ['Events', formatDecimal(summary.n_events)], + ['Covariates in model', formatDecimal(summary.n_covariates)], + ]; + + if (options.variant === 'classical' && isClassicalCoxSummary(summary)) { + summaryRows.push( + ['Model converged', summary.converged ? 'Yes' : 'No'], + ['Distinct event times', formatDecimal(summary.n_unique_event_times)], + ['Tied events handling', summary.ties ?? ''], + ); + } else if (options.variant === 'stacked') { + const stacked = summary as StackedCoxRegressionSummary; + summaryRows.push( + ['Analysis rows', formatDecimal(stacked.n_stacked_rows)], + ['Pseudo R² (CS)', formatDecimal(stacked.r_squared_cs)], + ['Pseudo R² (McF)', formatDecimal(stacked.r_squared_mcf)], + ['AIC', formatDecimal(stacked.aic)], + ['BIC', formatDecimal(stacked.bic)], + ); + } + + tables.push({ + title: 'Study and model summary', + columns: ['Item', 'Value'], + rows: summaryRows, + layout: 'full', + }); + + return tables; +} + +function buildMixedEffectsSummary(result: Record<string, any>, extraKeys: string[] = []): TableSpec { + const labels: Record<string, string> = { + dependent_var: 'Dependent variable', + grouping_var: 'Grouping variable', + n_obs: 'Observations', + n_groups: 'Groups', + df_model: 'DF Model', + df_resid: 'DF Residual', + sigma2: 'Residual variance', + sigma_u2: 'Random intercept variance', + ll_reml: 'REML log-likelihood', + aic: 'AIC', + bic: 'BIC', + converged: 'Converged', + n_iter: 'Iterations', + }; + const keys = [ + 'dependent_var', + 'grouping_var', + 'n_obs', + 'n_groups', + ...extraKeys, + 'sigma2', + 'sigma_u2', + 'll_reml', + 'aic', + 'bic', + 'converged', + 'n_iter', + ]; + return { + title: 'Model Summary', + columns: ['Metric', 'Value'], + rows: buildMetricRows(result, labels, keys), + layout: 'full', + }; +} + +function buildHistogramTables(result: HistogramResult): TableSpec[] { + const tables: TableSpec[] = []; + for (const item of result.histogram) { + const rows = []; + for (let i = 0; i < item.counts.length; i++) { + if (item.counts[i] === null) continue; + rows.push([item.bins[i], item.counts[i]]); + } + tables.push({ + title: `Histogram Data: ${item.var}` + (item.grouping_var ? ` (${item.grouping_enum})` : ''), + columns: ['Bin', 'Count'], + rows, + }); + } + return tables; } export const AlgorithmTableRegistry: Record<string, TableBuilder> = { kmeans: (result: KMeansResult) => { - const centers = result?.centers; - if (!Array.isArray(centers) || centers.length === 0) return []; - const dims = centers[0]?.length || 0; - const columns = [...Array(dims)].map((_, i) => ['x', 'y', 'z'][i] || `dim${i + 1}`); - const rows = centers.map((row: number[]) => row.map(v => Number(v.toFixed(3)))); - return [{ title: 'K-Means Centers', columns, rows }]; + if (!Array.isArray(result?.clusters) || result.clusters.length === 0) return []; + + const variables = result.variables ?? []; + const clusters = result.clusters; + const tables: TableSpec[] = [{ + title: 'K-Means Summary', + columns: ['Metric', 'Value'], + layout: 'compact', + rows: [ + ['K selection', result.k_selection], + ['Selected k', formatDecimal(result.selected_k)], + ['Observations', result.n_obs_interval], + ['Initialization method', result.initialization_method], + ['Initializations', formatDecimal(result.n_init)], + ['Converged', result.converged ? 'Yes' : 'No'], + ['Iterations', formatDecimal(result.n_iter)], + ], + }, { + title: 'Cluster Centers', + columns: ['Cluster', 'Size', ...variables], + rows: clusters.map(c => [c.label, c.size_interval, ...variables.map(v => formatDecimal(c.center[v]))]), + }, { + // The interpretation sentence only restates the profile and the compactness. + title: 'Cluster Profiles', + columns: ['Cluster', 'Compactness', 'Profile'], + rows: clusters.map(c => [c.label, c.quality?.compactness ?? '-', (c.profile ?? []).join('; ')]), + }]; + + const noteRows: any[][] = []; + if (result.privacy_note) { + noteRows.push(['Privacy', result.privacy_note]); + } + if (result.center_definition) { + noteRows.push(['Centers', result.center_definition]); + } + for (const warning of result.warnings ?? []) { + noteRows.push(['Warning', warning]); + } + if (result.elbow?.warning) { + noteRows.push(['Warning', result.elbow.warning]); + } + for (const limitation of result.limitations ?? []) { + noteRows.push(['Limitation', limitation]); + } + for (const use of result.intended_use ?? []) { + noteRows.push(['Intended use', use]); + } + if (noteRows.length) { + tables.push({ title: 'Notes', columns: ['Type', 'Note'], rows: noteRows }); + } + + return tables; }, linear_regression: (result: LinearRegressionResult) => { @@ -118,7 +526,7 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { formatDecimal(coefficients[idx]), formatDecimal(stdErr[idx]), formatDecimal(tStats[idx]), - formatDecimal(pValues[idx]), + formatTableP(pValues[idx]), formatDecimal(lowerCi[idx]), formatDecimal(upperCi[idx]), ]); @@ -155,7 +563,11 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { const infoRows = infoKeys .filter((key) => result[key] !== undefined && result[key] !== null) - .map((key) => [labelMap[key] || key, formatDecimal(result[key])]); + .map((key) => [labelMap[key] || key, formatLinearRegressionSummaryMetric(key, result)]); + + if (typeof result.f_stat_note === 'string' && result.f_stat_note.trim()) { + infoRows.push(['F-statistic note', result.f_stat_note.trim()]); + } return [ { @@ -174,6 +586,113 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { return []; }, + lmm: (result: LMMResult | null | undefined) => { + if (!result) return []; + + const pvalueLabel = + typeof result.pvalue_label === 'string' && result.pvalue_label.trim() + ? result.pvalue_label.trim() + : 'P(>|t|)'; + const coefficientColumns: CoefficientColumnSpec[] = [ + { key: 'coefficients', label: 'Coefficient' }, + { key: 'std_err', label: 'Std.Err.' }, + { key: 't_stats', label: 't-statistic' }, + { + key: 'pvalues', + label: pvalueLabel, + displayKey: 'pvalues_display', + format: 'text', + }, + { key: 'lower_ci', label: 'Lower 95% CI' }, + { key: 'upper_ci', label: 'Upper 95% CI' }, + ]; + const coefRows = buildCoefficientRows( + result as unknown as Record<string, unknown>, + coefficientColumns + ); + + const tables: TableSpec[] = []; + if (coefRows.length) { + tables.push({ + title: 'Fixed Effects', + columns: ['Variable', ...coefficientColumns.map(({ label }) => label)], + rows: coefRows, + layout: 'full', + }); + } + + const summary = buildMixedEffectsSummary(result, ['df_model', 'df_resid']); + if (summary.rows.length) tables.push(summary); + + return tables; + }, + + glmm_binary: (result: any) => { + if (!result) return []; + + const coefficientColumns = [{ key: 'coefficients', label: 'Coefficient' }]; + const coefRows = buildCoefficientRows(result, coefficientColumns); + const tables: TableSpec[] = []; + + if (coefRows.length) { + tables.push({ + title: 'Fixed Effects', + columns: ['Variable', ...coefficientColumns.map(({ label }) => label)], + rows: coefRows, + layout: 'full', + }); + } + + const summary = buildMixedEffectsSummary(result); + if (summary.rows.length) tables.push(summary); + + return tables; + }, + + glmm_ordinal: (result: any) => { + if (!result) return []; + + const coefficientColumns = [{ key: 'coefficients', label: 'Coefficient' }]; + const coefRows = buildCoefficientRows(result, coefficientColumns); + const tables: TableSpec[] = []; + + if (coefRows.length) { + tables.push({ + title: 'Fixed Effects', + columns: ['Variable', ...coefficientColumns.map(({ label }) => label)], + rows: coefRows, + layout: 'full', + }); + } + + if (Array.isArray(result.cutpoints) && result.cutpoints.length) { + tables.push({ + title: 'Cutpoints', + columns: ['Boundary', 'Cutpoint'], + rows: result.cutpoints.map((value: any, index: number) => [ + `Cutpoint ${index + 1}`, + formatDecimal(value), + ]), + }); + } + + if (Array.isArray(result.category_order) && result.category_order.length) { + tables.push({ + title: 'Ordinal Category Order', + columns: ['Level', 'Category'], + rows: result.category_order.map((category: any, index: number) => [ + index + 1, + category, + ]), + }); + } + + const summary = buildMixedEffectsSummary(result); + if (summary.rows.length) tables.push(summary); + + return tables; + }, + linear_regression_cv: (result: LinearRegressionCVResult) => { if (!result) return []; @@ -184,22 +703,36 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { : []; const normalizeStat = (value: any): { mean: number | null; std: number | null } => { - if (!value || typeof value !== 'object') return { mean: null, std: null }; - const mean = typeof value.mean === 'number' && !isNaN(value.mean) ? value.mean : null; - const std = typeof value.std === 'number' && !isNaN(value.std) ? value.std : null; - return { mean, std }; + if (!value) return { mean: null, std: null }; + if (Array.isArray(value)) { + return { + mean: typeof value[0] === 'number' && !isNaN(value[0]) ? value[0] : null, + std: typeof value[1] === 'number' && !isNaN(value[1]) ? value[1] : null + }; + } + if (typeof value === 'object') { + const mean = typeof value.mean === 'number' && !isNaN(value.mean) ? value.mean : null; + const std = typeof value.std === 'number' && !isNaN(value.std) ? value.std : null; + return { mean, std }; + } + return { mean: null, std: null }; }; - const candidates: Array<{ label: string; stat: { mean: number | null; std: number | null } }> = [ - { label: 'Root mean squared error', stat: normalizeStat((result as any).mean_sq_error) }, - { label: 'R-squared', stat: normalizeStat((result as any).r_squared) }, - { label: 'Mean absolute error', stat: normalizeStat((result as any).mean_abs_error) }, - { label: 'F-statistic', stat: normalizeStat((result as any).f_stat) }, + // mean_sq_error is the pre-rename name of the same RMSE value in stored experiments. + const candidates: Array<{ label: string; value: unknown }> = [ + { label: 'Root mean squared error', value: result.root_mean_sq_error ?? result.mean_sq_error }, + { label: 'R-squared', value: result.r_squared }, + { label: 'Mean absolute error', value: result.mean_abs_error }, + { label: 'F diagnostic', value: result.f_stat }, ]; + // Exaflow sends [null, null] when any fold is undefined or infinite; show that instead of hiding the metric. const statsRows = candidates - .filter(({ stat }) => stat.mean !== null || stat.std !== null) - .map(({ label, stat }) => [label, stat.mean ?? '', stat.std ?? '']); + .filter(({ value }) => value != null) + .map(({ label, value }) => { + const stat = normalizeStat(value); + return [label, stat.mean ?? 'Undefined', stat.std ?? 'Undefined']; + }); return [ { @@ -241,10 +774,10 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { tables.push({ title: 'Class Summary', columns: ['Class', 'Count', 'Prior'], - rows: rawClasses.map((cls, i) => [ + rows: rawClasses.map((_cls, i) => [ classLabels[i], formatDecimal(result.class_count?.[i]), - formatDecimal(result.class_prior?.[i]), + exactCell(result.class_prior?.[i], formatDecimal), ]), layout: 'full', }); @@ -275,60 +808,71 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { naive_bayes_gaussian_cv: (result: NaiveBayesCVResult) => { const summary = result?.classification_summary; - if (!summary) return []; - - const metrics = ['accuracy', 'precision', 'recall', 'fscore'] as const; - if (!summary.accuracy) return []; + if (!summary?.accuracy) return []; const classes = Object.keys(summary.accuracy); if (!classes.length) return []; - const folds = Object.keys(summary.accuracy[classes[0]]).filter(k => k !== 'average' && k !== 'stdev'); + const confusion = readConfusionMatrix(result.confusion_matrix); + const support = (cls: string): number | null => { + const index = confusion?.labels.indexOf(cls) ?? -1; + return confusion && index >= 0 ? confusion.matrix[index].reduce((sum, value) => sum + value, 0) : null; + }; + // A class the confusion matrix never saw (an unused enumeration) has no metrics worth a row. + const shown = confusion ? classes.filter((cls) => (support(cls) ?? 0) > 0) : classes; + // Fixed decimals, so "0.710 ± 0.007" lines up with "0.832 ± 0.011" (the ± text is shown as written). + const meanSd = (block: Record<string, number> | undefined) => + block?.['average'] === undefined ? '' : `${Number(block['average']).toFixed(3)} ± ${Number(block['stdev'] ?? 0).toFixed(3)}`; + + const tables: TableSpec[] = [{ + title: 'Per-class metrics (mean ± SD over folds)', + columns: ['Class', 'Precision', 'Recall', 'F1', 'Support'], + rows: shown.map((cls) => [ + cls, + meanSd(summary.precision?.[cls]), + meanSd(summary.recall?.[cls]), + meanSd(summary.fscore?.[cls]), + formatDecimal(support(cls) ?? ''), + ]), + }]; - const rows = [...folds, 'average', 'stdev'].map(fold => { - const row: any[] = [fold]; - for (const metric of metrics) { - for (const cls of classes) { - row.push(formatDecimal(summary[metric][cls]?.[fold])); - } - } - const nObsVal = summary.n_obs?.[fold] ?? 0; - row.push(formatDecimal(nObsVal)); - return row; + // Accuracy is one number per fold, repeated under every class key. + const accuracy = summary.accuracy[classes[0]]; + const folds = Object.keys(accuracy).filter((key) => key !== 'average' && key !== 'stdev'); + // n_obs per fold counts the rows the model trained on, not the held-out fold. + tables.push({ + title: 'Accuracy per fold', + columns: ['Fold', 'Accuracy', 'Training rows'], + layout: 'compact', + rows: [ + ...folds.map((fold) => [fold.replace(/^fold(\d+)$/, (_, n) => `Fold ${Number(n) + 1}`), formatDecimal(accuracy[fold]), formatDecimal(summary.n_obs?.[fold] ?? '')]), + ['Mean ± SD', meanSd(accuracy), ''], + ], }); - const columns = ['Fold']; - for (const metric of metrics) { - for (const cls of classes) { - const name = `${capitalize(metric)} (${cls})`; - columns.push(name); - } - } - columns.push('Number of observations'); - - const tables: TableSpec[] = [ - { - title: 'Classification Metrics per Fold', - columns, - rows, - layout: 'full' - } - ]; - - if (result.confusion_matrix) { + if (confusion) { tables.push({ title: 'Confusion Matrix (Combined)', - columns: ['Actual \\ Predicted', ...result.confusion_matrix.labels], - rows: result.confusion_matrix.data.map((row, i) => [ - result.confusion_matrix.labels[i], - ...row.map(v => formatDecimal(v)) - ]) + columns: ['Actual \\ Predicted', ...confusion.labels], + rows: confusion.matrix.map((row, i) => [confusion.labels[i], ...row.map((v) => formatDecimal(v))]), }); } return tables; }, + cox_regression_classical: (result: CoxRegressionResult) => + buildCoxRegressionTables(result, { + coefficientsTitle: 'Factor estimates (table)', + variant: 'classical', + }), + + cox_regression_stacked: (result: CoxRegressionResult) => + buildCoxRegressionTables(result, { + coefficientsTitle: 'Factor estimates (table)', + variant: 'stacked', + }), + logistic_regression: (result: LogisticRegressionResult) => { if (!result) return []; @@ -338,7 +882,7 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { formatDecimal(s.coefficients[i]), formatDecimal(s.stderr[i]), formatDecimal(s.z_scores[i]), - formatDecimal(s.pvalues[i]), + formatTableP(s.pvalues[i]), formatDecimal(s.lower_ci[i]), formatDecimal(s.upper_ci[i]), ]); @@ -375,10 +919,12 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { const s = result.summary; const n = s.n_obs.length; + const rowLabel = (name: string) => + name === 'average' ? 'Mean' : name === 'stdev' ? 'SD' : name.replace(/^fold_?(\d+)$/i, 'Fold $1'); const rows = []; for (let i = 0; i < n; i++) { rows.push([ - s.row_names[i], + rowLabel(String(s.row_names[i])), formatDecimal(s.n_obs[i]), formatDecimal(s.accuracy[i]), formatDecimal(s.precision[i]), @@ -389,7 +935,8 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { return [{ title: 'Logistic Regression CV Summary', - columns: ['Fold', 'Observations', 'Accuracy', 'Precision', 'Recall', 'F-Score'], + // n_obs per fold counts the rows the model trained on, not the held-out fold. + columns: ['Fold', 'Training rows', 'Accuracy', 'Precision', 'Recall', 'F-Score'], rows }]; }, @@ -411,66 +958,70 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { // Map class codes to labels const classLabels = rawClasses.map(c => yVar ? getEnumLabel(yVar, String(c)) : String(c)); + // Probabilities read as shares, not logs: exp(log prior) and exp(log P(category | class)). + const probability = (log: unknown): number | null => { + const value = Math.exp(Number(log)); + return Number.isFinite(value) ? value : null; + }; + const share = (log: unknown) => { + const value = probability(log); + return value === null ? '' : `${(value * 100).toFixed(1)}%`; + }; + // Class summary if (rawClasses.length > 0) { tables.push({ title: 'Class Summary', - columns: ['Class', 'Count', 'Log Prior'], - rows: rawClasses.map((cls, i) => [ + columns: ['Class', 'Count', 'Prior'], + rows: rawClasses.map((_cls, i) => [ classLabels[i], formatDecimal(result.class_count[i]), - formatDecimal(result.class_log_prior[i]), + exactCell(probability(result.class_log_prior[i]), () => share(result.class_log_prior[i])), ]), layout: 'full', }); } - // Category tables + // One table per feature: each cell is the count and the model's P(category | class). + // A category no class ever had (an unused enumeration) is left out. for (const feature of result.feature_names) { const counts = result.category_count[feature]; - const logProbs = result.category_log_prob[feature]; + const logProbs = result.category_log_prob?.[feature]; const categories = result.categories[feature]; - - const featureLabel = getLabel(feature); - if (!counts || !categories) continue; - const numCats = categories.length; - const countRows = []; - const logProbRows = []; - - for (let c = 0; c < numCats; c++) { - const catVal = String(categories[c]); - const catLabel = getEnumLabel(feature, catVal); - - const countRow = [catLabel]; - const logRow = [catLabel]; - - for (let i = 0; i < rawClasses.length; i++) { - countRow.push(formatDecimal(counts[i][c])); - if (logProbs) logRow.push(formatDecimal(logProbs[i][c])); - } - countRows.push(countRow); - logProbRows.push(logRow); - } - - const colHeaders = ['Category', ...classLabels]; + const used = categories + .map((category, c) => ({ label: getEnumLabel(feature, String(category)), c })) + .filter(({ c }) => rawClasses.some((_cls, i) => Number(counts[i]?.[c]) > 0)); + const rows = used.map(({ label, c }) => [ + label, + ...rawClasses.map((_cls, i) => { + const count = formatInteger(Number(counts[i]?.[c])); + const categoryShare = logProbs ? share(logProbs[i]?.[c]) : ''; + return categoryShare ? `${count} · ${categoryShare}` : count; + }), + ]); tables.push({ - title: `Category Counts: ${featureLabel}`, - columns: colHeaders, - rows: countRows, - layout: 'full' + title: `${getLabel(feature)} by class`, + columns: ['Category', ...classLabels], + rows, + layout: 'full', + // Exported as a count and an exact P(category | class) column per class, not "120 · 31.4%". + export: { + columns: ['Category', ...classLabels.flatMap((label) => [ + `${label} count`, + ...(logProbs ? [`${label} P(category | class)`] : []), + ])], + rows: used.map(({ label, c }) => [ + label, + ...rawClasses.flatMap((_cls, i) => [ + toFiniteNumber(counts[i]?.[c]), + ...(logProbs ? [probability(logProbs[i]?.[c])] : []), + ]), + ]), + }, }); - - if (logProbs) { - tables.push({ - title: `Category Log Probabilities: ${featureLabel}`, - columns: colHeaders, - rows: logProbRows, - layout: 'full' - }); - } } return tables; @@ -485,20 +1036,56 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { if (!result) return []; const tables: TableSpec[] = []; - // Eigenvalues - if (result.eigenvalues && result.eigenvalues.length) { + const asPercent = (ratio: number) => `${(ratio * 100).toFixed(1)}%`; + const components = Array.isArray(result.components) ? result.components : []; + if (components.length) { tables.push({ title: 'PCA Summary', - columns: ['Component', 'Eigenvalue'], - rows: result.eigenvalues.map((v, i) => [`PC${i + 1}`, formatDecimal(v)]) + columns: ['Component', 'Eigenvalue', 'Variance explained (%)', 'Cumulative (%)'], + rows: components.map((component) => [ + component.component_id, + formatDecimal(component.explained_variance), + asPercent(component.explained_variance_ratio), + asPercent(component.cumulative_explained_variance), + ]), + }); + } + + // Rows of eigenvectors are components; the table turns them so each variable reads across. + const vectors = Array.isArray(result.eigenvectors) ? result.eigenvectors : []; + const nVariables = vectors[0]?.length ?? 0; + if (nVariables) { + // Caller-supplied labels (saved experiments) win over the result's raw codes. + const labels = (result as any).variable_names; + const names: unknown[] = Array.isArray(labels) && labels.length + ? labels + : Array.isArray(result.variables) + ? result.variables + : []; + tables.push({ + title: 'Component weights', + columns: ['Variable', ...vectors.map((_, i) => `PC${i + 1}`)], + rows: Array.from({ length: nVariables }, (_, j) => [ + String(names[j] ?? `Var${j + 1}`), + ...vectors.map((row) => formatDecimal(row[j])), + ]), }); } return tables; }, - pca_with_transformation: (result: PCAResult) => { - return AlgorithmTableRegistry['pca'](result); + quartiles: (result: QuartilesResult) => { + if (!Array.isArray(result?.quantiles)) return []; + return [{ + title: 'Quartiles', + columns: ['Quantile', 'Value', 'Actual quantile'], + rows: result.quantiles.map((item) => [ + formatDecimal(item.q), + formatDecimal(item.value), + formatDecimal(item.actual_q), + ]), + }]; }, describe: (result: DescriptiveStatsResult) => { @@ -535,7 +1122,7 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { formatDecimal(d.std), formatDecimal(d.min), formatDecimal(d.q1), - formatDecimal(d.q2), + formatDecimal(d.median ?? d.q2), formatDecimal(d.q3), formatDecimal(d.max) ]); @@ -560,8 +1147,7 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { } }; - buildTables(result.variable_based, 'Variable-based'); - buildTables(result.model_based, 'Model-based'); + buildTables(getFeaturewiseDescribeRows(result), 'Featurewise'); return tables; }, @@ -572,8 +1158,8 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { title: 'ANOVA Summary', columns: ['Source', 'df', 'Sum Sq', 'Mean Sq', 'F', 'Prob>F'], rows: [ - ['Explained', formatDecimal(result.anova_table.df_explained), formatDecimal(result.anova_table.ss_explained), formatDecimal(result.anova_table.ms_explained), formatDecimal(result.anova_table.f_stat), formatDecimal(result.anova_table.p_value)], - ['Residual', formatDecimal(result.anova_table.df_residual), formatDecimal(result.anova_table.ss_residual), formatDecimal(result.anova_table.ms_residual), '', ''] + ['Explained', formatAnovaMetric(result.anova_table.df_explained), formatAnovaMetric(result.anova_table.ss_explained), formatAnovaMetric(result.anova_table.ms_explained), formatAnovaMetric(result.anova_table.f_stat), formatTableP(result.anova_table.p_value)], + ['Residual', formatAnovaMetric(result.anova_table.df_residual), formatAnovaMetric(result.anova_table.ss_residual), formatAnovaMetric(result.anova_table.ms_residual), '', ''] ] }; @@ -585,9 +1171,9 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { columns: ['Group A', 'Group B', 'Mean A', 'Mean B', 'Diff', 'SE', 't-stat', 'p-value'], rows: result.tuckey_test.map(r => [ r.groupA, r.groupB, - formatDecimal(r.meanA), formatDecimal(r.meanB), - formatDecimal(r.diff), formatDecimal(r.se), - formatDecimal(r.t_stat), formatDecimal(r.p_tuckey) + formatAnovaMetric(r.meanA), formatAnovaMetric(r.meanB), + formatAnovaMetric(r.diff), formatAnovaMetric(r.se), + formatAnovaMetric(r.t_stat), formatTukeyP(r.p_tuckey) ]) }); } @@ -598,8 +1184,8 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { const maxValues = Array.isArray(result.min_max_per_group.max) ? result.min_max_per_group.max : []; const rows = categories.map((category, index) => [ category, - formatDecimal(minValues[index]), - formatDecimal(maxValues[index]) + formatAnovaMetric(minValues[index]), + formatAnovaMetric(maxValues[index]) ]); tables.push({ title: 'Group Min/Max', @@ -617,7 +1203,7 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { formatDecimal(result.sum_sq[i]), formatDecimal(result.df[i]), formatDecimal(result.f_stat[i]), - formatDecimal(result.f_pvalue[i]) + formatTableP(result.f_pvalue[i]) ]); return [{ @@ -628,9 +1214,30 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { }, pearson_correlation: (result: PearsonResult) => { - // Guide says "Correlation Heatmap" is primary. - // We can return a small summary if needed, e.g. number of observations? const tables: TableSpec[] = []; + const matrices = readCorrelations(result); + + if (matrices) { + const { correlations, pValues, ciLower, ciUpper, cols, rows, square, cell } = matrices; + // Each pair once: in a square matrix keep the pairs below the diagonal. + const pairs = rows.flatMap((row) => cols + .filter((col) => !square || cols.indexOf(col) < cols.indexOf(row)) + .map((col) => ({ row, col, r: cell(correlations, row, col) })) + .filter((pair): pair is { row: string; col: string; r: number } => pair.r !== null)); + pairs.sort((a, b) => Math.abs(b.r) - Math.abs(a.r)); + + if (pairs.length) { + tables.push({ + title: 'Correlation pairs', + columns: ['Variable A', 'Variable B', 'r', '95% CI lower', '95% CI upper', 'p'], + rows: pairs.map(({ row, col, r }) => { + const p = cell(pValues, row, col); + return [row, col, formatDecimal(r), formatDecimal(cell(ciLower, row, col)), formatDecimal(cell(ciUpper, row, col)), formatTableP(p)]; + }), + }); + } + } + if (typeof result.n_obs === 'number') { tables.push({ title: 'Number of Observations', @@ -642,24 +1249,189 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { return tables; }, - histogram: (result: HistogramResult) => { - // Primary is chart. Table could be bin counts. + chi_squared: (result: any) => { + if (!result) return []; const tables: TableSpec[] = []; - for (const item of result.histogram) { - const rows = []; - for (let i = 0; i < item.counts.length; i++) { - if (item.counts[i] === null) continue; - rows.push([item.bins[i], item.counts[i]]); - } + const ctx: ContingencyContext = { + __labelMap__: result.__labelMap__, + __xVar__: result.__xVar__, + __yVar__: result.__yVar__, + }; + + const summaryRows = buildMetricRows(result, { + chi2: 'Chi-Squared statistic', + p_value: 'p-value', + dof: 'Degrees of freedom', + }, ['chi2', 'p_value', 'dof']); + + if (summaryRows.length) { tables.push({ - title: `Histogram Data: ${item.var}` + (item.grouping_var ? ` (${item.grouping_enum})` : ''), - columns: ['Bin', 'Count'], - rows + title: 'Chi-Squared Test Summary', + columns: ['Metric', 'Value'], + rows: summaryRows, + layout: 'compact', + }); + } + + const xLabels = Array.isArray(result.x_labels) ? result.x_labels : []; + const yLabels = Array.isArray(result.y_labels) ? result.y_labels : []; + const categoryTable = buildContingencyCategoryTable(xLabels, yLabels, ctx); + if (categoryTable) { + tables.push(categoryTable); + } + + if (Array.isArray(result.expected) && result.expected.length) { + tables.push({ + title: 'Expected Frequencies', + columns: [buildContingencyCornerLabel(ctx), ...yLabels.map(String)], + rows: result.expected.map((row: any[], index: number) => [ + xLabels[index] ?? `Row ${index + 1}`, + ...(Array.isArray(row) ? row.map(value => formatDecimal(value)) : []), + ]), + layout: 'full', + }); + } + + return tables; + }, + + fisher_exact: (result: any) => { + if (!result) return []; + const tables: TableSpec[] = []; + const ctx: ContingencyContext = { + __labelMap__: result.__labelMap__, + __xVar__: result.__xVar__, + __yVar__: result.__yVar__, + }; + + const summaryRows = buildMetricRows(result, { + odds_ratio: 'Odds ratio', + p_value: 'p-value', + }, ['odds_ratio', 'p_value']); + + if (summaryRows.length) { + tables.push({ + title: "Fisher's Exact Test Summary", + columns: ['Metric', 'Value'], + rows: summaryRows, + layout: 'compact', }); } + + const xLabels = Array.isArray(result.x_labels) ? result.x_labels : []; + const yLabels = Array.isArray(result.y_labels) ? result.y_labels : []; + const categoryTable = buildContingencyCategoryTable(xLabels, yLabels, ctx); + if (categoryTable) { + tables.push(categoryTable); + } + return tables; }, + outlier_report: (result: any) => { + const featurewise = Array.isArray(result?.featurewise) + ? result.featurewise + : Array.isArray(result?.result?.featurewise) + ? result.result.featurewise + : []; + if (!featurewise.length) return []; + + // One rule column and one kept-range column instead of strategy/tail/fold and two bounds, + // and each variable's datasets kept together. + const strategyLabels: Record<string, string> = { gaussian: 'Mean ± SD', iqr: 'IQR', mad: 'MAD', quantile: 'Quantile' }; + const tailLabels: Record<string, string> = { left: 'lower tail', right: 'upper tail', both: 'both tails' }; + const bound = (value: unknown) => (value === null || value === undefined ? '—' : formatDecimal(value)); + const share = (value: unknown) => { + const percent = toFiniteNumber(value); + return percent === null ? 'Unavailable' : `${percent.toFixed(1)}%`; + }; + const rows = featurewise.filter((row: any) => String(row?.dataset ?? '').trim().toLowerCase() !== 'all datasets'); + const variableOrder = [...new Set(rows.map((row: any) => row?.variable))]; + rows.sort((a: any, b: any) => variableOrder.indexOf(a?.variable) - variableOrder.indexOf(b?.variable)); + + const rule = (data: any) => { + const strategy = data.strategy ? strategyLabels[data.strategy] ?? data.strategy : 'Unavailable'; + const fold = data.fold === null || data.fold === undefined ? '' : ` × ${formatDecimal(data.fold)}`; + const tail = data.tail ? `, ${tailLabels[data.tail] ?? data.tail}` : ''; + return `${strategy}${fold}${tail}`; + }; + const exact = (value: unknown) => toFiniteNumber(value); + + return [{ + title: 'Outlier Report', + columns: ['Variable', 'Dataset', 'Rule', 'Kept range', 'Below', 'Above', 'Outliers', 'Share'], + rows: rows.map((row: any) => { + const data = row?.data ?? {}; + return [ + row?.variable ?? '', + row?.dataset ?? '', + rule(data), + `${bound(data.lower_bound)} to ${bound(data.upper_bound)}`, + formatNullableOutlierValue(data.lower_outlier_count), + formatNullableOutlierValue(data.upper_outlier_count), + formatNullableOutlierValue(data.total_outlier_count), + // Exaflow sends the share as a percentage (8.917 for 8.9%). + share(data.total_outlier_percentage), + ]; + }), + // Exported with both bounds and the share exact, one value per column. + export: { + columns: ['Variable', 'Dataset', 'Rule', 'Lower bound', 'Upper bound', 'Below', 'Above', 'Outliers', 'Share (%)'], + rows: rows.map((row: any) => { + const data = row?.data ?? {}; + return [ + row?.variable ?? '', + row?.dataset ?? '', + rule(data), + exact(data.lower_bound), + exact(data.upper_bound), + exact(data.lower_outlier_count), + exact(data.upper_outlier_count), + exact(data.total_outlier_count), + exact(data.total_outlier_percentage), + ]; + }), + }, + }]; + }, + + histogram: buildHistogramTables, + + binned_mann_whitney_u_test: (result: BinnedMannWhitneyUTestResult) => { + if (!result) return []; + // Exaflow reports U for group A, so U / (n1·n2) is the chance a group A value outranks a + // group B value, ties counted half: the effect size (Vargha–Delaney A) next to p. + const u = toFiniteNumber(result.u_stat); + const n1 = toFiniteNumber(result.n1); + const n2 = toFiniteNumber(result.n2); + return [{ + title: 'Binned Mann-Whitney U Test', + columns: ['Metric', 'Value'], + rows: [ + ['U statistic', formatDecimal(result.u_stat)], + ['p-value', formatTableP(result.p_value)], + ['z-score', formatDecimal(result.z_score)], + ...(u !== null && n1 && n2 ? [['P(A > B)', displayCell((u / (n1 * n2)).toFixed(2), u / (n1 * n2))]] : []), + ['Group A sample size', formatDecimal(result.n1)], + ['Group B sample size', formatDecimal(result.n2)], + ], + layout: 'compact', + }]; + }, + + standardized_mean_difference: (result: StandardizedMeanDifferenceResult) => { + if (!Array.isArray(result?.comparisons)) return []; + return [{ + title: 'Standardized Mean Difference', + columns: ['Group 1', 'Group 2', 'SMD'], + rows: result.comparisons.map(comparison => [ + String(comparison.group1), + String(comparison.group2), + formatDecimal(comparison.smd), + ]), + }]; + }, + ttest_independent: (result: TTestResult) => { return [{ title: 'Independent T-Test', @@ -672,7 +1444,7 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { return [{ title: 'Paired T-Test', columns: ['Metric', 'Value'], - rows: buildTTestRows(result as Record<string, any>) + rows: buildTTestRows(result as Record<string, any>, formatAnovaMetric) }]; }, @@ -685,7 +1457,6 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { }, // Legacy/Aliases - logistic_regression_cv_fedaverage: (result) => AlgorithmTableRegistry['logistic_regression_cv'](result), linear_svm: (result) => { if (!result) return []; @@ -715,3 +1486,11 @@ export const AlgorithmTableRegistry: Record<string, TableBuilder> = { // Default fallback default: () => [] }; + + +/** Safe key lookup for callers holding an algorithm name from the backend. */ +export function getAlgorithmTableBuilder(algorithmKey: string | null | undefined): TableBuilder | undefined { + if (!algorithmKey) return undefined; + const entry = Object.entries(AlgorithmTableRegistry).find(([key]) => key === algorithmKey); + return entry?.[1]; +} diff --git a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.css b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.css index 517d1bab5..444100e01 100644 --- a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.css +++ b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.css @@ -1,8 +1,8 @@ :host { display: block; - font-family: 'Inter', sans-serif; + font-family: var(--font-sans); font-size: 0.875rem; - color: var(--text-main, #0f172a); + color: var(--text-main); } /* CONTAINER – GRID LAYOUT */ @@ -10,166 +10,180 @@ width: 100%; display: grid; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); - gap: 1.75rem; + gap: 16px; align-items: stretch; } -/* CARDS */ +/* CARDS: flat, quiet frames. No lift and no tinted chrome. */ .ar-card { background: #ffffff; - border: 1px solid #e1e7e9; - border-radius: 10px; - box-shadow: 0 2px 6px rgba(0, 60, 60, 0.08); + border: 1px solid rgba(148, 163, 184, 0.28); + border-radius: 12px; + box-shadow: none; overflow: hidden; - transition: box-shadow 0.2s ease-in-out, transform 0.15s ease-in-out; grid-column: span 1; - animation: fadeIn 0.25s ease-in-out; } -.ar-card:hover { - box-shadow: 0 4px 10px rgba(0, 80, 80, 0.15); -} - -/* Wide tables = full width row */ .ar-card--full { grid-column: 1 / -1; } /* HEADER */ .ar-header { - background: linear-gradient(90deg, var(--primary-color, #2b33e9), var(--primary-light, #7f9ce8)); - color: white; display: flex; - justify-content: space-between; align-items: center; - padding: 8px 16px; - border-bottom: 1px solid rgba(43, 51, 233, 0.3); + justify-content: space-between; + gap: 10px; + padding: 12px 16px; + border-bottom: 1px solid rgba(148, 163, 184, 0.2); + background: #ffffff; } -/* TITLES */ .ar-title { - font-weight: 600; - font-size: 1rem; - letter-spacing: 0.3px; + min-width: 0; + margin-right: 12px; + font-size: 0.95rem; + font-weight: 700; + letter-spacing: 0; + color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; - margin-right: 12px; } -/* EXPORT BUTTON */ -.ar-export-btn { - display: flex; +.ar-csv-link { + display: inline-flex; align-items: center; - gap: 6px; - background: rgba(255, 255, 255, 0.15); - border: 1px solid rgba(255, 255, 255, 0.3); - color: white; - padding: 4px 10px; - border-radius: 6px; - font-size: 0.75rem; - font-weight: 500; + flex: 0 0 auto; + height: 30px; + padding: 0 10px; + border: 0; + border-radius: 8px; + background: transparent; + color: var(--primary-color); + font: inherit; + font-size: 0.76rem; + font-weight: 600; cursor: pointer; - transition: all 0.2s ease; - white-space: nowrap; } -.ar-export-btn:hover { - background: rgba(255, 255, 255, 0.25); - border-color: rgba(255, 255, 255, 0.5); - transform: translateY(-1px); -} - -.ar-export-btn:active { - transform: translateY(0); +.ar-csv-link:hover, +.ar-csv-link:focus-visible { + background: #eef0fd; + outline: none; } /* TABLE WRAPPER */ .ar-table-wrapper { + padding: 0 16px 12px; +} + +.ar-card--full .ar-table-wrapper { overflow-x: auto; - padding: 0.75rem 1rem 1rem 1rem; +} + +.ar-card--compact .ar-table-wrapper { + overflow-x: visible; } /* TABLE BASE STYLING */ .ar-table { width: 100%; border-collapse: collapse; + min-width: 0; + font-size: 0.85rem; +} + +.ar-card--full .ar-table { min-width: 400px; - font-size: 0.875rem; } -/* Header row */ +/* Header row: quiet, uppercase labels, no teal wash. */ .ar-table thead th { - background-color: rgba(43, 51, 233, 0.08); - color: var(--primary-color, #2b33e9); - font-weight: 600; + padding: 8px 0; + border-bottom: 1px solid rgba(148, 163, 184, 0.24); + background: transparent; + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.04em; + text-transform: uppercase; text-align: left; - padding: 10px 12px; - border-bottom: 2px solid rgba(43, 51, 233, 0.25); - text-transform: capitalize; - letter-spacing: 0.2px; } -/* Body rows */ -.ar-table tbody tr:nth-child(even) { - background-color: var(--bg-color, #f8fafc); +/* Body rows: no zebra, no hover tint, only a hairline. */ +.ar-table tbody tr { + background: transparent; } -.ar-table tbody tr:hover { - background-color: rgba(43, 51, 233, 0.06); -} - -/* Cells */ .ar-table td { - padding: 8px 12px; - border-bottom: 1px solid var(--border-color, rgba(43, 51, 233, 0.1)); - color: var(--text-main, #0f172a); - font-family: 'Roboto Mono', monospace; + padding: 9px 0; + border-bottom: 1px solid rgba(148, 163, 184, 0.14); + color: var(--text-main); + font-family: var(--font-sans); font-size: 0.85rem; - transition: background 0.2s ease; } -/* First column emphasis */ +.ar-table tbody tr:last-child td { + border-bottom: 0; +} + +/* First column is the row label, not a link. */ .ar-table td:first-child { - font-weight: 500; - color: var(--primary-color, #2b33e9); + padding-right: 16px; + font-weight: 600; + color: var(--text-main); + text-align: left; + /* break-word, not anywhere: a squeezed column breaks long words only, never "Cluster 0" mid-word. */ + overflow-wrap: break-word; } -/* Last column (numeric) */ -.ar-table td:last-child { - color: var(--text-main, #0f172a); +.ar-table td.ar-role-factor { + font-weight: 700; + color: var(--covariate-color); } -/* ANIMATION */ -@keyframes fadeIn { - from { - opacity: 0; - transform: translateY(4px); - } +.ar-table td.ar-role-outcome { + font-weight: 700; + color: var(--primary-color); +} - to { - opacity: 1; - transform: translateY(0); - } +.ar-table td:not(:first-child), +.ar-table thead th:not(:first-child) { + /* A gap before every column, so neighbours never touch ("0 11,130", "COMPACTNESSPROFILE"). */ + padding-left: 8px; + text-align: right; + font-variant-numeric: tabular-nums; } -/* RESPONSIVE */ +/* Name columns read left; declared after the right-align rule it overrides (same specificity). */ +.ar-table td.ar-cell-text, +.ar-table thead th.ar-cell-text { + text-align: left; + font-family: var(--font-sans); +} -/* Medium screens */ @media (max-width: 1024px) { .ar-container { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); } } -/* Small screens - single column */ @media (max-width: 768px) { .ar-container { grid-template-columns: minmax(0, 1fr); - gap: 1.25rem; + gap: 12px; } - .ar-table { + .ar-card--full .ar-table { min-width: 0; } -} \ No newline at end of file +} + +.ar-heading { + margin: 0 0 0.35rem; + font-size: 0.95rem; + font-weight: 700; + color: var(--text-main); + letter-spacing: 0.02em; +} diff --git a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.html b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.html index 9f9fbaad5..093d13082 100644 --- a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.html +++ b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.html @@ -1,18 +1,14 @@ -@if (tableSpec()) { +@if (tableSpec()?.length) { +@if (heading()) { +<h3 class="ar-heading">{{ heading() }}</h3> +} <div class="ar-container"> @for (table of tableSpec(); track table) { <div class="ar-card" [class.ar-card--full]="!isCompactTable(table)" [class.ar-card--compact]="isCompactTable(table)"> - <div class="ar-header"> + <div class="ar-header" data-guide="result-actions"> <div class="ar-title">{{ table.title || 'Result Table' }}</div> - <button class="ar-export-btn" (click)="exportToCSV(table)" title="Export to CSV"> - <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" - stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"> - <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" /> - <polyline points="7 10 12 15 17 10" /> - <line x1="12" x2="12" y1="15" y2="3" /> - </svg> - <span>Export CSV</span> - </button> + <button type="button" class="ar-csv-link" (click)="exportToCSV(table)" title="Download table as CSV" + [attr.aria-label]="'Download ' + (table.title || 'table') + ' as CSV'">CSV ↓</button> </div> <div class="ar-table-wrapper"> @@ -20,15 +16,20 @@ <thead> <tr> @for (col of table.columns; track col) { - <th>{{ col }}</th> + <th [class.ar-cell-text]="isTextColumn(col)">{{ col }}</th> } </tr> </thead> <tbody> @for (row of table.rows; track row) { <tr> - @for (cell of row; track cell) { - <td>{{ formatValue(cell) }}</td> + @for (cell of row; track $index; let colIndex = $index) { + <td + [class.ar-role-factor]="table.columns[colIndex] === 'Role' && cell === 'Factor'" + [class.ar-role-outcome]="table.columns[colIndex] === 'Role' && cell === 'Outcome'" + [class.ar-cell-text]="isTextColumn(table.columns[colIndex])"> + {{ formatCell(cell, table.columns[colIndex], colIndex) }} + </td> } </tr> } @@ -38,4 +39,4 @@ </div> } </div> -} \ No newline at end of file +} diff --git a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.spec.ts b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.spec.ts index 96e6d41aa..34c923d41 100644 --- a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.spec.ts +++ b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.spec.ts @@ -1,80 +1,123 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideZonelessChangeDetection } from '@angular/core'; import { AutoRendererComponent } from './auto-renderer.component'; import { AlgorithmTableRegistry } from './algorithm-table-registry'; +import { CsvExportService } from '../../../../services/csv-export.service'; describe('AutoRendererComponent', () => { - it('renders tables for known algorithms', () => { - const cmp = new AutoRendererComponent(); - cmp.algorithm = 'kmeans'; - cmp.value = { centers: [[1, 2], [3, 4]] }; + let fixture: ComponentFixture<AutoRendererComponent>; + let cmp: AutoRendererComponent; + + const kmeansResult = { + title: 'K-Means', + variables: ['a', 'b'], + k_selection: 'manual', + selected_k: 2, + initialization_method: 'k-means++', + n_init: 10, + n_obs_interval: '100-200', + center_definition: 'c', + intended_use: [], + privacy_note: 'p', + clusters: [ + { + cluster_id: '0', + label: 'Cluster 0', + size_interval: '10-20', + center: { a: 1, b: 2 }, + profile: ['p0'], + interpretation: 'i0', + quality: { compactness: null }, + }, + { + cluster_id: '1', + label: 'Cluster 1', + size_interval: '20-30', + center: { a: 3, b: 4 }, + profile: ['p1'], + interpretation: 'i1', + quality: { compactness: null }, + }, + ], + elbow: null, + converged: true, + n_iter: 5, + warnings: [], + limitations: [], + }; + + const setInputs = (inputs: Record<string, unknown>): void => { + Object.entries(inputs).forEach(([name, value]) => { + fixture.componentRef.setInput(name, value); + }); + fixture.detectChanges(); + }; - cmp.ngOnChanges({}); + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [AutoRendererComponent], + providers: [provideZonelessChangeDetection()], + }).compileComponents(); + + fixture = TestBed.createComponent(AutoRendererComponent); + cmp = fixture.componentInstance; + }); + + it('renders tables for known algorithms', () => { + setInputs({ algorithm: 'kmeans', value: kmeansResult }); const tables = cmp.tableSpec(); expect(tables).toBeTruthy(); - expect(tables?.[0].columns.length).toBe(2); + expect(tables?.[1].columns).toEqual(['Cluster', 'Size', 'a', 'b']); }); it('sets error when builder is missing', () => { - const cmp = new AutoRendererComponent(); - cmp.algorithm = 'does-not-exist'; - cmp.value = {}; - - cmp.ngOnChanges({}); + setInputs({ algorithm: 'does-not-exist', value: {} }); expect(cmp.tableSpec()).toBeNull(); expect(cmp.error()).toContain('No renderer'); }); it('caches identical inputs to avoid redundant work', () => { - const cmp = new AutoRendererComponent(); - cmp.algorithm = 'kmeans'; - cmp.value = { centers: [[1, 2]] }; - - const spy = spyOn(AlgorithmTableRegistry, 'kmeans').and.callThrough(); + const spy = vi.spyOn(AlgorithmTableRegistry, 'kmeans'); - cmp.ngOnChanges({}); - cmp.ngOnChanges({}); + setInputs({ algorithm: 'kmeans', value: kmeansResult }); + fixture.detectChanges(); + setInputs({ algorithm: 'kmeans', value: { ...kmeansResult } }); expect(spy).toHaveBeenCalledTimes(1); }); it('recomputes when mapping inputs change even if value is unchanged', () => { - const cmp = new AutoRendererComponent(); - cmp.algorithm = 'kmeans'; - cmp.value = { centers: [[1, 2]] }; + const spy = vi.spyOn(AlgorithmTableRegistry, 'kmeans'); - const spy = spyOn(AlgorithmTableRegistry, 'kmeans').and.callThrough(); - - cmp.ngOnChanges({}); - cmp.labelMap = { x1: 'X 1' }; - cmp.ngOnChanges({}); + setInputs({ algorithm: 'kmeans', value: kmeansResult }); + setInputs({ labelMap: { x1: 'X 1' } }); expect(spy).toHaveBeenCalledTimes(2); }); it('uses result title for single-table algorithms', () => { - const cmp = new AutoRendererComponent(); - cmp.algorithm = 'kmeans'; - cmp.value = { title: 'Custom K-Means Title', centers: [[1, 2], [3, 4]] }; - - cmp.ngOnChanges({}); + setInputs({ + algorithm: 'binned_mann_whitney_u_test', + value: { title: 'Custom Title', u_stat: 1, p_value: 0.5, z_score: 0.1, n1: 10, n2: 12 }, + }); const tables = cmp.tableSpec(); expect(tables?.length).toBe(1); - expect(tables?.[0].title).toBe('Custom K-Means Title'); + expect(tables?.[0].title).toBe('Custom Title'); }); it('prefixes first table title for multi-table algorithms when result title exists', () => { - const cmp = new AutoRendererComponent(); - cmp.algorithm = 'linear_regression_cv'; - cmp.value = { - title: 'Linear Regression CV Report', - n_obs: [100, 101], - mean_sq_error: { mean: 1.1, std: 0.1 }, - r_squared: { mean: 0.9, std: 0.02 }, - }; - - cmp.ngOnChanges({}); + setInputs({ + algorithm: 'linear_regression_cv', + value: { + title: 'Linear Regression CV Report', + n_obs: [100, 101], + root_mean_sq_error: [1.1, 0.1], + r_squared: [0.9, 0.02], + }, + }); const tables = cmp.tableSpec(); expect(tables?.length).toBe(2); @@ -82,16 +125,166 @@ describe('AutoRendererComponent', () => { expect(tables?.[1].title).toBe('Error metrics'); }); - it('uses fallback title when result title is missing', () => { - const cmp = new AutoRendererComponent(); - cmp.algorithm = 'kmeans'; - cmp.fallbackTitle = 'Result K-Means'; - cmp.value = { centers: [[1, 2], [3, 4]] }; + it('titles the builder\'s first table before leaving any table out', () => { + setInputs({ + algorithm: 'linear_regression_cv', + covered: [{ title: 'Training set sample sizes' }], + value: { title: 'CV Report', n_obs: [100, 101], root_mean_sq_error: [1.1, 0.1] }, + }); + + // The covered first table took the result title with it; the next keeps its own name. + expect(cmp.tableSpec()?.map((table) => table.title)).toEqual(['Error metrics']); + }); + + it('leaves out tables the page already shows above', () => { + setInputs({ + algorithm: 'linear_regression_cv', + covered: [{ title: 'Error metrics' }], + heading: 'Detailed results', + value: { n_obs: [100, 101], root_mean_sq_error: [1.1, 0.1] }, + }); + + expect(cmp.tableSpec()?.map((table) => table.title)).toEqual(['Training set sample sizes']); + expect((fixture.nativeElement as HTMLElement).querySelector('.ar-heading')?.textContent).toBe('Detailed results'); + }); + + it('renders nothing, heading included, when every table is shown above', () => { + setInputs({ + algorithm: 'binned_mann_whitney_u_test', + covered: [{ title: 'Binned Mann-Whitney U Test' }], + heading: 'Detailed results', + value: { u_stat: 1, p_value: 0.5, z_score: 0.1, n1: 10, n2: 12 }, + }); + + expect(cmp.tableSpec()).toEqual([]); + expect((fixture.nativeElement as HTMLElement).children.length).toBe(0); + }); - cmp.ngOnChanges({}); + it('uses fallback title when result title is missing', () => { + setInputs({ + algorithm: 'binned_mann_whitney_u_test', + fallbackTitle: 'Result K-Means', + value: { u_stat: 1, p_value: 0.5, z_score: 0.1, n1: 10, n2: 12 }, + }); const tables = cmp.tableSpec(); expect(tables?.length).toBe(1); - expect(tables?.[0].title).toBe('K-Means Centers'); + expect(tables?.[0].title).toBe('Binned Mann-Whitney U Test'); + }); + + it('uses full-width layout for tables with long row labels', () => { + const table = { + title: 'Fixed Effects', + columns: ['Variable', 'Coefficient'], + rows: [ + [ + 'Vessel imaging findings[stenosis 50-99% in suspected ischemic territory]', + '-1.171', + ], + ], + }; + + expect(cmp.isCompactTable(table)).toBe(false); + }); + + it('has renderers for new Exaflow algorithms', () => { + const payloads: Record<string, any> = { + lmm: { + indep_vars: ['Intercept'], + coefficients: [1], + std_err: [0.1], + t_stats: [10], + pvalues: [0.01], + lower_ci: [0.8], + upper_ci: [1.2], + }, + glmm_binary: { + indep_vars: ['Intercept'], + coefficients: [0.5], + }, + glmm_ordinal: { + indep_vars: ['Intercept'], + coefficients: [0.5], + category_order: ['Low', 'High'], + cutpoints: [0.1], + }, + chi_squared: { + chi2: 1.2, + p_value: 0.2, + dof: 1, + expected: [[1, 2]], + x_labels: ['A'], + y_labels: ['B', 'C'], + }, + fisher_exact: { + odds_ratio: 1.1, + p_value: 0.4, + x_labels: ['A', 'B'], + y_labels: ['C', 'D'], + }, + outlier_report: { + featurewise: [ + { + variable: 'age', + dataset: 'ds1', + data: { + strategy: 'iqr', + tail: 'both', + fold: 1.5, + lower_bound: null, + upper_bound: 90, + lower_outlier_count: 0, + upper_outlier_count: 1, + total_outlier_count: 1, + total_outlier_percentage: 5, + }, + }, + ], + }, + }; + + Object.entries(payloads).forEach(([algorithm, value]) => { + setInputs({ algorithm, value }); + + expect(cmp.error()).toBeNull(); + expect(cmp.tableSpec()?.length).toBeGreaterThan(0); + }); + }); + + it('groups thousands and uses a typographic minus on screen, plain numbers for CSV', () => { + expect(cmp.formatValue(1065586.73)).toBe('1\u202f065\u202f587'); + expect(cmp.formatValue('111854')).toBe('111\u202f854'); + expect(cmp.formatValue(-3.8885)).toBe('−3.889'); + expect(cmp.formatValue(0.00001234)).toBe('1.234e-5'); + expect(cmp.formatValue(1065586.73, false)).toBe('1065587'); + expect(cmp.formatValue(-3.8885, false)).toBe('-3.889'); + expect(cmp.formatValue('< 0.001')).toBe('< 0.001'); + }); + + // Exaflow sends the open side of a one-sided t-test CI as the string "Infinity" / "-Infinity". + it('shows the open side of a one-sided CI as an infinity sign on screen', () => { + expect(cmp.formatValue('Infinity')).toBe('∞'); + expect(cmp.formatValue('-Infinity')).toBe('−∞'); + }); + + it('prints row labels and name columns as sent, never digit-grouped', () => { + expect(cmp.formatCell('10001', 'Category', 0)).toBe('10001'); + expect(cmp.formatCell('2020', 'Group 2', 1)).toBe('2020'); + expect(cmp.formatCell('111854', 'Count', 1)).toBe('111\u202f854'); + expect(cmp.formatCell({ text: '< 0.001', value: 1e-16 }, 'p-value', 1)).toBe('< 0.001'); + }); + + it('exports a table\'s exact values where the screen rounds them', () => { + const download = vi.spyOn(TestBed.inject(CsvExportService), 'downloadCsv').mockImplementation(() => {}); + setInputs({ + algorithm: 'binned_mann_whitney_u_test', + value: { u_stat: 163.5, p_value: 8.436804884099734e-6, z_score: 4.45, n1: 15, n2: 80 }, + }); + + cmp.exportToCSV(cmp.tableSpec()![0]); + + const csv = download.mock.calls[0][0]; + expect(csv).toContain(`p-value,${8.436804884099734e-6}`); + expect(csv).toContain(`P(A > B),${163.5 / 1200}`); }); }); diff --git a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.ts b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.ts index a1544a98b..d26c2f4cd 100644 --- a/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.ts +++ b/src/app/pages/experiment-studio/visualisations/auto-renderer/auto-renderer.component.ts @@ -1,60 +1,96 @@ -import { Component, Input, OnChanges, SimpleChanges, signal } from '@angular/core'; -import { AlgorithmTableRegistry, TableSpec } from './algorithm-table-registry'; +import { ChangeDetectionStrategy, Component, effect, inject, input, signal } from '@angular/core'; +import { CsvExportService } from '../../../../services/csv-export.service'; +import { cellText, getAlgorithmTableBuilder, TableSpec, TableTrim, trimTable } from './algorithm-table-registry'; import { EnumMaps } from '../../../../core/algorithm-result-enum-mapper'; +import { groupThousands } from '../result-format'; +// 'Kept range' holds "a to b": left-aligned it stays clear of the right-aligned count beside it. +const TEXT_COLUMNS = new Set(['Role', 'Categories', 'Dataset', 'Rule', 'Kept range', 'Compactness', 'Profile', 'Type', 'Note']); +// Columns of names, not quantities: a group, class or category called "2020" or "10001" prints as sent. +const LABEL_COLUMNS = new Set(['Group', 'Group 1', 'Group 2', 'Group A', 'Group B', 'Class', 'Category', 'Cluster', 'Fold', 'Level']); @Component({ selector: 'app-auto-renderer', imports: [], templateUrl: './auto-renderer.component.html', - styleUrl: './auto-renderer.component.css' + styleUrl: './auto-renderer.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class AutoRendererComponent implements OnChanges { - @Input() value: any = null; - @Input() algorithm: string | null = null; - @Input() fallbackTitle: string | null = null; - @Input() labelMap: Record<string, string> | null = null; - @Input() enumMaps: EnumMaps | null = null; - @Input() yVar: string | null = null; - algorithmToRender: string = ''; +export class AutoRendererComponent { + private readonly csvExport = inject(CsvExportService); + readonly value = input<any>(null); + readonly algorithm = input<string | null>(null); + readonly fallbackTitle = input<string | null>(null); + readonly labelMap = input<Record<string, string> | null>(null); + readonly enumMaps = input<EnumMaps | null>(null); + readonly yVar = input<string | null>(null); + readonly xVar = input<string | null>(null); + /** What the page already shows above (as figures or a chart): whole tables, or some of their + * columns or rows. Left out here; exports keep every table. */ + readonly covered = input<TableTrim[]>([]); + /** Section heading drawn above the tables, only when at least one table is shown. */ + readonly heading = input<string | null>(null); tableSpec = signal<TableSpec[] | null>(null); error = signal<string | null>(null); private lastKey: string | null = null; - ngOnChanges(changes: SimpleChanges) { - if (!this.algorithm) { + constructor() { + effect(() => this.updateTableSpec()); + } + + private updateTableSpec(): void { + const algorithm = this.algorithm(); + const value = this.value(); + const labelMap = this.labelMap(); + const enumMaps = this.enumMaps(); + const yVar = this.yVar(); + const xVar = this.xVar(); + const fallbackTitle = this.fallbackTitle(); + const covered = this.covered(); + + if (!algorithm) { this.tableSpec.set(null); this.error.set(null); return; } - const builder = AlgorithmTableRegistry[this.algorithm]; + const builder = getAlgorithmTableBuilder(algorithm); if (!builder) { this.tableSpec.set(null); - this.error.set(`No renderer for algorithm ${this.algorithm}`); + this.error.set(`No renderer for algorithm ${algorithm}`); return; } const key = JSON.stringify({ - algorithm: this.algorithm, - value: this.value, - labelMap: this.labelMap, - enumMaps: this.enumMaps, - yVar: this.yVar, - fallbackTitle: this.fallbackTitle, + algorithm, + value, + labelMap, + enumMaps, + yVar, + xVar, + fallbackTitle, + covered, }); if (key === this.lastKey && this.tableSpec()) return; try { - const enrichedValue = this.value && (this.labelMap || this.enumMaps) - ? { ...this.value, __labelMap__: this.labelMap, __enumMaps__: this.enumMaps, __yVar__: this.yVar } - : this.value; - const spec = builder(enrichedValue); + const enrichedValue = value && (labelMap || enumMaps || yVar || xVar) + ? { ...value, __labelMap__: labelMap, __enumMaps__: enumMaps, __yVar__: yVar, __xVar__: xVar } + : value; + const tables = builder(enrichedValue); const explicitTitle = this.getResultTitle(enrichedValue); const resultTitle = explicitTitle ?? this.getFallbackTitle(); - this.tableSpec.set(this.applyResultTitle(spec, resultTitle, !!explicitTitle)); + // The result title goes on the first table the builder made, as in the exports, before any + // table is left out; what is covered is matched on the builder's own titles. + const titled = this.applyResultTitle(tables, resultTitle, !!explicitTitle); + const spec = titled.flatMap((table, index) => { + const trim = covered.find((entry) => entry.title === tables[index].title); + const kept = trim ? trimTable(table, trim) : table; + return kept ? [kept] : []; + }); + this.tableSpec.set(spec); this.error.set(null); this.lastKey = key; } catch (err) { @@ -78,11 +114,33 @@ export class AutoRendererComponent implements OnChanges { // few columns + not a lot of rows -> compact if (colCount === 0) return false; + const maxLabelLength = (table.rows ?? []).reduce((max, row) => { + const label = String(row?.[0] ?? ''); + return Math.max(max, label.length); + }, 0); + // Long row labels need the full grid row; compact cards cap near 360px. + if (maxLabelLength > 48) return false; + return colCount <= 3 && rowCount <= 12; } - // helper - formatValue(value: any): string { + /** Name and prose columns read left-aligned; every other column holds numbers and aligns right. */ + isTextColumn(column: string | undefined): boolean { + return !!column && (TEXT_COLUMNS.has(column) || column.startsWith('Variable')); + } + + /** One cell on screen. Row labels and name columns print as sent; every other cell as a number. */ + formatCell(cell: unknown, column: string | undefined, columnIndex: number): string { + const shown = cellText(cell); + if (columnIndex === 0 || this.isTextColumn(column) || (!!column && LABEL_COLUMNS.has(column))) { + return shown === null || shown === undefined ? '' : String(shown); + } + return this.formatValue(shown); + } + + /** On screen: thousands grouped with a thin space (111 854; 1 065 587), trailing zeros trimmed, a typographic minus; + * scientific notation only below 1e-4. With `grouping` off (CSV) numbers stay plain for spreadsheets. */ + formatValue(value: any, grouping = true): string { if (value === null || value === undefined) return ''; // If number type is string, turn to number @@ -98,150 +156,22 @@ export class AutoRendererComponent implements OnChanges { const num = maybeNum; const abs = Math.abs(num); if (abs === 0) return '0'; + if (abs < 1e-4) return num.toExponential(3); - // scientific numbers - if (abs < 1e-4 || abs >= 1_000_000) { - return num.toExponential(3); - } - - // Less decimals - const decimals = abs < 1 ? 4 : 3; - let fixed = num.toFixed(decimals); - - // Less zeros - fixed = fixed - .replace(/(\.\d*?[1-9])0+$/, '$1') - .replace(/\.0+$/, ''); - - if (fixed === '-0') fixed = '0'; - - return fixed; - } - - getOverrideTables(): TableSpec[] | null { - if (!this.algorithm) return null; - const builder = AlgorithmTableRegistry[this.algorithm]; - - if (!builder) return null; - - try { - const enrichedValue = this.value && (this.labelMap || this.enumMaps) - ? { ...this.value, __labelMap__: this.labelMap, __enumMaps__: this.enumMaps, __yVar__: this.yVar } - : this.value; - const table = builder(enrichedValue); - const explicitTitle = this.getResultTitle(enrichedValue); - const resultTitle = explicitTitle ?? this.getFallbackTitle(); - return this.applyResultTitle(table, resultTitle, !!explicitTitle); - } catch (err) { - console.warn('[AutoRenderer] Custom table builder failed', err); - return null; - } - } - - isPrimitive(val: any): boolean { - return val === null || ['string', 'number', 'boolean'].includes(typeof val); - } - - isArray(val: any): boolean { - return Array.isArray(val); - } - - isObject(val: any): boolean { - return val && typeof val === 'object' && !Array.isArray(val); - } - - isFlatObject(val: any): boolean { - if (!this.isObject(val)) return false; - return Object.values(val).every(v => this.isPrimitive(v)); - } - - isMatrix(arr: any[]): boolean { - return this.isArray(arr) && arr.length > 0 && this.isArray(arr[0]); - } - - getKeys(obj: any): string[] { - return obj ? Object.keys(obj) : []; - } - - trackByIndex(index: number): number { - return index; - } - - flattenObject(obj: any, prefix = '', res: any = {}): any { - for (const key in obj) { - const val = obj[key]; - const fullKey = prefix ? `${prefix}.${key}` : key; - - if (typeof val === 'object' && val !== null && !Array.isArray(val)) { - this.flattenObject(val, fullKey, res); - } else { - res[fullKey] = val; - } - } - return res; - } - - shouldFlatten(val: any): boolean { - return this.isObject(val) && !this.isFlatObject(val); - } - - isTabularObject(val: any): boolean { - if (!this.isObject(val)) return false; - const keys = Object.keys(val); - if (keys.length === 0) return false; - const firstLength = Array.isArray(val[keys[0]]) ? val[keys[0]].length : -1; - if (firstLength === -1) return false; - return keys.every(k => Array.isArray(val[k]) && val[k].length === firstLength); - } - - getTabularRows(obj: any): any[] { - const keys = Object.keys(obj); - const rowCount = obj[keys[0]].length; - const rows = []; - for (let i = 0; i < rowCount; i++) { - const row: any = {}; - for (const key of keys) { - row[key] = obj[key][i]; - } - rows.push(row); - } - return rows; + // Large values keep no decimals, mid-size three, small four; trailing zeros go. + const decimals = abs >= 1_000_000 ? 0 : abs < 1 ? 4 : 3; + const text = num.toLocaleString('en-US', { + maximumFractionDigits: decimals, + useGrouping: grouping, + }); + if (text === '-0') return '0'; + // Typographic minus on screen, like the key figures; CSV keeps the ASCII hyphen. + return grouping ? groupThousands(text).replace(/^-/, '−') : text; } + /** The table's exact values: rounded cells (p-values, shares) export what the engine sent. */ exportToCSV(table: TableSpec) { - if (!table || !table.columns || !table.rows) return; - - const headers = table.columns.map(c => this.escapeCSV(c)).join(','); - const csvRows = table.rows.map(row => - row.map(cell => this.escapeCSV(this.formatValue(cell))).join(',') - ); - - const csvContent = [headers, ...csvRows].join('\n'); - const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); - const url = URL.createObjectURL(blob); - const link = document.createElement('a'); - - const fileName = (table.title || this.algorithm || 'export') - .toLowerCase() - .trim() - .replace(/\s+/g, '_') - .replace(/[^\w-]/g, ''); - - link.setAttribute('href', url); - link.setAttribute('download', `${fileName}.csv`); - link.style.visibility = 'hidden'; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - URL.revokeObjectURL(url); - } - - private escapeCSV(val: any): string { - const str = String(val ?? ''); - if (str.includes(',') || str.includes('"') || str.includes('\n')) { - return `"${str.replace(/"/g, '""')}"`; - } - return str; + this.csvExport.exportTableCsv(table, this.algorithm() || 'export'); } private getResultTitle(result: any): string | null { @@ -252,8 +182,9 @@ export class AutoRendererComponent implements OnChanges { } private getFallbackTitle(): string | null { - if (typeof this.fallbackTitle !== 'string') return null; - const trimmed = this.fallbackTitle.trim(); + const fallbackTitle = this.fallbackTitle(); + if (typeof fallbackTitle !== 'string') return null; + const trimmed = fallbackTitle.trim(); return trimmed.length ? trimmed : null; } diff --git a/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.css b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.css index c71ce2556..f8ef3a1d8 100644 --- a/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.css +++ b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.css @@ -15,22 +15,7 @@ circle { } circle:hover { - stroke-width: 4; - transform: scale(1.1); -} - -circle.selected { - fill: hsl(200, 80%, 60%) !important; - stroke: black !important; - stroke-width: 2px !important; -} - -/* Visualization type container */ -.visualization-type-container { - display: flex; - align-items: center; - gap: 6px; - margin-bottom: 4px; + stroke-width: 1.5; } /* Chart container styling */ @@ -48,11 +33,11 @@ circle.selected { } .error-message { - color: var(--danger, #ef4444); + color: var(--danger); font-size: 1.2em; text-align: center; padding: 20px; - border: 1px solid var(--danger, #ef4444); + border: 1px solid var(--danger); background-color: rgba(239, 68, 68, 0.1); } @@ -67,183 +52,95 @@ circle.selected { flex-direction: column; } -#chart-canvas { - width: 100%; - height: 100%; - position: relative; - z-index: 1; - display: flex; - align-items: center; - justify-content: center; - flex: 1; -} - -#chart-canvas svg { - width: 100% !important; - height: 100% !important; - display: block; -} - -.chart-body { - position: relative; - width: 100%; - height: 100%; - flex: 1; -} - -.chart-settings { - position: absolute; - top: 12px; - right: 12px; - z-index: 20; -} - -.settings-btn { - background: #ffffff; - border: 1px solid var(--border-color); - color: var(--text-muted); - cursor: pointer; - padding: 6px; - font-size: 18px; - transition: all 0.2s ease; +.map-chrome { + position: static; display: flex; align-items: center; - justify-content: center; - border-radius: 999px; - box-shadow: 0 3px 8px rgba(15, 23, 42, 0.08); -} - -.settings-btn:hover { - background: var(--bg-color, #f1f5f9); - color: var(--primary-color); -} - -.settings-dropdown { - position: absolute; - top: calc(100% + 8px); - right: 0; - width: 200px; - background: white; - border: 1px solid var(--border-color); - border-radius: var(--radius-md); - box-shadow: var(--shadow-lg); - z-index: 30; - overflow: hidden; - animation: slideDown 0.2s ease-out; -} - -@keyframes slideDown { - from { - opacity: 0; - transform: translateY(-10px); - } - - to { - opacity: 1; - transform: translateY(0); - } + justify-content: space-between; + gap: 12px; + padding: 10px 14px; + border-top: 1px solid var(--studio-card-header-border); + background: var(--studio-card-header-bg); + pointer-events: auto; } -.dropdown-header { - padding: 10px 12px; - background: var(--bg-color, #f8fafc); - border-bottom: 1px solid var(--border-color); +.map-legend { display: flex; - justify-content: space-between; - align-items: center; - font-size: 12px; + flex-wrap: wrap; + gap: 14px; + padding: 0; + border: 0; + background: transparent; + font-size: 0.72rem; font-weight: 600; - color: var(--text-main); -} - -.close-dropdown { - background: none; - border: none; - font-size: 18px; - cursor: pointer; color: var(--text-muted); - line-height: 1; } -.close-dropdown:hover { - color: var(--danger); +.map-legend-swatch { + width: 8px; + height: 8px; + border-radius: 999px; + display: inline-block; + margin-right: 4px; + vertical-align: middle; } -.mode-selector { - display: flex; - padding: 10px; - background: var(--bg-color, #f1f5f9); - gap: 4px; +.map-legend-swatch.group { + background: var(--primary-light); } -.mode-btn { - flex: 1; - padding: 6px 4px; - font-size: 11px; - font-weight: 600; - border: 1px solid var(--border-color); - background: white; - color: var(--text-muted); - cursor: pointer; - border-radius: 6px; - transition: all 0.2s ease; - white-space: nowrap; +.map-legend-swatch.available { + background: #ffffff; + border: 1px solid rgba(15, 23, 42, 0.28); } -.mode-btn:hover { - background: var(--bg-color, #f8fafc); - color: var(--primary-color); +.map-legend-swatch.pool { + background: var(--accent-color); } -.mode-btn.active { - background: var(--primary-color); - color: white; - border-color: var(--primary-dark); +.map-legend-swatch.selected { + background: #3f6078; } -.bubble-color-controls { - padding: 12px; - display: flex; - flex-direction: column; - gap: 8px; - border-top: 1px solid var(--border-color); +.map-hint { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 600; } -.bubble-color-controls label { +#chart-canvas { + width: 100%; + height: 100%; + position: relative; + z-index: 1; display: flex; align-items: center; - justify-content: space-between; - gap: 10px; - font-size: 12px; - color: var(--text-main); + justify-content: center; + flex: 1; } -.bubble-color-controls input[type="color"] { - width: 24px; - height: 24px; - padding: 0; - border: 1px solid var(--border-color); - border-radius: 4px; - background: none; - cursor: pointer; +#chart-canvas svg { + width: 100% !important; + height: 100% !important; + display: block; } +.chart-body { + position: relative; + width: 100%; + height: 100%; + flex: 1; +} /* Label styling for D3 nodes */ .label { text-anchor: middle; - font: 10px sans-serif; + font-family: var(--font-sans); + font-size: 12px; pointer-events: none; font-weight: 500; } -.label-bg { - stroke: none; - pointer-events: none; - fill: white; - opacity: 0.9; -} - /* Dynamic colors are now handled by D3 and component state */ /* circle.node.variable { @@ -263,7 +160,8 @@ circle.node.group { fill-opacity: 0.8; } -circle.node.selected { - stroke: black; - stroke-width: 2px; -} \ No newline at end of file +/* The map square is the canvas, not the shell: the footer strip adds height below it + instead of being carved out of a square shell. */ +:host-context(.metadata-browser-shell.is-map) .chart-body { + aspect-ratio: 1 / 1; +} diff --git a/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.html b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.html index 84ea46838..03c50ba93 100644 --- a/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.html +++ b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.html @@ -1,31 +1,19 @@ <div id="chart"> <div class="chart-body"> <div id="chart-canvas" #chartCanvas></div> - <div class="chart-settings"> - <button class="settings-btn" (click)="toggleSettings()" title="Customize Colors"> - <i class="fas fa-cog"></i> - </button> - - @if (showSettings) { - <div class="settings-dropdown"> - <div class="dropdown-header"> - <span>Color Settings</span> - <button class="close-dropdown" (click)="toggleSettings()">×</button> - </div> - - <div class="mode-selector"> - <button class="mode-btn" [class.active]="colorMode === 'default'" - (click)="applyColorMode('default')">Default</button> - <button class="mode-btn" [class.active]="colorMode === 'colorBlind'" - (click)="applyColorMode('colorBlind')">Color Blind</button> - </div> - </div> - } - </div> </div> - @if (error) { + @if (error()) { <div class="error-message"> - {{ error }} + {{ error() }} </div> } -</div> \ No newline at end of file + <div class="map-chrome"> + <div class="map-legend" aria-label="Map legend"> + <span><span class="map-legend-swatch group"></span>Group</span> + <span><span class="map-legend-swatch available"></span>Available</span> + <span><span class="map-legend-swatch pool"></span>In pool</span> + <span><span class="map-legend-swatch selected"></span>Selected</span> + </div> + <span class="map-hint">Click to inspect · double-click to add</span> + </div> +</div> diff --git a/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.spec.ts b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.spec.ts new file mode 100644 index 000000000..3963db196 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.spec.ts @@ -0,0 +1,100 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { BubbleChartComponent } from './bubble-chart.component'; +import { ExperimentStudioGuideStateService } from '../../guide/experiment-studio-guide-state.service'; + +describe('BubbleChartComponent tutorial highlighting', () => { + let fixture: ComponentFixture<BubbleChartComponent>; + let component: BubbleChartComponent; + let guideState: { + activeStepId: ReturnType<typeof signal<string | null>>; + expectedTutorialCovariate: ReturnType<typeof signal<string | null>>; + matchesTutorialCovariate: (node: any, expected?: string | null) => boolean; + }; + + const setInputs = (inputs: Record<string, unknown>): void => { + Object.entries(inputs).forEach(([name, value]) => { + fixture.componentRef.setInput(name, value); + }); + fixture.detectChanges(); + }; + + beforeEach(async () => { + guideState = { + activeStepId: signal<string | null>('select-sex-variable'), + expectedTutorialCovariate: signal<string | null>('sex'), + matchesTutorialCovariate: (node: any, expected?: string | null) => { + const target = String(expected ?? '').trim().toLowerCase(); + const candidates = [node?.code, node?.label, node?.name] + .map((value) => String(value ?? '').trim().toLowerCase()) + .filter(Boolean); + + return candidates.some((value) => value === target || value.includes(target)); + }, + }; + + await TestBed.configureTestingModule({ + imports: [BubbleChartComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentStudioGuideStateService, useValue: guideState }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(BubbleChartComponent); + component = fixture.componentInstance; + setInputs({ + d3Data: { + label: 'root', + children: [ + { + label: 'Demographics', + children: [ + { code: 'sex', label: 'Sex', type: 'nominal' }, + { code: 'age_value', label: 'Age', type: 'continuous' }, + ], + }, + ], + }, + }); + }); + + it('returns the highlighted tutorial node code when the guide target is still pending', () => { + expect((component as any).getPendingTutorialHighlightCode()).toBe('sex'); + }); + + it('removes the tutorial highlight once the target is added to the pool', () => { + guideState.activeStepId.set('add-sex-covariate'); + setInputs({ selectedVariables: [{ code: 'sex', label: 'Sex', type: 'nominal' }] }); + + expect((component as any).getPendingTutorialHighlightCode()).toBeNull(); + }); + + it('keeps Age highlighted until it is added to Variables for the Age add step', () => { + guideState.activeStepId.set('add-age-variable'); + guideState.expectedTutorialCovariate.set('age'); + + expect((component as any).getPendingTutorialHighlightCode()).toBe('age_value'); + + setInputs({ selectedVariables: [{ code: 'age_value', label: 'Age', type: 'continuous' }] }); + + expect((component as any).getPendingTutorialHighlightCode()).toBeNull(); + }); + + it('always shows every legend state and the map hint', () => { + const legend = fixture.nativeElement.querySelector('.map-legend') as HTMLElement; + + expect(legend.textContent).toContain('Group'); + expect(legend.textContent).toContain('Available'); + expect(legend.textContent).toContain('In pool'); + expect(legend.textContent).toContain('Selected'); + expect(fixture.nativeElement.querySelector('.map-legend-swatch.selected')).not.toBeNull(); + + setInputs({ selectedVariables: [{ code: 'age_value', label: 'Age', type: 'continuous' }] }); + + expect(legend.textContent).toContain('In pool'); + + const hint = fixture.nativeElement.querySelector('.map-hint') as HTMLElement; + expect(hint.textContent).toContain('double-click to add'); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.ts b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.ts index 8335f8d62..2fd16dd2f 100644 --- a/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.ts +++ b/src/app/pages/experiment-studio/visualisations/bubble-chart/bubble-chart.component.ts @@ -1,32 +1,62 @@ -import { Input, OnChanges, OnInit, SimpleChanges, AfterViewInit, ViewChild, OnDestroy, NgZone } from '@angular/core'; -import { Component, EventEmitter, Output, ElementRef } from '@angular/core'; -import { FormsModule } from '@angular/forms'; -import { createZoomableCirclePacking } from './zoomable-circle-packing'; +import { + AfterViewInit, + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + ElementRef, + effect, + inject, + NgZone, + OnChanges, + OnDestroy, + OnInit, + SimpleChanges, + ViewChild, + signal, + output, + input +} from '@angular/core'; +import { observeSize } from '../../../../core/observe-resize.util'; +import { createZoomableCirclePacking, DEFAULT_BUBBLE_COLORS } from './zoomable-circle-packing'; +import { ExperimentStudioGuideStateService } from '../../guide/experiment-studio-guide-state.service'; +import { + EMPTY_UNAVAILABLE_VARIABLES, + UnavailableVariablesMap, +} from '../../../../core/variable-availability.utils'; @Component({ selector: 'app-bubble-chart', templateUrl: './bubble-chart.component.html', - styleUrls: ['./bubble-chart.component.css'], - imports: [FormsModule] + styleUrl: './bubble-chart.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class BubbleChartComponent implements OnInit, OnChanges, AfterViewInit, OnDestroy { - @Input() d3Data: any; - @Input() highlightNode: any | null = null; - @Input() selectedVariables: any[] = []; - @Input() selectedCovariates: any[] = []; - @Input() selectedFilters: any[] = []; - @Input() bubbleColors?: Partial<{ + private elementRef = inject(ElementRef); + private ngZone = inject(NgZone); + private cdr = inject(ChangeDetectorRef); + + private readonly guideState = inject(ExperimentStudioGuideStateService); + private readonly tutorialHighlightColor = '#22c55e'; + private lastTutorialZoom: string | null = null; + + readonly d3Data = input<any | null>(null); + readonly highlightNode = input<any | null>(null); + readonly selectedVariables = input<any[]>([]); + readonly selectedFilters = input<any[]>([]); + /** Variables grayed out and not selectable for the current datasets. */ + readonly unavailableVariables = input<UnavailableVariablesMap>(EMPTY_UNAVAILABLE_VARIABLES); + readonly bubbleColors = input<Partial<{ variable: string; covariate: string; filter: string; selected: string; groupStart: string; groupEnd: string; - }>; +}>>(); - @Output() selectedNodeChange = new EventEmitter<any>(); - @Output() nodeDoubleClicked = new EventEmitter<any>(); + readonly selectedNodeChange = output<any>(); + readonly nodeDoubleClicked = output<any>(); @ViewChild('chartCanvas') chartCanvas?: ElementRef<HTMLElement>; private lastHighlighted: any = null; @@ -34,38 +64,18 @@ export class BubbleChartComponent implements OnInit, OnChanges, AfterViewInit, O private viewReady = false; private refreshColorsFn!: (options?: { selectedVariables?: any[]; - selectedCovariates?: any[]; selectedFilters?: any[]; colors?: Partial<BubbleChartComponent['colors']>; }) => void; private destroyFn?: () => void; - private resizeObserver?: ResizeObserver; - private resizeRaf = 0; - private resizeDebounce: any = 0; - private lastSize = { width: 0, height: 0 }; + private stopObservingSize: (() => void) | undefined; + /** Size the drawing in the DOM was measured at; zero while its step is hidden. */ + private renderedSize = { width: 0, height: 0 }; private isAnimating = false; - error: string | null = null; // Holds the current error message - readonly DEFAULT_PALETTE = { - variable: '#2b33e9', // Brand Blue (--variable-color) - covariate: '#ccb692', // Muted Sand (--covariate-color) - filter: '#94a3b8', // Muted Slate (--filter-color) - selected: '#1b21a3', // Brand Blue darker - groupStart: '#dfefe4', // Light green for groups - groupEnd: '#2b33e9', // Brand Blue for groups - }; - - readonly COLORBLIND_PALETTE = { - variable: '#ffba08', // MIP golden yellow (from portal-frontend) - covariate: '#bba66f', // MIP tan/beige (from portal-frontend) - filter: '#483300', // MIP dark brown (from portal-frontend) - selected: '#3f6078', // MIP steel blue (from portal-frontend) - groupStart: '#c8d5f0', // Light pale blue (from portal-frontend) - groupEnd: '#3340e8', // Deep blue (from portal-frontend) - }; - - colorMode: 'default' | 'colorBlind' | 'custom' = 'default'; + readonly error = signal<string | null>(null); // Holds the current error message + readonly COLORBLIND_PALETTE = { ...DEFAULT_BUBBLE_COLORS }; colors: { variable: string; @@ -74,67 +84,22 @@ export class BubbleChartComponent implements OnInit, OnChanges, AfterViewInit, O selected: string; groupStart: string; groupEnd: string; - } = { ...this.DEFAULT_PALETTE }; - - showSettings = false; - - toggleSettings(): void { - this.showSettings = !this.showSettings; - } + } = { ...this.COLORBLIND_PALETTE }; - applyColorMode(mode: 'default' | 'colorBlind' | 'custom'): void { - this.colorMode = mode; - if (mode === 'default') { - this.colors = { ...this.DEFAULT_PALETTE }; - document.body.classList.remove('colorblind-mode'); - } else if (mode === 'colorBlind') { - this.colors = { ...this.COLORBLIND_PALETTE }; - document.body.classList.add('colorblind-mode'); - } else { - document.body.classList.remove('colorblind-mode'); - } - this.saveSettings(); - this.onColorChange(); - } - - private readonly STORAGE_KEY = 'bubble_chart_colors'; - - private saveSettings(): void { - const settings = { - mode: this.colorMode, - colors: this.colors - }; - localStorage.setItem(this.STORAGE_KEY, JSON.stringify(settings)); - } + constructor() { + effect(() => { + const code = this.getPendingTutorialHighlightCode(); - private loadSettings(): void { - const saved = localStorage.getItem(this.STORAGE_KEY); - if (saved) { - try { - const settings = JSON.parse(saved); - if (settings.mode) { - this.colorMode = settings.mode; - // Apply colorblind class if mode was saved as colorBlind - if (settings.mode === 'colorBlind') { - document.body.classList.add('colorblind-mode'); - } else { - document.body.classList.remove('colorblind-mode'); - } - } - if (settings.colors) this.colors = { ...settings.colors }; - } catch (e) { - console.error('Failed to load chart settings', e); + if (!this.refreshColorsFn) { + return; } - } - } - constructor( - private elementRef: ElementRef, - private ngZone: NgZone - ) { } + this.refreshColorsFn(this.buildRefreshOptions()); + this.zoomToTutorialTarget(code); + }); + } ngOnInit(): void { - this.loadSettings(); this.renderChart(); } @@ -142,37 +107,17 @@ export class BubbleChartComponent implements OnInit, OnChanges, AfterViewInit, O this.viewReady = true; const canvas = this.chartCanvas?.nativeElement; - // Initialize lastSize BEFORE starting the observer to prevent immediate double-render - if (canvas) { - const rect = canvas.getBoundingClientRect(); - this.lastSize = { width: Math.floor(rect.width), height: Math.floor(rect.height) }; - } - this.renderChart(); - if (canvas && typeof ResizeObserver !== 'undefined') { - this.ngZone.runOutsideAngular(() => { - this.resizeObserver = new ResizeObserver(entries => { - const entry = entries[0]; - if (!entry) return; - - const { width, height } = entry.contentRect; - const roundedW = Math.floor(width); - const roundedH = Math.floor(height); - - if (roundedW === this.lastSize.width && roundedH === this.lastSize.height) return; - if (this.isAnimating) return; - if (roundedW === 0 || roundedH === 0) return; - - this.lastSize = { width: roundedW, height: roundedH }; - - // Debounce: wait until resizing stops before re-rendering - clearTimeout(this.resizeDebounce); - this.resizeDebounce = setTimeout(() => { - this.ngZone.run(() => this.renderChart()); - }, 150); - }); - this.resizeObserver.observe(canvas); + if (canvas) { + this.stopObservingSize = observeSize(canvas, this.ngZone, 150, (width, height) => { + // Zero size is the step being hidden - never a size worth drawing for. + if (width === 0 || height === 0 || this.isAnimating) return; + // Compared against the last actual draw, so a chart built while the step + // was hidden is redrawn when the step comes back. + if (width === this.renderedSize.width && height === this.renderedSize.height) return; + + this.renderChart(); }); } } @@ -188,32 +133,24 @@ export class BubbleChartComponent implements OnInit, OnChanges, AfterViewInit, O } if (changes['highlightNode']?.currentValue && this.zoomToNodeFn) { - if (!this.lastHighlighted || this.lastHighlighted.code !== this.highlightNode.code) { - this.zoomToNodeFn(this.highlightNode); - this.lastHighlighted = this.highlightNode; + const highlightNode = this.highlightNode(); + if (!this.lastHighlighted || this.lastHighlighted.code !== highlightNode.code) { + this.zoomToNodeFn(highlightNode); + this.lastHighlighted = highlightNode; } } if ( - (changes['selectedVariables'] || - changes['selectedCovariates'] || - changes['selectedFilters']) && + (changes['selectedVariables'] || changes['selectedFilters'] || changes['unavailableVariables']) && this.refreshColorsFn ) { - this.refreshColorsFn({ - selectedVariables: this.selectedVariables, - selectedCovariates: this.selectedCovariates, - selectedFilters: this.selectedFilters, - colors: this.colors, - }); + this.refreshColorsFn(this.buildRefreshOptions()); } } ngOnDestroy(): void { this.destroyFn?.(); - this.resizeObserver?.disconnect(); - if (this.resizeRaf) cancelAnimationFrame(this.resizeRaf); - clearTimeout(this.resizeDebounce); + this.stopObservingSize?.(); } renderChart(): void { @@ -222,25 +159,27 @@ export class BubbleChartComponent implements OnInit, OnChanges, AfterViewInit, O ?? this.elementRef.nativeElement.querySelector('#chart-canvas'); if (!container) return; - if (!this.d3Data) { - this.error = 'No data available for visualization.'; + if (!this.d3Data()) { + this.error.set('No data available for visualization.'); + this.cdr.markForCheck(); return; } - this.error = null; + this.error.set(null); + this.cdr.markForCheck(); // Clean up previous chart if exists (e.g. tooltip) this.destroyFn?.(); + const rect = container.getBoundingClientRect(); + this.renderedSize = { width: Math.floor(rect.width), height: Math.floor(rect.height) }; + const { zoomToNode, refreshColors, destroy } = createZoomableCirclePacking( - this.d3Data, + this.d3Data(), container, node => this.selectedNodeChange.emit(node), node => this.nodeDoubleClicked.emit(node), { - selectedVariables: this.selectedVariables, - selectedCovariates: this.selectedCovariates, - selectedFilters: this.selectedFilters, - colors: this.colors, + ...this.buildRefreshOptions(), onAnimationStart: () => this.isAnimating = true, onAnimationEnd: () => this.isAnimating = false, } @@ -250,62 +189,99 @@ export class BubbleChartComponent implements OnInit, OnChanges, AfterViewInit, O this.destroyFn = destroy; + this.lastTutorialZoom = null; + this.zoomToTutorialTarget(this.getPendingTutorialHighlightCode()); + // apply pending highlight after chart is created - if (this.highlightNode?.code) { - this.zoomToNodeFn(this.highlightNode); - this.lastHighlighted = this.highlightNode; + const highlightNode = this.highlightNode(); + if (highlightNode?.code) { + this.zoomToNodeFn(highlightNode); + this.lastHighlighted = highlightNode; } } - public updateSelectionColors(): void { - if (!this.zoomToNodeFn) return; - this.renderChart(); // re-render to update fills + private buildRefreshOptions(): { + selectedVariables: any[]; + selectedFilters: any[]; + colors: { + variable: string; + covariate: string; + filter: string; + selected: string; + groupStart: string; + groupEnd: string; + }; + tutorialHighlightCode: string | null; + tutorialHighlightColor: string; + unavailableVariables: UnavailableVariablesMap; + } { + return { + selectedVariables: this.selectedVariables(), + selectedFilters: this.selectedFilters(), + colors: this.colors, + tutorialHighlightCode: this.getPendingTutorialHighlightCode(), + tutorialHighlightColor: this.tutorialHighlightColor, + unavailableVariables: this.unavailableVariables(), + }; } - onNodeClick(node: any): void { - this.selectedNodeChange.emit(node); + private zoomToTutorialTarget(code: string | null): void { + if (!code) { + this.lastTutorialZoom = null; + return; + } + if (!this.zoomToNodeFn || code === this.lastTutorialZoom) { + return; + } + this.lastTutorialZoom = code; + this.zoomToNodeFn(code); } - public zoomToNode(variable: any): void { - if (this.zoomToNodeFn) { - this.zoomToNodeFn(variable); - } else { - console.warn('zoomToNodeFn not ready yet, retrying...'); - setTimeout(() => { - if (this.zoomToNodeFn) { - this.zoomToNodeFn(variable); - } - }, 15); + private getPendingTutorialHighlightCode(): string | null { + const expected = this.guideState.expectedTutorialCovariate(); + if (!expected || this.isTutorialStepSatisfied(expected)) { + return null; } + + return this.findTutorialHighlightCode(this.d3Data(), expected); } - public refreshColors(newOptions?: { - selectedVariables?: any[]; - selectedCovariates?: any[]; - selectedFilters?: any[]; - }): void { - if (this.refreshColorsFn) { - this.refreshColorsFn(newOptions ?? { - selectedVariables: this.selectedVariables, - selectedCovariates: this.selectedCovariates, - selectedFilters: this.selectedFilters, - colors: this.colors, - }); - } else { - console.warn('refreshColorsFn not ready yet.'); + private isTutorialStepSatisfied(expected: string): boolean { + switch (this.guideState.activeStepId()) { + case 'select-sex-variable': + case 'select-age-variable': + return this.guideState.matchesTutorialCovariate(this.highlightNode(), expected); + case 'add-sex-covariate': + return this.selectedVariables().some((node) => this.guideState.matchesTutorialCovariate(node, expected)); + case 'add-age-variable': + return this.selectedVariables().some((node) => this.guideState.matchesTutorialCovariate(node, expected)); + default: + return false; } } - onColorChange(): void { - if (this.refreshColorsFn) { - this.refreshColorsFn({ - selectedVariables: this.selectedVariables, - selectedCovariates: this.selectedCovariates, - selectedFilters: this.selectedFilters, - colors: this.colors, - }); + private findTutorialHighlightCode(node: any, expected: string): string | null { + if (!node) { + return null; + } + + if (!node.children?.length && this.guideState.matchesTutorialCovariate(node, expected)) { + return this.getNodeCode(node); + } + + for (const child of node.children ?? []) { + const match = this.findTutorialHighlightCode(child, expected); + if (match) { + return match; + } } - this.saveSettings(); + + return null; + } + + private getNodeCode(node: any): string | null { + const raw = node?.code ?? node?.uniqueId ?? node?.id ?? node?.label ?? null; + return raw == null ? null : String(raw); } } diff --git a/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.spec.ts b/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.spec.ts new file mode 100644 index 000000000..61b2a5454 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.spec.ts @@ -0,0 +1,143 @@ +import { createZoomableCirclePacking } from './zoomable-circle-packing'; + +describe('createZoomableCirclePacking', () => { + let container: HTMLDivElement; + let chart: ReturnType<typeof createZoomableCirclePacking>; + + beforeEach(() => { + container = document.createElement('div'); + vi.spyOn(container, 'getBoundingClientRect').mockReturnValue({ + width: 420, + height: 420, + top: 0, + right: 420, + bottom: 420, + left: 0, + x: 0, + y: 0, + toJSON: () => ({}), + } as DOMRect); + + chart = createZoomableCirclePacking( + { + label: 'root', + children: [ + { + label: 'Demographics', + children: [ + { code: 'sex', label: 'Sex', type: 'nominal', value: 1 }, + { code: 'age_value', label: 'Age', type: 'continuous', value: 1 }, + ], + value: 2, + }, + ], + value: 2, + }, + container, + () => undefined, + () => undefined, + { + tutorialHighlightCode: 'sex', + tutorialHighlightColor: '#22c55e', + } + ); + }); + + afterEach(() => { + chart.destroy?.(); + container.remove(); + }); + + it('exposes the chart as one labelled image, not a wall of unnamed circles', () => { + const svg = container.querySelector('svg'); + expect(svg?.getAttribute('role')).toBe('img'); + expect(svg?.getAttribute('aria-label')).toContain('1 group and 2 variables'); + }); + + it('clears the previous tutorial highlight when the next step has no resolved highlight code yet', () => new Promise<void>((done) => { + expect(container.querySelector('circle[fill="#22c55e"]')).not.toBeNull(); + + chart.refreshColors({ tutorialHighlightCode: null }); + + window.setTimeout(() => { + expect(container.querySelector('circle[fill="#22c55e"]')).toBeNull(); + done(); + }, 260); + })); +}); + +describe('createZoomableCirclePacking unavailable variables', () => { + let container: HTMLDivElement; + let chart: ReturnType<typeof createZoomableCirclePacking>; + const onClick = vi.fn(); + const onDoubleClick = vi.fn(); + + const circleFor = (code: string): SVGCircleElement => + Array.from(container.querySelectorAll<SVGCircleElement>('circle.leaf')) + .find((circle) => (circle as any).__data__?.data?.code === code)!; + + beforeEach(() => { + onClick.mockReset(); + onDoubleClick.mockReset(); + container = document.createElement('div'); + vi.spyOn(container, 'getBoundingClientRect').mockReturnValue({ + width: 420, height: 420, top: 0, right: 420, bottom: 420, left: 0, x: 0, y: 0, toJSON: () => ({}), + } as DOMRect); + chart = createZoomableCirclePacking( + { + label: 'root', + children: [ + { + label: 'Demographics', + children: [ + { code: 'sex', label: 'Sex', type: 'nominal', value: 1 }, + { code: 'age_value', label: 'Age', type: 'continuous', value: 1 }, + ], + value: 2, + }, + ], + value: 2, + }, + container, + onClick, + onDoubleClick, + { unavailableVariables: new Map([['sex', ['Dataset A', 'Dataset B']]]) } + ); + }); + + afterEach(() => { + chart.destroy?.(); + container.remove(); + }); + + it('grays an unavailable leaf, ignores its clicks and names the datasets in the tooltip', () => { + const sex = circleFor('sex'); + expect(sex.getAttribute('fill')).toBe('#cbd5e1'); + expect(sex.getAttribute('aria-disabled')).toBe('true'); + expect(sex.style.cursor).toBe('not-allowed'); + expect(circleFor('age_value').getAttribute('aria-disabled')).toBeNull(); + + sex.dispatchEvent(new MouseEvent('click', { bubbles: true })); + sex.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })); + expect(onClick).not.toHaveBeenCalled(); + expect(onDoubleClick).not.toHaveBeenCalled(); + + sex.dispatchEvent(new MouseEvent('mouseover', { bubbles: true })); + expect(document.querySelector('body > .tooltip')?.textContent).toContain('Not available in: Dataset A, Dataset B'); + + circleFor('age_value').dispatchEvent(new MouseEvent('click', { bubbles: true })); + expect(onClick).toHaveBeenCalledTimes(1); + }); + + it('restores a leaf when it becomes available again', () => new Promise<void>((done) => { + chart.refreshColors({ unavailableVariables: new Map() }); + window.setTimeout(() => { + const sex = circleFor('sex'); + expect(sex.getAttribute('fill')).not.toBe('#cbd5e1'); + expect(sex.getAttribute('aria-disabled')).toBeNull(); + sex.dispatchEvent(new MouseEvent('click', { bubbles: true })); + expect(onClick).toHaveBeenCalledTimes(1); + done(); + }, 260); + })); +}); diff --git a/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts b/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts index 9d2a341c7..a9eb199ab 100644 --- a/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts +++ b/src/app/pages/experiment-studio/visualisations/bubble-chart/zoomable-circle-packing.ts @@ -1,11 +1,23 @@ import * as d3 from 'd3'; +import { + EMPTY_UNAVAILABLE_VARIABLES, + UnavailableVariablesMap, + unavailableDatasetsFor, + unavailableVariableTooltip, +} from '../../../../core/variable-availability.utils'; + +/** Unavailable leaves: grayed out and inert, see variable-availability.utils. */ +const UNAVAILABLE_FILL = '#cbd5e1'; +const UNAVAILABLE_FILL_OPACITY = 0.6; +const UNAVAILABLE_STROKE_DASH = '3 2'; +const UNAVAILABLE_LABEL_FILL = '#94a3b8'; // helpers // Splits labels // Creates label + background rect -function createLabelGroup(group: d3.Selection<SVGGElement, any, any, any>, d: any) { +function createLabelGroup(group: d3.Selection<SVGGElement, any, any, any>, d: any, unavailable = false) { group.selectAll('*').remove(); const label = d.data.label || ''; @@ -28,6 +40,7 @@ function createLabelGroup(group: d3.Selection<SVGGElement, any, any, any>, d: an if (currentLine.length > 0) lines.push(currentLine.join(' ')); const fontSize = d.children ? 15 : 10; + const hangFromTop = !!d.children; const textNode = group .append('text') @@ -35,7 +48,7 @@ function createLabelGroup(group: d3.Selection<SVGGElement, any, any, any>, d: an .attr('text-anchor', 'middle') .style('font-size', `${fontSize}px`) .style('font-weight', '600') - .style('fill', '#0f172a') + .style('fill', unavailable ? UNAVAILABLE_LABEL_FILL : '#0f172a') .style('paint-order', 'stroke') .style('stroke', 'rgba(255,255,255,0.9)') .style('stroke-width', 2) @@ -43,9 +56,12 @@ function createLabelGroup(group: d3.Selection<SVGGElement, any, any, any>, d: an // Add tspans lines.forEach((lineStr, i) => { + const firstDy = hangFromTop + ? '1em' + : (lines.length === 1 ? '0.35em' : `-${(lines.length - 1) * 0.6}em`); textNode.append('tspan') .attr('x', 0) - .attr('dy', i === 0 ? (lines.length === 1 ? '0.35em' : `-${(lines.length - 1) * 0.6}em`) : '1.2em') + .attr('dy', i === 0 ? firstDy : '1.2em') .text(lineStr); }); @@ -66,6 +82,19 @@ const toCodeSet = (arr: any[] | undefined | null): Set<string> => .filter((v): v is string => !!v) ); +/** Text alternative for the packed chart: the shape of the data, not the 294 circles. */ +const bubbleChartLabel = (root: d3.HierarchyNode<any>): string => { + const nodes = root.descendants().slice(1); + const groups = nodes.filter((d) => d.children).length; + const leaves = nodes.length - groups; + const groupWord = groups === 1 ? 'group' : 'groups'; + const leafWord = leaves === 1 ? 'variable' : 'variables'; + return ( + `Variable hierarchy bubble chart, ${groups} ${groupWord} and ${leaves} ${leafWord}. ` + + `Double-click a bubble to add it to the pool, or use the search field to find a variable by name.` + ); +}; + type BubbleColorConfig = { variable: string; covariate: string; @@ -75,25 +104,27 @@ type BubbleColorConfig = { groupEnd: string; }; -const defaultColors: BubbleColorConfig = { - variable: '#2b33e9', // Brand Blue (matches --variable-color) - covariate: '#ccb692', // Muted Sand (matches --covariate-color) - filter: '#94a3b8', // Muted Slate (matches --filter-color) - selected: '#1b21a3', // Brand Blue darker - groupStart: '#dfefe4', // Light green for groups - groupEnd: '#2b33e9', // Brand Blue for groups +export const DEFAULT_BUBBLE_COLORS: BubbleColorConfig = { + variable: '#ffba08', // MIP golden yellow + covariate: '#bba66f', // MIP tan/beige + filter: '#483300', // MIP dark brown + selected: '#3f6078', // MIP steel blue + groupStart: '#c8d5f0', // Light pale blue + groupEnd: '#3340e8', // Deep blue }; // Calculate leaf color const colorForLeaf = ( d: any, - sets: { vars: Set<string>; covs: Set<string>; filters: Set<string> }, - colors: BubbleColorConfig + sets: { vars: Set<string>; filters: Set<string> }, + colors: BubbleColorConfig, + tutorialHighlightCode: string | null, + tutorialHighlightColor: string ): string => { const code = codeOf(d.data); if (!code) return 'white'; + if (tutorialHighlightCode && code === tutorialHighlightCode) return tutorialHighlightColor; if (sets.vars.has(code)) return colors.variable; // variable - if (sets.covs.has(code)) return colors.covariate; // covariate if (sets.filters.has(code)) return colors.filter; // filter return 'white'; }; @@ -107,22 +138,42 @@ export function createZoomableCirclePacking( onNodeDoubleClick: (node: any) => void, options?: { selectedVariables?: any[]; - selectedCovariates?: any[]; selectedFilters?: any[]; colors?: Partial<BubbleColorConfig>; + tutorialHighlightCode?: string | null; + tutorialHighlightColor?: string; + unavailableVariables?: UnavailableVariablesMap; onAnimationStart?: () => void; onAnimationEnd?: () => void; + onFocusChange?: (path: Array<{ code: string; label: string }>) => void; } -): { zoomToNode: (d: any) => void; refreshColors: (opts?: any) => void; destroy?: () => void } { +): { + zoomToNode: (d: any) => void; + refreshColors: (opts?: any) => void; + resetZoom: () => void; + destroy?: () => void; +} { // local snapshots, decouple references of the experiment studio service signals let sets = { vars: toCodeSet(options?.selectedVariables), - covs: toCodeSet(options?.selectedCovariates), filters: toCodeSet(options?.selectedFilters), }; - let colors: BubbleColorConfig = { ...defaultColors, ...(options?.colors ?? {}) }; + let colors: BubbleColorConfig = { ...DEFAULT_BUBBLE_COLORS, ...(options?.colors ?? {}) }; + let tutorialHighlightCode = options?.tutorialHighlightCode ?? null; + let tutorialHighlightColor = options?.tutorialHighlightColor ?? '#ffba08'; + let unavailableVariables: UnavailableVariablesMap = options?.unavailableVariables ?? EMPTY_UNAVAILABLE_VARIABLES; + // unavailableDatasetsFor already skips groups: d.data.children is what d3 builds d.children from. + const unavailableIn = (d: any): readonly string[] => unavailableDatasetsFor(unavailableVariables, d?.data); + const isUnavailable = (d: any): boolean => unavailableIn(d).length > 0; + const leafFillOpacity = (d: any): number => + d.children ? 0.6 : (isUnavailable(d) ? UNAVAILABLE_FILL_OPACITY : 1); + const applyAvailability = (selection: d3.Selection<any, any, any, any>): void => { + selection + .style('cursor', (d: any) => (isUnavailable(d) ? 'not-allowed' : 'pointer')) + .attr('aria-disabled', (d: any) => (isUnavailable(d) ? 'true' : null)); + }; const bounds = container.getBoundingClientRect(); const width = Math.floor(bounds.width || 0) || 700; @@ -140,7 +191,7 @@ export function createZoomableCirclePacking( if (!container) { console.error('No container provided'); - return { zoomToNode: () => { }, refreshColors: () => { } }; + return { zoomToNode: () => { }, refreshColors: () => { }, resetZoom: () => { } }; } container.innerHTML = ''; @@ -160,20 +211,28 @@ export function createZoomableCirclePacking( } } + function appendTooltipRow(name: string, value: string): void { + const row = tooltip.append('div').style('margin-top', '4px'); + row.append('strong').text(name); + row.append('span').text(' ' + value); + } + function showTooltip(event: MouseEvent, d: any) { const label = d.data.label || '(no label)'; const descriptionRaw = d.data.description || ''; const description = decodeUnicode(descriptionRaw.trim()); const type = d.data.type || ''; - let html = `<div><strong>${label}</strong></div>`; - if (type) - html += `<div style="margin-top:4px;"><strong>Type:</strong> ${type}</div>`; - if (description) - html += `<div style="margin-top:4px;"><strong>Description:</strong> ${description}</div>`; + tooltip.selectAll('*').remove(); + tooltip.append('div').append('strong').text(label); + if (type) appendTooltipRow('Type:', type); + if (description) appendTooltipRow('Description:', description); + const missingIn = unavailableIn(d); + if (missingIn.length) { + tooltip.append('div').style('margin-top', '4px').text(unavailableVariableTooltip(missingIn)); + } tooltip - .html(html) .style('left', `${event.clientX + 10}px`) .style('top', `${event.clientY + 10}px`) .transition() @@ -192,10 +251,8 @@ export function createZoomableCirclePacking( } // Create a perfectly square pack layout - // Create a perfectly square pack layout based on the minimum dimension const packSize = size; const packHeight = packSize; - const topMargin = 0; // Handled by zoom radius buffer now const root = d3.pack<any>().size([packSize, packHeight]).padding(3)( d3.hierarchy<any>(data, (d: any) => d.children) @@ -205,13 +262,20 @@ export function createZoomableCirclePacking( let focus = root; // Use a 2.05x radius to reduce padding - let view: [number, number, number] = [focus.x, focus.y, focus.r * 2]; + let view: [number, number, number] = [focus.x, focus.y, focus.r * 2.05]; let selectedDataNode: d3.HierarchyNode<any> | null = null; const svg = d3 .create('svg') .attr('viewBox', `0 0 ${width} ${height}`) .attr('preserveAspectRatio', 'xMidYMid meet') + // The 294 circles carry no semantics of their own, so expose the chart as one + // labelled image and let the search field stay the keyboard path to a variable. + .attr('role', 'img') + .attr( + 'aria-label', + bubbleChartLabel(root) + ) .attr( 'style', `width: 100%; height: 100%; display: block; margin: 0; @@ -231,19 +295,44 @@ export function createZoomableCirclePacking( .attr('stdDeviation', 2.5) .attr('flood-color', 'rgba(0,0,0,0.35)'); + const fillForNode = (d: any): string => { + if (d.children) { + return groupColor(d.depth); + } + + if (isUnavailable(d)) { + return UNAVAILABLE_FILL; + } + + if (tutorialHighlightCode) { + const code = codeOf(d.data); + if (code && code === tutorialHighlightCode) { + return tutorialHighlightColor; + } + } + + if (d === selectedDataNode) { + return colors.selected; + } + + return colorForLeaf(d, sets, colors, tutorialHighlightCode, tutorialHighlightColor); + }; + // Nodes const node = svg.append('g') .selectAll('circle') .data(root.descendants().slice(1)) .join('circle') .attr('class', (d: any) => (d.children ? 'group' : 'leaf')) - .attr('fill', (d: any) => (d.children ? groupColor(d.depth) : colorForLeaf(d, sets, colors))) - .attr('fill-opacity', (d: any) => (d.children ? 0.6 : 1)) + .attr('fill', (d: any) => fillForNode(d)) + .attr('fill-opacity', (d: any) => leafFillOpacity(d)) .attr('stroke', (d: any) => (d.children ? 'rgba(0,0,0,0.08)' : 'rgba(0,0,0,0.12)')) .attr('stroke-width', (d: any) => (d.children ? 1 : 0.6)) - .style('cursor', 'pointer') + .call(applyAvailability) + .attr('stroke-dasharray', (d: any) => (isUnavailable(d) ? UNAVAILABLE_STROKE_DASH : null)) .on('click', (event: MouseEvent, d: any) => { event.stopPropagation(); + if (isUnavailable(d)) return; if (!d.children) { selectedDataNode = d; onNodeClick(d.data); @@ -254,29 +343,21 @@ export function createZoomableCirclePacking( }) .on('dblclick', (event: MouseEvent, d: any) => { event.stopPropagation(); - if (!d.children) { + if (!d.children && !isUnavailable(d)) { onNodeDoubleClick(d.data); } }) - .on('mouseover', function (event, d) { + .on('mouseover', function (event, d: any) { d3.select(this) - .transition() - .duration(200) - .attr('stroke', '#000') - .attr('stroke-width', 2) - .attr('filter', 'url(#node-glow)'); + .attr('stroke', '#2b33e9') + .attr('stroke-width', 1.5) + .attr('fill-opacity', d.children ? 0.78 : leafFillOpacity(d)); showTooltip(event, d); }) .on('mousemove', function (event) { moveTooltip(event); }) .on('mouseout', function () { - d3.select(this) - .transition() - .duration(200) - .attr('stroke', (d: any) => (d.children ? 'rgba(0,0,0,0.08)' : 'rgba(0,0,0,0.12)')) - .attr('stroke-width', (d: any) => (d.children ? 1 : 0.6)) - .attr('filter', 'none'); updateSelection(); hideTooltip(); }); @@ -292,7 +373,8 @@ export function createZoomableCirclePacking( .attr('transform', (d: any) => `translate(${d.x},${d.y})`) .style('display', (d: any) => (d.parent === focus ? 'inline' : 'none')); - zoomTo([focus.x, focus.y, focus.r * 2]); + zoomTo([focus.x, focus.y, focus.r * 2.05]); + emitFocusChange(); container.appendChild(svg.node()!); // Functions @@ -306,37 +388,39 @@ export function createZoomableCirclePacking( labelNodes.attr('transform', (d: any) => { const x = (d.x - v[0]) * k + size / 2 + offsetX; const y = (d.y - v[1]) * k + size / 2 + offsetY; - const offset = d.children ? d.r * k + 8 : 0; - return `translate(${x}, ${y - offset})`; + const topY = d.children ? y - d.r * k + 2 : y; + return `translate(${x}, ${topY})`; }) .each(function (d: any) { const el = d3.select(this as SVGGElement); if (d.parent === focus && shouldShowLabel(d, k)) { el.style('display', 'inline').style('fill-opacity', 0.92); - createLabelGroup(el, d); + createLabelGroup(el, d, isUnavailable(d)); } else el.style('display', 'none'); }); } + const isTutorialTarget = (d: any): boolean => + !d.children && !!tutorialHighlightCode && codeOf(d.data) === tutorialHighlightCode; + function updateSelection() { node.transition().duration(200) - .attr('fill', (d: any) => { - if (d === selectedDataNode) return colors.selected; - if (d.children) return groupColor(d.depth); - return colorForLeaf(d, sets, colors); - }) - .attr('fill-opacity', (d: any) => (d.children ? 0.6 : 1)) + .attr('fill', (d: any) => fillForNode(d)) + .attr('fill-opacity', (d: any) => leafFillOpacity(d)) .attr('stroke', (d: any) => { + if (isTutorialTarget(d)) return '#15803d'; if (d === selectedDataNode) return '#000'; if (d.children) return 'rgba(0,0,0,0.08)'; return 'rgba(0,0,0,0.12)'; }) .attr('stroke-width', (d: any) => { + if (isTutorialTarget(d)) return 2.5; if (d === selectedDataNode) return 2; if (d.children) return 1; return 0.6; }) - .attr('filter', (d: any) => (d === selectedDataNode ? 'url(#node-glow)' : 'none')); + .attr('filter', (d: any) => (d === selectedDataNode ? 'url(#node-glow)' : 'none')) + .attr('stroke-dasharray', (d: any) => (isUnavailable(d) ? UNAVAILABLE_STROKE_DASH : null)); } function zoom(event: MouseEvent | null, d: any) { @@ -351,12 +435,13 @@ export function createZoomableCirclePacking( svg.transition() .duration(isFast ? 7500 : 750) .tween('zoom', () => { - const i = d3.interpolateZoom(view, [d.x, d.y, d.r * 2]); + const i = d3.interpolateZoom(view, [d.x, d.y, d.r * 2.05]); return (t: number) => zoomTo(i(t)); }) .on('end', () => { selectedDataNode = null; updateSelection(); + emitFocusChange(); options?.onAnimationEnd?.(); }) .on('interrupt', () => { @@ -377,26 +462,28 @@ export function createZoomableCirclePacking( if (focus === group) { selectedDataNode = target.children ? null : target; updateSelection(); + emitFocusChange(); return; } - const zoomTarget: [number, number, number] = [group.x, group.y, group.r * 2]; + const zoomTarget: [number, number, number] = [group.x, group.y, group.r * 2.05]; labelNodes.each(function (nd: any) { const el = d3.select(this as SVGGElement); if (nd.parent === group && shouldShowLabel(nd, size / zoomTarget[2])) { el.style('display', 'inline').style('fill-opacity', 0.6); - createLabelGroup(el, nd); + createLabelGroup(el, nd, isUnavailable(nd)); } else el.style('display', 'none').style('fill-opacity', 0); }); - if (!view) view = [root.x, root.y, root.r * 2]; + if (!view) view = [root.x, root.y, root.r * 2.05]; if (focus === root && !selectedDataNode) { - zoomTo(zoomTarget); focus = group; - selectedDataNode = target.children ? null : target; // group -> null + selectedDataNode = target.children ? null : target; + zoomTo(zoomTarget); updateSelection(); + emitFocusChange(); } options?.onAnimationStart?.(); @@ -422,6 +509,7 @@ export function createZoomableCirclePacking( } else el.style('display', 'none').style('fill-opacity', 0); }); + emitFocusChange(); options?.onAnimationEnd?.(); }) .on('interrupt', () => { @@ -429,6 +517,19 @@ export function createZoomableCirclePacking( }); } + function emitFocusChange(): void { + const path: Array<{ code: string; label: string }> = []; + let current: any = focus; + while (current) { + path.unshift({ + code: String(current.data?.code ?? ''), + label: String(current.data?.label ?? current.data?.name ?? ''), + }); + current = current.parent; + } + options?.onFocusChange?.(path); + } + svg.on('click', function (event: MouseEvent) { if (event.target === this) zoom(event, root); }); @@ -436,11 +537,32 @@ export function createZoomableCirclePacking( // immutable refreshColors function refreshColors(newOptions?: { selectedVariables?: any[]; - selectedCovariates?: any[]; selectedFilters?: any[]; colors?: Partial<BubbleColorConfig>; + tutorialHighlightCode?: string | null; + tutorialHighlightColor?: string; + unavailableVariables?: UnavailableVariablesMap; }) { + const hasTutorialHighlightCode = !!newOptions + && Object.prototype.hasOwnProperty.call(newOptions, 'tutorialHighlightCode'); + const hasTutorialHighlightColor = !!newOptions + && Object.prototype.hasOwnProperty.call(newOptions, 'tutorialHighlightColor'); + colors = { ...colors, ...(newOptions?.colors ?? {}) }; + if (hasTutorialHighlightCode) { + tutorialHighlightCode = newOptions?.tutorialHighlightCode ?? null; + } + if (hasTutorialHighlightColor && newOptions?.tutorialHighlightColor) { + tutorialHighlightColor = newOptions.tutorialHighlightColor; + } + if (newOptions && Object.prototype.hasOwnProperty.call(newOptions, 'unavailableVariables')) { + unavailableVariables = newOptions.unavailableVariables ?? EMPTY_UNAVAILABLE_VARIABLES; + node.call(applyAvailability); + labelNodes.each(function (d: any) { + const el = d3.select(this as SVGGElement); + if (el.style('display') !== 'none') createLabelGroup(el, d, isUnavailable(d)); + }); + } groupColor = d3.scaleLinear<string>() .domain([0, 5]) .range([colors.groupStart, colors.groupEnd]) @@ -453,11 +575,6 @@ export function createZoomableCirclePacking( .map(codeOf) .filter((v): v is string => !!v) ), - covs: new Set( - [...(newOptions?.selectedCovariates ?? [])] - .map(codeOf) - .filter((v): v is string => !!v) - ), filters: new Set( [...(newOptions?.selectedFilters ?? [])] .map(codeOf) @@ -465,30 +582,25 @@ export function createZoomableCirclePacking( ), }; - // Updates leaf node colors based on the new snapshot - node.each(function (d: any) { - const circle = d3.select(this); - if (!d.children) { - const newFill = colorForLeaf(d, sets, colors); - if (circle.attr('fill') !== newFill) { - circle.attr('fill', newFill); - } - } else { - circle.attr('fill', groupColor(d.depth)); - } - }); updateSelection(); } function shouldShowLabel(d: any, k: number): boolean { + if (d === root) return false; + if (focus === root && !d.children) return false; const radius = d.r * k; - if (radius < 16) return false; - return true; + if (d.children) return radius >= 18; + return radius >= 22; } return { zoomToNode, refreshColors, + resetZoom: () => { + if (focus !== root) { + zoom(null, root); + } + }, destroy: () => { tooltip.remove(); } diff --git a/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.spec.ts b/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.spec.ts index 7c6cfa5bd..d28cf2387 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.spec.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.spec.ts @@ -1,15 +1,26 @@ +import { TestBed } from '@angular/core/testing'; import { ChartBuilderService } from './chart-builder.service'; +import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; describe('ChartBuilderService', () => { const experimentServiceStub = { - selectedVariables: () => [], - selectedCovariates: () => [], - selectedFilters: () => [], + algorithmAssignableVariables: (): Array<{ code: string; label: string; name: string }> => [], + selectedFilters: (): Array<{ code: string; label: string; name: string }> => [], + getDatasetLabelMap: () => ({ 'dataset-a': 'Dataset A' }), }; - it('keeps chart-specific title for single-chart algorithms', () => { - const service = new ChartBuilderService(experimentServiceStub as any); + let service: ChartBuilderService; + + beforeEach(() => { + experimentServiceStub.algorithmAssignableVariables = () => []; + experimentServiceStub.selectedFilters = () => []; + TestBed.configureTestingModule({ + providers: [{ provide: ExperimentStudioService, useValue: experimentServiceStub }], + }); + service = TestBed.inject(ChartBuilderService); + }); + it('keeps chart-specific title for single-chart algorithms', () => { const charts = service.getChartsForAlgorithm('linear_svm', { title: 'Federated SVM Report', weights: [0.2, -0.1, 0.3, 0.4], @@ -21,8 +32,6 @@ describe('ChartBuilderService', () => { }); it('keeps chart-specific titles for multi-chart algorithms', () => { - const service = new ChartBuilderService(experimentServiceStub as any); - const charts = service.getChartsForAlgorithm('pca', { title: 'PCA Run 2026', eigenvectors: [ @@ -33,13 +42,51 @@ describe('ChartBuilderService', () => { }); expect(charts.length).toBe(2); - expect((charts[0] as any).title?.text).toBe('Eigenvectors'); - expect((charts[1] as any).title?.text).toBe('Scree Plot (Eigenvalues)'); + expect((charts[0] as any).title?.text).toBe('Component weights'); + expect((charts[1] as any).title?.text).toBe('Variance explained'); + expect((charts[1] as any).series[0].data).toEqual([66.7, 33.3]); }); - it('uses fallback title when result title is missing', () => { - const service = new ChartBuilderService(experimentServiceStub as any); + it('labels PCA heatmap variables from result.variables, not the studio selection', () => { + experimentServiceStub.algorithmAssignableVariables = () => [ + { code: 'wrong1', label: 'Wrong One', name: 'Wrong One' }, + { code: 'wrong2', label: 'Wrong Two', name: 'Wrong Two' }, + ]; + experimentServiceStub.selectedFilters = () => [ + { code: 'age', label: 'Age', name: 'Age' }, + { code: 'bmi', label: 'BMI', name: 'BMI' }, + ]; + const charts = service.getChartsForAlgorithm('pca', { + title: 'PCA Run 2026', + variables: ['age', 'bmi'], + eigenvectors: [ + [0.7, 0.2], + [0.1, 0.9], + ], + eigenvalues: [1.8, 0.9], + }); + + expect((charts[0] as any).yAxis.data).toEqual(['Age', 'BMI']); + expect((charts[0] as any).yAxis.data).not.toContain('Wrong One'); + }); + + it('keeps PCA heatmap labels a saved experiment supplies', () => { + const charts = service.getChartsForAlgorithm('pca', { + title: 'PCA Run 2026', + variables: ['age', 'bmi'], + variable_names: ['Age (years)', 'Body mass index'], + eigenvectors: [ + [0.7, 0.2], + [0.1, 0.9], + ], + eigenvalues: [1.8, 0.9], + }); + + expect((charts[0] as any).yAxis.data).toEqual(['Age (years)', 'Body mass index']); + }); + + it('uses fallback title when result title is missing', () => { const charts = service.getChartsForAlgorithm( 'linear_svm', { @@ -53,8 +100,7 @@ describe('ChartBuilderService', () => { expect((charts[0] as any).title?.text).toBe('SVM Support Vector Distribution'); }); - it('renders pearson p-values and CI charts for legacy key aliases', () => { - const service = new ChartBuilderService(experimentServiceStub as any); + it('draws one lower-triangle correlation heatmap and reads legacy p-value and CI aliases', () => { const matrix = { variables: ['v1', 'v2'], v1: [1, 0.25], @@ -63,16 +109,89 @@ describe('ChartBuilderService', () => { const charts = service.getChartsForAlgorithm('pearson_correlation', { correlations: matrix, - 'p-values': matrix, + 'p-values': { variables: ['v1', 'v2'], v1: [0, 0.0004], v2: [0.0004, 0] }, low_confidence_intervals: matrix, high_confidence_intervals: matrix, }); - expect(charts.length).toBe(4); - const titles = charts.map((c) => (c as any).title?.text); - expect(titles).toContain('Correlation Matrix'); - expect(titles).toContain('P-Values'); - expect(titles).toContain('Confidence Interval (Lower 95%)'); - expect(titles).toContain('Confidence Interval (Upper 95%)'); + expect(charts.length).toBe(1); + const series = (charts[0] as any).series[0]; + expect(series.data.map((cell: any) => cell.value)).toEqual([[0, 0, 0.25]]); + const tooltip = (charts[0] as any).tooltip.formatter({ value: [0, 0, 0.25] }); + expect(tooltip).toContain('v2 × v1'); + expect(tooltip).toContain('p < 0.001'); + expect(tooltip).toContain('95% CI 0.250 to 0.250'); + }); + + it('renders describe box plots from featurewise rows', () => { + const charts = service.getChartsForAlgorithm('describe', { + featurewise: [ + { + variable: 'age', + dataset: 'ds1', + data: { min: 40, q1: 45, q2: 50, q3: 55, max: 60, mean: 51, std: 4.5 }, + }, + { variable: 'age', dataset: 'empty_ds', data: null }, + ], + }); + + expect(charts.length).toBe(1); + const chart = charts[0] as any; + expect(chart.title?.text).toBe('Distribution for age'); + expect(chart.xAxis.data).toEqual(['ds1']); + expect(chart.series.map((s: any) => s.name)).toEqual(['Q1–Q3 box · min–max whiskers', 'Median', 'Mean ± SD']); + expect(chart.series[0].data).toEqual([[0, 40, 45, 50, 55, 60, 51, 4.5, 46.5, 55.5]]); + const tooltip = chart.tooltip.formatter({ dataIndex: 0 }); + expect(tooltip).toContain('Median'); + expect(tooltip).toContain('4.50'); + }); + + it('renders a pooled All datasets band from featurewise rows', () => { + const charts = service.getChartsForAlgorithm('describe', { + featurewise: [ + { + variable: 'age', + dataset: 'ds1', + data: { min: 40, q1: 45, q2: 50, q3: 55, max: 60, mean: 51, std: 4.5 }, + }, + { + variable: 'age', + dataset: 'all datasets', + data: { min: 38, q1: 44, q2: 74, q3: 80, max: 90, mean: 62, std: 8 }, + }, + ], + }); + + expect(charts.length).toBe(1); + const chart = charts[0] as any; + expect(chart.series.map((s: any) => s.name)).toContain('All datasets · Q1–Q3'); + expect(chart.legend.data.map((d: any) => d.name)).toContain('All datasets · Q1–Q3'); + expect(chart.xAxis.data).toEqual(['ds1']); + expect(chart.xAxis.data).not.toContain('All datasets'); + expect(chart.grid.right).toBe(92); + const tooltip = chart.tooltip.formatter({ seriesName: 'All datasets · Q1–Q3', dataIndex: 0 }); + expect(tooltip).toContain('(est.)'); + }); + + it('omits the pooled band when the All datasets row has no quartiles', () => { + const charts = service.getChartsForAlgorithm('describe', { + featurewise: [ + { + variable: 'age', + dataset: 'ds1', + data: { min: 40, q1: 45, q2: 50, q3: 55, max: 60, mean: 51, std: 4.5 }, + }, + { + variable: 'age', + dataset: 'all datasets', + data: { min: 38, max: 90, mean: 62, std: 8 }, + }, + ], + }); + + expect(charts.length).toBe(1); + const chart = charts[0] as any; + expect(chart.series.map((s: any) => s.name)).not.toContain('All datasets · Q1–Q3'); + expect(chart.grid.right).toBe(40); }); }); diff --git a/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.ts b/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.ts index 6b82e12b2..51486e7c5 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/chart-builder.service.ts @@ -1,54 +1,59 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { EChartsOption } from 'echarts'; import { AlgorithmChartRegistry } from './chart-registry'; import { ExperimentStudioService } from '../../../../services/experiment-studio.service'; -import 'echarts-gl'; - -function getByPath(obj: any, path: string): any { - if (!path) return obj; - return path.split('.').reduce((o, key) => o?.[key], obj); -} @Injectable({ providedIn: 'root' }) export class ChartBuilderService { - constructor(private experimentService: ExperimentStudioService) { } + private experimentService = inject(ExperimentStudioService); - getChartsForAlgorithm(algorithm: string, result: any, _fallbackTitle?: string | null): EChartsOption[] { - const config = AlgorithmChartRegistry[algorithm] || AlgorithmChartRegistry['default']; - // raw input - const input = getByPath(result, config.inputPath); + getChartsForAlgorithm(algorithm: string, result: any, _fallbackTitle?: string | null): EChartsOption[] { + const config = AlgorithmChartRegistry[algorithm]; + if (!config) return []; // enrich with display names - const enrichedInput = this.enrichLabels(input); - - // PCA specific enrichment: inject actual variable names for the heatmap - if (algorithm === 'pca' || algorithm === 'pca_with_transformation') { - const vars = this.experimentService.selectedVariables(); - const covs = this.experimentService.selectedCovariates(); - const allSelected = [...vars, ...covs]; - - if (allSelected.length > 0) { - // We use a unique property name to avoid collisions - (enrichedInput as any).variable_names = allSelected.map(v => v.label || v.name || v.code); + const enrichedInput = this.enrichLabels(result); + + // PCA: heatmap columns come from the result's own variable codes (request y order), + // labelled through the studio label map — never the current studio selection. Labels a + // saved experiment already supplied (`variable_names`) are kept. + if (algorithm === 'pca' && !enrichedInput?.variable_names?.length) { + const variables = Array.isArray(enrichedInput?.variables) ? enrichedInput.variables : []; + if (variables.length > 0) { + (enrichedInput as any).variable_names = variables; } } + if (algorithm === 'describe') { + this.attachDescribeDatasetLabels(enrichedInput); + } + // Keep chart titles chart-specific. Experiment-level title is rendered in the result header. return config.build(enrichedInput); } + private attachDescribeDatasetLabels(target: any): void { + const map = this.experimentService.getDatasetLabelMap(); + if (!Object.keys(map).length || !target || typeof target !== 'object') return; + + if (target.result && typeof target.result === 'object') { + (target.result as Record<string, unknown>)['dataset_labels'] = map; + return; + } + + target['dataset_labels'] = map; + } + private enrichLabels(input: any): any { if (!input) return input; - const variables = this.experimentService.selectedVariables(); - const covariates = this.experimentService.selectedCovariates(); + const variables = this.experimentService.algorithmAssignableVariables(); const filters = this.experimentService.selectedFilters(); const replaceLabel = (raw: string) => { const match = variables.find(v => v.code === raw) || - covariates.find(c => c.code === raw) || filters.find(f => f.code === raw); return match?.name || match?.label || raw; diff --git a/src/app/pages/experiment-studio/visualisations/charts/chart-palette.ts b/src/app/pages/experiment-studio/visualisations/charts/chart-palette.ts new file mode 100644 index 000000000..8a0a367e6 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/charts/chart-palette.ts @@ -0,0 +1,29 @@ +/** + * Categorical series colours, assigned in this order and never cycled past the end. + * Slots 1-3 are the MIP charter (primary, primary-light, accent); 4-8 are hues picked + * so every adjacent pair stays apart for deuteranopia/protanopia (worst ΔE 15.2) and + * for full colour vision (worst ΔE 15.6). Slots 2, 3, 6 and 8 sit under 3:1 on white, + * so a chart using them always carries a legend and the table view below it. + */ +export const CHART_SERIES_COLORS = [ + '#2b33e9', + '#7f9ce8', + '#ffba08', + '#e34948', + '#4a3aa7', + '#e87ba4', + '#008300', + '#1baf7a', +]; + +/** Signed heatmap scale from -1 to 1: red below zero, neutral grey at zero, primary blue above. */ +export const DIVERGING_COLORS = ['#e34948', '#f1f5f9', '#2b33e9']; + +/** + * Text colour for a value printed on a cell shaded by DIVERGING_COLORS (or its grey-to-blue half), + * given the cell's place on that scale. Ink stays at 4.5:1 or better across the red half, so white + * only takes over on the deep blue end, where it beats ink. + */ +export function cellLabelColor(scalePosition: number): string { + return scalePosition >= 0.7 ? '#ffffff' : '#0f172a'; +} diff --git a/src/app/pages/experiment-studio/visualisations/charts/chart-registry.spec.ts b/src/app/pages/experiment-studio/visualisations/charts/chart-registry.spec.ts index aea4246de..dc219aa45 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/chart-registry.spec.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/chart-registry.spec.ts @@ -1,4 +1,4 @@ -import { AlgorithmChartRegistry } from './chart-registry'; +import { AlgorithmChartRegistry, tablesReplacedByCharts } from './chart-registry'; describe('AlgorithmChartRegistry', () => { it('contains canonical SVM key', () => { @@ -16,4 +16,16 @@ describe('AlgorithmChartRegistry', () => { expect((charts[0] as any).title?.text).toBe('SVM Support Vector Distribution'); }); + it('replaces a table only when the chart that shows it rendered', () => { + const chart = (text: string) => ({ title: { text } }); + + expect(tablesReplacedByCharts('pca', [chart('Component weights')])).toEqual([{ title: 'Component weights' }]); + expect(tablesReplacedByCharts('pca', [chart('Component weights'), chart('Variance explained')])) + .toEqual([{ title: 'PCA Summary' }, { title: 'Component weights' }]); + expect(tablesReplacedByCharts('kmeans', [chart('K-Means Centers')])).toEqual([]); + expect(tablesReplacedByCharts('kmeans', [chart('Cluster centres')])).toEqual([{ title: 'Cluster Centers' }]); + expect(tablesReplacedByCharts('naive_bayes_gaussian', [chart('Class profiles')])).toEqual([]); + expect(tablesReplacedByCharts('naive_bayes_gaussian_cv', [chart('Confusion Matrix')])).toEqual([{ title: 'Confusion Matrix (Combined)' }]); + expect(tablesReplacedByCharts('unknown', [chart('Confusion Matrix')])).toEqual([]); + }); }); diff --git a/src/app/pages/experiment-studio/visualisations/charts/chart-registry.ts b/src/app/pages/experiment-studio/visualisations/charts/chart-registry.ts index a630f4d08..2e6a7953a 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/chart-registry.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/chart-registry.ts @@ -1,142 +1,95 @@ import { EChartsOption } from 'echarts'; +import { TableTrim } from '../auto-renderer/algorithm-table-registry'; import { buildRocCurveChart } from './renderers/roc-curve-chart'; import { buildMatrixChart } from './renderers/matrix-chart'; -import { buildBarChart } from './renderers/bar-chart'; + import { buildPCAHeatmapChart } from './renderers/pca-heatmap-chart'; import { buildNaiveBayesConfusionChart } from './renderers/naive-bayes-confusion-matrix-chart'; import { buildLogRegConfusionChart } from './renderers/log-reg-confusion-matrix-chart'; import { buildKMeansChart } from './renderers/k-means-chart'; -import { buildMeanPlotChart } from './renderers/mean-plot-chart'; +import { buildMeanPlotChart, buildTTestIntervalChart } from './renderers/mean-plot-chart'; import { buildBoxPlotChart } from './renderers/box-plot-chart'; import { buildSVMChart } from './renderers/svm-chart'; -// New Renderers -import { buildForestPlotChart } from './renderers/forest-plot-chart'; -import { buildTTestChart } from './renderers/t-test-chart'; -import { buildTwoWayAnovaChart } from './renderers/anova-twoway-chart'; -import { buildCVMetricsChart } from './renderers/cv-metrics-chart'; import { buildHistogramChart } from './renderers/histogram-chart'; -import { buildNaiveBayesPriorsChart } from './renderers/naive-bayes-priors-chart'; +import { buildNaiveBayesPriorsChart, buildNaiveBayesProfileChart } from './renderers/naive-bayes-priors-chart'; + +/** What one chart shows of a result table, named by that chart's title text. */ +interface ChartTableTrim extends TableTrim { + byChart: string; +} -export interface AlgorithmChartConfig { +interface AlgorithmChartConfig { build: (input: any) => EChartsOption[]; - inputPath: string; + /** Heading of the chart section on the result page. */ + title: string; + /** What these charts show of the result tables, each left out on screen only when the chart named + * in `byChart` rendered: another chart of the same result does not stand in for it. */ + replaces?: ChartTableTrim[]; + /** How to read the charts, in plain language, shown under them. */ + note?: string; } +const PCA_REPLACES: ChartTableTrim[] = [ + { title: 'PCA Summary', byChart: 'Variance explained' }, + { title: 'Component weights', byChart: 'Component weights' }, +]; + function composeCharts(...builders: ((result: any) => EChartsOption[])[]): (result: any) => EChartsOption[] { return (result: any) => builders.flatMap(fn => fn(result)); } export const AlgorithmChartRegistry: Record<string, AlgorithmChartConfig> = { - kmeans: { - build: buildKMeansChart, - inputPath: '', - }, - - linear_regression: { - build: buildForestPlotChart, - inputPath: '', - }, - - linear_regression_cv: { - build: buildCVMetricsChart, - inputPath: '', - }, - - // Logistic Regression (Standard) - logistic_regression: { - build: buildForestPlotChart, - inputPath: '', - }, - - // Legacy alias retained for backwards compatibility with historical payloads. - logistic_regression_cv_fedaverage: { - build: composeCharts(buildLogRegConfusionChart, buildRocCurveChart), - inputPath: '', - }, - + // The heatmap prints every centre in its cells; the two-variable bubble chart only places them, + // so with two variables the centres table stays. + kmeans: { build: buildKMeansChart, title: 'Cluster centres', replaces: [{ title: 'Cluster Centers', byChart: 'Cluster centres' }] }, logistic_regression_cv: { - build: composeCharts(buildLogRegConfusionChart, buildRocCurveChart), // CV Metrics chart could be added if supported - inputPath: '', + build: composeCharts(buildLogRegConfusionChart, buildRocCurveChart), + title: 'Classification performance', }, - naive_bayes_gaussian: { - build: buildNaiveBayesPriorsChart, - inputPath: '', + build: composeCharts(buildNaiveBayesPriorsChart, buildNaiveBayesProfileChart), + title: 'Classes', + replaces: [{ title: 'Class Summary', byChart: 'Class Prior Probabilities' }], + note: + "In the class profiles, each dot shows how far one class's average for a variable lies from that variable's overall average, " + + 'in standard deviations (0 is the overall average). The further apart the dots on a row, the better that variable tells the classes apart.', }, - naive_bayes_categorical: { build: buildNaiveBayesPriorsChart, - inputPath: '', + title: 'Class priors', + replaces: [{ title: 'Class Summary', byChart: 'Class Prior Probabilities' }], }, - naive_bayes_gaussian_cv: { build: buildNaiveBayesConfusionChart, - inputPath: '', + title: 'Classification performance', + replaces: [{ title: 'Confusion Matrix (Combined)', byChart: 'Confusion Matrix' }], }, - naive_bayes_categorical_cv: { build: buildNaiveBayesConfusionChart, - inputPath: '', - }, - - pearson_correlation: { - build: buildMatrixChart, - inputPath: '', - }, - - pca: { - build: buildPCAHeatmapChart, - inputPath: '', - }, - - pca_with_transformation: { - build: buildPCAHeatmapChart, - inputPath: '', - }, - - anova_oneway: { - build: composeCharts(buildBarChart, buildMeanPlotChart), - inputPath: '', - }, - - anova_twoway: { - build: buildTwoWayAnovaChart, - inputPath: '', - }, - - ttest_independent: { - build: buildTTestChart, - inputPath: '', - }, - - ttest_paired: { - build: buildTTestChart, - inputPath: '', - }, - - ttest_onesample: { - build: buildTTestChart, - inputPath: '', - }, - - linear_svm: { - build: buildSVMChart, - inputPath: '', - }, - - describe: { - build: buildBoxPlotChart, - inputPath: '', - }, + title: 'Classification performance', + replaces: [{ title: 'Confusion Matrix (Combined)', byChart: 'Confusion Matrix' }], + }, + // The pairs table keeps r: it is sorted by strength, and the value it is sorted by belongs in it. + pearson_correlation: { build: buildMatrixChart, title: 'Correlation matrix' }, + pca: { build: buildPCAHeatmapChart, title: 'Principal components', replaces: PCA_REPLACES }, + anova_oneway: { build: buildMeanPlotChart, title: 'Group means' }, + linear_svm: { build: buildSVMChart, title: 'Model weights' }, + describe: { build: buildBoxPlotChart, title: 'Distributions' }, + histogram: { build: buildHistogramChart, title: 'Distribution' }, + ttest_independent: { build: buildTTestIntervalChart, title: 'Mean difference' }, +}; - histogram: { - build: buildHistogramChart, - inputPath: '', - }, +function chartTitle(chart: EChartsOption): string | null { + const title = Array.isArray(chart.title) ? chart.title[0] : chart.title; + return typeof title?.text === 'string' ? title.text : null; +} - default: { - build: () => [], - inputPath: '', - }, -}; +/** The result tables an algorithm's rendered charts stand in for on screen: each only when its chart is among them. */ +export function tablesReplacedByCharts(algorithm: string | null, charts: EChartsOption[]): TableTrim[] { + const replaces = algorithm ? AlgorithmChartRegistry[algorithm]?.replaces ?? [] : []; + const rendered = new Set(charts.map(chartTitle)); + return replaces + .filter((entry) => rendered.has(entry.byChart)) + .map(({ title, columns, rows }) => ({ title, ...(columns ? { columns } : {}), ...(rows ? { rows } : {}) })); +} diff --git a/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.css b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.css index f362bb0e4..0c3a413b2 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.css +++ b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.css @@ -1,11 +1,9 @@ .chart-container { - margin: 24px; + width: 100%; + margin: 0; } .chart { width: 100%; - max-width: 800px; - height: 500px; - margin: 0 auto; + min-height: 280px; } - diff --git a/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.html b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.html index 09a18539a..c020d5024 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.html +++ b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.html @@ -1,8 +1,9 @@ -@if(charts.length) { +@if(charts().length) { <div> - @for(chart of charts; track chart) { + @for(chart of themedCharts; track chart) { <div class="chart-container"> - <div echarts [options]="chart" [initOpts]="{ renderer: 'canvas' }" class="chart"></div> + <div echarts [options]="chart" [initOpts]="{ renderer: 'canvas' }" class="chart" + [style.height.px]="chartHeight(chart)"></div> </div> } </div> @@ -10,4 +11,4 @@ <ng-template #noCharts> <p>No charts to display.</p> </ng-template> -} \ No newline at end of file +} diff --git a/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.spec.ts b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.spec.ts new file mode 100644 index 000000000..c4f8e2cfc --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.spec.ts @@ -0,0 +1,51 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { provideEchartsCore } from 'ngx-echarts'; +import { ChartRendererComponent } from './charts-renderer.component'; + +describe('ChartRendererComponent house style', () => { + function themed(chart: Record<string, unknown>): any { + TestBed.configureTestingModule({ + imports: [ChartRendererComponent], + providers: [provideZonelessChangeDetection(), provideEchartsCore({ echarts: () => import('echarts') })], + }); + const fixture = TestBed.createComponent(ChartRendererComponent); + fixture.componentRef.setInput('charts', [chart]); + fixture.detectChanges(); + return fixture.componentInstance.themedCharts[0]; + } + + it('adds the app font, title ink and tooltip card under the chart’s own settings', () => { + const chart = themed({ + title: { text: 'T', textStyle: { fontSize: 16 } }, + tooltip: { trigger: 'axis', backgroundColor: '#000' }, + series: [{ type: 'line', data: [1, 2] }], + }); + + expect(chart.textStyle.fontFamily).toContain('Inter'); + expect(chart.title.textStyle).toEqual({ fontSize: 16, fontWeight: 600, color: '#0f172a' }); + expect(chart.tooltip.backgroundColor).toBe('#000'); + expect(chart.tooltip.borderWidth).toBe(1); + expect(chart.aria.enabled).toBe(true); + // Lines keep their real shape: no default smoothing or area fill. + expect(chart.series[0].smooth).toBeUndefined(); + expect(chart.series[0].areaStyle).toBeUndefined(); + }); + + it('groups value-axis thousands with a thin space unless the axis formats its own labels', () => { + const chart = themed({ + xAxis: { type: 'category', data: ['a'] }, + yAxis: [{}, { type: 'value', axisLabel: { formatter: '{value} %' } }], + series: [], + }); + + expect(chart.xAxis.axisLabel).toBeUndefined(); + expect(chart.yAxis[0].axisLabel.formatter(12500)).toBe('12\u202f500'); + expect(chart.yAxis[0].axisLabel.formatter(0.25)).toBe('0.25'); + expect(chart.yAxis[1].axisLabel.formatter).toBe('{value} %'); + }); + + it('adds no tooltip to a chart that has none', () => { + expect(themed({ series: [] }).tooltip).toBeUndefined(); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.ts b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.ts index 568fc9e9a..b7254796c 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/charts-renderer/charts-renderer.component.ts @@ -1,36 +1,126 @@ -import { Component, Input, AfterViewInit, QueryList, ViewChildren, NgZone, inject, ChangeDetectionStrategy } from '@angular/core'; +import { Component, ChangeDetectionStrategy, OnChanges, input } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { ECharts, EChartsOption } from 'echarts'; -import { NgxEchartsModule, NgxEchartsDirective } from 'ngx-echarts'; -import { ElementRef, OnChanges, SimpleChanges } from '@angular/core'; +import { EChartsOption } from 'echarts'; +import { NgxEchartsModule } from 'ngx-echarts'; +import { SimpleChanges } from '@angular/core'; +import { CHART_SERIES_COLORS } from '../chart-palette'; +import { groupThousands } from '../../result-format'; + +/** --font-sans without the licensed Alaska faces, which canvas text cannot load either. */ +const CHART_FONT = "Inter, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif"; @Component({ selector: 'app-chart-renderer', imports: [CommonModule, NgxEchartsModule], templateUrl: './charts-renderer.component.html', - styleUrls: ['./charts-renderer.component.css'], + styleUrl: './charts-renderer.component.css', changeDetection: ChangeDetectionStrategy.OnPush }) -export class ChartRendererComponent implements AfterViewInit { - private zone = inject(NgZone); - @Input() charts: EChartsOption[] = []; - @ViewChildren(NgxEchartsDirective) echartsDirectives!: QueryList<NgxEchartsDirective>; - - private instances: ECharts[] = []; +export class ChartRendererComponent implements OnChanges { + readonly charts = input<EChartsOption[]>([]); + themedCharts: EChartsOption[] = []; constructor() { } - ngAfterViewInit(): void { - this.zone.runOutsideAngular(() => { - setTimeout(() => { - this.instances = this.echartsDirectives - .map(d => (d as any).getInstance?.()) - .filter((i): i is ECharts => !!i); - }, 1000); - }); + ngOnChanges(_changes: SimpleChanges): void { + this.themedCharts = this.charts().map((chart) => this.applyBrandChartDefaults(chart)); + } + + /** + * House style every chart starts from; a renderer's own settings win. Canvas text cannot read + * CSS, so the app font, title ink and tooltip card are set here once rather than per renderer. + */ + private applyBrandChartDefaults(chart: EChartsOption): EChartsOption { + const title = chart.title; + const tooltip = chart.tooltip; + return { + ...chart, + // ECharts writes a text description of each chart for screen readers; no decal patterns. + aria: { enabled: true, decal: { show: false } }, + color: CHART_SERIES_COLORS, + textStyle: { fontFamily: CHART_FONT, ...this.objectValue(chart.textStyle) }, + ...(title && !Array.isArray(title) + ? { + title: { + ...title, + textStyle: { fontSize: 13, fontWeight: 600, color: '#0f172a', ...this.objectValue((title as Record<string, unknown>)['textStyle']) }, + subtextStyle: { fontSize: 12, color: '#475569', ...this.objectValue((title as Record<string, unknown>)['subtextStyle']) }, + }, + } + : {}), + ...(tooltip && !Array.isArray(tooltip) + ? { + tooltip: { + backgroundColor: '#ffffff', + borderColor: 'rgba(148, 163, 184, 0.45)', + borderWidth: 1, + extraCssText: 'border-radius: 8px; box-shadow: 0 4px 12px rgba(15, 23, 42, 0.08);', + ...tooltip, + textStyle: { color: '#0f172a', fontSize: 12, fontFamily: CHART_FONT, ...this.objectValue((tooltip as Record<string, unknown>)['textStyle']) }, + }, + } + : {}), + ...(chart.xAxis ? { xAxis: this.groupAxisLabels(chart.xAxis, 'category') as EChartsOption['xAxis'] } : {}), + ...(chart.yAxis ? { yAxis: this.groupAxisLabels(chart.yAxis, 'value') as EChartsOption['yAxis'] } : {}), + series: this.softenSeries(chart.series), + } as EChartsOption; + } + + /** Value-axis ticks grouped like the result tables (10 000): ECharts' own label puts a comma there. + * An axis with its own label formatter keeps it; `defaultType` is ECharts' type when none is set. */ + private groupAxisLabels(axis: unknown, defaultType: 'category' | 'value'): unknown { + if (Array.isArray(axis)) return axis.map((entry) => this.groupAxisLabels(entry, defaultType)); + const options = this.objectValue(axis); + const axisLabel = this.objectValue(options['axisLabel']); + if ((options['type'] ?? defaultType) !== 'value' || axisLabel['formatter']) return axis; + return { + ...options, + axisLabel: { ...axisLabel, formatter: (value: number) => groupThousands(value.toLocaleString('en-US', { maximumFractionDigits: 20 })) }, + }; } - getInstances(): ECharts[] { - return this.instances; + private softenSeries(series: EChartsOption['series']): EChartsOption['series'] { + if (!series) return series; + if (Array.isArray(series)) { + return series.map((entry) => this.softenSingleSeries(entry)) as EChartsOption['series']; + } + + return this.softenSingleSeries(series) as EChartsOption['series']; } + + private softenSingleSeries(series: unknown): unknown { + if (!series || typeof series !== 'object') return series; + + const next = { ...(series as Record<string, unknown>) }; + const type = next['type']; + + if (type === 'bar') { + next['itemStyle'] = { + borderRadius: [5, 5, 0, 0], + ...this.objectValue(next['itemStyle']), + }; + } + + // Lines keep their series colour and their real shape: smoothing invents bends + // between discrete points (an elbow curve, a ROC curve) that the data does not have. + if (type === 'line') { + next['lineStyle'] = { + width: 2, + ...this.objectValue(next['lineStyle']), + }; + } + + return next; + } + + private objectValue(value: unknown): Record<string, unknown> { + return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {}; + } + + + chartHeight(chart: EChartsOption): number { + const custom = (chart as EChartsOption & { mipChartHeight?: number }).mipChartHeight; + return typeof custom === 'number' && custom > 0 ? custom : 500; + } + } diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/anova-twoway-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/anova-twoway-chart.ts deleted file mode 100644 index ec6861502..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/anova-twoway-chart.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { EChartsOption } from 'echarts'; - -export function buildTwoWayAnovaChart(result: any): EChartsOption[] { - // Visualizes Sum of Squares and Mean Squares for Two-Way ANOVA terms - - const terms = result.terms || []; - const sumSq = result.sum_sq || []; - const meanSq = result.mean_sq || []; // Optional, might not be present or computed - - if (!terms.length || !sumSq.length) return []; - - // If sumSq is object (term -> value) - let dataSS: { name: string, value: number }[] = []; - - if (!Array.isArray(sumSq) && typeof sumSq === 'object') { - dataSS = terms.map((t: string) => ({ name: t, value: sumSq[t] })); - } else if (Array.isArray(sumSq)) { - dataSS = terms.map((t: string, i: number) => ({ name: t, value: sumSq[i] })); - } - - // Filter out invalid values - dataSS = dataSS.filter(d => typeof d.value === 'number'); - - if (dataSS.length === 0) return []; - - const chartSS: EChartsOption = { - title: { - text: 'Two-Way ANOVA: Sum of Squares', - left: 'center' - }, - tooltip: { - trigger: 'axis', - axisPointer: { type: 'shadow' } - }, - grid: { - bottom: '15%' - }, - xAxis: { - type: 'category', - data: dataSS.map(d => d.name), - axisLabel: { rotate: 30, interval: 0 } - }, - yAxis: { - type: 'value', - name: 'Sum of Squares' - }, - series: [ - { - type: 'bar', - data: dataSS.map(d => d.value), - itemStyle: { color: '#5470c6' }, - label: { show: true, position: 'top', formatter: (p: any) => p.value.toFixed(2) } - } - ] - }; - - return [chartSS]; -} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/bar-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/bar-chart.ts deleted file mode 100644 index baf8e8b10..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/bar-chart.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { EChartsOption } from 'echarts'; - -export function buildBarChart(result: any): EChartsOption[] { - const table = result?.anova_table; - if (!table) return []; - - const explained = Number(table?.ss_explained); - const residual = Number(table?.ss_residual); - if (!Number.isFinite(explained) && !Number.isFinite(residual)) return []; - - return [ - { - title: { - text: 'ANOVA Sum of Squares', - left: 'center', - }, - tooltip: { - trigger: 'axis', - axisPointer: { type: 'shadow' }, - }, - grid: { - top: 56, - right: 24, - bottom: 56, - left: 64, - }, - xAxis: { - type: 'category', - data: ['Explained', 'Residual'], - axisLabel: { - interval: 0, - }, - }, - yAxis: { - type: 'value', - name: 'Sum of Squares', - }, - series: [ - { - type: 'bar', - data: [Number.isFinite(explained) ? explained : 0, Number.isFinite(residual) ? residual : 0], - itemStyle: { - color: (params: any) => params?.dataIndex === 0 ? '#2b33e9' : '#7f9ce8', - }, - label: { - show: true, - position: 'top', - formatter: (params: any) => { - const val = Number(params?.value); - return Number.isFinite(val) ? val.toFixed(3).replace(/\.?0+$/, '') : ''; - }, - }, - }, - ], - }, - ]; -} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/box-plot-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/box-plot-chart.ts index fc78e5ecd..5a592e79f 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/box-plot-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/box-plot-chart.ts @@ -1,113 +1,333 @@ import { EChartsOption } from 'echarts'; +import { escapeHtml } from '../../../../../core/html.utils'; +import { + getDescribeDatasetLabels, + getFeaturewiseDescribeRows, + resolveDatasetDisplayLabel, +} from '../../../../../core/describe-result.utils'; -// Builds a Box Plot chart (Q1–Q3 boxes + min/max whiskers + mean dots) -// for descriptive statistics results. +const PRIMARY = '#2b33e9'; +const PRIMARY_DARK = '#1b21a3'; +const BOX_FILL = 'rgba(43, 51, 233, 0.12)'; +const ACCENT = '#ffba08'; +const AXIS = '#475569'; +const SPLIT = '#e2e8f0'; +const LABEL = '#334155'; +const TEXT = '#0f172a'; +const LABEL_FONT = '600 11px sans-serif'; + +const BOX_SERIES = 'Q1–Q3 box · min–max whiskers'; +const MEDIAN_SERIES = 'Median'; +const MEAN_SERIES = 'Mean ± SD'; +const POOLED_SERIES = 'All datasets · Q1–Q3'; +const POOLED_FILL = 'rgba(43, 51, 233, 0.06)'; +const POOLED_LINE = 'rgba(43, 51, 233, 0.55)'; + +interface BoxStats { + label: string; + min: number | null; + q1: number | null; + median: number | null; + q3: number | null; + max: number | null; + mean: number | null; + std: number | null; +} + +const num = (v: unknown): number | null => + typeof v === 'number' && Number.isFinite(v) ? v : null; + +const fmt = (v: number | null): string => + v === null ? 'N/A' : Number.isInteger(v) ? String(v) : v.toFixed(2); + +type LabelMode = 'full' | 'short' | 'none'; + +// Direct labels need room on both sides of the box: named labels on wide bands, +// bare values on medium ones, tooltip only below that. +function geometry(api: any): { half: number; labels: LabelMode } { + const band = api.size([1, 0])[0] as number; + const labels: LabelMode = band >= 250 ? 'full' : band >= 150 ? 'short' : 'none'; + return { half: Math.max(12, Math.min(32, band * 0.15)), labels }; +} + +// Opacity of the other datasets while one is hovered (ECharts defaults to 0.1). +const BLUR = { style: { opacity: 0.25 } }; +// Highlighting a dataset blurs every series in the coordinate system; keep the +// pooled band fully visible as the reference. +const KEEP = { style: { opacity: 1 } }; + +const line = (x1: number, y1: number, x2: number, y2: number, stroke: string, lineWidth: number) => ({ + type: 'line', + shape: { x1, y1, x2, y2 }, + style: { stroke, lineWidth }, + blur: BLUR, +}); + +const label = (x: number, y: number, text: string, align: 'left' | 'right') => ({ + type: 'text', + x, + y, + style: { text, fill: LABEL, font: LABEL_FONT, align, verticalAlign: 'middle', lineHeight: 15 }, + blur: BLUR, +}); + +// Legend-only entry: the dataset series draws every layer, so hovering one +// dataset can fade the others as a whole. +const legendKey = (name: string) => ({ name, type: 'custom' as const, data: [], renderItem: () => null }); + +const pooledBand = (s: BoxStats) => { + const q1 = s.q1 as number; + const q3 = s.q3 as number; + return { + name: POOLED_SERIES, + type: 'custom' as const, + z: 1, + encode: { x: 0, y: [1, 2, 3] }, + data: [[0, q1, s.median, q3]], + renderItem: (params: any, api: any) => { + const cs = params.coordSys as { x: number; width: number }; + const yOf = (v: number) => api.coord([0, v])[1] as number; + const yQ1 = yOf(q1); + const yQ3 = yOf(q3); + const x1 = cs.x; + const x2 = cs.x + cs.width; + const edge = (y: number) => ({ + type: 'line' as const, + shape: { x1, y1: y, x2, y2: y }, + style: { stroke: POOLED_LINE, lineWidth: 1, lineDash: [6, 4] }, + blur: KEEP, + }); + const children: any[] = [ + { + type: 'rect', + shape: { x: x1, y: yQ3, width: cs.width, height: Math.max(1, yQ1 - yQ3) }, + style: { fill: POOLED_FILL }, + blur: KEEP, + }, + edge(yQ3), + edge(yQ1), + ]; + const median = s.median; + const hasMedian = median !== null; + const yLabel = hasMedian ? yOf(median) : (yQ1 + yQ3) / 2; + if (hasMedian) { + children.push({ + type: 'line', + shape: { x1, y1: yLabel, x2, y2: yLabel }, + style: { stroke: PRIMARY, lineWidth: 1.5, lineDash: [6, 4] }, + blur: KEEP, + }); + } + children.push({ + type: 'text', + x: x2 + 8, + y: yLabel, + style: { + text: hasMedian ? `All datasets\nmedian ${fmt(median)}` : 'All datasets', + fill: PRIMARY_DARK, + font: LABEL_FONT, + align: 'left', + verticalAlign: 'middle', + lineHeight: 15, + }, + blur: KEEP, + }); + return { type: 'group' as const, children }; + }, + }; +}; + +// Builds a Box Plot chart (Q1–Q3 boxes, min/max whiskers, labelled median, +// mean ± SD bar) for descriptive statistics results. export function buildBoxPlotChart(result: any): EChartsOption[] { - const variable_based = result?.result?.variable_based ?? result?.variable_based ?? []; - if (!Array.isArray(variable_based) || variable_based.length === 0) return []; - - const firstVariable = variable_based[0]?.variable ?? 'Variable'; - // Filter for the first variable and exclude 'all datasets' as per request - const filtered = variable_based.filter(v => - v.variable === firstVariable && - v.dataset !== 'all datasets' - ); - - // datasets (x-axis) - const datasets = filtered.map(v => v.dataset || 'Dataset'); - - // Extract Q1, Q3, median, min, max, mean - const boxData = filtered.map(v => { - const d = v.data ?? {}; - // Only include in boxplot if we have essential quartiles - if (d.q1 == null || d.q3 == null) return []; - return [d.min, d.q1, d.q2, d.q3, d.max]; - }); - - const meanPoints = filtered.map((v, idx) => { - const mean = v.data?.mean ?? null; - return mean != null ? [idx, mean] : null; - }).filter(Boolean) as [number, number][]; - - const isDark = document.body.classList.contains('theme-dark'); - const textColor = isDark ? '#f1f5f9' : '#0f172a'; - const axisColor = isDark ? 'rgba(255, 255, 255, 0.3)' : '#475569'; - const splitLineColor = isDark ? 'rgba(255, 255, 255, 0.05)' : '#e2e8f0'; + const featurewise = getFeaturewiseDescribeRows(result); + if (featurewise.length === 0) return []; + + const firstVariable = featurewise[0]?.variable ?? 'Variable'; + const rows = featurewise.filter(v => v.variable === firstVariable); + const isPooled = (v: { dataset: string }) => String(v.dataset).toLowerCase() === 'all datasets'; + + const datasetLabels = getDescribeDatasetLabels(result); + const toStats = (v: (typeof rows)[number]): BoxStats => { + const d = (v.data ?? {}) as Record<string, unknown>; + return { + label: resolveDatasetDisplayLabel(String(v.dataset || 'Dataset'), datasetLabels), + min: num(d['min']), + q1: num(d['q1']), + median: num(d['q2']) ?? num(d['median']), + q3: num(d['q3']), + max: num(d['max']), + mean: num(d['mean']), + std: num(d['std']), + }; + }; + const hasBox = (s: BoxStats) => s.q1 !== null && s.q3 !== null; + const stats = rows.filter(v => !isPooled(v)).map(toStats).filter(hasBox); + if (stats.length === 0) return []; + const pooledRow = rows.find(isPooled); + const pooled = pooledRow ? toStats(pooledRow) : null; + const showPooled = !!pooled && hasBox(pooled); + + const tooltipRow = (name: string, value: number | null) => + `<div style="display:flex;justify-content:space-between;gap:20px">` + + `<span style="color:${AXIS}">${name}</span><b>${fmt(value)}</b></div>`; + const tooltipFor = (s: BoxStats, title: string, quartileNote = '') => + `<div style="font-weight:700;margin-bottom:6px">${escapeHtml(title)}</div>` + + tooltipRow('Max', s.max) + + tooltipRow(`Q3${quartileNote}`, s.q3) + + tooltipRow(`Median${quartileNote}`, s.median) + + tooltipRow(`Q1${quartileNote}`, s.q1) + + tooltipRow('Min', s.min) + + `<div style="border-top:1px solid ${SPLIT};margin:6px 0"></div>` + + tooltipRow('Mean', s.mean) + + tooltipRow('Std. deviation', s.std); const chart: EChartsOption = { title: { text: `Distribution for ${firstVariable}`, left: 'center', - textStyle: { color: textColor } + textStyle: { color: TEXT, fontSize: 14, fontWeight: 700 }, + }, + legend: { + top: 36, + left: 'center', + itemGap: 20, + itemWidth: 16, + itemHeight: 10, + selectedMode: false, + textStyle: { color: AXIS, fontSize: 12, fontWeight: 600 }, + data: [ + { name: BOX_SERIES, icon: 'roundRect', itemStyle: { color: BOX_FILL, borderColor: PRIMARY, borderWidth: 1.5 } }, + { name: MEDIAN_SERIES, icon: 'path://M0,0H24V4H0Z', itemStyle: { color: PRIMARY_DARK } }, + { name: MEAN_SERIES, icon: 'diamond', itemStyle: { color: ACCENT, borderColor: AXIS, borderWidth: 1 } }, + ...(showPooled + ? [{ name: POOLED_SERIES, icon: 'rect', itemStyle: { color: POOLED_FILL, borderColor: POOLED_LINE, borderWidth: 1, borderType: 'dashed' as const } }] + : []), + ], }, tooltip: { trigger: 'item', - backgroundColor: isDark ? '#1c253d' : '#fff', - borderColor: isDark ? 'rgba(255,255,255,0.1)' : '#ccc', - textStyle: { color: textColor }, + backgroundColor: 'rgba(255, 255, 255, 0.96)', + borderColor: 'rgba(43, 51, 233, 0.16)', + borderWidth: 1, + padding: [10, 12], + textStyle: { color: TEXT, fontSize: 12 }, formatter: (p: any) => { - const fmt = (v: any) => (v === null || v === undefined) ? 'N/A' : (typeof v === 'number' ? v.toFixed(2) : v); - - if (p.seriesType === 'boxplot') { - // ECharts might prepend category index to p.value on category axes - const vals = p.value.length === 6 ? p.value.slice(1) : p.value; - const [min, q1, median, q3, max] = vals; - return ` - <b>${datasets[p.dataIndex]}</b><br/> - Min: ${fmt(min)}<br/> - Q1: ${fmt(q1)}<br/> - Median: ${fmt(median)}<br/> - Q3: ${fmt(q3)}<br/> - Max: ${fmt(max)} - `; - } else if (p.seriesType === 'scatter') { - // p.value for scatter is [index, mean] - return `${datasets[p.value[0]]}<br/>Mean: ${fmt(p.value[1])}`; + if (p.seriesName === POOLED_SERIES) { + return pooled ? tooltipFor(pooled, 'All datasets (pooled)', ' (est.)') : ''; } - return ''; + const s = stats[p.dataIndex]; + return s ? tooltipFor(s, s.label) : ''; }, }, - grid: { left: '10%', right: '10%', bottom: '10%', top: '15%' }, + // The pooled band's label sits past the plot's right edge. + grid: { top: 88, right: showPooled ? 92 : 40, bottom: 60, left: 72 }, xAxis: { type: 'category', - data: datasets, - boundaryGap: true, - name: 'Dataset', - nameLocation: 'middle', - nameGap: 25, - axisLabel: { color: textColor }, - axisLine: { lineStyle: { color: axisColor } }, - nameTextStyle: { color: textColor } + data: stats.map(s => s.label), + axisLine: { lineStyle: { color: '#cbd5e1' } }, + axisTick: { show: false }, + // Two wrapped lines per dataset name; the width assumes the narrowest + // workspace plot (~520px) because the real band is unknown until render. + axisLabel: { + color: AXIS, + fontWeight: 600, + interval: 0, + margin: 12, + width: Math.max(72, 520 / Math.max(1, stats.length) - 16), + overflow: 'break', + lineHeight: 15, + height: 30, + lineOverflow: 'truncate', + }, }, yAxis: { type: 'value', - name: 'Value', + name: firstVariable, nameLocation: 'middle', - nameGap: 35, - axisLabel: { color: textColor }, - axisLine: { lineStyle: { color: axisColor } }, - splitLine: { lineStyle: { color: splitLineColor } }, - nameTextStyle: { color: textColor } + nameGap: 48, + scale: true, + axisLine: { show: false }, + axisTick: { show: false }, + axisLabel: { color: AXIS }, + splitLine: { lineStyle: { color: SPLIT, type: 'dashed' } }, + nameTextStyle: { color: AXIS, fontWeight: 700 }, }, series: [ { - name: 'Boxplot', - type: 'boxplot', - data: boxData as any[], - itemStyle: { - color: isDark ? 'rgba(127, 156, 232, 0.2)' : 'rgba(43, 51, 233, 0.3)', - borderColor: isDark ? '#7f9ce8' : '#2b33e9', + name: BOX_SERIES, + type: 'custom', + z: 2, + // [i, min, q1, median, q3, max, mean, std, mean - std, mean + std] + encode: { x: 0, y: [1, 2, 3, 4, 5, 6, 8, 9] }, + data: stats.map(({ min, q1, median, q3, max, mean, std }, i) => + mean !== null && std !== null + ? [i, min, q1, median, q3, max, mean, std, mean - std, mean + std] + : [i, min, q1, median, q3, max, mean, std, mean, mean] + ), + renderItem: (_params: any, api: any) => { + const [min, q1, median, q3, max, mean, std] = [1, 2, 3, 4, 5, 6, 7].map(d => api.value(d) as number); + const { half, labels } = geometry(api); + const cap = half * 0.5; + const y = (v: number) => api.coord([api.value(0), v])[1] as number; + const x = api.coord([api.value(0), q1])[0] as number; + const yQ1 = y(q1); + const yQ3 = y(q3); + const children: any[] = []; + + for (const [end, boxEdge] of [[min, yQ1], [max, yQ3]]) { + if (Number.isFinite(end)) { + children.push(line(x, boxEdge, x, y(end), PRIMARY, 1.5), line(x - cap, y(end), x + cap, y(end), PRIMARY, 1.5)); + } + } + children.push({ + type: 'rect', + shape: { x: x - half, y: yQ3, width: half * 2, height: Math.max(1, yQ1 - yQ3), r: 4 }, + style: { fill: BOX_FILL, stroke: PRIMARY, lineWidth: 1.5 }, + blur: BLUR, + }); + + if (Number.isFinite(median)) { + const yMedian = y(median); + children.push(line(x - half, yMedian, x + half, yMedian, PRIMARY_DARK, 3)); + if (labels !== 'none') { + const text = labels === 'full' ? `Median ${fmt(median)}` : fmt(median); + children.push(label(x - half - 8, yMedian, text, 'right')); + } + } + + if (Number.isFinite(mean)) { + const sx = x + half + 16; + const yMean = y(mean); + if (Number.isFinite(std)) { + const yLo = y(mean - std); + const yHi = y(mean + std); + children.push( + line(sx, yLo, sx, yHi, AXIS, 1.5), + line(sx - 5, yLo, sx + 5, yLo, AXIS, 1.5), + line(sx - 5, yHi, sx + 5, yHi, AXIS, 1.5), + ); + } + children.push({ + type: 'polygon', + shape: { points: [[sx, yMean - 6], [sx + 6, yMean], [sx, yMean + 6], [sx - 6, yMean]] }, + style: { fill: ACCENT, stroke: AXIS, lineWidth: 1 }, + blur: BLUR, + }); + if (labels !== 'none') { + const [meanPrefix, sdPrefix] = labels === 'full' ? ['Mean ', 'SD '] : ['', '']; + const text = Number.isFinite(std) + ? `${meanPrefix}${fmt(mean)}\n${sdPrefix}±${fmt(std)}` + : `${meanPrefix}${fmt(mean)}`; + children.push(label(sx + 12, yMean, text, 'left')); + } + } + return { type: 'group', focus: 'self', children }; }, - boxWidth: [20, 30], - }, - { - name: 'Mean', - type: 'scatter', - data: meanPoints, - symbol: 'circle', - symbolSize: 8, - itemStyle: { color: '#ffba08' }, // MIP orange for mean }, + legendKey(MEDIAN_SERIES), + legendKey(MEAN_SERIES), + ...(showPooled && pooled ? [pooledBand(pooled)] : []), ], }; diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/chart-renderers-regressions.spec.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/chart-renderers-regressions.spec.ts index 374791ff5..c6ac307c9 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/chart-renderers-regressions.spec.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/chart-renderers-regressions.spec.ts @@ -1,60 +1,217 @@ -import { buildConfusionMatrixChart } from './confusion-matrix-chart'; -import { buildCVMetricsChart } from './cv-metrics-chart'; -import { buildTTestChart } from './t-test-chart'; +import { buildConfusionMatrixChart, readConfusionMatrix } from './confusion-matrix-chart'; import { buildKMeansChart } from './k-means-chart'; -import { buildNaiveBayesPriorsChart } from './naive-bayes-priors-chart'; +import { buildPCAHeatmapChart } from './pca-heatmap-chart'; +import { buildTTestIntervalChart } from './mean-plot-chart'; +import { buildNaiveBayesPriorsChart, buildNaiveBayesProfileChart } from './naive-bayes-priors-chart'; +import { buildBoxPlotChart } from './box-plot-chart'; +import * as echarts from 'echarts'; describe('Chart renderers regressions', () => { + it('draws the t-test mean difference as a vertical interval, value on the y axis', () => { + const [chart] = buildTTestIntervalChart({ mean_diff: -3.454, ci_lower: -3.463, ci_upper: -3.445 }) as any[]; + + expect(chart.xAxis.type).toBe('category'); + expect(chart.xAxis.data).toEqual(['Mean difference']); + expect(chart.yAxis.type).toBe('value'); + expect(chart.series[0].data[0].value).toEqual(['Mean difference', -3.454]); + expect(chart.series[1].data[0].slice(1, 3)).toEqual([-3.463, -3.445]); + // Zero is far outside this interval, so no zero line stretches the axis. + expect(chart.series[0].markLine).toBeUndefined(); + + const [crossing] = buildTTestIntervalChart({ mean_diff: 0.2, ci_lower: -0.1, ci_upper: 0.5 }) as any[]; + expect(crossing.series[0].markLine.data).toEqual([{ yAxis: 0 }]); + expect(buildTTestIntervalChart({ mean_diff: 1 })).toEqual([]); + }); + + it('fades the other box plot datasets as a whole while one is hovered', async () => { + const stats = { min: 40, q1: 45, q2: 50, q3: 55, max: 60, mean: 51, std: 4.5 }; + const [option] = buildBoxPlotChart({ + featurewise: [ + { variable: 'age', dataset: 'ds1', data: stats }, + { variable: 'age', dataset: 'ds2', data: stats }, + ], + }); + const host = document.createElement('div'); + host.style.cssText = 'width: 700px; height: 400px'; + document.body.appendChild(host); + const chart = echarts.init(host); + chart.setOption(option); + + chart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: 0 }); + // ECharts applies highlight states on its next render frame. + await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))); + const data = (chart as any).getModel().getSeriesByIndex(0).getData(); + const blurred = (i: number) => + data.getItemGraphicEl(i).children().map((c: any) => c.currentStates.includes('blur')); + + expect(blurred(0).length, 'box, whiskers, median and mean drawn together').toBeGreaterThan(6); + expect(blurred(0).every((b: boolean) => !b)).toBe(true); + expect(blurred(1).every((b: boolean) => b)).toBe(true); + + chart.dispose(); + host.remove(); + }); + + it('keeps the pooled All datasets band fully visible while a dataset is hovered', async () => { + const stats = { min: 40, q1: 45, q2: 50, q3: 55, max: 60, mean: 51, std: 4.5 }; + const [option] = buildBoxPlotChart({ + featurewise: [ + { variable: 'age', dataset: 'ds1', data: stats }, + { variable: 'age', dataset: 'ds2', data: stats }, + { + variable: 'age', + dataset: 'all datasets', + data: { min: 38, q1: 42, q2: 74, q3: 80, max: 90, mean: 62, std: 8 }, + }, + ], + }); + (option as any).animation = false; + (option as any).stateAnimation = { duration: 0 }; + const host = document.createElement('div'); + host.style.cssText = 'width: 700px; height: 400px'; + document.body.appendChild(host); + const chart = echarts.init(host); + chart.setOption(option); + + const boxIndex = (option.series as any[]).findIndex((s: any) => s.name === 'Q1–Q3 box · min–max whiskers'); + const pooledIndex = (option.series as any[]).findIndex((s: any) => s.name === 'All datasets · Q1–Q3'); + chart.dispatchAction({ type: 'highlight', seriesIndex: boxIndex, dataIndex: 0 }); + // ECharts applies highlight states on its next render frame. + await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))); + + const model = (chart as any).getModel(); + const boxData = model.getSeriesByIndex(boxIndex).getData(); + expect(boxData.getItemGraphicEl(1).children().every((c: any) => c.currentStates.includes('blur'))).toBe(true); + + const band = model.getSeriesByIndex(pooledIndex).getData().getItemGraphicEl(0); + const displayables: any[] = []; + band.traverse((child: any) => { + if (!child.isGroup) displayables.push(child); + }); + expect(displayables.length).toBeGreaterThan(3); + expect(displayables.every((el: any) => el.currentStates.includes('blur'))).toBe(true); + expect(displayables.every((el: any) => (el.style?.opacity ?? 1) === 1)).toBe(true); + + chart.dispose(); + host.remove(); + }); + it('maps tp/fp/fn/tn confusion matrix with rows=actual and cols=predicted', () => { const charts = buildConfusionMatrixChart({ confusion_matrix: { tp: 5, fp: 1, fn: 2, tn: 7 }, }); expect(charts.length).toBe(1); - const heatmap = (charts[0] as any).series?.[0]?.data ?? []; + const cells = ((charts[0] as any).series?.[0]?.data ?? []).map((cell: any) => [cell.value[0], cell.value[1], cell.count]); + + expect(cells).toContainEqual([0, 0, 5]); // TP + expect(cells).toContainEqual([1, 0, 2]); // FN + expect(cells).toContainEqual([0, 1, 1]); // FP + expect(cells).toContainEqual([1, 1, 7]); // TN + }); + + it('names a binary matrix after the positive class when the run gives it', () => { + expect(readConfusionMatrix({ tp: 5, fp: 1, fn: 2, tn: 7, positive_label: 'yes' })!.labels).toEqual(['yes', 'not yes']); + expect(readConfusionMatrix({ tp: 5, fp: 1, fn: 2, tn: 7 })!.labels).toEqual(['Positive', 'Negative']); + }); + + it('drops classes absent from both sides and shades by share of the actual class', () => { + const confusion = readConfusionMatrix({ data: [[3, 1, 0], [2, 6, 0], [0, 0, 0]], labels: ['no', 'yes', 'unknown'] })!; + expect(confusion.labels).toEqual(['no', 'yes']); - expect(heatmap).toContain([0, 0, 5]); // TP - expect(heatmap).toContain([1, 0, 2]); // FN - expect(heatmap).toContain([0, 1, 1]); // FP - expect(heatmap).toContain([1, 1, 7]); // TN + const charts = buildConfusionMatrixChart({ confusion_matrix: { data: [[3, 1, 0], [2, 6, 0], [0, 0, 0]], labels: ['no', 'yes', 'unknown'] } }); + const shares = ((charts[0] as any).series[0].data).map((cell: any) => cell.value[2]); + expect(shares).toEqual([0.75, 0.25, 0.25, 0.75]); }); - it('renders linear regression CV metrics when payload uses mean/std objects', () => { - const charts = buildCVMetricsChart({ - n_obs: [100, 95, 98, 101, 97], - mean_sq_error: { mean: 1.2, std: 0.1 }, - r_squared: { mean: 0.82, std: 0.03 }, - mean_abs_error: { mean: 0.7, std: 0.06 }, - f_stat: { mean: 9.5, std: 1.3 }, + it('draws three or more kmeans variables as a centres heatmap shaded around the size-weighted mean', () => { + const charts = buildKMeansChart({ + variables: ['v1', 'v2', 'v3'], + clusters: [ + { label: 'Cluster 0', size_interval: '30-30', center: { v1: 10, v2: 1, v3: 5 } }, + { label: 'Cluster 1', size_interval: '10-10', center: { v1: 50, v2: 1, v3: 1 } }, + ], + elbow: null, }); expect(charts.length).toBe(1); - expect((charts[0] as any).title?.text).toBe('Cross-Validation Metric Summary'); - expect((charts[0] as any).series?.length).toBe(2); + const chart = charts[0] as any; + expect(chart.xAxis.data).toEqual(['v1', 'v2', 'v3']); + expect(chart.yAxis.data).toEqual(['Cluster 0 · ~30', 'Cluster 1 · ~10']); + // v1 overall mean (3·10 + 1·50) / 4 = 20: deviations −10 and +30, scaled by the larger. + const cells = chart.series[0].data.map((cell: any) => cell.value); + expect(cells[0]).toEqual([0, 0, -1 / 3, 10]); + expect(cells[3]).toEqual([0, 1, 1, 50]); + // A variable equal in every cluster sits at the mean. + expect(cells[1][2]).toBe(0); + // White text only on the deep blue end; ink reads better on the whole red half. + const labelColors = chart.series[0].data.map((cell: any) => cell.label.color); + expect(cells[5][2]).toBe(-1); + expect(labelColors[5]).toBe('#0f172a'); + expect(labelColors[3]).toBe('#ffffff'); + }); + + it('escapes labels in chart tooltips, which ECharts renders as HTML', () => { + const chart = buildKMeansChart({ + variables: ['<img src=x onerror=alert(1)>', 'v2', 'v3'], + clusters: [{ label: 'Cluster 0', size_interval: '<10', center: { '<img src=x onerror=alert(1)>': 1, v2: 1, v3: 1 } }], + elbow: null, + })[0] as any; + + const html = chart.tooltip.formatter({ value: [0, 0, 0, 1] }); + expect(html).not.toContain('<img'); + expect(html).toContain('<img src=x onerror=alert(1)>'); + expect(html).toContain('Cluster 0 · <10'); + }); + + it('weights a kmeans cluster under the privacy threshold by half the threshold', () => { + const chart = buildKMeansChart({ + variables: ['v1', 'v2', 'v3'], + clusters: [ + { label: 'Cluster 0', size_interval: '30-30', center: { v1: 0, v2: 1, v3: 1 } }, + { label: 'Cluster 1', size_interval: '<10', center: { v1: 70, v2: 1, v3: 1 } }, + ], + elbow: null, + })[0] as any; + + expect(chart.yAxis.data).toEqual(['Cluster 0 · ~30', 'Cluster 1 · <10']); + // Weights 30 and 5: v1 mean (30·0 + 5·70) / 35 = 10, so deviations −10 and +60. + const cells = chart.series[0].data.map((cell: any) => cell.value); + expect(cells[0][2]).toBeCloseTo(-1 / 6, 10); + expect(cells[3][2]).toBe(1); }); - it('accepts numeric strings in t-test CI bounds', () => { - const charts = buildTTestChart({ - mean_diff: 0.5, - ci_lower: '-0.1', - ci_upper: '1.2', - p: 0.03, + it('sizes two-variable kmeans bubbles by the midpoint of each size interval', () => { + const charts = buildKMeansChart({ + variables: ['Age', 'Score'], + clusters: [ + { label: 'Cluster 0', size_interval: '90-110', center: { Age: 70, Score: 3 } }, + { label: 'Cluster 1', size_interval: '20-30', center: { Age: 50, Score: 4 } }, + ], + elbow: null, }); - expect(charts.length).toBe(1); + const series = (charts[0] as any).series; + expect(series.map((s: any) => s.data[0])).toEqual([[70, 3], [50, 4]]); + // Area proportional to size: the largest cluster gets the full radius. + expect(series[0].symbolSize).toBe(48); + expect(series[1].symbolSize).toBeCloseTo(12 + 36 * Math.sqrt(25 / 100), 6); }); - it('falls back to parallel coordinates for high-dimensional kmeans centers', () => { + it('appends an elbow curve when kmeans used elbow selection', () => { const charts = buildKMeansChart({ - centers: [ - [0.1, 0.2, 0.3, 0.4], - [0.5, 0.6, 0.7, 0.8], + variables: ['v1', 'v2'], + clusters: [ + { label: 'Cluster 0', center: { v1: 0.1, v2: 0.2 } }, + { label: 'Cluster 1', center: { v1: 0.5, v2: 0.6 } }, ], + elbow: { k_min: 2, k_max: 4, selected_k: 3, inertia_by_k: { '4': 10, '2': 30, '3': 15 }, warning: null }, }); - expect(charts.length).toBe(1); - expect((charts[0] as any).title?.text).toContain('Parallel Coordinates'); - expect((charts[0] as any).series?.length).toBe(2); + expect(charts.length).toBe(2); + expect((charts[1] as any).title?.text).toBe('Elbow Curve'); + expect((charts[1] as any).xAxis?.data).toEqual(['2', '3', '4']); + expect((charts[1] as any).series?.[0]?.data).toEqual([30, 15, 10]); }); it('builds class prior probabilities from Naive Bayes log priors', () => { @@ -69,4 +226,49 @@ describe('Chart renderers regressions', () => { expect(data[0]).toBeCloseTo(0.2, 3); expect(data[1]).toBeCloseTo(0.8, 3); }); + + it('places Gaussian naive Bayes class means in pooled SDs from the overall mean', () => { + const charts = buildNaiveBayesProfileChart({ + classes: ['no', 'yes'], + class_count: [1, 3], + feature_names: ['Score'], + theta: [[8], [4]], + var: [[4], [4]], + }); + + // Overall mean 5, pooled SD 2: "no" sits +1.5 SD, "yes" −0.5 SD. + const series = (charts[0] as any).series; + expect(series[0].data).toEqual([[1.5, 0]]); + expect(series[1].data).toEqual([[-0.5, 0]]); + }); + + it('draws a missing k-means centre value as missing, not NaN', () => { + const [heatmap] = buildKMeansChart({ + variables: ['a', 'b', 'c'], + clusters: [ + { label: 'Cluster 0', size_interval: '10-20', center: { a: 1, b: 2, c: 3 } }, + { label: 'Cluster 1', size_interval: '10-20', center: { a: 3, b: null, c: 5 } }, + ], + }) as any[]; + + const data = heatmap.series[0].data; + expect(data.every((cell: any) => cell.value.slice(0, 3).every((n: number) => Number.isFinite(n)))).toBe(true); + const missing = data.find((cell: any) => cell.value[0] === 1 && cell.value[1] === 1); + expect(missing.value[3]).toBeNull(); + expect(heatmap.series[0].label.formatter({ value: missing.value })).toBe('—'); + // Variable b's mean comes from the one cluster that reports it, so that cluster sits at it. + expect(data.find((cell: any) => cell.value[0] === 1 && cell.value[1] === 0).value[2]).toBe(0); + }); + + it('keeps each PCA variance share on its own component when an eigenvalue is missing', () => { + const charts = buildPCAHeatmapChart({ + eigenvectors: [[0.7, 0.7], [0.7, -0.7], [0.1, 0.2]], + eigenvalues: [3, null, 1], + variable_names: ['x', 'y'], + }) as any[]; + + const variance = charts.find((chart) => chart.title?.text === 'Variance explained'); + expect(variance.series[0].data).toEqual([75, null, 25]); + expect(variance.xAxis.data.map((label: string) => label.split('\n')[0])).toEqual(['PC1', 'PC2', 'PC3']); + }); }); diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/cluster-3d-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/cluster-3d-chart.ts deleted file mode 100644 index 2ffb98b4e..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/cluster-3d-chart.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { EChartsOption } from 'echarts'; - -export function build3DClusterChart(result: any): EChartsOption[] { - console.warn('[KMeans 3D] Not implemented yet'); - return []; -} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/confusion-matrix-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/confusion-matrix-chart.ts index acc6fffc1..0a6d6a3b4 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/confusion-matrix-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/confusion-matrix-chart.ts @@ -1,128 +1,128 @@ import { EChartsOption } from 'echarts'; +import { cellLabelColor } from '../chart-palette'; +import { escapeHtml } from '../../../../../core/html.utils'; +import { formatInteger } from '../../result-format'; -export function buildConfusionMatrixChart(result: any): EChartsOption[] { - let matrix: number[][] = []; - let classNames: string[] = []; +export interface ConfusionMatrix { + labels: string[]; + /** rows = actual class, columns = predicted class */ + matrix: number[][]; +} - const cm = result?.confusion_matrix; - if (!cm) return []; +/** + * Reads both Exaflow shapes — `{ data, labels }` and the binary `{ tp, fp, fn, tn }` — and drops + * classes that never occur on either side (an enumeration like "unknown" with an all-zero row + * and column says nothing about the model). + */ +export function readConfusionMatrix(cm: any): ConfusionMatrix | null { + let matrix: number[][] = []; + let labels: string[] = []; - // Format 1: { data: number[][], labels: string[] } - if (Array.isArray(cm.data) && Array.isArray(cm.labels)) { - matrix = cm.data; - classNames = cm.labels; - } - // Format 2: { tp, fp, fn, tn } - else if (cm.tp !== undefined && cm.fp !== undefined && cm.fn !== undefined && cm.tn !== undefined) { - // Rows = Actual [Positive, Negative], Columns = Predicted [Positive, Negative] + if (Array.isArray(cm?.data) && Array.isArray(cm?.labels)) { + matrix = cm.data.map((row: unknown[]) => (Array.isArray(row) ? row.map((value) => Number(value) || 0) : [])); + labels = cm.labels.map((label: unknown) => String(label)); + } else if (cm && cm.tp !== undefined && cm.fp !== undefined && cm.fn !== undefined && cm.tn !== undefined) { matrix = [ - [cm.tp, cm.fn], - [cm.fp, cm.tn] + [Number(cm.tp) || 0, Number(cm.fn) || 0], + [Number(cm.fp) || 0, Number(cm.tn) || 0], ]; - classNames = ['Positive', 'Negative']; + const positive = typeof cm.positive_label === 'string' && cm.positive_label ? cm.positive_label : null; + labels = positive ? [positive, `not ${positive}`] : ['Positive', 'Negative']; } - if (matrix.length === 0 || classNames.length === 0) { - console.warn('[ConfusionMatrix] Empty matrix or labels'); - return []; - } + if (!matrix.length || matrix.length !== labels.length) return null; - // Dimension Sanity Check - if (matrix.length !== classNames.length) { - console.warn(`[ConfusionMatrix] Dimension mismatch: ${matrix.length} rows vs ${classNames.length} labels`); - } + const keep = labels + .map((_, i) => i) + .filter((i) => matrix[i].some((value) => value !== 0) || matrix.some((row) => (row[i] ?? 0) !== 0)); + if (!keep.length) return null; - const heatmapData: [number, number, number][] = []; - for (let i = 0; i < matrix.length; i++) { - const row = matrix[i]; // row index i = Actual category index - if (!Array.isArray(row)) continue; - for (let j = 0; j < row.length; j++) { - // j = Predicted category index - heatmapData.push([j, i, row[j]]); // [col, row, value] -> [Predicted, Actual, Value] - } - } + return { + labels: keep.map((i) => labels[i]), + matrix: keep.map((i) => keep.map((j) => matrix[i][j] ?? 0)), + }; +} + +export function buildConfusionMatrixChart(result: any): EChartsOption[] { + const confusion = readConfusionMatrix(result?.confusion_matrix); + if (!confusion) return []; + const { labels, matrix } = confusion; + + // Shade by share of the actual class, so a large majority class does not wash out the rest. + const heatmapData = matrix.flatMap((row, i) => { + const total = row.reduce((sum, value) => sum + value, 0); + return row.map((count, j) => { + const share = total > 0 ? count / total : 0; + return { + value: [j, i, share], + count, + label: { color: cellLabelColor(share) }, + }; + }); + }); return [ { + mipChartHeight: 156 + 56 * labels.length, title: { text: 'Confusion Matrix', - subtext: 'Rows: Actual (True) | Columns: Predicted', + // Two short lines, so the caption fits a 360px compare column. + subtext: 'Rows: actual · columns: predicted\nShading: share of the actual class', left: 'center', - top: 0, - subtextStyle: { - color: '#666', - fontSize: 12, - }, }, tooltip: { position: 'top', formatter: (params: any) => { - const [predictedIdx, actualIdx, val] = params.value; - return `Actual (True): ${classNames[actualIdx]}<br/>Predicted: ${classNames[predictedIdx]}<br/>Count: ${val}`; + const [predictedIdx, actualIdx, share] = params.value; + const actual = escapeHtml(labels[actualIdx]); + return `Actual: ${actual}<br/>Predicted: ${escapeHtml(labels[predictedIdx])}<br/>Count: <b>${formatInteger(params.data.count)}</b> (${(share * 100).toFixed(1)}% of actual ${actual})`; }, }, - grid: { - height: '60%', // Slightly smaller to ensure title + legend fit well - top: 60, // More top room for title/subtitle - left: 40, // Safe left buffer - right: 80, - bottom: 110, // Ample room for 45 deg rotated labels - containLabel: true, - }, + grid: { top: 80, left: 28, right: 16, bottom: 40, containLabel: true }, xAxis: { type: 'category', - data: classNames, - nameGap: 55, - splitArea: { show: true }, - axisLabel: { - fontSize: 12, - rotate: 45, - interval: 0 - }, + data: labels, + name: 'Predicted', + nameLocation: 'middle', + nameGap: 28, + nameTextStyle: { color: '#475569', fontWeight: 600 }, + axisLine: { show: false }, + axisTick: { show: false }, + axisLabel: { color: '#475569', interval: 0 }, }, yAxis: { type: 'category', - data: classNames, - nameGap: 100, - splitArea: { show: true }, - axisLabel: { - fontSize: 12, - rotate: 0, - interval: 0 - } + data: labels, + inverse: true, + name: 'Actual', + nameLocation: 'middle', + nameGap: 56, + nameTextStyle: { color: '#475569', fontWeight: 600 }, + axisLine: { show: false }, + axisTick: { show: false }, + axisLabel: { color: '#475569', interval: 0 }, }, visualMap: { + show: false, min: 0, - max: (() => { - const flat = matrix.flat().filter(v => typeof v === 'number' && !isNaN(v)); - return flat.length > 0 ? Math.max(...flat) : 1; - })(), - calculable: true, - orient: 'vertical', - right: 0, - top: 'center', + max: 1, + inRange: { color: ['#f1f5f9', '#2b33e9'] }, }, series: [ { name: 'Confusion Matrix', type: 'heatmap', data: heatmapData, + itemStyle: { borderColor: '#ffffff', borderWidth: 2 }, label: { show: true, - formatter: (params: any) => `${params.value[2]}`, - color: '#000', - fontSize: 14, - }, - emphasis: { - itemStyle: { - borderColor: '#333', - borderWidth: 1, - }, + fontSize: 13, + // ECharts' default line height sets the count and its share line on top of each other. + lineHeight: 18, + formatter: (params: any) => `${formatInteger(params.data.count)}\n${(params.value[2] * 100).toFixed(1)}%`, }, }, ], - }, + } as EChartsOption, ]; } - - diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/correlation-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/correlation-chart.ts deleted file mode 100644 index 5727f2bf0..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/correlation-chart.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { EChartsOption } from 'echarts'; - -export function buildCorrelationChart(result: any): EChartsOption[] { - const correlations = result?.output?.correlations; - if (!correlations || !Array.isArray(correlations)) return []; - - const variablesSet = new Set<string>(); - - // Extract all variable names - for (const entry of correlations) { - entry.variables?.forEach((v: string) => variablesSet.add(v)); - } - const variables = Array.from(variablesSet); - - // Create matrix of values - const heatmapData = []; - - for (let i = 0; i < variables.length; i++) { - for (let j = 0; j < variables.length; j++) { - const var1 = variables[i]; - const var2 = variables[j]; - - const value = correlations.find(c => - c.variables.includes(var1) && c.variables.includes(var2) - )?.values?.[var1]?.[var2] ?? null; - - heatmapData.push([j, i, value]); - } - } - - return [ - { - tooltip: { - position: 'top', - }, - grid: { - height: '80%', - top: '10%', - }, - xAxis: { - type: 'category', - data: variables, - splitArea: { show: true }, - }, - yAxis: { - type: 'category', - data: variables, - splitArea: { show: true }, - }, - visualMap: { - min: -1, - max: 1, - calculable: true, - orient: 'horizontal', - left: 'center', - bottom: '5%', - }, - series: [ - { - name: 'Correlation', - type: 'heatmap', - data: heatmapData, - label: { show: true }, - emphasis: { - itemStyle: { - shadowBlur: 10, - shadowColor: 'rgba(0, 0, 0, 0.5)', - }, - }, - }, - ], - }, - ]; -} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/cv-metrics-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/cv-metrics-chart.ts deleted file mode 100644 index f2c7958ca..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/cv-metrics-chart.ts +++ /dev/null @@ -1,124 +0,0 @@ -import { EChartsOption } from 'echarts'; - -export function buildCVMetricsChart(result: any): EChartsOption[] { - // Supports: - // 1) Per-fold arrays (e.g. classification CV summary) - // 2) Mean/std objects (e.g. linear_regression_cv) - const data = result?.summary || result; - if (!data) return []; - - const metricDefs = [ - { key: 'mean_sq_error', label: 'MSE' }, - { key: 'r_squared', label: 'R²' }, - { key: 'mean_abs_error', label: 'MAE' }, - { key: 'f_stat', label: 'F-Stat' }, - { key: 'accuracy', label: 'Accuracy' }, - { key: 'precision', label: 'Precision' }, - { key: 'recall', label: 'Recall' }, - { key: 'fscore', label: 'F-Score' }, - ]; - - const foldCount = Array.isArray(data.n_obs) ? data.n_obs.length : 0; - const perFold = metricDefs - .map((m) => ({ ...m, values: data[m.key] })) - .filter((m) => Array.isArray(m.values) && m.values.length === foldCount) as Array<{ - key: string; - label: string; - values: number[]; - }>; - - if (perFold.length > 0 && foldCount > 0) { - const folds = data.n_obs.map((_: any, i: number) => `Fold ${i + 1}`); - return perFold.map((metric) => ({ - title: { - text: `${metric.label} per Fold`, - left: 'center', - }, - tooltip: { trigger: 'axis' }, - xAxis: { - type: 'category', - data: folds, - axisLabel: { interval: 0, rotate: 30 }, - }, - yAxis: { type: 'value', name: metric.label }, - series: [ - { - type: 'line', - data: metric.values, - symbol: 'circle', - symbolSize: 8, - label: { show: true, position: 'top', formatter: (p: any) => Number(p.value).toFixed(3) }, - }, - { - type: 'bar', - data: metric.values, - itemStyle: { opacity: 0.2 }, - }, - ], - })); - } - - const summaryMetrics = metricDefs - .map((m) => { - const raw = data[m.key]; - const mean = Number(raw?.mean); - const std = Number(raw?.std); - if (!Number.isFinite(mean) || !Number.isFinite(std)) return null; - return { label: m.label, mean, std }; - }) - .filter((m): m is { label: string; mean: number; std: number } => m !== null); - - if (summaryMetrics.length === 0) return []; - - return [ - { - title: { - text: 'Cross-Validation Metric Summary', - left: 'center', - }, - tooltip: { - trigger: 'axis', - axisPointer: { type: 'shadow' }, - }, - legend: { - top: 30, - }, - grid: { - top: 80, - left: 60, - right: 30, - bottom: 60, - }, - xAxis: { - type: 'category', - data: summaryMetrics.map((m) => m.label), - axisLabel: { interval: 0, rotate: 20 }, - }, - yAxis: { - type: 'value', - }, - series: [ - { - name: 'Mean', - type: 'bar', - data: summaryMetrics.map((m) => m.mean), - label: { - show: true, - position: 'top', - formatter: (p: any) => Number(p.value).toFixed(3), - }, - }, - { - name: 'Std Dev', - type: 'bar', - data: summaryMetrics.map((m) => m.std), - label: { - show: true, - position: 'top', - formatter: (p: any) => Number(p.value).toFixed(3), - }, - }, - ], - }, - ]; -} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/forest-plot-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/forest-plot-chart.ts deleted file mode 100644 index 70a5a1577..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/forest-plot-chart.ts +++ /dev/null @@ -1,175 +0,0 @@ -import { EChartsOption } from 'echarts'; - -export function buildForestPlotChart(result: any): EChartsOption[] { - // Supports Linear Regression and Logistic Regression - // Needs: variables (names), coefficients (values), lower_ci, upper_ci - - let variables: string[] = []; - let coefficients: number[] = []; - let lowerCi: number[] = []; - let upperCi: number[] = []; - let title = 'Forest Plot'; - - // Linear Regression - if (result.indep_vars && result.coefficients && result.lower_ci && result.upper_ci) { - variables = result.indep_vars; - coefficients = result.coefficients; - lowerCi = result.lower_ci; - upperCi = result.upper_ci; - title = 'Linear Regression Coefficients (95% CI)'; - } - // Logistic Regression - else if (result.summary && result.indep_vars) { - const s = result.summary; - variables = result.indep_vars; - coefficients = s.coefficients; - lowerCi = s.lower_ci; - upperCi = s.upper_ci; - title = 'Logistic Regression Coefficients (95% CI)'; - } else { - return []; - } - - if (!variables.length || variables.length !== coefficients.length) return []; - - const data = variables.map((v, i) => ({ - name: v, - value: coefficients[i], - low: lowerCi[i], - high: upperCi[i] - })).reverse(); // Reverse to have first variable at top of Y axis - - const option: EChartsOption = { - title: { - text: title, - left: 'center' - }, - tooltip: { - trigger: 'item', - formatter: (params: any) => { - const item = data[params.dataIndex]; - if (!item) return ''; - return `<b>${item.name}</b><br/> - Coef: ${item.value.toFixed(4)}<br/> - 95% CI: [${item.low.toFixed(4)}, ${item.high.toFixed(4)}]`; - } - }, - grid: { - top: 60, - left: 20, - right: 40, - bottom: 20, - containLabel: true - }, - yAxis: { - type: 'category', - data: data.map(d => d.name), - axisLine: { show: false }, - axisTick: { show: false }, - splitLine: { show: true, lineStyle: { type: 'dashed' } } - }, - xAxis: { - type: 'value', - scale: true, - axisLabel: { - formatter: (val: number) => val.toFixed(2) - }, - splitLine: { show: true } - }, - series: [ - { - type: 'custom', - name: 'Forest Plot', - renderItem: (params: any, api: any) => { - const index = params.dataIndex; // maps to category index in reversed data - const item = data[index]; // Use closure for simplicity as logic is coupled to 'data' order - - // Validations - if (item.value === undefined || item.low === undefined || item.high === undefined) return; - - const y = api.coord([0, index])[1]; // Y position for category - - // Coordinates using values from closure (which matches data passed below) - const xVal = api.coord([item.value, 0])[0]; - const xLow = api.coord([item.low, 0])[0]; - const xHigh = api.coord([item.high, 0])[0]; - - const height = api.size([0, 1])[1]; - const boxHeight = Math.min(height * 0.6, 20); - - return { - type: 'group', - children: [ - // Line for CI - { - type: 'line', - shape: { - x1: xLow, y1: y, - x2: xHigh, y2: y - }, - style: { - stroke: '#333', - lineWidth: 2 - } - }, - // Cap at Low - { - type: 'line', - shape: { - x1: xLow, y1: y - boxHeight / 2, - x2: xLow, y2: y + boxHeight / 2 - }, - style: { - stroke: '#333', - lineWidth: 2 - } - }, - // Cap at High - { - type: 'line', - shape: { - x1: xHigh, y1: y - boxHeight / 2, - x2: xHigh, y2: y + boxHeight / 2 - }, - style: { - stroke: '#333', - lineWidth: 2 - } - }, - // Point for Coef - { - type: 'circle', - shape: { - cx: xVal, cy: y, r: 5 - }, - style: { - fill: '#5470c6', - stroke: '#333' - } - } - ] - }; - }, - // Pass full data for auto-scaling - data: data.map((d, i) => [d.value, d.low, d.high, i]), - encode: { - x: [0, 1, 2], // Value, Low, High map to X axis - y: 3 // Index maps to Y axis - } - } - ] - }; - - // Add a vertical line at x=0 - (option.series as any[]).push({ - type: 'line', - markLine: { - silent: true, - symbol: 'none', - lineStyle: { color: '#ccc', type: 'solid' }, - data: [{ xAxis: 0 }] - } - }); - - return [option]; -} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/grouped-bar-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/grouped-bar-chart.ts index d3ea4976f..8800540a8 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/grouped-bar-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/grouped-bar-chart.ts @@ -1,18 +1,6 @@ import { EChartsOption } from 'echarts'; - -// Color palette for bar chart segments - MIP brand colors -const BAR_COLORS = [ - '#2b33e9', // MIP primary blue - '#026d6d', // MIP teal - '#ffba08', // MIP orange/yellow - '#06d6a0', // MIP green - '#ef476f', // accent pink - '#118ab2', // accent blue - '#073b4c', // dark teal - '#ffd166', // soft yellow - '#8338ec', // purple - '#ff6b6b', // coral -]; +import { CHART_SERIES_COLORS as BAR_COLORS } from '../chart-palette'; +import { escapeHtml } from '../../../../../core/html.utils'; /** * Builds a grouped bar chart for nominal variable distributions. @@ -47,9 +35,6 @@ export function buildGroupedBarChart( const remainingCategories = [...allCategories].filter(k => !orderedCategories.includes(k)); const categoryOrder = [...orderedCategories, ...remainingCategories]; - // Category labels for legend - const categoryLabels = categoryOrder.map(k => enumMap?.get(k) ?? k); - // Pre-calculate totals for each dataset const datasetTotals: Record<string, number> = {}; for (const entry of variableData) { @@ -96,7 +81,7 @@ export function buildGroupedBarChart( }; }); - const isDark = document.body.classList.contains('theme-dark'); + const isDark = false; const textColor = isDark ? '#f1f5f9' : '#0f172a'; const axisColor = isDark ? 'rgba(255, 255, 255, 0.3)' : '#475569'; const splitLineColor = isDark ? 'rgba(255, 255, 255, 0.05)' : '#e2e8f0'; @@ -122,11 +107,11 @@ export function buildGroupedBarChart( }, formatter: (params: any) => { const dataset = params[0]?.axisValue ?? ''; - let html = `<b>${dataset}</b><br/>`; + let html = `<b>${escapeHtml(dataset)}</b><br/>`; for (const p of params) { const pct = p.data?.value ?? p.value; const cnt = p.data?.count ?? 0; - html += `${p.marker} ${p.seriesName}: ${pct}% (n=${cnt})<br/>`; + html += `${p.marker} ${escapeHtml(p.seriesName)}: ${pct}% (n=${cnt})<br/>`; } return html; }, diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/histogram-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/histogram-chart.ts index 4e6786886..703c49761 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/histogram-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/histogram-chart.ts @@ -1,36 +1,25 @@ import { EChartsOption } from 'echarts'; +import { clipHistogramNullEdges, shouldClipNullEdges } from '../../histogram/histogram-chart'; export function buildHistogramChart(result: any): EChartsOption[] { const histogramData = result?.histogram; if (!Array.isArray(histogramData) || histogramData.length === 0) return []; - return histogramData.map((item: any) => { - const bins = item.bins || []; - const counts = item.counts || []; + return histogramData.flatMap((item: any) => { + const clipped = shouldClipNullEdges(item.bins || []) + ? clipHistogramNullEdges(item.bins || [], item.counts || []) + : { bins: (item.bins || []).map(String), counts: item.counts || [] }; + const bins = clipped.bins; + const counts = clipped.counts; + if (!bins.length || !counts.length) { + return []; + } const variable = item.var || 'Variable'; const group = item.grouping_enum ? ` (${item.grouping_enum})` : ''; const title = `Histogram: ${variable}${group}`; + const categories = bins.map(String); - // data for bar chart - // bins are edges? or centers? Usually bins are edges [0, 10, 20...] and counts are 1 less. - // Or bins are labels. - // If bins.length == counts.length + 1, they are edges. - // We can label bars with intervals. - - let categories: string[] = []; - if (bins.length === counts.length + 1) { - // Edges - categories = counts.map((_: any, i: number) => { - const start = typeof bins[i] === 'number' ? Number(bins[i]).toFixed(2) : bins[i]; - const end = typeof bins[i + 1] === 'number' ? Number(bins[i + 1]).toFixed(2) : bins[i + 1]; - return `[${start}, ${end})`; - }); - } else { - // Categories - categories = bins.map(String); - } - - return { + return [{ title: { text: title, left: 'center' @@ -51,7 +40,7 @@ export function buildHistogramChart(result: any): EChartsOption[] { axisTick: { alignWithLabel: true }, axisLabel: { rotate: 45, - interval: 0 // Show all labels if possible, or 'auto' + interval: 0 } }, yAxis: { @@ -62,14 +51,20 @@ export function buildHistogramChart(result: any): EChartsOption[] { { name: 'Count', type: 'bar', - barWidth: '90%', // Reduce gap - data: counts, + barWidth: '90%', + data: counts.map((count: number | null) => count ?? null), + itemStyle: { + borderRadius: [5, 5, 0, 0], + color: '#2B33E9', + }, label: { show: true, - position: 'top' + position: 'top', + formatter: (params: { value: number | null }) => + params.value === null ? '' : String(params.value), } } ] - }; + } as EChartsOption]; }); } diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/k-means-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/k-means-chart.ts index f3860a316..e9258ff89 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/k-means-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/k-means-chart.ts @@ -1,149 +1,238 @@ import { EChartsOption } from 'echarts'; +import { formatInteger, formatSummaryNumber, toFiniteNumber } from '../../result-format'; +import { DIVERGING_COLORS, cellLabelColor } from '../chart-palette'; +import { escapeHtml } from '../../../../../core/html.utils'; + +const AXIS_COLOR = '#475569'; export function buildKMeansChart(output: any): EChartsOption[] { - const centers = output?.centers; - const title = 'K-Means Centers'; + const clusters = output?.clusters; - if (!Array.isArray(centers) || centers.length === 0) { - console.warn('[KMeans] No centers provided.'); - return []; - } + if (!Array.isArray(clusters) || clusters.length === 0) return []; + + const variables: string[] = output.variables ?? []; + const names: string[] = clusters.map((c: any) => c.label); + // A centre value the engine did not send (or sent as null) stays null: drawn as missing, never NaN. + const centers: (number | null)[][] = clusters.map((c: any) => variables.map((v: string) => toFiniteNumber(c?.center?.[v]))); - const dims = centers[0].length; + const sizes: (number | null)[] = clusters.map((c: any) => intervalMidpoint(c.size_interval)); + // A range prints as its midpoint ("~66 245"); "<10" and an empty cluster's "0" print as sent. + const sizeTexts: (string | null)[] = clusters.map((c: any, i: number) => { + const interval = String(c.size_interval ?? '').trim(); + if (sizes[i] === null) return null; + return interval.includes('-') ? `~${formatInteger(Math.round(sizes[i]!))}` : interval; + }); + // Each cluster's name carries its size, so the centres table is not needed on screen. + const sizedNames = names.map((name, i) => (sizeTexts[i] === null ? name : `${name} · ${sizeTexts[i]}`)); + const charts = variables.length === 2 + ? buildKMeans2DChart(centers as [number | null, number | null][], names, sizedNames, variables, sizes, sizeTexts) + : buildKMeansCentresHeatmap(centers, sizedNames, variables, sizes); - if (dims === 2) { - return buildKMeans2DChart(centers, title); - } else if (dims === 3) { - return buildKMeans3DChart(centers, title); - } else { - return buildKMeansParallelCoordinatesChart(centers, title); + if (output.elbow) { + charts.push(buildKMeansElbowChart(output.elbow)); } -} -function buildKMeans2DChart(centers: [number, number][], title: string): EChartsOption[] { - const series = centers.map(([x, y]: [number, number], i: number) => ({ - name: `Cluster ${i + 1}`, - type: 'scatter', - data: [[x, y]], - symbolSize: 20, - label: { - show: true, - formatter: `Cluster ${i + 1}`, - position: 'top', - }, - })); + return charts; +} - return [ - { - title: { - text: title, - left: 'center', - }, - xAxis: { - type: 'value', - name: 'x', - }, - yAxis: { - type: 'value', - name: 'y', - }, - series: series as any, - }, - ]; +/** Cluster sizes arrive as privacy-safe intervals ("87520-87529", "<10"); the midpoint is close + * enough to scale a bubble or weight a mean. */ +function intervalMidpoint(interval: unknown): number | null { + const match = String(interval ?? '').match(/^\s*(\d+(?:\.\d+)?)\s*-\s*(\d+(?:\.\d+)?)\s*$/); + if (match) return (Number(match[1]) + Number(match[2])) / 2; + const below = String(interval ?? '').match(/^\s*<\s*(\d+(?:\.\d+)?)\s*$/); + if (below) return Number(below[1]) / 2; + const single = Number(interval); + return Number.isFinite(single) ? single : null; } -function buildKMeans3DChart(centers: [number, number, number][], title: string): EChartsOption[] { - const series: any[] = [ - { - type: 'scatter3D', - data: centers, - symbolSize: 20, - label: { - // show: true, - // formatter: (_: any, i: number) => `Cluster ${i + 1}`, - }, - }, - ]; +/** Two variables: one bubble per centre, its area proportional to the cluster's size. */ +function buildKMeans2DChart( + centers: [number | null, number | null][], + names: string[], + legendNames: string[], + variables: string[], + sizes: (number | null)[], + sizeTexts: (string | null)[] +): EChartsOption[] { + const largest = Math.max(1, ...sizes.map((size) => size ?? 0)); + const radius = (size: number | null) => (size === null ? 18 : 12 + 36 * Math.sqrt(size / largest)); + const axis = (name: string) => ({ + type: 'value' as const, + name, + nameLocation: 'middle' as const, + nameGap: 32, + scale: true, + // Room around the extreme centres so their bubbles do not sit on the axes. + boundaryGap: ['15%', '15%'] as [string, string], + nameTextStyle: { color: '#475569', fontWeight: 600 }, + axisLabel: { color: '#475569' }, + splitLine: { lineStyle: { color: '#e2e8f0' } }, + }); return [ { - title: { - text: `${title} (3D)`, - left: 'center', - }, - xAxis3D: { - type: 'value', - name: 'x', - }, - yAxis3D: { - type: 'value', - name: 'y', - }, - zAxis3D: { - type: 'value', - name: 'z', - }, - grid3D: { - boxWidth: 100, - boxDepth: 100, - light: { - main: { - intensity: 1.2, - }, - ambient: { - intensity: 0.3, - }, - }, - }, + mipChartHeight: 380, + title: { text: 'K-Means Centers', show: false }, + legend: { top: 0, textStyle: { color: '#475569' } }, tooltip: { - formatter: (params: any) => { - const [x, y, z] = params.value; - return `x: ${x}<br>y: ${y}<br>z: ${z}`; + trigger: 'item', + formatter: (p: any) => { + const size = sizeTexts[p.seriesIndex]; + return `${escapeHtml(names[p.seriesIndex])}<br/>${escapeHtml(variables[0])}: <b>${p.value[0].toFixed(2)}</b><br/>${escapeHtml(variables[1])}: <b>${p.value[1].toFixed(2)}</b>` + + (size === null ? '' : `<br/>${escapeHtml(size)} rows`); }, }, - series: series as any, // Type assertion to bypass TS type check + grid: { top: 64, left: 24, right: 32, bottom: 48, containLabel: true }, + xAxis: axis(variables[0]), + yAxis: { ...axis(variables[1]), nameLocation: 'end', nameGap: 14, nameTextStyle: { color: '#475569', fontWeight: 600, align: 'left' } }, + series: centers.map(([x, y], i) => ({ + name: legendNames[i], + type: 'scatter', + // A centre missing either coordinate has no place on the plot; its legend entry stays. + data: x === null || y === null ? [] : [[x, y]], + symbolSize: radius(sizes[i]), + itemStyle: { opacity: 0.85, borderColor: '#ffffff', borderWidth: 2 }, + label: { show: true, position: 'top', color: '#0f172a', formatter: names[i] }, + })) as any, }, ]; } -function buildKMeansParallelCoordinatesChart(centers: number[][], title: string): EChartsOption[] { - if (!centers.length || !Array.isArray(centers[0])) return []; +/** + * Three or more variables: one row per cluster, one column per variable, each cell the centre's + * value. Rows name the clusters, so no colour has to tell five or more lines apart (no categorical + * palette stays distinct across every pair at that count). Shading compares each centre with the + * variable's overall mean (centres weighted by cluster size), scaled per column because units differ. + */ +function buildKMeansCentresHeatmap( + centers: (number | null)[][], + names: string[], + variables: string[], + sizes: (number | null)[] +): EChartsOption[] { + if (!centers.length || !variables.length) return []; + + // An empty cluster ("0") weighs nothing; an unreadable size drops the weighting for every cluster. + const sized = sizes.every((size) => size !== null) && sizes.some((size) => size! > 0); + const weights = sized ? (sizes as number[]) : centers.map(() => 1); + // Each variable's mean over the clusters that report it; a missing centre shades neutral. + const deviations = variables.map((_, v) => { + const reported = centers.map((_center, c) => c).filter((c) => centers[c][v] !== null); + const totalWeight = reported.reduce((sum, c) => sum + weights[c], 0); + const mean = totalWeight > 0 ? reported.reduce((sum, c) => sum + weights[c] * centers[c][v]!, 0) / totalWeight : null; + const raw = centers.map((center) => (center[v] === null || mean === null ? null : center[v]! - mean)); + const largest = Math.max(0, ...raw.map((value) => Math.abs(value ?? 0))); + return raw.map((value) => (value !== null && largest > 0 ? value / largest : 0)); + }); - const dims = centers[0].length; - const parallelAxis = Array.from({ length: dims }, (_, i) => ({ - dim: i, - name: `Dim ${i + 1}`, - })); + const data = centers.flatMap((center, c) => + variables.map((_, v) => { + const shade = deviations[v][c]; + return { + value: [v, c, shade, center[v]], + label: { color: cellLabelColor(shade) }, + }; + }) + ); return [ { - title: { - text: `${title} (Parallel Coordinates)`, - left: 'center', - }, + mipChartHeight: 110 + 44 * centers.length, + // The section heading names the chart and the colour bar under it says what the shading means. + title: { text: 'Cluster centres', show: false }, tooltip: { - trigger: 'item', + position: 'top', + formatter: (p: any) => { + const [v, c, shade, value] = p.value; + if (value === null) return `${escapeHtml(names[c])}<br/>${escapeHtml(variables[v])}: not reported`; + const side = shade > 0 ? 'above' : shade < 0 ? 'below' : 'at'; + return `${escapeHtml(names[c])}<br/>${escapeHtml(variables[v])}: <b>${formatSummaryNumber(value)}</b><br/>${side} the overall mean`; + }, }, - legend: { - top: 30, - type: 'scroll', + grid: { top: 8, left: 8, right: 16, bottom: 48, containLabel: true }, + xAxis: { + type: 'category', + data: variables, + position: 'top', + axisLine: { show: false }, + axisTick: { show: false }, + // Narrow enough to wrap two-word names before they meet in a 360px compare column. + axisLabel: { color: AXIS_COLOR, interval: 0, width: 72, overflow: 'break', lineHeight: 15 }, }, - parallel: { - top: 90, - left: 70, - right: 70, - bottom: 60, + yAxis: { + type: 'category', + data: names, + inverse: true, + axisLine: { show: false }, + axisTick: { show: false }, + // Name over size ("Cluster 0" / "~66 245"): one long line got clipped at the chart's left edge. + axisLabel: { color: AXIS_COLOR, interval: 0, lineHeight: 16, formatter: (name: string) => name.replace(' · ', '\n') }, }, - parallelAxis, - series: centers.map((center, i) => ({ - type: 'parallel', - name: `Cluster ${i + 1}`, - data: [center], - lineStyle: { - width: 2, - opacity: 0.85, + visualMap: { + dimension: 2, + min: -1, + max: 1, + orient: 'horizontal', + left: 'center', + bottom: 4, + itemWidth: 12, + itemHeight: 160, + text: ['Above mean', 'Below mean'], + textStyle: { color: AXIS_COLOR }, + inRange: { color: DIVERGING_COLORS }, + }, + series: [ + { + name: 'Cluster centres', + type: 'heatmap', + data, + itemStyle: { borderColor: '#ffffff', borderWidth: 2 }, + label: { show: true, fontSize: 12, formatter: (p: any) => (p.value[3] === null ? '—' : formatSummaryNumber(p.value[3])) }, }, - })), - }, + ], + } as EChartsOption, ]; } + +function buildKMeansElbowChart(elbow: any): EChartsOption { + const sortedKs = Object.keys(elbow.inertia_by_k).sort((a, b) => Number(a) - Number(b)); + const selected = String(elbow.selected_k); + + return { + mipChartHeight: 340, + // The y-axis measure is the subtitle: as an axis name it ran into the title in a 360px column. + title: { + text: 'Elbow Curve', + subtext: 'Within-cluster sum of squares for each k', + left: 'center', + }, + tooltip: { + trigger: 'axis', + }, + grid: { top: 64, right: 32, bottom: 48, left: 88 }, + xAxis: { + type: 'category', + name: 'k', + data: sortedKs, + }, + yAxis: { + type: 'value', + }, + series: [ + { + type: 'line', + name: 'Within-cluster sum of squares', + data: sortedKs.map(k => elbow.inertia_by_k[k]), + symbolSize: 6, + markPoint: { + symbol: 'circle', + symbolSize: 12, + itemStyle: { borderColor: '#ffffff', borderWidth: 2 }, + label: { show: true, position: 'top', distance: 8, color: '#0f172a', fontWeight: 600, formatter: `k = ${selected}` }, + data: [{ coord: [selected, elbow.inertia_by_k[selected]], name: 'Selected k' }], + }, + }, + ], + } as EChartsOption; +} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/label-helper.util.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/label-helper.util.ts deleted file mode 100644 index 58b731210..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/label-helper.util.ts +++ /dev/null @@ -1,11 +0,0 @@ -export function prettifyLabel(label: string): string { - if (!label) return ''; - - return label - .replace(/_/g, ' ') // underscores → spaces - .replace(/([a-z])([A-Z])/g, '$1 $2') // split camelCase - .replace(/^r(?=[A-Z])/g, 'Right ') // rX → Right - .replace(/^l(?=[A-Z])/g, 'Left ') // lX → Left - .replace(/\s+/g, ' ') // cleanup multiple spaces - .trim(); -} diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/matrix-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/matrix-chart.ts index 2c300046f..4f7e1b585 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/matrix-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/matrix-chart.ts @@ -1,125 +1,133 @@ import { EChartsOption } from 'echarts'; +import { formatPValue } from '../../result-format'; +import { DIVERGING_COLORS, cellLabelColor } from '../chart-palette'; +import { escapeHtml } from '../../../../../core/html.utils'; -export function buildMatrixChart(result: any): EChartsOption[] { - const charts: EChartsOption[] = []; +const AXIS_COLOR = '#475569'; + +type Matrix = Record<string, unknown> & { variables?: string[] }; + +/** A Pearson result's correlation, p-value and CI matrices, read the same way by the chart and the pairs table. */ +export interface CorrelationMatrices { + correlations: Matrix; + pValues: Matrix | null; + ciLower: Matrix | null; + ciUpper: Matrix | null; + /** Column variables, in the order the engine sent them. */ + cols: string[]; + /** Row variables, in the order the engine sent them. */ + rows: string[]; + /** Rows and columns name the same variables: every pair appears twice and the diagonal is 1. */ + square: boolean; + /** One finite value of a matrix, or null. */ + cell: (matrix: Matrix | null, row: string, col: string) => number | null; +} - const pickMatrix = (...keys: string[]): any => { +export function readCorrelations(result: any): CorrelationMatrices | null { + const pickMatrix = (...keys: string[]): Matrix | null => { for (const key of keys) { const candidate = result?.[key]; - if (candidate && typeof candidate === 'object' && !Array.isArray(candidate)) { - return candidate; - } + if (candidate && typeof candidate === 'object' && !Array.isArray(candidate)) return candidate; } return null; }; - const generateChart = (matrix: any, title: string, min: number, max: number, colors?: string[]) => { - const cols: string[] = matrix?.variables ?? []; - const rows: string[] = Object.keys(matrix).filter(k => k !== 'variables'); - - if (!cols.length || !rows.length) return; - - const heatmapData: [number, number, number][] = []; - - for (let i = 0; i < rows.length; i++) { - const rowVar = rows[i]; - const values = matrix[rowVar]; - - if (!Array.isArray(values)) continue; - - for (let j = 0; j < cols.length; j++) { - const value = values[j]; - if (value !== null && value !== undefined) { - heatmapData.push([j, i, value]); - } - } - } - - charts.push({ - title: { - text: title, - left: 'center' - }, - tooltip: { position: 'top' }, - grid: { height: '65%', top: '15%', left: '30%', right: '10%', bottom: '25%' }, - xAxis: { - type: 'category', - data: cols, - splitArea: { show: true }, - nameLocation: 'middle', - nameGap: 50, - axisLabel: { - fontSize: 12, - rotate: 45, - interval: 0, - width: 100, - overflow: 'break' - } - }, - yAxis: { - type: 'category', - data: rows, - splitArea: { show: true }, - nameLocation: 'middle', - nameGap: 80, - axisLabel: { - fontSize: 12, - interval: 0, - width: 100, - overflow: 'break' - } - }, - visualMap: { - min, - max, - calculable: true, - orient: 'vertical', - left: '90%', - top: 'center', - inRange: colors ? { color: colors } : undefined - }, - series: [ - { - name: title, - type: 'heatmap', - data: heatmapData, - label: { - show: true, - fontSize: 14, - formatter: (params: any) => params.value[2]?.toFixed(2) - }, - emphasis: { - itemStyle: { - shadowBlur: 10, - shadowColor: 'rgba(0, 0, 0, 0.5)' - } - } - } - ] - }); - }; - - // 1. Correlation Matrix const correlations = pickMatrix('correlations'); - if (correlations) { - generateChart(correlations, 'Correlation Matrix', -1, 1); - } + if (!correlations) return null; + const cols: string[] = correlations.variables ?? []; + const rows = Object.keys(correlations).filter((key) => key !== 'variables'); + const cell = (matrix: Matrix | null, row: string, col: string): number | null => { + const values = matrix?.[row]; + const index = (matrix?.variables ?? cols).indexOf(col); + const value = Array.isArray(values) && index >= 0 ? Number(values[index]) : NaN; + return Number.isFinite(value) ? value : null; + }; + return { + correlations, + // Backwards compatibility: saved Pearson results may use historical p-value and CI aliases. + pValues: pickMatrix('p_values', 'p-values', 'pvalues'), + ciLower: pickMatrix('ci_lo', 'low_confidence_intervals'), + ciUpper: pickMatrix('ci_hi', 'high_confidence_intervals'), + cols, + rows, + square: rows.length === cols.length && rows.every((row) => cols.includes(row)), + cell, + }; +} - // 2. P-Values Matrix - const pValues = pickMatrix('p_values', 'p-values', 'pvalues'); - if (pValues) { - generateChart(pValues, 'P-Values', 0, 1, ['#d73027', '#fee090', '#ffffbf']); - } +/** + * One correlation heatmap. A square matrix keeps only the lower triangle (each pair once, + * no diagonal of 1s); a y-by-x matrix is drawn whole. p-values and the 95% CI travel in + * the tooltip, and the pairs table under the chart lists them in full. + */ +export function buildMatrixChart(result: any): EChartsOption[] { + const matrices = readCorrelations(result); + if (!matrices || !matrices.cols.length || !matrices.rows.length) return []; + const { correlations, pValues, ciLower, ciUpper, square, cell } = matrices; + const rows = square ? matrices.cols.slice(1) : matrices.rows; + const cols = square ? matrices.cols.slice(0, -1) : matrices.cols; - // 3. Confidence Intervals - const ciLower = pickMatrix('ci_lo', 'low_confidence_intervals'); - if (ciLower) { - generateChart(ciLower, 'Confidence Interval (Lower 95%)', -1, 1); - } - const ciUpper = pickMatrix('ci_hi', 'high_confidence_intervals'); - if (ciUpper) { - generateChart(ciUpper, 'Confidence Interval (Upper 95%)', -1, 1); - } + const data = rows.flatMap((row, i) => + cols.flatMap((col, j) => { + if (square && j > i) return []; + const r = cell(correlations, row, col); + if (r === null) return []; + return [{ value: [j, i, r], label: { color: cellLabelColor(r) } }]; + }) + ); - return charts; + return [{ + mipChartHeight: 110 + 48 * rows.length, + tooltip: { + position: 'top', + formatter: (params: any) => { + const [j, i, r] = params.value; + const row = rows[i]; + const col = cols[j]; + const lines = [`${escapeHtml(row)} × ${escapeHtml(col)}`, `r = <b>${r.toFixed(3)}</b>`]; + const lower = cell(ciLower, row, col); + const upper = cell(ciUpper, row, col); + if (lower !== null && upper !== null) lines.push(`95% CI ${lower.toFixed(3)} to ${upper.toFixed(3)}`); + const p = cell(pValues, row, col); + if (p !== null) lines.push(`p ${formatPValue(p).startsWith('<') ? '' : '= '}${formatPValue(p)}`); + return lines.join('<br/>'); + }, + }, + grid: { top: 16, left: 8, right: 24, bottom: 48, containLabel: true }, + xAxis: { + type: 'category', + data: cols, + axisLine: { show: false }, + axisTick: { show: false }, + // Slanted so neighbouring names never run together, even in a 360px compare column. + axisLabel: { color: AXIS_COLOR, interval: 0, rotate: 30, width: 110, overflow: 'truncate' }, + }, + yAxis: { + type: 'category', + data: rows, + inverse: true, + axisLine: { show: false }, + axisTick: { show: false }, + axisLabel: { color: AXIS_COLOR, interval: 0, width: 140, overflow: 'break' }, + }, + visualMap: { + min: -1, + max: 1, + orient: 'horizontal', + left: 'center', + bottom: 4, + itemWidth: 12, + itemHeight: 160, + text: ['r = +1', '−1'], + textStyle: { color: AXIS_COLOR }, + inRange: { color: DIVERGING_COLORS }, + }, + series: [{ + name: 'Pearson r', + type: 'heatmap', + data, + itemStyle: { borderColor: '#ffffff', borderWidth: 2 }, + label: { show: true, fontSize: 12, formatter: (params: any) => params.value[2]?.toFixed(2) }, + }], + } as EChartsOption]; } diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/mean-plot-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/mean-plot-chart.ts index 889752fab..d51f79bf9 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/mean-plot-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/mean-plot-chart.ts @@ -1,55 +1,123 @@ import { EChartsOption } from 'echarts'; +import { escapeHtml } from '../../../../../core/html.utils'; +import { groupThousands } from '../../result-format'; +interface IntervalPoint { + group: string; + mean: number; + low: number; + high: number; +} + +interface IntervalLabels { + x: string; + y: string; + /** Legend names of the point and its whisker, and the whisker's name in the tooltip. */ + point: string; + /** Name of the point's value in the tooltip ("Mean"). */ + value: string; + interval: string; + intervalShort: string; +} + +/** Group means with whiskers at ± 1 sample SD — Exaflow's ci_info "m-s"/"m+s" are mean ∓ SD, not a CI. */ export function buildMeanPlotChart(result: any): EChartsOption[] { const ciInfo = result?.ci_info; if (!ciInfo || !ciInfo.means || !ciInfo['m-s'] || !ciInfo['m+s']) return []; - const groups = Object.keys(ciInfo.means); + const data = Object.keys(ciInfo.means).map((group) => ({ + group, + mean: ciInfo.means[group], + low: ciInfo['m-s'][group], + high: ciInfo['m+s'][group], + })); - const data = groups.map((group) => { - const mean = ciInfo.means[group]; - const low = ciInfo['m-s'][group]; - const high = ciInfo['m+s'][group]; - return { group, mean, low, high }; + return intervalPlot(data, { + x: result?.anova_table?.x_label ?? 'Group', + y: result?.anova_table?.y_label ?? 'Mean ± SD', + point: 'Group mean', + value: 'Mean', + interval: '± 1 standard deviation', + intervalShort: '± 1 SD', }); +} - // labels anova_table - const xLabel = result?.anova_table?.x_label ?? 'Group'; - const yLabel = result?.anova_table?.y_label ?? 'Mean ± CI'; +/** + * Independent t-test: the mean difference (group A − group B) with its CI as one vertical + * interval, value on the y axis, with a dashed zero line when the interval reaches it. The CI's + * level is (1 − alpha) of the run, which the chart does not know; the key figure states it. + */ +export function buildTTestIntervalChart(result: any): EChartsOption[] { + const [mean, low, high] = [result?.mean_diff, result?.ci_lower, result?.ci_upper].map(Number); + if (![mean, low, high].every(Number.isFinite)) return []; + const [option] = intervalPlot([{ group: 'Mean difference', mean, low, high }], { + x: '', + y: 'Difference in means', + point: 'Mean difference', + value: 'Difference', + interval: 'Confidence interval', + intervalShort: 'CI', + }, 320); + if (low <= 0 && high >= 0) { + (option.series as any[])[0].markLine = { + silent: true, + symbol: 'none', + label: { show: false }, + lineStyle: { color: '#94a3b8', type: 'dashed' }, + data: [{ yAxis: 0 }], + }; + } + return [option]; +} +/** Points with vertical whiskers, one per category. */ +function intervalPlot(data: IntervalPoint[], labels: IntervalLabels, height = 380): EChartsOption[] { const fmt = (v: number) => - Number.isFinite(v) - ? (Number.isInteger(v) ? String(v) : v.toFixed(3).replace(/\.?0+$/, '')) - : 'N/A'; - - const minY = Math.min(...data.map(d => d.low)); - const maxY = Math.max(...data.map(d => d.high)); - const span = Math.max(1e-6, maxY - minY); - const margin = span * 0.1; + Number.isFinite(v) ? groupThousands(v.toLocaleString('en-US', { maximumFractionDigits: 3 })) : 'N/A'; const meanMap: Record<string, number> = Object.fromEntries(data.map(d => [d.group, d.mean])); + const meanColor = '#2b33e9'; + const intervalColor = '#7f9ce8'; + const axisColor = '#64748b'; + const textColor = '#0f172a'; - const option: EChartsOption = { - title: { - text: 'Mean Plot', - left: 'center' + const option: EChartsOption & { mipChartHeight: number } = { + mipChartHeight: height, + color: [meanColor, intervalColor], + backgroundColor: 'transparent', + legend: { + top: 0, + left: 'center', + itemGap: 18, + itemWidth: 18, + itemHeight: 10, + textStyle: { + color: axisColor, + fontSize: 12, + fontWeight: 600, + }, + data: [labels.point, labels.interval], }, - grid: { top: 48, right: 24, bottom: 64, left: 100 }, + grid: { top: 44, right: 32, bottom: 64, left: 96 }, tooltip: { trigger: 'item', + backgroundColor: 'rgba(255, 255, 255, 0.96)', + borderColor: 'rgba(43, 51, 233, 0.16)', + borderWidth: 1, + textStyle: { color: textColor }, formatter: (p: any) => { if (p.seriesType === 'scatter') { const group = p.name ?? (Array.isArray(p.value) ? p.value[0] : ''); const mean = Array.isArray(p.value) ? p.value[1] : meanMap[group]; - return `${group}<br/>Mean: <b>${fmt(mean)}</b>`; + return `${escapeHtml(group)}<br/>${labels.value}: <b>${fmt(mean)}</b>`; } if (p.seriesType === 'custom' && Array.isArray(p.value)) { - const group = p.value[0]; + const group = p.value[4]; const low = p.value[1]; const high = p.value[2]; const mean = meanMap[group]; - return `${group}<br/>Mean: <b>${fmt(mean)}</b><br/>CI: <b>[${fmt(low)} – ${fmt(high)}]</b>`; + return `${escapeHtml(group)}<br/>${labels.value}: <b>${fmt(mean)}</b><br/>${labels.intervalShort}: <b>${fmt(low)} – ${fmt(high)}</b>`; } return ''; } @@ -57,36 +125,65 @@ export function buildMeanPlotChart(result: any): EChartsOption[] { xAxis: { type: 'category', data: data.map(d => d.group), - name: xLabel, + name: labels.x, nameLocation: 'middle', nameGap: 40, - axisLine: { lineStyle: { color: '#475569' } }, // MIP text-muted + axisLine: { lineStyle: { color: '#cbd5e1' } }, axisTick: { show: false }, splitLine: { show: false }, axisLabel: { + color: axisColor, + fontWeight: 600, formatter: (val: string) => val, - } + }, + nameTextStyle: { + color: axisColor, + fontWeight: 700, + }, }, yAxis: { type: 'value', - name: yLabel, + name: labels.y, nameLocation: 'middle', nameGap: 60, - min: Math.floor(minY - margin), - max: Math.ceil(maxY + margin), + scale: true, axisPointer: { show: false }, + axisLine: { show: false }, + axisTick: { show: false }, + splitLine: { + lineStyle: { + color: 'rgba(148, 163, 184, 0.22)', + type: 'dashed', + }, + }, axisLabel: { - formatter: (value: number) => (value < minY ? '' : value.toFixed(0)) - } + color: axisColor, + }, + nameTextStyle: { + color: axisColor, + fontWeight: 700, + }, }, series: [ { + name: labels.point, type: 'scatter', data: data.map(d => ({ name: d.group, value: [d.group, d.mean] })), - symbolSize: 12, + symbolSize: 14, + itemStyle: { + color: meanColor, + borderColor: '#ffffff', + borderWidth: 2, + shadowBlur: 8, + shadowColor: 'rgba(43, 51, 233, 0.28)', + }, label: { show: true, position: 'top', + color: textColor, + fontSize: 11, + fontWeight: 700, + distance: 8, formatter: (params: any) => { const raw = params?.data?.value; const value = Array.isArray(raw) ? raw[1] : raw; @@ -95,6 +192,7 @@ export function buildMeanPlotChart(result: any): EChartsOption[] { } }, { + name: labels.interval, type: 'custom', renderItem: (_params: any, api: any) => { const index = api.value(0) as number; @@ -107,19 +205,31 @@ export function buildMeanPlotChart(result: any): EChartsOption[] { const high = api.coord([category, highVal])[1]; const band = api.size([1, 0])[0] ?? 20; - const capWidth = Math.max(10, Math.min(24, band * 0.4)); + const capWidth = Math.max(12, Math.min(30, band * 0.36)); return { type: 'group', children: [ - { type: 'line', shape: { x1: x, y1: low, x2: x, y2: high }, style: { stroke: '#000', lineWidth: 1.5 } }, - { type: 'line', shape: { x1: x - capWidth / 2, y1: high, x2: x + capWidth / 2, y2: high }, style: { stroke: '#000', lineWidth: 1.5 } }, - { type: 'line', shape: { x1: x - capWidth / 2, y1: low, x2: x + capWidth / 2, y2: low }, style: { stroke: '#000', lineWidth: 1.5 } } + { + type: 'line', + shape: { x1: x, y1: low, x2: x, y2: high }, + style: { stroke: intervalColor, lineWidth: 3, opacity: 0.9 } + }, + { + type: 'line', + shape: { x1: x - capWidth / 2, y1: high, x2: x + capWidth / 2, y2: high }, + style: { stroke: intervalColor, lineWidth: 3, opacity: 0.9 } + }, + { + type: 'line', + shape: { x1: x - capWidth / 2, y1: low, x2: x + capWidth / 2, y2: low }, + style: { stroke: intervalColor, lineWidth: 3, opacity: 0.9 } + } ] }; }, encode: { x: 0, y: [1, 2] }, - data: data.map((d, i) => [i, d.low, d.high]), + data: data.map((d, i) => [i, d.low, d.high, d.mean, d.group]), } diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/naive-bayes-priors-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/naive-bayes-priors-chart.ts index 72ea5327d..bba754ea4 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/naive-bayes-priors-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/naive-bayes-priors-chart.ts @@ -1,4 +1,6 @@ import { EChartsOption } from 'echarts'; +import { escapeHtml } from '../../../../../core/html.utils'; +import { formatInteger } from '../../result-format'; function normalize(values: number[]): number[] { const safe = values.map((v) => (Number.isFinite(v) ? v : 0)); @@ -22,12 +24,15 @@ export function buildNaiveBayesPriorsChart(result: any): EChartsOption[] { } if (!priors.length || priors.length !== classes.length) return []; + const counts: unknown[] = Array.isArray(result?.class_count) ? result.class_count : []; + // One horizontal bar per class: a handful of shares does not need a 500px column chart. return [ { + mipChartHeight: 72 + 40 * classes.length, title: { text: 'Class Prior Probabilities', - left: 'center', + show: false, }, tooltip: { trigger: 'axis', @@ -35,44 +40,123 @@ export function buildNaiveBayesPriorsChart(result: any): EChartsOption[] { formatter: (params: any) => { const p = Array.isArray(params) ? params[0] : params; const value = Number(p?.value); - return `${p?.name}<br/>Prior: ${(value * 100).toFixed(2)}%`; + return `${escapeHtml(p?.name)}<br/>Prior: ${(value * 100).toFixed(2)}%`; }, }, grid: { - top: 56, - left: 56, - right: 28, - bottom: 56, + top: 8, + left: 8, + right: 120, + bottom: 28, + containLabel: true, }, - xAxis: { + yAxis: { type: 'category', data: classes, - axisLabel: { - interval: 0, - rotate: classes.length > 6 ? 25 : 0, - }, + inverse: true, + axisTick: { show: false }, + axisLabel: { color: '#475569', interval: 0 }, }, - yAxis: { + xAxis: { type: 'value', - name: 'Probability', min: 0, max: 1, + axisLabel: { color: '#475569', formatter: (value: number) => `${Math.round(value * 100)}%` }, + splitLine: { lineStyle: { color: '#e2e8f0' } }, }, series: [ { type: 'bar', data: priors, - barMaxWidth: 48, + barMaxWidth: 22, label: { show: true, - position: 'top', - formatter: (p: any) => `${(Number(p.value) * 100).toFixed(1)}%`, + position: 'right', + color: '#0f172a', + // Prior and training count together, so the class summary table is not needed on screen. + formatter: (p: any) => { + const count = Number(counts[p.dataIndex]); + const share = `${(Number(p.value) * 100).toFixed(1)}%`; + return Number.isFinite(count) ? `${share} · ${formatInteger(count)} rows` : share; + }, }, itemStyle: { color: '#2b33e9', + borderRadius: [0, 4, 4, 0], }, }, ], - }, + } as EChartsOption, + ]; +} + +/** + * Gaussian naive Bayes class profiles: for each feature, each class's mean expressed in pooled + * within-class SDs from the overall mean, (θ_c − μ) / s_pooled. One shared scale makes features + * in different units comparable and shows which ones separate the classes. + */ +export function buildNaiveBayesProfileChart(result: any): EChartsOption[] { + const classes: string[] = Array.isArray(result?.classes) ? result.classes.map(String) : []; + const features: string[] = Array.isArray(result?.feature_names) ? result.feature_names.map(String) : []; + const theta: number[][] = Array.isArray(result?.theta) ? result.theta : []; + const variance: number[][] = Array.isArray(result?.var) ? result.var : []; + const counts: number[] = Array.isArray(result?.class_count) ? result.class_count.map(Number) : []; + if (!classes.length || !features.length || theta.length !== classes.length || variance.length !== classes.length) return []; + + const total = counts.reduce((sum, n) => sum + (Number.isFinite(n) ? n : 0), 0); + const weight = (c: number) => (total > 0 ? counts[c] / total : 1 / classes.length); + const standardized = features.map((_, f) => { + const mean = classes.reduce((sum, _c, c) => sum + weight(c) * Number(theta[c][f]), 0); + const pooledSd = Math.sqrt(classes.reduce((sum, _c, c) => sum + weight(c) * Number(variance[c][f]), 0)); + return classes.map((_c, c) => (pooledSd > 0 ? (Number(theta[c][f]) - mean) / pooledSd : 0)); + }); + if (standardized.flat().some((value) => !Number.isFinite(value))) return []; + + return [ + { + mipChartHeight: 112 + 44 * features.length, + title: { + text: 'Class profiles', + subtext: 'Class mean minus overall mean, in pooled within-class SDs', + left: 'center', + }, + legend: { bottom: 0, textStyle: { color: '#475569' } }, + tooltip: { + trigger: 'item', + formatter: (p: any) => { + const f = p.dataIndex; + const c = p.seriesIndex; + return `${escapeHtml(features[f])} · ${escapeHtml(classes[c])}<br/>Mean: <b>${Number(theta[c][f]).toFixed(2)}</b> (SD ${Math.sqrt(Number(variance[c][f])).toFixed(2)})<br/>${p.value[0] >= 0 ? '+' : '−'}${Math.abs(p.value[0]).toFixed(2)} SD from the overall mean`; + }, + }, + // Room above the first row for its class-name labels. + grid: { top: 72, left: 8, right: 24, bottom: 36, containLabel: true }, + xAxis: { + type: 'value', + axisLabel: { color: '#475569', formatter: (v: number) => `${v > 0 ? '+' : ''}${v}` }, + splitLine: { lineStyle: { color: '#e2e8f0' } }, + }, + yAxis: { + type: 'category', + data: features, + inverse: true, + axisTick: { show: false }, + axisLabel: { color: '#475569' }, + splitLine: { show: true, lineStyle: { color: '#f1f5f9' } }, + }, + series: classes.map((name, c) => ({ + name, + type: 'scatter', + symbolSize: 12, + itemStyle: { borderColor: '#ffffff', borderWidth: 2 }, + // Class names on the first row's dots, so colour alone never tells the classes apart. + label: { show: true, position: 'top', color: '#0f172a', formatter: (p: any) => (p.dataIndex === 0 ? name : '') }, + labelLayout: { hideOverlap: true }, + data: features.map((_, f) => [standardized[f][c], f]), + ...(c === 0 + ? { markLine: { silent: true, symbol: 'none', label: { show: false }, lineStyle: { color: '#94a3b8', type: 'dashed' }, data: [{ xAxis: 0 }] } } + : {}), + })), + } as EChartsOption, ]; } diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/pca-heatmap-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/pca-heatmap-chart.ts index e2fc9786b..2d8957787 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/pca-heatmap-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/pca-heatmap-chart.ts @@ -1,4 +1,18 @@ import { EChartsOption } from "echarts"; +import { CHART_SERIES_COLORS, DIVERGING_COLORS, cellLabelColor } from '../chart-palette'; +import { escapeHtml } from '../../../../../core/html.utils'; + +const AXIS_COLOR = '#475569'; +const TEXT_COLOR = '#0f172a'; + +/** Share of total variance per component, in percent, at the component's own index: + * a missing eigenvalue is null there, so later components keep their shares. */ +function varianceShares(eigenvalues: unknown): (number | null)[] { + if (!Array.isArray(eigenvalues)) return []; + const values = eigenvalues.map((value) => (value === null || value === undefined || value === '' ? NaN : Number(value))); + const total = values.filter((value) => Number.isFinite(value)).reduce((sum, value) => sum + value, 0); + return total > 0 ? values.map((value) => (Number.isFinite(value) ? (value / total) * 100 : null)) : []; +} export function buildPCAHeatmapChart(result: any): EChartsOption[] { const matrix = result?.eigenvectors; @@ -29,140 +43,146 @@ export function buildPCAHeatmapChart(result: any): EChartsOption[] { variableNames = [...variableNames, ...padding]; } } - const componentNames = Array.from({ length: nComponents }, (_, i) => `PC${i + 1}`); - const heatmapData: [number, number, number][] = []; + const shares = varianceShares(result?.eigenvalues); + const componentNames = Array.from({ length: nComponents }, (_, i) => `PC${i + 1}`); + const componentLabels = componentNames.map((name, i) => + shares[i] == null ? name : `${name} · ${shares[i]!.toFixed(1)}%` + ); - for (let i = 0; i < nComponents; i++) { - for (let j = 0; j < nVariables; j++) { - const value = matrix[i][j]; - heatmapData.push([j, i, value]); - } - } + // Variables as rows and components as columns: long variable names wrap on the left, and + // the short component headers fit even a 360px compare column. + const componentHeaders = componentNames.map((name, i) => + shares[i] == null ? name : `${name}\n${shares[i]!.toFixed(1)}%` + ); + const heatmapData = matrix.flatMap((row: number[], component: number) => + row.map((value, variable) => ({ + value: [component, variable, value], + label: { color: cellLabelColor(value) }, + })) + ); const charts: EChartsOption[] = []; - // 1. Loadings Heatmap charts.push({ + mipChartHeight: 150 + 40 * nVariables, title: { - text: 'Eigenvectors', - left: 'center' + // Unit eigenvectors, not loadings (eigenvector × √eigenvalue): named for what they are. + text: 'Component weights', + subtext: "Each variable's eigenvector weight per component", + left: 'center', }, tooltip: { position: 'top', formatter: (params: any) => { - const [x, y, val] = params.value; - return `PC: ${componentNames[y]}<br/>Var: ${variableNames[x]}<br/>Weight: ${val.toFixed(3)}`; + const [component, variable, val] = params.value; + return `${componentLabels[component]}<br/>${escapeHtml(variableNames[variable])}: <b>${val.toFixed(3)}</b>`; }, }, grid: { - height: '65%', - top: '15%', - left: '10%', - right: '10%', - bottom: '25%', + top: 84, + left: 8, + right: 16, + bottom: 56, containLabel: true, }, xAxis: { type: 'category', - data: variableNames, - splitArea: { show: true }, - axisLabel: { - fontSize: 12, - rotate: 45, - interval: 0, - width: 100, - overflow: 'break' - }, + data: componentHeaders, + position: 'top', + axisLine: { show: false }, + axisTick: { show: false }, + axisLabel: { color: AXIS_COLOR, interval: 0, lineHeight: 15 }, }, yAxis: { type: 'category', - data: componentNames, - splitArea: { show: true }, - axisLabel: { - fontSize: 12, // Consistent font size - interval: 0 - } + data: variableNames, + inverse: true, + axisLine: { show: false }, + axisTick: { show: false }, + axisLabel: { color: AXIS_COLOR, interval: 0, width: 120, overflow: 'break' }, }, visualMap: { min: -1, max: 1, - calculable: true, - orient: 'vertical', - left: '95%', - top: 'center', + orient: 'horizontal', + left: 'center', + bottom: 4, + itemWidth: 12, + itemHeight: 160, + text: ['+1', '−1'], + textStyle: { color: AXIS_COLOR }, + inRange: { color: DIVERGING_COLORS }, }, series: [ { - name: 'Eigenvector weights', + name: 'Component weights', type: 'heatmap', data: heatmapData, + itemStyle: { borderColor: '#ffffff', borderWidth: 2 }, label: { show: true, - fontSize: 14, + fontSize: 12, formatter: (params: any) => params.value[2]?.toFixed(2), }, - emphasis: { - itemStyle: { - shadowBlur: 10, - shadowColor: 'rgba(0, 0, 0, 0.5)', - }, - }, }, ], - }); + } as EChartsOption); - // 2. Scree Plot (Eigenvalues) - const eigenvalues = result?.eigenvalues; - if (Array.isArray(eigenvalues) && eigenvalues.length > 0) { + if (shares.some((share) => share !== null)) { + let running = 0; + const cumulative = shares.map((share) => (share === null ? null : (running += share))); + const round1 = (value: number | null) => (value === null ? null : Math.round(value * 10) / 10); + // One bar per eigenvalue, named by its index, so a gap leaves the components after it in place. + const varianceComponents = shares.map((_, i) => `PC${i + 1}`); charts.push({ + mipChartHeight: 340, title: { - text: 'Scree Plot (Eigenvalues)', - left: 'center' + text: 'Variance explained', + left: 'center', }, + legend: { top: 32, data: ['Per component', 'Cumulative'], textStyle: { color: AXIS_COLOR } }, tooltip: { trigger: 'axis', - axisPointer: { type: 'shadow' } - }, - grid: { - height: '65%', - top: '15%', - left: '10%', - right: '10%', - bottom: '15%', - containLabel: true + axisPointer: { type: 'shadow' }, + valueFormatter: (value: any) => (Number.isFinite(Number(value)) && value !== null ? `${Number(value).toFixed(1)}%` : '—'), }, + grid: { top: 80, left: 8, right: 16, bottom: 12, containLabel: true }, xAxis: { type: 'category', - data: componentNames, - axisLabel: { interval: 0, rotate: 30 } + // Eigenvalue under each component, so the PCA summary table is not needed on screen. + data: varianceComponents.map((name, i) => { + const eigenvalue = Number(result.eigenvalues[i]); + return Number.isFinite(eigenvalue) ? `${name}\nλ ${eigenvalue.toFixed(2)}` : name; + }), + axisLabel: { color: AXIS_COLOR, interval: 0, lineHeight: 16 }, }, yAxis: { type: 'value', - name: 'Eigenvalue', - nameLocation: 'middle', - nameGap: 50 + max: 100, + axisLabel: { color: AXIS_COLOR, formatter: '{value}%' }, + splitLine: { lineStyle: { color: '#e2e8f0' } }, }, series: [ { - name: 'Eigenvalue', + name: 'Per component', type: 'bar', - data: eigenvalues.map((v: number) => parseFloat(v.toFixed(3))), - itemStyle: { color: '#5470c6' }, - label: { - show: true, - position: 'top' - } + barMaxWidth: 48, + data: shares.map(round1), + itemStyle: { color: CHART_SERIES_COLORS[0] }, + // One decimal like the weights' column headers: "14.0%", not "14%". + label: { show: true, position: 'top', color: TEXT_COLOR, formatter: (p: any) => (Number.isFinite(Number(p.value)) && p.value !== null ? `${Number(p.value).toFixed(1)}%` : '') }, }, { - name: 'Eigenvalue (Line)', + name: 'Cumulative', type: 'line', - data: eigenvalues.map((v: number) => parseFloat(v.toFixed(3))), - itemStyle: { color: '#ee6666' }, + data: cumulative.map(round1), + itemStyle: { color: CHART_SERIES_COLORS[2] }, + lineStyle: { color: CHART_SERIES_COLORS[2] }, symbol: 'circle', - symbolSize: 8 - } - ] - }); + symbolSize: 8, + }, + ], + } as EChartsOption); } return charts; diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/roc-curve-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/roc-curve-chart.ts index 6b85ebfb3..e78afb723 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/roc-curve-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/roc-curve-chart.ts @@ -1,5 +1,6 @@ import { EChartsOption } from "echarts"; import type { LineSeriesOption } from 'echarts'; +import { escapeHtml } from '../../../../../core/html.utils'; export function buildRocCurveChart(result: any): EChartsOption[] { @@ -10,13 +11,15 @@ export function buildRocCurveChart(result: any): EChartsOption[] { } const series: LineSeriesOption[] = []; + // Short legend entries ("Fold 1 · 0.864", the AUC after the dot) so five folds fit one row. + const aucByName: Record<string, string> = {}; for (const fold of rocCurves) { const fpr = fold.fpr; const tpr = fold.tpr; const auc = fold.auc; - const name = fold.name || `Fold`; - const label = (typeof auc === 'number' && !isNaN(auc)) ? `${name} (AUC: ${auc.toFixed(3)})` : name; + const name = String(fold.name || 'Fold').replace(/^fold_?(\d+)$/i, 'Fold $1'); + if (typeof auc === 'number' && !isNaN(auc)) aucByName[name] = auc.toFixed(3); if (!Array.isArray(fpr) || !Array.isArray(tpr) || fpr.length !== tpr.length) { console.warn(`[ROC Chart] Invalid data for ${name}`); @@ -26,10 +29,9 @@ export function buildRocCurveChart(result: any): EChartsOption[] { const points = fpr.map((x, i) => [x, tpr[i]]); series.push({ - name: label, + name, type: 'line' as const, data: points, - smooth: true, showSymbol: false, emphasis: { focus: 'series', @@ -37,15 +39,31 @@ export function buildRocCurveChart(result: any): EChartsOption[] { }); } + // Chance line: a classifier that guesses sits on the diagonal (AUC 0.5). + series.push({ + name: 'Chance', + type: 'line' as const, + data: [[0, 0], [1, 1]], + showSymbol: false, + silent: true, + lineStyle: { color: '#94a3b8', type: 'dashed', width: 1 }, + itemStyle: { color: '#94a3b8' }, + tooltip: { show: false }, + }); + return [ { + mipChartHeight: 440, title: { text: 'ROC Curve', left: 'center', }, grid: { containLabel: true, - bottom: '20%', + top: 76, + left: 40, + right: 24, + bottom: 32, }, xAxis: { type: 'value', @@ -65,14 +83,17 @@ export function buildRocCurveChart(result: any): EChartsOption[] { }, tooltip: { trigger: 'axis', - formatter: (params: any) => { - const p = Array.isArray(params) ? params[0] : params; - const { seriesName, value } = p; - return `${seriesName}<br/>FPR: ${value[0].toFixed(2)}<br/>TPR: ${value[1].toFixed(2)}`; - }, + // Every fold's nearest point, not only the first fold's. + formatter: (params: any) => (Array.isArray(params) ? params : [params]) + .filter((p: any) => p.seriesName !== 'Chance') + .map((p: any) => `${p.marker}${escapeHtml(p.seriesName)}: FPR ${p.value[0].toFixed(2)}, TPR ${p.value[1].toFixed(2)}`) + .join('<br/>'), }, legend: { - top: 'bottom', + top: 32, + type: 'scroll', + textStyle: { color: '#475569' }, + formatter: (name: string) => (aucByName[name] ? `${name} · ${aucByName[name]}` : name), }, series, }, diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/svm-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/svm-chart.ts index 2108a5955..de1384a24 100644 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/svm-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/charts/renderers/svm-chart.ts @@ -1,4 +1,5 @@ import { EChartsOption } from 'echarts'; +import { escapeHtml } from '../../../../../core/html.utils'; /** * Scientific SVM visualization: @@ -43,7 +44,7 @@ export function buildSVMChart(result: any): EChartsOption[] { formatter: (params: any) => { const p = params[0]; return ` - <strong>Range:</strong> ${p.name}<br> + <strong>Range:</strong> ${escapeHtml(p.name)}<br> <strong>Count:</strong> ${p.value} `; }, diff --git a/src/app/pages/experiment-studio/visualisations/charts/renderers/t-test-chart.ts b/src/app/pages/experiment-studio/visualisations/charts/renderers/t-test-chart.ts deleted file mode 100644 index 655ea4414..000000000 --- a/src/app/pages/experiment-studio/visualisations/charts/renderers/t-test-chart.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { EChartsOption } from 'echarts'; - -export function buildTTestChart(result: any): EChartsOption[] { - // Visualizes Mean Difference with 95% CI - const meanDiff = Number(result?.mean_diff); - const lower = Number(result?.ci_lower); - const upper = Number(result?.ci_upper); - const pValue = Number(result?.p); - - if (!Number.isFinite(meanDiff) || !Number.isFinite(lower) || !Number.isFinite(upper)) return []; - - const title = `Mean Difference (95% CI)\np-value: ${Number.isFinite(pValue) ? pValue.toExponential(3) : 'N/A'}`; - - const option: EChartsOption = { - title: { - text: title, - left: 'center', - textStyle: { fontSize: 14 } - }, - tooltip: { - show: true, - formatter: () => { - return `<b>Mean Difference</b>: ${meanDiff.toFixed(4)}<br/>` + - `<b>95% CI</b>: [${lower.toFixed(4)}, ${upper.toFixed(4)}]`; - } - }, - grid: { - top: 80, - bottom: 50, - left: 100, - right: 50 - }, - yAxis: { - type: 'category', - data: ['Diff'], - axisLine: { show: false }, - axisTick: { show: false } - }, - xAxis: { - type: 'value', - scale: true, - axisLabel: { - formatter: (val: number) => val.toFixed(2) - } - }, - series: [ - { - type: 'custom', - renderItem: (params: any, api: any) => { - const y = api.coord([0, 0])[1]; // y index 0 - - const val = api.value(0); - const low = api.value(1); - const high = api.value(2); - - const xVal = api.coord([val, 0])[0]; - const xLow = api.coord([low, 0])[0]; - const xHigh = api.coord([high, 0])[0]; - - return { - type: 'group', - children: [ - { - type: 'line', - shape: { x1: xLow, y1: y, x2: xHigh, y2: y }, - style: { stroke: '#333', lineWidth: 2 } - }, - { - type: 'line', - shape: { x1: xLow, y1: y - 5, x2: xLow, y2: y + 5 }, - style: { stroke: '#333', lineWidth: 2 } - }, - { - type: 'line', - shape: { x1: xHigh, y1: y - 5, x2: xHigh, y2: y + 5 }, - style: { stroke: '#333', lineWidth: 2 } - }, - { - type: 'circle', - shape: { cx: xVal, cy: y, r: 6 }, - style: { fill: '#5470c6', stroke: '#333' } - } - ] - }; - }, - // Pass data so axis scales correctly - data: [[meanDiff, lower, upper]], - // Map all dimensions to X axis for scaling - encode: { - x: [0, 1, 2], - y: -1 - } - }, - // Markline at 0 - { - type: 'line', - markLine: { - silent: true, - symbol: 'none', - data: [{ xAxis: 0 }], - lineStyle: { color: '#999', type: 'dashed' } - } - } - ] - }; - - return [option]; -} diff --git a/src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.spec.ts b/src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.spec.ts new file mode 100644 index 000000000..b7079790c --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.spec.ts @@ -0,0 +1,494 @@ +import { + clipHistogramNullEdges, + createHistogram, + describeClippedNullEdges, + formatClippedNullEdgesNote, + orderHorizontalBarRows, + selectXTickValues, + shouldClipNullEdges, + wrapCategoryLabel, +} from './histogram-chart'; +import { + HISTOGRAM_EDGE_PRIVACY_TOOLTIP, + HISTOGRAM_INTERIOR_PRIVACY_TOOLTIP, + HISTOGRAM_RESIDUAL_LABEL, +} from './histogram-data'; + +describe('wrapCategoryLabel', () => { + it('keeps short labels on a single line', () => { + expect(wrapCategoryLabel('Age')).toEqual(['Age']); + expect(wrapCategoryLabel('Detailed acute treatment')).toEqual(['Detailed acute treatment']); + }); + + it('breaks long labels at a space near the midpoint', () => { + expect(wrapCategoryLabel('Cerebrovascular risk assessment protocol')).toEqual([ + 'Cerebrovascular risk', + 'assessment protocol', + ]); + }); + + it('breaks at the only space when it is the nearest break point', () => { + // 30 chars, single space at index 25 -> the only available word break. + expect(wrapCategoryLabel('abcdefghijklmnopqrstuvwxy z123')).toEqual([ + 'abcdefghijklmnopqrstuvwxy', + 'z123', + ]); + }); + + it('keeps a label with no space on one line', () => { + expect(wrapCategoryLabel('abcdefghijklmnopABCDEFGHIJKLMNOP')).toEqual([ + 'abcdefghijklmnopABCDEFGHIJKLMNOP', + ]); + }); + + it('returns the empty label unchanged', () => { + expect(wrapCategoryLabel('')).toEqual(['']); + }); +}); + +describe('clipHistogramNullEdges', () => { + it('trims leading and trailing null counts but keeps interior null bins', () => { + expect(clipHistogramNullEdges( + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + [44, null, 10, 22, 84, 266, 1214, 3106, 2802, 470, 206, 50, 46, null, null] + )).toEqual({ + bins: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'], + counts: [44, null, 10, 22, 84, 266, 1214, 3106, 2802, 470, 206, 50, 46], + }); + }); + + it('trims trailing null counts on edge-based histograms', () => { + expect(clipHistogramNullEdges( + [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], + [null, 190000, 20000, 5000, null, null, null, null, null, null] + )).toEqual({ + bins: ['10', '20', '30'], + counts: [190000, 20000, 5000], + }); + }); + + it('keeps categorical bins aligned with counts', () => { + expect(clipHistogramNullEdges( + ['A', 'B', 'C', 'D'], + [null, 3, 5, null] + )).toEqual({ + bins: ['B', 'C'], + counts: [3, 5], + }); + }); + + it('returns empty data when all counts are null', () => { + expect(clipHistogramNullEdges([0, 10, 20], [null, null])).toEqual({ + bins: [], + counts: [], + }); + }); + + it('clips grouped counts with the same window as the totals', () => { + expect(clipHistogramNullEdges( + [0, 1, 2, 3], + [null, 10, 20, null], + [{ key: 'a', label: 'A', counts: [1, 4, 8, 2] }] + )).toEqual({ + bins: ['1', '2'], + counts: [10, 20], + groups: [{ key: 'a', label: 'A', counts: [4, 8] }], + }); + }); +}); + +describe('selectXTickValues', () => { + const denseBins = ['110', '115', '120', '125', '130', '135', '140', '145', '150', '155', '160', '165', '170', '175', '180', '185', '190', '195', '200', '205', '210', '215']; + + it('keeps all labels when there is enough width', () => { + expect(selectXTickValues(['110', '115', '120'], 400, 3)).toEqual(['110', '115', '120']); + }); + + it('subsamples dense multi-digit numeric labels', () => { + const ticks = selectXTickValues(denseBins, 529, 3); + expect(ticks.length).toBeLessThan(denseBins.length); + expect(ticks.length).toBeGreaterThanOrEqual(4); + expect(ticks[0]).toBe('110'); + expect(ticks[ticks.length - 1]).toBe('215'); + }); + + it('does not place the last bin right next to the previous tick', () => { + const ticks = selectXTickValues(denseBins, 529, 3); + const indices = ticks.map((tick) => denseBins.indexOf(tick)); + const gaps = indices.slice(1).map((index, i) => index - indices[i]); + expect(Math.min(...gaps)).toBeGreaterThanOrEqual(gaps[0]); + }); +}); + +describe('orderHorizontalBarRows', () => { + it('keeps numeric histogram bins in value order', () => { + expect(orderHorizontalBarRows(['30', '45', '60'], [10, 80, 20])).toEqual([ + { bin: '30', count: 10 }, + { bin: '45', count: 80 }, + { bin: '60', count: 20 }, + ]); + }); + + it('sorts named group bins by count descending', () => { + expect(orderHorizontalBarRows(['Acute', 'Hospitalization'], [4, 12])).toEqual([ + { bin: 'Hospitalization', count: 12 }, + { bin: 'Acute', count: 4 }, + ]); + }); +}); + +describe('shouldClipNullEdges', () => { + it('returns true for numeric bins', () => { + expect(shouldClipNullEdges([0, 1, 2, 3])).toBe(true); + }); + + it('returns false for nominal bins', () => { + expect(shouldClipNullEdges(['yes', 'no', 'unknown'])).toBe(false); + }); +}); + +describe('describeClippedNullEdges', () => { + it('returns the trimmed bin edges', () => { + expect(describeClippedNullEdges( + [0, 10, 20, 30, 40], + [null, 5, 8, null, null] + )).toEqual({ below: '10', above: '30' }); + }); + + it('is null when nothing was trimmed or bins are nominal', () => { + expect(describeClippedNullEdges([0, 10, 20], [4, 5, 6])).toBeNull(); + expect(describeClippedNullEdges(['yes', 'no'], [null, 3])).toBeNull(); + }); + + it('formats the Data Exploration warning from those edges', () => { + expect(formatClippedNullEdgesNote({ below: '10', above: '30' })).toBe( + 'Values below 10 / above 30 hidden (too few records per bin for privacy). Review outliers in Data Handling.' + ); + expect(formatClippedNullEdgesNote({ below: null, above: '85' })).toBe( + 'Values above 85 hidden (too few records per bin for privacy). Review outliers in Data Handling.' + ); + expect(formatClippedNullEdgesNote(null)).toBeNull(); + }); +}); + +describe('createHistogram with floating-point bin edges', () => { + let container: HTMLDivElement; + + afterEach(() => { + container?.remove(); + }); + + it('sizes the plot from formatted labels so it stays inside the container', () => { + container = document.createElement('div'); + container.style.width = '511px'; + document.body.appendChild(container); + const bins = Array.from({ length: 30 }, (_, i) => String(i * 0.02 - Number.EPSILON * i)); + const counts = bins.map((_, i) => 1000 - i * 30); + + createHistogram({ bins, counts, variableName: 'Predicted death', variableType: 'real' }, container, {}); + + const svg = container.querySelector('svg')!; + expect(Number(svg.getAttribute('width'))).toBe(511); + const translateX = parseFloat(svg.querySelector(':scope > g')!.getAttribute('transform')!.slice('translate('.length)); + expect(translateX).toBeLessThan(120); + const tickText = Array.from(container.querySelectorAll('.tick text')).map((t) => t.textContent); + expect(tickText.every((t) => (t ?? '').length <= 4)).toBe(true); + }); +}); + +describe('createHistogram stacked groups', () => { + const unsplit = { + bins: ['0', '10', '20'], + counts: [10, 20, 30], + variableName: 'Age', + }; + const groups = [ + { key: 'a', label: 'Group A', counts: [4, 8, 10] }, + { key: 'b', label: 'Group B', counts: [4, 8, 10] }, + ]; + let container: HTMLDivElement; + let unsplitContainer: HTMLDivElement; + + afterEach(() => { + container?.remove(); + unsplitContainer?.remove(); + }); + + function mount(): HTMLDivElement { + const host = document.createElement('div'); + host.style.width = '480px'; + document.body.appendChild(host); + return host; + } + + it('draws stacked segments per bin, a residual when groups do not sum, and a legend', () => { + container = mount(); + createHistogram({ ...unsplit, groups, splitLabel: 'Dataset' }, container, {}); + + const bars = Array.from(container.querySelectorAll('.bar')); + expect(bars).toHaveLength(9); + expect(container.querySelectorAll('.bar-residual')).toHaveLength(3); + expect(container.querySelector('.histogram-legend-title')?.textContent).toBe('Dataset'); + expect(getComputedStyle(container.querySelector('.histogram-legend-title')!).textTransform).toBe('uppercase'); + const legendItems = Array.from(container.querySelectorAll('.histogram-legend-item')).map((item) => item.textContent); + expect(legendItems).toEqual(['Group A', 'Group B', 'Not shown']); + expect( + container.querySelector('.histogram-legend-item:last-child')?.getAttribute('title') + ).toBe(HISTOGRAM_RESIDUAL_LABEL); + + const swatch = container.querySelector('.histogram-legend-swatch') as HTMLElement; + const item = container.querySelector('.histogram-legend-item') as HTMLElement; + const swatchStyle = getComputedStyle(swatch); + expect(parseFloat(swatchStyle.width)).toBeGreaterThan(0); + expect(parseFloat(swatchStyle.height)).toBeGreaterThan(0); + expect(swatchStyle.backgroundColor).not.toMatch(/^(transparent|rgba\(0,\s*0,\s*0,\s*0\))$/); + const itemGap = getComputedStyle(item).columnGap || getComputedStyle(item).gap; + expect(parseFloat(itemGap)).toBeGreaterThan(0); + expect(container.querySelectorAll('.bar-value')).toHaveLength(3); + }); + + it('does not draw a slate box around bars or yellow segments', () => { + container = mount(); + createHistogram({ + ...unsplit, + groups: [ + ...groups, + { key: 'c', label: 'Group C', counts: [2, 4, 10] }, + ], + }, container, {}); + + for (const bar of Array.from(container.querySelectorAll('.bar'))) { + const stroke = bar.getAttribute('stroke'); + expect(stroke === null || stroke === 'none').toBe(true); + } + expect(container.querySelector('[stroke="#475569"]')).toBeNull(); + const hue = Array.from(container.querySelectorAll('.bar-hue-edge')).map( + (line) => line.getAttribute('stroke') + ); + expect(hue.length).toBeGreaterThan(0); + expect(hue.every((color) => color === '#d99a00')).toBe(true); + }); + + it('aligns the legend with the plot origin', () => { + container = mount(); + createHistogram({ ...unsplit, groups, splitLabel: 'Dataset' }, container, {}); + + const svg = container.querySelector('svg')!; + const plot = svg.querySelector(':scope > g')!; + const match = plot.getAttribute('transform')?.match(/translate\(([\d.]+)/); + const plotLeft = svg.getBoundingClientRect().left + Number(match?.[1] ?? 0); + const legendLeft = container.querySelector('.histogram-legend')!.getBoundingClientRect().left; + expect(Math.abs(legendLeft - plotLeft)).toBeLessThanOrEqual(2); + }); + + it('fades other stacks on hover', () => { + container = mount(); + createHistogram({ ...unsplit, groups }, container, {}); + + const hits = Array.from(container.querySelectorAll('.bar-hit-area')); + hits[0].dispatchEvent(new MouseEvent('mouseover', { bubbles: true })); + + const hovered = Array.from(container.querySelectorAll('.bar[data-index="0"]')); + const other = Array.from(container.querySelectorAll('.bar[data-index="1"]')); + expect(hovered.every((bar) => bar.getAttribute('opacity') === '1')).toBe(true); + expect(other.every((bar) => bar.getAttribute('opacity') === '0.45')).toBe(true); + expect(container.querySelector('.histogram-chart-tooltip')?.textContent).toContain('Total'); + }); + + it('keeps the stacked bar height equal to the unsplit total', () => { + unsplitContainer = mount(); + container = mount(); + createHistogram(unsplit, unsplitContainer, {}); + createHistogram({ ...unsplit, groups }, container, {}); + + const unsplitBars = Array.from(unsplitContainer.querySelectorAll('.bar')); + expect(unsplitBars).toHaveLength(3); + for (let index = 0; index < 3; index += 1) { + const unsplitHeight = (unsplitBars[index] as SVGGraphicsElement).getBBox().height; + const stackedHeight = Array.from(container.querySelectorAll(`.bar[data-index="${index}"]`)) + .reduce((sum, bar) => sum + (bar as SVGGraphicsElement).getBBox().height, 0); + expect(stackedHeight).toBeCloseTo(unsplitHeight, 0); + } + }); +}); + +describe('createHistogram numeric bins', () => { + let container: HTMLDivElement; + + afterEach(() => { + container?.remove(); + }); + + function barLeft(bar: Element): number { + const match = (bar.getAttribute('d') ?? '').match(/^M([\d.]+),/); + return match ? Number(match[1]) : NaN; + } + + function barRight(bar: Element): number { + const match = (bar.getAttribute('d') ?? '').match(/H([\d.]+)/); + return match ? Number(match[1]) : NaN; + } + + it('draws adjacent numeric bars with no gaps', () => { + container = document.createElement('div'); + container.style.width = '480px'; + document.body.appendChild(container); + createHistogram({ bins: ['0', '10', '20'], counts: [10, 20, 30], variableName: 'Age' }, container, {}); + + const bars = Array.from(container.querySelectorAll('.bar')); + expect(bars).toHaveLength(3); + expect(Math.abs(barRight(bars[0]) - barLeft(bars[1]))).toBeLessThanOrEqual(1); + expect(Math.abs(barRight(bars[1]) - barLeft(bars[2]))).toBeLessThanOrEqual(1); + expect(container.querySelectorAll('.bar-value')).toHaveLength(3); + }); + + it('hides total labels when bars are too narrow', () => { + container = document.createElement('div'); + container.style.width = '200px'; + document.body.appendChild(container); + createHistogram({ + bins: Array.from({ length: 20 }, (_, i) => String(i)), + counts: Array.from({ length: 20 }, () => 10), + variableName: 'Age', + }, container, {}); + + expect(container.querySelectorAll('.bar-value')).toHaveLength(0); + }); + + it('draws adjacent numeric bars with no gaps when stacked', () => { + container = document.createElement('div'); + container.style.width = '480px'; + document.body.appendChild(container); + createHistogram({ + bins: ['0', '10', '20'], + counts: [10, 20, 30], + groups: [ + { key: 'a', label: 'A', counts: [4, 8, 10] }, + { key: 'b', label: 'B', counts: [6, 12, 20] }, + ], + variableName: 'Age', + splitLabel: 'Sex', + }, container, {}); + + const bars = [0, 1, 2].map((index) => container.querySelector(`.bar[data-index="${index}"]`)!); + expect(Math.abs(barRight(bars[0]) - barLeft(bars[1]))).toBeLessThanOrEqual(1); + expect(Math.abs(barRight(bars[1]) - barLeft(bars[2]))).toBeLessThanOrEqual(1); + expect(container.querySelector('.histogram-legend-title')?.textContent).toBe('Sex'); + }); + + it('keeps gaps between nominal bars', () => { + container = document.createElement('div'); + container.style.width = '480px'; + document.body.appendChild(container); + createHistogram({ bins: ['Female', 'Male'], counts: [12, 8], variableName: 'Sex' }, container, {}); + + const bars = Array.from(container.querySelectorAll('.bar')); + expect(bars).toHaveLength(2); + expect(barRight(bars[0])).toBeLessThan(barLeft(bars[1]) - 1); + }); +}); + +describe('createHistogram privacy stubs', () => { + let container: HTMLDivElement; + + afterEach(() => { + container?.remove(); + }); + + it('trims privacy-null edges by default and does not hatch them', () => { + container = document.createElement('div'); + container.style.width = '480px'; + document.body.appendChild(container); + createHistogram( + { bins: ['0', '10', '20', '30'], counts: [null, 8, 12, null], variableName: 'Age' }, + container, + {} + ); + + expect(container.querySelectorAll('.bar-privacy-stub')).toHaveLength(0); + expect(container.querySelectorAll('.bar')).toHaveLength(2); + }); + + it('keeps edge nulls and hatches them when clipping is off', () => { + container = document.createElement('div'); + container.style.width = '480px'; + document.body.appendChild(container); + createHistogram( + { bins: ['0', '10', '20', '30'], counts: [null, 8, null, 4], variableName: 'Age' }, + container, + { clipNullEdges: false } + ); + + const stubs = Array.from(container.querySelectorAll('.bar-privacy-stub')); + expect(stubs).toHaveLength(2); + expect(container.querySelectorAll('.bar:not(.bar-privacy-stub)')).toHaveLength(2); + + const hits = Array.from(container.querySelectorAll('.bar-hit-area')); + hits[0].dispatchEvent(new MouseEvent('mouseover', { bubbles: true })); + expect(container.querySelector('.histogram-chart-tooltip')?.textContent).toContain(HISTOGRAM_EDGE_PRIVACY_TOOLTIP); + + hits[2].dispatchEvent(new MouseEvent('mouseover', { bubbles: true })); + expect(container.querySelector('.histogram-chart-tooltip')?.textContent).toContain(HISTOGRAM_INTERIOR_PRIVACY_TOOLTIP); + }); + + it('treats every bin in a leading or trailing null run as an edge', () => { + container = document.createElement('div'); + container.style.width = '480px'; + document.body.appendChild(container); + createHistogram( + { bins: ['0', '10', '20', '30', '40'], counts: [null, null, 8, null, null], variableName: 'Age' }, + container, + { clipNullEdges: false } + ); + + const hits = Array.from(container.querySelectorAll('.bar-hit-area')); + for (const index of [1, 3]) { + hits[index].dispatchEvent(new MouseEvent('mouseover', { bubbles: true })); + expect(container.querySelector('.histogram-chart-tooltip')?.textContent).toContain(HISTOGRAM_EDGE_PRIVACY_TOOLTIP); + } + }); +}); + +describe('createHistogram in an unlaid-out container', () => { + const SAMPLE = { + bins: ['0-1', '1-2', '2-3', '3-4', '4-5'], + counts: [12, 30, 22, 5, 1], + variableName: 'Age', + }; + let host: HTMLDivElement; + + afterEach(() => { + host?.remove(); + }); + + /** Mirrors a Studio step hidden with `display: none`: every getBBox() is empty. */ + function hiddenContainer(): HTMLDivElement { + host = document.createElement('div'); + host.style.display = 'none'; + const container = document.createElement('div'); + container.style.width = '480px'; + host.appendChild(container); + document.body.appendChild(host); + return container; + } + + it('keeps the computed height for the vertical chart', () => { + const container = hiddenContainer(); + createHistogram(SAMPLE, container, {}); + + const svg = container.querySelector('svg'); + expect(svg).not.toBeNull(); + expect(Number(svg!.getAttribute('height'))).toBeGreaterThan(150); + expect(parseFloat(container.style.height)).toBeGreaterThan(150); + }); + + it('keeps the computed height for the horizontal chart', () => { + const container = hiddenContainer(); + createHistogram(SAMPLE, container, { orientation: 'horizontal' }); + + const svg = container.querySelector('svg'); + expect(svg).not.toBeNull(); + expect(Number(svg!.getAttribute('height'))).toBeGreaterThan(150); + expect(parseFloat(container.style.height)).toBeGreaterThan(150); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.ts b/src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.ts index 46e8904a8..6ddce80fa 100644 --- a/src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.ts +++ b/src/app/pages/experiment-studio/visualisations/histogram/histogram-chart.ts @@ -1,46 +1,455 @@ import * as d3 from 'd3'; +import { + HISTOGRAM_EDGE_PRIVACY_TOOLTIP, + HISTOGRAM_INTERIOR_PRIVACY_TOOLTIP, + HISTOGRAM_PRIVACY_STUB_HEIGHT, + HISTOGRAM_RESIDUAL_COLOR, + HISTOGRAM_RESIDUAL_KEY, + HISTOGRAM_RESIDUAL_LABEL, + HISTOGRAM_RESIDUAL_LEGEND_LABEL, + HISTOGRAM_SOFT_EDGE, + HistogramChartData, + HistogramGroupSeries, + histogramSeriesColor, + histogramSeriesEdgeColor, + residualCount, +} from './histogram-data'; + +export type { HistogramChartData, HistogramGroupSeries } from './histogram-data'; + +const MIN_X_TICKS = 4; +const MAX_X_TICKS = 12; + +/** + * getBBox() returns an empty rect for text that has never been laid out - a chart + * inside a Studio step hidden with `display: none`, for example. Used where a + * measured label extent would otherwise collapse the layout to nothing. + */ +const UNMEASURED_LABEL_WIDTH_PX = 46; + +function binsAreNumeric(bins: string[]): boolean { + return bins.length > 0 && bins.every((bin) => Number.isFinite(Number(bin))); +} + +/** Named groups sort by count; numeric histogram bins keep value order. */ +export function orderHorizontalBarRows( + bins: string[], + counts: Array<number | null> +): Array<{ bin: string; count: number | null }> { + const rows = bins.map((bin, index) => ({ + bin: String(bin), + count: counts[index] ?? null, + })); + if (binsAreNumeric(rows.map((row) => row.bin))) { + return rows; + } + return [...rows].sort((a, b) => (b.count ?? -1) - (a.count ?? -1)); +} + +export function shouldClipNullEdges(bins: Array<string | number>): boolean { + return binsAreNumeric(bins.map(String)); +} + +/** First and last non-null count; `start > end` when every count is null. */ +function nonNullRange(counts: Array<number | null>): { start: number; end: number } { + let start = 0; + while (start < counts.length && counts[start] === null) start += 1; + let end = counts.length - 1; + while (end >= start && counts[end] === null) end -= 1; + return { start, end }; +} + +export function clipHistogramNullEdges( + bins: Array<string | number>, + counts: Array<number | null>, + groups?: HistogramGroupSeries[] +): { bins: string[]; counts: Array<number | null>; groups?: HistogramGroupSeries[] } { + if (!Array.isArray(counts) || !Array.isArray(bins) || !bins.length) { + return { bins: [], counts: [], groups: groups && [] }; + } + const { start, end } = nonNullRange(counts); + const clip = <T>(values: T[]): T[] => values.slice(start, end + 1); + return { + bins: clip(bins).map(String), + counts: clip(counts), + groups: groups?.map((group) => ({ ...group, counts: clip(group.counts) })), + }; +} + +/** Bin edges removed by {@link clipHistogramNullEdges}, or null when nothing was trimmed. */ +export function describeClippedNullEdges( + bins: Array<string | number>, + counts: Array<number | null> +): { below: string | null; above: string | null } | null { + if (!shouldClipNullEdges(bins) || !counts.length || !bins.length) return null; + + const { start, end } = nonNullRange(counts); + if (start > end || (start === 0 && end === counts.length - 1)) { + return null; + } + + return { + below: start > 0 ? String(bins[start] ?? '') : null, + above: end < counts.length - 1 ? String(bins[Math.min(end + 1, bins.length - 1)] ?? '') : null, + }; +} + +export function formatClippedNullEdgesNote( + bounds: { below: string | null; above: string | null } | null +): string | null { + if (!bounds) return null; + const range = [bounds.below && `below ${bounds.below}`, bounds.above && `above ${bounds.above}`] + .filter(Boolean) + .join(' / '); + return range + ? `Values ${range} hidden (too few records per bin for privacy). Review outliers in Data Handling.` + : null; +} + +export function selectXTickValues(bins: string[], chartWidth: number, maxLabelLength: number): string[] { + const minLabelWidth = Math.max(32, maxLabelLength * 7 + 8); + const maxTicks = Math.max(MIN_X_TICKS, Math.min(MAX_X_TICKS, Math.floor(chartWidth / minLabelWidth))); + if (bins.length <= maxTicks) { + return bins; + } + + const step = Math.ceil(bins.length / maxTicks); + const ticks: string[] = []; + for (let index = 0; index < bins.length; index += step) { + ticks.push(bins[index]); + } + + // The last bin is always labelled, but it is less than one step past the last + // regular tick, so it takes that tick's place instead of crowding beside it. + const last = bins[bins.length - 1]; + if (ticks[ticks.length - 1] !== last) { + if (ticks.length > 1) ticks.pop(); + ticks.push(last); + } + + return ticks; +} + +/** + * Split a long horizontal-bar category label into two lines, breaking at the + * space closest to the midpoint so words stay intact. + */ +export function wrapCategoryLabel(label: string): string[] { + if (label.length <= 26) return [label]; + const mid = label.length / 2; + const spaces = [...label.matchAll(/ /g)].map((match) => match.index ?? 0); + if (!spaces.length) return [label]; + const cut = spaces.reduce((best, i) => (Math.abs(i - mid) < Math.abs(best - mid) ? i : best)); + return [label.slice(0, cut), label.slice(cut + 1)]; +} + +// Numeric bins are never rotated: their labels are formatted short, and dense +// ones are thinned by selectXTickValues to ticks that fit side by side. +function shouldRotateXLabels(binCount: number, hasStringBins: boolean, maxLabelLength: number): boolean { + const estimatedLines = Math.max(1, Math.ceil(maxLabelLength / 10)); + return hasStringBins && (estimatedLines > 1 || binCount > 8); +} + +function formatNumber(d: any): string { + if (typeof d !== 'number' || isNaN(d)) return String(d); + const abs = Math.abs(d); + if (Number.isInteger(d)) return d3.format(',')(d); + if (abs > 10000) return d3.format('.2f')(d); + if (abs > 0 && abs < 0.01) return d3.format('.2e')(d); + return d3.format('.2f')(d); +} + +/** 1,621 → 1.6k on a bar, but counts under 1,000 stay exact. */ +function formatBarCount(count: number): string { + return count >= 1000 ? d3.format('.2~s')(count) : String(count); +} + +/** Top corners rounded, bottom square so the bar sits flush on the baseline. */ +function topRoundedBarPath(x: number, y: number, width: number, height: number, radius: number): string { + const r = Math.max(0, Math.min(radius, width / 2, height)); + return `M${x},${y + height}V${y + r}Q${x},${y} ${x + r},${y}` + + `H${x + width - r}Q${x + width},${y} ${x + width},${y + r}V${y + height}Z`; +} + +/** Hatched swatch for the residual ("Not shown") series in HTML legends/tooltips. */ +const RESIDUAL_SWATCH = + 'repeating-linear-gradient(-45deg, #cbd5e1, #cbd5e1 1.5px, #e2e8f0 1.5px, #e2e8f0 3px)'; + +interface StackedTooltipRow { + label: string; + count: number | null; + color: string; + hatch?: boolean; +} + +interface LegendItem { + label: string; + color: string; + hatch?: boolean; + title?: string; +} + +function attachChartTooltip( + container: HTMLElement, + formatTitle: (binLabel: string) => string = (binLabel) => binLabel, + total?: number, +) { + container.style.position = 'relative'; + const tooltip = d3 + .select(container) + .append('div') + .attr('class', 'histogram-chart-tooltip') + .style('position', 'absolute') + .style('visibility', 'hidden') + .style('background-color', 'rgba(255, 255, 255, 0.96)') + .style('color', '#0f172a') + .style('padding', '10px 12px') + .style('border-radius', '8px') + .style('font-size', '12px') + .style('font-weight', '500') + .style('box-shadow', '0 8px 24px -8px rgba(15, 23, 42, 0.18)') + .style('border', '1px solid rgba(43, 51, 233, 0.16)') + .style('max-width', '250px') + .style('white-space', 'normal') + .style('pointer-events', 'none') + .style('z-index', '10'); + + const formatCountLabel = (count: number | null, privacyMessage?: string): string => + count === null + ? (privacyMessage ?? 'Masked for privacy (count below minimum threshold)') + : formatNumber(count); + + const appendCountRow = ( + parent: typeof tooltip, + label: string, + count: number | null, + shareOf: number | null, + privacyMessage?: string + ) => { + const row = parent.append('div'); + row.append('span').style('color', '#475569').text(`${label} `); + row.append('b').text(formatCountLabel(count, privacyMessage)); + if (count !== null && shareOf) { + parent + .append('div') + .style('color', '#475569') + .text(`${d3.format('.1%')(count / shareOf)} of total`); + } + }; + + const showTooltip = (binLabel: string, count: number | null, privacyMessage?: string) => { + tooltip.selectAll('*').remove(); + tooltip + .append('div') + .style('margin-bottom', '4px') + .style('font-weight', '700') + .style('line-height', '1.4') + .text(formatTitle(binLabel)); + appendCountRow(tooltip, 'Count', count, total ?? null, privacyMessage); + tooltip.style('visibility', 'visible'); + }; + + const showStackedTooltip = ( + binLabel: string, + binTotal: number | null, + rows: StackedTooltipRow[] + ) => { + tooltip.selectAll('*').remove(); + tooltip.style('max-width', '280px'); + tooltip + .append('div') + .style('margin-bottom', '2px') + .style('font-weight', '700') + .style('line-height', '1.4') + .text(formatTitle(binLabel)); + const totalRow = tooltip.append('div').style('margin-bottom', '6px'); + totalRow.append('span').style('color', 'var(--text-muted, #475569)').text('Total '); + totalRow.append('b').text(formatCountLabel(binTotal)); + if (binTotal !== null && total) { + totalRow + .append('span') + .style('color', 'var(--text-muted, #475569)') + .text(` · ${d3.format('.0%')(binTotal / total)} of all`); + } + for (const row of rows) { + const line = tooltip + .append('div') + .style('display', 'flex') + .style('align-items', 'center') + .style('gap', '6px') + .style('margin-top', '3px'); + const dot = line.append('span'); + dot + .style('width', '8px') + .style('height', '8px') + .style('border-radius', '2px') + .style('flex', '0 0 auto') + .style('background', row.hatch ? RESIDUAL_SWATCH : row.color); + if (!row.hatch) { + const edge = histogramSeriesEdgeColor(row.color); + if (edge) dot.style('box-shadow', `inset 0 0 0 1px ${edge}`); + } + line.append('span') + .style('flex', '1') + .style('min-width', '0') + .style('color', 'var(--text-muted, #475569)') + .style('overflow', 'hidden') + .style('text-overflow', 'ellipsis') + .style('white-space', 'nowrap') + .text(row.label); + line.append('b').style('flex', '0 0 auto').text(formatCountLabel(row.count)); + if (row.count !== null && binTotal) { + line + .append('span') + .style('flex', '0 0 auto') + .style('color', 'var(--text-muted, #475569)') + .text(d3.format('.0%')(row.count / binTotal)); + } + } + tooltip.style('visibility', 'visible'); + }; + + const moveTooltip = (event: MouseEvent) => { + const [x, y] = d3.pointer(event, container); + const tooltipNode = tooltip.node() as HTMLElement; + const tooltipWidth = tooltipNode?.offsetWidth || 100; + const tooltipHeight = tooltipNode?.offsetHeight || 60; + const bounds = container.getBoundingClientRect(); + let left = x + 15; + let top = y - 10; + if (left + tooltipWidth > bounds.width) left = x - tooltipWidth - 15; + if (top + tooltipHeight > bounds.height) top = y - tooltipHeight - 10; + tooltip.style('left', `${left}px`).style('top', `${top}px`); + }; + + const hideTooltip = () => { + tooltip.style('visibility', 'hidden'); + }; + + return { showTooltip, showStackedTooltip, moveTooltip, hideTooltip }; +} + +function appendHistogramLegend( + container: HTMLElement, + items: LegendItem[], + title?: string, + plotLeft = 0 +): number { + // D3/DOM nodes created at runtime have no `_ngcontent-*`, so emulated + // component CSS never matches them. Inline styles, same as the tooltip. + const el = (tag: string, className: string, css: string): HTMLElement => { + const node = document.createElement(tag); + node.className = className; + node.style.cssText = css; + return node; + }; + const muted = 'color: var(--text-muted, #475569);'; + const legend = el('div', 'histogram-legend', + 'display: flex; flex-direction: column; align-items: flex-start; gap: 4px; padding: 0;' + + ` margin: 0 0 4px ${Math.max(0, plotLeft)}px;` + + ` width: ${plotLeft ? `calc(100% - ${plotLeft}px)` : '100%'};` + + ` box-sizing: border-box; font-family: var(--font-sans, inherit); ${muted}`); + if (title) { + const heading = el('span', 'histogram-legend-title', + 'display: block; font-size: 0.7rem; font-weight: 600; letter-spacing: 0.08em;' + + ` text-transform: uppercase; ${muted}`); + heading.textContent = title; + legend.appendChild(heading); + } + const list = el('div', 'histogram-legend-items', + 'display: flex; flex-wrap: wrap; align-items: center; column-gap: 12px; row-gap: 6px; width: 100%;'); + list.setAttribute('role', 'list'); + for (const item of items) { + const entry = el('span', 'histogram-legend-item', + 'display: inline-flex; align-items: center; gap: 6px; max-width: 180px; min-height: 16px;' + + ` font-size: 12px; line-height: 1.2; font-variant-numeric: tabular-nums; ${muted}`); + entry.setAttribute('role', 'listitem'); + entry.title = item.title || item.label; + const edge = item.hatch ? HISTOGRAM_SOFT_EDGE : histogramSeriesEdgeColor(item.color); + const swatch = el('span', 'histogram-legend-swatch', + 'display: inline-block; width: 10px; height: 10px; border-radius: 2px; flex: 0 0 auto;' + + ` background: ${item.hatch ? RESIDUAL_SWATCH : item.color};` + + (edge ? ` box-shadow: inset 0 0 0 1px ${edge};` : '')); + const caption = el('span', '', 'overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0;'); + caption.textContent = item.label; + entry.append(swatch, caption); + list.appendChild(entry); + } + legend.appendChild(list); + container.insertBefore(legend, container.firstChild); + return legend.offsetHeight || 28; +} + +let hatchSerial = 0; + +function appendHatch( + svg: d3.Selection<SVGSVGElement, unknown, HTMLElement | null, unknown>, + kind: 'privacy' | 'residual', + fill: string, + stroke: string +): string { + hatchSerial += 1; + const id = `histogram-${kind}-hatch-${hatchSerial}`; + const pattern = svg + .append('defs') + .append('pattern') + .attr('id', id) + .attr('patternUnits', 'userSpaceOnUse') + .attr('width', 6) + .attr('height', 6); + pattern.append('rect').attr('width', 6).attr('height', 6).attr('fill', fill); + pattern.append('path').attr('d', 'M0,6 L6,0').attr('stroke', stroke).attr('stroke-width', 1); + return id; +} + +function preventChartScrollbar(container: HTMLElement): void { + container.style.overflowX = 'auto'; + container.style.overflowY = 'hidden'; +} export function createHistogram( - data: { - bins: string[]; - counts: number[]; - variable?: string; - variableName?: string; - description?: string; - variableType?: string; - }, + data: HistogramChartData, container: HTMLElement, config: { color?: string; - skipEveryOtherLabel?: boolean; + orientation?: 'vertical' | 'horizontal'; + clipNullEdges?: boolean; } = {} ): void { - const isDark = document.body.classList.contains('theme-dark'); - const textColor = isDark ? '#f1f5f9' : '#475569'; - const mutedTextColor = isDark ? '#94a3b8' : '#64748b'; - const gridColor = isDark ? 'rgba(255, 255, 255, 0.1)' : '#e2e8f0'; - const domainColor = isDark ? 'rgba(255, 255, 255, 0.2)' : '#cbd5e1'; - const barColor = isDark ? '#7f9ce8' : (config.color || '#2b33e9'); - - const { bins, counts } = data; - const { - skipEveryOtherLabel = false, - } = config; - - // Base container dimensions - const containerRect = container.getBoundingClientRect(); - const containerWidth = containerRect.width || 0; - const containerHeight = Math.max(containerRect.height || 0, 360); + if (config.orientation === 'horizontal') { + createHorizontalBarChart(data, container, config); + return; + } + + const textColor = '#475569'; + const gridColor = '#e2e8f0'; + const domainColor = '#cbd5e1'; + const barColor = config.color || '#2b33e9'; + + const clipNullEdges = config.clipNullEdges !== false; + const clipped = clipNullEdges && shouldClipNullEdges(data.bins) + ? clipHistogramNullEdges(data.bins, data.counts, data.groups) + : { bins: data.bins.map(String), counts: data.counts, groups: data.groups }; + const { bins, counts } = clipped; + const groups = clipped.groups; + const stacked = groups !== undefined; + const showPrivacyStubs = !clipNullEdges; + const containerWidth = container.clientWidth || 640; // Clear any existing chart container.innerHTML = ''; + container.style.height = 'auto'; + container.style.minHeight = '0'; + preventChartScrollbar(container); + + if (!bins.length || !counts.length) { + return; + } - // Create SVG once + // Create SVG once; height is computed from plot content below. const svg = d3 .select(container) - .append('svg') - .attr('width', containerWidth) - .attr('height', containerHeight); + .append('svg'); // Measure Y-axis label to make left margin dynamic const yLabelText = 'Count'; @@ -49,51 +458,72 @@ export function createHistogram( .append('text') .attr('x', -9999) .attr('y', -9999) - .style('font-size', '16px') + .style('font-size', '12px') .text(yLabelText); - const yLabelBBox = (tempLabel.node() as SVGTextElement).getBBox(); + const yLabelBBox = (tempLabel.node() as SVGTextElement | null)?.getBBox(); tempLabel.remove(); + const measuredYLabelWidth = yLabelBBox && yLabelBBox.width > 0 + ? yLabelBBox.width + : UNMEASURED_LABEL_WIDTH_PX; - const baseMargins = { top: 50, right: 10, bottom: 60, left: 40 }; - const labelCharsPerLine = 10; - const maxLabelLength = bins.reduce((max, b) => Math.max(max, String(b).length), 0); - const estimatedLines = Math.max(1, Math.ceil(maxLabelLength / labelCharsPerLine)); - const hasStringBins = bins.some((b) => isNaN(parseFloat(String(b)))); - const hasLongNumericBins = bins.some((b) => { - const raw = String(b).trim(); - if (!raw) return false; - const num = Number(raw); - if (Number.isNaN(num)) return false; - return raw.length >= 4 && raw.includes('.'); - }); - const needsRotate = (hasStringBins && estimatedLines > 1) || hasLongNumericBins || (hasStringBins && bins.length > 8); - const rotateExtra = needsRotate ? 22 : 0; - const bottomMargin = Math.max( - 70, - baseMargins.bottom + estimatedLines * 16 + rotateExtra - ); - const effectiveHeight = containerHeight; + // Layout is sized from the labels as drawn: raw float bins such as + // 0.019999999999999997 would otherwise size every bin for 20 characters. + const formatBin = (bin: string): string => { + const num = parseFloat(bin); + if (isNaN(num)) return bin; + if (data.variableType === 'discrete' || Number.isInteger(num)) { + return d3.format(',')(Math.round(num)); + } + return formatNumber(num); + }; + const binLabels = bins.map(formatBin); + + const baseMargins = { top: 18, right: 8, bottom: 44, left: 28 }; + const maxLabelLength = binLabels.reduce((max, label) => Math.max(max, label.length), 0); + const estimatedLines = Math.max(1, Math.ceil(maxLabelLength / 10)); + const hasStringBins = !binsAreNumeric(bins); + const denseNumericLabels = !hasStringBins && bins.length > 8 && maxLabelLength >= 3; + const needsRotate = shouldRotateXLabels(bins.length, hasStringBins, maxLabelLength); + const bottomMargin = needsRotate + ? Math.max(72, 20 + Math.min(120, maxLabelLength * 3.2)) + : Math.max(52, baseMargins.bottom + estimatedLines * 12); + + // A -40° label hangs left of its bar by ~cos(40°) of its length (~6.5px a + // character at 12px), so the first one is clipped unless the margin covers it. + const firstLabelReach = needsRotate ? binLabels[0].length * 6.5 * Math.cos(Math.PI * 40 / 180) : 0; - // Left margin = base + label width + small padding + // Left margin = ticks + a tight slot for the rotated "COUNT" title. + const yAxisGutter = baseMargins.left + measuredYLabelWidth + 4; const margin = { top: baseMargins.top, right: baseMargins.right, bottom: bottomMargin, - left: baseMargins.left + yLabelBBox.width + 8 + left: Math.max(yAxisGutter, Math.ceil(firstLabelReach)), }; - // Scale width by bin count to allow horizontal scrolling when needed - const minPerBinWidth = hasStringBins ? 30 : 20; + const innerHeight = needsRotate + ? 240 + : hasStringBins && bins.length <= 8 + ? 200 + : 260; + let containerHeight = margin.top + innerHeight + margin.bottom; + + // Aesthetic: Limit max bar width for small number of bins + const maxBarWidth = 100; + + // Scale width by bin count to allow horizontal scrolling when needed. Dense + // numeric ticks are thinned, so their bins do not each need a label's width. + // Capped at maxBarWidth: a wider bin centres the plot off-screen in empty SVG. + const minPerBinWidth = Math.min( + maxBarWidth, + hasStringBins ? 30 : denseNumericLabels ? 12 : Math.max(24, maxLabelLength * 8), + ); const desiredWidth = Math.max(containerWidth, bins.length * minPerBinWidth); - svg.attr('width', desiredWidth).attr('height', effectiveHeight); + svg.attr('width', desiredWidth).attr('height', containerHeight); const innerWidth = desiredWidth - margin.left - margin.right; - const innerHeight = effectiveHeight - margin.top - margin.bottom - 40; - - // Aesthetic: Limit max bar width for small number of bins - const maxBarWidth = 100; const optimalChartWidth = bins.length * maxBarWidth; const chartWidth = Math.min(innerWidth, optimalChartWidth); const xOffset = (innerWidth - chartWidth) / 2; @@ -103,18 +533,25 @@ export function createHistogram( .scaleBand() .domain(bins) .range([0, chartWidth]) - .padding(0.2); + .padding(hasStringBins ? 0.2 : 0); const yScale = d3 .scaleLinear() - .domain([0, d3.max(counts) || 0]) + .domain([0, d3.max(counts.filter((count): count is number => count !== null)) || 0]) .nice() .range([innerHeight, 0]); + const binPoints = bins.map((bin, index) => ({ + bin, + index, + count: counts[index] ?? null, + })); + + const plotLeft = margin.left + xOffset; // Chart group const chart = svg .append('g') - .attr('transform', `translate(${margin.left + xOffset}, ${margin.top})`); + .attr('transform', `translate(${plotLeft}, ${margin.top})`); // Background Grid (Y-axis only) chart @@ -133,108 +570,275 @@ export function createHistogram( ); - // Tooltip - const tooltip = d3 - .select(container) - .append('div') - .style('position', 'absolute') - .style('visibility', 'hidden') - .style('background-color', isDark ? '#1e293b' : '#ffffff') - .style('color', isDark ? '#f1f5f9' : '#0f172a') - .style('padding', '8px 12px') - .style('border-radius', '6px') - .style('font-size', '12px') - .style('font-weight', '500') - .style('box-shadow', '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)') - .style('border', `1px solid ${isDark ? '#334155' : '#e2e8f0'}`) - .style('max-width', '250px') - .style('white-space', 'normal') - .style('pointer-events', 'none') - .style('z-index', '10'); + const total = d3.sum(counts, (count) => count ?? 0); + const tooltipBinTitle = (bin: string, index: number): string => + !hasStringBins && index < bins.length - 1 + ? `${formatBin(bin)}–${formatBin(bins[index + 1])}` + : formatBin(bin); + const { showTooltip, showStackedTooltip, moveTooltip, hideTooltip } = attachChartTooltip( + container, + (binLabel) => `${data.variableName ? data.variableName + ' · ' : ''}${binLabel}`, + total, + ); + const visiblePoints = binPoints.filter((entry) => entry.count !== null); + // Every null bin before the first or after the last visible bin is an edge outlier. + const firstVisibleIndex = visiblePoints[0]?.index ?? bins.length; + const lastVisibleIndex = visiblePoints[visiblePoints.length - 1]?.index ?? -1; + const privacyStubPoints = showPrivacyStubs + ? binPoints.filter((entry) => entry.count === null) + : []; + const bandWidth = xScale.bandwidth(); + const barOpacity = 0.9; + const groupColorByKey = new Map( + (groups ?? []).map((group, index) => [group.key, histogramSeriesColor(index)]) + ); + + // Hover band behind the bars: a full-height tint marks the bin under the pointer. + const hoverBand = chart + .append('rect') + .attr('y', 0) + .attr('width', bandWidth + xScale.step() * xScale.paddingInner()) + .attr('height', innerHeight) + .attr('rx', hasStringBins ? 4 : 0) + .attr('fill', 'rgba(43, 51, 233, 0.06)') + .attr('visibility', 'hidden') + .style('pointer-events', 'none'); + + type BarDatum = { + index: number; + groupKey: string; + color: string; + y: number; + height: number; + separator: boolean; + isTop: boolean; + }; + + type HueEdge = { + index: number; + x1: number; + y1: number; + x2: number; + y2: number; + color: string; + }; + + // One segment per visible bin; a stacked bin splits into group pieces plus a + // residual for what the groups do not show, keeping the unsplit total height. + const segments: BarDatum[] = []; + for (const point of visiblePoints) { + let remaining = point.count as number; + const pieces: Array<{ groupKey: string; count: number; color: string }> = []; + for (const group of groups ?? []) { + const shown = Math.max(0, Math.min(group.counts[point.index] ?? 0, remaining)); + if (shown > 0) { + pieces.push({ groupKey: group.key, count: shown, color: groupColorByKey.get(group.key) ?? barColor }); + remaining -= shown; + } + } + if (!stacked) { + pieces.push({ groupKey: '', count: remaining, color: barColor }); + } else if (remaining > 0) { + pieces.push({ groupKey: HISTOGRAM_RESIDUAL_KEY, count: remaining, color: HISTOGRAM_RESIDUAL_COLOR }); + } + let yCursor = innerHeight; + pieces.forEach((piece, pieceIndex) => { + const height = innerHeight - yScale(piece.count); + yCursor -= height; + segments.push({ + index: point.index, + groupKey: piece.groupKey, + color: piece.color, + y: yCursor, + height, + separator: pieceIndex > 0, + isTop: pieceIndex === pieces.length - 1, + }); + }); + } - // Bars + const anyResidual = segments.some((segment) => segment.groupKey === HISTOGRAM_RESIDUAL_KEY); + const privacyHatchId = privacyStubPoints.length + ? appendHatch(svg, 'privacy', 'rgba(43, 51, 233, 0.08)', 'rgba(43, 51, 233, 0.4)') + : ''; + const residualHatchId = anyResidual ? appendHatch(svg, 'residual', '#e2e8f0', '#94a3b8') : ''; + + // Visible bars: proportional heights only. Numeric bins are square, category bars round. + // No per-segment slate stroke — yellow/mint get a hue edge on exposed sides only. chart .selectAll('.bar') - .data(counts) + .data(segments) .enter() - .append('rect') - .attr('class', 'bar') - .attr('x', (_, i) => xScale(bins[i]) || 0) - .attr('y', (d) => yScale(d)) - .attr('width', xScale.bandwidth()) - .attr('height', (d) => innerHeight - yScale(d)) - .attr('fill', barColor) - .attr('opacity', 0.85) - .on('mouseover', function (event, d) { - d3.select(this).attr('opacity', 1).attr('filter', 'brightness(1.1)'); - const i = counts.indexOf(d); - const binLabel = bins[i]; - - tooltip - .style('visibility', 'visible') - .html(` - <div style="margin-bottom: 4px; font-weight: 600; line-height: 1.4;">${data.variableName ? data.variableName + ': ' : ''}${binLabel}</div> - <div>Count: ${smartFormat(d)}</div> - `); - }) - .on('mousemove', function (event) { - // Get container position to calculate relative coordinates - const [x, y] = d3.pointer(event, container); + .append('path') + .attr('class', (d) => d.groupKey === HISTOGRAM_RESIDUAL_KEY ? 'bar bar-residual' : 'bar') + .attr('data-index', (d) => d.index) + .attr('data-group', (d) => (stacked ? d.groupKey : null)) + .attr('d', (d) => + topRoundedBarPath(xScale(bins[d.index]) || 0, d.y, bandWidth, d.height, hasStringBins ? 4 : 0)) + .attr('fill', (d) => d.groupKey === HISTOGRAM_RESIDUAL_KEY ? `url(#${residualHatchId})` : d.color) + .attr('stroke', 'none') + .attr('opacity', barOpacity) + .style('transition', 'opacity 120ms ease') + .style('pointer-events', 'none'); - // Keep tooltip within container bounds - const tooltipNode = tooltip.node() as HTMLElement; - const tooltipWidth = tooltipNode?.offsetWidth || 100; - const tooltipHeight = tooltipNode?.offsetHeight || 60; + if (stacked) { + const hueEdges: HueEdge[] = segments.flatMap((segment) => { + const color = histogramSeriesEdgeColor(segment.color); + if (!color) return []; + const x = xScale(bins[segment.index]) || 0; + const top = segment.y; + const bottom = segment.y + segment.height; + const edge = (x1: number, y1: number, x2: number, y2: number): HueEdge => + ({ index: segment.index, x1, y1, x2, y2, color }); + return [ + edge(x + 0.5, top, x + 0.5, bottom), + edge(x + bandWidth - 0.5, top, x + bandWidth - 0.5, bottom), + ...(segment.isTop ? [edge(x, top + 0.5, x + bandWidth, top + 0.5)] : []), + ]; + }); + chart + .selectAll('.bar-hue-edge') + .data(hueEdges) + .enter() + .append('line') + .attr('class', 'bar-hue-edge') + .attr('data-index', (d) => d.index) + .attr('x1', (d) => d.x1) + .attr('y1', (d) => d.y1) + .attr('x2', (d) => d.x2) + .attr('y2', (d) => d.y2) + .attr('stroke', (d) => d.color) + .attr('stroke-width', 1) + .style('pointer-events', 'none'); - let left = x + 15; - let top = y - 10; + chart + .selectAll('.bar-stack-separator') + .data(segments.filter((segment) => segment.separator)) + .enter() + .append('rect') + .attr('class', 'bar-stack-separator') + .attr('data-index', (d) => d.index) + .attr('x', (d) => xScale(bins[d.index]) || 0) + .attr('y', (d) => d.y + d.height - 0.5) + .attr('width', bandWidth) + .attr('height', 1) + .attr('fill', '#ffffff') + .style('pointer-events', 'none'); + } - // Flip if too close to right edge - if (left + tooltipWidth > containerWidth) { - left = x - tooltipWidth - 15; - } + if (privacyStubPoints.length) { + chart + .selectAll('.bar-privacy-stub') + .data(privacyStubPoints) + .enter() + .append('rect') + .attr('class', 'bar bar-privacy-stub') + .attr('data-index', (d) => d.index) + .attr('x', (d) => xScale(d.bin) || 0) + .attr('y', innerHeight - HISTOGRAM_PRIVACY_STUB_HEIGHT) + .attr('width', bandWidth) + .attr('height', HISTOGRAM_PRIVACY_STUB_HEIGHT) + .attr('fill', `url(#${privacyHatchId})`) + .style('pointer-events', 'none'); + } - // Flip if too close to bottom edge - if (top + tooltipHeight > containerHeight) { - top = y - tooltipHeight - 10; - } + const labelPoints = visiblePoints.filter((entry) => { + const text = formatBarCount(entry.count as number); + const fits = bandWidth >= Math.max(28, text.length * 6.5 + 4); + if (!fits) return false; + return !hasStringBins || (!stacked && bandWidth >= 34); + }); + if (labelPoints.length) { + chart + .selectAll('.bar-value') + .data(labelPoints) + .enter() + .append('text') + .attr('class', 'bar-value') + .attr('data-index', (d) => d.index) + .attr('x', (d) => (xScale(bins[d.index]) || 0) + bandWidth / 2) + .attr('y', (d) => yScale(d.count as number) - 4) + .attr('text-anchor', 'middle') + .attr('font-size', '11px') + .attr('font-weight', '500') + .attr('fill', 'var(--text-muted, #475569)') + .style('pointer-events', 'none') + .text((d) => formatBarCount(d.count as number)); + } + + const stackedTooltipRows = (index: number, binTotal: number | null): StackedTooltipRow[] => { + const rows: StackedTooltipRow[] = (groups ?? []).map((group) => ({ + label: group.label, + count: group.counts[index] ?? null, + color: groupColorByKey.get(group.key) ?? barColor, + })); + const leftover = residualCount(binTotal, groups ?? [], index); + if (leftover > 0) { + rows.push({ label: HISTOGRAM_RESIDUAL_LABEL, count: leftover, color: HISTOGRAM_RESIDUAL_COLOR, hatch: true }); + } + return rows; + }; - tooltip - .style('left', `${left}px`) - .style('top', `${top}px`); + // Invisible hit areas: full-height bin targets for reliable hover. + chart + .selectAll('.bar-hit-area') + .data(binPoints) + .enter() + .append('rect') + .attr('class', 'bar-hit-area') + .attr('x', (d) => xScale(d.bin) || 0) + .attr('y', 0) + .attr('width', bandWidth) + .attr('height', innerHeight) + .attr('fill', 'transparent') + .style('cursor', 'pointer') + .on('mouseover', function (_event, d) { + hoverBand + .attr('x', (xScale(d.bin) || 0) - (xScale.step() * xScale.paddingInner()) / 2) + .attr('visibility', 'visible'); + const fade = (index: number) => (index === d.index ? 1 : 0.45); + chart.selectAll<SVGElement, { index: number }>('.bar') + .attr('opacity', (bar) => fade(bar.index)); + chart.selectAll<SVGLineElement, HueEdge>('.bar-hue-edge') + .attr('opacity', (edge) => fade(edge.index)); + chart.selectAll<SVGTextElement, { index: number }>('.bar-value') + .attr('opacity', (label) => fade(label.index)); + const tooltipTitle = tooltipBinTitle(d.bin, d.index); + if (d.count === null && showPrivacyStubs) { + const isEdge = d.index < firstVisibleIndex || d.index > lastVisibleIndex; + showTooltip( + tooltipTitle, + null, + isEdge ? HISTOGRAM_EDGE_PRIVACY_TOOLTIP : HISTOGRAM_INTERIOR_PRIVACY_TOOLTIP + ); + } else if (stacked) { + showStackedTooltip(tooltipTitle, d.count, stackedTooltipRows(d.index, d.count)); + } else { + showTooltip(tooltipTitle, d.count); + } + }) + .on('mousemove', function (event) { + moveTooltip(event as MouseEvent); }) .on('mouseout', function () { - d3.select(this).attr('opacity', 0.85).attr('filter', null); - tooltip.style('visibility', 'hidden'); + hoverBand.attr('visibility', 'hidden'); + chart.selectAll('.bar').attr('opacity', barOpacity); + chart.selectAll('.bar-hue-edge').attr('opacity', 1); + chart.selectAll('.bar-value').attr('opacity', 1); + hideTooltip(); }); - // Smart number formatter - function smartFormat(d: any): string { - if (typeof d !== 'number' || isNaN(d)) return String(d); - const abs = Math.abs(d); - - if (Number.isInteger(d)) return d.toString(); - if (abs > 10000) return d3.format('.2f')(d); - if (abs > 0 && abs < 0.01) return d3.format('.2e')(d); - return d3.format('.2f')(d); - } + const xTickValues = denseNumericLabels + ? selectXTickValues(bins, chartWidth, maxLabelLength) + : bins; // X axis const xAxis = d3 .axisBottom(xScale) - .tickValues(bins) - .tickFormat((d: any) => { - const num = parseFloat(d); - if (isNaN(num)) return d; - - if (data.variableType === 'integer') { - return Math.round(num).toString(); - } - - return smartFormat(num); - }) + .tickValues(xTickValues) + .tickFormat((d: any) => formatBin(String(d))) .tickSize(0) - .tickPadding(12); + .tickPadding(needsRotate ? 6 : 8); const xAxisGroup = chart .append('g') @@ -242,61 +846,312 @@ export function createHistogram( .call(xAxis) .call(g => g.select('.domain').attr('stroke', domainColor)); + const tickFontSize = '11px'; const tickText = xAxisGroup .selectAll<SVGTextElement, any>('text') - .attr('font-size', '13px') + .attr('font-size', tickFontSize) .attr('font-weight', '500') .attr('fill', textColor) .style('text-anchor', needsRotate ? 'end' : 'middle'); if (needsRotate) { - tickText.attr('transform', 'rotate(-35)'); + tickText.attr('transform', 'rotate(-40)').attr('dx', '-0.35em').attr('dy', '0.15em'); + } + + const xAxisBBox = (xAxisGroup.node() as SVGGElement | null)?.getBBox(); + const xTicksBottom = xAxisBBox && xAxisBBox.height > 0 ? xAxisBBox.y + xAxisBBox.height : 28; + const xLabelY = innerHeight + xTicksBottom + 10; + const axisBottom = margin.top + xLabelY + 8; + if (axisBottom > containerHeight) { + containerHeight = Math.ceil(axisBottom); + svg.attr('height', containerHeight); + } + + if (data.variableName) { + chart + .append('text') + .attr('x', chartWidth / 2) + .attr('y', xLabelY) + .attr('text-anchor', 'middle') + .text(data.variableName) + .style('font-size', '11px') + .style('font-weight', '700') + .style('fill', 'var(--text-muted, #475569)') + .style('letter-spacing', '0.08em') + .style('text-transform', 'uppercase'); } - // Y axis + // Y axis: compact counts (50k); fractional ticks on small counts are not counts. const yAxis = d3 .axisLeft(yScale) .ticks(6) - .tickFormat((d: any) => smartFormat(d)) + .tickFormat((d: any) => (Number.isInteger(d) ? d3.format('~s')(d) : '')) .tickSize(0) - .tickPadding(12); + .tickPadding(8); chart.append('g') .call(yAxis) .call(g => g.select('.domain').remove()) .selectAll('text') - .attr('font-size', '13px') + .attr('font-size', '11px') .attr('font-weight', '500') .attr('fill', textColor); // Y label - const yLabelPaddingFromAxis = 32; + const yLabelPaddingFromAxis = 18; chart .append('text') .attr('transform', 'rotate(-90)') .attr('x', -innerHeight / 2) - .attr('y', -margin.left + yLabelPaddingFromAxis) + .attr('y', -yAxisGutter + yLabelPaddingFromAxis) .attr('text-anchor', 'middle') .text(yLabelText) + .style('font-size', '11px') + .style('font-weight', '700') + .style('fill', 'var(--text-muted, #475569)') + .style('letter-spacing', '0.08em') + .style('text-transform', 'uppercase'); + + // A zero-height bbox is the signature of an unlaid-out container; keep the + // computed height instead of collapsing the chart to a few visible pixels. + const svgBBox = (svg.node() as SVGSVGElement | null)?.getBBox(); + if (svgBBox && svgBBox.height > 0) { + containerHeight = Math.ceil(svgBBox.y + svgBBox.height + 4); + svg.attr('height', containerHeight); + } + + if (stacked) { + const legendItems: LegendItem[] = (groups ?? []).map((group) => ({ + label: group.label, + color: groupColorByKey.get(group.key) ?? barColor, + })); + if (anyResidual) { + legendItems.push({ + label: HISTOGRAM_RESIDUAL_LEGEND_LABEL, + color: HISTOGRAM_RESIDUAL_COLOR, + hatch: true, + title: HISTOGRAM_RESIDUAL_LABEL, + }); + } + if (legendItems.length) { + const legendHeight = appendHistogramLegend( + container, + legendItems, + data.splitLabel, + plotLeft + ); + containerHeight += legendHeight + 4; + } + } + + container.style.height = `${containerHeight}px`; +} + +/** + * Horizontal bar chart used for group summaries: one bar per group/variable, + * category labels on the Y axis and count on the X axis. Reads cleanly without + * rotating labels or horizontal scrolling. + */ +function createHorizontalBarChart( + data: { + bins: string[]; + counts: Array<number | null>; + variable?: string; + variableName?: string; + description?: string; + variableType?: string; + }, + container: HTMLElement, + config: { + color?: string; + orientation?: 'vertical' | 'horizontal'; + } = {} +): void { + const textColor = '#475569'; + const mutedTextColor = '#64748b'; + const gridColor = '#e2e8f0'; + const barColor = config.color || '#2b33e9'; + const containerWidth = container.clientWidth || 640; + + const rows = orderHorizontalBarRows(data.bins.map(String), data.counts); + const bins = rows.map((row) => row.bin); + const sortedCounts = rows.map((row) => row.count); + + container.innerHTML = ''; + container.style.height = 'auto'; + container.style.minHeight = '0'; + container.style.position = 'relative'; + preventChartScrollbar(container); + + if (!bins.length || !sortedCounts.length) { + return; + } + + const yLabelWidth = 200; + const margin = { top: 40, right: 44, bottom: 40, left: yLabelWidth }; + + const rowHeight = 42; + const innerHeight = Math.max(120, bins.length * rowHeight); + const containerHeight = margin.top + innerHeight + margin.bottom; + const desiredWidth = Math.max(containerWidth, 360); + const innerWidth = desiredWidth - margin.left - margin.right; + + const svg = d3 + .select(container) + .append('svg') + .attr('width', desiredWidth) + .attr('height', containerHeight); + + // Y scale: categories (bands) + const yScale = d3 + .scaleBand<string>() + .domain(bins) + .range([0, innerHeight]) + .padding(0.22); + + // X scale: count (linear) + const maxCount = d3.max(sortedCounts.filter((count): count is number => count !== null)) || 0; + const xScale = d3 + .scaleLinear() + .domain([0, maxCount]) + .nice() + .range([0, innerWidth]); + + const chart = svg + .append('g') + .attr('transform', `translate(${margin.left}, ${margin.top})`); + + // Vertical grid lines (X axis) + chart + .append('g') + .attr('class', 'grid') + .call( + d3.axisBottom(xScale) + .ticks(Math.min(8, Math.max(3, Math.floor(innerWidth / 80)))) + .tickSize(-innerHeight) + .tickFormat(() => '') + ) + .call((g) => g.select('.domain').remove()) + .call((g) => g.selectAll('.tick line') + .attr('stroke', gridColor) + .attr('stroke-dasharray', '3,3') + ); + + const { showTooltip, moveTooltip, hideTooltip } = attachChartTooltip(container); + const getBarSelector = (index: number) => `.bar[data-index="${index}"]`; + + const binPoints = bins.map((bin, index) => ({ + bin, + index, + count: sortedCounts[index] ?? null, + })); + + // Visible bars: horizontal, proportional widths. + chart + .selectAll('.bar') + .data(binPoints.filter((entry) => entry.count !== null)) + .enter() + .append('rect') + .attr('class', 'bar') + .attr('data-index', (d) => d.index) + .attr('x', 0) + .attr('y', (d) => yScale(bins[d.index]) || 0) + .attr('width', (d) => xScale(d.count as number)) + .attr('height', yScale.bandwidth()) + .attr('fill', barColor) + .attr('opacity', 0.85) + .attr('rx', 3) + .attr('ry', 3) + .style('pointer-events', 'none'); + + chart + .selectAll('.bar-value') + .data(binPoints.filter((entry) => entry.count !== null)) + .enter() + .append('text') + .attr('class', 'bar-value') + .attr('x', (d) => xScale(d.count as number) + 6) + .attr('y', (d) => (yScale(bins[d.index]) || 0) + yScale.bandwidth() / 2) + .attr('dy', '0.35em') + .attr('font-size', '11px') + .attr('font-weight', '600') + .attr('fill', textColor) + .style('pointer-events', 'none') + .text((d) => formatNumber(d.count)); + + // Invisible full-height hit areas for reliable hover. + chart + .selectAll('.bar-hit-area') + .data(binPoints) + .enter() + .append('rect') + .attr('class', 'bar-hit-area') + .attr('x', -yLabelWidth) + .attr('y', (d) => yScale(d.bin) || 0) + .attr('width', innerWidth + yLabelWidth) + .attr('height', yScale.bandwidth()) + .attr('fill', 'transparent') + .style('cursor', 'pointer') + .on('mouseover', function (_event, d) { + chart.select(getBarSelector(d.index)).attr('opacity', 1).attr('filter', 'brightness(1.1)'); + showTooltip(String(d.bin), d.count); + }) + .on('mousemove', function (event) { + moveTooltip(event as MouseEvent); + }) + .on('mouseout', function (_event, d) { + chart.select(getBarSelector(d.index)).attr('opacity', 0.85).attr('filter', null); + hideTooltip(); + }); + + // Y axis (category labels): long clinical names wrap to two lines instead of + // hard-truncating; the full label stays available via the native <title>. + const yAxisGroup = chart + .append('g') + .call(d3.axisLeft(yScale).tickSize(0).tickPadding(10).tickFormat((label) => String(label))) + .call((g) => g.select('.domain').remove()); + yAxisGroup + .selectAll<SVGTextElement, string>('text') + .attr('font-size', '12px') + .attr('font-weight', '500') + .attr('fill', textColor) + .each(function (label) { + const text = d3.select(this); + const lines = wrapCategoryLabel(String(label)); + const x = this.getAttribute('x'); + text.text(''); + lines.forEach((line, i) => { + text + .append('tspan') + .attr('x', x) + .attr('dy', i === 0 ? 0 : 12) + .text(line); + }); + text.append('title').text(String(label)); + }); + + // X axis (count) + chart.append('g') + .attr('transform', `translate(0, ${innerHeight})`) + .call(d3.axisBottom(xScale).ticks(Math.min(8, Math.max(3, Math.floor(innerWidth / 80)))).tickSize(0).tickPadding(8).tickFormat((d: any) => formatNumber(d))) + .call((g) => g.select('.domain').attr('stroke', '#cbd5e1')) + .selectAll('text') + .attr('font-size', '11px') + .attr('font-weight', '500') + .attr('fill', textColor); + + // X label + chart.append('text') + .attr('x', innerWidth / 2) + .attr('y', innerHeight + 30) + .attr('text-anchor', 'middle') + .text('Count') .style('font-size', '12px') .style('font-weight', '700') .style('fill', mutedTextColor) .style('letter-spacing', '0.08em') .style('text-transform', 'uppercase'); - // X label - // X label (now at top) - if (data.variableName) { - chart - .append('text') - .attr('x', chartWidth / 2) - .attr('y', -25) // Position above the grid/chart - .attr('text-anchor', 'middle') - .text(data.variableName) - .style('font-size', '14px') - .style('font-weight', '700') - .style('fill', textColor) - //.style('letter-spacing', '0.08em') - } + container.style.height = `${containerHeight}px`; } diff --git a/src/app/pages/experiment-studio/visualisations/histogram/histogram-data.spec.ts b/src/app/pages/experiment-studio/visualisations/histogram/histogram-data.spec.ts new file mode 100644 index 000000000..6e7c7a234 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/histogram/histogram-data.spec.ts @@ -0,0 +1,147 @@ +import { + MAX_HISTOGRAM_SPLIT_LEVELS, + buildHistogramSplitOptions, + buildStackedHistogramSeries, + histogramCacheKey, + isNominalHistogramNode, + residualCount, +} from './histogram-data'; + +describe('histogram split helpers', () => { + it('disables a split variable missing from a selected dataset and names the datasets', () => { + const options = buildHistogramSplitOptions({ + plottedCode: 'age', + selectedVariables: [ + { code: 'sex', label: 'Sex', type: 'nominal', enumerations: ['M', 'F'] }, + { code: 'apoe', label: 'APOE', type: 'nominal', enumerations: ['0', '1'] }, + ], + datasetLevelCount: 2, + unavailableVariables: new Map([['apoe', ['ADNI', 'CHUV']], ['dataset', ['ADNI']]]), + }); + + expect(options.find((option) => option.code === 'dataset')).toEqual( + expect.objectContaining({ disabled: false, title: 'Split bars by dataset' }) + ); + expect(options.find((option) => option.code === 'sex')?.disabled).toBe(false); + expect(options.find((option) => option.code === 'apoe')).toEqual({ + code: 'apoe', + label: 'APOE', + disabled: true, + title: 'Not available in: ADNI, CHUV', + unavailable: true, + }); + }); + + it('offers Dataset plus selected nominal variables, excluding the plotted variable', () => { + const options = buildHistogramSplitOptions({ + plottedCode: 'age', + selectedVariables: [ + { code: 'sex', label: 'Sex', type: 'nominal', enumerations: ['M', 'F'] }, + { code: 'age', label: 'Age', type: 'nominal', enumerations: ['young', 'old'] }, + { code: 'dataset', label: 'Dataset', type: 'nominal', enumerations: ['a', 'b', 'c'] }, + { code: 'weight', label: 'Weight', type: 'continuous' }, + { code: 'subjectid', label: 'Subject', type: 'nominal' }, + ], + datasetLevelCount: 2, + }); + + expect(options.map((option) => option.code)).toEqual(['dataset', 'sex']); + expect(options.every((option) => !option.disabled)).toBe(true); + }); + + it(`disables splits with more than ${MAX_HISTOGRAM_SPLIT_LEVELS} levels`, () => { + const options = buildHistogramSplitOptions({ + plottedCode: 'age', + selectedVariables: [ + { + code: 'site', + label: 'Site', + type: 'nominal', + enumerations: Array.from({ length: MAX_HISTOGRAM_SPLIT_LEVELS + 1 }, (_, index) => index), + }, + ], + datasetLevelCount: MAX_HISTOGRAM_SPLIT_LEVELS + 2, + }); + + expect(options.find((option) => option.code === 'dataset')?.disabled).toBe(true); + expect(options.find((option) => option.code === 'site')?.disabled).toBe(true); + }); + + it('treats nominal and categorical nodes as non-splittable plots', () => { + expect(isNominalHistogramNode({ type: 'continuous' })).toBe(false); + expect(isNominalHistogramNode({ type: 'discrete' })).toBe(false); + expect(isNominalHistogramNode({ type: 'nominal' })).toBe(true); + expect(isNominalHistogramNode({ type: 'ordinal' })).toBe(true); + expect(isNominalHistogramNode({ type: 'binary' })).toBe(true); + expect(isNominalHistogramNode({ type: 'real' })).toBe(false); + }); + + it('keys the histogram cache by variable and split', () => { + expect(histogramCacheKey('age', '')).toBe('age'); + expect(histogramCacheKey('age', 'dataset')).toBe('age::dataset'); + }); +}); + +describe('buildStackedHistogramSeries', () => { + const items = [ + { var: 'age', grouping_var: null, grouping_enum: null, bins: [0, 10, 20], counts: [10, 20, 30] }, + { var: 'age', grouping_var: 'dataset', grouping_enum: 'ds1', bins: [0, 10, 20], counts: [4, 8, null] }, + { var: 'age', grouping_var: 'dataset', grouping_enum: 'ds2', bins: [0, 10, 20], counts: [4, 8, 10] }, + ]; + + it('returns the overall series when no grouping is requested', () => { + expect(buildStackedHistogramSeries(items, 'age', null, () => 'x')).toEqual({ + bins: [0, 10, 20], + counts: [10, 20, 30], + }); + }); + + it('keeps leading and trailing null counts on the overall series', () => { + expect(buildStackedHistogramSeries( + [{ var: 'age', bins: [0, 10, 20, 30], counts: [null, 8, 12, null] }], + 'age', + null, + () => 'x' + )?.counts).toEqual([null, 8, 12, null]); + }); + + it('attaches grouped series for the requested split', () => { + const stacked = buildStackedHistogramSeries(items, 'age', 'dataset', (_var, value) => value.toUpperCase()); + expect(stacked?.groups).toEqual([ + { key: 'ds1', label: 'DS1', counts: [4, 8, null] }, + { key: 'ds2', label: 'DS2', counts: [4, 8, 10] }, + ]); + expect(stacked?.counts).toEqual([10, 20, 30]); + }); + + it('does not use a grouped entry as the bin totals when the overall entry is missing', () => { + expect(buildStackedHistogramSeries(items.slice(1), 'age', 'dataset', (_var, value) => value)).toBeNull(); + }); + + it('drops unselected datasets and all-null groups', () => { + const stacked = buildStackedHistogramSeries( + [ + ...items, + { var: 'age', grouping_var: 'dataset', grouping_enum: 'ds3', bins: [0, 10, 20], counts: [null, null, null] }, + { var: 'age', grouping_var: 'dataset', grouping_enum: 'other', bins: [0, 10, 20], counts: [2, 4, 6] }, + ], + 'age', + 'dataset', + (_var, value) => value, + ['ds1', 'ds3'] + ); + expect(stacked?.groups).toEqual([ + { key: 'ds1', label: 'ds1', counts: [4, 8, null] }, + ]); + }); +}); + +describe('residualCount', () => { + it('is the leftover after summing visible group counts', () => { + const series = (counts: Array<number | null>) => + counts.map((count, index) => ({ key: String(index), label: String(index), counts: [count] })); + expect(residualCount(10, series([4, 4, null]), 0)).toBe(2); + expect(residualCount(10, series([4, 6]), 0)).toBe(0); + expect(residualCount(null, series([4]), 0)).toBe(0); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/histogram/histogram-data.ts b/src/app/pages/experiment-studio/visualisations/histogram/histogram-data.ts new file mode 100644 index 000000000..e6ddfc1b7 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/histogram/histogram-data.ts @@ -0,0 +1,270 @@ +import { UnavailableVariablesMap, unavailableTooltipFor } from '../../../../core/variable-availability.utils'; +import { isCategoricalVariableType } from '../../../../core/data-model.utils'; + +/** Grouping CDE present in every data model. Always offered as a histogram split. */ +export const DATASET_VARIABLE_CODE = 'dataset'; + +/** Stacked splits with more levels than this are offered but disabled. */ +export const MAX_HISTOGRAM_SPLIT_LEVELS = 8; + +export const HISTOGRAM_SPLIT_NONE = ''; + +export const HISTOGRAM_RESIDUAL_KEY = '__residual__'; + +export const HISTOGRAM_RESIDUAL_LABEL = 'Not shown (missing or < privacy threshold)'; + +/** Short legend text; {@link HISTOGRAM_RESIDUAL_LABEL} stays on the title/tooltip. */ +export const HISTOGRAM_RESIDUAL_LEGEND_LABEL = 'Not shown'; + +export const HISTOGRAM_EDGE_PRIVACY_TOOLTIP = + 'Fewer than the privacy threshold (hidden for privacy)'; + +export const HISTOGRAM_INTERIOR_PRIVACY_TOOLTIP = + '0 or fewer than the privacy threshold'; + +export const HISTOGRAM_PRIVACY_STUB_HEIGHT = 7; + +/** DESIGN.md series order, then darker MIP inks so later groups stay visible on white. */ +export const HISTOGRAM_SERIES_COLORS = [ + '#2b33e9', + '#7f9ce8', + '#ffba08', + '#dfefe4', + '#1b21a3', + '#065f46', + '#92400e', + '#0f172a', +] as const; + +export const HISTOGRAM_RESIDUAL_COLOR = '#cbd5e1'; + +/** Darker yellow for the exposed edge of `#ffba08` segments (DESIGN.md contrast). */ +export const HISTOGRAM_YELLOW_EDGE = '#d99a00'; + +/** Slate at ~40% — mint and residual swatches, never a full bar box. */ +export const HISTOGRAM_SOFT_EDGE = 'rgba(71, 85, 105, 0.4)'; + +export interface HistogramGroupSeries { + key: string; + label: string; + counts: Array<number | null>; +} + +export interface HistogramChartData { + bins: string[]; + counts: Array<number | null>; + variable?: string; + variableName?: string; + description?: string; + variableType?: string; + groups?: HistogramGroupSeries[]; + /** Split-variable label for the stacked legend title (`Dataset`, `Sex`, …). */ + splitLabel?: string; +} + +export interface HistogramSplitOption { + code: string; + label: string; + disabled: boolean; + title: string; + /** Missing from a selected dataset: offered grayed out, disabled, with the datasets in the title. */ + unavailable?: boolean; +} + +/** A selected variable as the studio holds it; only nominal ones become splits. */ +export interface HistogramSplitVariable { + code?: string | null; + label?: string | null; + name?: string | null; + type?: string | null; + enumerations?: unknown[] | null; +} + +export interface RawHistogramItem { + var?: string; + variable?: string; + grouping_var?: string | null; + grouping_enum?: string | null; + bins?: unknown; + counts?: unknown; +} + +export function histogramCacheKey(variableCode: string, splitCode: string): string { + return splitCode ? `${variableCode}::${splitCode}` : variableCode; +} + +export function isNominalHistogramNode( + node: { + type?: string | null; + enumerations?: unknown[] | null; + } | null | undefined +): boolean { + if (!node) return false; + if (isCategoricalVariableType(node.type)) return true; + return Array.isArray(node.enumerations) && node.enumerations.length > 0; +} + +export function buildHistogramSplitOptions(input: { + plottedCode: string | null | undefined; + selectedVariables: HistogramSplitVariable[]; + datasetLevelCount: number; + unavailableVariables?: UnavailableVariablesMap; +}): HistogramSplitOption[] { + const plotted = String(input.plottedCode ?? ''); + const tooMany = (count: number, kind: 'datasets' | 'categories'): string => + `Too many ${kind} (${count}). Split is limited to ${MAX_HISTOGRAM_SPLIT_LEVELS} categories.`; + + const datasetDisabled = input.datasetLevelCount > MAX_HISTOGRAM_SPLIT_LEVELS; + const options: HistogramSplitOption[] = [ + { + code: DATASET_VARIABLE_CODE, + label: 'Dataset', + disabled: datasetDisabled, + title: datasetDisabled + ? tooMany(input.datasetLevelCount, 'datasets') + : 'Split bars by dataset', + }, + ]; + + for (const variable of input.selectedVariables) { + const code = String(variable.code ?? '').trim(); + if (!code || code.toLowerCase() === DATASET_VARIABLE_CODE || code === plotted) continue; + // Split levels come from enumerations; free-text categoricals have none and Exaflow rejects them. + if (!isNominalHistogramNode(variable) || !variable.enumerations?.length) continue; + const label = String(variable.label ?? variable.name ?? variable.code); + const levelCount = variable.enumerations?.length ?? 0; + const unavailableTitle = unavailableTooltipFor(input.unavailableVariables, { code }); + if (unavailableTitle) { + options.push({ code, label, disabled: true, title: unavailableTitle, unavailable: true }); + continue; + } + const disabled = levelCount > MAX_HISTOGRAM_SPLIT_LEVELS; + options.push({ + code, + label, + disabled, + title: disabled ? tooMany(levelCount, 'categories') : `Split bars by ${label}`, + }); + } + + return options; +} + +export function histogramSeriesColor(index: number): string { + return HISTOGRAM_SERIES_COLORS[index % HISTOGRAM_SERIES_COLORS.length]; +} + +export function histogramSeriesEdgeColor(color: string): string | null { + if (color === '#ffba08') return HISTOGRAM_YELLOW_EDGE; + if (color === '#dfefe4') return HISTOGRAM_SOFT_EDGE; + return null; +} + +export function toNumericCounts(counts: unknown): Array<number | null> { + if (!Array.isArray(counts)) return []; + return counts.map((count) => { + if (count === null || count === undefined) return null; + const value = typeof count === 'number' ? count : Number(count); + return Number.isFinite(value) ? value : null; + }); +} + +export function extractHistogramItems(response: unknown): RawHistogramItem[] { + const payload = (response as { result?: unknown } | null)?.result ?? response; + const fromPayload = (payload as { histogram?: unknown } | null)?.histogram; + const fromResponse = (response as { histogram?: unknown } | null)?.histogram; + const histList = Array.isArray(fromPayload) + ? fromPayload + : Array.isArray(fromResponse) + ? fromResponse + : []; + return histList as RawHistogramItem[]; +} + +export function histogramItemMatchesVariable(item: RawHistogramItem | undefined, code: string): boolean { + if (!item) return false; + const entryCode = String(item.var ?? item.variable ?? ''); + return entryCode.toLowerCase() === code.toLowerCase(); +} + +export function selectOverallHistogramItem( + items: RawHistogramItem[], + code: string +): RawHistogramItem | undefined { + // Never fall back to a grouped entry: one group's counts are not the bin totals. + const ungrouped = items.filter((entry) => !entry?.grouping_var); + return ungrouped.find((entry) => histogramItemMatchesVariable(entry, code)) ?? ungrouped[0]; +} + +export function buildStackedHistogramSeries( + items: RawHistogramItem[], + yCode: string, + groupingCode: string | null, + labelFor: (groupingVar: string, groupingEnum: string) => string, + /** Selected dataset codes; a `dataset` split keeps only these groups. */ + selectedDatasets?: Iterable<string> | null +): { bins: unknown[]; counts: Array<number | null>; groups?: HistogramGroupSeries[] } | null { + const overall = selectOverallHistogramItem(items, yCode); + const bins = overall?.bins; + const counts = toNumericCounts(overall?.counts); + if (!overall || !Array.isArray(bins) || !bins.length || !counts.length) { + return null; + } + + if (!groupingCode) { + return { bins, counts }; + } + + const grouped = items.filter((entry) => { + if (!histogramItemMatchesVariable(entry, yCode)) return false; + return String(entry.grouping_var ?? '').toLowerCase() === groupingCode.toLowerCase(); + }); + + const groups: HistogramGroupSeries[] = []; + for (const entry of grouped) { + const enumCode = entry.grouping_enum; + if (enumCode === null || enumCode === undefined || enumCode === '') continue; + const key = String(enumCode); + groups.push({ + key, + label: labelFor(groupingCode, key), + counts: alignGroupCounts(toNumericCounts(entry.counts), counts.length), + }); + } + + return { + bins, + counts, + groups: filterVisibleHistogramGroups( + groups, + groupingCode.toLowerCase() === DATASET_VARIABLE_CODE ? selectedDatasets : null + ) ?? [], + }; +} + +/** Drop groups with no visible counts, and optionally keep only allowed keys. */ +export function filterVisibleHistogramGroups( + groups: HistogramGroupSeries[] | undefined, + allowedKeys?: Iterable<string> | null +): HistogramGroupSeries[] | undefined { + if (!groups?.length) return undefined; + const allowed = allowedKeys + ? new Set([...allowedKeys].map((key) => String(key).toLowerCase())) + : null; + const visible = groups.filter((group) => { + if (allowed && !allowed.has(group.key.toLowerCase())) return false; + return group.counts.some((count) => count !== null); + }); + return visible.length ? visible : undefined; +} + +/** Bin `index`'s total minus what the groups show there. */ +export function residualCount(total: number | null, groups: HistogramGroupSeries[], index: number): number { + if (total === null) return 0; + const shown = groups.reduce((sum, group) => sum + (group.counts[index] ?? 0), 0); + return Math.max(0, total - shown); +} + +function alignGroupCounts(counts: Array<number | null>, length: number): Array<number | null> { + return Array.from({ length }, (_, index) => counts[index] ?? null); +} diff --git a/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.css b/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.css index 6b360eb16..71b060a81 100644 --- a/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.css +++ b/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.css @@ -2,73 +2,32 @@ display: block; padding: 0; width: 100%; - height: 100%; - min-height: 450px; box-sizing: border-box; + /* overflow-x:auto + overflow-y:visible computes to overflow-y:auto and + paints a stray vertical scrollbar. Hide Y; size the box to the drawing. */ overflow-x: auto; - overflow-y: visible; + overflow-y: hidden; +} + +#histogram-chart svg { + display: block; } .histogram-container { - display: flex; - flex-direction: column; - justify-content: center; - align-items: center; + display: block; width: 100%; - height: 100%; - min-height: 450px; - padding: 10px; + padding: 0; background: transparent; border: none; border-radius: 0; box-shadow: none; } -.chart-description { - font-size: 14px; - color: var(--text-muted, #475569); - text-align: center; -} - -/* Adjust text styles for better readability */ +/* SVG text is attribute-styled in the chart code. Legend/tooltip nodes are + created at runtime without `_ngcontent-*`, so they also use inline styles. */ #histogram-chart text { font-size: 12px; - font-family: 'Arial', sans-serif; - fill: var(--text-main, #0f172a); + font-family: inherit; + fill: var(--text-main); } -/* Add hover effects for bars if desired */ -.bar:hover { - fill: var(--primary-color, #2b33e9); - cursor: pointer; -} - -/* Loading Spinner */ -.loading-indicator { - display: flex; - flex-direction: column; - align-items: center; - margin: 20px 0; - font-size: 16px; - color: var(--primary-color, #2b33e9); -} - -.spinner { - border: 4px solid rgba(0, 0, 0, 0.1); - border-left-color: var(--primary-color, #2b33e9); - width: 40px; - height: 40px; - border-radius: 50%; - animation: spin 1s linear infinite; - margin-top: 10px; -} - -@keyframes spin { - 0% { - transform: rotate(0deg); - } - - 100% { - transform: rotate(360deg); - } -} \ No newline at end of file diff --git a/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.spec.ts b/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.spec.ts new file mode 100644 index 000000000..c68158049 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.spec.ts @@ -0,0 +1,175 @@ +import { Component, ElementRef, provideZonelessChangeDetection, signal, viewChild } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { HistogramChartConfig, HistogramComponent } from './histogram.component'; + +type ChartData = { bins: string[]; counts: number[]; variableName: string }; + +const OVERALL: ChartData = { + bins: ['0-1', '1-2', '2-3', '3-4', '4-5'], + counts: [12, 30, 22, 5, 1], + variableName: 'Age', +}; +const SWAPPED: ChartData = { bins: ['a', 'b', 'c'], counts: [4, 8, 2], variableName: 'Sex' }; +const VERTICAL: HistogramChartConfig = { color: '#2b33e9', orientation: 'vertical' }; + +/** Long enough for afterNextRender plus the 150ms resize debounce. */ +const RENDER_MS = 200; +const RESIZE_MS = 300; + +@Component({ + selector: 'app-histogram-spec-host', + standalone: true, + imports: [HistogramComponent], + template: `<div #wrapper><app-histogram [data]="data()" [config]="config()" /></div>`, + styles: ['app-histogram { display: block; }'], +}) +class HistogramSpecHostComponent { + readonly wrapper = viewChild.required<ElementRef<HTMLDivElement>>('wrapper'); + readonly data = signal<ChartData | null>(null); + readonly config = signal<HistogramChartConfig>({}); +} + +describe('HistogramComponent', () => { + let fixture: ComponentFixture<HistogramSpecHostComponent>; + let host: HistogramSpecHostComponent; + let wrapper: HTMLDivElement; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HistogramSpecHostComponent], + providers: [provideZonelessChangeDetection()], + }); + + fixture = TestBed.createComponent(HistogramSpecHostComponent); + host = fixture.componentInstance; + wrapper = host.wrapper().nativeElement; + wrapper.style.width = '480px'; + }); + + afterEach(() => { + fixture.destroy(); + }); + + async function settle(waitMs: number): Promise<void> { + fixture.detectChanges(); + await fixture.whenStable(); + if (waitMs > 0) { + await new Promise((resolve) => setTimeout(resolve, waitMs)); + fixture.detectChanges(); + await fixture.whenStable(); + } + } + + function chart(): HTMLElement { + return wrapper.querySelector('#histogram-chart') as HTMLElement; + } + + function svg(): SVGElement | null { + return chart()?.querySelector('svg'); + } + + function drawnHeight(): number { + return parseFloat(chart().style.height || '0'); + } + + function bars(): number { + return chart().querySelectorAll('.bar').length; + } + + it('draws the chart from the width its container actually has', async () => { + host.data.set(OVERALL); + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + + expect(svg(), 'chart drawn').not.toBeNull(); + expect(bars()).toBe(OVERALL.bins.length); + expect(drawnHeight()).toBeGreaterThan(150); + }); + + it('does not draw a collapsed chart while its step is hidden', async () => { + wrapper.style.display = 'none'; + host.data.set(OVERALL); + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + + expect(svg(), 'nothing measured, nothing drawn').toBeNull(); + }); + + it('draws as soon as the hidden step becomes visible again', async () => { + wrapper.style.display = 'none'; + host.data.set(OVERALL); + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + expect(svg()).toBeNull(); + + wrapper.style.display = ''; + await settle(RESIZE_MS); + + expect(svg(), 'redrawn on reveal').not.toBeNull(); + expect(bars()).toBe(OVERALL.bins.length); + expect(drawnHeight()).toBeGreaterThan(150); + }); + + it('keeps the drawing when an equal config object arrives again', async () => { + host.data.set(OVERALL); + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + + const first = svg(); + expect(first).not.toBeNull(); + first!.setAttribute('data-probe', 'first'); + + // A fresh object with the same values, exactly as an inline template literal does. + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + + expect(chart().querySelectorAll('svg').length).toBe(1); + expect(svg()!.getAttribute('data-probe')).toBe('first'); + }); + + it('redraws when the container is resized', async () => { + host.data.set(OVERALL); + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + + const widthBefore = Number(svg()!.getAttribute('width')); + wrapper.style.width = '300px'; + await settle(RESIZE_MS); + + const widthAfter = Number(svg()!.getAttribute('width')); + expect(widthAfter).toBeGreaterThan(0); + expect(widthAfter).toBeLessThan(widthBefore); + }); + + it('draws once at layout width while a parent is scaled by an enter animation', async () => { + wrapper.style.transform = 'scale(0.9)'; + host.data.set(OVERALL); + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + + const first = svg(); + expect(Number(first!.getAttribute('width'))).toBe(480); + first!.setAttribute('data-probe', 'first'); + + wrapper.style.transform = ''; + await settle(RESIZE_MS); + + expect(svg()!.getAttribute('data-probe'), 'no second draw').toBe('first'); + }); + + it('draws data that arrived while the step was hidden once it is back', async () => { + host.data.set(OVERALL); + host.config.set({ ...VERTICAL }); + await settle(RENDER_MS); + expect(bars()).toBe(OVERALL.bins.length); + + wrapper.style.display = 'none'; + host.data.set(SWAPPED); + await settle(RENDER_MS); + + wrapper.style.display = ''; + await settle(RESIZE_MS); + + expect(bars()).toBe(SWAPPED.bins.length); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.ts b/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.ts index 3b7f1a353..0794e77a1 100644 --- a/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.ts +++ b/src/app/pages/experiment-studio/visualisations/histogram/histogram.component.ts @@ -1,46 +1,127 @@ -import { Component, ElementRef, Input, OnChanges, SimpleChanges } from '@angular/core'; -import { createHistogram } from './histogram-chart'; +import { + afterNextRender, + ChangeDetectionStrategy, + Component, + ElementRef, + effect, + inject, + input, + NgZone, + OnDestroy, +} from '@angular/core'; +import { observeSize } from '../../../../core/observe-resize.util'; +import { createHistogram, HistogramChartData } from './histogram-chart'; + +export interface HistogramChartConfig { + color?: string; + orientation?: 'vertical' | 'horizontal'; + /** When false, keep privacy-null edge bins (Data Handling). Default trims them. */ + clipNullEdges?: boolean; +} + +/** Matches the sibling D3 charts: redraw once the resizing has settled. */ +const RESIZE_DEBOUNCE_MS = 150; + +/** ResizeObserver floors a fractional width; clientWidth rounds it. */ +const WIDTH_TOLERANCE_PX = 1; @Component({ selector: 'app-histogram', - standalone: true, templateUrl: './histogram.component.html', - styleUrls: ['./histogram.component.css'], + styleUrl: './histogram.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class HistogramComponent implements OnChanges { - @Input() data: { bins: string[]; counts: number[]; variableName: string; variableType?: string } | null = null; // Data for histogram - @Input() config: { color?: string; width?: number; height?: number } = {}; // Configuration for the graph +export class HistogramComponent implements OnDestroy { + private elementRef = inject(ElementRef); + private ngZone = inject(NgZone); + private viewReady = false; + + readonly data = input<HistogramChartData | null>(null); + readonly config = input<HistogramChartConfig>({}); isLoading = false; - constructor(private elementRef: ElementRef) { } + /** What the drawing in the DOM was built from, and the width it was measured at. */ + private renderedData: HistogramChartData | null = null; + private renderedConfigKey = ''; + private renderedWidth = 0; - ngOnChanges(changes: SimpleChanges): void { - this.isLoading = true; // Show loading only if empty - if (changes['data'] && changes['data'].currentValue) { + private stopObservingSize: (() => void) | undefined; + + constructor() { + afterNextRender(() => { + this.viewReady = true; this.renderHistogram(); - this.isLoading = false; - } + const container = this.chartContainer(); + if (container) { + // Width only: the render writes an inline height, which would otherwise feed + // this callback back to itself. + this.stopObservingSize = observeSize(container, this.ngZone, RESIZE_DEBOUNCE_MS, (width) => { + if (width === 0 || Math.abs(width - this.renderedWidth) <= WIDTH_TOLERANCE_PX) { + return; + } + this.renderHistogram(); + }); + } + }); + + effect(() => { + this.data(); + this.config(); + if (this.viewReady) { + this.renderHistogram(); + } + }); + } + + ngOnDestroy(): void { + this.stopObservingSize?.(); } renderHistogram(): void { - if (!this.data || !this.data.bins || !this.data.counts) { + const data = this.data(); + if (!data || !data.bins || !data.counts) { return; } - const container = this.elementRef.nativeElement.querySelector('#histogram-chart'); + const container = this.chartContainer(); if (!container) { console.error('Histogram container not found.'); return; } - const binsCount = this.data.bins.length; - const baseRect = container.parentElement?.getBoundingClientRect(); - const baseWidth = baseRect?.width; - const baseHeight = baseRect?.height; - createHistogram(this.data, container, { - ...this.config, - skipEveryOtherLabel: binsCount > 6, - }); // Call D3 rendering logic + const config = this.config(); + const configKey = this.describeConfig(config); + const isStale = data !== this.renderedData || configKey !== this.renderedConfigKey; + // Layout width, not getBoundingClientRect: the tab-enter animation scales the + // panel, and a scaled measurement disagrees with the observer's and redraws. + const measuredWidth = container.clientWidth; + + // A Studio step hidden with `display: none` measures 0x0 and reports zeroed + // getBBox() sizes, so drawing here would collapse the chart to a few pixels. + // Keep the existing drawing; the observer redraws once the step is visible, + // and forgetting the rendered width is what makes that redraw happen. + if (measuredWidth === 0) { + if (isStale) { + this.renderedWidth = 0; + } + return; + } + + if (!isStale && Math.abs(measuredWidth - this.renderedWidth) <= WIDTH_TOLERANCE_PX) { + return; + } + + createHistogram(data, container, { ...config }); + this.renderedData = data; + this.renderedConfigKey = configKey; + this.renderedWidth = measuredWidth; + } + + private chartContainer(): HTMLElement | null { + return this.elementRef.nativeElement.querySelector('#histogram-chart'); + } + private describeConfig(config: HistogramChartConfig): string { + return [config.color, config.orientation, config.clipNullEdges === false ? 'full' : 'clip'].join('|'); } } diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/README.md b/src/app/pages/experiment-studio/visualisations/metadata-browser/README.md new file mode 100644 index 000000000..ce378bbe0 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/README.md @@ -0,0 +1,39 @@ +# Metadata Browser + +The metadata browser is an alternative to the bubble chart for exploring a pathology +`CDEsMetadata.json` hierarchy in Experiment Studio. + +## Normalized metadata model + +`metadata-browser-normalizer.ts` converts the existing `D3HierarchyNode` tree into +a normalized index with stable group and variable IDs, parent links, child group +lists, direct variable lists, breadcrumb labels, direct counts, and total variable +counts. Variables keep their original `D3HierarchyNode` reference so existing +selection, histogram, and parameter-list flows continue to receive the same node +shape as the bubble chart. + +## Hierarchy navigation model + +The browser currently supports three modes: + +- **Tree**: BioPortal-style ontology browser with a keyboard-accessible group + tree and immediate group contents. This is the default and the best fit for + clinical metadata review because it keeps navigation explicit and uses the + shared Experiment Studio search bar. +- **Collapsible**: D3 node-link hierarchy visualization for interactive visual + traversal. It is best when users want to see how branches unfold from the + selected data model while keeping variable labels readable. +- **Bubble**: The existing D3 bubble chart overview. + +## Mixed-node support + +Groups may contain both child groups and direct variables. The normalized model +stores these separately, and the Tree browser shows mixed nodes without assuming +variables only appear at leaf groups. + +## Search behavior + +The shared Experiment Studio search bar is shown above every visualization mode. +It searches variables and groups, then emits the original node to the active +visualization and Variables panel. Tree and Collapsible expand to the selected +path; Bubble keeps its zoom/highlight behavior. diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.css b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.css new file mode 100644 index 000000000..c75e3076d --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.css @@ -0,0 +1,179 @@ +:host { + display: block; + height: 100%; + min-height: 0; +} + +.collapsible-tree-browser { + display: flex; + flex-direction: column; + height: 100%; + min-height: 0; + background: + linear-gradient(180deg, rgba(248, 250, 252, 0.92), rgba(255, 255, 255, 0.96)); + color: var(--text-main); +} + +.chart-shell { + position: relative; + flex: 1; + min-height: 0; + overflow: hidden; +} + +.chart-canvas { + width: 100%; + height: 100%; + min-height: 0; +} + +.tree-error { + position: absolute; + inset: 0; + display: grid; + place-items: center; + padding: 24px; + color: #64748b; + font-size: 0.84rem; + text-align: center; +} + +:host ::ng-deep .collapsible-tree-svg { + display: block; + width: 100%; + height: 100%; + cursor: grab; + background: + linear-gradient(rgba(226, 232, 240, 0.48) 1px, transparent 1px), + linear-gradient(90deg, rgba(226, 232, 240, 0.48) 1px, transparent 1px); + background-size: 28px 28px; +} + +:host ::ng-deep .collapsible-tree-svg:active { + cursor: grabbing; +} + +:host ::ng-deep .tree-link { + fill: none; + stroke: #cbd5e1; + stroke-width: 1.7px; +} + +:host ::ng-deep .collapsible-node { + cursor: pointer; + outline: none; +} + +:host ::ng-deep .collapsible-node .node-card { + fill: #ffffff; + stroke: #dbe3ef; + stroke-width: 1px; +} + +:host ::ng-deep .collapsible-node.group-node .node-card { + fill: var(--bg-neutral); +} + +:host ::ng-deep .collapsible-node.variable-node .node-card { + fill: #ffffff; +} + +:host ::ng-deep .collapsible-node:hover .node-card, +:host ::ng-deep .collapsible-node:focus-visible .node-card { + stroke: var(--primary-color); + stroke-width: 1.8px; +} + +:host ::ng-deep .collapsible-node.highlighted .node-card { + fill: #eef2ff; + stroke: var(--primary-color); + stroke-width: 2px; +} + +:host ::ng-deep .collapsible-node.selected-variable .node-card { + fill: #fff7ed; + stroke: var(--accent-color); +} + +/* Not present in every selected dataset: grayed out and inert (see variable-availability.utils). */ +:host ::ng-deep .collapsible-node.unavailable-variable { + cursor: not-allowed; + opacity: var(--unavailable-opacity); +} + +:host ::ng-deep .collapsible-node.unavailable-variable .node-card, +:host ::ng-deep .collapsible-node.unavailable-variable:hover .node-card, +:host ::ng-deep .collapsible-node.unavailable-variable:focus-visible .node-card { + fill: var(--unavailable-fill); + stroke: var(--unavailable-border); + stroke-width: 1px; + stroke-dasharray: 4 3; +} + +:host ::ng-deep .collapsible-node.unavailable-variable .node-anchor { + fill: var(--unavailable-border); +} + +:host ::ng-deep .collapsible-node.unavailable-variable .node-label, +:host ::ng-deep .collapsible-node.unavailable-variable .node-meta { + fill: var(--unavailable-text); +} + +:host ::ng-deep .node-anchor { + fill: #94a3b8; +} + +:host ::ng-deep .group-node .node-anchor { + fill: var(--primary-color); +} + +:host ::ng-deep .variable-node .node-anchor { + fill: #16a34a; +} + +:host ::ng-deep .node-label { + fill: var(--text-main); + font-size: 11px; + font-weight: 760; + pointer-events: none; +} + +:host ::ng-deep .node-meta { + fill: #64748b; + font-size: 9px; + font-weight: 620; + pointer-events: none; +} + +:host ::ng-deep .node-chevron { + fill: #64748b; + font-size: 15px; + font-weight: 800; + pointer-events: none; +} + +:host ::ng-deep .collapsible-node.collapsed .node-chevron { + fill: var(--primary-color); +} + +:host ::ng-deep .collapsible-node.expanded .node-chevron { + fill: #64748b; +} + +::ng-deep .collapsible-tree-tooltip { + position: fixed; + z-index: 10000; + max-width: 260px; + display: flex; + flex-direction: column; + gap: 5px; + padding: 10px 12px; + border: 1px solid rgba(148, 163, 184, 0.4); + border-radius: 7px; + background: rgba(255, 255, 255, 0.96); + color: var(--text-main); + font-size: 0.76rem; + line-height: 1.35; + box-shadow: 0 14px 28px rgba(15, 23, 42, 0.18); + pointer-events: none; +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.html b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.html new file mode 100644 index 000000000..7d04dc10a --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.html @@ -0,0 +1,8 @@ +<div class="collapsible-tree-browser"> + <div class="chart-shell"> + <div #chartCanvas class="chart-canvas" aria-label="Collapsible metadata hierarchy"></div> + @if (error()) { + <div class="tree-error" role="status">{{ error() }}</div> + } + </div> +</div> diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.spec.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.spec.ts new file mode 100644 index 000000000..09bd776e7 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.spec.ts @@ -0,0 +1,174 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { D3HierarchyNode } from '../../../../../models/data-model.interface'; +import { CollapsibleTreeBrowserComponent } from './collapsible-tree-browser.component'; + +describe('CollapsibleTreeBrowserComponent', () => { + const model: D3HierarchyNode = { + label: 'Stroke 3.7', + code: 'stroke', + children: [ + { + label: 'Demographics', + code: 'demographics', + children: [ + { + label: 'Sex', + code: 'sex', + description: 'Patient sex', + type: 'nominal', + }, + ], + }, + { + label: 'Age', + code: 'age', + description: 'Patient age', + type: 'continuous', + units: 'years', + }, + ], + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [CollapsibleTreeBrowserComponent], + providers: [provideZonelessChangeDetection()], + }); + }); + + it('emits the original node when selecting a visible variable', () => { + const fixture = TestBed.createComponent(CollapsibleTreeBrowserComponent); + const component = fixture.componentInstance; + const age = model.children![1]; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + vi.spyOn(component.selectedNodeChange, 'emit').mockReturnValue(undefined); + + const variableNode = findRenderedNode(fixture.nativeElement, 'Age'); + variableNode.dispatchEvent(new MouseEvent('click', { bubbles: true })); + + expect(component.selectedNodeChange.emit).toHaveBeenCalledWith(age); + }); + + it('double-click emits the original variable for the add action', () => { + const fixture = TestBed.createComponent(CollapsibleTreeBrowserComponent); + const component = fixture.componentInstance; + const age = model.children![1]; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + vi.spyOn(component.nodeDoubleClicked, 'emit').mockReturnValue(undefined); + + const variableNode = findRenderedNode(fixture.nativeElement, 'Age'); + variableNode.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })); + + expect(component.nodeDoubleClicked.emit).toHaveBeenCalledWith(age); + }); + + it('expands a highlighted variable inside a collapsed branch', () => { + const fixture = TestBed.createComponent(CollapsibleTreeBrowserComponent); + fixture.componentRef.setInput('data', model); + fixture.componentRef.setInput('highlightNode', model.children?.[0].children?.[0]); + fixture.detectChanges(); + + expect(fixture.nativeElement.textContent).toContain('Sex'); + }); + + it('keeps an externally highlighted group selected on initial render', () => { + const fixture = TestBed.createComponent(CollapsibleTreeBrowserComponent); + const group = model.children![0]; + fixture.componentRef.setInput('data', model); + fixture.componentRef.setInput('highlightNode', group); + fixture.detectChanges(); + + const highlightedNode = fixture.nativeElement.querySelector('g.collapsible-node.highlighted'); + expect(highlightedNode?.textContent).toContain('Demographics'); + }); + + it('skips rebuilds for unchanged observer sizes and refits on real resizes', async () => { + type CaptureCallback = (entries: ResizeObserverEntry[]) => void; + let observerCallback: CaptureCallback | undefined; + const globals = globalThis as Record<string, unknown>; + const previousObserver = globals['ResizeObserver']; + globals['ResizeObserver'] = class { + constructor(callback: CaptureCallback) { + observerCallback = callback; + } + observe(): void {} + unobserve(): void {} + disconnect(): void {} + }; + const restoreObserver = (): void => { + if (previousObserver === undefined) { + delete globals['ResizeObserver']; + } else { + globals['ResizeObserver'] = previousObserver; + } + }; + + try { + const fixture = TestBed.createComponent(CollapsibleTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + const svg = fixture.nativeElement.querySelector('svg.collapsible-tree-svg'); + expect(svg).toBeTruthy(); + const renderer = (component as any).renderer; + vi.spyOn(renderer, 'resize').mockReturnValue(undefined); + const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + + // The component compares each observer size against the box it actually drew + // into, so the fake observer and the measured box have to agree. + const canvas = fixture.nativeElement.querySelector('.chart-canvas') as HTMLElement; + let measuredBox = { width: 0, height: 0 }; + vi.spyOn(canvas, 'getBoundingClientRect').mockImplementation(() => ({ + width: measuredBox.width, + height: measuredBox.height, + top: 0, + left: 0, + right: measuredBox.width, + bottom: measuredBox.height, + x: 0, + y: 0, + }) as DOMRect); + + // Same as the initial (zero) snapshot: no work scheduled. + observerCallback!([{ contentRect: { width: 0, height: 0 } }] as unknown as ResizeObserverEntry[]); + await wait(200); + expect(renderer.resize).not.toHaveBeenCalled(); + + // Real size change - the step coming back into view after a hidden draw: + // debounced refit, not a rebuild. + measuredBox = { width: 800, height: 600 }; + observerCallback!([{ contentRect: { width: 800, height: 600 } }] as unknown as ResizeObserverEntry[]); + await wait(200); + expect(renderer.resize).toHaveBeenCalledTimes(1); + + // Unchanged size afterwards: ignored. + observerCallback!([{ contentRect: { width: 800, height: 600 } }] as unknown as ResizeObserverEntry[]); + await wait(200); + expect(renderer.resize).toHaveBeenCalledTimes(1); + + expect(fixture.nativeElement.querySelector('svg.collapsible-tree-svg')).toBe(svg); + } finally { + restoreObserver(); + } + }); + + it('renders mixed nodes with child groups and direct variables', () => { + const fixture = TestBed.createComponent(CollapsibleTreeBrowserComponent); + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + expect(fixture.nativeElement.textContent).toContain('Demographics'); + expect(fixture.nativeElement.textContent).toContain('Age'); + }); +}); + +function findRenderedNode(host: HTMLElement, label: string): SVGGElement { + const nodes = Array.from(host.querySelectorAll<SVGGElement>('g.collapsible-node')); + const node = nodes.find((item) => item.textContent?.includes(label)); + expect(node, `Expected rendered node for ${label}`).toBeDefined(); + return node!; +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.ts new file mode 100644 index 000000000..2f5094eb7 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-browser.component.ts @@ -0,0 +1,159 @@ +import { + AfterViewInit, + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + ElementRef, + NgZone, + OnDestroy, + effect, + inject, + input, + output, + signal, + viewChild, +} from '@angular/core'; +import { D3HierarchyNode } from '../../../../../models/data-model.interface'; +import { observeSize } from '../../../../../core/observe-resize.util'; +import { + EMPTY_UNAVAILABLE_VARIABLES, + UnavailableVariablesMap, +} from '../../../../../core/variable-availability.utils'; +import { + CollapsibleTreeRenderer, + createCollapsibleTree, +} from './collapsible-tree-renderer'; + +const EMPTY_TREE: D3HierarchyNode = { + label: 'Data model', + code: 'data-model', + children: [], +}; + +@Component({ + selector: 'app-collapsible-tree-browser', + templateUrl: './collapsible-tree-browser.component.html', + styleUrl: './collapsible-tree-browser.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class CollapsibleTreeBrowserComponent implements AfterViewInit, OnDestroy { + readonly data = input<D3HierarchyNode | null>(null); + readonly highlightNode = input<D3HierarchyNode | null>(null); + readonly selectedVariables = input<D3HierarchyNode[]>([]); + readonly unavailableVariables = input<UnavailableVariablesMap>(EMPTY_UNAVAILABLE_VARIABLES); + readonly selectedNodeChange = output<D3HierarchyNode>(); + readonly nodeDoubleClicked = output<D3HierarchyNode>(); + + readonly chartCanvas = viewChild<ElementRef<HTMLElement>>('chartCanvas'); + readonly error = signal<string | null>(null); + + private readonly ngZone = inject(NgZone); + private readonly cdr = inject(ChangeDetectorRef); + private renderer: CollapsibleTreeRenderer | null = null; + private stopObservingSize: (() => void) | undefined; + private viewReady = false; + private lastDataRef: D3HierarchyNode | null = null; + private skipNextHighlightExpand = false; + /** Size the drawing in the DOM was measured at; zero while its step is hidden. */ + private renderedSize = { width: 0, height: 0 }; + + constructor() { + effect(() => { + const data = this.data(); + if (!this.viewReady) return; + if (data !== this.lastDataRef) { + this.render(); + } + }); + + // Separate from the selection effect so a dataset change regrays nodes without recentering. + effect(() => { + const unavailable = this.unavailableVariables(); + this.renderer?.setUnavailableVariables(unavailable); + }); + + effect(() => { + const highlightNode = this.highlightNode(); + const renderer = this.renderer; + if (!renderer) return; + renderer.refreshSelection({ + selectedVariables: this.selectedVariables(), + highlightNode, + }); + if (highlightNode) { + if (this.skipNextHighlightExpand) { + this.skipNextHighlightExpand = false; + return; + } + renderer.expandToNode(highlightNode); + } + }); + } + + ngAfterViewInit(): void { + this.viewReady = true; + this.render(); + const canvas = this.chartCanvas()?.nativeElement; + if (!canvas) return; + + this.stopObservingSize = observeSize(canvas, this.ngZone, 120, (width, height) => { + // Zero size is the step being hidden - never a size worth drawing for. + if (width === 0 || height === 0) return; + // Compared against the last actual draw, so a tree built while the step was + // hidden is re-laid-out when the step comes back. + if (width === this.renderedSize.width && height === this.renderedSize.height) return; + + this.renderer?.resize(); + this.rememberRenderedSize(); + }); + } + + ngOnDestroy(): void { + this.renderer?.destroy(); + this.stopObservingSize?.(); + } + + /** Records the box the drawing that follows was measured against. */ + private rememberRenderedSize(): void { + const rect = this.chartCanvas()?.nativeElement?.getBoundingClientRect(); + this.renderedSize = rect + ? { width: Math.floor(rect.width), height: Math.floor(rect.height) } + : { width: 0, height: 0 }; + } + + private render(): void { + const canvas = this.chartCanvas()?.nativeElement; + if (!canvas) return; + + const data = this.data() ?? EMPTY_TREE; + this.lastDataRef = data; + this.renderer?.destroy(); + this.renderer = null; + + if (!data.children?.length) { + this.error.set('No metadata available for visualization.'); + this.cdr.markForCheck(); + return; + } + + this.error.set(null); + this.rememberRenderedSize(); + this.renderer = createCollapsibleTree(data, canvas, { + selectedVariables: this.selectedVariables(), + highlightNode: this.highlightNode(), + unavailableVariables: this.unavailableVariables(), + onNodeClick: (node) => { + // This selection originates from the renderer itself; avoid immediately + // replaying expandToNode through the highlight effect. + this.skipNextHighlightExpand = true; + this.selectedNodeChange.emit(node); + this.cdr.markForCheck(); + }, + onNodeDoubleClick: (node) => { + this.nodeDoubleClicked.emit(node); + this.cdr.markForCheck(); + }, + }); + this.cdr.markForCheck(); + } +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-renderer.spec.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-renderer.spec.ts new file mode 100644 index 000000000..b514bf1ba --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-renderer.spec.ts @@ -0,0 +1,298 @@ +import * as d3 from 'd3'; +import { D3HierarchyNode } from '../../../../../models/data-model.interface'; +import { + cloneNode, + createCollapsibleTree, + findHierarchyNode, + findHierarchyNodeByPath, + focalPointForExpandedGroup, + initializeCollapse, +} from './collapsible-tree-renderer'; + +describe('collapsible tree renderer helpers', () => { + const model: D3HierarchyNode = { + label: 'Stroke 3.7', + code: 'stroke', + children: [ + { + label: 'Vitals', + code: 'vitals', + children: [ + { + label: 'Metabolic', + code: 'metabolic', + children: [ + { + label: 'Glucose', + code: 'glucose', + type: 'continuous', + }, + ], + }, + ], + }, + ], + }; + + it('clones input hierarchy without sharing children arrays', () => { + const cloned = cloneNode(model); + + expect(cloned).not.toBe(model); + expect(cloned.children).not.toBe(model.children); + expect(cloned.children?.[0]).not.toBe(model.children?.[0]); + }); + + it('keeps root and first level open while collapsing deeper branches', () => { + const root = d3.hierarchy(cloneNode(model), (node) => node.children) as any; + + initializeCollapse(root); + + expect(root.children?.length).toBe(1); + expect(root.children[0].children?.length).toBe(1); + expect(root.children[0].children[0].children).toBeUndefined(); + expect(root.children[0].children[0]._children?.length).toBe(1); + }); + + it('finds nodes inside collapsed branches for external highlighting', () => { + const root = d3.hierarchy(cloneNode(model), (node) => node.children) as any; + initializeCollapse(root); + + const target = findHierarchyNode(root, { label: 'Glucose', code: 'glucose', type: 'continuous' }); + + expect(target?.data.label).toBe('Glucose'); + }); + + it('finds duplicate group labels by full path instead of the first code match', () => { + const duplicateModel: typeof model = { + label: 'Stroke 3.7', + code: 'stroke', + children: [ + { + label: 'Vitals', + code: 'vitals', + children: [ + { + label: 'Arterial', + code: 'arterial', + children: [{ label: 'MAP', code: 'map', type: 'continuous' }], + }, + ], + }, + { + label: 'Other', + code: 'other', + children: [ + { + label: 'Arterial', + code: 'arterial', + children: [{ label: 'DBP', code: 'dbp', type: 'continuous' }], + }, + ], + }, + ], + }; + const root = d3.hierarchy(cloneNode(duplicateModel), (node) => node.children) as any; + initializeCollapse(root); + + const target = findHierarchyNodeByPath(root, 'Stroke 3.7 > Other > Arterial'); + + expect(target?.data.label).toBe('Arterial'); + expect(target?.parent?.data.label).toBe('Other'); + }); + + it('schedules auto-fit when opening without a highlighted node', () => { + const container = createContainer(); + const onAutoFitScheduled = vi.fn(); + const renderer = createCollapsibleTree(model, container, { + onNodeClick: () => undefined, + onNodeDoubleClick: () => undefined, + onAutoFitScheduled, + }); + + expect(onAutoFitScheduled).toHaveBeenCalled(); + renderer.destroy(); + container.remove(); + }); + + it('centers a highlighted group instead of scheduling initial auto-fit', () => { + const group = model.children![0]; + const container = createContainer(); + const onAutoFitScheduled = vi.fn(); + const onNodeCentered = vi.fn(); + + const renderer = createCollapsibleTree(model, container, { + highlightNode: group, + onNodeClick: () => undefined, + onNodeDoubleClick: () => undefined, + onAutoFitScheduled, + onNodeCentered, + }); + + expect(onAutoFitScheduled).not.toHaveBeenCalled(); + expect(onNodeCentered).toHaveBeenCalledWith(group); + renderer.destroy(); + container.remove(); + }); + + it('cancels pending auto-fit when expanding to a selected node', () => { + const container = createContainer(); + const onAutoFitCanceled = vi.fn(); + const renderer = createCollapsibleTree(model, container, { + onNodeClick: () => undefined, + onNodeDoubleClick: () => undefined, + onAutoFitCanceled, + }); + + renderer.expandToNode(model.children![0]); + + expect(onAutoFitCanceled).toHaveBeenCalled(); + renderer.destroy(); + container.remove(); + }); + + it('targets expanded group content instead of the group node position', () => { + const root = d3.hierarchy(cloneNode(model), (node) => node.children) as any; + const group = root.children![0]; + group.x = 80; + group.y = 120; + const child = group.children![0]; + child.x = 140; + child.y = 380; + const grandchild = child.children![0]; + grandchild.x = 200; + grandchild.y = 380; + + const focal = focalPointForExpandedGroup(group); + + expect(focal.x).toBe(170); + expect(focal.y).toBe(380); + expect(focal.y).not.toBe(group.y); + }); + + it('resizes the svg in place without rebuilding the tree', () => { + const rect = { width: 900, height: 600 }; + const container = document.createElement('div'); + vi.spyOn(container, 'getBoundingClientRect').mockImplementation(() => ({ + ...rect, + x: 0, + y: 0, + top: 0, + right: rect.width, + bottom: rect.height, + left: 0, + toJSON: () => undefined, + } as DOMRect)); + document.body.appendChild(container); + + const renderer = createCollapsibleTree(model, container, { + onNodeClick: () => undefined, + onNodeDoubleClick: () => undefined, + }); + + const svg = container.querySelector('svg.collapsible-tree-svg') as SVGElement; + const firstNodes = Array.from(container.querySelectorAll('g.collapsible-node')); + expect(svg.getAttribute('width')).toBe('900'); + expect(firstNodes.length).toBeGreaterThan(0); + + rect.width = 1200; + rect.height = 800; + renderer.resize(); + + expect(svg.getAttribute('width')).toBe('1200'); + expect(svg.getAttribute('height')).toBe('800'); + expect(svg.getAttribute('viewBox')).toBe('0 0 1200 800'); + const resizedNodes = Array.from(container.querySelectorAll('g.collapsible-node')); + expect(resizedNodes.length).toBe(firstNodes.length); + expect(resizedNodes[0]).toBe(firstNodes[0]); + renderer.destroy(); + container.remove(); + }); + + it('cancels pending auto-fit when destroyed before the next animation frame', () => { + const container = createContainer(); + const onAutoFitCanceled = vi.fn(); + const renderer = createCollapsibleTree(model, container, { + onNodeClick: () => undefined, + onNodeDoubleClick: () => undefined, + onAutoFitCanceled, + }); + + renderer.destroy(); + + expect(onAutoFitCanceled).toHaveBeenCalled(); + container.remove(); + }); + +describe('collapsible tree unavailable variables', () => { + const model: D3HierarchyNode = { + label: 'Root', + code: 'root', + children: [ + { + label: 'Demographics', + code: 'demographics', + children: [ + { label: 'Sex', code: 'sex', type: 'nominal' }, + { label: 'Age', code: 'age', type: 'continuous' }, + ], + }, + ], + }; + + const nodeFor = (container: HTMLElement, label: string): SVGGElement => + Array.from(container.querySelectorAll<SVGGElement>('g.collapsible-node')) + .find((node) => node.querySelector('.node-label')?.textContent === label)!; + + it('grays unavailable variables, ignores their clicks and lists the datasets on hover', () => { + const container = createContainer(); + const onNodeClick = vi.fn(); + const onNodeDoubleClick = vi.fn(); + const renderer = createCollapsibleTree(model, container, { + unavailableVariables: new Map([['sex', ['Dataset B']]]), + onNodeClick, + onNodeDoubleClick, + }); + + const sex = nodeFor(container, 'Sex'); + const age = nodeFor(container, 'Age'); + expect(sex.classList).toContain('unavailable-variable'); + expect(sex.getAttribute('aria-disabled')).toBe('true'); + expect(age.classList).not.toContain('unavailable-variable'); + expect(nodeFor(container, 'Demographics').classList).not.toContain('unavailable-variable'); + + sex.dispatchEvent(new MouseEvent('click', { bubbles: true })); + sex.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })); + sex.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); + expect(onNodeClick).not.toHaveBeenCalled(); + expect(onNodeDoubleClick).not.toHaveBeenCalled(); + + sex.dispatchEvent(new MouseEvent('mouseenter')); + expect(document.querySelector('.collapsible-tree-tooltip')?.textContent).toContain('Not available in: Dataset B'); + + age.dispatchEvent(new MouseEvent('click', { bubbles: true })); + expect(onNodeClick).toHaveBeenCalledTimes(1); + + renderer.setUnavailableVariables(new Map()); + expect(nodeFor(container, 'Sex').classList).not.toContain('unavailable-variable'); + renderer.destroy(); + container.remove(); + }); +}); +}); + +function createContainer(): HTMLElement { + const container = document.createElement('div'); + vi.spyOn(container, 'getBoundingClientRect').mockReturnValue({ + x: 0, + y: 0, + top: 0, + right: 900, + bottom: 600, + left: 0, + width: 900, + height: 600, + toJSON: () => undefined, + } as DOMRect); + document.body.appendChild(container); + return container; +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-renderer.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-renderer.ts new file mode 100644 index 000000000..a1045e16c --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/collapsible-tree-browser/collapsible-tree-renderer.ts @@ -0,0 +1,616 @@ +import * as d3 from 'd3'; +import { D3HierarchyNode } from '../../../../../models/data-model.interface'; +import { + EMPTY_UNAVAILABLE_VARIABLES, + UnavailableVariablesMap, + unavailableDatasetsFor, + unavailableVariableTooltip, +} from '../../../../../core/variable-availability.utils'; + +interface CollapsibleTreeSelectionOptions { + selectedVariables?: D3HierarchyNode[]; + highlightNode?: D3HierarchyNode | null; +} + +export interface CollapsibleTreeRenderer { + expandToNode: (node: D3HierarchyNode) => void; + refreshSelection: (options?: CollapsibleTreeSelectionOptions) => void; + setUnavailableVariables: (unavailable: UnavailableVariablesMap | null | undefined) => void; + fitView: () => void; + resize: () => void; + destroy: () => void; +} + +interface CollapsibleTreeOptions extends CollapsibleTreeSelectionOptions { + /** Variables drawn grayed out that ignore clicks, double-clicks and Enter/Space. */ + unavailableVariables?: UnavailableVariablesMap; + onNodeClick: (node: D3HierarchyNode) => void; + onNodeDoubleClick: (node: D3HierarchyNode) => void; + onAutoFitScheduled?: () => void; + onAutoFitCanceled?: () => void; + onNodeCentered?: (node: D3HierarchyNode) => void; +} + +interface RenderNode extends D3HierarchyNode { + _children?: RenderNode[]; + children?: RenderNode[]; +} + +type TreeDatum = d3.HierarchyPointNode<RenderNode> & { + x0?: number; + y0?: number; + _children?: Array<d3.HierarchyPointNode<RenderNode>>; +}; + +const nodeWidth = 220; +const nodeHeight = 34; +const levelWidth = 260; +const rowGap = 22; +const margin = { top: 40, right: 260, bottom: 40, left: 46 }; + +export function createCollapsibleTree( + data: D3HierarchyNode, + container: HTMLElement, + options: CollapsibleTreeOptions +): CollapsibleTreeRenderer { + container.innerHTML = ''; + + const root = d3.hierarchy<RenderNode>(data as RenderNode, (node) => node.children) as TreeDatum; + root.x0 = 0; + root.y0 = 0; + initializeCollapse(root); + + let selectedVariables = toCodeSet(options.selectedVariables); + let unavailableVariables = options.unavailableVariables ?? EMPTY_UNAVAILABLE_VARIABLES; + const unavailableIn = (node: TreeDatum): readonly string[] => + isVariable(node) ? unavailableDatasetsFor(unavailableVariables, node.data) : []; + const isUnavailable = (node: TreeDatum): boolean => unavailableIn(node).length > 0; + let highlightedCode = codeOf(options.highlightNode); + let highlightedPath = pathOf(options.highlightNode); + let bounds = measureContainer(container); + let treeSize = { width: bounds.width, height: bounds.height }; + let pendingAutoFitFrame: number | null = null; + + const svg = d3 + .select(container) + .append('svg') + .attr('class', 'collapsible-tree-svg') + .attr('role', 'img') + .attr('aria-label', 'Collapsible metadata tree') + .attr('width', bounds.width) + .attr('height', bounds.height) + .attr('viewBox', `0 0 ${bounds.width} ${bounds.height}`); + + const zoomLayer = svg.append('g').attr('class', 'collapsible-tree-layer'); + const linkLayer = zoomLayer.append('g').attr('class', 'collapsible-links'); + const nodeLayer = zoomLayer.append('g').attr('class', 'collapsible-nodes'); + + const tooltip = d3.select('body') + .append('div') + .attr('class', 'collapsible-tree-tooltip') + .style('opacity', 0); + + const zoom = d3.zoom<SVGSVGElement, unknown>() + .scaleExtent([0.35, 2.2]) + .on('zoom', (event) => { + zoomLayer.attr('transform', event.transform.toString()); + }); + + svg.call(zoom); + + const tree = d3.tree<RenderNode>().nodeSize([nodeHeight + rowGap, levelWidth]); + let duration = 240; + + const update = (source: TreeDatum): void => { + bounds = measureContainer(container); + svg + .attr('width', bounds.width) + .attr('height', bounds.height) + .attr('viewBox', `0 0 ${bounds.width} ${bounds.height}`); + + const layoutRoot = tree(root); + const nodes = layoutRoot.descendants() as TreeDatum[]; + const links = layoutRoot.links(); + + const minX = d3.min(nodes, (node) => node.x) ?? 0; + const maxX = d3.max(nodes, (node) => node.x) ?? 0; + const maxY = d3.max(nodes, (node) => node.y) ?? 0; + treeSize = { + width: maxY + margin.left + margin.right, + height: maxX - minX + margin.top + margin.bottom, + }; + + nodes.forEach((node) => { + node.y = node.depth * levelWidth + margin.left; + node.x = node.x - minX + margin.top; + }); + + const nodeSelection = nodeLayer + .selectAll<SVGGElement, TreeDatum>('g.collapsible-node') + .data(nodes, (node) => stableKey(node.data)); + + const nodeEnter = nodeSelection.enter() + .append('g') + .attr('class', 'collapsible-node') + .attr('transform', `translate(${source.y0 ?? margin.left},${source.x0 ?? margin.top})`) + .attr('tabindex', 0) + .attr('role', 'button') + .attr('aria-label', (node) => ariaLabel(node)) + .on('click', (event, node) => { + event.stopPropagation(); + handleNodeClick(node); + }) + .on('dblclick', (event, node) => { + event.stopPropagation(); + if (isVariable(node) && !isUnavailable(node)) { + options.onNodeDoubleClick(node.data); + } + }) + .on('keydown', (event, node) => { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + handleNodeClick(node); + } + }) + .on('mouseenter', (event, node) => showTooltip(event, node, unavailableIn(node))) + .on('mousemove', (event) => moveTooltip(event)) + .on('mouseleave', hideTooltip); + + nodeEnter.append('rect') + .attr('class', 'node-card') + .attr('x', -nodeWidth / 2) + .attr('y', -nodeHeight / 2) + .attr('width', nodeWidth) + .attr('height', nodeHeight) + .attr('rx', 7); + + nodeEnter.append('circle') + .attr('class', 'node-anchor') + .attr('cx', -nodeWidth / 2 + 13) + .attr('r', 4.5); + + nodeEnter.append('text') + .attr('class', 'node-label') + .attr('x', -nodeWidth / 2 + 26) + .attr('y', -2) + .text((node) => truncateLabel(node.data.label)); + + nodeEnter.append('text') + .attr('class', 'node-meta') + .attr('x', -nodeWidth / 2 + 26) + .attr('y', 12) + .text((node) => nodeMeta(node)); + + nodeEnter.append('text') + .attr('class', 'node-chevron') + .attr('x', nodeWidth / 2 - 16) + .attr('y', 4) + .text((node) => chevron(node)); + + const nodeMerge = nodeEnter.merge(nodeSelection); + + nodeMerge + .attr('class', (node) => nodeClass(node)) + .attr('aria-disabled', (node) => (isUnavailable(node) ? 'true' : null)) + .attr('aria-expanded', (node) => hasChildren(node) ? String(!!node.children) : null) + .transition() + .duration(duration) + .attr('transform', (node) => `translate(${node.y},${node.x})`); + + nodeMerge.select<SVGTextElement>('text.node-meta').text((node) => nodeMeta(node)); + nodeMerge.select<SVGTextElement>('text.node-chevron').text((node) => chevron(node)); + + nodeSelection.exit() + .transition() + .duration(duration) + .attr('transform', `translate(${source.y},${source.x})`) + .style('opacity', 0) + .remove(); + + const linkSelection = linkLayer + .selectAll<SVGPathElement, d3.HierarchyPointLink<RenderNode>>('path.tree-link') + .data(links, (link) => stableKey(link.target.data)); + + linkSelection.enter() + .append('path') + .attr('class', 'tree-link') + .attr('d', () => diagonal({ source, target: source } as d3.HierarchyPointLink<RenderNode>)) + .merge(linkSelection) + .transition() + .duration(duration) + .attr('d', diagonal); + + linkSelection.exit() + .transition() + .duration(duration) + .attr('d', () => diagonal({ source, target: source } as d3.HierarchyPointLink<RenderNode>)) + .remove(); + + nodes.forEach((node) => { + node.x0 = node.x; + node.y0 = node.y; + }); + }; + + const handleNodeClick = (node: TreeDatum): void => { + if (isUnavailable(node)) return; + let shouldCenterAfterUpdate = false; + let didExpand = false; + if (!isVariable(node) && hasChildren(node)) { + if (node.children) { + node._children = node.children; + node.children = undefined; + } else if (node._children) { + node.children = node._children; + node._children = undefined; + didExpand = true; + } + update(node); + shouldCenterAfterUpdate = true; + } + refreshNodeClasses(); + options.onNodeClick(node.data); + if (shouldCenterAfterUpdate) { + window.setTimeout(() => { + if (didExpand) { + centerExpandedContent(node); + } else { + centerNode(node); + } + }, duration + 20); + } else { + centerNode(node); + } + }; + + const refreshNodeClasses = (): void => { + nodeLayer + .selectAll<SVGGElement, TreeDatum>('g.collapsible-node') + .attr('class', (node) => nodeClass(node)) + .attr('aria-disabled', (node) => (isUnavailable(node) ? 'true' : null)); + }; + + const fitView = (): void => { + const scale = Math.min( + 1.15, + Math.max(0.35, Math.min(bounds.width / Math.max(treeSize.width, 1), bounds.height / Math.max(treeSize.height, 1)) * 0.92) + ); + const translateX = Math.max(18, (bounds.width - treeSize.width * scale) / 2); + const translateY = Math.max(18, (bounds.height - treeSize.height * scale) / 2); + svg + .transition() + .duration(duration) + .call(zoom.transform, d3.zoomIdentity.translate(translateX, translateY).scale(scale)); + }; + + const cancelPendingAutoFit = (): void => { + if (pendingAutoFitFrame === null) return; + cancelAnimationFrame(pendingAutoFitFrame); + pendingAutoFitFrame = null; + options.onAutoFitCanceled?.(); + }; + + const scheduleAutoFit = (): void => { + cancelPendingAutoFit(); + pendingAutoFitFrame = requestAnimationFrame(() => { + pendingAutoFitFrame = null; + fitView(); + }); + options.onAutoFitScheduled?.(); + }; + + const expandToNode = (node: D3HierarchyNode): void => { + cancelPendingAutoFit(); + const target = findHierarchyNode(root, node); + if (!target) return; + target.ancestors().reverse().forEach((ancestor) => { + const treeNode = ancestor as TreeDatum; + if (treeNode._children) { + treeNode.children = treeNode._children; + treeNode._children = undefined; + } + }); + highlightedCode = codeOf(node); + highlightedPath = hierarchyPath(target); + update(target as TreeDatum); + centerNode(target as TreeDatum); + }; + + const centerOnPoint = (treeX: number, treeY: number, centeredNode?: D3HierarchyNode): void => { + const scale = 0.9; + const x = bounds.width / 2 - treeY * scale; + const y = bounds.height / 2 - treeX * scale; + svg + .transition() + .duration(duration) + .call(zoom.transform, d3.zoomIdentity.translate(x, y).scale(scale)); + if (centeredNode) { + options.onNodeCentered?.(centeredNode); + } + }; + + const centerNode = (node: TreeDatum): void => { + centerOnPoint(node.x ?? 0, node.y ?? 0, node.data); + }; + + const centerExpandedContent = (node: TreeDatum): void => { + const focal = focalPointForExpandedGroup(node); + centerOnPoint(focal.x, focal.y, node.data); + }; + + const refreshSelection = (selectionOptions?: CollapsibleTreeSelectionOptions): void => { + selectedVariables = toCodeSet(selectionOptions?.selectedVariables); + const highlight = selectionOptions?.highlightNode ?? null; + highlightedCode = codeOf(highlight); + highlightedPath = pathOf(highlight); + refreshNodeClasses(); + }; + + const setUnavailableVariables = (unavailable: UnavailableVariablesMap | null | undefined): void => { + unavailableVariables = unavailable ?? EMPTY_UNAVAILABLE_VARIABLES; + refreshNodeClasses(); + }; + + const resize = (): void => { + bounds = measureContainer(container); + svg + .attr('width', bounds.width) + .attr('height', bounds.height) + .attr('viewBox', `0 0 ${bounds.width} ${bounds.height}`); + fitView(); + }; + + update(root); + if (options.highlightNode && findHierarchyNode(root, options.highlightNode)) { + expandToNode(options.highlightNode); + } else { + scheduleAutoFit(); + } + + return { + expandToNode, + refreshSelection, + setUnavailableVariables, + fitView, + resize, + destroy: () => { + cancelPendingAutoFit(); + tooltip.remove(); + svg.on('.zoom', null); + container.innerHTML = ''; + }, + }; + + function nodeClass(node: TreeDatum): string { + const classes = ['collapsible-node']; + classes.push(isVariable(node) ? 'variable-node' : 'group-node'); + if (node.children) classes.push('expanded'); + if (!node.children && node._children) classes.push('collapsed'); + const code = codeOf(node.data); + const nodePath = hierarchyPath(node); + if (highlightedPath && nodePath === highlightedPath) { + classes.push('highlighted'); + } else if (!highlightedPath && code && code === highlightedCode) { + classes.push('highlighted'); + } + if (code && selectedVariables.has(code)) classes.push('selected-variable'); + if (isUnavailable(node)) classes.push('unavailable-variable'); + return classes.join(' '); + } +} + +export function cloneNode(node: D3HierarchyNode): RenderNode { + return { + ...node, + children: node.children?.map((child) => cloneNode(child)), + }; +} + +export function focalPointForExpandedGroup(node: TreeDatum): { x: number; y: number } { + const descendants = node.descendants().filter((descendant) => descendant !== node); + if (!descendants.length) { + return { x: node.x ?? 0, y: node.y ?? 0 }; + } + + const xs = descendants.map((descendant) => descendant.x ?? 0); + const ys = descendants.map((descendant) => descendant.y ?? 0); + return { + x: (Math.min(...xs) + Math.max(...xs)) / 2, + y: (Math.min(...ys) + Math.max(...ys)) / 2, + }; +} + +export function initializeCollapse(root: TreeDatum): void { + root.descendants().forEach((node) => { + if (node.depth > 1 && node.children) { + node._children = node.children as TreeDatum[]; + node.children = undefined; + } + }); +} + +export function findHierarchyNode(root: TreeDatum, target: D3HierarchyNode): TreeDatum | null { + const nodes = collectTreeNodes(root); + const byReference = nodes.find((node) => node.data === target); + if (byReference) { + return byReference; + } + + const targetPath = pathOf(target); + if (targetPath) { + const byPath = findHierarchyNodeByPath(root, targetPath); + if (byPath) { + return byPath; + } + } + + const targetCode = codeOf(target); + const targetLabel = normalizeString(target.label); + const matches = nodes.filter((node) => { + const nodeCode = codeOf(node.data); + if (targetCode && nodeCode === targetCode) return true; + return !!targetLabel && normalizeString(node.data.label) === targetLabel; + }); + + return matches.length === 1 ? matches[0] : null; +} + +export function findHierarchyNodeByPath(root: TreeDatum, path: string): TreeDatum | null { + const segments = path + .split('>') + .map((segment) => normalizeString(segment)) + .filter(Boolean); + if (!segments.length) { + return null; + } + + if (normalizeString(root.data.label) !== segments[0]) { + return null; + } + + let current: TreeDatum = root; + for (let index = 1; index < segments.length; index += 1) { + const segment = segments[index]; + const children = [...(current.children ?? []), ...(current._children ?? [])] as TreeDatum[]; + const next = children.find((child) => normalizeString(child.data.label) === segment); + if (!next) { + return null; + } + current = next; + } + + return current; +} + +function hierarchyPath(node: TreeDatum): string { + return node + .ancestors() + .reverse() + .map((ancestor) => normalizeString(ancestor.data.label)) + .filter(Boolean) + .join(' > '); +} + +function pathOf(node: D3HierarchyNode | null | undefined): string { + return normalizeString((node as D3HierarchyNode & { path?: string } | null | undefined)?.path); +} + +function collectTreeNodes(root: TreeDatum): TreeDatum[] { + const nodes: TreeDatum[] = []; + const visit = (node: TreeDatum): void => { + nodes.push(node); + ([...(node.children ?? []), ...(node._children ?? [])] as TreeDatum[]).forEach(visit); + }; + visit(root); + return nodes; +} + +function diagonal(link: d3.HierarchyPointLink<RenderNode>): string { + const source = link.source as TreeDatum; + const target = link.target as TreeDatum; + const midY = (source.y + target.y) / 2; + return `M${source.y},${source.x}C${midY},${source.x} ${midY},${target.x} ${target.y},${target.x}`; +} + +function isVariable(node: TreeDatum): boolean { + return !hasChildren(node) && !!node.data.type; +} + +function hasChildren(node: TreeDatum): boolean { + return !!node.children?.length || !!node._children?.length; +} + +function nodeMeta(node: TreeDatum): string { + if (isVariable(node)) { + const type = normalizeString(node.data.type); + return type || 'Variable'; + } + const variableCount = countVariables(node); + const childCount = (node.children?.length ?? 0) + (node._children?.length ?? 0); + return `${childCount} groups/items · ${variableCount} variables`; +} + +function countVariables(node: TreeDatum): number { + const children = [...(node.children ?? []), ...(node._children ?? [])] as TreeDatum[]; + if (!children.length) return isVariable(node) ? 1 : 0; + return children.reduce((sum, child) => sum + countVariables(child), 0); +} + +function chevron(node: TreeDatum): string { + if (isVariable(node)) return ''; + if (node.children?.length) return '−'; + if (node._children?.length) return '+'; + return ''; +} + +function stableKey(node: RenderNode): string { + return `${normalizeString(node.code)}:${normalizeString(node.label)}:${normalizeString(node.type)}`; +} + +function ariaLabel(node: TreeDatum): string { + return `${isVariable(node) ? 'Variable' : 'Group'} ${node.data.label ?? ''}`; +} + +function truncateLabel(value: unknown): string { + const label = normalizeString(value) || 'Untitled'; + return label.length > 31 ? `${label.slice(0, 28)}...` : label; +} + +function showTooltip(event: MouseEvent, node: TreeDatum, unavailableIn: readonly string[] = []): void { + const label = normalizeString(node.data.label) || 'Untitled'; + const type = normalizeString(node.data.type); + const description = normalizeString(node.data.description); + const tooltip = d3.select('.collapsible-tree-tooltip'); + + const appendRow = (name: string, value: string): void => { + const row = tooltip.append('span'); + row.append('b').text(name); + row.append('span').text(' ' + value); + }; + + tooltip.selectAll('*').remove(); + tooltip.append('strong').text(label); + if (type) appendRow('Type:', type); + if (description) appendRow('Description:', description); + if (unavailableIn.length) tooltip.append('span').text(unavailableVariableTooltip(unavailableIn)); + tooltip + .style('left', `${event.clientX + 12}px`) + .style('top', `${event.clientY + 12}px`) + .transition() + .duration(120) + .style('opacity', 1); +} + +function moveTooltip(event: MouseEvent): void { + d3.select('.collapsible-tree-tooltip') + .style('left', `${event.clientX + 12}px`) + .style('top', `${event.clientY + 12}px`); +} + +function hideTooltip(): void { + d3.select('.collapsible-tree-tooltip') + .transition() + .duration(120) + .style('opacity', 0); +} + +function measureContainer(container: HTMLElement): { width: number; height: number } { + const rect = container.getBoundingClientRect(); + return { + width: Math.max(640, Math.floor(rect.width || 0)), + height: Math.max(500, Math.floor(rect.height || 0)), + }; +} + +function toCodeSet(nodes: D3HierarchyNode[] | null | undefined): Set<string> { + return new Set((nodes ?? []).map((node) => codeOf(node)).filter((code): code is string => !!code)); +} + +function codeOf(node: D3HierarchyNode | null | undefined): string { + return normalizeString(node?.code); +} + +function normalizeString(value: unknown): string { + if (value === null || value === undefined) return ''; + return String(value).trim(); +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser-normalizer.spec.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser-normalizer.spec.ts new file mode 100644 index 000000000..3352ac45b --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser-normalizer.spec.ts @@ -0,0 +1,219 @@ +import { D3HierarchyNode } from '../../../../models/data-model.interface'; +import { + normalizeMetadataTree, + searchMetadataIndex, + selectionFromSearchResult, +} from './metadata-browser-normalizer'; + +describe('metadata browser normalizer', () => { + const model: D3HierarchyNode = { + label: 'Stroke 3.7', + code: 'Stroke', + children: [ + { + label: 'Demographics', + code: 'demographics', + children: [ + { + label: 'Dataset Registry Origin', + code: 'dataset-group', + children: [ + { + label: 'Dataset Registry origin', + code: 'dataset', + description: 'Variable used to differentiate datasets', + type: 'nominal', + enumerations: [ + { code: 'SSR', label: 'Swiss Stroke Registry' }, + { code: 'GRSR', label: 'Greek Stroke Registry' }, + ], + }, + ], + }, + { + label: 'Sex', + code: 'sex', + description: 'Patient sex', + type: 'nominal', + enumerations: [{ code: '1', label: 'Female' }], + }, + ], + }, + { + label: 'Vitals', + code: 'vitals', + children: [ + { + label: 'Metabolic', + code: 'duplicate', + children: [ + { + label: 'Glucose', + code: 'glucose', + description: 'Blood glucose value', + type: 'continuous', + units: 'mmol/L', + minValue: 0, + maxValue: 50, + }, + ], + }, + ], + }, + { + label: 'Other Vitals', + code: 'other-vitals', + children: [ + { + label: 'Metabolic', + code: 'duplicate', + children: [ + { + label: 'Glucose copy', + code: 'glucose', + description: 'Duplicate code under another path', + type: 'continuous', + }, + ], + }, + ], + }, + ], + }; + + it('normalizes deep and mixed groups without dropping direct variables', () => { + const index = normalizeMetadataTree(model); + const demographics = Object.values(index.groupsById).find((group) => group.code === 'demographics'); + + expect(demographics).toBeDefined(); + expect(demographics?.directGroupCount).toBe(1); + expect(demographics?.directVariableCount).toBe(1); + expect(demographics?.totalVariableCount).toBe(2); + }); + + it('generates breadcrumbs for groups and variables', () => { + const index = normalizeMetadataTree(model); + const glucose = Object.values(index.variablesById).find((variable) => variable.label === 'Glucose'); + + expect(glucose?.pathLabels).toEqual(['Stroke 3.7', 'Vitals', 'Metabolic', 'Glucose']); + expect(glucose?.units).toBe('mmol/L'); + expect(glucose?.minValue).toBe('0'); + expect(glucose?.maxValue).toBe('50'); + }); + + it('keeps duplicate group and variable codes distinct by path when needed', () => { + const index = normalizeMetadataTree(model); + const duplicateGroups = Object.values(index.groupsById).filter((group) => group.code === 'duplicate'); + const duplicateVariables = Object.values(index.variablesById).filter((variable) => variable.code === 'glucose'); + + expect(duplicateGroups.length).toBe(2); + expect(new Set(duplicateGroups.map((group) => group.id)).size).toBe(2); + expect(duplicateVariables.length).toBe(2); + expect(new Set(duplicateVariables.map((variable) => variable.id)).size).toBe(2); + }); + + it('lists all metadata entries when the search query is empty', () => { + const index = normalizeMetadataTree(model); + + expect(searchMetadataIndex(index, '').length).toBe(index.variableIds.length + index.groupIds.length); + expect(searchMetadataIndex(index, ' ').map((result) => result.label)).toContain('Glucose'); + }); + + it('searches labels, codes, descriptions, and enumeration labels', () => { + const index = normalizeMetadataTree(model); + + expect(searchMetadataIndex(index, 'greek')[0]?.label).toBe('Dataset Registry origin'); + expect(searchMetadataIndex(index, 'glucose')[0]?.label).toContain('Glucose'); + expect(searchMetadataIndex(index, 'demographics')[0]?.kind).toBe('group'); + expect(searchMetadataIndex(index, 'patient sex')[0]?.label).toBe('Sex'); + }); + + it('maps a variable search result to its containing group and original node', () => { + const index = normalizeMetadataTree(model); + const result = searchMetadataIndex(index, 'female')[0]; + const selection = selectionFromSearchResult(index, result); + + expect(selection.variableId).toBe(result.id); + expect(index.groupsById[selection.groupId].label).toBe('Demographics'); + expect(selection.originalNode.code).toBe('sex'); + }); + + it('selects the duplicate group that matches the chosen search path', () => { + const index = normalizeMetadataTree(model); + const results = searchMetadataIndex(index, 'metabolic'); + const otherVitalsResult = results.find((result) => result.path.includes('Other Vitals')); + + expect(otherVitalsResult).toBeDefined(); + const selection = selectionFromSearchResult(index, otherVitalsResult!); + + expect(selection.originalNode.label).toBe('Metabolic'); + expect(index.groupsById[selection.groupId].pathLabels).toContain('Other Vitals'); + expect(selection.originalNode).toBe(index.groupsById[otherVitalsResult!.id].original); + }); + + it('ranks exact label matches above prefix, substring, description, and enumeration hits', () => { + const modelWithAge: D3HierarchyNode = { + label: 'Stroke 3.7', + code: 'Stroke', + children: [ + { + label: 'Demographics', + code: 'demographics', + children: [ + { label: 'Age', code: 'age', type: 'discrete' }, + { label: 'Native imaged setting', code: 'native', type: 'nominal' }, + ], + }, + { + label: 'Imaging', + code: 'imaging', + children: [ + { + label: 'Age acquisition note', + code: 'age-note', + type: 'nominal', + }, + ], + }, + ], + }; + const index = normalizeMetadataTree(modelWithAge); + const results = searchMetadataIndex(index, 'age'); + const order = results.map((result) => result.label); + + expect(order[0]).toBe('Age'); + expect(order.indexOf('Age')).toBeLessThan(order.indexOf('Native imaged setting')); + expect(order.indexOf('Age')).toBeLessThan(order.indexOf('Age acquisition note')); + }); + + it('ranks label substring hits before description-only hits', () => { + const modelWithNote: D3HierarchyNode = { + label: 'Root', + code: 'root', + children: [ + { + label: 'Group A', + code: 'group-a', + children: [{ label: 'Note', code: 'note', type: 'nominal' }], + }, + { + label: 'Group B', + code: 'group-b', + children: [ + { + label: 'Unrelated', + code: 'unrelated', + type: 'nominal', + description: 'Contains a note about the visit', + }, + ], + }, + ], + }; + const index = normalizeMetadataTree(modelWithNote); + const results = searchMetadataIndex(index, 'note'); + + expect(results[0]?.label).toBe('Note'); + expect(results.map((result) => result.label)).toContain('Unrelated'); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser-normalizer.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser-normalizer.ts new file mode 100644 index 000000000..021da34c9 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser-normalizer.ts @@ -0,0 +1,416 @@ +import { D3HierarchyNode } from '../../../../models/data-model.interface'; +import { + MetadataSearchResult, + MetadataSelection, + NormalizedEnumeration, + NormalizedGroupNode, + NormalizedMetadataIndex, + NormalizedVariableNode, +} from './metadata-browser.model'; +import { isCategoricalVariableType } from '../../../../core/data-model.utils'; + +const ROOT_FALLBACK_LABEL = 'Data model'; + +export function normalizeMetadataTree(root: D3HierarchyNode): NormalizedMetadataIndex { + const groupsById: Record<string, NormalizedGroupNode> = {}; + const variablesById: Record<string, NormalizedVariableNode> = {}; + const groupIds: string[] = []; + const variableIds: string[] = []; + const variableCodeCounts = countVariableCodes(root); + + const visitGroup = ( + node: D3HierarchyNode, + parentId: string | null, + pathSegments: string[], + pathLabels: string[], + siblingIndex: number, + depth: number + ): string => { + const label = normalizeLabel(node.label, ROOT_FALLBACK_LABEL); + const code = normalizeString(node.code); + const segment = buildPathSegment(code || label, siblingIndex); + const nextPathSegments = [...pathSegments, segment]; + const id = `group:${nextPathSegments.join('/')}`; + const nextPathLabels = [...pathLabels, label]; + + groupsById[id] = { + kind: 'group', + id, + label, + code, + parentId, + childGroupIds: [], + variableIds: [], + pathGroupIds: [], + pathLabels: nextPathLabels, + depth, + directGroupCount: 0, + directVariableCount: 0, + totalVariableCount: 0, + original: node, + }; + groupIds.push(id); + + (node.children ?? []).forEach((child, childIndex) => { + if (isGroupNode(child)) { + const childGroupId = visitGroup( + child, + id, + nextPathSegments, + nextPathLabels, + childIndex, + depth + 1 + ); + groupsById[id].childGroupIds.push(childGroupId); + return; + } + + const variableId = visitVariable( + child, + id, + nextPathSegments, + nextPathLabels, + childIndex, + variableCodeCounts + ); + groupsById[id].variableIds.push(variableId); + }); + + groupsById[id].pathGroupIds = buildGroupPathIds(groupsById, id); + groupsById[id].directGroupCount = groupsById[id].childGroupIds.length; + groupsById[id].directVariableCount = groupsById[id].variableIds.length; + return id; + }; + + const visitVariable = ( + node: D3HierarchyNode, + parentGroupId: string, + parentPathSegments: string[], + parentPathLabels: string[], + siblingIndex: number, + codeCounts: Map<string, number> + ): string => { + const label = normalizeLabel(node.label, normalizeString(node.code) || 'Variable'); + const code = normalizeString(node.code); + const uniqueCode = code && codeCounts.get(code) === 1; + const id = uniqueCode + ? `variable:${encodeIdSegment(code)}` + : `variable:${[...parentPathSegments, buildPathSegment(code || label, siblingIndex)].join('/')}`; + const pathLabels = [...parentPathLabels, label]; + + variablesById[id] = { + kind: 'variable', + id, + label, + code, + description: normalizeString(node.description), + type: normalizeString(node.type || 'unknown'), + isCategorical: normalizeCategorical(node), + units: normalizeString(node.units), + minValue: normalizeString(node.minValue), + maxValue: normalizeString(node.maxValue), + enumerations: normalizeEnumerations(node.enumerations), + parentGroupId, + pathGroupIds: [], + pathLabels, + original: node, + }; + variableIds.push(id); + return id; + }; + + const rootId = visitGroup(root, null, [], [], 0, 0); + + groupIds.forEach((id) => { + groupsById[id].pathGroupIds = buildGroupPathIds(groupsById, id); + }); + variableIds.forEach((id) => { + variablesById[id].pathGroupIds = buildGroupPathIds(groupsById, variablesById[id].parentGroupId); + }); + calculateTotalVariableCounts(groupsById, rootId); + + return { + rootId, + groupsById, + variablesById, + groupIds, + variableIds, + }; +} + +function listMetadataSearchResults(index: NormalizedMetadataIndex): MetadataSearchResult[] { + const groupResults = index.groupIds.map((id) => toGroupSearchResult(index.groupsById[id])); + const variableResults = index.variableIds.map((id) => toVariableSearchResult(index.variablesById[id])); + + return [...groupResults, ...variableResults].sort((a, b) => a.path.localeCompare(b.path)); +} + +export function searchMetadataIndex(index: NormalizedMetadataIndex, query: string): MetadataSearchResult[] { + const normalizedQuery = normalizeSearchText(query); + if (!normalizedQuery) { + return listMetadataSearchResults(index); + } + + const scored: Array<{ result: MetadataSearchResult; score: number }> = []; + + index.groupIds.forEach((id) => { + const group = index.groupsById[id]; + const score = matchScore(normalizedQuery, group.label, group.code, '', []); + if (score >= 0) { + scored.push({ result: toGroupSearchResult(group), score }); + } + }); + + index.variableIds.forEach((id) => { + const variable = index.variablesById[id]; + const enumTexts = [ + ...variable.enumerations.map((item) => item.label), + ...variable.enumerations.map((item) => item.code), + ]; + const score = matchScore(normalizedQuery, variable.label, variable.code, variable.description, enumTexts); + if (score >= 0) { + scored.push({ result: toVariableSearchResult(variable, normalizedQuery), score }); + } + }); + + scored.sort((a, b) => a.score - b.score || a.result.label.localeCompare(b.result.label)); + return scored.map((entry) => entry.result).slice(0, 50); +} + +function toGroupSearchResult(group: NormalizedGroupNode): MetadataSearchResult { + return { + kind: 'group', + id: group.id, + label: group.label, + path: group.pathLabels.join(' > '), + pathLabels: group.pathLabels, + matchText: group.label, + parentGroupId: group.parentId, + }; +} + +function toVariableSearchResult( + variable: NormalizedVariableNode, + normalizedQuery = '' +): MetadataSearchResult { + return { + kind: 'variable', + id: variable.id, + label: variable.label, + path: variable.pathLabels.join(' > '), + pathLabels: variable.pathLabels, + matchText: normalizedQuery ? bestMatchText(normalizedQuery, variable) : variable.label, + parentGroupId: variable.parentGroupId, + }; +} + +export function selectionFromSearchResult( + index: NormalizedMetadataIndex, + result: MetadataSearchResult +): MetadataSelection { + if (result.kind === 'group') { + const group = index.groupsById[result.id]; + return { + groupId: group.id, + variableId: null, + originalNode: group.original, + }; + } + + const variable = index.variablesById[result.id]; + return { + groupId: variable.parentGroupId, + variableId: variable.id, + originalNode: variable.original, + }; +} + +export function findGroupByNode(index: NormalizedMetadataIndex, node: D3HierarchyNode | null): NormalizedGroupNode | null { + if (!node) return null; + + const path = normalizeString((node as D3HierarchyNode & { path?: string }).path); + if (path) { + const byPath = index.groupIds + .map((id) => index.groupsById[id]) + .find((group) => group.pathLabels.join(' > ') === path); + if (byPath) return byPath; + } + + const code = normalizeString(node.code); + const label = normalizeString(node.label); + const matches = index.groupIds + .map((id) => index.groupsById[id]) + .filter((group) => (code && group.code === code) || (label && group.label === label)); + + if (matches.length === 1) { + return matches[0]; + } + + return null; +} + +export function findVariableByNode( + index: NormalizedMetadataIndex, + node: D3HierarchyNode | null +): NormalizedVariableNode | null { + if (!node) return null; + + const path = normalizeString((node as D3HierarchyNode & { path?: string }).path); + if (path) { + const byPath = index.variableIds + .map((id) => index.variablesById[id]) + .find((variable) => variable.pathLabels.join(' > ') === path); + if (byPath) return byPath; + } + + const code = normalizeString(node.code); + const label = normalizeString(node.label); + const matches = index.variableIds + .map((id) => index.variablesById[id]) + .filter((variable) => (code && variable.code === code) || (label && variable.label === label)); + + if (matches.length === 1) { + return matches[0]; + } + + return null; +} + +export function pathForGroup(index: NormalizedMetadataIndex, groupId: string): NormalizedGroupNode[] { + return (index.groupsById[groupId]?.pathGroupIds ?? []) + .map((id) => index.groupsById[id]) + .filter((group): group is NormalizedGroupNode => !!group); +} + +function isGroupNode(node: D3HierarchyNode): boolean { + return Array.isArray(node.children) && node.children.length > 0; +} + +function buildGroupPathIds(groupsById: Record<string, NormalizedGroupNode>, groupId: string): string[] { + const path: string[] = []; + let current: NormalizedGroupNode | undefined = groupsById[groupId]; + while (current) { + path.unshift(current.id); + current = current.parentId ? groupsById[current.parentId] : undefined; + } + return path; +} + +function calculateTotalVariableCounts(groupsById: Record<string, NormalizedGroupNode>, groupId: string): number { + const group = groupsById[groupId]; + const childTotal = group.childGroupIds + .map((childGroupId) => calculateTotalVariableCounts(groupsById, childGroupId)) + .reduce((sum, count) => sum + count, 0); + group.totalVariableCount = group.variableIds.length + childTotal; + return group.totalVariableCount; +} + +function countVariableCodes(root: D3HierarchyNode): Map<string, number> { + const counts = new Map<string, number>(); + const visit = (node: D3HierarchyNode): void => { + (node.children ?? []).forEach((child) => { + if (isGroupNode(child)) { + visit(child); + return; + } + const code = normalizeString(child.code); + if (code) { + counts.set(code, (counts.get(code) ?? 0) + 1); + } + }); + }; + visit(root); + return counts; +} + +function normalizeEnumerations(value: unknown): NormalizedEnumeration[] { + if (!Array.isArray(value)) return []; + return value.map((entry) => { + if (entry && typeof entry === 'object') { + const record = entry as Record<string, unknown>; + const code = normalizeString(record['code'] ?? record['value'] ?? record['label']); + const label = normalizeString(record['label'] ?? record['name'] ?? record['code']); + return { code, label }; + } + const primitive = normalizeString(entry); + return { code: primitive, label: primitive }; + }).filter((entry) => entry.code || entry.label); +} + +function normalizeCategorical(node: D3HierarchyNode): boolean { + return isCategoricalVariableType(node.type) || (Array.isArray(node.enumerations) && node.enumerations.length > 0); +} + +function matchScore( + query: string, + label: string, + code: string, + description: string, + extraTexts: string[] +): number { + const labelNorm = normalizeSearchText(label); + const codeNorm = normalizeSearchText(code); + const descNorm = normalizeSearchText(description); + const extraNorms = extraTexts.map((text) => normalizeSearchText(text)); + + if (labelNorm === query || codeNorm === query) { + return 0; + } + if (labelNorm.startsWith(query) || codeNorm.startsWith(query)) { + return 1; + } + + const tokens = query.split(/\s+/).filter(Boolean); + if (tokens.length > 1) { + if (tokens.every((token) => labelNorm.includes(token))) { + return 2; + } + } + + if (labelNorm.includes(query) || codeNorm.includes(query)) { + return 3; + } + + if (descNorm.includes(query)) { + return 4; + } + + if (extraNorms.some((text) => text.includes(query))) { + return 5; + } + + return -1; +} + +function bestMatchText(query: string, variable: NormalizedVariableNode): string { + const candidates = [ + variable.label, + variable.code, + variable.description, + ...variable.enumerations.map((item) => item.label), + ...variable.enumerations.map((item) => item.code), + ]; + return candidates.find((candidate) => normalizeSearchText(candidate).includes(query)) ?? variable.label; +} + +function buildPathSegment(value: string, index: number): string { + return `${encodeIdSegment(value || 'node')}-${index}`; +} + +function encodeIdSegment(value: string): string { + return encodeURIComponent(value.trim().toLowerCase().replace(/\s+/g, '-')); +} + +function normalizeLabel(value: unknown, fallback: string): string { + const normalized = normalizeString(value); + return normalized || fallback; +} + +function normalizeString(value: unknown): string { + if (value === null || value === undefined) return ''; + return String(value).trim(); +} + +function normalizeSearchText(value: unknown): string { + return normalizeString(value).toLowerCase(); +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser.model.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser.model.ts new file mode 100644 index 000000000..1bff77b25 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/metadata-browser.model.ts @@ -0,0 +1,67 @@ +import { D3HierarchyNode } from '../../../../models/data-model.interface'; + +export type MetadataBrowserMode = 'ontology' | 'collapsible' | 'bubble'; + +export interface NormalizedEnumeration { + code: string; + label: string; +} + +export interface NormalizedGroupNode { + kind: 'group'; + id: string; + label: string; + code: string; + parentId: string | null; + childGroupIds: string[]; + variableIds: string[]; + pathGroupIds: string[]; + pathLabels: string[]; + depth: number; + directGroupCount: number; + directVariableCount: number; + totalVariableCount: number; + original: D3HierarchyNode; +} + +export interface NormalizedVariableNode { + kind: 'variable'; + id: string; + label: string; + code: string; + description: string; + type: string; + isCategorical: boolean; + units: string; + minValue: string; + maxValue: string; + enumerations: NormalizedEnumeration[]; + parentGroupId: string; + pathGroupIds: string[]; + pathLabels: string[]; + original: D3HierarchyNode; +} + +export interface NormalizedMetadataIndex { + rootId: string; + groupsById: Record<string, NormalizedGroupNode>; + variablesById: Record<string, NormalizedVariableNode>; + groupIds: string[]; + variableIds: string[]; +} + +export interface MetadataSearchResult { + kind: 'group' | 'variable'; + id: string; + label: string; + path: string; + pathLabels: string[]; + matchText: string; + parentGroupId: string | null; +} + +export interface MetadataSelection { + groupId: string; + variableId: string | null; + originalNode: D3HierarchyNode; +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.css b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.css new file mode 100644 index 000000000..7412ec9a7 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.css @@ -0,0 +1,272 @@ +:host { + display: block; + height: 100%; + min-height: 0; +} + +.ontology-browser { + display: flex; + flex-direction: column; + height: 100%; + min-height: 0; + color: var(--text-main); +} + +.ontology-layout { + display: grid; + grid-template-columns: minmax(220px, 36%) minmax(320px, 64%); + min-height: 0; + flex: 1; +} + +.tree-pane, +.contents-pane { + min-width: 0; + overflow: auto; + background: #ffffff; +} + +.tree-pane, +.contents-pane { + border-right: 1px solid rgba(148, 163, 184, 0.28); +} + +.pane-title { + position: sticky; + top: 0; + display: flex; + flex-direction: column; + gap: 2px; + padding: 10px 12px; + border-bottom: 1px solid rgba(226, 232, 240, 0.9); + background: var(--bg-neutral); + color: var(--text-main); + font-size: 0.82rem; + font-weight: 800; + z-index: 1; +} + +.pane-title small { + color: #64748b; + font-size: 0.68rem; + font-weight: 600; +} + +.tree-row { + display: grid; + grid-template-columns: 18px minmax(0, 1fr) auto; + align-items: center; + gap: 7px; + width: 100%; + min-height: 34px; + border: 0; + border-bottom: 1px solid rgba(241, 245, 249, 0.9); + background: transparent; + color: var(--text-main); + text-align: left; + cursor: pointer; +} + +.tree-row:hover, +.tree-row:focus-visible, +.tree-row.selected { + background: rgba(43, 51, 233, 0.08); + outline: none; + box-shadow: inset 3px 0 0 var(--primary-color); +} + +.content-row:hover, +.content-row:focus-visible, +.content-row.selected { + background: rgba(43, 51, 233, 0.08); + outline: none; +} + +.content-row.variable-row.in-pool .row-label::before { + content: ''; + display: inline-block; + width: 7px; + height: 7px; + margin-right: 6px; + border-radius: 999px; + background: var(--accent-color); + vertical-align: middle; +} + +.row-add { + border: 0; + background: var(--primary-color); + color: #ffffff; + border-radius: 8px; + padding: 2px 8px; + font-size: 0.68rem; + font-weight: 700; + cursor: pointer; +} + +.row-remove { + border: 0; + background: color-mix(in srgb, var(--primary-light) 18%, white); + color: var(--primary-color); + border-radius: 8px; + padding: 2px 8px; + font-size: 0.68rem; + font-weight: 700; + cursor: pointer; +} + +/* Not present in every selected dataset: grayed out and inert (see variable-availability.utils). */ +.content-row.variable-row.unavailable { + cursor: not-allowed; + opacity: 0.6; +} + +.content-row.variable-row.unavailable:hover, +.content-row.variable-row.unavailable:focus-visible { + background: rgba(148, 163, 184, 0.08); +} + +.content-row.variable-row.unavailable .row-label, +.content-row.variable-row.unavailable .row-description { + color: var(--unavailable-text); +} + +.content-row.variable-row.unavailable .type-badge { + background: #e2e8f0; + color: #64748b; +} + +.row-unavailable { + border-radius: 8px; + padding: 2px 8px; + font-size: 0.68rem; + font-weight: 700; + background: #e2e8f0; + color: #64748b; +} + +.tree-toggle { + display: inline-flex; + align-items: center; + justify-content: center; + width: 18px; + height: 18px; + color: #64748b; + font-size: 0.68rem; +} + +.tree-toggle .fa-circle { + color: #cbd5e1; + font-size: 0.35rem; +} + +.tree-label { + overflow: hidden; + font-size: 0.78rem; + font-weight: 650; + white-space: normal; + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; +} + +.tree-count { + padding: 1px 6px; + border-radius: 999px; + background: #f1f5f9; + color: var(--text-muted); + font-size: 0.68rem; + font-weight: 800; +} + +.content-section-title { + padding: 12px 12px 6px; + color: #64748b; + font-size: 0.68rem; + font-weight: 800; + letter-spacing: 0.03em; + text-transform: uppercase; +} + +.content-row { + display: grid; + grid-template-columns: minmax(0, 1fr) minmax(0, auto) minmax(0, auto); + align-items: center; + gap: 8px; + width: 100%; + max-width: 100%; + padding: 10px 12px; + border: 0; + border-bottom: 1px solid rgba(241, 245, 249, 0.95); + background: transparent; + text-align: left; + cursor: pointer; +} + +.content-row:hover, +.content-row:focus-visible, +.content-row.selected { + background: rgba(43, 51, 233, 0.08); + outline: none; +} + +.row-main { + display: flex; + flex-direction: column; + min-width: 0; + gap: 2px; +} + +.row-label { + overflow: hidden; + color: var(--text-main); + font-size: 0.82rem; + font-weight: 750; + white-space: normal; + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; +} + +.row-meta, +.row-description { + overflow: hidden; + color: #64748b; + font-size: 0.7rem; + white-space: normal; + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; +} + +.type-badge { + display: inline-flex; + align-items: center; + min-height: 20px; + padding: 2px 7px; + border-radius: 999px; + font-size: 0.68rem; + font-weight: 800; + text-transform: uppercase; + background: #dfefe4; + color: #14532d; +} + +.empty-state { + padding: 12px; + color: #64748b; + font-size: 0.8rem; +} + +@media (max-width: 1000px) { + .ontology-layout { + grid-template-columns: 1fr; + } + + .tree-pane, + .contents-pane { + min-height: 260px; + border-right: 0; + border-bottom: 1px solid rgba(148, 163, 184, 0.28); + } +} diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.html b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.html new file mode 100644 index 000000000..a3025be95 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.html @@ -0,0 +1,94 @@ +<div class="ontology-browser"> + <div class="ontology-layout"> + <section class="tree-pane" aria-label="Metadata hierarchy"> + <div class="pane-title">Hierarchy</div> + <div class="tree" role="tree" aria-label="Pathology metadata tree"> + @for (row of visibleTreeRows(); track row.group.id; let i = $index) { + <button + #treeRowButton + type="button" + class="tree-row" + role="treeitem" + [class.selected]="row.selected" + [attr.aria-expanded]="row.hasChildren ? row.expanded : null" + [attr.tabindex]="activeTreeIndex() === i ? 0 : -1" + [style.padding-left.px]="treePadding(row.depth)" + [title]="row.group.label" + (click)="selectGroup(row.group)" + (keydown)="onTreeKeydown($event, row, i)" + > + <span class="tree-toggle" (click)="row.hasChildren ? toggleGroup(row.group, $event) : null"> + @if (row.hasChildren) { + <i [class]="row.expanded ? 'fas fa-chevron-down' : 'fas fa-chevron-right'" aria-hidden="true"></i> + } @else { + <i class="fas fa-circle" aria-hidden="true"></i> + } + </span> + <span class="tree-label">{{ row.group.label }}</span> + <span class="tree-count">{{ row.group.totalVariableCount }}</span> + </button> + } + </div> + </section> + + <section class="contents-pane" aria-label="Selected group contents"> + <div class="pane-title"> + <span>{{ contentsPaneTitle() }}</span> + <small>{{ contentsPaneSubtitle() }}</small> + </div> + + @if (!selectedChildGroups().length && !selectedVariables().length) { + <div class="empty-state">No child groups or direct variables.</div> + } + + @if (selectedChildGroups().length) { + <div class="content-section-title">Child groups</div> + @for (group of selectedChildGroups(); track group.id) { + <button type="button" class="content-row group-row" (click)="selectGroup(group)"> + <span class="row-main"> + <span class="row-label">{{ group.label }}</span> + <span class="row-meta">{{ pluralize(group.directGroupCount, 'group') }} · {{ pluralize(group.totalVariableCount, 'variable') }}</span> + </span> + <i class="fas fa-chevron-right" aria-hidden="true"></i> + </button> + } + } + + @if (selectedVariables().length) { + <div class="content-section-title">Direct variables</div> + @for (variable of selectedVariables(); track variable.id) { + <button + type="button" + class="content-row variable-row" + [title]="variableRowTitle(variable)" + [class.selected]="isVariableSelected(variable.id)" + [class.in-pool]="isVariableInPool(variable)" + [class.unavailable]="isVariableUnavailable(variable)" + [attr.aria-disabled]="isVariableUnavailable(variable) ? true : null" + (click)="onVariableRowClick($event, variable)" + (dblclick)="openVariable(variable)" + > + <span class="row-main"> + <span class="row-label">{{ variable.label }}</span> + @if (variable.description) { + <span class="row-description">{{ variable.description }}</span> + } + </span> + <span class="type-badge">{{ variable.type }}</span> + @if (isVariableUnavailable(variable)) { + <span class="row-unavailable">Unavailable</span> + } @else { + <span + [class.row-add]="!isVariableInPool(variable)" + [class.row-remove]="isVariableInPool(variable)" + > + {{ isVariableInPool(variable) ? 'Remove' : 'Add' }} + </span> + } + </button> + } + } + </section> + + </div> +</div> diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.spec.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.spec.ts new file mode 100644 index 000000000..5420054a7 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.spec.ts @@ -0,0 +1,225 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { D3HierarchyNode } from '../../../../../models/data-model.interface'; +import { OntologyTreeBrowserComponent } from './ontology-tree-browser.component'; + +describe('OntologyTreeBrowserComponent', () => { + const model: D3HierarchyNode = { + label: 'Stroke 3.7', + code: 'Stroke', + children: [ + { + label: 'Demographics', + code: 'demographics', + children: [ + { + label: 'Sex', + code: 'sex', + description: 'Patient sex', + type: 'nominal', + enumerations: [{ code: '1', label: 'Female' }], + }, + ], + }, + { + label: 'Vitals', + code: 'vitals', + children: [ + { + label: 'Metabolic', + code: 'metabolic', + children: [ + { + label: 'Glucose', + code: 'glucose', + description: 'Blood glucose value', + type: 'continuous', + }, + ], + }, + ], + }, + ], + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [OntologyTreeBrowserComponent], + providers: [provideZonelessChangeDetection()], + }); + }); + + it('supports keyboard movement in the tree', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + const event = new KeyboardEvent('keydown', { key: 'ArrowDown' }); + vi.spyOn(event, 'preventDefault').mockReturnValue(undefined); + component.onTreeKeydown(event, component.visibleTreeRows()[0], 0); + + expect(event.preventDefault).toHaveBeenCalled(); + expect(component.activeTreeIndex()).toBe(1); + }); + + it('opens an externally highlighted variable in its containing group', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.componentRef.setInput('highlightNode', model.children?.[0].children?.[0]); + fixture.detectChanges(); + + expect(component.selectedGroup().code).toBe('demographics'); + }); + + it('emits the original node when selecting a direct variable', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + vi.spyOn(component.selectedNodeChange, 'emit').mockReturnValue(undefined); + + const variable = Object.values(component.index().variablesById).find((item) => item.code === 'sex'); + expect(variable).toBeDefined(); + component.selectVariable(variable!); + + expect(component.selectedNodeChange.emit).toHaveBeenCalledWith(variable!.original); + }); + + it('emits variablePoolToggle and selects the variable when the row Add control is clicked', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + const demographics = Object.values(component.index().groupsById).find((item) => item.code === 'demographics')!; + component.selectGroup(demographics); + fixture.detectChanges(); + vi.spyOn(component.selectedNodeChange, 'emit').mockReturnValue(undefined); + vi.spyOn(component.variablePoolToggle, 'emit').mockReturnValue(undefined); + + (fixture.nativeElement.querySelector('.row-add') as HTMLElement).click(); + + expect(component.selectedNodeChange.emit).toHaveBeenCalled(); + expect(component.variablePoolToggle.emit).toHaveBeenCalled(); + }); + + it('shows a neutral Contents heading while the tree root is selected', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + // The selected group is the root (pathology) by default; the middle pane + // must not re-head the pathology name (it is already in the chip and root). + expect(component.contentsPaneTitle()).toBe('Contents'); + expect(component.selectedGroup().label).toBe('Stroke 3.7'); + }); + + it('exposes the full label in tree-row and variable-row tooltips', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + const treeRows = fixture.nativeElement.querySelectorAll('button.tree-row'); + expect(treeRows[0].getAttribute('title')).toBe('Stroke 3.7'); + expect(treeRows[1].getAttribute('title')).toBe('Demographics'); + + // Select a group to reveal its direct variables in the contents pane. + const demographics = Object.values(component.index().groupsById).find((item) => item.code === 'demographics')!; + component.selectGroup(demographics); + fixture.detectChanges(); + + const variableRows = fixture.nativeElement.querySelectorAll('button.content-row.variable-row'); + expect(variableRows.length).toBeGreaterThan(0); + const labels = Array.from(variableRows as NodeListOf<Element>).map((row) => row.getAttribute('title')); + expect(labels).toContain('Sex'); + }); + + it('heads the middle pane with the group name for a non-root group', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + const demographics = Object.values(component.index().groupsById).find((item) => item.code === 'demographics')!; + component.selectGroup(demographics); + expect(component.selectedGroup().id).toBe(demographics.id); + expect(component.contentsPaneTitle()).toBe('Demographics'); + }); + + it('pluralizes group counts in the pane subtitle and rows', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + // Vitals has exactly one child group (Metabolic) and no direct variables. + const vitals = Object.values(component.index().groupsById).find((item) => item.code === 'vitals')!; + component.selectGroup(vitals); + fixture.detectChanges(); + + expect(component.contentsPaneSubtitle()).toBe('1 group · 1 variable'); + // Child groups stay listed and clickable even when the group has no + // direct variables. + const groupRow = fixture.nativeElement.querySelector('button.content-row.group-row') as HTMLElement; + expect(groupRow.textContent).toContain('Metabolic'); + groupRow.click(); + fixture.detectChanges(); + expect(component.selectedGroup().code).toBe('metabolic'); + }); + + it('lists the root child groups instead of a summary line', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.detectChanges(); + + // Root has child groups but no direct variables: the contents pane shows + // clickable child-group rows (the "pick a group" summary is gone). + expect(fixture.nativeElement.querySelector('.contents-summary')).toBeNull(); + const groupLabels = Array.from( + fixture.nativeElement.querySelectorAll('button.content-row.group-row'), + ).map((row: any) => row.querySelector('.row-label')?.textContent); + expect(groupLabels).toEqual(['Demographics', 'Vitals']); + + // A group with direct variables also lists its variables. + const demographics = Object.values(component.index().groupsById).find((item) => item.code === 'demographics')!; + component.selectGroup(demographics); + fixture.detectChanges(); + expect(fixture.nativeElement.querySelector('button.content-row.variable-row')).toBeTruthy(); + }); + + it('grays an unavailable variable row, shows which datasets lack it and ignores clicks', () => { + const fixture = TestBed.createComponent(OntologyTreeBrowserComponent); + const component = fixture.componentInstance; + fixture.componentRef.setInput('data', model); + fixture.componentRef.setInput('unavailableVariables', new Map([['sex', ['Dataset A', 'Dataset B']]])); + const demographics = Object.values(component.index().groupsById).find((item) => item.code === 'demographics')!; + component.selectGroup(demographics); + fixture.detectChanges(); + + const emitted: D3HierarchyNode[] = []; + const opened: D3HierarchyNode[] = []; + const toggled: D3HierarchyNode[] = []; + component.selectedNodeChange.subscribe((node) => emitted.push(node)); + component.nodeDoubleClicked.subscribe((node) => opened.push(node)); + component.variablePoolToggle.subscribe((node) => toggled.push(node)); + + const row = fixture.nativeElement.querySelector('button.content-row.variable-row') as HTMLElement; + expect(row.classList).toContain('unavailable'); + expect(row.getAttribute('aria-disabled')).toBe('true'); + expect(row.getAttribute('title')).toBe('Sex\nNot available in: Dataset A, Dataset B'); + expect(row.querySelector('.row-add')).toBeNull(); + expect(row.querySelector('.row-unavailable')?.textContent?.trim()).toBe('Unavailable'); + + row.click(); + row.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })); + expect(emitted).toEqual([]); + expect(opened).toEqual([]); + expect(toggled).toEqual([]); + expect(component.selectedVariableId()).toBeNull(); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.ts b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.ts new file mode 100644 index 000000000..b122b7b46 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/metadata-browser/ontology-tree-browser/ontology-tree-browser.component.ts @@ -0,0 +1,293 @@ +import { + ChangeDetectionStrategy, + Component, + ElementRef, + QueryList, + ViewChildren, + computed, + effect, + input, + output, + signal, +} from '@angular/core'; +import { pluralize } from '../../../../../core/result-label.utils'; +import { D3HierarchyNode } from '../../../../../models/data-model.interface'; +import { + EMPTY_UNAVAILABLE_VARIABLES, + UnavailableVariablesMap, + unavailableTooltipFor, +} from '../../../../../core/variable-availability.utils'; +import { + NormalizedGroupNode, + NormalizedVariableNode, +} from '../metadata-browser.model'; +import { + findGroupByNode, + findVariableByNode, + normalizeMetadataTree, + pathForGroup, +} from '../metadata-browser-normalizer'; + +interface TreeRow { + group: NormalizedGroupNode; + depth: number; + expanded: boolean; + selected: boolean; + hasChildren: boolean; +} + +const EMPTY_METADATA_TREE: D3HierarchyNode = { + label: 'Metadata', + code: 'metadata', + children: [], +}; + +@Component({ + selector: 'app-ontology-tree-browser', + templateUrl: './ontology-tree-browser.component.html', + styleUrl: './ontology-tree-browser.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class OntologyTreeBrowserComponent { + readonly data = input<D3HierarchyNode | null>(null); + readonly highlightNode = input<D3HierarchyNode | null>(null); + readonly poolVariables = input<D3HierarchyNode[]>([]); + /** Variables shown grayed out that ignore clicks for the current datasets. */ + readonly unavailableVariables = input<UnavailableVariablesMap>(EMPTY_UNAVAILABLE_VARIABLES); + readonly selectedNodeChange = output<D3HierarchyNode>(); + readonly nodeDoubleClicked = output<D3HierarchyNode>(); + readonly variablePoolToggle = output<D3HierarchyNode>(); + + @ViewChildren('treeRowButton') private treeRowButtons?: QueryList<ElementRef<HTMLButtonElement>>; + + readonly selectedGroupId = signal<string | null>(null); + readonly selectedVariableId = signal<string | null>(null); + readonly expandedGroupIds = signal<ReadonlySet<string>>(new Set<string>()); + readonly activeTreeIndex = signal(0); + + readonly index = computed(() => normalizeMetadataTree(this.data() ?? EMPTY_METADATA_TREE)); + readonly selectedGroup = computed(() => { + const index = this.index(); + const groupId = this.selectedGroupId() ?? index.rootId; + return index.groupsById[groupId] ?? index.groupsById[index.rootId]; + }); + /** + * Middle-pane heading. Uses the selected group's own name, but when the + * selected group is the tree root (the pathology/data-model root) it shows a + * neutral "Contents" so the pathology name is not re-headed a third time + * (it already appears in the context chip and the tree root). + */ + readonly contentsPaneTitle = computed(() => { + const index = this.index(); + return this.selectedGroup().id === index.rootId ? 'Contents' : this.selectedGroup().label; + }); + readonly selectedChildGroups = computed(() => { + const index = this.index(); + return this.selectedGroup().childGroupIds.map((id) => index.groupsById[id]); + }); + readonly selectedVariables = computed(() => { + const index = this.index(); + return this.selectedGroup().variableIds.map((id) => index.variablesById[id]); + }); + readonly visibleTreeRows = computed<TreeRow[]>(() => { + const index = this.index(); + const expanded = this.expandedGroupIds(); + const selectedGroupId = this.selectedGroup().id; + const rows: TreeRow[] = []; + const visit = (group: NormalizedGroupNode, depth: number): void => { + const hasChildren = group.childGroupIds.length > 0; + const isExpanded = expanded.has(group.id); + rows.push({ + group, + depth, + expanded: isExpanded, + selected: group.id === selectedGroupId, + hasChildren, + }); + + if (!isExpanded) return; + group.childGroupIds.forEach((id) => visit(index.groupsById[id], depth + 1)); + }; + + visit(index.groupsById[index.rootId], 0); + return rows; + }); + readonly contentsPaneSubtitle = computed(() => { + const group = this.selectedGroup(); + return `${this.pluralize(group.directGroupCount, 'group')} · ${this.pluralize(group.totalVariableCount, 'variable')}`; + }); + + readonly pluralize = pluralize; + + constructor() { + effect(() => { + const index = this.index(); + if (!this.selectedGroupId() || !index.groupsById[this.selectedGroupId() ?? '']) { + this.selectedGroupId.set(index.rootId); + this.selectedVariableId.set(null); + } + if (!this.expandedGroupIds().has(index.rootId)) { + this.expandedGroupIds.update((current) => new Set([...current, index.rootId])); + } + }); + + effect(() => { + const node = this.highlightNode(); + if (node) { + this.applyExternalSelection(node); + } + }); + } + + selectGroup(group: NormalizedGroupNode): void { + this.selectedGroupId.set(group.id); + this.selectedVariableId.set(null); + this.expandPathToGroup(group.id); + this.selectedNodeChange.emit(group.original); + } + + selectVariable(variable: NormalizedVariableNode): void { + this.selectedGroupId.set(variable.parentGroupId); + this.selectedVariableId.set(variable.id); + this.expandPathToGroup(variable.parentGroupId); + this.selectedNodeChange.emit(variable.original); + } + + openVariable(variable: NormalizedVariableNode): void { + if (this.isVariableUnavailable(variable)) return; + this.selectVariable(variable); + this.nodeDoubleClicked.emit(variable.original); + } + + toggleGroup(group: NormalizedGroupNode, event: Event): void { + event.stopPropagation(); + this.expandedGroupIds.update((current) => { + const next = new Set(current); + if (next.has(group.id)) { + next.delete(group.id); + } else { + next.add(group.id); + } + return next; + }); + } + + onTreeKeydown(event: KeyboardEvent, row: TreeRow, rowIndex: number): void { + const rows = this.visibleTreeRows(); + switch (event.key) { + case 'ArrowDown': + event.preventDefault(); + this.focusTreeIndex(Math.min(rowIndex + 1, rows.length - 1)); + break; + case 'ArrowUp': + event.preventDefault(); + this.focusTreeIndex(Math.max(rowIndex - 1, 0)); + break; + case 'Home': + event.preventDefault(); + this.focusTreeIndex(0); + break; + case 'End': + event.preventDefault(); + this.focusTreeIndex(rows.length - 1); + break; + case 'ArrowRight': + event.preventDefault(); + if (row.hasChildren && !row.expanded) { + this.expandedGroupIds.update((current) => new Set([...current, row.group.id])); + } else { + this.focusTreeIndex(Math.min(rowIndex + 1, rows.length - 1)); + } + break; + case 'ArrowLeft': + event.preventDefault(); + if (row.expanded) { + this.expandedGroupIds.update((current) => { + const next = new Set(current); + next.delete(row.group.id); + return next; + }); + } else if (row.group.parentId) { + const parentIndex = rows.findIndex((item) => item.group.id === row.group.parentId); + if (parentIndex >= 0) { + this.focusTreeIndex(parentIndex); + this.selectGroup(rows[parentIndex].group); + } + } + break; + case 'Enter': + case ' ': + event.preventDefault(); + this.selectGroup(row.group); + break; + } + } + + isVariableSelected(variableId: string): boolean { + return this.selectedVariableId() === variableId; + } + + isVariableInPool(variable: NormalizedVariableNode): boolean { + return this.poolVariables().some((node) => { + const code = String(node?.code ?? ''); + return code !== '' && code === variable.code; + }); + } + + /** Row click selects; a click on the Add/Remove affordance also toggles the pool. */ + onVariableRowClick(event: MouseEvent, variable: NormalizedVariableNode): void { + if (this.isVariableUnavailable(variable)) return; + this.selectVariable(variable); + if ((event.target as HTMLElement).closest('.row-add, .row-remove')) { + this.variablePoolToggle.emit(variable.original); + } + } + + isVariableUnavailable(variable: NormalizedVariableNode): boolean { + return this.unavailableTooltip(variable) !== ''; + } + + /** "Not available in: …" for an unavailable variable, '' otherwise. */ + unavailableTooltip(variable: NormalizedVariableNode): string { + return unavailableTooltipFor(this.unavailableVariables(), { code: variable.code }); + } + + variableRowTitle(variable: NormalizedVariableNode): string { + const unavailable = this.unavailableTooltip(variable); + return unavailable ? `${variable.label}\n${unavailable}` : variable.label; + } + + treePadding(depth: number): number { + return 10 + depth * 16; + } + + private applyExternalSelection(node: D3HierarchyNode): void { + const index = this.index(); + const variable = findVariableByNode(index, node); + if (variable) { + this.selectedGroupId.set(variable.parentGroupId); + this.selectedVariableId.set(variable.id); + this.expandPathToGroup(variable.parentGroupId); + return; + } + + const group = findGroupByNode(index, node); + if (group) { + this.selectedGroupId.set(group.id); + this.selectedVariableId.set(null); + this.expandPathToGroup(group.id); + } + } + + private expandPathToGroup(groupId: string): void { + const ids = pathForGroup(this.index(), groupId).map((group) => group.id); + this.expandedGroupIds.update((current) => new Set([...current, ...ids])); + } + + private focusTreeIndex(index: number): void { + this.activeTreeIndex.set(index); + queueMicrotask(() => { + this.treeRowButtons?.get(index)?.nativeElement.focus(); + }); + } +} diff --git a/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.css b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.css new file mode 100644 index 000000000..85186b837 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.css @@ -0,0 +1,344 @@ +:host { + display: block; +} + +.lr-result { + display: flex; + flex-direction: column; + gap: 16px; +} + +.coefficients-card { + background: #ffffff; + border: 1px solid rgba(148, 163, 184, 0.28); + border-radius: 12px; + overflow: hidden; +} + +.coefficients-head { + display: flex; + align-items: center; + justify-content: space-between; + flex-wrap: wrap; + gap: 12px; + padding: 12px 16px; +} + +.coefficients-copy { + display: flex; + align-items: baseline; + flex-wrap: wrap; + gap: 4px 10px; + min-width: 0; +} + +.coefficients-title { + margin: 0; + font-size: 1rem; + font-weight: 700; + color: var(--text-main); +} + +.coefficients-subtitle { + font-size: 0.78rem; + color: var(--text-muted); +} + +.coefficients-actions { + display: inline-flex; + align-items: center; + gap: 8px; + flex-wrap: wrap; +} + +.coef-columns button { + min-height: 26px; + padding: 0 12px; + font-size: 0.76rem; +} + +.csv-link { + display: inline-flex; + align-items: center; + height: 32px; + padding: 0 10px; + border: 0; + border-radius: 8px; + background: transparent; + color: var(--primary-color); + font: inherit; + font-size: 0.76rem; + font-weight: 600; + cursor: pointer; +} + +.csv-link:hover, +.csv-link:focus-visible { + background: #eef0fd; +} + +/* A visible focus ring, as on the column toggle (WCAG 2.4.7). */ +.csv-link:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +.coef-table { + display: grid; + min-width: 0; +} + +.coef-row { + display: grid; + /* Tracks per shown column, set by the component. */ + grid-template-columns: var(--coef-columns); + column-gap: 16px; + align-items: center; + padding: 11px 16px; + border-top: 1px solid rgba(148, 163, 184, 0.16); + font-size: 0.88rem; + color: var(--text-main); + min-width: 0; +} + +.coef-row--head { + padding: 8px 16px; + border-top: 0; + border-bottom: 1px solid rgba(148, 163, 184, 0.22); + background: #f8fafc; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.04em; + text-transform: uppercase; + color: var(--text-muted); +} + +.coef-row:not(.coef-row--head):first-of-type { + border-top: 0; +} + +.coef-row.is-intercept { + border-top: 1px dashed rgba(148, 163, 184, 0.4); + color: #64748b; +} + +.coef-row.is-intercept .coef-variable, +.coef-row.is-intercept .coef-estimate { + font-weight: 400; +} + +.coef-variable { + min-width: 0; + font-weight: 600; + overflow-wrap: anywhere; +} + +.coef-row.is-intercept .coef-variable { + font-weight: 400; +} + +.coef-level { + font-weight: 400; + color: var(--text-muted); +} + +.is-numeric { + white-space: nowrap; + text-align: right; + font-variant-numeric: tabular-nums; +} + +.is-center { + text-align: center; +} + +.coef-muted { + color: var(--text-muted); +} + +.coef-p { + font-weight: 400; +} + +.coef-p.is-significant { + color: #0f172a; + font-weight: 700; +} + +.coef-plot-cell { + display: flex; + align-items: center; + justify-content: center; + min-width: 0; +} + +.coef-plot { + position: relative; + display: block; + width: 100%; + height: 18px; +} + +.coef-plot-zero { + position: absolute; + top: 2px; + bottom: 2px; + left: 50%; + width: 1px; + background: rgba(148, 163, 184, 0.75); +} + +.coef-plot-interval { + position: absolute; + top: 7px; + height: 4px; + border-radius: 999px; + background: #cbd5e1; +} + +.coef-plot-interval.is-significant { + background: color-mix(in srgb, var(--primary-color) 62%, white); +} + +.coef-plot-marker { + position: absolute; + top: 4px; + width: 10px; + height: 10px; + margin-left: -5px; + border-radius: 50%; + background: #94a3b8; + box-shadow: 0 0 0 2px #ffffff; +} + +.coef-plot-marker.is-significant { + background: var(--primary-color); +} + +.coef-note { + margin: 0; + padding: 10px 16px 12px; + border-top: 1px solid rgba(148, 163, 184, 0.16); + font-size: 0.76rem; + line-height: 1.5; + color: var(--text-muted); +} + +.model-fit { + background: #ffffff; + border: 1px solid rgba(148, 163, 184, 0.28); + border-radius: 12px; + overflow: hidden; +} + +.model-fit-summary { + display: flex; + align-items: center; + gap: 8px; + width: 100%; + padding: 14px 16px; + border: 0; + background: transparent; + font: inherit; + font-size: 0.88rem; + font-weight: 600; + color: var(--text-main); + cursor: pointer; + text-align: left; +} + +.model-fit-summary:hover, +.model-fit-summary:focus-visible { + background: #f8fafc; +} + +/* A visible focus ring, as on the column toggle (WCAG 2.4.7). */ +.model-fit-summary:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +.model-fit-arrow { + width: 0.42rem; + height: 0.42rem; + border-right: 1.5px solid currentColor; + border-bottom: 1.5px solid currentColor; + transform: rotate(-45deg); + transition: transform 0.2s ease; + flex: 0 0 auto; +} + +.model-fit-arrow.is-open { + transform: rotate(45deg); +} + +.model-fit-glance { + margin-left: auto; + font-size: 0.78rem; + font-weight: 400; + color: var(--text-muted); +} + +.model-fit-grid { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + margin: 0; + border-top: 1px solid rgba(148, 163, 184, 0.22); +} + +.model-fit-item { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 12px; + min-width: 0; + padding: 10px 16px; + border-bottom: 1px solid rgba(148, 163, 184, 0.14); +} + +.model-fit-item dt { + font-size: 0.8rem; + color: var(--text-muted); +} + +.model-fit-item dd { + margin: 0; + font-size: 0.85rem; + font-weight: 600; + color: var(--text-main); + text-align: right; + font-variant-numeric: tabular-nums; +} + +/* Narrow by container, not viewport: the same card sits in the studio, the dashboard + detail and 360px compare columns. Below 620px the interval sketch goes (the CI and p + carry the same answer) so the key columns fit without wrapping; the + wider All columns view scrolls sideways. */ +.lr-result { + container-type: inline-size; +} + +@container (max-width: 620px) { + .coef-row { + grid-template-columns: var(--coef-columns-narrow); + column-gap: 12px; + padding-inline: 12px; + } + + .coef-row > .coef-plot-cell, + .coef-row > .is-center { + display: none; + } + + .coef-table.is-all-columns { + overflow-x: auto; + } + + .coef-table.is-all-columns .coef-row { + min-width: 560px; + } + + .model-fit-grid { + grid-template-columns: minmax(0, 1fr); + } +} diff --git a/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.html b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.html new file mode 100644 index 000000000..2de714fbe --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.html @@ -0,0 +1,103 @@ +@let model = view(); +<section class="lr-result" aria-label="Regression result"> + @if (model.figures.length) { + <app-key-figures [figures]="model.figures" /> + } + + <section class="coefficients-card" aria-labelledby="coef-heading"> + <header class="coefficients-head"> + <div class="coefficients-copy"> + <h3 id="coef-heading" class="coefficients-title">Coefficients</h3> + <span class="coefficients-subtitle">{{ model.subtitle }}</span> + </div> + <div class="coefficients-actions"> + <div class="modal-tabs coef-columns" role="group" aria-label="Coefficient columns"> + <button type="button" [class.active]="coefficientColumns() === 'key'" + [attr.aria-pressed]="coefficientColumns() === 'key'" (click)="coefficientColumns.set('key')"> + Key columns + </button> + <button type="button" [class.active]="coefficientColumns() === 'all'" + [attr.aria-pressed]="coefficientColumns() === 'all'" (click)="coefficientColumns.set('all')"> + All columns + </button> + </div> + <button type="button" class="csv-link" (click)="downloadCoefficientsCsv()" + title="Download coefficients as CSV" aria-label="Download coefficients as CSV">CSV ↓</button> + </div> + </header> + + <div class="coef-table" role="table" aria-label="Coefficients" [class.is-all-columns]="coefficientColumns() === 'all'" + [style.--coef-columns]="gridColumns()" [style.--coef-columns-narrow]="narrowGridColumns()"> + <div class="coef-row coef-row--head" role="row"> + <span role="columnheader">Variable</span> + @for (column of columns(); track column) { + <span role="columnheader" [class.is-numeric]="column !== 'interval'" [class.is-center]="column === 'interval'">{{ columnLabel(column) }}</span> + } + </div> + + @for (row of model.rows; track row.variable) { + <div class="coef-row" [class.is-intercept]="row.isIntercept" role="row"> + <span role="cell" class="coef-variable">{{ row.term }}@if (row.level) {<span class="coef-level"> · {{ row.level }}</span>}</span> + @for (column of columns(); track column; let lead = $first) { + @switch (column) { + @case ('interval') { + <span role="cell" class="coef-plot-cell"> + @if (!row.isIntercept) { + <span class="coef-plot" aria-hidden="true"> + <span class="coef-plot-zero"></span> + <span class="coef-plot-interval" [class.is-significant]="row.significant" + [style.left.%]="row.plotLeft" [style.width.%]="row.plotWidth"></span> + <span class="coef-plot-marker" [class.is-significant]="row.significant" + [style.left.%]="row.markerLeft"></span> + </span> + } + </span> + } + @case ('p') { + <span role="cell" class="is-numeric coef-p" [class.is-significant]="row.significant && !row.isIntercept">{{ row.pText }}</span> + } + @default { + <span role="cell" class="is-numeric" [class.coef-estimate]="lead" [class.coef-muted]="!lead">{{ cellText(row, column) }}</span> + } + } + } + </div> + } + </div> + + <p class="coef-note"> + Bold p-values are below 0.05. + @if (showsInterval()) { + Each interval is drawn on its own scale around + {{ model.nullValue === 1 ? ratioPhrase() + ' of 1 (log scale)' : 'zero' }}, so compare direction and whether it + crosses {{ model.nullValue }}, not size between rows. + } + @if (hasLevels()) { + A category row compares that level with the variable's reference level, the one not listed. + } + </p> + </section> + + @if (model.fitMetrics.length) { + <section class="model-fit"> + <button type="button" class="model-fit-summary" [attr.aria-expanded]="fitOpen()" (click)="fitOpen.set(!fitOpen())"> + <span class="model-fit-arrow" [class.is-open]="fitOpen()" aria-hidden="true"></span> + <span class="model-fit-title">Model fit details</span> + @if (model.fitGlance) { + <span class="model-fit-glance">{{ model.fitGlance }}</span> + } + </button> + + @if (fitOpen()) { + <dl class="model-fit-grid"> + @for (metric of model.fitMetrics; track metric.label) { + <div class="model-fit-item"> + <dt>{{ metric.label }}</dt> + <dd>{{ metric.value }}</dd> + </div> + } + </dl> + } + </section> + } +</section> diff --git a/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.spec.ts b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.spec.ts new file mode 100644 index 000000000..6b4ce4ec3 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.spec.ts @@ -0,0 +1,47 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { CsvExportService } from '../../../../services/csv-export.service'; +import { RegressionResultComponent } from './regression-result.component'; +import { buildRegressionView } from './regression-view'; + +describe('RegressionResultComponent', () => { + const logistic = { + dependent_var: '3m mRS good outcome', + indep_vars: ['Intercept', 'IVT[yes]', 'Age'], + summary: { + n_obs: 27308, + coefficients: [7.0586, 0.4794, -0.063], + stderr: [0.1287, 0.0405, 0.0016], + lower_ci: [6.8063, 0.4, -0.0662], + upper_ci: [7.311, 0.5587, -0.0599], + z_scores: [54.82, 11.84, -39.52], + pvalues: [0, 2.3e-32, 0], + df_model: 2, + df_resid: 27305, + ll0: -16452.2, + ll: -10977.8, + }, + }; + + it('exports the odds ratio and its CI in the coefficients CSV while the table shows only the key columns', () => { + TestBed.configureTestingModule({ imports: [RegressionResultComponent], providers: [provideZonelessChangeDetection()] }); + const exportToCsv = vi.spyOn(TestBed.inject(CsvExportService), 'exportToCsv').mockImplementation(() => {}); + const view = buildRegressionView('logistic_regression', logistic, null)!; + const fixture = TestBed.createComponent(RegressionResultComponent); + fixture.componentRef.setInput('view', view); + fixture.detectChanges(); + + expect(fixture.componentInstance.coefficientColumns()).toBe('key'); + (fixture.nativeElement as HTMLElement).querySelector<HTMLButtonElement>('.csv-link')!.click(); + + const [rows, headers, filename] = exportToCsv.mock.calls[0]; + expect(headers).toEqual(['Variable', 'Estimate', 'Std. error', view.statLabel, 'Odds ratio', 'CI 95% lower', 'CI 95% upper', 'p-value']); + expect(filename).toBe('3m_mrs_good_outcome_coefficients.csv'); + const ivt = rows.find((row) => row['Variable'] === 'IVT[yes]')!; + expect(ivt['Estimate']).toBe('0.4794'); + expect(ivt['Odds ratio']).toBe(String(Math.exp(0.4794))); + expect(ivt['CI 95% lower']).toBe(String(view.rows[0].lower)); + expect(ivt['CI 95% upper']).toBe(String(view.rows[0].upper)); + expect(ivt['p-value']).toBe('2.3e-32'); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.ts b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.ts new file mode 100644 index 000000000..d19651b57 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/regression/regression-result.component.ts @@ -0,0 +1,99 @@ +import { ChangeDetectionStrategy, Component, computed, inject, input, signal } from '@angular/core'; +import { CsvExportService } from '../../../../services/csv-export.service'; +import { KeyFiguresComponent } from '../summary/key-figures.component'; +import { CoefficientColumn, CoefficientRow, RegressionView } from './regression-view'; + +type CoefficientColumnMode = 'key' | 'all'; +type TextColumn = Exclude<CoefficientColumn, 'interval'>; + +/** Grid track of each column after the variable name. */ +const COLUMN_WIDTHS: Record<CoefficientColumn, string> = { + estimate: '0.7fr', + coefficient: '0.7fr', + stdErr: '0.7fr', + stat: '0.55fr', + ci: '1.1fr', + interval: '1.3fr', + p: '0.6fr', +}; + +const COLUMN_TEXT: Record<TextColumn, keyof CoefficientRow> = { + estimate: 'estimateText', + coefficient: 'coefficientText', + stdErr: 'stdErrText', + stat: 'statText', + ci: 'ciText', + p: 'pText', +}; + +/** Key figures, one coefficients card with an interval-vs-zero column, and collapsed model-fit details. */ +@Component({ + selector: 'app-regression-result', + imports: [KeyFiguresComponent], + templateUrl: './regression-result.component.html', + styleUrl: './regression-result.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class RegressionResultComponent { + private readonly csvExport = inject(CsvExportService); + + readonly view = input.required<RegressionView>(); + readonly coefficientColumns = signal<CoefficientColumnMode>('key'); + readonly fitOpen = signal(false); + readonly columns = computed(() => (this.coefficientColumns() === 'all' ? this.view().allColumns : this.view().keyColumns)); + readonly showsInterval = computed(() => this.columns().includes('interval')); + /** Grid tracks for the container's wide and narrow (< 620px, no interval sketch) layouts. */ + readonly gridColumns = computed(() => ['minmax(0, 1.3fr)', ...this.columns().map((column) => COLUMN_WIDTHS[column])].join(' ')); + readonly narrowGridColumns = computed(() => + ['minmax(96px, 1fr)', ...this.columns().filter((column) => column !== 'interval').map(() => 'auto')].join(' '), + ); + readonly hasLevels = computed(() => this.view().rows.some((row) => row.level !== null)); + /** "an odds ratio", "a hazard ratio". */ + readonly ratioPhrase = computed(() => { + const label = this.view().estimateLabel.toLowerCase(); + return `${/^[aeiou]/.test(label) ? 'an' : 'a'} ${label}`; + }); + + columnLabel(column: CoefficientColumn): string { + const model = this.view(); + switch (column) { + case 'estimate': return model.estimateLabel; + case 'coefficient': return model.coefficientLabel ?? ''; + case 'stdErr': return 'Std. error'; + case 'stat': return model.statLabel; + case 'ci': return '95% CI'; + case 'interval': return `Interval vs ${model.nullValue}`; + case 'p': return 'p-value'; + } + } + + cellText(row: CoefficientRow, column: TextColumn): string { + return String(row[COLUMN_TEXT[column]]); + } + + /** Every column the model has (an odds ratio and its CI too while the table shows only the key + * columns) as raw, unrounded numbers with an ASCII minus and separate CI bounds, so the file reads + * as numeric columns in a spreadsheet or R; blanks stand for missing values. */ + downloadCoefficientsCsv(): void { + const model = this.view(); + const raw = (value: number | null) => (value === null ? '' : String(value)); + const fields: [string, (row: CoefficientRow) => number | null][] = model.allColumns.flatMap((column) => { + switch (column) { + case 'interval': return []; + case 'ci': return [['CI 95% lower', (row: CoefficientRow) => row.lower], ['CI 95% upper', (row: CoefficientRow) => row.upper]]; + case 'estimate': return [[this.columnLabel(column), (row: CoefficientRow) => row.estimate]]; + case 'coefficient': return [[this.columnLabel(column), (row: CoefficientRow) => row.coefficient]]; + case 'stdErr': return [[this.columnLabel(column), (row: CoefficientRow) => row.stdErr]]; + case 'stat': return [[this.columnLabel(column), (row: CoefficientRow) => row.stat]]; + case 'p': return [[this.columnLabel(column), (row: CoefficientRow) => row.pValue]]; + } + }); + const headers = ['Variable', ...fields.map(([header]) => header)]; + const rows = model.rows.map((row) => ({ + 'Variable': row.variable, + ...Object.fromEntries(fields.map(([header, value]) => [header, raw(value(row))])), + })); + const slug = model.dependent.toLowerCase().replace(/[^a-z0-9]+/g, '_') || 'coefficients'; + this.csvExport.exportToCsv(rows, headers, `${slug}_coefficients.csv`); + } +} diff --git a/src/app/pages/experiment-studio/visualisations/regression/regression-view.spec.ts b/src/app/pages/experiment-studio/visualisations/regression/regression-view.spec.ts new file mode 100644 index 000000000..279bb6cf3 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/regression/regression-view.spec.ts @@ -0,0 +1,246 @@ +import { chiSquarePValue, formatPValue, formatRatio, ratioDigits } from '../result-format'; +import { buildRegressionView } from './regression-view'; + +describe('result-format', () => { + it('matches χ² upper-tail reference values', () => { + expect(chiSquarePValue(3.841459, 1)).toBeCloseTo(0.05, 6); + expect(chiSquarePValue(2, 2)).toBeCloseTo(Math.exp(-1), 10); + expect(chiSquarePValue(18.307038, 10)).toBeCloseTo(0.05, 6); + // df = 4 has a closed form: e^(-x/2) * (1 + x/2). + expect(chiSquarePValue(0.5, 4)).toBeCloseTo(Math.exp(-0.25) * 1.25, 10); + expect(chiSquarePValue(10948.8, 7)).toBe(0); + expect(chiSquarePValue(0, 3)).toBe(1); + }); + + it('formats p-values APA style and ratios at 3 significant digits', () => { + expect(formatPValue(1.1102230246251565e-16)).toBe('< 0.001'); + expect(formatPValue(0.8119)).toBe('0.812'); + expect(formatRatio(0.98999)).toBe('0.990'); + expect(formatRatio(903.51)).toBe('904'); + expect(formatRatio(1163.4)).toBe('1\u202f163'); + expect(ratioDigits(1.03513, 1.03648)).toBe(4); + expect(ratioDigits(1.49, 1.75)).toBe(3); + }); +}); + +describe('buildRegressionView', () => { + const logistic = { + dependent_var: '3m mRS good outcome', + indep_vars: ['Intercept', 'IVT[yes]', 'Age'], + summary: { + n_obs: 27308, + coefficients: [7.0586, 0.4794, -0.063], + stderr: [0.1287, 0.0405, 0.0016], + lower_ci: [6.8063, 0.4, -0.0662], + upper_ci: [7.311, 0.5587, -0.0599], + z_scores: [54.82, 11.84, -39.52], + pvalues: [0, 2.3e-32, 0], + df_model: 2, + df_resid: 27305, + r_squared_cs: 0.33, + r_squared_mcf: 0.3327, + ll0: -16452.2, + ll: -10977.8, + aic: 21971.6, + bic: 22037.3, + }, + }; + + it('reports logistic coefficients as odds ratios, sorted by log-odds size', () => { + const view = buildRegressionView('logistic_regression', logistic, null)!; + + expect(view.estimateLabel).toBe('Odds ratio'); + expect(view.nullValue).toBe(1); + // Key columns read like a model printout; the odds ratio, its CI and the sketch wait for all columns. + expect(view.coefficientLabel).toBe('Estimate'); + expect(view.keyColumns).toEqual(['coefficient', 'stdErr', 'stat', 'p']); + expect(view.allColumns).toEqual(['coefficient', 'stdErr', 'stat', 'estimate', 'ci', 'interval', 'p']); + expect(view.rows.map((row) => row.variable)).toEqual(['IVT[yes]', 'Age', 'Intercept']); + const ivt = view.rows[0]; + expect([ivt.term, ivt.level]).toEqual(['IVT', 'yes']); + expect(view.rows[1].level).toBeNull(); + expect(ivt.estimate).toBeCloseTo(Math.exp(0.4794), 10); + expect(ivt.coefficient).toBe(0.4794); + expect(ivt.ciText).toBe('1.49 to 1.75'); + // Plot sits on the log-odds scale: a positive coefficient lands right of the null line. + expect(ivt.markerLeft).toBeGreaterThan(50); + expect(view.rows[1].markerLeft).toBeLessThan(50); + }); + + it('keeps Exaflow F-test text when the statistic is not a finite number', () => { + const perfect = buildRegressionView('linear_regression', { + dependent_var: 'y', + indep_vars: ['Intercept', 'x'], + coefficients: [0, 1], + df_model: 1, + df_resid: 8, + f_stat: null, + f_pvalue: 0, + f_stat_display: 'Perfect fit', + f_pvalue_display: '<0.001', + f_stat_note: 'Residual sum of squares is zero.', + }, null)!; + const perfectFigure = perfect.figures.find((figure) => figure.label === 'Model F-test')!; + expect(perfectFigure.value).toBe('< 0.001'); + expect(perfectFigure.note).toBe('Perfect fit. Residual sum of squares is zero.'); + expect(Object.fromEntries(perfect.fitMetrics.map((metric) => [metric.label, metric.value]))['F-statistic']).toBe('Perfect fit'); + + const undefinedFit = buildRegressionView('linear_regression', { + indep_vars: ['Intercept'], + coefficients: [1], + f_stat: Number.NaN, + f_pvalue: Number.NaN, + f_stat_display: 'Undefined', + f_pvalue_display: 'Undefined', + f_stat_note: 'The overall F-statistic is not defined.', + }, null)!; + const undefinedFigure = undefinedFit.figures.find((figure) => figure.label === 'Model F-test')!; + expect(undefinedFigure).toEqual({ + label: 'Model F-test', + value: 'Undefined', + note: 'Undefined. The overall F-statistic is not defined.', + }); + + const finite = buildRegressionView('linear_regression', { + indep_vars: ['Intercept', 'x'], + coefficients: [1, 2], + df_model: 1, + df_resid: 8, + f_stat: 12.5, + f_pvalue: 0.002, + f_stat_display: '12.500', + f_stat_note: 'Overall F-test for the fitted regression model.', + }, null)!; + expect(finite.figures.find((figure) => figure.label === 'Model F-test')).toEqual({ + label: 'Model F-test', + value: '0.002', + note: 'F(1, 8) = 12.50', + }); + }); + + it('reads created-column dummy codes as words', () => { + const view = buildRegressionView('linear_regression', { + indep_vars: ['Intercept', 'kmeans_cluster[cluster_2]', 'stroke_territory_cohort[PCS]'], + coefficients: [80, -46, 1], + }, null)!; + + expect(view.rows.map((row) => [row.term, row.level])).toEqual([ + ['K-means cluster', 'Cluster 2'], + ['Stroke territory cohort', 'PCS'], + ['Intercept', null], + ]); + }); + + it('names the positive class in the logistic subtitle when the run gives it', () => { + expect(buildRegressionView('logistic_regression', logistic, null, 'yes')!.subtitle) + .toBe('Effect on the odds that 3m mRS good outcome is yes, other predictors held constant'); + expect(buildRegressionView('logistic_regression', logistic, null)!.subtitle) + .toBe('Effect on the odds of 3m mRS good outcome, other predictors held constant'); + }); + + it('derives the model likelihood-ratio test from ll and ll0', () => { + const view = buildRegressionView('logistic_regression', logistic, null)!; + const test = view.figures.find((figure) => figure.label === 'Model LR test')!; + + expect(test.value).toBe('< 0.001'); + expect(test.note).toBe('χ²(2) = 10\u202f949'); + expect(view.fitMetrics.map((metric) => metric.label)).toContain('Null log-likelihood'); + }); + + it('summarises a linear mixed model with the ICC from its variance components', () => { + const view = buildRegressionView('lmm', { + dependent_var: '3m mRS', + grouping_var: ['Unit'], + indep_vars: ['Intercept', 'Age'], + n_obs: 1000, + n_groups: 14, + coefficients: [-1.1, 0.031], + std_err: [0.08, 0.0003], + t_stats: [-13.5, 101.8], + pvalues: [1e-41, 0], + lower_ci: [-1.27, 0.0305], + upper_ci: [-0.95, 0.0317], + sigma2: 3, + sigma_u2: 1, + converged: true, + }, null)!; + + expect(view.estimateLabel).toBe('Estimate'); + expect(view.keyColumns).toEqual(['estimate', 'stdErr', 'interval', 'p']); + expect(view.figures[0]).toEqual({ label: 'ICC', value: '0.250', note: '25.0% of the variance lies between groups' }); + expect(view.figures.some((figure) => figure.label === 'Converged')).toBe(false); + }); + + it('reports a binary GLMM as odds ratios and leads with non-convergence', () => { + const view = buildRegressionView('glmm_binary', { + dependent_var: 'Good outcome', + grouping_var: 'Unit', + indep_vars: ['Intercept', 'Age'], + coefficients: [5.36, -0.0616], + z_stats: [121.4, -111.1], + pvalues: [0, 0], + lower_ci: [5.27, -0.0627], + upper_ci: [5.44, -0.0605], + sigma_u2: Math.PI * Math.PI / 3, + converged: false, + n_iter: 50, + }, null)!; + + expect(view.estimateLabel).toBe('Odds ratio'); + expect(view.figures[0].label).toBe('Converged'); + expect(view.figures[0].note).toBe('Stopped after 50 iterations; treat estimates with caution'); + // Latent-scale ICC: σu² / (σu² + π²/3). + expect(view.figures[1].value).toBe('0.500'); + }); + + it('reads the ordinal GLMM as odds of a higher category and lists its order and cutpoints', () => { + const view = buildRegressionView('glmm_ordinal', { + dependent_var: '3m mRS', + grouping_var: ['Unit'], + indep_vars: ['Intercept', 'Age'], + category_order: ['0.0', '1.0', '2.0'], + coefficients: [-2.39, 0.0352], + z_stats: [-23.2, 105.7], + pvalues: [1e-119, 0], + lower_ci: [-2.59, 0.0345], + upper_ci: [-2.19, 0.0358], + cutpoints: [0, 1.1058], + sigma_u2: 0.1377, + converged: true, + }, null)!; + + expect(view.estimateLabel).toBe('Odds ratio'); + expect(view.subtitle).toContain('odds of a higher 3m mRS category'); + const fit = Object.fromEntries(view.fitMetrics.map((metric) => [metric.label, metric.value])); + expect(fit['Category order']).toBe('0 < 1 < 2'); + expect(fit['Cutpoints']).toBe('0.000, 1.11'); + }); + + it('reports Cox hazard ratios and leads with events per covariate', () => { + const view = buildRegressionView('cox_regression_classical', { + dependent_var: 'Days to follow-up', + event_var: 'Death', + indep_vars: ['Sex[female]', 'Age'], + summary: { + n_obs: 582, n_events: 18, n_covariates: 3, converged: true, + coefficients: [0.154, 0.0387], std_err: [0.283, 0.0133], z_scores: [0.54, 2.91], pvalues: [0.586, 0.0037], + lower_ci: [-0.401, 0.0126], upper_ci: [0.709, 0.0647], + }, + }, null)!; + + expect(view.estimateLabel).toBe('Hazard ratio'); + expect(view.rows.map((row) => row.term)).toEqual(['Sex', 'Age']); + expect(view.rows[0].estimate).toBeCloseTo(Math.exp(0.154), 10); + const figures = Object.fromEntries(view.figures.map((figure) => [figure.label, figure])); + expect(figures['Events'].note).toBe('3.1% of 582 participants'); + expect(figures['Events per covariate'].note).toBe('Below the usual 10; estimates may be unstable'); + const fit = Object.fromEntries(view.fitMetrics.map((metric) => [metric.label, metric.value])); + expect(fit['Follow-up time']).toBe('Days to follow-up'); + expect(fit['Event indicator']).toBe('Death'); + }); + + it('returns null for algorithms without a coefficient view or incomplete payloads', () => { + expect(buildRegressionView('kmeans', logistic, null)).toBeNull(); + expect(buildRegressionView('logistic_regression', { indep_vars: ['Intercept'] }, null)).toBeNull(); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/regression/regression-view.ts b/src/app/pages/experiment-studio/visualisations/regression/regression-view.ts new file mode 100644 index 000000000..b6964c014 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/regression/regression-view.ts @@ -0,0 +1,545 @@ +import { + chiSquarePValue, + formatInteger, + formatPValue, + formatRatio, + ratioDigits, + readable, + formatSignedNumber, + formatSummaryNumber, + toFiniteNumber, +} from '../result-format'; +import { KeyFigure } from '../summary/key-figures.component'; + +export interface CoefficientRow { + /** As the result names it ("Sex[female]"); kept for the CSV. */ + variable: string; + /** Display split of a dummy term: "Sex" and "female"; level is null for a numeric predictor. */ + term: string; + level: string | null; + estimateText: string; + /** The model-scale coefficient when the estimate column shows a transform of it (log-odds behind an odds ratio). */ + coefficientText: string; + stdErrText: string; + statText: string; + ciText: string; + pText: string; + pValue: number | null; + /** Unrounded values for export; the *Text fields are display-only. */ + estimate: number | null; + coefficient: number | null; + stdErr: number | null; + stat: number | null; + lower: number | null; + upper: number | null; + significant: boolean; + isIntercept: boolean; + plotLeft: number; + plotWidth: number; + markerLeft: number; +} + +/** A coefficients-table column after the variable name; `interval` is the sketch against the null value. */ +export type CoefficientColumn = 'estimate' | 'coefficient' | 'stdErr' | 'stat' | 'ci' | 'interval' | 'p'; + +export interface RegressionView { + dependent: string; + /** Header of the estimate column and its CSV column ("Estimate", "Odds ratio"). */ + estimateLabel: string; + /** Header of the model-scale coefficient (log-odds, log hazard; "Estimate" for logistic regression); null when the estimate is the coefficient. */ + coefficientLabel: string | null; + /** Where "no effect" sits on the estimate scale: 0 for a coefficient, 1 for a ratio. */ + nullValue: 0 | 1; + /** Test statistic column ("t" for OLS, "z" for the logistic Wald test). */ + statLabel: string; + /** Columns of the Key and All views, in display order; the first one reads bold. */ + keyColumns: CoefficientColumn[]; + allColumns: CoefficientColumn[]; + subtitle: string; + rows: CoefficientRow[]; + figures: KeyFigure[]; + fitMetrics: KeyFigure[]; + fitGlance: string; +} + +const arrayOf = (value: unknown): unknown[] => (Array.isArray(value) ? value : []); + +const KEY_COLUMNS: CoefficientColumn[] = ['estimate', 'ci', 'interval', 'p']; +const ALL_COLUMNS: CoefficientColumn[] = ['estimate', 'stdErr', 'stat', 'ci', 'interval', 'p']; +/** Ratio models: the ratio leads, its log-scale coefficient follows. */ +const ALL_RATIO_COLUMNS: CoefficientColumn[] = ['estimate', 'coefficient', 'stdErr', 'stat', 'ci', 'interval', 'p']; + +/** "Sex[female]" → { term: "Sex", level: "female" }: the bracket is model syntax, not a label. */ +function splitTerm(variable: string): { term: string; level: string | null } { + const match = variable.match(/^(.+?)\[(.+)\]$/); + return match ? { term: readable(match[1]), level: readable(match[2]) } : { term: readable(variable), level: null }; +} + +interface CoefficientInput { + variables: string[]; + estimates: unknown[]; + stdErr: unknown[]; + stats: unknown[]; + pValues: unknown[]; + lower: unknown[]; + upper: unknown[]; +} + +/** + * Rows sorted by effect size with the intercept last. Each interval is drawn on its own scale around + * zero on the model scale; `toEstimate` maps a coefficient and its CI bounds to the reported scale + * (exp for odds ratios), so a ratio's plot is its log scale around 1. + */ +function buildRows(input: CoefficientInput, toEstimate?: (value: number) => number): CoefficientRow[] { + const shown = (value: number | null) => (value === null || !toEstimate ? value : toEstimate(value)); + const rows = input.variables.map((variable, index) => { + const coefficient = toFiniteNumber(input.estimates[index]); + const lowerCoefficient = toFiniteNumber(input.lower[index]); + const upperCoefficient = toFiniteNumber(input.upper[index]); + const estimate = shown(coefficient); + const lower = shown(lowerCoefficient); + const upper = shown(upperCoefficient); + const digits = ratioDigits(lower, upper); + const formatShown = (value: number) => (toEstimate ? formatRatio(value, digits) : formatSignedNumber(value)); + const pValue = toFiniteNumber(input.pValues[index]); + const stdErr = toFiniteNumber(input.stdErr[index]); + const stat = toFiniteNumber(input.stats[index]); + const scale = + Math.max(Math.abs(coefficient ?? 0), Math.abs(lowerCoefficient ?? 0), Math.abs(upperCoefficient ?? 0)) * 1.15 || 1; + const position = (value: number) => Math.max(0, Math.min(100, 50 + (value / scale) * 50)); + const left = lowerCoefficient === null ? 50 : position(lowerCoefficient); + const right = upperCoefficient === null ? 50 : position(upperCoefficient); + + return { + variable, + ...splitTerm(variable), + estimateText: estimate === null ? '—' : formatShown(estimate), + coefficientText: coefficient === null ? '—' : formatSignedNumber(coefficient), + stdErrText: stdErr === null ? '—' : formatSignedNumber(stdErr), + statText: stat === null ? '—' : formatSignedNumber(stat), + ciText: lower === null || upper === null ? '—' : `${formatShown(lower)} to ${formatShown(upper)}`, + pText: formatPValue(pValue), + pValue, + estimate, + coefficient, + stdErr, + stat, + lower, + upper, + significant: pValue !== null && pValue < 0.05, + isIntercept: /^intercept$/i.test(variable), + plotLeft: left, + plotWidth: Math.max(1, right - left), + markerLeft: coefficient === null ? 50 : position(coefficient), + }; + }); + + return rows.sort((a, b) => { + if (a.isIntercept !== b.isIntercept) return a.isIntercept ? 1 : -1; + return Math.abs(b.coefficient ?? 0) - Math.abs(a.coefficient ?? 0); + }); +} + +/** Label/value pairs with blanks dropped, plus the AIC · BIC · log-likelihood glance line. */ +function fitSummary(pairs: [string, string][]): Pick<RegressionView, 'fitMetrics' | 'fitGlance'> { + const fitMetrics = pairs.filter(([, value]) => value !== '').map(([label, value]) => ({ label, value })); + const fitGlance = fitMetrics + .filter((metric) => ['AIC', 'BIC', 'Log-likelihood'].includes(metric.label)) + .map((metric) => `${metric.label.replace('Log-', 'log-')} ${metric.value}`) + .join(' · '); + return { fitMetrics, fitGlance }; +} + +const summary = (value: unknown) => { + const number = toFiniteNumber(value); + return number === null ? '' : formatSummaryNumber(number); +}; + +const textField = (value: unknown) => (typeof value === 'string' ? value.trim() : ''); + +const integer = (value: number | null) => (value === null ? '' : formatInteger(value)); + +function linearView(result: any, yVar: string | null): RegressionView | null { + const variables = arrayOf(result.indep_vars).map((entry) => String(entry ?? '')); + const estimates = arrayOf(result.coefficients); + if (!variables.length || !estimates.length) return null; + + const dependent = String(result.dependent_var ?? yVar ?? 'outcome'); + const rows = buildRows({ + variables, + estimates, + stdErr: arrayOf(result.std_err), + stats: arrayOf(result.t_stats), + pValues: arrayOf(result.pvalues), + lower: arrayOf(result.lower_ci), + upper: arrayOf(result.upper_ci), + }); + + const rSquared = toFiniteNumber(result.r_squared); + const adjusted = toFiniteNumber(result.r_squared_adjusted); + // JSON turns Infinity/NaN into null, so a perfect fit or an undefined test survives only as Exaflow's display text. + const fStat = toFiniteNumber(result.f_stat); + const fPvalue = toFiniteNumber(result.f_pvalue); + const fStatText = fStat === null ? textField(result.f_stat_display) : formatSummaryNumber(fStat); + const fPvalueText = fPvalue === null ? textField(result.f_pvalue_display) : formatPValue(fPvalue); + const fStatNote = fStat === null ? textField(result.f_stat_note) : ''; + const rse = toFiniteNumber(result.rse); + const nObs = toFiniteNumber(result.n_obs); + const dfModel = toFiniteNumber(result.df_model); + const dfResid = toFiniteNumber(result.df_resid); + + const figures: KeyFigure[] = []; + if (rSquared !== null) { + figures.push({ + label: 'R²', + value: rSquared.toFixed(3), + note: `${Math.round(rSquared * 100)}% of the variance in ${dependent} is explained`, + }); + } + if (adjusted !== null) { + const predictorCount = rows.filter((row) => !row.isIntercept).length; + figures.push({ + label: 'Adjusted R²', + value: adjusted.toFixed(3), + note: `Corrected for ${predictorCount} predictor${predictorCount === 1 ? '' : 's'}`, + }); + } + if (fPvalueText || fStatText) { + const dfText = dfModel !== null && dfResid !== null ? `F(${formatInteger(dfModel)}, ${formatInteger(dfResid)})` : 'Model F'; + const finiteNote = `${dfText}${fStatText ? ` = ${fStatText}` : ''}`; + figures.push({ + label: 'Model F-test', + value: fPvalueText || '—', + note: fStatNote ? (fStatText ? `${fStatText}. ${fStatNote}` : fStatNote) : finiteNote, + }); + } + if (rse !== null) { + figures.push({ label: 'Residual SE', value: formatSummaryNumber(rse), note: `${dependent} points, typical prediction error` }); + } + if (nObs !== null) { + figures.push({ label: 'Observations', value: formatInteger(nObs), note: 'Rows in the fitted model' }); + } + + return { + dependent, + estimateLabel: 'Estimate', + coefficientLabel: null, + nullValue: 0, + statLabel: 't', + keyColumns: KEY_COLUMNS, + allColumns: ALL_COLUMNS, + subtitle: `Effect on ${dependent}, other predictors held constant`, + rows, + figures, + ...fitSummary([ + ['Dependent variable', dependent], + ['Observations', integer(nObs)], + ['df (model)', integer(dfModel)], + ['df (residual)', integer(dfResid)], + ['R²', rSquared === null ? '' : rSquared.toFixed(3)], + ['Adjusted R²', adjusted === null ? '' : adjusted.toFixed(3)], + ['F-statistic', fStatText], + ['p-value (F)', fPvalueText], + ['Residual std. error', summary(rse)], + ['Log-likelihood', summary(result.ll)], + ['AIC', summary(result.aic)], + ['BIC', summary(result.bic)], + ]), + }; +} + +/** Exaflow nests the logistic fit under `summary`; indep_vars and dependent_var stay top-level. */ +function logisticView(result: any, yVar: string | null, positiveClass: string | null): RegressionView | null { + const fit = result.summary; + const variables = arrayOf(result.indep_vars).map((entry) => String(entry ?? '')); + const estimates = arrayOf(fit?.coefficients); + if (!fit || !variables.length || !estimates.length) return null; + + const dependent = String(result.dependent_var ?? yVar ?? 'outcome'); + const rows = buildRows( + { + variables, + estimates, + stdErr: arrayOf(fit.stderr), + stats: arrayOf(fit.z_scores), + pValues: arrayOf(fit.pvalues), + lower: arrayOf(fit.lower_ci), + upper: arrayOf(fit.upper_ci), + }, + Math.exp, + ); + + const mcFadden = toFiniteNumber(fit.r_squared_mcf); + const coxSnell = toFiniteNumber(fit.r_squared_cs); + const ll = toFiniteNumber(fit.ll); + const ll0 = toFiniteNumber(fit.ll0); + const nObs = toFiniteNumber(fit.n_obs); + const dfModel = toFiniteNumber(fit.df_model); + const dfResid = toFiniteNumber(fit.df_resid); + const aic = toFiniteNumber(fit.aic); + // Likelihood-ratio test of the fitted model against the intercept-only model. + const lrStat = ll !== null && ll0 !== null ? 2 * (ll - ll0) : null; + const lrPvalue = lrStat !== null && dfModel !== null && dfModel > 0 ? chiSquarePValue(lrStat, dfModel) : null; + + const figures: KeyFigure[] = []; + if (mcFadden !== null) { + figures.push({ + label: 'Pseudo R² (McFadden)', + value: mcFadden.toFixed(3), + note: 'Log-likelihood gained over the intercept-only model', + }); + } + if (lrPvalue !== null && lrStat !== null) { + figures.push({ + label: 'Model LR test', + value: formatPValue(lrPvalue), + note: `χ²(${formatInteger(dfModel!)}) = ${formatSummaryNumber(lrStat)}`, + }); + } + if (aic !== null) { + figures.push({ label: 'AIC', value: formatSummaryNumber(aic), note: 'Lower fits better when comparing models' }); + } + if (nObs !== null) { + figures.push({ label: 'Observations', value: formatInteger(nObs), note: 'Rows in the fitted model' }); + } + + return { + dependent, + estimateLabel: 'Odds ratio', + coefficientLabel: 'Estimate', + nullValue: 1, + statLabel: 'z', + // Reviewer convention: the log-odds estimate with its SE, z and p first, as in a model printout; + // the odds ratio with its CI and interval sketch only with all columns. + keyColumns: ['coefficient', 'stdErr', 'stat', 'p'], + allColumns: ['coefficient', 'stdErr', 'stat', 'estimate', 'ci', 'interval', 'p'], + subtitle: `Effect on the odds ${outcomeOdds(dependent, positiveClass)}, other predictors held constant`, + rows, + figures, + ...fitSummary([ + ['Dependent variable', dependent], + ['Observations', integer(nObs)], + ['df (model)', integer(dfModel)], + ['df (residual)', integer(dfResid)], + ['Pseudo R² (McFadden)', mcFadden === null ? '' : mcFadden.toFixed(3)], + ['Pseudo R² (Cox–Snell)', coxSnell === null ? '' : coxSnell.toFixed(3)], + ['LR χ²', summary(lrStat)], + ['p-value (LR)', lrPvalue === null ? '' : formatPValue(lrPvalue)], + ['Log-likelihood', summary(ll)], + ['Null log-likelihood', summary(ll0)], + ['AIC', summary(aic)], + ['BIC', summary(fit.bic)], + ]), + }; +} + +/** "that 3m mRS good outcome is yes" when the run names its positive class, else "of 3m mRS good outcome". */ +function outcomeOdds(dependent: string, positiveClass: string | null): string { + return positiveClass ? `that ${dependent} is ${positiveClass}` : `of ${dependent}`; +} + +/** Exaflow sends grouping_var as a one-item list; older payloads as a string. */ +function groupingLabel(result: any): string { + const raw = Array.isArray(result.grouping_var) ? result.grouping_var.join(', ') : result.grouping_var; + return typeof raw === 'string' && raw ? raw : 'group'; +} + +/** Exaflow echoes integer categories as floats ("2.0"); show them as entered. */ +function categoryLabel(value: unknown): string { + const text = String(value ?? ''); + return /^-?\d+\.0+$/.test(text) ? text.replace(/\.0+$/, '') : text; +} + +/** A non-converged mixed model is the first thing a reader needs to know. */ +function convergenceFigure(result: any): KeyFigure[] { + if (result.converged !== false) return []; + const iterations = toFiniteNumber(result.n_iter); + return [{ + label: 'Converged', + value: 'No', + note: `Stopped${iterations === null ? '' : ` after ${formatInteger(iterations)} iterations`}; treat estimates with caution`, + }]; +} + +type MixedKind = 'linear' | 'binary' | 'ordinal'; + +/** + * Mixed models (lmm, glmm_binary, glmm_ordinal): fixed effects in the coefficients card and the + * random intercept summarised as an ICC — the share of variance that lies between groups. For the + * logit GLMMs the residual variance is the logistic distribution's π²/3 (latent-scale ICC). + * The ordinal model is cumulative logit, P(Y ≤ j) = σ(κⱼ − η), so exp(β) multiplies the odds of + * a higher outcome category. + */ +function mixedView(result: any, yVar: string | null, kind: MixedKind, positiveClass: string | null = null): RegressionView | null { + const logit = kind !== 'linear'; + const variables = arrayOf(result.indep_vars).map((entry) => String(entry ?? '')); + const estimates = arrayOf(result.coefficients); + if (!variables.length || !estimates.length) return null; + + const dependent = String(result.dependent_var ?? yVar ?? 'outcome'); + const grouping = groupingLabel(result); + const rows = buildRows( + { + variables, + estimates, + stdErr: arrayOf(result.std_err), + stats: arrayOf(logit ? result.z_stats : result.t_stats), + pValues: arrayOf(result.pvalues), + lower: arrayOf(result.lower_ci), + upper: arrayOf(result.upper_ci), + }, + logit ? Math.exp : undefined, + ); + + const sigmaU2 = toFiniteNumber(result.sigma_u2); + const sigma2 = logit ? (Math.PI * Math.PI) / 3 : toFiniteNumber(result.sigma2); + const nObs = toFiniteNumber(result.n_obs); + const nGroups = toFiniteNumber(result.n_groups); + const aic = toFiniteNumber(result.aic); + const ll = toFiniteNumber(logit ? result.ll_laplace : result.ll_reml); + + const figures: KeyFigure[] = [...convergenceFigure(result)]; + if (sigmaU2 !== null && sigma2 !== null && sigmaU2 + sigma2 > 0) { + const icc = sigmaU2 / (sigmaU2 + sigma2); + figures.push({ + label: 'ICC', + value: icc.toFixed(3), + note: `${(icc * 100).toFixed(1)}% of the ${logit ? 'latent ' : ''}variance lies between groups`, + }); + } + if (nGroups !== null) figures.push({ label: 'Groups', value: formatInteger(nGroups), note: `Levels of ${grouping}` }); + if (nObs !== null) figures.push({ label: 'Observations', value: formatInteger(nObs), note: 'Rows in the fitted model' }); + if (aic !== null) figures.push({ label: 'AIC', value: formatSummaryNumber(aic), note: 'Lower fits better when comparing models' }); + + return { + dependent, + estimateLabel: logit ? 'Odds ratio' : 'Estimate', + coefficientLabel: logit ? 'Log-odds' : null, + nullValue: logit ? 1 : 0, + statLabel: logit ? 'z' : 't', + // The linear mixed model reads by its standard error rather than the CI. + keyColumns: logit ? KEY_COLUMNS : ['estimate', 'stdErr', 'interval', 'p'], + allColumns: logit ? ALL_RATIO_COLUMNS : ALL_COLUMNS, + subtitle: `${ + kind === 'ordinal' + ? `Effect on the odds of a higher ${dependent} category` + : logit ? `Effect on the odds ${outcomeOdds(dependent, positiveClass)}` : `Effect on ${dependent}` + }, other predictors held constant, with a random intercept per ${grouping}`, + rows, + figures, + ...fitSummary([ + ['Dependent variable', dependent], + ['Grouping variable', grouping], + ['Category order', Array.isArray(result.category_order) ? result.category_order.map(categoryLabel).join(' < ') : ''], + ['Cutpoints', Array.isArray(result.cutpoints) ? result.cutpoints.map((value: unknown) => summary(value) || '—').join(', ') : ''], + ['Observations', integer(nObs)], + ['Groups', integer(nGroups)], + ['df (model)', integer(toFiniteNumber(result.df_model))], + ['df (residual)', integer(toFiniteNumber(result.df_resid))], + ['Residual variance', logit ? '' : summary(result.sigma2)], + ['Random intercept variance', summary(sigmaU2)], + [logit ? 'Laplace log-likelihood' : 'REML log-likelihood', summary(ll)], + ['AIC', summary(aic)], + ['BIC', summary(result.bic)], + ['Converged', result.converged === undefined ? '' : result.converged ? 'Yes' : 'No'], + ]), + }; +} + +/** + * Cox regression (classical and stacked): hazard ratios in the coefficients card, led by how + * much information the model had — events, not rows, limit a survival model. + */ +function coxView(result: any, yVar: string | null): RegressionView | null { + const fit = result.summary; + const variables = arrayOf(result.indep_vars).map((entry) => String(entry ?? '')); + const estimates = arrayOf(fit?.coefficients); + if (!fit || !variables.length || !estimates.length) return null; + + const time = String(result.dependent_var ?? yVar ?? 'time'); + const event = String(result.event_var ?? 'the event'); + const rows = buildRows( + { + variables, + estimates, + stdErr: arrayOf(fit.std_err), + stats: arrayOf(fit.z_scores), + pValues: arrayOf(fit.pvalues), + lower: arrayOf(fit.lower_ci), + upper: arrayOf(fit.upper_ci), + }, + Math.exp, + ).filter((row) => !row.isIntercept); + + const events = toFiniteNumber(fit.n_events); + const participants = toFiniteNumber(fit.n_obs); + const covariates = toFiniteNumber(fit.n_covariates); + const mcFadden = toFiniteNumber(fit.r_squared_mcf); + + const figures: KeyFigure[] = [...convergenceFigure(fit)]; + if (events !== null) { + figures.push({ + label: 'Events', + value: formatInteger(events), + note: participants ? `${((events / participants) * 100).toFixed(1)}% of ${formatInteger(participants)} participants` : undefined, + }); + } + if (events !== null && covariates) { + // The usual rule of thumb asks for about ten events per covariate. + const perCovariate = events / covariates; + figures.push({ + label: 'Events per covariate', + value: perCovariate.toFixed(1), + note: perCovariate < 10 ? 'Below the usual 10; estimates may be unstable' : `${formatInteger(covariates)} covariates in the model`, + }); + } + if (mcFadden !== null) { + figures.push({ label: 'Pseudo R² (McFadden)', value: mcFadden.toFixed(3), note: 'Stacked model against its intercept-only fit' }); + } + + return { + dependent: event, + estimateLabel: 'Hazard ratio', + coefficientLabel: 'Log hazard', + nullValue: 1, + statLabel: 'z', + keyColumns: KEY_COLUMNS, + allColumns: ALL_RATIO_COLUMNS, + subtitle: `Effect on the hazard of ${event} over ${time}, other predictors held constant`, + rows, + figures, + ...fitSummary([ + ['Follow-up time', time], + ['Event indicator', event], + ['Participants', integer(participants)], + ['Events', integer(events)], + ['Covariates', integer(covariates)], + ['Distinct event times', integer(toFiniteNumber(fit.n_unique_event_times))], + ['Analysis rows', integer(toFiniteNumber(fit.n_stacked_rows))], + ['Tied events', typeof fit.ties === 'string' ? fit.ties : ''], + ['Pseudo R² (Cox–Snell)', toFiniteNumber(fit.r_squared_cs) === null ? '' : Number(fit.r_squared_cs).toFixed(3)], + ['Pseudo R² (McFadden)', mcFadden === null ? '' : mcFadden.toFixed(3)], + ['Log-likelihood', summary(fit.ll)], + ['AIC', summary(fit.aic)], + ['BIC', summary(fit.bic)], + ['Converged', fit.converged === undefined ? '' : fit.converged ? 'Yes' : 'No'], + ]), + }; +} + +/** The coefficient/fit view for a result, or null when the algorithm has none or the payload is incomplete. */ +export function buildRegressionView( + algorithm: string | null, + result: any, + yVar: string | null, + positiveClass: string | null = null, +): RegressionView | null { + if (!result || typeof result !== 'object') return null; + if (algorithm === 'linear_regression') return linearView(result, yVar); + if (algorithm === 'logistic_regression') return logisticView(result, yVar, positiveClass); + if (algorithm === 'lmm') return mixedView(result, yVar, 'linear'); + if (algorithm === 'glmm_binary') return mixedView(result, yVar, 'binary', positiveClass); + if (algorithm === 'glmm_ordinal') return mixedView(result, yVar, 'ordinal'); + if (algorithm === 'cox_regression_classical' || algorithm === 'cox_regression_stacked') return coxView(result, yVar); + return null; +} diff --git a/src/app/pages/experiment-studio/visualisations/result-format.ts b/src/app/pages/experiment-studio/visualisations/result-format.ts new file mode 100644 index 000000000..a7fdd824e --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/result-format.ts @@ -0,0 +1,121 @@ +/** Number formatting shared by the summary views (key figures, coefficient rows). */ + +export function toFiniteNumber(value: unknown): number | null { + if (typeof value === 'number' && Number.isFinite(value)) return value; + if (typeof value === 'string' && value.trim() !== '' && Number.isFinite(Number(value))) { + return Number(value); + } + return null; +} + +/** Typographic minus, 3 decimals below 1 and 2 above. */ +export function formatSignedNumber(value: number): string { + const sign = value < 0 ? '−' : ''; + const abs = Math.abs(value); + return `${sign}${abs.toFixed(abs < 1 ? 3 : 2)}`; +} + +/** Narrow no-break space between digit groups (27 308): the SI/ISO 80000 separator, which reads + * the same in English and in locales where a comma is the decimal mark, and never wraps. */ +export const THOUSANDS_SEPARATOR = '\u202f'; + +/** An en-US formatted number with its grouping commas swapped for the separator: "1,065,587.5" → "1 065 587.5". */ +export function groupThousands(text: string): string { + return text.replace(/,/g, THOUSANDS_SEPARATOR); +} + +export function formatInteger(value: number): string { + return groupThousands(value.toLocaleString('en-US')); +} + +/** Grouped integers from 1 000 up, signed decimals below. */ +export function formatSummaryNumber(value: number): string { + if (!Number.isFinite(value)) return ''; + if (Math.abs(value) >= 1000) return formatInteger(Math.round(value)); + return formatSignedNumber(value); +} + +/** Ratios (odds, hazard) at 3 significant digits by default: 0.725, 1.07, 904, then grouped integers from 1 000. */ +export function formatRatio(value: number, digits = 3): string { + if (!Number.isFinite(value)) return ''; + if (value >= 1000) return formatInteger(Math.round(value)); + return value.toPrecision(digits); +} + +/** The fewest significant digits (3 to 6) at which a ratio's CI bounds read differently, + * so a tight interval such as 1.0345–1.0358 does not print as "1.04 to 1.04". */ +export function ratioDigits(lower: number | null, upper: number | null): number { + if (lower === null || upper === null) return 3; + for (let digits = 3; digits < 6; digits++) { + if (formatRatio(lower, digits) !== formatRatio(upper, digits)) return digits; + } + return 6; +} + +/** APA style: `< 0.001` instead of 0, 1.1e-16 or 3.9e-238 — values that small are float noise. */ +export function formatPValue(value: number | null): string { + if (value === null) return '—'; + if (value < 0.001) return '< 0.001'; + return value.toFixed(3); +} + +/** ln Γ(x), Lanczos approximation (g = 7), accurate to ~15 digits for x > 0. */ +function logGamma(x: number): number { + const c = [ + 0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, + -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, + 1.5056327351493116e-7, + ]; + if (x < 0.5) return Math.log(Math.PI / Math.sin(Math.PI * x)) - logGamma(1 - x); + const shifted = x - 1; + const t = shifted + 7.5; + let sum = c[0]; + for (let i = 1; i < c.length; i++) sum += c[i] / (shifted + i); + return 0.5 * Math.log(2 * Math.PI) + (shifted + 0.5) * Math.log(t) - t + Math.log(sum); +} + +/** Upper tail P(χ²(df) ≥ x): the regularized upper incomplete gamma Q(df/2, x/2), + * by series below a + 1 and Lentz's continued fraction above (Numerical Recipes 6.2). */ +export function chiSquarePValue(x: number, df: number): number { + if (!(x > 0) || !(df > 0)) return 1; + const a = df / 2; + const z = x / 2; + const lead = Math.exp(-z + a * Math.log(z) - logGamma(a)); + const tiny = 1e-300; + + if (z < a + 1) { + let term = 1 / a; + let sum = term; + for (let n = 1; n < 1000 && term > sum * 1e-15; n++) { + term *= z / (a + n); + sum += term; + } + return Math.min(1, Math.max(0, 1 - sum * lead)); + } + + let b = z + 1 - a; + let c = 1 / tiny; + let d = 1 / b; + let h = d; + for (let i = 1; i < 1000; i++) { + const an = -i * (i - a); + b += 2; + d = an * d + b; + if (Math.abs(d) < tiny) d = tiny; + c = b + an / c; + if (Math.abs(c) < tiny) c = tiny; + d = 1 / d; + const delta = d * c; + h *= delta; + if (Math.abs(delta - 1) < 1e-15) break; + } + return Math.min(1, Math.max(0, lead * h)); +} + +/** A raw snake_case code (a created column such as stroke_territory_cohort, or a level such as + * cluster_2) reads as words; labelled names pass through unchanged. */ +export function readable(code: string): string { + if (!/^[a-z0-9]+(_[a-z0-9]+)+$/.test(code)) return code; + const words = code.replace(/_/g, ' ').replace(/^kmeans\b/, 'K-means'); + return words.charAt(0).toUpperCase() + words.slice(1); +} diff --git a/src/app/pages/experiment-studio/visualisations/summary/key-figures.component.css b/src/app/pages/experiment-studio/visualisations/summary/key-figures.component.css new file mode 100644 index 000000000..605d0a7c5 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/summary/key-figures.component.css @@ -0,0 +1,49 @@ +:host { + display: block; +} + +/* Wraps on the container's width, not the viewport's: the same strip sits in the + studio, the dashboard detail and 360px compare columns. Each cell draws its left + and top hairline as an inset-free shadow that the card's overflow clips at the edge. */ +.key-figures { + display: flex; + flex-wrap: wrap; + background: #ffffff; + border: 1px solid rgba(148, 163, 184, 0.28); + border-radius: 12px; + overflow: hidden; +} + +.key-figure { + display: flex; + flex: 1 1 120px; + flex-direction: column; + gap: 2px; + min-width: 0; + padding: 14px 16px; + box-shadow: -1px 0 0 rgba(148, 163, 184, 0.22), 0 -1px 0 rgba(148, 163, 184, 0.22); +} + +.key-figure-label { + font-size: 0.7rem; + font-weight: 700; + letter-spacing: 0.05em; + text-transform: uppercase; + color: var(--text-muted); +} + +.key-figure-value { + font-size: 1.5rem; + font-weight: 700; + line-height: 1.2; + letter-spacing: -0.01em; + color: var(--text-main); + font-variant-numeric: tabular-nums; +} + +.key-figure-note { + font-size: 0.76rem; + line-height: 1.4; + color: var(--text-muted); + text-wrap: pretty; +} diff --git a/src/app/pages/experiment-studio/visualisations/summary/key-figures.component.ts b/src/app/pages/experiment-studio/visualisations/summary/key-figures.component.ts new file mode 100644 index 000000000..652665250 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/summary/key-figures.component.ts @@ -0,0 +1,30 @@ +import { ChangeDetectionStrategy, Component, input } from '@angular/core'; + +export interface KeyFigure { + label: string; + value: string; + note?: string; +} + +/** The headline numbers of a result: one strip of label / value / note cells. */ +@Component({ + selector: 'app-key-figures', + template: ` + <div class="key-figures" role="group" aria-label="Key figures"> + @for (figure of figures(); track figure.label) { + <div class="key-figure"> + <span class="key-figure-label">{{ figure.label }}</span> + <span class="key-figure-value">{{ figure.value }}</span> + @if (figure.note) { + <span class="key-figure-note">{{ figure.note }}</span> + } + </div> + } + </div> + `, + styleUrl: './key-figures.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class KeyFiguresComponent { + readonly figures = input.required<KeyFigure[]>(); +} diff --git a/src/app/pages/experiment-studio/visualisations/summary/summary-figures.spec.ts b/src/app/pages/experiment-studio/visualisations/summary/summary-figures.spec.ts new file mode 100644 index 000000000..e69f2e9a2 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/summary/summary-figures.spec.ts @@ -0,0 +1,168 @@ +import { buildSummaryFigures, tablesShownAsFigures } from './summary-figures'; + +const byLabel = (figures: { label: string; value: string; note?: string }[]) => + Object.fromEntries(figures.map((figure) => [figure.label, figure])); + +describe('buildSummaryFigures', () => { + it('summarises a t-test with the CI, the t statistic and the size of Cohen\'s d', () => { + const figures = byLabel(buildSummaryFigures('ttest_independent', { + t_stat: -739.928, df: 195004, p: 0, mean_diff: -3.454, ci_lower: -3.463, ci_upper: -3.4452, cohens_d: -3.756, + })!); + + expect(figures['Mean difference'].note).toBe('95% CI −3.46 to −3.45'); + expect(figures['p-value'].value).toBe('< 0.001'); + expect(figures['p-value'].note).toBe('t(195\u202f004) = −739.93'); + expect(figures["Cohen's d"].note).toBe('Large effect'); + }); + + it('derives Cramér\'s V and the sample size from the expected counts', () => { + const figures = byLabel(buildSummaryFigures('chi_squared', { + chi2: 1085.879, p_value: 3.87e-238, dof: 1, + expected: [[16328.81, 3623.19], [6020.19, 1335.81]], + })!); + + expect(figures["Cramér's V"].value).toBe('0.199'); + expect(figures['Observations'].value).toBe('27\u202f308'); + }); + + it('reports eta squared for one-way ANOVA and partial eta squared per two-way term', () => { + const oneWay = byLabel(buildSummaryFigures('anova_oneway', { + anova_table: { n_obs: 100, y_label: 'Age', df_explained: 1, df_residual: 98, ss_explained: 10, ss_residual: 90, f_stat: 10.9, p_value: 0.0013 }, + ci_info: { means: { male: 70, female: 74 } }, + })!); + expect(oneWay['Eta squared'].value).toBe('0.100'); + expect(oneWay['Groups'].value).toBe('2'); + + const twoWay = buildSummaryFigures('anova_twoway', { + terms: ['Sex', 'Unit', 'Sex:Unit', 'Residuals'], + sum_sq: [10, 30, 5, 90], df: [1, 2, 2, 94], f_stat: [10.4, 15.7, 2.6, null], f_pvalue: [0.002, 0.0000001, 0.08, null], + })!; + expect(twoWay.map((figure) => figure.label)).toEqual(['Sex', 'Unit', 'Sex × Unit']); + expect(twoWay[2].value).toBe('0.080'); + expect(twoWay[0].note).toBe('F(1, 94) = 10.40 · partial η² 0.100'); + }); + + it('returns null for algorithms without a test summary', () => { + expect(buildSummaryFigures('kmeans', { clusters: [] })).toBeNull(); + expect(buildSummaryFigures('ttest_independent', {})).toBeNull(); + }); + + it('sets CV accuracy against the majority baseline and class-balanced scores', () => { + const figures = byLabel(buildSummaryFigures('naive_bayes_gaussian_cv', { + confusion_matrix: { data: [[18159, 19207, 0], [7414, 95038, 0], [0, 0, 0]], labels: ['no', 'yes', 'unknown'] }, + classification_summary: { + accuracy: { no: { fold0: 0.82, fold1: 0.80, average: 0.81, stdev: 0.0089 } }, + }, + })!); + + expect(figures['Accuracy'].value).toBe('81.0%'); + expect(figures['Accuracy'].note).toBe('± 0.9 pts across 2 folds · +7.7 pts over the baseline'); + expect(figures['Majority baseline'].value).toBe('73.3%'); + expect(figures['Majority baseline'].note).toBe('Accuracy of always predicting yes'); + expect(figures['Balanced accuracy'].value).toBe('70.7%'); + }); + + it('reports linear regression CV error from [mean, sd] pairs and skips undefined metrics', () => { + const figures = byLabel(buildSummaryFigures('linear_regression_cv', { + dependent_var: 'Age', + n_obs: [199461, 199462, 199463], + root_mean_sq_error: [13.4115, 0.1331], + r_squared: [null, null], + mean_abs_error: { mean: 10.65, std: 0.027 }, + f_stat: [3903.46, 325.48], + })!); + + expect(Object.keys(figures)).toEqual(['RMSE', 'MAE', 'Folds']); + expect(figures['RMSE'].value).toBe('13.41'); + expect(figures['RMSE'].note).toBe('Age units, ± 0.13 across folds'); + expect(figures['Folds'].note).toBe('~199\u202f462 training rows each'); + }); + + it('reads RMSE from the legacy mean_sq_error field', () => { + const figures = buildSummaryFigures('linear_regression_cv', { mean_sq_error: [1.2, 0.1] })!; + expect(figures[0].label).toBe('RMSE'); + }); + + it('names the compared groups when the run parameters are known', () => { + const context = { groupA: 'yes', groupB: 'no' }; + const ttest = byLabel(buildSummaryFigures('ttest_independent', { mean_diff: -3.45, ci_lower: -3.46, ci_upper: -3.44, p: 0 }, context)!); + expect(ttest['Mean difference'].note).toBe('yes − no · 95% CI −3.46 to −3.44'); + }); + + // Exaflow PR #656: one-sided runs report a one-sided (1 − alpha) bound and "Infinity" on the open side. + it('shows the one-sided bound of a one-sided t-test instead of dropping the CI', () => { + const greater = byLabel(buildSummaryFigures('ttest_onesample', { + mean_diff: 4.634, ci_lower: 4.5738, ci_upper: 'Infinity', p: 0.00015, t_stat: 3.68, df: 216, + }, { groupA: null, groupB: null, alpha: 0.05, alternative: 'greater' })!); + expect(greater['Mean difference'].note).toBe('95% one-sided CI ≥ 4.57'); + + const less = byLabel(buildSummaryFigures('ttest_independent', { + mean_diff: -0.0398, ci_lower: '-Infinity', ci_upper: 0.0743, p: 0.28, + }, { groupA: 'edsd0', groupB: 'ppmi0', alpha: 0.05, alternative: 'less' })!); + expect(less['Mean difference'].note).toBe('edsd0 − ppmi0 · 95% one-sided CI ≤ 0.074'); + }); + + it('reads the open side from the result when the run parameters are unknown', () => { + const figures = byLabel(buildSummaryFigures('ttest_paired', { mean_diff: -0.502, ci_lower: -0.523, ci_upper: 'Infinity', p: 1 })!); + expect(figures['Mean difference'].note).toBe('95% one-sided CI ≥ −0.523'); + }); + + it('labels the CI with the run\'s confidence level', () => { + const figures = byLabel(buildSummaryFigures('ttest_onesample', { mean_diff: 1.2, ci_lower: 0.9, ci_upper: 1.5, p: 0.01 }, + { groupA: null, groupB: null, alpha: 0.1, alternative: 'two-sided' })!); + expect(figures['Mean difference'].note).toBe('90% CI 0.900 to 1.50'); + }); + + it('names the paired difference as measurement 1 (x) minus measurement 2 (y)', () => { + const figures = byLabel(buildSummaryFigures('ttest_paired', { mean_diff: -0.502, ci_lower: -0.527, ci_upper: -0.477, p: 1e-101 }, + { groupA: null, groupB: null, measurement1: 'Right orbital IFG', measurement2: 'Right planum polare' })!); + expect(figures['Mean difference'].note).toBe('Right orbital IFG − Right planum polare · 95% CI −0.527 to −0.477'); + }); +}); + +describe('tablesShownAsFigures', () => { + it('covers only the t-test rows the figures print', () => { + expect(tablesShownAsFigures('ttest_paired', { t_stat: 2, df: 9, p: 0.04, mean_diff: 1.2, ci_lower: 0.1, ci_upper: 2.3, se_diff: 0.5 })).toEqual([{ + title: 'Paired T-Test', + rows: ['Mean Difference', 'CI Lower', 'CI Upper', 'Std. Error of Difference', 'p-value', 'T-statistic', 'Degrees of Freedom'], + }]); + // Without a mean difference there is no figure for its CI or SE, and without p none for t or df. + expect(tablesShownAsFigures('ttest_onesample', { t_stat: 2, df: 9, ci_lower: 0.1, ci_upper: 2.3, cohens_d: 0.4 })).toEqual([ + { title: 'One-Sample T-Test', rows: ["Cohen's d"] }, + ]); + }); + + it('covers both CI rows of a one-sided t-test, whose figure prints the finite bound', () => { + expect(tablesShownAsFigures('ttest_paired', { mean_diff: 1.2, ci_lower: 0.3, ci_upper: 'Infinity' })).toEqual([{ + title: 'Paired T-Test', rows: ['Mean Difference', 'CI Lower', 'CI Upper'], + }]); + }); + + it('covers a one-sided CI whose open side arrived as null when the run says which side', () => { + const result = { mean_diff: 1.2, ci_lower: 0.3, ci_upper: null }; + expect(tablesShownAsFigures('ttest_paired', result, { groupA: null, groupB: null, alternative: 'greater' })).toEqual([{ + title: 'Paired T-Test', rows: ['Mean Difference', 'CI Lower', 'CI Upper'], + }]); + // Without the run's alternative the figure prints no CI, so both rows stay in the table. + expect(tablesShownAsFigures('ttest_paired', result)).toEqual([{ title: 'Paired T-Test', rows: ['Mean Difference'] }]); + }); + + it('leaves the F diagnostic and undefined linear regression CV metrics in their table', () => { + expect(tablesShownAsFigures('linear_regression_cv', { + root_mean_sq_error: [1.2, 0.1], mean_abs_error: { mean: 0.8, std: 0.05 }, r_squared: [null, null], f_stat: [12, 1], + })).toEqual([{ title: 'Error metrics', rows: ['Root mean squared error', 'Mean absolute error'] }]); + expect(tablesShownAsFigures('linear_regression_cv', { r_squared: [null, null], f_stat: [12, 1] })).toEqual([]); + }); + + it('covers ANOVA columns, SMD and the chi-squared summary only when the figures carry their values', () => { + expect(tablesShownAsFigures('anova_oneway', { anova_table: { p_value: 0.01, f_stat: 5, df_explained: 2, df_residual: 40 } })) + .toEqual([{ title: 'ANOVA Summary', columns: ['Prob>F', 'F', 'df'] }]); + expect(tablesShownAsFigures('anova_oneway', { anova_table: { f_stat: 5 } })).toEqual([]); + expect(tablesShownAsFigures('anova_twoway', { terms: ['A', 'Residual'], f_pvalue: [null, null], f_stat: [2, null], df: [1, 30] })).toEqual([]); + expect(tablesShownAsFigures('standardized_mean_difference', { comparisons: [{ group1: 'a', group2: 'b', smd: 0.2 }, { group1: 'a', group2: 'c', smd: null }] })) + .toEqual([]); + expect(tablesShownAsFigures('chi_squared', { p_value: 0.02, chi2: 6.1, dof: 2 })) + .toEqual([{ title: 'Chi-Squared Test Summary', rows: ['p-value', 'Chi-Squared statistic', 'Degrees of freedom'] }]); + expect(tablesShownAsFigures('kmeans', { clusters: [] })).toEqual([]); + }); +}); diff --git a/src/app/pages/experiment-studio/visualisations/summary/summary-figures.ts b/src/app/pages/experiment-studio/visualisations/summary/summary-figures.ts new file mode 100644 index 000000000..2f1edf257 --- /dev/null +++ b/src/app/pages/experiment-studio/visualisations/summary/summary-figures.ts @@ -0,0 +1,498 @@ +import { formatInteger, formatPValue, formatRatio, formatSignedNumber, formatSummaryNumber, toFiniteNumber } from '../result-format'; +import { readConfusionMatrix } from '../charts/renderers/confusion-matrix-chart'; +import { formatTTestKey, TableTrim } from '../auto-renderer/algorithm-table-registry'; +import { KeyFigure } from './key-figures.component'; + +/** Cohen's conventions for d / SMD: 0.2 small, 0.5 medium, 0.8 large. */ +function standardizedMagnitude(value: number): string { + const abs = Math.abs(value); + if (abs < 0.2) return 'Negligible'; + if (abs < 0.5) return 'Small'; + if (abs < 0.8) return 'Medium'; + return 'Large'; +} + +/** Cohen's conventions for η²: 0.01 small, 0.06 medium, 0.14 large. */ +function etaMagnitude(value: number): string { + if (value < 0.01) return 'Negligible'; + if (value < 0.06) return 'Small'; + if (value < 0.14) return 'Medium'; + return 'Large'; +} + +/** Cohen's w thresholds (0.1 / 0.3 / 0.5) scaled by √(min(r, c) − 1), as for Cramér's V. */ +function cramerMagnitude(value: number, dfStar: number): string { + const scale = Math.sqrt(Math.max(1, dfStar)); + if (value < 0.1 / scale) return 'Negligible'; + if (value < 0.3 / scale) return 'Small'; + if (value < 0.5 / scale) return 'Medium'; + return 'Large'; +} + +const first = (result: any, keys: string[]): number | null => { + for (const key of keys) { + const value = toFiniteNumber(result?.[key]); + if (value !== null) return value; + } + return null; +}; + +/** What the caller knows of the run: the groups a two-group test compared, the paired + * measurements, and the alpha and alternative that set the confidence interval. */ +export interface SummaryContext { + groupA: string | null; + groupB: string | null; + /** Paired t-test: Exaflow reports mean(x − y), measurement 1 (x) minus measurement 2 (y). */ + measurement1?: string | null; + measurement2?: string | null; + alpha?: number | null; + alternative?: string | null; +} + +/** "Infinity" / "-Infinity" (how Exaflow sends an open CI side) or a numeric infinity, as its sign. */ +function infiniteSign(value: unknown): 1 | -1 | null { + const numeric = typeof value === 'number' ? value : typeof value === 'string' ? Number(value.trim()) : NaN; + return numeric === Infinity ? 1 : numeric === -Infinity ? -1 : null; +} + +/** The CI level as a percentage: (1 − alpha) when alpha is a usable significance level, else 95. */ +function confidenceLevel(alpha: number | null | undefined): string { + if (typeof alpha !== 'number' || !(alpha > 0 && alpha < 1)) return '95'; + return String(Number(((1 - alpha) * 100).toFixed(2))); +} + +/** The t-test CI as a note: two-sided "95% CI a to b", one-sided "95% one-sided CI ≥ a" (greater) + * or "≤ b" (less). Exaflow (PR #656) reports a one-sided (1 − alpha) bound with the other side + * open, so the open side is read from the result and the run's alternative alike. */ +function ttestCiNote(result: any, context?: SummaryContext): string { + const lower = toFiniteNumber(result?.ci_lower); + const upper = toFiniteNumber(result?.ci_upper); + const level = confidenceLevel(context?.alpha); + const alternative = context?.alternative; + if (lower !== null && (infiniteSign(result?.ci_upper) === 1 || (upper === null && alternative === 'greater'))) { + return `${level}% one-sided CI ≥ ${formatSignedNumber(lower)}`; + } + if (upper !== null && (infiniteSign(result?.ci_lower) === -1 || (lower === null && alternative === 'less'))) { + return `${level}% one-sided CI ≤ ${formatSignedNumber(upper)}`; + } + return lower !== null && upper !== null ? `${level}% CI ${formatSignedNumber(lower)} to ${formatSignedNumber(upper)}` : ''; +} + +/** What the mean difference is the difference of, when the run says. */ +function ttestDifferenceNote(context?: SummaryContext): string { + // Exaflow's independent t-test reports mean(group A) − mean(group B). + if (context?.groupA && context?.groupB) return `${context.groupA} − ${context.groupB}`; + if (context?.measurement1 && context?.measurement2) return `${context.measurement1} − ${context.measurement2}`; + return ''; +} + +function ttestFigures(result: any, context?: SummaryContext): KeyFigure[] { + const figures: KeyFigure[] = []; + const meanDiff = first(result, ['mean_diff']); + const t = first(result, ['t_stat']); + const df = first(result, ['df', 'dof']); + const p = first(result, ['p', 'p_value']); + const d = first(result, ['cohens_d', 'cohen_d']); + const nObs = first(result, ['n_obs']); + const se = first(result, ['se_diff', 'se_difference', 'std_err_diff']); + const sd = first(result, ['std']); + + if (meanDiff !== null) { + figures.push({ + label: 'Mean difference', + value: formatSignedNumber(meanDiff), + note: [ttestDifferenceNote(context), ttestCiNote(result, context), se === null ? '' : `SE ${se.toPrecision(2)}`] + .filter(Boolean).join(' · ') || undefined, + }); + } + if (p !== null) { + const stat = t === null ? '' : `t${df === null ? '' : `(${formatInteger(df)})`} = ${formatSummaryNumber(t)}`; + figures.push({ label: 'p-value', value: formatPValue(p), note: stat || undefined }); + } + if (d !== null) { + figures.push({ label: "Cohen's d", value: formatSignedNumber(d), note: `${standardizedMagnitude(d)} effect` }); + } + if (nObs !== null) { + figures.push({ label: 'Observations', value: formatInteger(nObs), note: sd === null ? 'Rows in the test' : `Rows in the test · SD ${formatSignedNumber(sd)}` }); + } + return figures; +} + +function chiSquaredFigures(result: any): KeyFigure[] { + const figures: KeyFigure[] = []; + const chi2 = first(result, ['chi2']); + const dof = first(result, ['dof']); + const p = first(result, ['p_value']); + const expected: unknown[][] = Array.isArray(result?.expected) ? result.expected : []; + // Expected counts keep the observed margins, so their sum is the sample size. + const n = expected.flat().reduce<number>((sum, cell) => sum + (toFiniteNumber(cell) ?? 0), 0); + const rows = expected.length; + const cols = Array.isArray(expected[0]) ? expected[0].length : 0; + + if (p !== null) { + const stat = chi2 === null ? undefined : `χ²${dof === null ? '' : `(${formatInteger(dof)})`} = ${formatSummaryNumber(chi2)}`; + figures.push({ label: 'p-value', value: formatPValue(p), note: stat }); + } + if (chi2 !== null && n > 0 && rows > 1 && cols > 1) { + const dfStar = Math.min(rows, cols) - 1; + const v = Math.sqrt(chi2 / (n * dfStar)); + figures.push({ label: "Cramér's V", value: v.toFixed(3), note: `${cramerMagnitude(v, dfStar)} association` }); + } + if (n > 0) { + figures.push({ label: 'Observations', value: formatInteger(Math.round(n)), note: `${rows} × ${cols} table` }); + } + return figures; +} + +function fisherFigures(result: any): KeyFigure[] { + const figures: KeyFigure[] = []; + const p = first(result, ['p_value']); + const oddsRatio = first(result, ['odds_ratio']); + if (p !== null) figures.push({ label: 'p-value', value: formatPValue(p), note: "Fisher's exact test" }); + if (oddsRatio !== null) figures.push({ label: 'Odds ratio', value: formatRatio(oddsRatio), note: 'Sample odds ratio of the 2 × 2 table' }); + return figures; +} + +function anovaOneWayFigures(result: any): KeyFigure[] { + const table = result?.anova_table; + if (!table || typeof table !== 'object') return []; + const figures: KeyFigure[] = []; + const f = first(table, ['f_stat']); + const p = first(table, ['p_value']); + const dfBetween = first(table, ['df_explained']); + const dfWithin = first(table, ['df_residual']); + const ssBetween = first(table, ['ss_explained']); + const ssWithin = first(table, ['ss_residual']); + const nObs = first(table, ['n_obs']); + const groups = Object.keys(result?.ci_info?.means ?? {}).length; + const outcome = typeof table.y_label === 'string' && table.y_label ? table.y_label : 'the outcome'; + + if (p !== null) { + const df = dfBetween !== null && dfWithin !== null ? `(${formatInteger(dfBetween)}, ${formatInteger(dfWithin)})` : ''; + figures.push({ label: 'p-value', value: formatPValue(p), note: f === null ? undefined : `F${df} = ${formatSummaryNumber(f)}` }); + } + if (ssBetween !== null && ssWithin !== null && ssBetween + ssWithin > 0) { + const eta = ssBetween / (ssBetween + ssWithin); + figures.push({ + // Spelled out: the uppercase label style turns η into Η. + label: 'Eta squared', + value: eta.toFixed(3), + note: `η², ${etaMagnitude(eta).toLowerCase()} effect · ${(eta * 100).toFixed(1)}% of the variance in ${outcome} lies between groups`, + }); + } + if (groups) figures.push({ label: 'Groups', value: formatInteger(groups), note: 'Compared in the test' }); + if (nObs !== null) figures.push({ label: 'Observations', value: formatInteger(nObs), note: 'Rows in the test' }); + return figures; +} + +/** One cell per model term: its p-value, F and partial η² against the residual. */ +function anovaTwoWayFigures(result: any): KeyFigure[] { + const terms: unknown[] = Array.isArray(result?.terms) ? result.terms : []; + const at = (key: string, index: number) => toFiniteNumber(Array.isArray(result?.[key]) ? result[key][index] : null); + const residualIndex = terms.findIndex((term) => /^residuals?$/i.test(String(term))); + const ssResidual = residualIndex >= 0 ? at('sum_sq', residualIndex) : null; + const dfResidual = residualIndex >= 0 ? at('df', residualIndex) : null; + + return terms.flatMap((term, index) => { + if (index === residualIndex) return []; + const p = at('f_pvalue', index); + if (p === null) return []; + const f = at('f_stat', index); + const df = at('df', index); + const ss = at('sum_sq', index); + const parts = [ + f === null ? '' : `F${df !== null && dfResidual !== null ? `(${formatInteger(df)}, ${formatInteger(dfResidual)})` : ''} = ${formatSummaryNumber(f)}`, + ss !== null && ssResidual !== null && ss + ssResidual > 0 ? `partial η² ${(ss / (ss + ssResidual)).toFixed(3)}` : '', + ]; + return [{ label: String(term).replace(/:/g, ' × '), value: formatPValue(p), note: parts.filter(Boolean).join(' · ') || undefined }]; + }); +} + +function smdFigures(result: any): KeyFigure[] { + const comparisons: any[] = Array.isArray(result?.comparisons) ? result.comparisons : []; + return comparisons.flatMap((comparison) => { + const smd = toFiniteNumber(comparison?.smd); + if (smd === null) return []; + return [{ + label: `${comparison.group1 ?? 'Group 1'} vs ${comparison.group2 ?? 'Group 2'}`, + value: formatSignedNumber(smd), + note: `${standardizedMagnitude(smd)} difference${Math.abs(smd) >= 0.1 ? ' · above the 0.1 balance threshold' : ''}`, + }]; + }); +} + +const percent = (value: number) => `${(value * 100).toFixed(1)}%`; + +/** Fold-average accuracy (and its SD) from either CV summary shape: + * naive Bayes `classification_summary.accuracy[class].{average, stdev, fold*}`, + * logistic regression `summary.{row_names, accuracy[]}`. */ +function foldAccuracy(result: any): { mean: number; sd: number | null; folds: number } | null { + const perClass = result?.classification_summary?.accuracy; + if (perClass && typeof perClass === 'object') { + const first = Object.values(perClass)[0] as any; + const mean = toFiniteNumber(first?.average); + if (mean === null) return null; + return { mean, sd: toFiniteNumber(first?.stdev), folds: Object.keys(first).filter((key) => key.startsWith('fold')).length }; + } + const rows: unknown[] = Array.isArray(result?.summary?.row_names) ? result.summary.row_names : []; + const accuracy: unknown[] = Array.isArray(result?.summary?.accuracy) ? result.summary.accuracy : []; + const mean = toFiniteNumber(accuracy[rows.indexOf('average')]); + if (mean === null) return null; + return { + mean, + sd: toFiniteNumber(accuracy[rows.indexOf('stdev')]), + folds: rows.filter((row) => String(row).startsWith('fold')).length, + }; +} + +/** The largest actual class of a pooled confusion matrix and its share: what always guessing it would score. */ +function majorityClass(confusion: { labels: string[]; matrix: number[][] }): { label: string; share: number } | null { + const totals = confusion.matrix.map((row) => row.reduce((sum, value) => sum + value, 0)); + const total = totals.reduce((sum, value) => sum + value, 0); + if (!(total > 0)) return null; + const index = totals.reduce((best, value, i) => (value > totals[best] ? i : best), 0); + return { label: confusion.labels[index], share: totals[index] / total }; +} + +/** + * Cross-validated classifiers: fold accuracy next to the majority-class baseline it has to beat, + * then balanced accuracy and macro F1 from the pooled confusion matrix (both weigh every class + * equally, so a large majority class cannot carry them), and the mean ROC AUC when folds report one. + */ +function classificationCvFigures(result: any): KeyFigure[] { + const figures: KeyFigure[] = []; + const accuracy = foldAccuracy(result); + const confusion = readConfusionMatrix(result?.confusion_matrix); + + const majority = confusion ? majorityClass(confusion) : null; + const baseline = majority?.share ?? null; + + if (accuracy) { + const spread = accuracy.sd === null ? '' : `± ${(accuracy.sd * 100).toFixed(1)} pts`; + const folds = accuracy.folds ? `across ${accuracy.folds} folds` : ''; + const gain = baseline === null ? '' : `${accuracy.mean >= baseline ? '+' : '−'}${(Math.abs(accuracy.mean - baseline) * 100).toFixed(1)} pts over the baseline`; + figures.push({ + label: 'Accuracy', + value: percent(accuracy.mean), + note: [[spread, folds].filter(Boolean).join(' '), gain].filter(Boolean).join(' · ') || undefined, + }); + } + + if (confusion) { + const { labels, matrix } = confusion; + const rowTotals = matrix.map((row) => row.reduce((sum, value) => sum + value, 0)); + const colTotals = labels.map((_, j) => matrix.reduce((sum, row) => sum + row[j], 0)); + const supported = labels.map((_, i) => i).filter((i) => rowTotals[i] > 0); + + if (majority) { + figures.push({ + label: 'Majority baseline', + value: percent(majority.share), + note: `Accuracy of always predicting ${majority.label}`, + }); + } + if (supported.length > 1) { + const recall = supported.map((i) => matrix[i][i] / rowTotals[i]); + const f1 = supported.map((i, k) => { + const precision = colTotals[i] > 0 ? matrix[i][i] / colTotals[i] : 0; + return precision + recall[k] > 0 ? (2 * precision * recall[k]) / (precision + recall[k]) : 0; + }); + const mean = (values: number[]) => values.reduce((sum, value) => sum + value, 0) / values.length; + figures.push({ label: 'Balanced accuracy', value: percent(mean(recall)), note: 'Mean recall over classes, pooled folds' }); + figures.push({ label: 'Macro F1', value: mean(f1).toFixed(3), note: 'Mean F1 over classes, pooled folds' }); + } + } + + // Logistic regression's positive-class precision, recall and F1 stay in its fold table + // (with their mean and SD rows): a sixth figure wraps the strip onto a second line. + const aucs = (Array.isArray(result?.roc_curves) ? result.roc_curves : []) + .map((curve: any) => toFiniteNumber(curve?.auc)) + .filter((auc: number | null): auc is number => auc !== null); + if (aucs.length) { + const mean = aucs.reduce((sum: number, auc: number) => sum + auc, 0) / aucs.length; + figures.push({ label: 'ROC AUC', value: mean.toFixed(3), note: `Mean over ${aucs.length} folds` }); + } + + return figures; +} + +/** + * Out-of-fold error for linear regression CV. Exaflow sends each metric as [mean, sd] + * (older runs { mean, std }; [null, null] when undefined). The F statistic is left out: + * it describes each training fit, not how well the model predicts held-out rows. + */ +function linearRegressionCvFigures(result: any): KeyFigure[] { + const meanSd = (raw: any): [number, number | null] | null => { + const [mean, sd] = (Array.isArray(raw) ? raw : [raw?.mean, raw?.std]).map(toFiniteNumber); + return mean === null ? null : [mean, sd]; + }; + const spread = (sd: number | null, digits: number) => (sd === null ? undefined : `± ${sd.toFixed(digits)} across folds`); + const outcome = typeof result?.dependent_var === 'string' ? result.dependent_var : 'outcome'; + const figures: KeyFigure[] = []; + + // mean_sq_error is the pre-rename name of the same RMSE value in stored experiments. + const rmse = meanSd(result?.root_mean_sq_error ?? result?.mean_sq_error); + if (rmse) figures.push({ label: 'RMSE', value: formatSummaryNumber(rmse[0]), note: `${outcome} units, ${spread(rmse[1], 2) ?? 'held-out rows'}` }); + const mae = meanSd(result?.mean_abs_error); + if (mae) figures.push({ label: 'MAE', value: formatSummaryNumber(mae[0]), note: `${outcome} units, ${spread(mae[1], 2) ?? 'held-out rows'}` }); + const r2 = meanSd(result?.r_squared); + if (r2) { + figures.push({ + label: 'Out-of-fold R²', + value: r2[0].toFixed(3), + note: `${(r2[0] * 100).toFixed(1)}% of the variance in held-out rows${r2[1] === null ? '' : `, ± ${r2[1].toFixed(3)}`}`, + }); + } + const folds: unknown[] = Array.isArray(result?.n_obs) ? result.n_obs : []; + const sizes = folds.map(toFiniteNumber).filter((n): n is number => n !== null); + if (sizes.length) { + const mean = sizes.reduce((sum, n) => sum + n, 0) / sizes.length; + figures.push({ label: 'Folds', value: formatInteger(sizes.length), note: `~${formatInteger(Math.round(mean))} training rows each` }); + } + return figures; +} + +const BUILDERS: Record<string, (result: any, context?: SummaryContext) => KeyFigure[]> = { + linear_regression_cv: linearRegressionCvFigures, + naive_bayes_gaussian_cv: classificationCvFigures, + naive_bayes_categorical_cv: classificationCvFigures, + logistic_regression_cv: classificationCvFigures, + ttest_independent: (result, context) => ttestFigures(result, context && { ...context, measurement1: null, measurement2: null }), + ttest_paired: (result, context) => ttestFigures(result, context && { ...context, groupA: null, groupB: null }), + ttest_onesample: (result, context) => ttestFigures(result, context && { ...context, groupA: null, groupB: null, measurement1: null, measurement2: null }), + chi_squared: chiSquaredFigures, + fisher_exact: fisherFigures, + anova_oneway: anovaOneWayFigures, + anova_twoway: anovaTwoWayFigures, + standardized_mean_difference: smdFigures, +}; + +/** What a figure means, in plain language, for figures a reader may not know; shown under them. */ +export const FIGURE_NOTES: Record<string, string> = { + linear_regression_cv: + 'RMSE (root mean squared error) is the typical size of a prediction error on rows the model did not see, in the outcome\'s units; ' + + 'squaring makes large misses count more. MAE (mean absolute error) is the average size of that error, so an RMSE well above the MAE points to a few large misses.', +}; + +/** The t-test rows the figures show: each key only when its figure (or that figure's note) prints it. */ +function ttestShownRows(result: any, context?: SummaryContext): string[] { + const has = (key: string) => toFiniteNumber(result?.[key]) !== null; + const firstKey = (keys: string[]) => keys.find(has) ?? null; + const meanDiff = has('mean_diff'); + const p = firstKey(['p', 'p_value']); + const t = has('t_stat'); + const df = firstKey(['df', 'dof']); + const d = firstKey(['cohens_d', 'cohen_d']); + const nObs = has('n_obs'); + const se = firstKey(['se_diff', 'se_difference', 'std_err_diff']); + const keys = [ + ...(meanDiff ? ['mean_diff'] : []), + // A one-sided CI's figure prints its finite bound, which implies the open side. + ...(meanDiff && ttestCiNote(result, context) ? ['ci_lower', 'ci_upper'] : []), + ...(meanDiff && se ? [se] : []), + ...(p ? [p] : []), + ...(p && t ? ['t_stat'] : []), + ...(p && t && df ? [df] : []), + ...(d ? [d] : []), + ...(nObs ? ['n_obs'] : []), + ...(nObs && has('std') ? ['std'] : []), + ]; + return keys.map(formatTTestKey); +} + +/** One summary table's rows the figures show, by label, each only when its value is a finite number. */ +function metricRows(result: any, rows: Array<[label: string, key: string, shown: boolean]>): string[] { + return rows.filter(([, key, shown]) => shown && toFiniteNumber(result?.[key]) !== null).map(([label]) => label); +} + +/** Linear regression CV: an error metric's row once its figure shows both the mean and the SD. + * The F diagnostic has no figure, and an undefined metric ([null, null]) keeps its row. */ +function linearRegressionCvShownRows(result: any): string[] { + const known = (raw: any) => { + const [mean, sd] = (Array.isArray(raw) ? raw : [raw?.mean, raw?.std]).map(toFiniteNumber); + return mean !== null && sd !== null; + }; + return [ + ...(known(result?.root_mean_sq_error ?? result?.mean_sq_error) ? ['Root mean squared error'] : []), + ...(known(result?.mean_abs_error) ? ['Mean absolute error'] : []), + ...(known(result?.r_squared) ? ['R-squared'] : []), + ]; +} + +/** ANOVA columns the figures show: p, F and both degrees of freedom of every model term, or none. */ +function anovaTwoWayShownColumns(result: any): string[] { + const terms: unknown[] = Array.isArray(result?.terms) ? result.terms : []; + const at = (key: string, index: number) => toFiniteNumber(Array.isArray(result?.[key]) ? result[key][index] : null); + const residual = terms.findIndex((term) => /^residuals?$/i.test(String(term))); + const modelTerms = terms.map((_, i) => i).filter((i) => i !== residual); + const complete = residual >= 0 && at('df', residual) !== null && modelTerms.length > 0 && + modelTerms.every((i) => at('f_pvalue', i) !== null && at('f_stat', i) !== null && at('df', i) !== null); + return complete ? ['df', 'F', 'Prob>F'] : []; +} + +function anovaOneWayShownColumns(result: any): string[] { + const table = result?.anova_table; + const has = (key: string) => toFiniteNumber(table?.[key]) !== null; + if (!has('p_value')) return []; + return ['Prob>F', ...(has('f_stat') ? ['F'] : []), ...(has('f_stat') && has('df_explained') && has('df_residual') ? ['df'] : [])]; +} + +function rowsTrim(title: string, rows: string[]): TableTrim[] { + return rows.length ? [{ title, rows }] : []; +} + +function columnsTrim(title: string, columns: string[]): TableTrim[] { + return columns.length ? [{ title, columns }] : []; +} + +/** + * What the figures show of each result's tables, so the result view does not repeat it on screen + * (exports keep every table). Worked out from the result itself: a row or column is covered only + * when the figures print its value, so nothing a figure leaves out is hidden with it. + */ +const SHOWN_AS_FIGURES: Record<string, (result: any, context?: SummaryContext) => TableTrim[]> = { + ttest_independent: (result, context) => rowsTrim('Independent T-Test', ttestShownRows(result, context)), + ttest_paired: (result, context) => rowsTrim('Paired T-Test', ttestShownRows(result, context)), + ttest_onesample: (result, context) => rowsTrim('One-Sample T-Test', ttestShownRows(result, context)), + chi_squared: (result) => { + const p = toFiniteNumber(result?.p_value) !== null; + const chi2 = p && toFiniteNumber(result?.chi2) !== null; + return rowsTrim('Chi-Squared Test Summary', metricRows(result, [ + ['p-value', 'p_value', p], + ['Chi-Squared statistic', 'chi2', chi2], + ['Degrees of freedom', 'dof', chi2], + ])); + }, + fisher_exact: (result) => rowsTrim("Fisher's Exact Test Summary", metricRows(result, [ + ['p-value', 'p_value', true], + ['Odds ratio', 'odds_ratio', true], + ])), + // One figure per comparison: the table goes only when every comparison has its figure. + standardized_mean_difference: (result) => { + const comparisons: any[] = Array.isArray(result?.comparisons) ? result.comparisons : []; + return comparisons.length && comparisons.every((comparison) => toFiniteNumber(comparison?.smd) !== null) + ? [{ title: 'Standardized Mean Difference' }] + : []; + }, + linear_regression_cv: (result) => rowsTrim('Error metrics', linearRegressionCvShownRows(result)), + // F, its degrees of freedom and p are in the figures; the sums and mean squares stay. + anova_oneway: (result) => columnsTrim('ANOVA Summary', anovaOneWayShownColumns(result)), + anova_twoway: (result) => columnsTrim('Two-Way ANOVA', anovaTwoWayShownColumns(result)), + // Fold mean and SD are in the accuracy figure; the per-fold rows stay. + naive_bayes_gaussian_cv: (result) => rowsTrim('Accuracy per fold', foldAccuracy(result)?.sd != null ? ['Mean ± SD'] : []), + naive_bayes_categorical_cv: (result) => rowsTrim('Accuracy per fold', foldAccuracy(result)?.sd != null ? ['Mean ± SD'] : []), +}; + +/** The parts of a result's tables its figures already show; `context` is the one the figures were built with. */ +export function tablesShownAsFigures(algorithm: string | null, result: any, context?: SummaryContext): TableTrim[] { + const shown = algorithm ? SHOWN_AS_FIGURES[algorithm] : undefined; + return shown && result && typeof result === 'object' ? shown(result, context) : []; +} + +/** Headline figures for a result (hypothesis tests, cross-validated classifiers), or null when the algorithm has none. */ +export function buildSummaryFigures(algorithm: string | null, result: any, context?: SummaryContext): KeyFigure[] | null { + const builder = algorithm ? BUILDERS[algorithm] : undefined; + if (!builder || !result || typeof result !== 'object') return null; + const figures = builder(result, context); + return figures.length ? figures : null; +} diff --git a/src/app/pages/experiments-dashboard/dashboard-query.utils.ts b/src/app/pages/experiments-dashboard/dashboard-query.utils.ts new file mode 100644 index 000000000..a354375f5 --- /dev/null +++ b/src/app/pages/experiments-dashboard/dashboard-query.utils.ts @@ -0,0 +1,29 @@ +/** + * Dashboard URL state helpers. + * + * The dashboard keeps selection, tab, page, filters and sort in the query string so a + * refresh, bookmark or browser Back returns the same view. It uses history.replaceState + * instead of Router.navigate so changing a filter never restarts route resolution or + * creates a history entry for every keystroke. Components listen for popstate and re-read + * the query themselves. + */ +export type DashboardQueryPatch = Record<string, string | null | undefined>; + +export function readDashboardQuery(): URLSearchParams { + return new URLSearchParams(window.location.search); +} + +export function updateDashboardQuery(patch: DashboardQueryPatch): void { + const params = readDashboardQuery(); + for (const [key, value] of Object.entries(patch)) { + if (value === null || value === undefined || value === '') { + params.delete(key); + } else { + params.set(key, value); + } + } + + const query = params.toString(); + const url = `${window.location.pathname}${query ? `?${query}` : ''}${window.location.hash}`; + window.history.replaceState(window.history.state, '', url); +} diff --git a/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.css b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.css index a8e15b494..1b548fdef 100644 --- a/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.css +++ b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.css @@ -2,83 +2,21 @@ .experiment-details-root { position: relative; /* for toast */ + box-sizing: border-box; display: flex; flex-direction: column; gap: 16px; - height: 100%; -} - -/* META CARD */ - -.experiment-meta-card { - background: var(--glass-bg); - backdrop-filter: var(--glass-blur); - -webkit-backdrop-filter: var(--glass-blur); - border-radius: 20px 20px 0 0; + min-height: 100%; padding: 24px; - border: 1px solid var(--glass-border); - border-bottom: none; - box-shadow: var(--glass-shadow); -} - -.meta-header { - display: flex; - justify-content: space-between; - align-items: center; - margin-bottom: 8px; -} - -.meta-pill { - font-size: 0.7rem; - text-transform: uppercase; - letter-spacing: 0.12em; - color: #94a3b8; -} - -.status-chip { - font-size: 0.65rem; - font-weight: 800; - text-transform: uppercase; - letter-spacing: 0.1em; - padding: 4px 8px; - border-radius: 6px; - background: #f1f5f9; - color: #64748b; - border: 1px solid var(--border-color); - display: inline-flex; - align-items: center; - gap: 4px; -} - -.status-chip.completed { - background: #f0fdf4; - color: #166534; - border-color: #bbf7d0; -} - -.status-chip.completed::before { - content: '\f058'; - font-family: 'Font Awesome 5 Free'; - font-weight: 900; -} - -.status-chip.error { - background: #fef2f2; - color: #991b1b; - border-color: #fecaca; -} - -.status-chip.error::before { - content: '\f06a'; - font-family: 'Font Awesome 5 Free'; - font-weight: 900; } .experiment-title { - margin: 0 0 4px; - font-size: 1.25rem; + margin: 0; + font-size: 1.375rem; font-weight: 700; - color: #020617; + line-height: 1.3; + color: var(--text-main); + text-wrap: pretty; } .experiment-title-row { @@ -87,27 +25,6 @@ gap: 10px; } -.title-edit-btn { - border: 1px solid rgba(148, 163, 184, 0.6); - background: #ffffff; - color: #0f172a; - border-radius: 999px; - padding: 4px 10px; - cursor: pointer; - transition: - background 0.12s ease, - border-color 0.12s ease, - box-shadow 0.12s ease, - transform 0.12s ease; -} - -.title-edit-btn:hover { - background: #e0f2fe; - border-color: var(--primary-color); - box-shadow: 0 6px 12px rgba(15, 23, 42, 0.12); - transform: translateY(-1px); -} - .title-edit-group { display: flex; flex-wrap: wrap; @@ -127,8 +44,8 @@ .title-edit-action { border-radius: 999px; - border: 1px solid rgba(2, 122, 122, 0.5); - background: #e6f7f7; + border: 1px solid rgba(43, 51, 233, 0.3); + background: #eef1fc; padding: 6px 12px; font-size: 0.8rem; color: #0f172a; @@ -153,34 +70,6 @@ color: #b91c1c; } -.experiment-description { - margin: 0 0 10px; - font-size: 0.9rem; - color: #4b5563; -} - -.meta-grid { - display: grid; - grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); - gap: 8px 16px; - margin-bottom: 10px; -} - -.meta-item .label { - display: block; - font-size: 0.7rem; - text-transform: uppercase; - letter-spacing: 0.12em; - color: #94a3b8; -} - -.meta-item .value { - font-size: 0.85rem; - color: #111827; - font-family: 'JetBrains Mono', 'Roboto Mono', monospace; - letter-spacing: -0.02em; -} - /* ACTIONS */ .details-actions { @@ -193,49 +82,55 @@ border-top: 1px solid rgba(148, 163, 184, 0.1); } -.action-group { - display: flex; - gap: 8px; -} - -.ghost-btn { - border-radius: 999px; - border: 1px solid rgba(148, 163, 184, 0.4); - background: rgba(255, 255, 255, 0.6); - padding: 8px 16px; +/* One button shape for the pane: 36px pills in the app font. */ +.ghost-btn, +.detail-primary-btn { + box-sizing: border-box; display: inline-flex; align-items: center; - gap: 8px; + gap: 7px; + height: 36px; + padding: 0 14px; + border-radius: 999px; + font: inherit; font-size: 0.82rem; font-weight: 600; - color: #475569; + white-space: nowrap; cursor: pointer; - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); + transition: background-color 0.15s ease, border-color 0.15s ease; +} + +.ghost-btn { + border: 1px solid rgba(43, 51, 233, 0.16); + background: #ffffff; + color: var(--text-main); } .ghost-btn i { - font-size: 0.9rem; + font-size: 0.72rem; + color: var(--primary-color); } .ghost-btn:hover:not(:disabled) { - background: #ffffff; - color: var(--primary-color); - border-color: var(--primary-color); - box-shadow: 0 4px 12px rgba(2, 128, 144, 0.1); - transform: translateY(-1px); + background: #e5ebfa; + border-color: rgba(43, 51, 233, 0.3); } -.ghost-btn.run-btn { - background: #f0fdfa; - color: #0d9488; - border-color: rgba(13, 148, 136, 0.2); +.detail-primary-btn { + gap: 8px; + padding: 0 16px; + border: none; + background: var(--primary-color); + color: #ffffff; + box-shadow: 0 1px 2px rgba(43, 51, 233, 0.18); +} + +.detail-primary-btn i { + font-size: 0.7rem; } -.ghost-btn.run-btn:hover { - background: #0d9488; - color: white; - border-color: #0d9488; - box-shadow: 0 8px 16px rgba(13, 148, 136, 0.2); +.detail-primary-btn:hover { + background: var(--primary-dark); } .ghost-btn.danger { @@ -251,10 +146,9 @@ /* Shared state */ .ghost-btn.shared { - background: linear-gradient(135deg, var(--primary-color), var(--primary-light)); - border-color: transparent; - color: #ecfeff; - box-shadow: 0 10px 24px rgba(15, 23, 42, 0.25); + background: #eef1fc; + border-color: rgba(43, 51, 233, 0.3); + color: var(--primary-dark); } .ghost-btn.shared i { @@ -262,17 +156,6 @@ } /* Small ghost button variant */ -.ghost-btn.ghost-btn--small { - padding: 4px 10px; - font-size: 0.78rem; -} - -.ghost-btn.ghost-btn--small .icon-download { - width: 14px; - height: 14px; - fill: currentColor; -} - /* MAIN LAYOUT */ .experiment-details-body { @@ -281,483 +164,519 @@ gap: 16px; } -/* CONFIG CARD */ +/* RESULTS CARD */ -.config-card { - margin-top: 0; - width: 100%; - max-width: 100%; - box-sizing: border-box; +.results-card { + border: 1px solid rgba(43, 51, 233, 0.1); + border-radius: var(--radius-lg); overflow: hidden; - border-radius: 0; - padding: 24px; - background: rgba(248, 250, 252, 0.6); - backdrop-filter: var(--glass-blur); - -webkit-backdrop-filter: var(--glass-blur); - border-left: 1px solid var(--glass-border); - border-right: 1px solid var(--glass-border); - font-size: 0.82rem; + background: #ffffff; } -.config-header h3 { - margin: 0; - font-size: 0.95rem; - font-weight: 600; - color: #020617; +.results-card-head { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + padding: 10px 16px; + background: #f8fbff; + border-bottom: 1px solid rgba(43, 51, 233, 0.08); } -.config-subtitle { - margin: 2px 0 12px; - font-size: 0.78rem; - color: #6b7280; +.results-card-head h3 { + margin: 0; + font-size: 0.85rem; + font-weight: 700; + color: var(--text-main); } -/* Wrapper for config content */ -.config-grid { - width: 100%; - max-width: 100%; - box-sizing: border-box; +.results-card-head span { + font-size: 0.75rem; + color: var(--text-muted); } -/* TOP ROW: Domain (left) | Datasets (right) */ -.config-row--top { - display: grid; - grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); - gap: 18px 28px; - align-items: start; +.results-card-body { + padding: 16px; } -/* Each field = label + pill inline */ -.config-field { +/* EMPTY STATE */ + +.empty-details { + height: 100%; + border-radius: var(--radius-lg); + background: var(--card-bg); display: flex; + flex-direction: column; align-items: center; - gap: 10px; - min-width: 0; - /* critical for preventing overflow */ + justify-content: center; + padding: 40px; + text-align: center; + border: 1px dashed var(--border-color); + box-shadow: none; } -/* shared label style */ -.config-field .label { +.empty-details h2 { + margin: 0 0 10px; + font-size: 1.4rem; + font-weight: 700; + color: var(--text-main); + letter-spacing: 0.01em; +} + +.empty-details p { margin: 0; - flex: 0 0 auto; - white-space: nowrap; - font-size: 0.7rem; - text-transform: uppercase; - letter-spacing: 0.12em; - color: #94a3b8; + font-size: 1rem; + color: var(--text-muted); + max-width: 320px; + line-height: 1.6; } -/* Pill containers must be shrinkable */ -.domain-value, -.datasets-value { +/* MESSAGES */ + +.error-box { + margin-top: 12px; + padding: 10px 12px; + border-radius: 10px; + background: #fef2f2; + color: #b91c1c; + font-size: 0.85rem; + border: 1px solid #fecaca; +} + +.no-data-text { + margin-top: 12px; + font-size: 0.85rem; + color: #6b7280; +} + +/* TOAST */ + +.copy-toast { + position: absolute; + top: 10px; + right: 16px; display: inline-flex; align-items: center; gap: 8px; - max-width: 100%; - min-width: 0; + padding: 6px 12px; + border-radius: 999px; + background: rgba(15, 23, 42, 0.92); + color: #ffffff; + font-size: 0.8rem; + box-shadow: 0 12px 30px rgba(15, 23, 42, 0.3); + animation: fadeInOut 2.4s ease-out; + z-index: 10; +} + +.copy-toast i { + font-size: 0.9rem; +} + +@keyframes fadeInOut { + 0% { + opacity: 0; + transform: translateY(-4px); + } + + 10% { + opacity: 1; + transform: translateY(0); + } + + 80% { + opacity: 1; + } + + 100% { + opacity: 0; + transform: translateY(-4px); + } +} + +/* RESPONSIVE */ + +@media (max-width: 960px) { + + .experiment-details-root, + .empty-details { + padding: 16px; + } } -/* Stop “one letter per line” */ -.domain-text, -.datasets-text { +.detail-header { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + align-items: flex-start; + gap: 16px; +} + +.detail-heading { + display: flex; + flex-direction: column; + gap: 4px; min-width: 0; - white-space: normal; - word-break: normal; - overflow-wrap: break-word; } -/* Domain pill */ -.domain-value { - position: relative; - padding: 4px 12px; - border-radius: 999px; - background: radial-gradient(circle at top left, #ecfeff, #f9fafb); - border: 1px solid rgba(148, 163, 184, 0.7); - box-shadow: - 0 6px 14px rgba(15, 23, 42, 0.08), - 0 0 0 1px rgba(148, 163, 184, 0.35); - font-size: 0.8rem; - color: #0f172a; - transition: - transform 0.16s ease, - border-color 0.16s ease, - box-shadow 0.16s ease; +.detail-kicker { + display: block; + font-size: 0.72rem; + font-weight: 700; + letter-spacing: 0.05em; + text-transform: uppercase; + color: var(--text-muted); } -.domain-value::before { - content: ""; - position: absolute; - inset: -2px; - border-radius: inherit; - background: radial-gradient(circle at center, rgba(56, 189, 248, 0.35), transparent 65%); - opacity: 0; - pointer-events: none; - transition: opacity 0.2s ease; - z-index: -1; +.detail-meta { + display: flex; + flex-wrap: wrap; + gap: 6px 10px; + align-items: center; + margin-top: 6px; + color: var(--text-muted); + font-size: 0.8125rem; } -.domain-value:hover { - transform: translateY(-1px); - border-color: rgba(56, 189, 248, 0.85); - box-shadow: - 0 10px 24px rgba(15, 23, 42, 0.18), - 0 0 0 1px rgba(56, 189, 248, 0.4); +.detail-meta-sep { + color: #cbd5e1; } -.domain-value:hover::before { - opacity: 1; +.detail-meta-visibility { + display: inline-flex; + align-items: center; + gap: 5px; } -.domain-value i { - font-size: 0.78rem; - color: var(--primary-color); - opacity: 0.9; +.detail-meta-visibility i { + font-size: 0.625rem; } -.domain-text { - font-family: "JetBrains Mono", "SFMono-Regular", ui-monospace, monospace; - letter-spacing: 0.03em; - white-space: nowrap; - /* nicer for domain */ +.detail-header .details-actions { + margin-top: 0; + padding-top: 0; + border-top: 0; + justify-content: flex-end; + flex-wrap: wrap; + gap: 8px; } -.domain-value.domain-value--empty { - background: transparent; - border-style: dashed; - border-color: rgba(148, 163, 184, 0.8); - box-shadow: none; - color: #9ca3af; +.detail-more { + position: relative; } -.domain-value.domain-value--empty::before { - opacity: 0; +.detail-more summary { + list-style: none; + cursor: pointer; + width: 36px; + height: 36px; + box-sizing: border-box; + display: grid; + place-items: center; + border-radius: 999px; + border: 1px solid rgba(43, 51, 233, 0.16); + background: #fff; + color: var(--text-muted); + font-size: 0.8125rem; + line-height: 1; } -.domain-value.domain-value--empty i { - color: #9ca3af; +.detail-more summary:hover { + background: #e5ebfa; } -/* Datasets chip */ -.datasets-value { - padding: 6px 12px; - border-radius: 14px; - background: rgba(255, 255, 255, 0.72); - border: 1px solid rgba(226, 232, 240, 0.95); - box-shadow: - 0 10px 22px rgba(15, 23, 42, 0.06), - inset 0 0 0 1px rgba(148, 163, 184, 0.18); - color: #0f172a; - font-size: 0.82rem; - line-height: 1.35; - transition: - transform 0.16s ease, - border-color 0.16s ease, - box-shadow 0.16s ease; +.detail-more summary::-webkit-details-marker { + display: none; } -.datasets-value:hover { - transform: translateY(-1px); - border-color: rgba(56, 189, 248, 0.45); - box-shadow: - 0 12px 26px rgba(15, 23, 42, 0.08), - inset 0 0 0 1px rgba(56, 189, 248, 0.12); +.detail-more-menu { + position: absolute; + right: 0; + z-index: 5; + min-width: 160px; + margin-top: 4px; + padding: 4px; + background: var(--card-bg); + border: 1px solid var(--border-color); + border-radius: var(--radius-md); + box-shadow: var(--studio-card-shadow); } -.datasets-value i { - font-size: 0.85rem; - color: var(--primary-color); - opacity: 0.85; +.detail-more-menu button { + display: block; + width: 100%; + text-align: left; + background: none; + border: 0; + padding: 8px 10px; + font: inherit; + cursor: pointer; + color: var(--text-main); + border-radius: 4px; } -.datasets-value.datasets-value--empty { - background: transparent; - border-style: dashed; - border-color: rgba(148, 163, 184, 0.8); - box-shadow: none; - color: #9ca3af; +.detail-more-menu button:hover { + background: #f3f5fe; } -.datasets-value.datasets-value--empty i { - color: #9ca3af; +.detail-more-danger { + color: #991b1b; } -/* ───────── TAGS (Variables / Covariates / Filters) ───────── */ +/* MODEL BAND */ -.config-tags-row:first-of-type { - margin-top: 12px; - padding-top: 10px; - border-top: 1px dashed rgba(148, 163, 184, 0.5); +.model-band { + display: grid; + grid-template-columns: minmax(0, 1.5fr) repeat(3, minmax(0, 1fr)); + border: 1px solid rgba(43, 51, 233, 0.1); + border-radius: var(--radius-lg); + background: #f8fbff; } -.config-tags-row { - margin-top: 8px; +.model-band-cell { display: flex; - align-items: flex-start; - gap: 10px; - flex-wrap: wrap; + flex-direction: column; + gap: 4px; + min-width: 0; + padding: 14px 16px; + border-left: 1px solid rgba(43, 51, 233, 0.08); } -.config-tags-row .label { - margin-top: 3px; - font-size: 0.7rem; +.model-band-cell--model { + gap: 8px; + border-left: 0; +} + +.model-band-cell h4 { + margin: 0; + font-size: 0.6875rem; + font-weight: 700; + letter-spacing: 0.06em; text-transform: uppercase; - letter-spacing: 0.12em; - color: #9ca3af; - flex-shrink: 0; + color: var(--text-muted); } -.tag-list { - display: flex; - flex-direction: column; - align-items: flex-start; - gap: 6px; +.model-band-value { + font-size: 0.85rem; + font-weight: 600; + color: var(--text-main); } -.tag { - padding: 4px 12px; - border-radius: 999px; - background: rgba(2, 128, 144, 0.08); - color: var(--primary-color); - font-size: 0.75rem; +.model-band-text { + font-size: 0.78rem; + line-height: 1.4; + color: var(--text-muted); + overflow-wrap: anywhere; +} + +.setup-fold { + font-size: 0.78rem; + line-height: 1.4; +} + +.setup-fold summary { + cursor: pointer; font-weight: 600; - border: 1px solid rgba(2, 128, 144, 0.15); - box-shadow: 0 2px 8px rgba(2, 128, 144, 0.04); - transition: all 0.2s ease; + color: var(--text-main); } -.tag:hover { - background: rgba(2, 128, 144, 0.12); - transform: translateY(-1px); +.setup-fold summary:hover { + color: var(--primary-color); } -/* RESULTS CARD */ +.setup-fold p, +.setup-fold ul { + margin: 4px 0 0; + color: var(--text-muted); +} -.results-card { - flex: 1; - border-radius: 0 0 24px 24px; - background: var(--glass-bg); - backdrop-filter: var(--glass-blur); - -webkit-backdrop-filter: var(--glass-blur); - padding: 24px; - border: 1px solid var(--glass-border); - border-top: 1px dashed rgba(148, 163, 184, 0.2); - display: flex; - flex-direction: column; - margin-top: 0; - box-shadow: var(--glass-shadow); +.setup-fold ul { + padding-left: 1.1rem; } -.results-header { +.model-formula { display: flex; - justify-content: space-between; + flex-wrap: wrap; align-items: center; - gap: 8px; + gap: 6px; + font-size: 0.82rem; + line-height: 1.3; } -.results-header>div { - display: flex; - flex-direction: column; +.model-var { + display: inline-flex; + align-items: center; + gap: 6px; + min-width: 0; + padding: 3px 8px 3px 3px; + border-radius: 6px; + background: #ffffff; + border: 1px solid rgba(43, 51, 233, 0.1); + color: var(--text-main); + overflow-wrap: anywhere; } -.results-card h3 { - margin: 0 0 12px 0; - font-size: 1.1rem; +.model-var--y { font-weight: 600; - color: #006f7a; } -.results-subtitle { - margin: 4px 0 0; - font-size: 0.8rem; - color: #6b7280; +.role-marker { + flex-shrink: 0; + display: grid; + place-items: center; + width: 18px; + height: 18px; + border-radius: 4px; + font-size: 0.656rem; + font-weight: 700; } -.results-placeholder { - margin-top: 12px; - padding: 14px 20px; - border-radius: 14px; - background: rgba(239, 246, 255, 0.4); - backdrop-filter: blur(4px); - border: 1px dashed var(--primary-light); - font-size: 0.85rem; - color: #4b5563; - font-family: 'JetBrains Mono', 'Roboto Mono', monospace; +.role-marker--y { + background: #fdf3e1; + color: #78350f; } -.placeholder-badge { - display: inline-block; - padding: 3px 8px; - border-radius: 999px; - font-size: 0.7rem; - text-transform: uppercase; - letter-spacing: 0.12em; - background: #0f766e; - color: #ecfeff; - margin-bottom: 6px; +.role-marker--x { + background: #eef1fc; + color: #1b21a3; } -/* EMPTY STATE */ +.model-tilde { + padding: 0 2px; + font-family: ui-monospace, SFMono-Regular, Menlo, monospace; + font-size: 0.94rem; + color: var(--text-muted); +} -.empty-details { - height: 100%; - border-radius: 24px; - background: linear-gradient(135deg, rgba(253, 242, 255, 0.2), rgba(224, 242, 254, 0.3)); +/* RUN STATES */ + +.run-failed { display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - padding: 40px; - text-align: center; - border: 1px dashed rgba(148, 163, 184, 0.3); - box-shadow: inset 0 0 40px rgba(15, 23, 42, 0.02); + align-items: flex-start; + gap: 14px; + padding: 20px; + border: 1px solid rgba(225, 29, 72, 0.2); + border-radius: var(--radius-lg); + background: #fff1f2; } -.empty-illustration { - width: 80px; - height: 80px; - background: white; - border-radius: 24px; +.run-failed-icon { + flex-shrink: 0; display: grid; place-items: center; - font-size: 2rem; - color: var(--primary-color); - margin-bottom: 24px; - box-shadow: 0 10px 30px rgba(15, 23, 42, 0.06); - border: 1px solid rgba(148, 163, 184, 0.1); + width: 32px; + height: 32px; + box-sizing: border-box; + border-radius: 999px; + border: 1px solid rgba(225, 29, 72, 0.2); + background: #ffffff; + color: #991b1b; + font-size: 0.95rem; } -.empty-details h2 { - margin: 0 0 10px; - font-size: 1.4rem; - font-weight: 800; - color: #0f172a; - letter-spacing: -0.01em; +.run-failed-body { + display: flex; + flex-direction: column; + gap: 6px; + min-width: 0; } -.empty-details p { +.run-failed h3, +.run-pending h3 { margin: 0; - font-size: 1rem; - color: #64748b; - max-width: 320px; - line-height: 1.6; + font-size: 0.95rem; + font-weight: 700; + color: var(--text-main); } -/* MESSAGES */ - -.error-box { - margin-top: 12px; - padding: 10px 12px; - border-radius: 10px; - background: #fef2f2; - color: #b91c1c; +.run-failed p { + margin: 0; + max-width: 62ch; font-size: 0.85rem; - border: 1px solid #fecaca; + line-height: 1.55; + color: var(--text-muted); + text-wrap: pretty; } -.no-data-text { - margin-top: 12px; - font-size: 0.85rem; - color: #6b7280; +.run-failed-actions { + display: flex; + flex-wrap: wrap; + gap: 8px; + margin-top: 8px; } -/* MIP VERSION BAR */ +.run-pending { + display: flex; + flex-direction: column; + gap: 12px; + padding: 20px; + border: 1px solid rgba(43, 51, 233, 0.1); + border-radius: var(--radius-lg); + background: #ffffff; +} -.mip-version-bar { +.run-pending-head { display: flex; + flex-wrap: wrap; align-items: center; - gap: 8px; - padding: 10px 24px; - background: rgba(248, 250, 252, 0.8); - border: 1px solid var(--glass-border); - border-radius: 0 0 20px 20px; - margin-top: -8px; - font-size: 0.75rem; - color: #64748b; + justify-content: space-between; + gap: 12px; } -.mip-version-label { - font-weight: 600; - text-transform: uppercase; - letter-spacing: 0.08em; - color: #94a3b8; +.run-pending-head span { + font-size: 0.78rem; + color: var(--text-muted); } -.mip-version-value { - font-family: 'JetBrains Mono', 'Roboto Mono', monospace; - color: #475569; +.run-pending-bar { + position: relative; + height: 6px; + border-radius: 999px; + background: #eef1fc; + overflow: hidden; } -/* TOAST */ - -.copy-toast { +.run-pending-bar span { position: absolute; - top: 10px; - right: 16px; - display: inline-flex; - align-items: center; - gap: 8px; - padding: 6px 12px; + inset: 0 auto 0 0; + width: 40%; border-radius: 999px; - background: rgba(15, 118, 110, 0.96); - color: #ecfeff; - font-size: 0.8rem; - box-shadow: 0 12px 30px rgba(15, 23, 42, 0.3); - animation: fadeInOut 2.4s ease-out; - z-index: 10; -} - -.copy-toast i { - font-size: 0.9rem; + background: var(--primary-color); + animation: run-pending-slide 1.4s cubic-bezier(0.23, 1, 0.32, 1) infinite; } -@keyframes fadeInOut { - 0% { - opacity: 0; - transform: translateY(-4px); - } - - 10% { - opacity: 1; - transform: translateY(0); - } - - 80% { - opacity: 1; +@keyframes run-pending-slide { + from { + transform: translateX(-100%); } - 100% { - opacity: 0; - transform: translateY(-4px); + to { + transform: translateX(250%); } } -/* RESPONSIVE */ - -@media (max-width: 960px) { - - .experiment-meta-card, - .results-card, - .empty-details { - padding: 16px; - } - - .config-card { - padding: 12px 14px; +@media (prefers-reduced-motion: reduce) { + .run-pending-bar span { + animation: none; + width: 100%; + opacity: 0.35; } } @media (max-width: 900px) { - .config-row--top { - grid-template-columns: 1fr; - /* stack */ + .model-band { + grid-template-columns: repeat(2, minmax(0, 1fr)); } - .config-field { - align-items: flex-start; + .model-band-cell--model { + grid-column: 1 / -1; } - .domain-text { - white-space: normal; - word-break: break-word; + .model-band-cell:nth-child(2) { + border-left: 0; + border-top: 1px solid rgba(43, 51, 233, 0.08); } -} \ No newline at end of file + + .model-band-cell:nth-child(n + 3) { + border-top: 1px solid rgba(43, 51, 233, 0.08); + } +} diff --git a/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.html b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.html index c76be5735..5db1e4849 100644 --- a/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.html +++ b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.html @@ -1,4 +1,4 @@ -<div class="experiment-details-root"> +<div class="experiment-details-root" data-guide="dashboard-workbench"> @if (copyToastVisible()) { <div class="copy-toast"> <i class="fas fa-check-circle"></i> @@ -8,216 +8,213 @@ @if (!selectedExperiment()) { <div class="empty-details"> - <div class="empty-illustration"> - <i class="fas fa-microscope"></i> - </div> - <h2>Research Workbench</h2> - <p> - Select an experiment to explore insights, or create a new one to begin your investigation. - </p> + <p>Select an experiment from the list.</p> </div> } @else { - <div class="experiment-details-body" #resultsCard> - <!-- META CARD --> - <section class="experiment-meta-card"> - <div class="meta-header"> - <span class="meta-pill">Experiment</span> - - <span class="status-chip" [ngClass]="{ - completed: selectedExperiment()?.status === 'success', - error: selectedExperiment()?.status === 'error' - }"> - {{ selectedExperiment()?.status }} - </span> - </div> - - <div class="experiment-title-row"> - @if (!isEditingName()) { - <h1 class="experiment-title"> - {{ selectedExperiment()?.name || "Untitled Investigation" }} - </h1> - <button type="button" class="title-edit-btn" (click)="startNameEdit()" title="Edit experiment name"> - <i class="far fa-edit"></i> - </button> - } @else { - <div class="title-edit-group"> - <input class="title-edit-input" [value]="nameDraft()" (input)="nameDraft.set($any($event.target).value)" - [disabled]="nameSaving()" /> - <button type="button" class="title-edit-action" (click)="saveNameEdit()" [disabled]="nameSaving()"> - Save - </button> - <button type="button" class="title-edit-action cancel" (click)="cancelNameEdit()" [disabled]="nameSaving()"> - Cancel - </button> + <div class="experiment-details-body" #resultsCard data-guide="dashboard-detail-card"> + <header class="detail-header"> + <div class="detail-heading"> + <span class="detail-kicker">{{ algorithmLabel() }}</span> + + <div class="experiment-title-row"> + @if (!isEditingName()) { + <h2 class="experiment-title"> + {{ selectedExperiment()?.name || "Untitled Investigation" }} + </h2> + } @else { + <div class="title-edit-group"> + <input class="title-edit-input" [value]="nameDraft()" (input)="nameDraft.set($any($event.target).value)" + [disabled]="nameSaving()" /> + <button type="button" class="title-edit-action" (click)="saveNameEdit()" [disabled]="nameSaving()"> + Save + </button> + <button type="button" class="title-edit-action cancel" (click)="cancelNameEdit()" [disabled]="nameSaving()"> + Cancel + </button> + </div> + } </div> + @if (nameError()) { + <p class="name-error">{{ nameError() }}</p> } - </div> - @if (nameError()) { - <p class="name-error">{{ nameError() }}</p> - } - - <div class="meta-grid"> - <div class="meta-item"> - <span class="label">Created</span> - <span class="value"> - {{ selectedExperiment()?.dateCreated | date : "medium" }} - </span> - </div> - <div class="meta-item"> - <span class="label">Algorithm</span> - <span class="value"> - {{ algorithmLabel() }} + <div class="detail-meta"> + <app-experiment-status [status]="selectedExperiment()?.status || 'unknown'" /> + <span>{{ selectedExperiment()?.dateCreated | date : "medium" }}</span> + <span class="detail-meta-sep" aria-hidden="true">·</span> + <span>{{ authorLine() }}</span> + <span class="detail-meta-sep" aria-hidden="true">·</span> + <span class="detail-meta-visibility"> + <i class="fas" [class.fa-lock]="visibilityLine() === 'Private'" + [class.fa-users]="visibilityLine() !== 'Private'" aria-hidden="true"></i> + {{ visibilityLine() }} </span> </div> </div> - <div class="meta-item"> - <span class="label">Created by</span> - <span class="value"> - {{ selectedExperiment()?.author || "Unknown" }} - </span> - </div> - - <div class="details-actions"> - <div class="action-group primary-actions"> - <button type="button" class="primary-action-btn run-btn" (click)="runExperiment()"> - <i class="far fa-edit"></i> - <span>Edit</span> - </button> - <button type="button" class="primary-action-btn export-btn" (click)="onExportPdf()" title="Export to PDF"> - <i class="far fa-file-pdf"></i> - <span>Export PDF</span> - </button> - </div> - - <div class="action-group secondary-actions"> - <!-- Copy Link (Always visible) --> - <button type="button" class="ghost-btn copy-btn" (click)="onCopyLink()" title="Copy link"> - <i class="fas fa-link"></i> - <span>Copy Link</span> - </button> + <div class="details-actions" data-guide="dashboard-detail-actions"> + @if (isOwner()) { + <button type="button" class="ghost-btn share-btn" [class.shared]="isShared()" (click)="onToggleShare()" + [title]="isShared() ? 'Stop sharing' : 'Share with others'"> + <i class="fas fa-user-plus" aria-hidden="true"></i> + <span>{{ isShared() ? "Shared" : "Share" }}</span> + </button> + } @else { + <button type="button" class="ghost-btn" (click)="onCopyLink()"> + <i class="fas fa-link" aria-hidden="true"></i> + Copy link + </button> + } - <!-- Share (Owner only) --> - @if (isOwner()) { - <button type="button" class="ghost-btn share-btn" [class.shared]="isShared()" (click)="onToggleShare()" - [title]="isShared() ? 'Stop sharing' : 'Share with others'"> - <i class="fas" [class.fa-eye]="isShared()" [class.fa-share-alt]="!isShared()"></i> - <span>{{ isShared() ? "Shared" : "Share" }}</span> - </button> - } + <button type="button" class="ghost-btn" (click)="onExportPdf()" title="Export to PDF"> + <i class="fas fa-file-arrow-down" aria-hidden="true"></i> + Export PDF + </button> - @if (isOwner()) { - <button type="button" class="ghost-btn danger delete-btn" (click)="onDelete()"> - <i class="far fa-trash-alt"></i> - <span>Delete</span> - </button> - } - </div> - </div> - </section> + <button type="button" class="ghost-btn" (click)="onExportZip()" + title="Download every result table as CSV, the full result data and a README, in one ZIP"> + <i class="fas fa-file-zipper" aria-hidden="true"></i> + Export CSV (ZIP) + </button> - <!-- CONFIGURATION CARD --> - @if (selectedExperiment(); as exp) { - <section class="config-card"> - <div class="config-header"> - <h3>Configuration</h3> - <p class="config-subtitle"> - Data model, datasets and variables used in this run. - </p> - </div> + <button type="button" class="detail-primary-btn" (click)="runExperiment()" data-guide="dashboard-edit-studio"> + Open in Studio + <i class="fas fa-arrow-right" aria-hidden="true"></i> + </button> - <div class="config-grid"> - <!-- Row 1: Domain | Datasets --> - <div class="config-row config-row--top"> - <!-- Domain --> - <div class="config-field"> - <span class="label">Domain</span> - <div class="domain-value" [class.domain-value--empty]="!selectedExperiment()?.domain"> - <i class="fas fa-layer-group"></i> - <span class="domain-text">{{ - domainLabel() - }}</span> - </div> + <details class="detail-more"> + <summary aria-label="More actions"><i class="fas fa-ellipsis" aria-hidden="true"></i></summary> + <div class="detail-more-menu"> + <button type="button" (click)="onCopyLink()">Copy link</button> + <button type="button" (click)="startNameEdit()">Rename</button> + @if (isOwner()) { + <button type="button" class="detail-more-danger" (click)="onDelete()">Delete…</button> + } </div> - </div> + </details> </div> - - @if (datasetsWithLabels().length) { - <div class="config-tags-row"> - <span class="label">Datasets</span> - <div class="tag-list"> - @for (d of datasetsWithLabels(); track d.code) { - <span class="tag"> - <i class="fas fa-database" style="margin-right: 4px; font-size: 0.7rem; opacity: 0.8;"></i> - {{ d.label }} - </span> + </header> + + <!-- The model band: the formula names the experiment, the rest is how it was run. It sits above + the results so they get the pane's full width. --> + <div class="model-band" aria-label="Setup"> + <section class="model-band-cell model-band-cell--model"> + <h4>Model</h4> + <div class="model-formula"> + @for (item of variablesWithLabels(); track item.code) { + <span class="model-var model-var--y"><span class="role-marker role-marker--y" aria-label="Outcome">y</span>{{ item.label }}</span> } - </div> - </div> - } - @if (variablesWithLabels().length) { - <div class="config-tags-row"> - <span class="label">Variables (Y)</span> - <div class="tag-list"> - @for (v of variablesWithLabels(); track v.code) { - <span class="tag">{{ v.label }}</span> + @if (variablesWithLabels().length && covariatesWithLabels().length) { + <span class="model-tilde" aria-hidden="true">~</span> } - </div> - </div> - } @if (covariatesWithLabels().length) { - <div class="config-tags-row"> - <span class="label">Covariates (X)</span> - <div class="tag-list"> - @for (c of covariatesWithLabels(); track c.code) { - <span class="tag">{{ c.label }}</span> + @for (item of covariatesWithLabels(); track item.code) { + <span class="model-var"><span class="role-marker role-marker--x" aria-label="Covariate">x</span>{{ item.label }}</span> } - </div> - </div> - } @if (filtersWithLabels().length) { - <div class="config-tags-row"> - <span class="label">Filters</span> - <div class="tag-list"> - @for (f of filtersWithLabels(); track f.code) { - <span class="tag">{{ f.label }}</span> + @if (!variablesWithLabels().length && !covariatesWithLabels().length) { + <span class="model-band-text">—</span> } </div> - </div> - } - </section> - } + </section> + + <section class="model-band-cell"> + <h4>Data</h4> + <span class="model-band-value">{{ datasetCountLine() }}</span> + @if (datasetNames()) { + <span class="model-band-text">{{ datasetNames() }}</span> + } + </section> + + <section class="model-band-cell"> + <h4>Cohort</h4> + @if (rowsUsed(); as rows) { + <span class="model-band-value">{{ rows }} rows</span> + } @else if (pendingRun()) { + <span class="model-band-value">Not finished</span> + } + @if (filterPreview(); as filters) { + <details class="setup-fold"> + <summary>Filters</summary> + <p>{{ filters }}</p> + </details> + } + @for (entry of preprocessingEntries(); track entry.label) { + <details class="setup-fold"> + <summary>{{ entry.label }}</summary> + <ul> + @for (line of entry.lines?.length ? entry.lines : [entry.value]; track $index) { + <li>{{ line }}</li> + } + </ul> + </details> + } + </section> + + <section class="model-band-cell"> + <h4>Run</h4> + @if (parameterEntries().length) { + <details class="setup-fold"> + <summary>Parameters</summary> + <ul> + @for (entry of parameterEntries(); track entry.key) { + <li>{{ entry.label }}: {{ entry.value }}</li> + } + </ul> + </details> + } @else { + <span class="model-band-text">No parameters</span> + } + @if (selectedExperiment()?.mipVersion; as version) { + <span class="model-band-text">MIP {{ version }}</span> + } + </section> + </div> - <!-- RESULTS --> - <section class="results-card"> - <div class="results-header"> - <div> - <h3>Results</h3> - <p class="results-subtitle"> - Visualisation and tables for the selected experiment. - </p> + @if (failedRun()) { + <section class="run-failed"> + <span class="run-failed-icon" aria-hidden="true"><i class="fas fa-circle-exclamation"></i></span> + <div class="run-failed-body"> + <h3>This run didn't finish</h3> + <p>{{ failureMessage() }}</p> + <div class="run-failed-actions"> + <button type="button" class="detail-primary-btn" (click)="runExperiment()">Fix in Studio</button> + <button type="button" class="ghost-btn" (click)="copyFailure()"> + <i class="far fa-copy" aria-hidden="true"></i> + Copy error + </button> </div> </div> - - @if (isLoading()) { - <app-spinner [overlay]="false"></app-spinner> - } @else if (loadError()) { - <div class="error-box">{{ loadError() }}</div> - } @else if (experimentResult()) { - <app-algorithm-result [result]="enrichedResult()" [schema]="outputSchema()" - [algorithm]="experimentalAlgorithmName()" [enumMaps]="enumMaps()" [yVar]="yVar()" [xVar]="xVar()" - [labelMap]="labelMap()" /> - } @else { - <p class="no-data-text">No results available for this experiment yet.</p> - } </section> - - <!-- MIP VERSION --> - @if (selectedExperiment()?.mipVersion) { - <div class="mip-version-bar"> - <span class="mip-version-label">MIP Version:</span> - <span class="mip-version-value">{{ selectedExperiment()?.mipVersion }}</span> - </div> + } @else if (pendingRun()) { + <section class="run-pending" aria-busy="true"> + <div class="run-pending-head"> + <h3>Running on {{ datasetCount() }}</h3> + <span>Results appear here when the run finishes</span> + </div> + <div class="run-pending-bar" role="progressbar" aria-label="Run in progress"><span></span></div> + </section> + } @else { + <section class="results-card" data-guide="dashboard-results"> + <header class="results-card-head"> + <h3>Results</h3> + @if (rowsUsed()) { + <span>n = {{ rowsUsed() }}</span> + } + </header> + <div class="results-card-body"> + @if (isLoading()) { + <app-spinner [overlay]="false"></app-spinner> + } @else if (loadError()) { + <div class="error-box">{{ loadError() }}</div> + } @else if (experimentResult()) { + <app-algorithm-result [result]="enrichedResult()" [schema]="outputSchema()" + [algorithm]="experimentalAlgorithmName()" [enumMaps]="enumMaps()" [yVar]="yVar()" [xVar]="xVar()" + [labelMap]="labelMap()" [parameters]="runParameters()" /> + } @else { + <p class="no-data-text">No results available for this experiment yet.</p> + } + </div> + </section> } </div> } -</div> \ No newline at end of file +</div> diff --git a/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.spec.ts b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.spec.ts new file mode 100644 index 000000000..c145ce85f --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.spec.ts @@ -0,0 +1,68 @@ +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { provideRouter } from '@angular/router'; +import { provideEchartsCore } from 'ngx-echarts'; +import { of } from 'rxjs'; + +import { Experiment } from '../../../models/experiments-dashboard.model'; +import { ExperimentLabelService } from '../../../services/experiment-label.service'; +import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentDetailsComponent } from './experiment-detail.component'; + +describe('ExperimentDetailsComponent', () => { + const experiment: Experiment = { + id: 'run-1', + name: 'Age by sex', + dateCreated: new Date('2026-01-01T00:00:00.000Z'), + status: 'success', + algorithmName: 'ttest_independent', + author: 'Marie Curie', + authorEmail: 'marie.curie@chuv.ch', + isShared: false, + }; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [ExperimentDetailsComponent], + providers: [ + provideZonelessChangeDetection(), + provideRouter([]), + provideEchartsCore({ echarts: () => import('echarts') }), + { + provide: ExperimentsDashboardService, + useValue: { + getExperimentResult: () => of({ + result: { t_stat: -3.9, df: 120, p: 0.0001, mean_diff: -1.2, ci_lower: -1.8, ci_upper: -0.6 }, + analysis: { algorithm: { x: ['sex'], y: ['age'], parameters: { groupA: 'M', groupB: 'F' } } }, + }), + }, + }, + { provide: ExperimentLabelService, useValue: { getLabelMap: async () => ({}), getEnumMaps: async () => ({}) } }, + { + provide: ExperimentStudioService, + useValue: { + backendAlgorithms: signal({}), + algorithmAssignableVariables: () => [], + selectedFilters: () => [], + getDatasetLabelMap: () => ({}), + formatPreprocessingConfig: () => '', + formatPreprocessingEntries: () => [], + }, + }, + ], + }).compileComponents(); + }); + + it('hands the run parameters to the result, which names the groups the t-test compared', async () => { + const fixture = TestBed.createComponent(ExperimentDetailsComponent); + fixture.componentRef.setInput('selectedExperiment', experiment); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + + expect(fixture.componentInstance.runParameters()).toEqual({ groupA: 'M', groupB: 'F' }); + const figures = (fixture.nativeElement as HTMLElement).querySelector('app-key-figures'); + expect(figures?.textContent).toContain('M − F'); + }); +}); diff --git a/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.ts b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.ts index 31e9d7ca6..7b65138d8 100644 --- a/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.ts +++ b/src/app/pages/experiments-dashboard/experiment-detail/experiment-detail.component.ts @@ -1,4 +1,4 @@ -import { Component, ElementRef, EventEmitter, Output, ViewChild } from '@angular/core'; +import { ChangeDetectionStrategy, Component, ElementRef, ViewChild, output, inject } from '@angular/core'; import { CommonModule } from '@angular/common'; import { input, computed, effect, signal } from '@angular/core'; import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; @@ -6,28 +6,47 @@ import { ExperimentStudioService } from '../../../services/experiment-studio.ser import { Experiment } from '../../../models/experiments-dashboard.model'; import { BackendExperimentWithResult } from '../../../models/backend-experiment.model'; import { AlgorithmResultComponent } from '../../experiment-studio/algorithm-panel/algorithm-result/algorithm-result.component'; -import { getOutputSchema } from '../../../core/algorithm-mappers'; +import { getOutputSchema, prettifyLabel } from '../../../core/algorithm-mappers'; +import { enrichPcaResult, observationCount, pluralize, withLabels } from '../../../core/result-label.utils'; import { SpinnerComponent } from '../../shared/spinner/spinner.component'; -import { ResultsPdfExportService } from '../../../services/export-results-pdf.service'; +import { ExperimentStatusComponent } from '../shared/experiment-status/experiment-status.component'; +import { experimentTables, PdfExportService } from '../../../services/pdf-export.service'; +import { CsvExportService } from '../../../services/csv-export.service'; import { Router } from '@angular/router'; +import { buildExperimentShareUrl, copyShareUrl, isExperimentOwner, SHARE_TOAST, shareToggleToast } from '../../../core/share.utils'; import { ExperimentLabelService } from '../../../services/experiment-label.service'; import { EnumMaps } from '../../../core/algorithm-result-enum-mapper'; +import { parameterDefaultsFromSchema } from '../../../core/algorithm-parameter.utils'; +import { formatFilterExpression } from '../../../core/filter-display.utils'; +import { preprocessingStepsToRecord } from '../experiments-dashboard.mapper'; @Component({ selector: 'app-experiment-details', templateUrl: './experiment-detail.component.html', - styleUrls: ['./experiment-detail.component.css'], - imports: [CommonModule, AlgorithmResultComponent, SpinnerComponent] + styleUrl: './experiment-detail.component.css', + imports: [CommonModule, AlgorithmResultComponent, SpinnerComponent, ExperimentStatusComponent], + changeDetection: ChangeDetectionStrategy.OnPush, }) export class ExperimentDetailsComponent { + private dashboardService = inject(ExperimentsDashboardService); + private expStudioService = inject(ExperimentStudioService); + private pdfExport = inject(PdfExportService); + private csvExport = inject(CsvExportService); + private router = inject(Router); + private labelService = inject(ExperimentLabelService); + selectedExperiment = input<Experiment | null>(null); - @Output() run = new EventEmitter<string>(); - @Output() deleteExperiment = new EventEmitter<void>(); - @Output() nameUpdated = new EventEmitter<{ id: string; name: string }>(); + readonly run = output<string>(); + readonly deleteExperiment = output<void>(); + readonly nameUpdated = output<{ + id: string; + name: string; +}>(); @ViewChild('resultsCard') resultsCardRef?: ElementRef<HTMLElement>; + private resultRequestId = 0; private resultSignal = signal<any | null>(null); private fullExperimentSignal = signal<BackendExperimentWithResult | null>(null); private loading = signal(false); @@ -40,12 +59,7 @@ export class ExperimentDetailsComponent { // Use input instead of direct injection to keep it consistent with list component currentUserEmail = input<string | null>(null); - isOwner = computed(() => { - const exp = this.selectedExperiment(); - const email = this.currentUserEmail(); - if (!exp?.authorEmail || !email) return false; - return exp.authorEmail === email; - }); + isOwner = computed(() => isExperimentOwner(this.currentUserEmail(), this.selectedExperiment()?.authorEmail)); readonly isShared = signal<boolean>(false); @@ -55,21 +69,18 @@ export class ExperimentDetailsComponent { readonly nameDraft = signal<string>(''); readonly nameSaving = signal<boolean>(false); readonly nameError = signal<string | null>(null); - readonly mipVersion = (window as any).__env?.MIP_VERSION || '9.0.0'; private codeToLabelSignal = signal<Record<string, string>>({}); private enumMapsSignal = signal<EnumMaps>({}); readonly enumMaps = this.enumMapsSignal.asReadonly(); readonly labelMap = this.codeToLabelSignal.asReadonly(); readonly yVar = computed(() => { - const algo = this.fullExperimentSignal()?.algorithm; - const y = (algo as any)?.inputdata?.y; + const y = this.fullExperimentSignal()?.analysis?.algorithm?.y; if (Array.isArray(y)) return y[0] ?? null; return y ?? null; }); readonly xVar = computed(() => { - const algo = this.fullExperimentSignal()?.algorithm; - const x = (algo as any)?.inputdata?.x; + const x = this.fullExperimentSignal()?.analysis?.algorithm?.x; if (Array.isArray(x)) return x[0] ?? null; return x ?? null; }); @@ -88,7 +99,7 @@ export class ExperimentDetailsComponent { }); readonly datasetsWithLabels = computed(() => - this.withLabels(this.selectedExperiment()?.datasets) + withLabels(this.selectedExperiment()?.datasets, this.codeToLabelSignal()) ); algorithmLabel = computed(() => { @@ -98,20 +109,13 @@ export class ExperimentDetailsComponent { return algoConfig?.label || algoCode; }); - constructor( - private dashboardService: ExperimentsDashboardService, - private expStudioService: ExperimentStudioService, - private pdfExport: ResultsPdfExportService, - private router: Router, - private labelService: ExperimentLabelService - ) { + constructor() { // sync isShared with selectedExperiment effect( () => { const exp = this.selectedExperiment(); this.isShared.set(!!exp?.isShared); - }, - { allowSignalWrites: true } + } ); // Load results for selected experiment @@ -127,8 +131,7 @@ export class ExperimentDetailsComponent { } this.fetchResult(exp.id); - }, - { allowSignalWrites: true } + } ); // Load label map for domain (cached by service) @@ -137,8 +140,7 @@ export class ExperimentDetailsComponent { const domain = this.selectedExperiment()?.domain ?? null; this.loadLabels(domain); this.loadEnumMaps(domain); - }, - { allowSignalWrites: true } + } ); effect( @@ -147,8 +149,7 @@ export class ExperimentDetailsComponent { if (!this.isEditingName() && exp?.name) { this.nameDraft.set(exp.name); } - }, - { allowSignalWrites: true } + } ); } @@ -183,7 +184,7 @@ export class ExperimentDetailsComponent { this.loadedEnumDomain.set(domain); } - private showCopyToast(message = 'Link copied to clipboard') { + private showCopyToast(message: string) { this.copyToastMessage.set(message); this.copyToastVisible.set(true); @@ -196,14 +197,17 @@ export class ExperimentDetailsComponent { this.loading.set(true); this.error.set(null); + const requestId = ++this.resultRequestId; this.dashboardService.getExperimentResult(uuid).subscribe({ next: (res) => { + if (requestId !== this.resultRequestId) return; this.fullExperimentSignal.set(res ?? null); const normalized = res?.result ?? res; this.resultSignal.set(normalized); this.loading.set(false); }, error: (err) => { + if (requestId !== this.resultRequestId) return; console.error('Error loading experiment result', err); this.error.set('Failed to load results for this experiment.'); this.loading.set(false); @@ -211,94 +215,169 @@ export class ExperimentDetailsComponent { }); } - private withLabels(codes: string[] | undefined | null) { - const map = this.codeToLabelSignal(); - return (codes ?? []).map((code) => ({ code, label: map[code] ?? code })); - } readonly variablesWithLabels = computed(() => - this.withLabels(this.selectedExperiment()?.variables) + withLabels(this.selectedExperiment()?.variables, this.codeToLabelSignal()) ); readonly covariatesWithLabels = computed(() => - this.withLabels(this.selectedExperiment()?.covariates) + withLabels(this.selectedExperiment()?.covariates, this.codeToLabelSignal()) ); readonly filtersWithLabels = computed(() => - this.withLabels(this.selectedExperiment()?.filters) + withLabels(this.selectedExperiment()?.filters, this.codeToLabelSignal()) ); - /** Enriches PCA results with actual variable labels so the heatmap doesn't fall back to Var1/Var2. */ - readonly enrichedResult = computed(() => { - const result = this.experimentResult(); - if (!result) return result; + readonly filterPreview = computed(() => + formatFilterExpression( + this.fullExperimentSignal()?.analysis?.inputdata?.filters ?? this.selectedExperiment()?.filterLogic, + { labelMap: this.labelMap(), enumMaps: this.enumMaps() } + ) + ); + readonly preprocessingPreview = computed(() => { + const preprocessing = + preprocessingStepsToRecord(this.fullExperimentSignal()?.analysis?.preprocessing) ?? + this.selectedExperiment()?.preprocessing ?? + null; + const summary = this.expStudioService.formatPreprocessingConfig(preprocessing, this.labelMap(), this.enumMaps()); + return summary === 'none' ? '' : summary; + }); - const algo = this.experimentalAlgorithmName(); - if (algo !== 'pca' && algo !== 'pca_with_transformation') return result; + readonly preprocessingEntries = computed(() => { + const preprocessing = + preprocessingStepsToRecord(this.fullExperimentSignal()?.analysis?.preprocessing) ?? + this.selectedExperiment()?.preprocessing ?? + null; + return this.expStudioService.formatPreprocessingEntries(preprocessing, this.labelMap(), this.enumMaps()); + }); - const allNames = [ - ...this.variablesWithLabels(), - ...this.covariatesWithLabels(), - ].map(v => v.label); + readonly authorLine = computed(() => + this.isOwner() ? 'by you' : `by ${this.selectedExperiment()?.author || 'someone else'}` + ); - if (allNames.length > 0) return { ...result, variable_names: allNames }; - return result; + readonly visibilityLine = computed(() => { + if (!this.isOwner()) return 'Shared with you'; + return this.isShared() ? 'Shared' : 'Private'; }); - onExportPdf(): void { - const element = this.resultsCardRef?.nativeElement; - const result = this.experimentResult(); + readonly failedRun = computed(() => (this.selectedExperiment()?.status || '').toLowerCase() === 'error'); + + readonly failureMessage = computed(() => + this.loadError() || 'This run failed before it produced a result.' + ); + + readonly rowsUsed = computed(() => observationCount(this.experimentResult())); + + readonly pendingRun = computed(() => (this.selectedExperiment()?.status || '').toLowerCase() === 'pending'); + + readonly datasetCount = computed(() => pluralize(this.datasetsWithLabels().length, 'dataset')); + + readonly datasetCountLine = computed(() => `${this.domainLabel()} · ${this.datasetCount()}`); + + readonly datasetNames = computed(() => + this.datasetsWithLabels().map((dataset) => dataset.label).join(', ') + ); + + copyFailure(): void { + const message = this.failureMessage(); + navigator.clipboard.writeText(message).then( + () => this.showCopyToast('Error copied'), + () => this.showCopyToast('Could not copy the error'), + ); + } + + readonly runParameters = computed(() => this.fullExperimentSignal()?.analysis?.algorithm?.parameters ?? null); + + readonly hasStoredParameters = computed(() => + Object.values(this.fullExperimentSignal()?.analysis?.algorithm?.parameters ?? {}) + .some((value) => !this.isEmptyParameterValue(value)) + ); + + /** Stored parameters, or the schema defaults when the run saved none. */ + readonly parameterEntries = computed(() => { const fullExperiment = this.fullExperimentSignal(); + const algoName = fullExperiment?.analysis?.algorithm?.name ?? this.experimentalAlgorithmName(); + const schema = this.expStudioService.backendAlgorithms()[algoName]?.configSchema ?? []; + const labelByKey = new Map( + schema.map((field: any) => [String(field.key), String(field.label ?? field.key)]) + ); + const params = this.hasStoredParameters() + ? fullExperiment?.analysis?.algorithm?.parameters ?? {} + : parameterDefaultsFromSchema(schema); + + return Object.entries(params) + .filter(([, value]) => !this.isEmptyParameterValue(value)) + .map(([key, value]) => ({ + key, + label: labelByKey.get(key) ?? prettifyLabel(key), + value: this.formatParameterValue(value, key), + })); + }); - if (!element || !result) { + /** Labels the variables of a PCA result so the heatmap and weights table do not show raw codes. */ + readonly enrichedResult = computed(() => + enrichPcaResult( + this.experimentResult(), + this.experimentalAlgorithmName(), + [...this.variablesWithLabels(), ...this.covariatesWithLabels()], + ) + ); + + onExportPdf(): void { + const element = this.resultsCardRef?.nativeElement; + const payload = this.resultExportPayload(); + if (!element || !payload) { console.warn('No result or element to export'); return; } + this.pdfExport.exportExperimentPdf({ ...payload, chartContainer: element }); + } + + onExportZip(): void { + const payload = this.resultExportPayload(); + if (!payload) { + console.warn('No result to export'); + return; + } + this.csvExport.exportExperimentZip({ + ...payload, + tables: experimentTables(payload.algorithmKey, payload.result), + }).catch((error) => console.error('Result ZIP export failed:', error)); + } + + /** The saved experiment's setup and result, shared by the PDF report and the ZIP export. */ + private resultExportPayload() { + const result = this.experimentResult(); + if (!result) return null; + const fullExperiment = this.fullExperimentSignal(); const baseName = this.selectedExperiment()?.name?.trim() || 'experiment results'; const filename = baseName.replace(/\s+/g, '_'); - const paramData = fullExperiment?.algorithm?.parameters || {}; - const transObj = (paramData as any)?.data_transformation || {}; - const transLines: string[] = []; - const transTypes = ['standardize', 'center', 'exp']; - - transTypes.forEach(type => { - const vars = transObj[type]; - if (Array.isArray(vars) && vars.length > 0) { - vars.forEach(v => { - const label = this.labelMap()[v] || v; - transLines.push(`${label}: ${type}`); - }); - } - }); - const transformations = transLines.length > 0 ? transLines : null; - - const algoCode = fullExperiment?.algorithm?.name ?? this.experimentalAlgorithmName(); + const algoCode = fullExperiment?.analysis?.algorithm?.name ?? this.experimentalAlgorithmName(); const algoConfig = this.expStudioService.backendAlgorithms()[algoCode]; const algoLabel = algoConfig?.label || algoCode; - this.pdfExport.exportExperimentPdf({ + return { filename, details: { experimentName: baseName, createdBy: this.selectedExperiment()?.author ?? null, createdAt: this.selectedExperiment()?.dateCreated ?? null, algorithm: algoLabel, - params: fullExperiment?.algorithm?.parameters ?? null, - preprocessing: 'none', + params: fullExperiment?.analysis?.algorithm?.parameters ?? null, + preprocessing: this.preprocessingPreview() || 'none', domain: this.domainLabel(), datasets: (this.selectedExperiment()?.datasets ?? []).map(code => this.labelMap()[code] || code), variables: this.variablesWithLabels().map((v) => v.label), covariates: this.covariatesWithLabels().map((c) => c.label), - filters: this.filtersWithLabels().map((f) => f.label), - transformations, - mipVersion: this.mipVersion, + filters: this.filterPreview() ? [this.filterPreview()] : this.filtersWithLabels().map((f) => f.label), + transformations: null, + mipVersion: this.selectedExperiment()?.mipVersion ?? fullExperiment?.mipVersion ?? null, }, - algorithmKey: fullExperiment?.algorithm?.name ?? this.experimentalAlgorithmName(), + algorithmKey: algoCode, result, - chartContainer: element, - }); + }; } runExperiment() { @@ -358,20 +437,7 @@ export class ExperimentDetailsComponent { const exp = this.selectedExperiment(); if (!exp) return; - const url = this.buildShareUrl(exp.id); - - if (navigator.clipboard?.writeText) { - navigator.clipboard.writeText(url).then( - () => this.showCopyToast('Link copied to clipboard'), - (err) => { - console.warn('Failed to copy share URL:', err); - this.showCopyToast('Could not copy link — check console.'); - } - ); - } else { - console.warn('Clipboard API not available, share URL:', url); - this.showCopyToast('Clipboard not available — check console log.'); - } + copyShareUrl(buildExperimentShareUrl(this.router, exp.id)).then((message) => this.showCopyToast(message)); } onToggleShare(): void { @@ -389,28 +455,85 @@ export class ExperimentDetailsComponent { this.dashboardService.toggleExperimentShare(exp.id, newShared).subscribe({ next: () => { this.isShared.set(newShared); - // Toast message update - const msg = newShared ? 'Experiment is now shared' : 'Experiment is no longer shared'; - this.showCopyToast(msg); + this.showCopyToast(shareToggleToast(newShared)); }, error: (err) => { console.error('Failed to toggle share:', err); - this.showCopyToast('Failed to update share state'); + this.showCopyToast(SHARE_TOAST.toggleFailed); }, }); } - private buildShareUrl(expId: string): string { - const tree = this.router.createUrlTree(['/experiments-dashboard'], { - queryParams: { experiment: expId }, - }); - - const relative = this.router.serializeUrl(tree); - const origin = window.location.origin; - return origin + relative; - } onDelete() { this.deleteExperiment.emit(); } + + private isEmptyParameterValue(value: unknown): boolean { + if (value === null || value === undefined || value === '') return true; + if (Array.isArray(value)) return value.length === 0; + if (typeof value === 'object') return Object.keys(value as Record<string, unknown>).length === 0; + return false; + } + + private formatParameterValue(value: unknown, parameterKey?: string): string { + if (Array.isArray(value)) { + return value.map((item) => this.formatParameterValue(item, parameterKey)).join(', '); + } + + if (value && typeof value === 'object') { + return Object.entries(value as Record<string, unknown>) + .filter(([, nestedValue]) => !this.isEmptyParameterValue(nestedValue)) + .map(([key, nestedValue]) => `${prettifyLabel(key)}: ${this.formatParameterValue(nestedValue, key)}`) + .join('; '); + } + + if (typeof value === 'boolean') return value ? 'Yes' : 'No'; + return this.formatParameterScalarValue(value, parameterKey); + } + + private formatParameterScalarValue(value: unknown, parameterKey?: string): string { + const raw = String(value); + const label = this.resolveParameterEnumLabel(raw, parameterKey); + return label || raw; + } + + private resolveParameterEnumLabel(value: string, parameterKey?: string): string | null { + const enumMaps = this.enumMaps(); + const schemaField = this.parameterSchemaField(parameterKey); + const enumSource = Array.isArray(schemaField?.enumSource) ? schemaField.enumSource : []; + + if (enumSource.includes('x') || parameterKey === 'groupA' || parameterKey === 'groupB') { + const targetMap = enumMaps[this.xVar() ?? '']; + if (targetMap?.[value]) return targetMap[value]; + } + + if (parameterKey === 'positive_class') { + const eventVar = this.fullExperimentSignal()?.analysis?.algorithm?.parameters?.['event_var']; + if (typeof eventVar === 'string' && enumMaps[eventVar]?.[value]) { + return enumMaps[eventVar][value]; + } + } + + if (enumSource.includes('y') || parameterKey === 'positive_class' || parameterKey === 'category_order') { + const targetMap = enumMaps[this.yVar() ?? '']; + if (targetMap?.[value]) return targetMap[value]; + } + + const matches = Object.values(enumMaps) + .map((map) => map[value]) + .filter((label): label is string => !!label); + + const unique = Array.from(new Set(matches)); + return unique.length === 1 ? unique[0] : null; + } + + private parameterSchemaField(parameterKey?: string): any | null { + if (!parameterKey) return null; + const fullExperiment = this.fullExperimentSignal(); + const algoName = fullExperiment?.analysis?.algorithm?.name ?? this.experimentalAlgorithmName(); + const schema = this.expStudioService.backendAlgorithms()[algoName]?.configSchema ?? []; + return schema.find((field: any) => String(field.key) === parameterKey) ?? null; + } + } diff --git a/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.css b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.css new file mode 100644 index 000000000..fb6b0223d --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.css @@ -0,0 +1,771 @@ +/* Centre-pane canvas for one analysis set. The member rows carry the same recipe as the + experiment rows they came from (surface, radius, status pill, meta line) so the two lists + read as one product; only the header is new. */ +.folder-canvas { + display: flex; + flex-direction: column; + gap: 20px; + padding: 4px 4px 8px; +} + +/* DnD: the canvas is the drop target for a run dragged out of the list. The ring is an outline + on the member stack rather than a box around the canvas — the centre pane scrolls, so a ring + drawn past the canvas edge would clip or summon scrollbars mid-drag. */ +.folder-canvas--receiving { + background: rgba(43, 51, 233, 0.03); +} + +/* THE SIGNATURE, spent here rather than on a chip: the header wears a card with one slab + stacked behind it, because an analysis set is a stack of runs. At 28px a layered edge is + noise; at card height it is the one thing a user will remember about this view. */ +.folder-canvas-header { + position: relative; + z-index: 0; + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 16px; + flex-wrap: wrap; + padding: 16px 18px; + background: var(--studio-card-header-bg); + border: 1px solid var(--studio-card-header-border); + border-radius: var(--radius-lg); + box-shadow: var(--studio-card-shadow); +} + +.folder-canvas-header::after { + content: ""; + position: absolute; + z-index: -1; + left: 18px; + right: 18px; + bottom: -7px; + height: 8px; + background: color-mix(in srgb, var(--primary-color) 8%, var(--card-bg)); + border: 1px solid var(--studio-card-header-border); + border-top: none; + border-radius: 0 0 var(--radius-lg) var(--radius-lg); +} + +.folder-heading { + display: flex; + flex-direction: column; + gap: 6px; + min-width: 0; + flex: 1; +} + +.folder-back-btn { + display: inline-flex; + align-items: center; + gap: 6px; + align-self: flex-start; + padding: 4px 10px 4px 6px; + border: 1px solid transparent; + border-radius: 999px; + background: transparent; + color: var(--text-muted); + font-size: 0.75rem; + font-weight: 700; + text-transform: uppercase; + letter-spacing: 0.12em; + cursor: pointer; + transition: all 0.2s ease; +} + +.folder-back-btn i { + font-size: 0.7rem; +} + +.folder-back-btn:hover { + color: var(--primary-color); + border-color: var(--primary-color); + background: rgba(148, 163, 184, 0.1); +} + +.folder-title-row { + display: flex; + align-items: center; + gap: 10px; + min-width: 0; +} + +.folder-title { + margin: 0; + font-size: 1.15rem; + font-weight: 800; + color: var(--text-main); + letter-spacing: -0.01em; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +/* The canvas' second gesture: the set's composition, not only its size. */ +.folder-summary { + margin: 0; + font-size: 0.78rem; + line-height: 1.6; + color: #64748b; + font-variant-numeric: tabular-nums; +} + +.folder-rename-form { + display: flex; + align-items: center; + gap: 8px; + flex-wrap: wrap; +} + +.folder-rename-input { + flex: 1; + min-width: 180px; + max-width: 360px; + padding: 8px 12px; + border-radius: var(--radius-md); + border: 1px solid var(--border-color); + background: var(--card-bg); + color: var(--text-main); + font: inherit; + font-size: 0.9rem; + outline: none; +} + +.folder-rename-input:focus { + border-color: var(--primary-color); + box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.3); +} + +.folder-form-action { + display: inline-flex; + align-items: center; + gap: 6px; + padding: 6px 12px; + border-radius: 8px; + border: 1px solid rgba(148, 163, 184, 0.5); + background: white; + color: #475569; + font-size: 0.78rem; + font-weight: 700; + cursor: pointer; + transition: all 0.2s ease; +} + +.folder-form-action i { + font-size: 0.85rem; +} + +.folder-form-action:hover { + border-color: var(--primary-color); + color: var(--primary-color); +} + +/* The header's Delete is an entry point, not the commit: it reddens on hover the way the + row-level delete icon does, and only the confirm button itself wears the red edge. */ +.folder-form-action.delete:hover { + border-color: #b91c1c; + color: #991b1b; +} + +.folder-form-action.cancel:hover { + border-color: rgba(148, 163, 184, 0.8); + color: var(--text-muted); +} + +.folder-form-action.danger { + border-color: var(--danger); + color: #991b1b; +} + +.folder-form-action.danger:hover { + background: #fee2e2; + border-color: #b91c1c; + color: #991b1b; +} + +.folder-form-error { + margin: 0; + font-size: 0.78rem; + color: #991b1b; +} + +/* A drop reports itself once, then gets out of the way: the count badge alone cannot tell + "added" apart from "that one was already here". */ +.folder-drop-notice { + display: inline-flex; + align-items: center; + gap: 6px; + align-self: flex-start; + margin: 0; + padding: 4px 10px; + border-radius: 999px; + font-size: 0.75rem; + font-weight: 700; + color: #047857; + background: #ecfdf5; + border: 1px solid rgba(16, 185, 129, 0.2); +} + +.folder-drop-notice i { + font-size: 0.7rem; +} + +.folder-drop-notice--duplicate { + color: var(--text-muted); + background: var(--bg-neutral); + border-color: var(--border-color); +} + +.folder-actions { + display: flex; + align-items: center; + gap: 10px; + flex-wrap: wrap; + justify-content: flex-end; +} + +.folder-compare-btn { + display: inline-flex; + align-items: center; + gap: 6px; + padding: 6px 12px; + border-radius: 8px; + font-size: 0.78rem; + font-weight: 700; + color: #ffffff; + background: var(--primary-color); + border: 1px solid var(--primary-dark); + box-shadow: 0 4px 10px rgba(43, 51, 233, 0.1); + cursor: pointer; + transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); +} + +.folder-compare-btn:hover:not(:disabled) { + background: var(--primary-dark); + box-shadow: var(--shadow-md); +} + +.folder-compare-btn:disabled { + opacity: 0.45; + cursor: not-allowed; + box-shadow: none; +} + +.folder-delete-confirm { + display: flex; + align-items: center; + gap: 8px; + font-size: 0.78rem; + color: var(--text-muted); +} + +/* MEMBER ROWS */ +.folder-members { + list-style: none; + margin: 0; + padding: 0; + display: flex; + flex-direction: column; + gap: 12px; +} + +.folder-canvas--receiving .folder-members { + outline: 2px dashed var(--primary-color); + outline-offset: 2px; +} + +.member-row { + position: relative; + width: 100%; + box-sizing: border-box; + background: var(--glass-bg); + border: 1px solid var(--glass-border); + border-radius: 16px; + padding: 16px 20px; + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + cursor: pointer; + text-align: left; + box-shadow: var(--glass-shadow); + transition: all 0.2s ease; +} + +.member-row:hover { + border-color: var(--primary-light); + box-shadow: var(--shadow-md); +} + +.member-row--gone { + cursor: default; + background: var(--bg-neutral); + border-style: dashed; +} + +.member-row--gone:hover { + border-color: var(--border-color); + box-shadow: var(--glass-shadow); +} + +/* The row a drop just added lands with one breath of primary, so the pointer's aim is confirmed + where the member actually appeared and not only in the header. */ +.member-row--landed { + animation: memberRowLanded 900ms ease-out; +} + +@keyframes memberRowLanded { + from { + background: #f1f5ff; + border-color: var(--primary-color); + transform: scale(0.99); + } + + to { + background: var(--glass-bg); + border-color: var(--glass-border); + transform: none; + } +} + +/* The canvas' first gesture: the set has an order the paginated list does not, so members + are numbered in the mono slot the dashboard already uses for figures and ids. */ +.member-index { + flex-shrink: 0; + min-width: 20px; + font-family: var(--font-mono); + font-size: 0.72rem; + font-weight: 700; + color: var(--text-muted); + font-variant-numeric: tabular-nums; +} + +.row-main { + flex: 1; + min-width: 0; +} + +.row-title-line { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; +} + +.member-name { + font-size: 0.95rem; + font-weight: 600; + color: var(--text-main); + letter-spacing: 0.01em; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.member-name--gone { + color: var(--text-muted); + font-weight: 600; +} + +.row-meta-line { + margin-top: 4px; + display: flex; + align-items: center; + gap: 14px; + font-size: 0.78rem; + color: #64748b; + min-width: 0; +} + +.meta-item { + display: inline-flex; + align-items: center; + gap: 4px; +} + +.meta-item i { + font-size: 0.8rem; +} + +.algo-item { + opacity: 0.85; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.member-skeleton { + display: block; + border-radius: 999px; + background: linear-gradient(90deg, #eef2f7 25%, #e2e8f0 37%, #eef2f7 63%); + background-size: 400% 100%; + animation: memberSkeletonShimmer 1.4s ease-in-out infinite; +} + +.member-skeleton--title { + height: 14px; + width: 45%; +} + +.member-skeleton--meta { + height: 10px; + width: 30%; +} + +@keyframes memberSkeletonShimmer { + from { + background-position: 100% 50%; + } + + to { + background-position: 0 50%; + } +} + +/* Same icon button as the experiment rows, so an action weighs the same in both panes. */ +.icon-btn { + border: 1px solid transparent; + background: transparent; + border-radius: 8px; + width: 32px; + height: 32px; + display: grid; + place-items: center; + cursor: pointer; + font-size: 0.85rem; + color: var(--text-muted); + transition: all 0.2s ease; + flex-shrink: 0; +} + +.icon-btn:hover { + background: #f1f5f9; + color: var(--primary-color); + border-color: var(--primary-color); +} + +/* The open trigger stays lit while its menu is, the way the list row's does. */ +.icon-btn.active { + background: #f1f5ff; + color: var(--primary-color); + border-color: var(--primary-color); +} + +.icon-btn.danger:hover { + background: #fee2e2; + color: #b91c1c; +} + +.row-actions { + display: flex; + align-items: center; + gap: 4px; + flex-shrink: 0; +} + +/* SET HEADINGS */ +/* The canvas partitions its members the way the compare workspace will section them, so it reads + as one order wearing headings — not a card around a card. The heading is a hairline over a + small caps label plus the one count recipe; the rows below keep their own surfaces untouched. */ +.folder-groups { + display: flex; + flex-direction: column; + gap: 18px; +} + +.member-group { + display: flex; + flex-direction: column; + gap: 10px; + min-width: 0; +} + +.member-group-header { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; + padding-bottom: 6px; + border-bottom: 1px solid var(--border-color); +} + +.member-group-toggle { + flex-shrink: 0; + width: 24px; + height: 24px; + display: grid; + place-items: center; + border: 1px solid transparent; + border-radius: 8px; + background: transparent; + color: var(--text-muted); + font-size: 0.75rem; + cursor: pointer; + transition: all 0.2s ease; +} + +.member-group-toggle:hover { + background: #f1f5f9; + color: var(--primary-color); + border-color: var(--primary-color); +} + +.member-group-name { + min-width: 0; + margin: 0; + font-size: 0.78rem; + font-weight: 800; + text-transform: uppercase; + letter-spacing: 0.08em; + color: var(--text-main); + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +/* Folded away, the heading is the only thing left, so it goes quiet rather than bold. */ +.member-group--collapsed .member-group-name { + color: var(--text-muted); +} + +.member-group-actions { + display: flex; + align-items: center; + gap: 2px; + margin-left: auto; + flex-shrink: 0; +} + +.member-group-confirm { + display: flex; + align-items: center; + gap: 8px; + margin-left: auto; + font-size: 0.72rem; + color: var(--text-muted); +} + +.member-group-rename { + display: flex; + align-items: center; + gap: 8px; + flex: 1; + min-width: 0; + flex-wrap: wrap; +} + +.member-group-rename-input { + flex: 1; + min-width: 140px; + max-width: 280px; + padding: 5px 10px; + border-radius: var(--radius-md); + border: 1px solid var(--border-color); + background: var(--card-bg); + color: var(--text-main); + font: inherit; + font-size: 0.8rem; + outline: none; +} + +.member-group-rename-input:focus { + border-color: var(--primary-color); + box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.3); +} + +/* An empty set is a name the user made on purpose, so it keeps its heading and says why it is bare. */ +.member-group-empty { + margin: 0; + padding-left: 32px; + font-size: 0.78rem; + line-height: 1.6; + color: var(--text-muted); +} + +/* MOVE-TO-SET MENU */ +/* House menu surface, hung off the row for the same reason the list row's is: the centre pane + clips horizontally, and a menu anchored to a trigger at the row's right edge loses its left + edge — and its check column — to that clip. */ +.set-menu-anchor { + display: inline-flex; +} + +.set-menu { + position: absolute; + top: calc(100% + 4px); + right: 12px; + z-index: 9999; + display: flex; + flex-direction: column; + min-width: min(230px, calc(100% - 24px)); + max-width: calc(100% - 24px); + padding: 6px 0; + background: var(--card-bg); + border: 1px solid var(--border-color); + border-radius: var(--radius-md); + box-shadow: var(--shadow-lg); + color: var(--text-main); +} + +.set-menu__hint { + margin: 0; + padding: 8px 12px; + font-size: 0.75rem; + line-height: 1.6; + color: var(--text-muted); +} + +.set-menu__item { + display: flex; + align-items: center; + gap: 10px; + width: 100%; + padding: 8px 12px; + background: transparent; + border: none; + font: inherit; + font-size: 0.8rem; + color: var(--text-main); + text-align: left; + cursor: pointer; +} + +.set-menu__item:hover { + background: #f1f5ff; +} + +.set-menu__check { + width: 14px; + flex-shrink: 0; + font-size: 0.7rem; + color: var(--primary-color); +} + +.set-menu__check--off { + color: transparent; +} + +.set-menu__name { + flex: 1; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.set-menu__item--unset { + color: var(--text-muted); +} + +.set-menu__item--new { + border-top: 1px solid var(--border-color); + color: var(--text-muted); + font-weight: 700; +} + +.set-menu__form { + display: flex; + align-items: center; + gap: 6px; + padding: 8px 12px; + border-top: 1px solid var(--border-color); +} + +.set-menu__input { + flex: 1; + min-width: 0; + padding: 6px 8px; + border-radius: var(--radius-sm); + border: 1px solid var(--border-color); + background: var(--card-bg); + color: var(--text-main); + font: inherit; + font-size: 0.78rem; + outline: none; +} + +.set-menu__input:focus { + border-color: var(--primary-color); + box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.3); +} + +.set-menu__form-action { + padding: 6px 10px; + border-radius: 8px; + border: 1px solid rgba(148, 163, 184, 0.5); + background: white; + color: #475569; + font-size: 0.75rem; + font-weight: 700; + cursor: pointer; + transition: all 0.2s ease; +} + +.set-menu__form-action:hover { + border-color: var(--primary-color); + color: var(--primary-color); +} + +.set-menu__error { + margin: 0; + padding: 0 12px 4px; + font-size: 0.72rem; + color: #991b1b; +} + +/* EMPTY STATE */ +.folder-empty { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 8px; + padding: 48px 24px; + text-align: center; + border: 1px dashed var(--border-color); + border-radius: 20px; +} + +.folder-empty-icon { + font-size: 40px; + color: var(--primary-color); + opacity: 0.7; +} + +/* The empty state already wears a dashed ring, so receiving only tightens it to primary. */ +.folder-empty--receiving { + border-color: var(--primary-color); + background: rgba(43, 51, 233, 0.03); +} + +.folder-empty--receiving .folder-empty-icon { + opacity: 1; +} + +.folder-empty h3 { + margin: 8px 0 0; + font-size: 1rem; + font-weight: 700; + color: var(--text-main); +} + +.folder-empty p { + margin: 0; + max-width: 340px; + font-size: 0.85rem; + line-height: 1.6; + color: var(--text-muted); +} + +@media (max-width: 1100px) { + .member-row { + padding: 10px 12px; + } + + .member-group-confirm { + font-size: 0.7rem; + } + + .folder-canvas-header { + gap: 12px; + } +} diff --git a/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.html b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.html new file mode 100644 index 000000000..17696d371 --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.html @@ -0,0 +1,259 @@ +@if (folder(); as activeFolder) { +<div class="folder-canvas" [class.folder-canvas--receiving]="isReceiving()" (dragover)="onDragOver($event)" + (dragleave)="onDragLeave($event)" (drop)="onDrop($event)"> + <header class="folder-canvas-header"> + <div class="folder-heading"> + <button type="button" class="folder-back-btn" (click)="back.emit()" title="Clear folder selection"> + <i class="fas fa-arrow-left" aria-hidden="true"></i> + <span>All</span> + </button> + + @if (!isEditingName()) { + <div class="folder-title-row"> + <h2 class="folder-title" [title]="activeFolder.name">{{ activeFolder.name }}</h2> + <span class="count-badge">{{ memberCount() }}</span> + </div> + } @else { + <div class="folder-rename-form"> + <input class="folder-rename-input" [value]="nameDraft()" (input)="nameDraft.set($any($event.target).value)" + (keydown.enter)="saveRename()" (keydown.escape)="cancelRename()" aria-label="Folder name" /> + <button type="button" class="folder-form-action" [disabled]="busy()" (click)="saveRename()">Save</button> + <button type="button" class="folder-form-action cancel" (click)="cancelRename()">Cancel</button> + </div> + } + + @if (summary(); as summaryLine) { + <p class="folder-summary">{{ summaryLine }}</p> + } + + @if (nameError(); as nameError) { + <p class="folder-form-error">{{ nameError }}</p> + } + + @if (actionError(); as actionError) { + <p class="folder-form-error" role="alert">{{ actionError }}</p> + } + + @if (dropNotice(); as notice) { + <p class="folder-drop-notice" [class.folder-drop-notice--duplicate]="notice.kind === 'duplicate'" role="status" + aria-live="polite"> + <i class="fas fa-arrow-down-to-line" aria-hidden="true"></i>{{ notice.text }} + </p> + } + </div> + + <div class="folder-actions"> + <button type="button" class="folder-compare-btn" (click)="onCompare()" [disabled]="!canCompare()" + [title]="canCompare() ? 'Compare these runs side by side' : 'Add at least two runs to compare'"> + <i class="fas fa-columns" aria-hidden="true"></i> + Compare in workspace + </button> + + @if (!isEditingName()) { + <button type="button" class="folder-form-action" title="Rename folder" (click)="startRename()"> + <i class="far fa-edit" aria-hidden="true"></i> + Rename + </button> + } + + @if (!isConfirmingDelete()) { + <button type="button" class="folder-form-action delete" title="Delete folder" (click)="askDelete()"> + <i class="far fa-trash-alt" aria-hidden="true"></i> + Delete + </button> + } @else { + <div class="folder-delete-confirm"> + <span>Delete folder? Runs stay in your history.</span> + <button type="button" class="folder-form-action danger" [disabled]="busy()" (click)="confirmDelete()">Delete</button> + <button type="button" class="folder-form-action cancel" (click)="cancelDelete()">Cancel</button> + </div> + } + </div> + </header> + + @if (isEmpty()) { + <div class="folder-empty" [class.folder-empty--receiving]="isReceiving()"> + <div class="folder-empty-icon"> + <i class="far fa-folder-open"></i> + </div> + <h3>{{ isReceiving() ? "Drop it here" : "Nothing in this folder yet" }}</h3> + <p> + Drag a run from the list on the left and let go here, or press its folder icon and pick this + folder. Runs you add here can be compared side by side. + </p> + </div> + } @else { + <!-- One heading per set, Ungrouped last: the canvas reads in the order the compare will. --> + <div class="folder-groups"> + @for (group of groups(); track group.id) { + <section class="member-group" [class.member-group--collapsed]="isGroupCollapsed(group)"> + <header class="member-group-header"> + <button type="button" class="member-group-toggle" (click)="toggleGroup(group)" + [attr.aria-expanded]="!isGroupCollapsed(group)" + [title]="isGroupCollapsed(group) ? 'Expand ' + group.name : 'Collapse ' + group.name"> + <i class="fas" [class.fa-chevron-down]="isGroupCollapsed(group)" [class.fa-chevron-up]="!isGroupCollapsed(group)" + aria-hidden="true"></i> + </button> + + @if (renamingSetId() === group.id) { + <div class="member-group-rename"> + <input class="member-group-rename-input" [value]="setRenameDraft()" + (input)="setRenameDraft.set($any($event.target).value)" (keydown.enter)="saveSetRename()" + (keydown.escape)="cancelSetRename()" aria-label="Set name" /> + <button type="button" class="folder-form-action" [disabled]="busy()" (click)="saveSetRename()">Save</button> + <button type="button" class="folder-form-action cancel" (click)="cancelSetRename()">Cancel</button> + </div> + } @else { + <h3 class="member-group-name" [title]="group.name">{{ group.name }}</h3> + <span class="count-badge">{{ group.rows.length }}</span> + } + + @if (renamingSetId() !== group.id && !group.isUngrouped) { + <div class="member-group-actions"> + @if (confirmingSetDeleteId() !== group.id) { + <button type="button" class="icon-btn" title="Rename set" (click)="startSetRename(group)"> + <i class="far fa-edit" aria-hidden="true"></i> + </button> + <button type="button" class="icon-btn danger" title="Delete set" (click)="askSetDelete(group)"> + <i class="far fa-trash-alt" aria-hidden="true"></i> + </button> + } @else { + <div class="member-group-confirm"> + <span>Delete set? Its runs stay in the folder.</span> + <button type="button" class="folder-form-action danger" [disabled]="busy()" (click)="confirmSetDelete()">Delete</button> + <button type="button" class="folder-form-action cancel" (click)="cancelSetDelete()">Cancel</button> + </div> + } + </div> + } + </header> + + @if (renamingSetId() === group.id && setRenameError(); as setRenameError) { + <p class="folder-form-error">{{ setRenameError }}</p> + } + + @if (!isGroupCollapsed(group)) { + @if (!group.rows.length) { + <p class="member-group-empty">Nothing moved here yet — press the set icon on a run to move it in.</p> + } @else { + <ul class="folder-members"> + @for (row of group.rows; track row.id; let position = $index) { + <li class="member-row" [class.member-row--gone]="isUnavailable(row.id)" + [class.member-row--landed]="dropNotice()?.landedId === row.id" (click)="onRowClick(row)"> + <span class="member-index" aria-hidden="true">{{ group.start + position + 1 }}</span> + <div class="row-main"> + <div class="row-title-line"> + @if (row.experiment) { + <span class="member-name" [title]="row.experiment.name"> + {{ row.experiment.name || "Untitled Investigation" }} + </span> + <app-experiment-status [status]="row.experiment.status" [compact]="true" /> + } @else if (isUnavailable(row.id)) { + <span class="member-name member-name--gone">No longer available</span> + } @else if (memberError(row.id); as loadError) { + <span class="member-name member-name--gone">{{ loadError }}</span> + } @else if (isLoading(row)) { + <span class="member-skeleton member-skeleton--title" aria-hidden="true"></span> + } + </div> + + <div class="row-meta-line"> + @if (row.experiment) { + <span class="meta-item"> + <i class="far fa-calendar"></i> + {{ row.experiment.dateCreated | date : "mediumDate" }} + </span> + <span class="meta-item algo-item"> + <i class="fas fa-flask"></i> + {{ getAlgorithmLabel(row.experiment.algorithmName) }} + </span> + } @else if (isUnavailable(row.id)) { + <span class="meta-item">Removed from this folder</span> + } @else if (memberError(row.id)) { + <span class="meta-item">Check your connection and try again.</span> + } @else if (isLoading(row)) { + <span class="member-skeleton member-skeleton--meta" aria-hidden="true"></span> + } + </div> + </div> + + <div class="row-actions"> + @if (!isUnavailable(row.id)) { + <!-- Move to set: the same membership menu the list rows already carry. --> + <span class="set-menu-anchor"> + <button type="button" class="icon-btn" [class.active]="setMenuRowId() === row.id" title="Move to set" + aria-haspopup="menu" [attr.aria-expanded]="setMenuRowId() === row.id" + (click)="toggleSetMenu(row); $event.stopPropagation()"> + <i class="fas fa-object-group" aria-hidden="true"></i> + </button> + </span> + } + + <button type="button" class="icon-btn" + [title]="isUnavailable(row.id) ? 'Dismiss this row' : 'Remove from folder'" + (click)="onRemoveMember(row); $event.stopPropagation()"> + <i class="fas fa-times"></i> + </button> + </div> + + <!-- The panel belongs to the row, not to the trigger: the centre pane clips horizontal + overflow, and a trigger-anchored menu runs off its left edge. --> + @if (setMenuRowId() === row.id) { + <div class="set-menu" cdkMenu (keydown.escape)="closeSetMenu()" + (click)="$event.stopPropagation()"> + @if (activeFolder.sets.length) { + <p class="set-menu__hint">Move to</p> + } + + @for (set of activeFolder.sets; track set.id) { + <button type="button" class="set-menu__item" cdkMenuItem role="menuitemradio" + [attr.aria-checked]="set.id === currentSetId(row)" (cdkMenuItemTriggered)="onSetMenuPick(set, row)"> + <i class="fas fa-check set-menu__check" [class.set-menu__check--off]="set.id !== currentSetId(row)" + aria-hidden="true"></i> + <span class="set-menu__name">{{ set.name }}</span> + <span class="count-badge">{{ set.experimentIds.length }}</span> + </button> + } + + @if (currentSetId(row)) { + <button type="button" class="set-menu__item set-menu__item--unset" cdkMenuItem + (cdkMenuItemTriggered)="onSetMenuUnset(row)"> + <i class="fas fa-ellipsis-h set-menu__check" aria-hidden="true"></i> + <span class="set-menu__name">Leave the set</span> + </button> + } + + @if (setMenuNewOpen()) { + <div class="set-menu__form"> + <input class="set-menu__input" [value]="setMenuDraft()" placeholder="Set name" aria-label="New set name" + (input)="setMenuDraft.set($any($event.target).value)" (keydown.enter)="commitSetMenuNew(row)" /> + <button type="button" class="set-menu__form-action" [disabled]="busy()" + (click)="commitSetMenuNew(row)">Add</button> + </div> + @if (setMenuError(); as setMenuError) { + <p class="set-menu__error">{{ setMenuError }}</p> + } + } @else { + <button type="button" class="set-menu__item set-menu__item--new" cdkMenuItem + (cdkMenuItemTriggered)="openSetMenuNew()"> + <i class="fas fa-plus set-menu__check" aria-hidden="true"></i> + <span class="set-menu__name">New set…</span> + </button> + } + + @if (!activeFolder.sets.length && !setMenuNewOpen()) { + <p class="set-menu__hint">No sets yet. Create one to group these runs.</p> + } + </div> + } + </li> + } + </ul> + } + } + </section> + } + </div> + } +</div> +} diff --git a/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.spec.ts b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.spec.ts new file mode 100644 index 000000000..fe651c9c3 --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.spec.ts @@ -0,0 +1,716 @@ +import type { Mock } from 'vitest'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { HttpErrorResponse } from '@angular/common/http'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { Subject } from 'rxjs'; + +import { EXPERIMENT_DRAG_MIME } from '../../../core/experiment-drag.utils'; +import { Experiment } from '../../../models/experiments-dashboard.model'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; +import { ExperimentFoldersService } from '../../../services/experiment-folders.service'; +import { FakeExperimentFoldersService } from '../experiment-folders.testing'; +import { ExperimentFolderComponent } from './experiment-folder.component'; + +const experiment = (id: string, overrides: Partial<Experiment> = {}): Experiment => ({ + id, + name: `Run ${id}`, + dateCreated: new Date('2026-01-01T00:00:00.000Z'), + status: 'success', + algorithmName: 'mock_anova', + author: 'Marie Curie', + authorEmail: 'marie.curie@chuv.ch', + isShared: false, + ...overrides, +}); + +describe('ExperimentFolderComponent', () => { + let fixture: ComponentFixture<ExperimentFolderComponent>; + let component: ExperimentFolderComponent; + let foldersService: FakeExperimentFoldersService; + let loaded: ReturnType<typeof signal<Experiment[]>>; + let fetchSpy: Mock; + let pending: Subject<Experiment>; + + const root = () => fixture.nativeElement as HTMLElement; + const canvas = () => root().querySelector<HTMLElement>('.folder-canvas')!; + const dataTransferWith = (type: string, value: string) => { + const dataTransfer = new DataTransfer(); + dataTransfer.setData(type, value); + return dataTransfer; + }; + /** Returns the event so a spec can assert whether the target accepted the drag. */ + const dragOverWith = (target: HTMLElement, dataTransfer: DataTransfer) => { + const event = new DragEvent('dragover', { bubbles: true, cancelable: true, dataTransfer }); + target.dispatchEvent(event); + return event; + }; + const dropWith = (target: HTMLElement, dataTransfer: DataTransfer) => { + const event = new DragEvent('drop', { bubbles: true, cancelable: true, dataTransfer }); + target.dispatchEvent(event); + return event; + }; + const runDrag = (experimentId: string) => dataTransferWith(EXPERIMENT_DRAG_MIME, experimentId); + const notice = () => root().querySelector<HTMLElement>('.folder-drop-notice'); + const rowsOf = () => Array.from(root().querySelectorAll<HTMLElement>('.member-row')); + const groupsOf = () => Array.from(root().querySelectorAll<HTMLElement>('.member-group')); + const groupHeaders = () => Array.from(root().querySelectorAll<HTMLElement>('.member-group-header')); + const groupNames = () => groupHeaders().map((header) => header.querySelector('.member-group-name')!.textContent!.trim()); + const rowsIn = (group: HTMLElement) => Array.from(group.querySelectorAll<HTMLElement>('.member-row')); + /** The move-to-set trigger sits in its own popover anchor; Remove stays the row's own action. */ + const setTrigger = (rowIndex: number) => rowsOf()[rowIndex].querySelector<HTMLButtonElement>('.set-menu-anchor > .icon-btn')!; + const removeButton = (rowIndex: number) => rowsOf()[rowIndex].querySelector<HTMLButtonElement>('.row-actions > .icon-btn:last-child')!; + const headerAction = (label: string) => + Array.from(root().querySelectorAll<HTMLButtonElement>('.folder-actions .folder-form-action')) + .find((button) => button.textContent!.trim().includes(label))!; + + /** Seeds the folder the canvas is pointed at — the click paths have their own specs below. */ + const openFolder = (memberIds: string[], name = 'Q3 meta') => { + const folder = foldersService.seedFolder(name, memberIds); + fixture.componentRef.setInput('folderId', folder.id); + return folder; + }; + + beforeEach(async () => { + foldersService = new FakeExperimentFoldersService(); + loaded = signal<Experiment[]>([]); + pending = new Subject<Experiment>(); + fetchSpy = vi.fn().mockReturnValue(pending); + + await TestBed.configureTestingModule({ + imports: [ExperimentFolderComponent], + providers: [ + provideZonelessChangeDetection(), + { provide: ExperimentsDashboardService, useValue: { experiments: loaded, fetchExperimentById: fetchSpy } }, + { provide: ExperimentStudioService, useValue: { backendAlgorithms: signal({}) } }, + { provide: ExperimentFoldersService, useValue: foldersService }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(ExperimentFolderComponent); + component = fixture.componentInstance; + }); + + + + it('lists the members it can resolve from the loaded page', () => { + loaded.set([experiment('a'), experiment('b')]); + openFolder(['a', 'b']); + fixture.detectChanges(); + + expect(root().querySelector('.folder-title')!.textContent.trim()).toBe('Q3 meta'); + expect(root().querySelector('.count-badge')!.textContent.trim()).toBe('2'); + expect(rowsOf().length).toBe(2); + expect(rowsOf()[0].textContent).toContain('Run a'); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it('asks for a member that sits on another page and holds the row open while loading', () => { + loaded.set([experiment('a')]); + openFolder(['a', 'off-page']); + fixture.detectChanges(); + + expect(fetchSpy).toHaveBeenCalledWith('off-page'); + expect(rowsOf()[1].querySelector('.member-skeleton--title')).toBeTruthy(); + + pending.next(experiment('off-page', { name: 'Off page run' })); + fixture.detectChanges(); + + expect(rowsOf()[1].textContent).toContain('Off page run'); + expect(rowsOf()[1].querySelector('.member-skeleton--title')).toBeNull(); + }); + + it('reports a member the backend dropped and prunes it from the folder', () => { + loaded.set([experiment('a')]); + openFolder(['a', 'off-page']); + fixture.detectChanges(); + + pending.error(new HttpErrorResponse({ status: 404 })); + fixture.detectChanges(); + + expect(rowsOf()[1].querySelector('.member-name--gone')!.textContent!.trim()).toBe('No longer available'); + expect(foldersService.folders()[0].experimentIds).toEqual(['a']); + }); + + it('keeps a member on a transient load error instead of pruning it', () => { + loaded.set([experiment('a')]); + openFolder(['a', 'off-page']); + fixture.detectChanges(); + + pending.error(new Error('network down')); + fixture.detectChanges(); + + expect(rowsOf()[1].textContent).toContain('Could not load this run'); + expect(foldersService.folders()[0].experimentIds).toEqual(['a', 'off-page']); + }); + + it('takes a run the backend dropped out of its set as well', () => { + loaded.set([experiment('a')]); + const folder = openFolder(['a', 'off-page']); + const set = foldersService.seedSet(folder.id, 'T-tests', ['off-page'])!; + fixture.detectChanges(); + + pending.error(new HttpErrorResponse({ status: 404 })); + fixture.detectChanges(); + + expect(foldersService.folders()[0].sets[0].id).toBe(set.id); + expect(foldersService.folders()[0].sets[0].experimentIds).toEqual([]); + expect(foldersService.folders()[0].experimentIds).toEqual(['a']); + }); + + it('explains how to add runs to an empty folder', () => { + openFolder([]); + fixture.detectChanges(); + + expect(root().querySelector('.folder-empty')).toBeTruthy(); + expect(rowsOf().length).toBe(0); + }); + + it('numbers the members, so the canvas reads as an ordered set', () => { + loaded.set([experiment('a'), experiment('b')]); + openFolder(['b', 'a']); + fixture.detectChanges(); + + expect(rowsOf().map((row) => row.querySelector('.member-index')!.textContent!.trim())).toEqual(['1', '2']); + }); + + it('summarises what the set holds', () => { + loaded.set([ + experiment('a', { algorithmName: 'mock_anova', domain: 'cmso' }), + experiment('b', { algorithmName: 'mock_pca', domain: 'cmso' }), + ]); + openFolder(['a', 'b']); + fixture.detectChanges(); + + expect(root().querySelector('.folder-summary')!.textContent!.trim()).toBe('2 algorithms · 1 domain'); + }); + + it('withholds the summary until a member resolves', () => { + openFolder(['off-page']); + fixture.detectChanges(); + + expect(root().querySelector('.folder-summary')).toBeNull(); + + pending.next(experiment('off-page', { algorithmName: 'mock_pca' })); + fixture.detectChanges(); + + expect(root().querySelector('.folder-summary')!.textContent!.trim()).toBe('1 algorithm'); + }); + + it('hands the member ids to the compare workspace in folder order', () => { + loaded.set([experiment('a'), experiment('b')]); + openFolder(['b', 'a']); + fixture.detectChanges(); + + const emitted: string[][] = []; + component.compareRequested.subscribe((ids) => emitted.push(ids)); + (root().querySelector('.folder-compare-btn') as HTMLButtonElement).click(); + + expect(emitted).toEqual([['b', 'a']]); + }); + + it('refuses to compare a single member', () => { + loaded.set([experiment('a')]); + openFolder(['a']); + fixture.detectChanges(); + + expect((root().querySelector('.folder-compare-btn') as HTMLButtonElement).disabled).toBe(true); + }); + + it('renames the folder through the service', () => { + openFolder([]); + fixture.detectChanges(); + + headerAction('Rename').click(); + fixture.detectChanges(); + + const input = root().querySelector('.folder-rename-input') as HTMLInputElement; + input.value = 'ANOVA sensitivity'; + input.dispatchEvent(new Event('input')); + fixture.detectChanges(); + + (root().querySelector('.folder-form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folders()[0].name).toBe('ANOVA sensitivity'); + expect(root().querySelector('.folder-title')!.textContent.trim()).toBe('ANOVA sensitivity'); + }); + + it('commits the rename on Enter', () => { + openFolder([]); + fixture.detectChanges(); + + headerAction('Rename').click(); + fixture.detectChanges(); + + const input = root().querySelector('.folder-rename-input') as HTMLInputElement; + input.value = 'PCA on bids'; + input.dispatchEvent(new Event('input')); + input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); + fixture.detectChanges(); + + expect(foldersService.folders()[0].name).toBe('PCA on bids'); + expect(root().querySelector('.folder-rename-input')).toBeNull(); + }); + + it('asks the server to delete the folder and only leaves the canvas once it has agreed', () => { + loaded.set([experiment('a')]); + const folder = openFolder(['a']); + fixture.detectChanges(); + + const back: number[] = []; + component.back.subscribe(() => back.push(1)); + + headerAction('Delete').click(); + fixture.detectChanges(); + (root().querySelector('.folder-delete-confirm .folder-form-action.danger') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)).toBeNull(); + expect(component.isConfirmingDelete()).toBe(false); + expect(back).toEqual([1]); + }); + + it('holds the canvas, and the confirmation, when the delete never reached the server', () => { + vi.spyOn(console, 'error').mockReturnValue(undefined); + loaded.set([experiment('a')]); + const folder = openFolder(['a']); + foldersService.failWith('deleteFolder'); + fixture.detectChanges(); + + const back: number[] = []; + component.back.subscribe(() => back.push(1)); + + headerAction('Delete').click(); + fixture.detectChanges(); + (root().querySelector('.folder-delete-confirm .folder-form-action.danger') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)).toBeTruthy(); + expect(back).toEqual([]); + expect(root().querySelector('.folder-delete-confirm')).toBeTruthy(); + expect(root().querySelector('.folder-form-error')!.textContent).toContain('Could not delete the folder'); + }); + + it('tells a server that did not answer apart from a name that is taken', () => { + vi.spyOn(console, 'error').mockReturnValue(undefined); + loaded.set([experiment('a')]); + openFolder(['a']); + fixture.detectChanges(); + + headerAction('Rename').click(); + fixture.detectChanges(); + + const input = root().querySelector('.folder-rename-input') as HTMLInputElement; + input.value = 'ANOVA sensitivity'; + input.dispatchEvent(new Event('input')); + foldersService.failWith('renameFolder'); + (root().querySelector('.folder-form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folders()[0].name).toBe('Q3 meta'); + expect(root().querySelector('.folder-rename-input')).toBeTruthy(); + expect(root().querySelector('.folder-form-error')!.textContent).toContain('Could not rename the folder'); + }); + + it('keeps the row and says so when removing the run did not reach the server', () => { + vi.spyOn(console, 'error').mockReturnValue(undefined); + loaded.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + foldersService.failWith('removeExperiment'); + fixture.detectChanges(); + + removeButton(0).click(); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)!.experimentIds).toEqual(['a', 'b']); + expect(rowsOf().length).toBe(2); + expect(root().querySelector('.folder-form-error')!.textContent).toContain('Could not remove that run'); + }); + + it('dismisses a ghost row instead of no-opping against a pruned member', () => { + loaded.set([experiment('a')]); + openFolder(['a', 'off-page']); + fixture.detectChanges(); + + pending.error(new HttpErrorResponse({ status: 404 })); + fixture.detectChanges(); + expect(rowsOf().length).toBe(2); + + removeButton(1).click(); + fixture.detectChanges(); + + expect(rowsOf().length).toBe(1); + expect(foldersService.folders()[0].experimentIds).toEqual(['a']); + }); + + it('removes a member without touching the experiment', () => { + loaded.set([experiment('a'), experiment('b')]); + openFolder(['a', 'b']); + fixture.detectChanges(); + + removeButton(0).click(); + fixture.detectChanges(); + + expect(rowsOf().length).toBe(1); + expect(loaded().length).toBe(2); + }); + + it('takes a run dragged onto the canvas and files it in the folder', () => { + loaded.set([experiment('a'), experiment('c')]); + const folder = openFolder(['a']); + fixture.detectChanges(); + + const dataTransfer = runDrag('c'); + const hover = dragOverWith(canvas(), dataTransfer); + fixture.detectChanges(); + + // Without preventDefault the browser never fires drop, so the ring would be a lie. + expect(hover.defaultPrevented).toBe(true); + expect(canvas().classList.contains('folder-canvas--receiving')).toBe(true); + + dropWith(canvas(), dataTransfer); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)!.experimentIds).toEqual(['a', 'c']); + expect(notice()!.textContent).toContain('Added to Q3 meta'); + expect(canvas().classList.contains('folder-canvas--receiving')).toBe(false); + expect(rowsOf()[1].classList.contains('member-row--landed')).toBe(true); + }); + + it('names a re-drop instead of adding the run twice or removing it', () => { + openFolder(['a']); + fixture.detectChanges(); + + dragOverWith(canvas(), runDrag('a')); + dropWith(canvas(), runDrag('a')); + fixture.detectChanges(); + + expect(foldersService.folders()[0].experimentIds).toEqual(['a']); + expect(notice()!.textContent).toContain('Already in Q3 meta'); + expect(notice()!.classList.contains('folder-drop-notice--duplicate')).toBe(true); + expect(root().querySelector('.member-row--landed')).toBeNull(); + }); + + it('withholds the "added" report from a drop the server refused', () => { + vi.spyOn(console, 'error').mockReturnValue(undefined); + loaded.set([experiment('a'), experiment('c')]); + const folder = openFolder(['a']); + foldersService.failWith('addExperiment'); + fixture.detectChanges(); + + dropWith(canvas(), runDrag('c')); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)!.experimentIds).toEqual(['a']); + expect(notice()).toBeNull(); + expect(root().querySelector('.folder-form-error')!.textContent).toContain('Could not add that run'); + }); + + it('leaves a drag alone that carries something other than a run', () => { + openFolder(['a']); + fixture.detectChanges(); + + const dataTransfer = dataTransferWith('text/plain', 'a'); + const hover = dragOverWith(canvas(), dataTransfer); + fixture.detectChanges(); + expect(hover.defaultPrevented).toBe(false); + expect(canvas().classList.contains('folder-canvas--receiving')).toBe(false); + + dropWith(canvas(), dataTransfer); + fixture.detectChanges(); + expect(foldersService.folders()[0].experimentIds).toEqual(['a']); + expect(notice()).toBeNull(); + }); + + it('tells an empty folder what the hovering run will do to it', () => { + openFolder([]); + fixture.detectChanges(); + expect(root().querySelector('.folder-empty h3')!.textContent!.trim()).toBe('Nothing in this folder yet'); + + dragOverWith(canvas(), runDrag('c')); + fixture.detectChanges(); + + expect(root().querySelector('.folder-empty--receiving')).toBeTruthy(); + expect(root().querySelector('.folder-empty h3')!.textContent!.trim()).toBe('Drop it here'); + }); + + it('fades its own drop report, with the newest drop holding the clock', () => { + vi.useFakeTimers(); + try { + loaded.set([experiment('a'), experiment('c')]); + openFolder(['a']); + fixture.detectChanges(); + + dropWith(canvas(), runDrag('c')); + fixture.detectChanges(); + expect(notice()!.textContent).toContain('Added'); + + dropWith(canvas(), runDrag('c')); + fixture.detectChanges(); + vi.advanceTimersByTime(2100); + expect(notice()!.textContent).toContain('Already in'); + + vi.advanceTimersByTime(200); + fixture.detectChanges(); + expect(notice()).toBeNull(); + } finally { + vi.useRealTimers(); + } + }); + + describe('sets', () => { + it('partitions the members under their set headings, Ungrouped last', () => { + loaded.set([experiment('a'), experiment('b'), experiment('c')]); + const folder = openFolder(['a', 'b', 'c']); + foldersService.seedSet(folder.id, 'T-tests', ['b']); + fixture.detectChanges(); + + expect(groupNames()).toEqual(['T-tests', 'Ungrouped']); + expect(rowsIn(groupsOf()[0]).map((row) => row.querySelector('.member-name')!.textContent!.trim())).toEqual(['Run b']); + expect(rowsIn(groupsOf()[1]).length).toBe(2); + // The set is a heading, not a renumbering: one order runs down the whole canvas. + expect(rowsOf().map((row) => row.querySelector('.member-index')!.textContent!.trim())).toEqual(['1', '2', '3']); + }); + + it('numbers every member once when no set exists yet', () => { + loaded.set([experiment('a'), experiment('b')]); + openFolder(['a', 'b']); + fixture.detectChanges(); + + expect(groupNames()).toEqual(['Ungrouped']); + expect(rowsOf().map((row) => row.querySelector('.member-index')!.textContent!.trim())).toEqual(['1', '2']); + }); + + it('collapses a set heading without losing its count', () => { + loaded.set([experiment('a'), experiment('b'), experiment('c')]); + const folder = openFolder(['a', 'b', 'c']); + foldersService.seedSet(folder.id, 'T-tests', ['b']); + fixture.detectChanges(); + + groupHeaders()[0].querySelector<HTMLButtonElement>('.member-group-toggle')!.click(); + fixture.detectChanges(); + + expect(rowsIn(groupsOf()[0]).length).toBe(0); + expect(groupHeaders()[0].querySelector('.count-badge')!.textContent!.trim()).toBe('1'); + expect(rowsIn(groupsOf()[1]).length).toBe(2); + + groupHeaders()[0].querySelector<HTMLButtonElement>('.member-group-toggle')!.click(); + fixture.detectChanges(); + expect(rowsIn(groupsOf()[0]).length).toBe(1); + }); + + it('withholds rename and delete from the implicit group', () => { + loaded.set([experiment('a')]); + openFolder(['a']); + fixture.detectChanges(); + + expect(groupHeaders()[0].querySelector('.member-group-actions')).toBeNull(); + expect(groupHeaders()[0].querySelector('.member-group-name')!.textContent!.trim()).toBe('Ungrouped'); + }); + + it('moves a run into a set from its row, out of the set that had it', () => { + loaded.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + const ttests = foldersService.seedSet(folder.id, 'T-tests', ['a'])!; + fixture.detectChanges(); + + setTrigger(1).click(); + fixture.detectChanges(); + expect(root().querySelector('.set-menu')).toBeTruthy(); + + root().querySelectorAll<HTMLElement>('.set-menu__item')[0].click(); + fixture.detectChanges(); + + expect(foldersService.setOf(folder.id, 'b')).toBe(ttests.id); + // Strict means b cannot sit in two sets — it left Ungrouped, which is where it was. The run + // T-tests already held keeps its home: a move files a run, it does not evict anyone. + expect(foldersService.folderById(folder.id)!.sets[0].experimentIds).toEqual(['a', 'b']); + expect(rowsIn(groupsOf()[0]).length).toBe(2); + expect(groupsOf().length).toBe(1); + }); + + it('leaves a set for Ungrouped without leaving the folder', () => { + loaded.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + foldersService.seedSet(folder.id, 'T-tests', ['a']); + fixture.detectChanges(); + + setTrigger(0).click(); + fixture.detectChanges(); + (root().querySelector('.set-menu__item--unset') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.setOf(folder.id, 'a')).toBeNull(); + expect(foldersService.folderById(folder.id)!.experimentIds).toEqual(['a', 'b']); + expect(root().querySelector('.set-menu')).toBeNull(); + // An empty set keeps its heading and says what would fill it. + expect(groupNames()).toEqual(['T-tests', 'Ungrouped']); + expect(groupsOf()[0].querySelector('.member-group-empty')).toBeTruthy(); + }); + + it('creates a set around the run whose menu is open', () => { + loaded.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + fixture.detectChanges(); + + setTrigger(0).click(); + fixture.detectChanges(); + (root().querySelector('.set-menu__item--new') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const input = root().querySelector('.set-menu__input') as HTMLInputElement; + input.value = 'Chi-squared'; + input.dispatchEvent(new Event('input')); + (root().querySelector('.set-menu__form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const stored = foldersService.folderById(folder.id)!; + expect(stored.sets.map((set) => set.name)).toEqual(['Chi-squared']); + expect(stored.sets[0].experimentIds).toEqual(['a']); + expect(groupNames()).toEqual(['Chi-squared', 'Ungrouped']); + }); + + it('refuses a set name a sibling already took', () => { + loaded.set([experiment('a')]); + const folder = openFolder(['a']); + foldersService.seedSet(folder.id, 'T-tests'); + fixture.detectChanges(); + + setTrigger(0).click(); + fixture.detectChanges(); + (root().querySelector('.set-menu__item--new') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const input = root().querySelector('.set-menu__input') as HTMLInputElement; + input.value = 't-tests'; + input.dispatchEvent(new Event('input')); + (root().querySelector('.set-menu__form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(root().querySelector('.set-menu__error')!.textContent).toContain('taken'); + expect(foldersService.folderById(folder.id)!.sets.length).toBe(1); + }); + + it('says so when a new set never made it to the server', () => { + vi.spyOn(console, 'error').mockReturnValue(undefined); + loaded.set([experiment('a')]); + const folder = openFolder(['a']); + foldersService.failWith('createSet'); + fixture.detectChanges(); + + setTrigger(0).click(); + fixture.detectChanges(); + (root().querySelector('.set-menu__item--new') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const input = root().querySelector('.set-menu__input') as HTMLInputElement; + input.value = 'Chi-squared'; + input.dispatchEvent(new Event('input')); + (root().querySelector('.set-menu__form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)!.sets).toEqual([]); + expect(root().querySelector('.set-menu__input')).toBeTruthy(); + expect(root().querySelector('.folder-form-error')!.textContent).toContain('Could not create the set'); + }); + + it('renames a set from its heading', () => { + loaded.set([experiment('a')]); + const folder = openFolder(['a']); + foldersService.seedSet(folder.id, 'T-tests', ['a']); + fixture.detectChanges(); + + groupHeaders()[0].querySelector<HTMLButtonElement>('.member-group-actions .icon-btn')!.click(); + fixture.detectChanges(); + + const input = root().querySelector('.member-group-rename-input') as HTMLInputElement; + input.value = 'Age tests'; + input.dispatchEvent(new Event('input')); + input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)!.sets[0].name).toBe('Age tests'); + expect(groupNames()[0]).toBe('Age tests'); + }); + + it('keeps a set name that clashes with a sibling, and says so', () => { + loaded.set([experiment('a')]); + const folder = openFolder(['a']); + foldersService.seedSet(folder.id, 'T-tests', ['a']); + foldersService.seedSet(folder.id, 'Chi-squared'); + fixture.detectChanges(); + + groupHeaders()[0].querySelector<HTMLButtonElement>('.member-group-actions .icon-btn')!.click(); + fixture.detectChanges(); + + const input = root().querySelector('.member-group-rename-input') as HTMLInputElement; + input.value = 'chi-squared'; + input.dispatchEvent(new Event('input')); + input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); + fixture.detectChanges(); + + expect(root().querySelector('.folder-form-error')!.textContent).toContain('already exists'); + expect(foldersService.folderById(folder.id)!.sets[0].name).toBe('T-tests'); + }); + + it('deletes a set and hands its runs back to Ungrouped', () => { + loaded.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + foldersService.seedSet(folder.id, 'T-tests', ['a']); + fixture.detectChanges(); + + groupHeaders()[0].querySelector<HTMLButtonElement>('.member-group-actions .icon-btn.danger')!.click(); + fixture.detectChanges(); + (root().querySelector('.member-group-confirm .folder-form-action.danger') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const stored = foldersService.folderById(folder.id)!; + expect(stored.sets).toEqual([]); + expect(stored.experimentIds).toEqual(['a', 'b']); + expect(groupNames()).toEqual(['Ungrouped']); + expect(rowsOf().length).toBe(2); + }); + + it('aborts a set delete before it happens', () => { + loaded.set([experiment('a')]); + const folder = openFolder(['a']); + foldersService.seedSet(folder.id, 'T-tests', ['a']); + fixture.detectChanges(); + + groupHeaders()[0].querySelector<HTMLButtonElement>('.member-group-actions .icon-btn.danger')!.click(); + fixture.detectChanges(); + root().querySelector<HTMLButtonElement>('.member-group-confirm .folder-form-action.cancel')!.click(); + fixture.detectChanges(); + + expect(foldersService.folderById(folder.id)!.sets.length).toBe(1); + expect(rowsOf().length).toBe(1); + }); + + it('keeps a set assignment out of the detail view', () => { + loaded.set([experiment('a'), experiment('b')]); + openFolder(['a', 'b']); + const openExperiment = vi.spyOn(fixture.componentInstance, 'onRowClick').mockReturnValue(undefined); + fixture.detectChanges(); + + setTrigger(0).click(); + fixture.detectChanges(); + root().querySelector<HTMLElement>('.set-menu__name')!.click(); + fixture.detectChanges(); + + expect(openExperiment).not.toHaveBeenCalled(); + }); + }); + + it('opens the detail view when a member row is clicked', () => { + loaded.set([experiment('a')]); + openFolder(['a']); + fixture.detectChanges(); + + const opened: Experiment[] = []; + component.openExperiment.subscribe((exp) => opened.push(exp)); + rowsOf()[0].click(); + + expect(opened.map((exp) => exp.id)).toEqual(['a']); + }); +}); diff --git a/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.ts b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.ts new file mode 100644 index 000000000..a7ec46d6a --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiment-folder/experiment-folder.component.ts @@ -0,0 +1,635 @@ +import { + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + computed, + effect, + inject, + input, + linkedSignal, + OnDestroy, + output, + Renderer2, + signal, + viewChild, +} from '@angular/core'; +import { CdkMenu, CdkMenuItem } from '@angular/cdk/menu'; +import { Observable } from 'rxjs'; +import { InflightWrites } from '../../../core/inflight-writes'; + +import { Experiment } from '../../../models/experiments-dashboard.model'; +import { ExperimentSet } from '../../../models/experiment-folder.model'; +import { droppedExperimentId, isExperimentDrag, leavesDragZone } from '../../../core/experiment-drag.utils'; +import { isNotFoundError } from '../../../core/http-error.utils'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; +import { ExperimentFoldersService } from '../../../services/experiment-folders.service'; +import { CommonModule } from '@angular/common'; +import { ExperimentStatusComponent } from '../shared/experiment-status/experiment-status.component'; + +/** One row of the canvas: a member id plus whatever we know about it yet. */ +interface FolderMemberRow { + id: string; + experiment: Experiment | null; +} + +/** One heading on the canvas: a named set, or the implicit Ungrouped tail. */ +interface MemberGroup { + id: string; + name: string; + isUngrouped: boolean; + /** Rows listed above this heading, so one number line runs down the whole canvas. */ + start: number; + rows: FolderMemberRow[]; +} + +/** Not a set id — nothing a set can be called, so it can never clash with one. */ +const UNGROUPED_GROUP_ID = 'ungrouped'; +const UNGROUPED_GROUP_NAME = 'Ungrouped'; + +/** One drop report: the copy, whether it changed anything (which drives its tint), and the row it just added. */ +interface DropNotice { + text: string; + kind: 'added' | 'duplicate'; + landedId: string | null; +} + +/** Long enough to read against the row that just appeared, short enough not to linger. */ +const DROP_NOTICE_MS = 2200; + +/** + * The folder canvas: the centre-pane view for a selected analysis set. It is a selection + * helper, not a second detail view — a member click still opens the detail, and the primary + * action hands the member ids to the compare workspace. + * + * Inside the canvas, members are partitioned into named sets: the headings the compare + * workspace later renders as sections. Everything not moved into a set sits under an + * implicit Ungrouped heading, which compare groups by algorithm instead. + */ +@Component({ + selector: 'app-experiment-folder', + templateUrl: './experiment-folder.component.html', + styleUrl: './experiment-folder.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, + imports: [CommonModule, CdkMenu, CdkMenuItem, ExperimentStatusComponent], +}) +export class ExperimentFolderComponent implements OnDestroy { + /** The folder is read through the service, so a member added or pruned elsewhere shows up + * without the parent having to push a fresh copy down. */ + readonly folderId = input<string | null>(null); + + readonly back = output<void>(); + readonly openExperiment = output<Experiment>(); + readonly compareRequested = output<string[]>(); + + private dashboardService = inject(ExperimentsDashboardService); + private foldersService = inject(ExperimentFoldersService); + private expStudio = inject(ExperimentStudioService); + private renderer = inject(Renderer2); + private cdr = inject(ChangeDetectorRef); + + readonly folder = computed(() => this.foldersService.folderById(this.folderId())); + + /** Members outside the loaded page, fetched for display only (never upserted into the list). */ + private fetched = signal<Record<string, Experiment>>({}); + /** Guards the fetch loop only; no template reads it, and `fetched` already re-runs the effect. */ + private readonly inFlight = new Set<string>(); + /** Members the backend stopped returning. The canvas shows one folder, so a folder id resets them. */ + private unavailable = linkedSignal({ + source: () => this.folderId(), + computation: () => [] as string[], + }); + /** Members that failed to load for a reason other than a 404; they stay in the folder. */ + private readonly memberErrors = linkedSignal({ + source: () => this.folderId(), + computation: () => ({} as Record<string, string>), + }); + + readonly isEditingName = signal(false); + readonly nameDraft = signal(''); + readonly nameError = signal<string | null>(null); + readonly isConfirmingDelete = signal(false); + + /** + * What the server refused. The canvas keeps no local copy of a folder, so a write that never + * landed has nowhere else to be admitted — silently leaving the rows as they were would read as + * the click having done nothing at all. + */ + readonly actionError = signal<string | null>(null); + /** Writes on their way to the server; `busy` is what the template disarms buttons with. */ + private readonly writes = new InflightWrites((message) => this.actionError.set(message)); + readonly busy = this.writes.busy; + + /** A run is hovering over the canvas with the folder as its plausible destination. */ + readonly isReceiving = signal(false); + /** What the last drop did, spelled out once: the count badge alone does not say "already in". + * Its `landedId` is the member that just appeared, so its row can land with a flash. */ + readonly dropNotice = signal<DropNotice | null>(null); + private dropNoticeTimer: ReturnType<typeof setTimeout> | null = null; + + constructor() { + effect(() => { + const folder = this.folder(); + if (!folder) return; + + const loaded = new Set(this.dashboardService.experiments().map((exp) => exp.id)); + const cached = this.fetched(); + + const pending = folder.experimentIds.filter( + (id) => !loaded.has(id) && !cached[id] && !this.inFlight.has(id), + ); + if (!pending.length) return; + + pending.forEach((id) => this.inFlight.add(id)); + + const requestFolderId = folder.id; + + pending.forEach((id) => + this.dashboardService.fetchExperimentById(id).subscribe({ + next: (exp) => { + this.inFlight.delete(id); + this.fetched.update((current) => ({ ...current, [exp.id]: exp })); + this.memberErrors.update((current) => { + if (!(id in current)) return current; + const next = { ...current }; + delete next[id]; + return next; + }); + }, + error: (err) => { + this.inFlight.delete(id); + if (isNotFoundError(err)) { + console.warn('[ExperimentFolder] Member no longer available', id, err); + this.markUnavailable(id); + return; + } + if (this.folderId() !== requestFolderId) return; + console.warn('[ExperimentFolder] Member failed to load', id, err); + this.markLoadError(id); + }, + }), + ); + }); + } + + readonly rows = computed<FolderMemberRow[]>(() => { + const folder = this.folder(); + if (!folder) return []; + + const loaded = this.dashboardService.experiments(); + const cached = this.fetched(); + + const members = folder.experimentIds.map((id) => ({ + id, + experiment: loaded.find((exp) => exp.id === id) ?? cached[id] ?? null, + })); + + const ghosts = this.unavailable().map((id) => ({ id, experiment: null })); + + return [...members, ...ghosts]; + }); + + /** + * Sets in folder order first, Ungrouped last — the same order compare sections follow, so + * the canvas is a preview of the workspace rather than a second arrangement of the runs. + * An empty set keeps its heading: it is a name the user made on purpose. An empty Ungrouped + * tail does not, because it is the absence of a name. + */ + readonly groups = computed<MemberGroup[]>(() => { + const folder = this.folder(); + if (!folder) return []; + + const rows = this.rows(); + if (!folder.sets.length) { + return [{ id: UNGROUPED_GROUP_ID, name: UNGROUPED_GROUP_NAME, isUngrouped: true, start: 0, rows }]; + } + + const buckets = new Map<string, FolderMemberRow[]>(folder.sets.map((set) => [set.id, []])); + const ungrouped: FolderMemberRow[] = []; + for (const row of rows) { + const setId = this.currentSetId(row); + const bucket = setId ? buckets.get(setId) : undefined; + if (bucket) bucket.push(row); + else ungrouped.push(row); + } + + // Numbered as they are listed, not as the folder stores them: a heading is a heading, so the + // canvas still reads 1, 2, 3 down the page and compare, which sections the same way, agrees. + let listed = 0; + const heading = (id: string, name: string, isUngrouped: boolean, members: FolderMemberRow[]) => { + const group = { id, name, isUngrouped, start: listed, rows: members }; + listed += members.length; + return group; + }; + + const headings = folder.sets.map((set) => heading(set.id, set.name, false, buckets.get(set.id) ?? [])); + + return ungrouped.length + ? [...headings, heading(UNGROUPED_GROUP_ID, UNGROUPED_GROUP_NAME, true, ungrouped)] + : headings; + }); + + readonly memberCount = computed(() => this.folder()?.experimentIds.length ?? 0); + readonly canCompare = computed(() => this.memberCount() >= 2); + readonly isEmpty = computed(() => this.memberCount() === 0); + + /** + * What the set is made of, as "2 algorithms · 1 domain". Only resolved members count: a + * member still loading or gone has no algorithm or domain to contribute, so the line stays + * out of the way until the canvas knows something true. + */ + readonly summary = computed<string | null>(() => { + const resolved = this.rows() + .map((row) => row.experiment) + .filter((exp): exp is Experiment => !!exp); + if (!resolved.length) return null; + + const algorithms = new Set(resolved.map((exp) => exp.algorithmName).filter(Boolean)); + const domains = new Set(resolved.map((exp) => exp.domain).filter(Boolean)); + const parts = [this.countLabel(algorithms.size, 'algorithm')]; + if (domains.size) parts.push(this.countLabel(domains.size, 'domain')); + + return parts.join(' · '); + }); + + isUnavailable(id: string): boolean { + return this.unavailable().includes(id); + } + + isLoading(row: FolderMemberRow): boolean { + return !row.experiment && !this.isUnavailable(row.id) && !this.memberError(row.id); + } + + memberError(id: string): string | null { + return this.memberErrors()[id] ?? null; + } + + getAlgorithmLabel(code: string | null | undefined): string { + if (!code) return 'Unknown algorithm'; + return this.expStudio.backendAlgorithms()[code]?.label || code; + } + + /** "1 algorithm" / "3 algorithms" — the summary line never says "1 algorithms". */ + private countLabel(count: number, noun: string): string { + return `${count} ${count === 1 ? noun : `${noun}s`}`; + } + + onRowClick(row: FolderMemberRow): void { + if (row.experiment) this.openExperiment.emit(row.experiment); + } + + /** The whole canvas is the target, not only the empty state: adding to a full set is commoner. */ + onDragOver(event: DragEvent): void { + if (!isExperimentDrag(event.dataTransfer)) return; + // The browser only fires `drop` when the drag was accepted here first. + event.preventDefault(); + if (event.dataTransfer) event.dataTransfer.dropEffect = 'copy'; + if (!this.isReceiving()) this.isReceiving.set(true); + } + + onDragLeave(event: DragEvent): void { + if (!leavesDragZone(event, event.currentTarget)) return; + this.isReceiving.set(false); + } + + onDrop(event: DragEvent): void { + this.isReceiving.set(false); + + const folder = this.folder(); + const experimentId = droppedExperimentId(event.dataTransfer); + if (!folder || !experimentId) return; + event.preventDefault(); + + // A drop adds; it never undoes. Re-dropping a member is a misaim, not a request to remove it. + // The mirror answers the duplicate straight away — the server stays the authority either way. + if (this.foldersService.isMember(folder.id, experimentId)) { + this.showDropNotice({ text: `Already in ${folder.name}`, kind: 'duplicate', landedId: null }); + return; + } + + // The notice waits for the answer, so a row that never appeared never arrives wearing "Added". + this.runWrite( + `add:${folder.id}:${experimentId}`, + this.foldersService.addExperiment(folder.id, experimentId), + (updated) => this.showDropNotice({ text: `Added to ${updated.name}`, kind: 'added', landedId: experimentId }), + 'Could not add that run to the folder.', + ); + } + + /** + * The bookend every folder write needs, in `core/inflight-writes`: one request per action in + * flight, a canvas that says it is waiting, and a visible line when the server refuses. A success + * retires the previous refusal before the caller reports what the write did. + */ + private runWrite<T>(dedupeKey: string, request: Observable<T>, onDone: ((value: T) => void) | null, failure: string): void { + this.writes.run(dedupeKey, request, (value) => { + this.actionError.set(null); + onDone?.(value); + }, failure); + } + + ngOnDestroy(): void { + this.clearDropNotice(); + this.unlistenSetMenuClick?.(); + this.unlistenSetMenuClick = null; + } + + onRemoveMember(row: FolderMemberRow): void { + // A ghost row is not a member: its id was pruned the moment it 404'd. Removing it from the + // folder again would no-op and leave the row on screen, so this button dismisses it instead. + if (this.isUnavailable(row.id)) { + this.dismissUnavailable(row.id); + return; + } + + const folder = this.folder(); + if (!folder) return; + this.runWrite( + `remove:${folder.id}:${row.id}`, + this.foldersService.removeExperiment(folder.id, row.id), + null, + 'Could not remove that run from the folder.', + ); + } + + onCompare(): void { + const folder = this.folder(); + if (!folder || folder.experimentIds.length < 2) return; + this.compareRequested.emit([...folder.experimentIds]); + } + + startRename(): void { + this.nameDraft.set(this.folder()?.name ?? ''); + this.nameError.set(null); + this.isEditingName.set(true); + } + + cancelRename(): void { + this.isEditingName.set(false); + this.nameError.set(null); + } + + saveRename(): void { + const folder = this.folder(); + if (!folder) return; + + const draft = this.nameDraft(); + this.runWrite( + `rename:${folder.id}`, + this.foldersService.renameFolder(folder.id, draft), + (renamed) => { + // False is the name rule speaking, and it belongs under the input it was typed into; + // anything else the server said is the general line above. + if (!renamed) { + this.nameError.set(draft.trim() ? 'A folder with that name already exists.' : 'Give the folder a name.'); + return; + } + this.isEditingName.set(false); + this.nameError.set(null); + }, + 'Could not rename the folder. Nothing was changed.', + ); + } + + askDelete(): void { + this.isConfirmingDelete.set(true); + } + + cancelDelete(): void { + this.isConfirmingDelete.set(false); + } + + confirmDelete(): void { + const folder = this.folder(); + if (!folder) return; + + // The way out only opens once the server has the folder gone; leaving the canvas over a + // folder that is still in the strip would leave the two disagreeing. + this.runWrite( + `delete:${folder.id}`, + this.foldersService.deleteFolder(folder.id), + () => { + this.isConfirmingDelete.set(false); + this.back.emit(); + }, + 'Could not delete the folder. It is still there.', + ); + } + + // ---- sets: the headings over the member list ---- + + /** Set ids whose rows are folded away. The implicit group is collapsible too. */ + private readonly collapsedGroups = signal<Record<string, boolean>>({}); + + isGroupCollapsed(group: MemberGroup): boolean { + return this.collapsedGroups()[group.id] ?? false; + } + + toggleGroup(group: MemberGroup): void { + this.collapsedGroups.update((current) => ({ ...current, [group.id]: !this.isGroupCollapsed(group) })); + } + + readonly renamingSetId = signal<string | null>(null); + readonly setRenameDraft = signal(''); + readonly setRenameError = signal<string | null>(null); + readonly confirmingSetDeleteId = signal<string | null>(null); + + startSetRename(group: MemberGroup): void { + this.closeSetMenu(); + this.renamingSetId.set(group.id); + this.setRenameDraft.set(group.name); + this.setRenameError.set(null); + } + + cancelSetRename(): void { + this.renamingSetId.set(null); + this.setRenameError.set(null); + } + + saveSetRename(): void { + const folder = this.folder(); + const setId = this.renamingSetId(); + if (!folder || !setId) return; + + const draft = this.setRenameDraft(); + this.runWrite( + `rename-set:${folder.id}:${setId}`, + this.foldersService.renameSet(folder.id, setId, draft), + (renamed) => { + if (!renamed) { + this.setRenameError.set( + draft.trim() ? 'A set with that name already exists.' : 'Give the set a name.', + ); + return; + } + this.cancelSetRename(); + }, + 'Could not rename the set. Nothing was changed.', + ); + } + + askSetDelete(group: MemberGroup): void { + this.confirmingSetDeleteId.set(group.id); + } + + cancelSetDelete(): void { + this.confirmingSetDeleteId.set(null); + } + + /** The name retires, the runs do not: they fall back to Ungrouped, listed where they now sit. */ + confirmSetDelete(): void { + const folder = this.folder(); + const setId = this.confirmingSetDeleteId(); + if (!folder || !setId) return; + + this.runWrite( + `delete-set:${folder.id}:${setId}`, + this.foldersService.deleteSet(folder.id, setId), + () => this.confirmingSetDeleteId.set(null), + 'Could not delete the set. It is still there.', + ); + } + + // ---- per-row move-to-set menu: same recipe as the list row's add-to-folder menu ---- + + readonly setMenuRowId = signal<string | null>(null); + readonly setMenuNewOpen = signal(false); + readonly setMenuDraft = signal(''); + readonly setMenuError = signal<string | null>(null); + + private readonly setMenu = viewChild(CdkMenu); + private unlistenSetMenuClick: (() => void) | null = null; + + /** Which set holds this member right now; null when it sits ungrouped in the folder. */ + currentSetId(row: FolderMemberRow): string | null { + const folder = this.folder(); + return folder ? this.foldersService.setOf(folder.id, row.id) : null; + } + + toggleSetMenu(row: FolderMemberRow): void { + if (this.setMenuRowId() === row.id) { + this.closeSetMenu(); + return; + } + this.openSetMenu(row.id); + } + + private openSetMenu(rowId: string): void { + this.setMenuRowId.set(rowId); + this.setMenuNewOpen.set(false); + this.setMenuDraft.set(''); + this.setMenuError.set(null); + // The menu is an @if, so CdkMenu's roving tabindex only exists after this render. + this.cdr.detectChanges(); + this.setMenu()?.focusFirstItem(); + + this.unlistenSetMenuClick?.(); + this.unlistenSetMenuClick = this.renderer.listen(document, 'pointerdown', (event: PointerEvent) => { + // Every row carries a trigger and only one row carries a panel, so the click is judged by + // what was hit rather than by comparing against the first of either. A trigger decides its + // own fate in its click handler; anything else outside the open menu is a click away. + const target = event.target as HTMLElement | null; + if (target?.closest('.set-menu-anchor, .set-menu')) return; + this.closeSetMenu(); + }); + } + + closeSetMenu(): void { + if (!this.setMenuRowId()) return; + this.setMenuRowId.set(null); + this.setMenuNewOpen.set(false); + this.setMenuError.set(null); + this.unlistenSetMenuClick?.(); + this.unlistenSetMenuClick = null; + } + + /** Move to: the run leaves whatever set held it, and the menu stays open to show the new home. */ + onSetMenuPick(set: ExperimentSet, row: FolderMemberRow): void { + const folder = this.folder(); + if (!folder) return; + this.runWrite( + `move:${folder.id}:${row.id}:${set.id}`, + this.foldersService.moveToSet(folder.id, row.id, set.id), + () => this.setMenuError.set(null), + 'Could not move that run. The sets are unchanged.', + ); + } + + onSetMenuUnset(row: FolderMemberRow): void { + const folder = this.folder(); + if (!folder) return; + this.closeSetMenu(); + this.runWrite( + `unset:${folder.id}:${row.id}`, + this.foldersService.removeFromSets(folder.id, row.id), + null, + 'Could not move that run back to Ungrouped.', + ); + } + + openSetMenuNew(): void { + this.setMenuNewOpen.set(true); + this.setMenuDraft.set(''); + this.setMenuError.set(null); + } + + /** A new set is born with the run inside it — an empty heading is not what was asked for. */ + commitSetMenuNew(row: FolderMemberRow): void { + const folder = this.folder(); + if (!folder) return; + + const draft = this.setMenuDraft(); + this.runWrite( + `create-set:${folder.id}:${draft}`, + this.foldersService.createSet(folder.id, draft, row.id), + (updated) => { + // null is the duplicate (or the blank) the inline line already explains. + if (!updated) { + this.setMenuError.set(draft.trim() ? 'That name is taken.' : 'Give the set a name.'); + return; + } + this.setMenuNewOpen.set(false); + this.setMenuError.set(null); + }, + 'Could not create the set.', + ); + } + + private showDropNotice(notice: DropNotice): void { + this.clearDropNotice(); + this.dropNotice.set(notice); + this.dropNoticeTimer = setTimeout(() => { + this.dropNotice.set(null); + this.dropNoticeTimer = null; + }, DROP_NOTICE_MS); + } + + /** A second drop restarts the countdown rather than letting an older timer cut the newer one short. */ + private clearDropNotice(): void { + if (!this.dropNoticeTimer) return; + clearTimeout(this.dropNoticeTimer); + this.dropNoticeTimer = null; + } + + /** Drop a reported ghost row. Only a member that resolves again can replace it. */ + private dismissUnavailable(id: string): void { + this.unavailable.update((ids) => ids.filter((seen) => seen !== id)); + } + + private markUnavailable(id: string): void { + // A 404 is the one signal that the run really is gone: prune it from every folder, + // then keep the row visible in the folder that was showing it so the count change + // has an explanation instead of a member that quietly disappears. + const wasMember = this.folder()?.experimentIds.includes(id) ?? false; + this.foldersService.pruneExperiment(id); + if (wasMember) { + this.unavailable.update((ids) => (ids.includes(id) ? ids : [...ids, id])); + } + } + + private markLoadError(id: string): void { + // Transient failures are not deletions: keep the member in the folder and show + // the row as failed rather than pruning user data on a network blip. + this.memberErrors.update((current) => ({ ...current, [id]: 'Could not load this run.' })); + } +} diff --git a/src/app/pages/experiments-dashboard/experiment-folders.testing.ts b/src/app/pages/experiments-dashboard/experiment-folders.testing.ts new file mode 100644 index 000000000..529a8f6be --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiment-folders.testing.ts @@ -0,0 +1,300 @@ +import { signal } from '@angular/core'; +import { Observable, of, throwError } from 'rxjs'; + +import { ExperimentFolder, ExperimentSet } from '../../models/experiment-folder.model'; +import { User } from '../../models/user.interface'; +import { ExperimentFoldersService } from '../../services/experiment-folders.service'; + +/** The surface the dashboard, the list, the canvas and the compare workspace call. */ +type FoldersSurface = Pick< + ExperimentFoldersService, + | 'folders' + | 'loading' + | 'loadError' + | 'setActiveUser' + | 'createFolder' + | 'renameFolder' + | 'deleteFolder' + | 'toggleExperiment' + | 'addExperiment' + | 'removeExperiment' + | 'createSet' + | 'renameSet' + | 'deleteSet' + | 'moveToSet' + | 'removeFromSets' + | 'pruneExperiment' + | 'folderById' + | 'isMember' + | 'setOf' +>; + +type FolderMethod = + | 'createFolder' + | 'renameFolder' + | 'deleteFolder' + | 'toggleExperiment' + | 'addExperiment' + | 'removeExperiment' + | 'createSet' + | 'renameSet' + | 'deleteSet' + | 'moveToSet' + | 'removeFromSets'; + +const nameKey = (name: string): string => name.replace(/\s+/g, ' ').trim().toLowerCase(); + +/** + * `FakeExperimentFoldersService` is the folder service as its components see it, minus the HTTP. + * + * `ExperimentFoldersService` speaks to `/services/experiment-folders` and answers every mutation + * with an Observable, so a spec double can keep the synchronous feel the component specs were + * written with: seeding is a plain call (`seedFolder`/`seedSet`), a component-triggered mutation + * answers immediately, and a spec that wants to see the failure path asks for it with `failWith`. + * The wire contract itself — URLs, bodies, the 409 that becomes `null`/`false` — is covered once, + * against `HttpTestingController`, in `experiment-folders.service.spec.ts`. + */ +export class FakeExperimentFoldersService implements FoldersSurface { + readonly folders = signal<ExperimentFolder[]>([]); + readonly loading = signal(false); + readonly loadError = signal<string | null>(null); + + /** Whom the host kept naming, so a spec can see the session was handed over — and handed over once. */ + readonly sessions: Array<string | null> = []; + + private readonly failures = new Map<FolderMethod, unknown>(); + private sequence = 0; + + // ---- test seams ---- + + /** Makes the next `method` the component calls fail, the way a dead backend would. */ + failWith(method: FolderMethod, error: unknown = new Error('the backend is down')): void { + this.failures.set(method, error); + } + + /** Seeds a folder without pretending it was a user gesture. */ + seedFolder(name: string, experimentIds: string[] = []): ExperimentFolder { + const folder: ExperimentFolder = { + id: this.id('folder'), + name: name.replace(/\s+/g, ' ').trim(), + experimentIds: [...experimentIds], + sets: [], + }; + this.folders.update((current) => [...current, folder]); + return folder; + } + + /** Seeds a set, and the members it holds, without going through the canvas menu. */ + seedSet(folderId: string, name: string, experimentIds: string[] = []): ExperimentSet | null { + const folder = this.folderById(folderId); + if (!folder) return null; + + const created: ExperimentSet = { id: this.id('set'), name: name.replace(/\s+/g, ' ').trim(), experimentIds: [...experimentIds] }; + this.folders.update((current) => + current.map((candidate) => + candidate.id === folderId + ? { + ...candidate, + sets: [...candidate.sets, created], + experimentIds: [ + ...candidate.experimentIds, + ...experimentIds.filter((id) => !candidate.experimentIds.includes(id)), + ], + } + : candidate, + ), + ); + return created; + } + + // ---- the surface itself ---- + + setActiveUser(user: User | null): void { + // Recorded, not obeyed: there is nothing to read when the spec is the one seeding the rows. + this.sessions.push(user?.username?.trim() || null); + } + + createFolder(name: string, experimentId?: string): Observable<ExperimentFolder | null> { + const cleanName = name.replace(/\s+/g, ' ').trim(); + if (!cleanName || this.folders().some((folder) => nameKey(folder.name) === nameKey(cleanName))) { + return of(null); + } + + return this.answer('createFolder', () => { + const created = this.seedFolder(cleanName, experimentId ? [experimentId] : []); + return created; + }); + } + + renameFolder(folderId: string, name: string): Observable<boolean> { + const cleanName = name.replace(/\s+/g, ' ').trim(); + if (!cleanName) return of(false); + + return this.answer('renameFolder', () => { + const clash = this.folders().some((folder) => folder.id !== folderId && nameKey(folder.name) === nameKey(cleanName)); + if (clash) return false; + + this.folders.update((current) => + current.map((folder) => (folder.id === folderId ? { ...folder, name: cleanName } : folder)), + ); + return true; + }); + } + + deleteFolder(folderId: string): Observable<void> { + return this.answer('deleteFolder', () => { + this.folders.update((current) => current.filter((folder) => folder.id !== folderId)); + }); + } + + toggleExperiment(folderId: string, experimentId: string): Observable<ExperimentFolder> { + return this.isMember(folderId, experimentId) + ? this.removeExperiment(folderId, experimentId) + : this.addExperiment(folderId, experimentId); + } + + addExperiment(folderId: string, experimentId: string): Observable<ExperimentFolder> { + return this.answer('addExperiment', () => { + const updated = this.map(folderId, (folder) => + folder.experimentIds.includes(experimentId) + ? folder + : { ...folder, experimentIds: [...folder.experimentIds, experimentId] }, + ); + return this.require(folderId, updated); + }); + } + + removeExperiment(folderId: string, experimentId: string): Observable<ExperimentFolder> { + return this.answer('removeExperiment', () => { + const updated = this.map(folderId, (folder) => ({ + ...folder, + experimentIds: folder.experimentIds.filter((id) => id !== experimentId), + sets: this.dropFromSets(folder.sets, experimentId), + })); + return this.require(folderId, updated); + }); + } + + createSet(folderId: string, name: string, experimentId?: string): Observable<ExperimentFolder | null> { + const cleanName = name.replace(/\s+/g, ' ').trim(); + const folder = this.folderById(folderId); + if (!folder || !cleanName || folder.sets.some((set) => nameKey(set.name) === nameKey(cleanName))) { + return of(null); + } + + return this.answer('createSet', () => { + if (experimentId) this.seedSet(folderId, cleanName, [experimentId]); + else this.seedSet(folderId, cleanName); + return this.folderById(folderId); + }); + } + + renameSet(folderId: string, setId: string, name: string): Observable<boolean> { + const cleanName = name.replace(/\s+/g, ' ').trim(); + const folder = this.folderById(folderId); + if (!folder || !cleanName || !folder.sets.some((set) => set.id === setId)) return of(false); + if (folder.sets.some((set) => set.id !== setId && nameKey(set.name) === nameKey(cleanName))) return of(false); + + return this.answer('renameSet', () => { + this.folders.update((current) => + current.map((candidate) => + candidate.id === folderId + ? { ...candidate, sets: candidate.sets.map((set) => (set.id === setId ? { ...set, name: cleanName } : set)) } + : candidate, + ), + ); + return true; + }); + } + + deleteSet(folderId: string, setId: string): Observable<ExperimentFolder> { + return this.answer('deleteSet', () => { + const updated = this.map(folderId, (folder) => ({ ...folder, sets: folder.sets.filter((set) => set.id !== setId) })); + return this.require(folderId, updated); + }); + } + + moveToSet(folderId: string, experimentId: string, setId: string): Observable<ExperimentFolder> { + return this.answer('moveToSet', () => { + const updated = this.map(folderId, (folder) => + folder.sets.some((set) => set.id === setId) + ? { + ...folder, + experimentIds: folder.experimentIds.includes(experimentId) + ? folder.experimentIds + : [...folder.experimentIds, experimentId], + sets: folder.sets.map((set) => + set.id === setId + ? { ...set, experimentIds: [...set.experimentIds.filter((id) => id !== experimentId), experimentId] } + : { ...set, experimentIds: set.experimentIds.filter((id) => id !== experimentId) }, + ), + } + : folder, + ); + return this.require(folderId, updated); + }); + } + + removeFromSets(folderId: string, experimentId: string): Observable<ExperimentFolder> { + return this.answer('removeFromSets', () => { + const updated = this.map(folderId, (folder) => ({ ...folder, sets: this.dropFromSets(folder.sets, experimentId) })); + return this.require(folderId, updated); + }); + } + + pruneExperiment(experimentId: string): void { + if (!experimentId) return; + this.folders.update((current) => + current.map((folder) => ({ + ...folder, + experimentIds: folder.experimentIds.filter((id) => id !== experimentId), + sets: this.dropFromSets(folder.sets, experimentId), + })), + ); + } + + folderById(folderId: string | null): ExperimentFolder | null { + if (!folderId) return null; + return this.folders().find((folder) => folder.id === folderId) ?? null; + } + + isMember(folderId: string, experimentId: string): boolean { + return this.folders().find((folder) => folder.id === folderId)?.experimentIds.includes(experimentId) ?? false; + } + + setOf(folderId: string, experimentId: string): string | null { + return this.folderById(folderId)?.sets.find((set) => set.experimentIds.includes(experimentId))?.id ?? null; + } + + // ---- internals ---- + + /** One answer per call, with the failure a spec asked for taking precedence. */ + private answer<T>(method: FolderMethod, mutate: () => T): Observable<T> { + const failure = this.failures.get(method); + if (failure !== undefined) { + this.failures.delete(method); + return throwError(() => failure); + } + return of(mutate()); + } + + private map(folderId: string, edit: (folder: ExperimentFolder) => ExperimentFolder): ExperimentFolder | null { + const updated = this.folders().map((folder) => (folder.id === folderId ? edit(folder) : folder)); + this.folders.set(updated); + return updated.find((folder) => folder.id === folderId) ?? null; + } + + /** The server always answers a write with the folder; the double says the same even if it never had it. */ + private require(folderId: string, folder: ExperimentFolder | null): ExperimentFolder { + return folder ?? { id: folderId, name: 'Missing folder', experimentIds: [], sets: [] }; + } + + private dropFromSets(sets: ExperimentSet[], experimentId: string): ExperimentSet[] { + return sets.map((set) => ({ ...set, experimentIds: set.experimentIds.filter((id) => id !== experimentId) })); + } + + private id(prefix: 'folder' | 'set'): string { + this.sequence += 1; + return `${prefix}-${this.sequence}`; + } +} diff --git a/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.css b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.css index e46ed3929..3bd8d64a8 100644 --- a/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.css +++ b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.css @@ -1,493 +1,1120 @@ /* ROOT WRAPPER */ .experiments-list-root { + min-height: 0; height: 100%; display: flex; flex-direction: column; - gap: 10px; + /* The dashboard rail this pane lives in is a ~290–370px column at every desktop width — the + viewport says nothing about that, so the fit is asked of the pane itself. */ + container-type: inline-size; + container-name: list-pane; } -/* TOOLBAR (search + filters) */ -.list-toolbar app-experiment-search { - flex: 1; +/* The controls above the list: search, whose runs, which folder, and the open filter panel. */ +.list-head { + flex-shrink: 0; + display: flex; + flex-direction: column; + gap: 10px; + padding: 14px 14px 12px; + border-bottom: 1px solid rgba(43, 51, 233, 0.08); } +/* TOOLBAR (search + date) */ .list-toolbar { display: flex; align-items: center; gap: 8px; } -/* "My experiments" row */ -.filter-row { - margin-top: 6px; - display: flex; - align-items: center; - justify-content: space-between; - font-size: 0.8rem; - color: #64748b; -} - -.filter-label { - display: inline-flex; - align-items: center; - gap: 6px; +.list-toolbar app-experiment-search { + flex: 1 1 auto; + min-width: 0; } -.filter-label i { - font-size: 0.85rem; +.list-toolbar .filter-toggle-btn { + flex-shrink: 0; } -.filter-right { - display: inline-flex; +/* TABS + VIEW CONTROLS */ +.filter-row.experiments-tabs-row { + display: flex; align-items: center; - gap: 10px; + justify-content: space-between; + gap: 8px; + /* One line at every width. The sort control collapsed to an icon precisely so this row stops + breaking in the rail; ≤640px is the deliberate two-row layout and asks for the wrap back. */ + flex-wrap: nowrap; } -.count-label { - font-size: 0.75rem; - color: #94a3b8; - white-space: nowrap; +.experiments-tabs { + /* Words stay intact. The folder select is the control that gives up width. */ + flex: 0 0 auto; + display: grid; + grid-template-columns: 1fr 1fr; + gap: 2px; + background: #f8fafc; + padding: 3px; + border-radius: 10px; + border: 1px solid rgba(43, 51, 233, 0.08); } -/* toggle */ -.toggle-switch { - position: relative; - width: 38px; - height: 20px; - border-radius: 999px; - border: 1px solid rgba(148, 163, 184, 0.9); - background: #f8fafc; - padding: 0; +.tab-btn { + min-width: 0; + border: none; + border-radius: 7px; + height: 28px; + padding: 0 10px; + background: transparent; + color: var(--text-muted); + font: inherit; + font-size: 0.8rem; + font-weight: 600; + line-height: 1.2; cursor: pointer; display: inline-flex; align-items: center; - transition: background 0.15s ease, border-color 0.15s ease; + justify-content: center; + gap: 6px; + white-space: nowrap; + transition: background-color 0.18s ease, color 0.18s ease, box-shadow 0.18s ease; } -.toggle-thumb { - position: relative; - width: 16px; - height: 16px; - border-radius: 999px; - background: #cbd5f5; - box-shadow: 0 1px 4px rgba(15, 23, 42, 0.2); - transform: translateX(2px); - transition: transform 0.15s ease, background 0.15s ease; +.tab-btn i { + font-size: 0.75rem; + flex-shrink: 0; } -.toggle-switch.on { - background: linear-gradient(135deg, var(--primary-color), var(--primary-light)); - border-color: transparent; +.tab-btn__label { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; } -.toggle-switch.on .toggle-thumb { - background: #ecfeff; - transform: translateX(18px); +.tab-btn:hover { + color: var(--text-main); } -.toggle-switch:focus-visible { - outline: 2px solid var(--primary-color); - outline-offset: 2px; +.tab-btn.active { + background: #ffffff; + color: var(--primary-color); + box-shadow: 0 1px 2px rgba(43, 51, 233, 0.12), 0 0 0 1px rgba(43, 51, 233, 0.1); } -/* SCROLLABLE LIST */ -.experiments-list { - list-style: none; - margin: 4px 0 0; - padding: 10px 12px 20px 12px; - display: flex; - flex-direction: column; - gap: 12px; - max-height: 100%; - overflow-y: auto; - overflow-x: visible; +.list-view-controls { + display: inline-flex; + align-items: center; + justify-content: flex-end; + gap: 8px; + margin-left: auto; + min-width: 0; + flex: 1 1 auto; } -/* Discreet scrollbar */ -.experiments-list::-webkit-scrollbar { - width: 6px; +/* The sort control is the row-overflow menu on an icon: same recipe the row actions already use, + only taller so it sits level with the Filters button beside it. */ +.list-view-controls .sort-overflow__trigger { + width: 34px; + height: 36px; + font-size: 0.85rem; } -.experiments-list::-webkit-scrollbar-track { - background: transparent; +/* The check column of the menu — same recipe as the folder menu, so a pick reads the same here. */ +.sort-overflow__check { + width: 14px; + flex-shrink: 0; + font-size: 0.72rem; + color: var(--primary-color); } -.experiments-list::-webkit-scrollbar-thumb { - background: rgba(148, 163, 184, 0.55); - border-radius: 999px; +.sort-overflow__check--off { + color: transparent; } -/* ROW (each experiment) */ -.experiment-row { - width: 100%; - box-sizing: border-box; - background: var(--glass-bg); - backdrop-filter: var(--glass-blur); - -webkit-backdrop-filter: var(--glass-blur); - border: 1px solid var(--glass-border); - border-radius: 16px; - padding: 16px 20px; - display: flex; +.filter-toggle-btn { + position: relative; + display: inline-flex; align-items: center; - justify-content: space-between; - gap: 16px; + justify-content: center; + gap: 6px; + min-width: 36px; + height: 36px; + box-sizing: border-box; + padding: 0 10px; + border: 1px solid rgba(43, 51, 233, 0.16); + border-radius: 8px; + background: #ffffff; + color: var(--primary-color); + font: inherit; + font-size: 0.8rem; + font-weight: 600; cursor: pointer; - text-align: left; - box-shadow: var(--glass-shadow); - position: relative; - z-index: 1; - transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); + white-space: nowrap; + transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease; } -.experiment-row:hover { - background: rgba(255, 255, 255, 0.95); - border-color: var(--primary-light); - transform: translateY(-4px) scale(1.015); - box-shadow: - 0 20px 40px rgba(43, 51, 233, 0.15), - 0 0 0 4px rgba(43, 51, 233, 0.05); - z-index: 10; +.filter-toggle-btn i { + font-size: 0.78rem; + color: var(--primary-color); } -.experiment-row.selected { - background: #f1f5ff; - border-color: var(--primary-color); - box-shadow: var(--shadow-sm); - position: relative; +.filter-toggle-btn:hover, +.filter-toggle-btn.active { + background: #e5ebfa; } -.experiment-row.selected::before { - content: ""; - position: absolute; - left: 0; - top: 14px; - bottom: 14px; - width: 4px; - border-radius: 0 4px 4px 0; - background: var(--primary-color); +/* COUNT LINE */ +.list-summary { + margin: 0; + padding: 0 2px; + font-size: 0.7rem; + line-height: 1.3; + color: var(--text-muted); + font-variant-numeric: tabular-nums; } -.experiment-row.selected:hover { - background: #ecfeff; - border-color: rgba(14, 165, 233, 0.8); +.list-summary strong { + font-weight: 700; + color: var(--text-main); } -/* CONTENT (left side) */ -.row-main { - flex: 1; - min-width: 0; +/* The history-cap note trails the count it qualifies, on its own line. */ +.list-summary-note { + display: block; + text-wrap: pretty; } -.row-title-line { +/* COMPARE SELECTION */ +.compare-selection-bar { + flex-shrink: 0; display: flex; align-items: center; justify-content: space-between; gap: 8px; + padding: 10px 12px 10px 14px; + border-top: 1px solid rgba(43, 51, 233, 0.14); + border-radius: 0 0 var(--radius-lg, 12px) var(--radius-lg, 12px); + background: #f4f5fe; } -.exp-name { - font-size: 0.95rem; - font-weight: 600; - color: #020617; - letter-spacing: 0.01em; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; +.compare-selection-cap { + font-weight: 500; + color: var(--text-muted); } -/* STATUS PILL */ -.status-pill { - font-size: 0.68rem; +.compare-selection-count { + font-size: 0.8rem; font-weight: 700; - text-transform: uppercase; - letter-spacing: 0.06em; - padding: 4px 10px; - border-radius: 999px; - background: #f1f5f9; - color: #475569; - flex-shrink: 0; - border: 1px solid transparent; -} - -.status-pill.completed { - background: #ecfdf5; - color: #10b981; - /* Brand success green */ - border-color: rgba(16, 185, 129, 0.15); + color: var(--primary-color); + font-variant-numeric: tabular-nums; } -.status-pill.completed::before { - content: '\f058'; - font-family: 'Font Awesome 5 Free'; - font-weight: 900; - margin-right: 6px; +.compare-selection-actions { + display: inline-flex; + gap: 6px; } -.status-pill.error { - background: #fff1f2; - color: #e11d48; - border-color: rgba(225, 29, 72, 0.2); +.compare-selection-btn { + display: inline-flex; + align-items: center; + justify-content: center; + gap: 6px; + height: 30px; + padding: 0 12px; + border: 1px solid rgba(43, 51, 233, 0.16); + border-radius: 999px; + background: #ffffff; + color: var(--text-muted); + font: inherit; + font-size: 0.78rem; + font-weight: 600; + cursor: pointer; + transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease; } -.status-pill.error::before { - content: '\f06a'; - font-family: 'Font Awesome 5 Free'; - font-weight: 900; - margin-right: 5px; +.compare-selection-btn:hover:not(:disabled) { + background: #e5ebfa; } -/* META LINE */ -.row-meta-line { - margin-top: 4px; - display: flex; - flex-direction: column; - align-items: flex-start; - gap: 2px; - font-size: 0.78rem; - color: #64748b; - width: 100%; +.compare-selection-btn--go { + padding: 0 14px; + border: none; + background: var(--primary-color); + color: #ffffff; } -.meta-item i { - margin-right: 4px; - font-size: 0.8rem; +.compare-selection-btn--go:hover:not(:disabled) { + background: var(--primary-dark); } -.algo-item { - opacity: 0.85; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; - display: block; +.compare-selection-btn--go:disabled { + background: #94a3b8; + cursor: not-allowed; } -.meta-description { - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; +/* ADVANCED FILTERS */ +.advanced-filters { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); + gap: 12px; + padding: 14px; + border: 1px solid var(--border-color); + border-radius: var(--radius-lg, 12px); + background: var(--card-bg); + box-shadow: var(--studio-card-shadow); } -/* ACTION ICONS (right side) */ -.row-actions { +.filter-field { display: flex; - align-items: center; - gap: 4px; - flex-shrink: 0; + flex-direction: column; + gap: 3px; + min-width: 0; } -.icon-btn { - border: 1px solid transparent; - background: transparent; - border-radius: 8px; - width: 32px; - height: 32px; - display: grid; - place-items: center; - cursor: pointer; - font-size: 0.85rem; +.filter-label { + font-size: 0.68rem; + font-weight: 700; + letter-spacing: 0.06em; + text-transform: uppercase; color: var(--text-muted); - transition: all 0.2s ease; } -.icon-btn:hover { - background: #f1f5f9; - color: var(--primary-color); - border-color: var(--primary-color); +.filter-field input, +.filter-field select { + appearance: none; + -webkit-appearance: none; + width: 100%; + min-width: 0; + min-height: 36px; + box-sizing: border-box; + border: 1px solid rgba(43, 51, 233, 0.16); + border-radius: var(--radius-md, 8px); + background-color: rgba(255, 255, 255, 0.72); + color: var(--text-main); + padding: 8px 11px; + font: inherit; + font-size: 0.82rem; + line-height: 1.35; + outline: none; + box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8); + transition: background-color 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease; } -.icon-btn.danger:hover { - background: #fee2e2; - color: #b91c1c; +.filter-field select { + padding-right: 32px; + background-image: + linear-gradient(45deg, transparent 50%, var(--primary-color) 50%), + linear-gradient(135deg, var(--primary-color) 50%, transparent 50%); + background-position: + calc(100% - 17px) 50%, + calc(100% - 12px) 50%; + background-size: 5px 5px, 5px 5px; + background-repeat: no-repeat; } -/* EMPTY STATE */ -.empty-state { - flex: 1; - border-radius: var(--radius-lg); - padding: 32px; - background: var(--bg-neutral); - display: flex; - flex-direction: column; - justify-content: center; - align-items: center; - /* Center for more polished look */ - text-align: center; - border: 1px solid var(--border-color); +.filter-field input:hover, +.filter-field select:hover { + border-color: rgba(43, 51, 233, 0.28); + background-color: rgba(255, 255, 255, 0.9); } -.empty-state h3 { - margin: 0 0 6px; - font-size: 0.95rem; - font-weight: 600; - color: #0f172a; +.filter-field input:focus, +.filter-field select:focus { + border-color: var(--primary-color); + background-color: #ffffff; + box-shadow: + 0 0 0 3px rgba(127, 156, 232, 0.24), + 0 6px 14px rgba(43, 51, 233, 0.06); } -.empty-state p { - margin: 0; +.filter-clear-btn { + align-self: end; + display: inline-flex; + align-items: center; + justify-content: center; + padding: 8px 14px; + border: 1px solid rgba(127, 156, 232, 0.38); + border-radius: 999px; + background: rgba(255, 255, 255, 0.96); + color: var(--text-muted); + font: inherit; font-size: 0.8rem; - color: #475569; + font-weight: 600; + cursor: pointer; + transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease; } -/* pagination bar */ -.list-pagination { - display: flex; - align-items: center; - justify-content: space-between; - gap: 8px; - margin: 4px 0; - font-size: 0.78rem; - color: #64748b; +.filter-clear-btn:hover { + border-color: rgba(43, 51, 233, 0.45); + background: rgba(43, 51, 233, 0.06); + color: var(--primary-color); } -.page-info { - flex: 1; - text-align: center; +/* FOLDER STRIP */ +.folder-strip { + display: flex; + flex-direction: column; + gap: 5px; + margin-top: 2px; } -.page-btn { - border-radius: 8px; - border: 1px solid rgba(148, 163, 184, 0.25); - background: rgba(255, 255, 255, 0.5); - backdrop-filter: blur(4px); - padding: 6px 14px; - font-size: 0.78rem; +.folder-chip { + flex-shrink: 0; + border: none; + border-radius: var(--radius-md, 8px); + padding: 8px 16px; + background: transparent; + color: var(--text-muted); + font: inherit; + font-size: 0.85rem; font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; - gap: 6px; - color: #475569; - transition: all 0.2s ease; + gap: 8px; + white-space: nowrap; + transition: background-color 0.18s ease, color 0.18s ease, box-shadow 0.18s ease; } -.page-btn:hover:not(:disabled) { - background: white; - color: var(--primary-color); - border-color: var(--primary-color); - box-shadow: 0 4px 12px rgba(2, 128, 144, 0.1); - transform: translateY(-1px); +.folder-chip i { + font-size: 0.95rem; } -.page-btn:disabled { - opacity: 0.35; - cursor: not-allowed; +.folder-chip:hover { + background: rgba(148, 163, 184, 0.1); + color: var(--text-main); } -/* Responsive */ -@media (max-width: 1100px) { - .experiment-row { - padding: 8px 10px; - } - - .exp-name { - font-size: 0.9rem; - } - - .row-meta-line { - font-size: 0.76rem; - } - - .list-pagination { - font-size: 0.76rem; - } - - .page-btn { - padding: 4px 8px; - } - - .filter-row { - font-size: 0.78rem; - } +.folder-chip--new { + width: 36px; + height: 36px; + box-sizing: border-box; + justify-content: center; + padding: 0; + border: 1px solid rgba(43, 51, 233, 0.16); + background: #ffffff; + color: var(--primary-color); } -.icon-btn.shared { - background: #ecfdf3; - color: #15803d; +.folder-chip--new i { + font-size: 0.8rem; } -.icon-btn.shared:hover { - background: #bbf7d0; - color: #166534; +.folder-chip--new:hover { + background: #e5ebfa; + color: var(--primary-color); } -.experiment-row { +.folder-chip.selected { + background: #ffffff; + color: var(--primary-color); + box-shadow: var(--shadow-sm); +} + +.folder-chip-form { + display: inline-flex; + align-items: center; + gap: 5px; + flex-shrink: 0; +} + +.folder-chip-input { + width: 132px; + min-height: 34px; + box-sizing: border-box; + padding: 7px 10px; + border-radius: var(--radius-md, 8px); + border: 1px solid rgba(43, 51, 233, 0.16); + background: rgba(255, 255, 255, 0.72); + color: var(--text-main); + font: inherit; + font-size: 0.8rem; + outline: none; +} + +.folder-chip-input:focus { + border-color: var(--primary-color); + background: #ffffff; + box-shadow: 0 0 0 3px rgba(127, 156, 232, 0.24); +} + +.folder-chip-form-action, +.folder-menu__form-action { + padding: 6px 11px; + border-radius: var(--radius-md, 8px); + border: 1px solid rgba(127, 156, 232, 0.38); + background: rgba(255, 255, 255, 0.96); + color: var(--text-muted); + font: inherit; + font-size: 0.78rem; + font-weight: 600; + cursor: pointer; + transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease; +} + +.folder-chip-form-action:hover, +.folder-menu__form-action:hover { + border-color: rgba(43, 51, 233, 0.45); + background: rgba(43, 51, 233, 0.06); + color: var(--primary-color); +} + +.folder-chip-form-action.cancel:hover { + border-color: rgba(148, 163, 184, 0.8); + color: var(--text-muted); +} + +/* A folder write is a server round-trip now: the button says it is spent while it is in flight. */ +.folder-chip-form-action:disabled, +.folder-menu__form-action:disabled { + opacity: 0.55; + cursor: default; +} + +.folder-strip-error, +.folder-menu__error { + margin: 0; + font-size: 0.7rem; + color: #991b1b; +} + +.folder-strip-hint { + margin: 0; + font-size: 0.7rem; + line-height: 1.45; + color: var(--text-muted); +} + +/* SCROLLABLE LIST */ +.experiments-list { + flex: 1 1 auto; + min-height: 0; + /* Six rows (78px) plus the group header, then the list scrolls. */ + max-height: 516px; + margin: 0; + padding: 0 6px 6px; + display: flex; + flex-direction: column; + overflow-y: auto; + overflow-x: hidden; +} + +.experiment-group { + display: flex; + flex-direction: column; + gap: 2px; +} + +/* Pins while its rows scroll; the next group's header pushes it out. */ +.experiment-group-head { + position: sticky; + top: 0; + z-index: 20; + display: flex; + align-items: center; + justify-content: space-between; + padding: 10px 8px 4px; + background: var(--card-bg); + font-size: 0.6875rem; + font-weight: 700; + letter-spacing: 0.06em; + text-transform: uppercase; + color: var(--text-muted); +} + +.experiment-group-count { + font-weight: 600; + letter-spacing: 0; +} + +.experiments-list::-webkit-scrollbar-track { + background: transparent; +} + +.experiments-list::-webkit-scrollbar-thumb { + background: rgba(148, 163, 184, 0.55); + border-radius: 999px; +} + +/* ROW */ +.experiment-row { position: relative; + width: 100%; + box-sizing: border-box; + background: transparent; + border: none; + border-radius: var(--radius-md, 8px); + padding: 10px 10px 10px 8px; + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 10px; + cursor: pointer; + text-align: left; + z-index: 1; + overflow: visible; + transition: background-color 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease; +} + +.experiment-row:hover { + background: #f7f8fe; + z-index: 10; +} + +.experiment-row:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 1px; +} + +.experiment-row.selected { + background: #f4f5fe; + box-shadow: inset 0 0 0 1px var(--primary-color); +} + +.experiment-row.selected .exp-name { + color: var(--primary-dark); +} + +.experiment-row.experiment-row--dragging, +.experiment-row.experiment-row--dragging:hover { + transform: none; + opacity: 0.55; + background: var(--card-bg); + box-shadow: inset 0 0 0 1px var(--primary-color); + z-index: 1; +} + +.row-check { + flex: 0 0 auto; + width: 18px; + height: 18px; + box-sizing: border-box; + margin: 2px 0 0; + padding: 0; + display: grid; + place-items: center; + border-radius: 5px; + border: 1px solid #94a3b8; + background: #fff; + color: #fff; + font-size: 0.625rem; + cursor: pointer; +} + +.row-check i { + opacity: 0; +} + +.row-check--on { + background: var(--primary-color); + border-color: var(--primary-color); +} + +.row-check--on i { + opacity: 1; +} + +.row-check:disabled { + opacity: 0.45; + cursor: not-allowed; +} + +.folder-select { + flex: 1 1 auto; + width: auto; + max-width: 11rem; + min-width: 4.5rem; + height: 36px; + border: 1px solid rgba(43, 51, 233, 0.16); + border-radius: 8px; + font-weight: 600; + background: var(--card-bg); + color: var(--text-main); + font: inherit; + font-size: 0.8rem; + padding: 0 8px; +} + +/* CONTENT */ +.row-main { + flex: 1 1 auto; + min-width: 0; + display: flex; + flex-direction: column; + gap: 2px; +} + +.row-title-line { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + min-width: 0; +} + +.exp-name { + flex: 1 1 auto; + min-width: 0; + font-size: 0.875rem; + font-weight: 600; + line-height: 1.4; + color: var(--text-main); + letter-spacing: 0.01em; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.row-model-line { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + font-size: 0.78rem; + line-height: 1.4; + color: var(--text-main); +} + +.row-meta-line { + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + font-size: 0.74rem; + line-height: 1.4; + color: var(--text-muted); +} + +/* ROW ACTIONS: the add-to-folder action stays out of sight until the row is pointed at. */ +.experiment-row:not(:hover):not(:focus-within):not(.selected) .row-actions .icon-btn:not(.active) { + opacity: 0; } .row-actions { position: relative; + display: inline-flex; + align-items: center; + gap: 2px; + flex-shrink: 0; overflow: visible; } -.copy-chip { +.icon-btn { + border: 1px solid transparent; + background: transparent; + border-radius: var(--radius-md, 8px); + width: 30px; + height: 30px; + display: inline-grid; + place-items: center; + cursor: pointer; + font-size: 0.8rem; + line-height: 1; + color: var(--text-muted); + transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease; +} + +.icon-btn:hover, +.icon-btn.active { + background: rgba(127, 156, 232, 0.14); + color: var(--primary-color); +} + +.icon-btn.shared { + background: color-mix(in srgb, var(--success) 10%, white); + color: #065f46; +} + +/* ROW OVERFLOW MENU */ +.row-overflow { + position: relative; + display: inline-flex; +} + +.row-overflow > summary { + list-style: none; +} + +.row-overflow > summary::-webkit-details-marker { + display: none; +} + +.row-overflow[open] > summary { + background: rgba(127, 156, 232, 0.14); + color: var(--primary-color); +} + +.row-overflow__menu { position: absolute; - top: calc(100% + 6px); + top: calc(100% + 5px); right: 0; - padding: 4px 10px; + z-index: 30; + min-width: 165px; + padding: 5px 0; + display: flex; + flex-direction: column; + background: var(--card-bg); + border: 1px solid var(--border-color); + border-radius: var(--radius-md, 8px); + box-shadow: var(--shadow-lg); +} + +.row-overflow__item { + width: 100%; + border: none; + background: transparent; + color: var(--text-main); + padding: 8px 12px; + display: inline-flex; + align-items: center; + gap: 10px; + font: inherit; + font-size: 0.8rem; + font-weight: 500; + text-align: left; + cursor: pointer; +} + +.row-overflow__item i { + width: 14px; + color: var(--text-muted); + text-align: center; +} + +.row-overflow__item:hover { + background: color-mix(in srgb, var(--primary-color) 7%, white); + color: var(--primary-color); +} + +.row-overflow__item:hover i { + color: inherit; +} + +/* LOADING SKELETON */ +.list-skeleton { + flex: 1 1 auto; + min-height: 0; + display: flex; + flex-direction: column; + gap: 6px; + padding: 14px; +} + +.skeleton-row { + border: 1px solid var(--border-color); + border-radius: var(--radius-lg, 12px); + padding: 14px 12px; + background: var(--card-bg); + box-shadow: var(--studio-card-shadow); + display: flex; + flex-direction: column; + gap: 8px; +} + +.skeleton-line { + display: block; + height: 10px; border-radius: 999px; - background: var(--primary-color); - color: #ecfeff; - font-size: 0.7rem; - box-shadow: 0 10px 20px rgba(15, 23, 42, 0.25); - white-space: nowrap; - z-index: 9999; + background: linear-gradient(90deg, #eef2f7 0%, #f8fafc 45%, #eef2f7 100%); + background-size: 220% 100%; + animation: skeletonShimmer 1.4s ease-in-out infinite; +} + +.skeleton-line--title { + width: 78%; + height: 12px; } -/* Empty State - Override */ +.skeleton-line--meta { + width: 52%; +} + +.skeleton-line--short { + width: 34%; +} + +@keyframes skeletonShimmer { + from { background-position: 160% 0; } + to { background-position: -60% 0; } +} + +/* EMPTY STATE */ .empty-state { + flex: 1 1 auto; + min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; - padding: 60px 20px; + padding: 32px 20px; text-align: center; - background-color: var(--bg-neutral, #f8fafc); - border-radius: 12px; - border: 1px dashed var(--border-color, #e2e8f0); - margin-top: 20px; - color: #64748b; + background: var(--bg-neutral); + border-radius: var(--radius-lg, 12px); + border: 1px dashed var(--border-color); + margin: 4px 2px; +} + +.empty-icon-wrapper { + margin-bottom: 16px; + position: relative; + display: flex; + justify-content: center; + align-items: center; +} + +.empty-icon-wrapper::after { + content: ""; + position: absolute; + width: 100px; + height: 100px; + background: radial-gradient(circle, rgba(43, 51, 233, 0.08), transparent 70%); + z-index: -1; } .empty-icon { - font-size: 48px; - color: #94a3b8; - margin-bottom: 20px; - opacity: 0.5; + font-size: 42px; + color: var(--primary-color); + opacity: 0.8; } .empty-state h3 { - font-size: 1.25rem; - font-weight: 600; - color: #0f172a; - margin: 0 0 10px 0; + margin: 0 0 8px; + font-size: 1rem; + font-weight: 800; + color: var(--text-main); } .empty-state p { - font-size: 0.95rem; - color: #475569; - max-width: 400px; - line-height: 1.5; - margin: 0 0 25px 0; + margin: 0 0 20px; + font-size: 0.82rem; + line-height: 1.55; + color: var(--text-muted); + max-width: 260px; } .empty-action-btn { - display: inline-flex; + box-shadow: 0 8px 20px rgba(43, 51, 233, 0.18) !important; +} + +/* PAGINATION */ +.list-pagination { + flex-shrink: 0; + display: flex; align-items: center; + justify-content: space-between; gap: 8px; - background-color: var(--primary-color); - color: white; - padding: 10px 20px; - border-radius: 8px; + padding: 10px 14px; + border-top: 1px solid rgba(43, 51, 233, 0.08); + font-size: 0.75rem; + color: var(--text-muted); +} + +.list-pagination .list-summary { + flex: 1 1 auto; + min-width: 0; + margin: 0; + padding: 0; + font-size: inherit; +} + +.list-pagination .list-summary strong { + font-weight: 600; +} + +.page-controls { + flex-shrink: 0; + display: inline-flex; + align-items: center; + gap: 4px; +} + +.page-info { + min-width: 44px; + text-align: center; + font-variant-numeric: tabular-nums; +} + +.page-btn { + width: 28px; + height: 28px; + box-sizing: border-box; + display: grid; + place-items: center; + padding: 0; + border-radius: 999px; + border: 1px solid rgba(43, 51, 233, 0.16); + background: #ffffff; + font-size: 0.69rem; + cursor: pointer; + color: var(--primary-color); + transition: background-color 0.15s ease; +} + +.page-btn:hover:not(:disabled) { + background: #e5ebfa; +} + +.page-btn:disabled { + color: var(--text-muted); + opacity: 0.4; + cursor: not-allowed; +} + +/* FOLDER MENU (anchored in the row) */ +.folder-menu-anchor { + display: inline-flex; +} + +.folder-menu { + position: absolute; + top: calc(100% + 4px); + right: 8px; + z-index: 40; + display: flex; + flex-direction: column; + min-width: min(220px, calc(100% - 16px)); + max-width: calc(100% - 16px); + padding: 5px 0; + background: var(--card-bg); + border: 1px solid var(--border-color); + border-radius: var(--radius-md, 8px); + box-shadow: var(--shadow-lg); + color: var(--text-main); +} + +.folder-menu__item { + display: flex; + align-items: center; + gap: 10px; + width: 100%; + padding: 8px 12px; + background: transparent; + border: none; + font: inherit; + font-size: 0.8rem; font-weight: 500; - text-decoration: none; - transition: opacity 0.2s, transform 0.1s; + color: var(--text-main); + text-align: left; + cursor: pointer; +} + +.folder-menu__item:hover { + background: color-mix(in srgb, var(--primary-color) 7%, white); +} + +.folder-menu__check { + width: 14px; + flex-shrink: 0; + font-size: 0.72rem; + color: var(--primary-color); } -.empty-action-btn:hover { - opacity: 0.9; - transform: translateY(-1px); +.folder-menu__check--off { + color: transparent; } -.empty-action-btn:active { - transform: translateY(0); -} \ No newline at end of file +.folder-menu__name { + flex: 1; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.folder-menu__item--new { + border-top: 1px solid var(--border-color); + color: var(--text-muted); + font-weight: 700; +} + +.folder-menu__form { + display: flex; + align-items: center; + gap: 8px; + padding: 8px 12px; + border-top: 1px solid var(--border-color); +} + +.folder-menu__input { + flex: 1; + min-width: 0; + min-height: 34px; + box-sizing: border-box; + padding: 7px 9px; + border-radius: var(--radius-md, 8px); + border: 1px solid rgba(43, 51, 233, 0.16); + background: rgba(255, 255, 255, 0.72); + color: var(--text-main); + font: inherit; + font-size: 0.8rem; + outline: none; +} + +.folder-menu__input:focus { + border-color: var(--primary-color); + background: #ffffff; + box-shadow: 0 0 0 3px rgba(127, 156, 232, 0.24); +} + +.folder-menu__error { + padding: 0 10px 4px; +} + +.folder-menu__hint { + margin: 0; + padding: 8px 12px; + font-size: 0.78rem; + line-height: 1.45; + color: var(--text-muted); +} + +/* Narrower than the two words plus a usable folder select: keep the icons, drop the labels. + aria-label and title on the buttons still name Mine and Shared. */ +@container list-pane (max-width: 320px) { + .tab-btn__label { + display: none; + } + + .tab-btn { + padding: 0 8px; + } +} + +/* RESPONSIVE */ +@media (max-width: 1100px) { + .advanced-filters { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } +} + +@media (max-width: 640px) { + /* Not a wrap accident: below the phone breakpoint the tabs take the line and the controls + take the next one, both full width. The row is named at the same specificity it is styled + with above, or the base nowrap wins and this layout never happens. */ + .filter-row.experiments-tabs-row { + align-items: stretch; + flex-wrap: wrap; + } + + .experiments-tabs { + width: 100%; + } + + .list-view-controls { + width: 100%; + margin-left: 0; + } + + .filter-toggle-btn { + flex: 1; + max-width: none; + justify-content: center; + } + + .advanced-filters { + grid-template-columns: 1fr; + } + + .filter-clear-btn { + width: 100%; + } +} diff --git a/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.html b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.html index fb71c7fb8..a07b4a5d1 100644 --- a/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.html +++ b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.html @@ -1,145 +1,317 @@ <div class="experiments-list-root"> - <!-- Search bar --> - <div class="list-toolbar"> + <div class="list-head"> + <!-- Row 1: the search the list answers to, and the panel that narrows it. --> + <div class="list-toolbar" data-guide="dashboard-search"> <app-experiment-search [filters]="filters()" (filtersChange)="patchFilters($event)"></app-experiment-search> + + <button type="button" class="filter-toggle-btn" [class.active]="filtersOpen() || hasActiveFilters()" + (click)="toggleFilters()" [attr.aria-expanded]="filtersOpen()" title="Filter"> + <i class="fas fa-sliders-h" aria-hidden="true"></i> + @if (activeFilterCount()) { + <span class="count-badge">{{ activeFilterCount() }}</span> + } + </button> </div> - <!-- My experiments toggle --> - <div class="filter-row"> - <div class="filter-label"> - <i class="far fa-user"></i> - <span>My experiments</span> + <!-- Row 2: whose runs, which folder, and the one write this row of controls owns. The folder is + a select because it is a choice of one, not a chip to press: "All folders" is the clear. --> + <div class="filter-row experiments-tabs-row" data-guide="dashboard-tabs"> + <div class="experiments-tabs"> + <button type="button" class="tab-btn" [class.active]="onlyMine()" (click)="setTab(true)" + aria-label="Mine" title="Mine"> + <i class="far fa-user" aria-hidden="true"></i> + <span class="tab-btn__label">Mine</span> + </button> + <button type="button" class="tab-btn" [class.active]="!onlyMine()" (click)="setTab(false)" + aria-label="Shared" title="Shared"> + <i class="fas fa-users" aria-hidden="true"></i> + <span class="tab-btn__label">Shared</span> + </button> </div> - <div class="filter-right"> - <span class="count-label"> - {{ totalExperiments() }} / {{ experimentsService.globalTotalExperiments() }} experiments - </span> - - <span class="filter-label" style="margin-right: 8px;">all</span> + <div class="list-view-controls"> + <select class="folder-select" aria-label="Folder" [value]="selectedFolderId() ?? 'all'" + (change)="onFolderPick($any($event.target).value)"> + <option value="all" [selected]="(selectedFolderId() ?? 'all') === 'all'">All folders</option> + @for (folder of foldersService.folders(); track folder.id) { + <option [value]="folder.id" [selected]="folder.id === selectedFolderId()">{{ folder.name }} ({{ + folder.experimentIds.length }})</option> + } + </select> - <button type="button" class="toggle-switch" [class.on]="onlyMine()" (click)="toggleOnlyMine()"> - <span class="toggle-thumb"></span> + @if (isCreatingFolder()) { + <span class="folder-chip-form"> + <input class="folder-chip-input" [value]="newFolderDraft()" placeholder="Folder name" + aria-label="New folder name" (input)="newFolderDraft.set($any($event.target).value)" + (keydown.enter)="commitNewFolder()" (keydown.escape)="cancelNewFolder()" /> + <button type="button" class="folder-chip-form-action" [disabled]="folderBusy()" + (click)="commitNewFolder()">Add</button> + <button type="button" class="folder-chip-form-action cancel" (click)="cancelNewFolder()">Cancel</button> + </span> + } @else { + <button type="button" class="folder-chip folder-chip--new" title="New folder" aria-label="New folder" + (click)="startNewFolder()"> + <i class="fas fa-folder-plus" aria-hidden="true"></i> </button> + } </div> </div> - @if (!pagedExperiments().length) { + @if (folderFormError(); as folderFormError) { + <p class="folder-strip-error">{{ folderFormError }}</p> + } @else if (folderWriteError(); as folderWriteError) { + <p class="folder-strip-error">{{ folderWriteError }}</p> + } @else if (foldersService.loadError(); as foldersLoadError) { + <p class="folder-strip-error">{{ foldersLoadError }}</p> + } @else if (!foldersService.folders().length && foldersService.loading()) { + <p class="folder-strip-hint">Reading your folders…</p> + } @else if (!foldersService.folders().length) { + <p class="folder-strip-hint">Group related runs here, then compare them side by side.</p> + } + + @if (filtersOpen()) { + <div class="advanced-filters"> + <!-- Sort re-orders what these filters narrow, so it lives with them and only exists while the + panel is open. The active order is named in the trigger's tooltip. --> + <details class="row-overflow sort-overflow" #sortMenu> + <summary class="icon-btn sort-overflow__trigger" [title]="'Sort: ' + sortLabel()" + aria-label="Sort experiments"> + <i class="fas fa-arrow-down-wide-short" aria-hidden="true"></i> + </summary> + + <div class="row-overflow__menu"> + @for (option of sortOptions; track option.value) { + <button type="button" class="row-overflow__item" (click)="setSort(option.value)"> + <i class="fas fa-check sort-overflow__check" + [class.sort-overflow__check--off]="option.value !== sort()" aria-hidden="true"></i> + <span>{{ option.label }}</span> + </button> + } + </div> + </details> + + <label class="filter-field"> + <span class="filter-label">Algorithm</span> + <select [ngModel]="filters().algorithm" (ngModelChange)="patchFilters({ algorithm: $event || null })"> + <option [ngValue]="null">All algorithms</option> + @for (option of algorithmOptions(); track option.value) { + <option [ngValue]="option.value">{{ option.label }}</option> + } + </select> + </label> + + <label class="filter-field"> + <span class="filter-label">Status</span> + <select [ngModel]="filters().status" (ngModelChange)="patchFilters({ status: $event })"> + <option value="any">Any status</option> + <option value="success">Completed</option> + <option value="error">Failed</option> + </select> + </label> + + <label class="filter-field"> + <span class="filter-label">Visibility</span> + <select [ngModel]="filters().shared" (ngModelChange)="patchFilters({ shared: $event })"> + <option value="any">Any visibility</option> + <option value="shared">Shared</option> + <option value="private">Private</option> + </select> + </label> + + <label class="filter-field"> + <span class="filter-label">Author</span> + <input type="search" [ngModel]="filters().author" (ngModelChange)="patchFilters({ author: $event || null })" + placeholder="Name or email" /> + </label> + + <label class="filter-field"> + <span class="filter-label">Variable</span> + <input type="search" [ngModel]="filters().variable" (ngModelChange)="patchFilters({ variable: $event || null })" + placeholder="Variable code" /> + </label> + + <button type="button" class="filter-clear-btn" (click)="clearFilters()"> + Clear all + </button> + </div> + } + </div> + + @if (isLoading() && !pagedExperiments().length) { + <div class="list-skeleton" aria-label="Loading experiments" aria-busy="true"> + @for (row of [1, 2, 3, 4]; track row) { + <div class="skeleton-row"> + <span class="skeleton-line skeleton-line--title"></span> + <span class="skeleton-line skeleton-line--meta"></span> + <span class="skeleton-line skeleton-line--meta skeleton-line--short"></span> + </div> + } + </div> + } @else if (!pagedExperiments().length) { <div class="empty-state"> - <div class="empty-icon"> - <i class="fas fa-flask"></i> + <div class="empty-icon-wrapper"> + <div class="empty-icon"> + <i class="fas" [class.fa-microscope]="!hasActiveFilters()" [class.fa-filter]="hasActiveFilters()"></i> + </div> </div> + + @if (hasActiveFilters()) { + <h3>No experiments match these filters</h3> + <p> + Try a different search, date range or filter combination. + </p> + <button type="button" class="empty-action-btn primary-action-btn" (click)="clearFilters()"> + <i class="fas fa-times"></i> + Clear filters + </button> + } @else { <h3>No experiments found</h3> <p> Ready to start your research? <br /> - Launch the Studio to create your first experiment. + Explore datasets and create your first investigation. </p> - <a routerLink="/experiment-studio" class="empty-action-btn"> + <a routerLink="/experiment-studio" class="empty-action-btn primary-action-btn"> <i class="fas fa-plus"></i> Create New Experiment </a> + } </div> } @else { - <!-- Pagination top --> - <div class="list-pagination list-pagination--top"> - <button type="button" class="page-btn" (click)="prevPage()" [disabled]="currentPage() === 1"> - ‹ Prev - </button> - - <div class="page-info">Page {{ currentPage() }} of {{ totalPages() }}</div> - - <button type="button" class="page-btn" (click)="nextPage()" [disabled]="currentPage() === totalPages()"> - Next › - </button> - </div> + <div class="experiments-list" role="listbox" aria-label="Experiments"> + <!-- Grouped by day while the list is in date order; the header pins while its rows scroll. --> + @for (group of experimentGroups(); track group.label) { + <div class="experiment-group" role="group" [attr.aria-label]="group.label || null"> + @if (group.label) { + <div class="experiment-group-head" aria-hidden="true"> + <span>{{ group.label }}</span> + <span class="experiment-group-count">{{ group.rows.length }}</span> + </div> + } + @for (exp of group.rows; track exp.id) { + <!-- A row is selected when it is the one the detail pane shows. Compare membership is a + separate check on the left of the row, so the two never borrow each other's highlight. --> + <div class="experiment-row" draggable="true" tabindex="0" role="option" + [attr.aria-selected]="exp.id === selectedExperimentId()" [class.selected]="exp.id === selectedExperimentId()" + [class.experiment-row--dragging]="draggingExperimentId() === exp.id" + [attr.data-guide]="isGuideExperiment(exp) ? 'dashboard-experiment-card' : null" (click)="selectExperiment(exp)" + (keydown)="onRowKeydown($event, exp)" (dragstart)="onExperimentDragStart(exp, $event)" + (dragend)="onExperimentDragEnd()"> + <button type="button" class="row-check" [class.row-check--on]="isInCompare(exp.id)" + [attr.aria-pressed]="isInCompare(exp.id)" aria-label="Select for compare" + [attr.data-guide]="exp.id === pagedExperiments()[0]?.id ? 'dashboard-compare' : null" + [disabled]="!isInCompare(exp.id) && compareIds().length >= 3" + (click)="onCompareCheck(exp.id, $event)"><i class="fas fa-check" aria-hidden="true"></i></button> - <ul class="experiments-list"> - @for (exp of pagedExperiments(); track exp.id) { - <li class="experiment-row" [class.selected]=" - (!compareMode && exp.id === selectedExperimentId) || - (compareMode && isInCompare(exp.id)) - " (click)="selectExperiment(exp)"> - <!-- LEFT: main info --> <div class="row-main"> <div class="row-title-line"> <span class="exp-name" [title]="exp.name"> {{ exp.name || "Untitled Investigation" }} </span> - - <span class="status-pill" [ngClass]="{ - completed: exp.status === 'success', - error: exp.status === 'error' - }"> - {{ exp.status }} - </span> + <!-- Only runs that need attention are marked; completed is the quiet default. --> + @if (exp.status !== 'success') { + <app-experiment-status [status]="exp.status" [compact]="true" /> + } </div> - <div class="row-meta-line"> - <span class="meta-item"> - <i class="far fa-calendar"></i> - {{ exp.dateCreated | date : "mediumDate" }} - </span> - - <span class="meta-item algo-item"> - <i class="fas fa-flask"></i> - {{ getAlgorithmLabel(exp.algorithmName) }} - </span> + @if (modelLine(exp); as line) { + <div class="row-model-line">{{ line }}</div> + } - @if (getDomainLabel(exp.domain); as domainLabel) { - <span class="meta-item domain-item"> - <i class="fas fa-layer-group"></i> - {{ domainLabel }} - </span> - } - </div> + <div class="row-meta-line">{{ metaLine(exp) }}</div> </div> - <!-- RIGHT: actions --> <div class="row-actions"> - <!-- 1. Copy Link (Always visible) --> - <button class="icon-btn" title="Copy link" (click)="onCopyLinkClicked(exp, $event)"> - <i class="fas fa-link"></i> - </button> + <!-- Add to folder: the membership menu for this run, and the only action left on the row. --> + <span class="folder-menu-anchor"> + <button class="icon-btn" [class.active]="folderMenuExpId() === exp.id" title="Add to folder" + aria-haspopup="menu" [attr.aria-expanded]="folderMenuExpId() === exp.id" + (click)="toggleFolderMenu(exp.id); $event.stopPropagation()"> + <i class="fas fa-object-group" aria-hidden="true"></i> + </button> + </span> + </div> - <!-- 2. Share/Unshare (Only for owner) --> - @if (isOwner(exp)) { - <button class="icon-btn" [class.shared]="exp.isShared" - [title]="exp.isShared ? 'Make private' : 'Share with others'" (click)="onToggleShare(exp, $event)"> - <i class="fas fa-share-alt"></i> + <!-- The panel belongs to the row, not to the trigger: the side pane clips horizontal + overflow, and a trigger-anchored menu runs off its left edge. --> + @if (folderMenuExpId() === exp.id) { + <div class="folder-menu" cdkMenu (keydown.escape)="closeFolderMenu()" (click)="$event.stopPropagation()"> + @for (folder of foldersService.folders(); track folder.id) { + <button type="button" class="folder-menu__item" cdkMenuItem + (cdkMenuItemTriggered)="onFolderMenuPick(folder, exp.id)" role="menuitemcheckbox" + [attr.aria-checked]="isFolderMember(folder.id, exp.id)"> + <i class="fas fa-check folder-menu__check" [class.folder-menu__check--off]="!isFolderMember(folder.id, exp.id)" + aria-hidden="true"></i> + <span class="folder-menu__name">{{ folder.name }}</span> + <span class="count-badge">{{ folder.experimentIds.length }}</span> </button> } - @if (copyToastVisible() && lastSharedExperimentId() === exp.id) { - <span class="copy-chip"> - {{ copyToastMessage() }} - </span> + @if (folderMenuNewOpen()) { + <div class="folder-menu__form"> + <input class="folder-menu__input" [value]="folderMenuDraft()" placeholder="Folder name" + aria-label="New folder name" (input)="folderMenuDraft.set($any($event.target).value)" + (keydown.enter)="commitFolderMenuNew(exp.id)" /> + <button type="button" class="folder-menu__form-action" [disabled]="folderBusy()" + (click)="commitFolderMenuNew(exp.id)">Add</button> + </div> + @if (folderMenuError(); as folderMenuError) { + <p class="folder-menu__error">{{ folderMenuError }}</p> } - - <button class="icon-btn" title="Edit" (click)="onEditRequested(exp.id); $event.stopPropagation()"> - <i class="far fa-edit"></i> + } @else { + <button type="button" class="folder-menu__item folder-menu__item--new" cdkMenuItem + (cdkMenuItemTriggered)="openFolderMenuNew()"> + <i class="fas fa-plus folder-menu__check" aria-hidden="true"></i> + <span class="folder-menu__name">New folder…</span> </button> + } - @if (isOwner(exp)) { - <button class="icon-btn danger" title="Delete" (click)="onDeleteRequested(exp.id); $event.stopPropagation()"> - <i class="far fa-trash-alt"></i> - </button> + @if (!foldersService.folders().length && !folderMenuNewOpen()) { + <p class="folder-menu__hint">No folders yet. Create one to start a set.</p> } </div> - </li> + } + </div> } - </ul> - - <!-- Pagination bottom --> - <div class="list-pagination list-pagination--bottom"> - <button type="button" class="page-btn" (click)="prevPage()" [disabled]="currentPage() === 1"> - ‹ Prev - </button> + </div> + } + </div> - <div class="page-info">Page {{ currentPage() }} of {{ totalPages() }}</div> + <!-- While a compare set is being picked, its footer takes the pager's place. --> + @if (compareIds().length > 0) { + <div class="compare-selection-bar"> + <span class="compare-selection-count">{{ compareIds().length }} selected <span class="compare-selection-cap">· up to + 3</span></span> + <div class="compare-selection-actions"> + <button type="button" class="compare-selection-btn" (click)="clearCompareSelection.emit()">Clear</button> + <button type="button" class="compare-selection-btn compare-selection-btn--go" + [disabled]="compareIds().length < 2" (click)="compareOpened.emit()"> + {{ compareIds().length >= 2 ? 'Compare ' + compareIds().length : 'Pick one more' }} + </button> + </div> + </div> + } @else { + <!-- Pagination: the only pager in the pane, and the range it pages through. --> + <div class="list-pagination"> + <p class="list-summary"> + <strong>{{ rangeStart() }}–{{ rangeEnd() }}</strong> of {{ totalExperiments() }} + @if (historyTruncated()) { + <!-- The count above only covers the most recent runs a client-side filter could read. --> + <span class="list-summary-note">searched your {{ historyCap() }} most recent experiments — narrow the filters to reach older ones</span> + } + </p> - <button type="button" class="page-btn" (click)="nextPage()" [disabled]="currentPage() === totalPages()"> - Next › - </button> + <div class="page-controls"> + <button type="button" class="page-btn" (click)="prevPage()" [disabled]="currentPage() === 1" + title="Previous page" aria-label="Previous page"> + <i class="fas fa-chevron-left" aria-hidden="true"></i> + </button> + <span class="page-info">{{ currentPage() }} / {{ totalPages() }}</span> + <button type="button" class="page-btn" (click)="nextPage()" [disabled]="currentPage() === totalPages()" + title="Next page" aria-label="Next page"> + <i class="fas fa-chevron-right" aria-hidden="true"></i> + </button> + </div> </div> } -</div> \ No newline at end of file + } +</div> diff --git a/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.spec.ts b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.spec.ts new file mode 100644 index 000000000..d13f3abc2 --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.spec.ts @@ -0,0 +1,447 @@ +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideRouter } from '@angular/router'; +import { of } from 'rxjs'; + +import { Experiment } from '../../../models/experiments-dashboard.model'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; +import { ExperimentFoldersService } from '../../../services/experiment-folders.service'; +import { ExperimentLabelService } from '../../../services/experiment-label.service'; +import { FakeExperimentFoldersService } from '../experiment-folders.testing'; +import { ExperimentsListComponent, dayBucket } from './experiment-list.component'; + +const experiment = (id: string, overrides: Partial<Experiment> = {}): Experiment => ({ + id, + name: `Run ${id}`, + dateCreated: new Date('2026-01-01T00:00:00.000Z'), + status: 'success', + algorithmName: 'mock_anova', + author: 'Marie Curie', + authorEmail: 'marie.curie@chuv.ch', + isShared: false, + ...overrides, +}); + +describe('ExperimentsListComponent list pane', () => { + let fixture: ComponentFixture<ExperimentsListComponent>; + let component: ExperimentsListComponent; + let foldersService: FakeExperimentFoldersService; + let loaded: ReturnType<typeof signal<Experiment[]>>; + let labelMaps: Record<string, Record<string, string>>; + let pendingLabels: Promise<Record<string, string>> | null; + + const root = () => fixture.nativeElement as HTMLElement; + const rowTrigger = () => + (fixture.nativeElement as HTMLElement).querySelector('.folder-menu-anchor > .icon-btn') as HTMLButtonElement; + + const rows = () => Array.from(root().querySelectorAll<HTMLElement>('.experiment-row')); + const folderSelect = () => root().querySelector<HTMLSelectElement>('select[aria-label="Folder"]')!; + + /** A pick from the select, the way the browser hands one over. */ + const pickFolder = (value: string) => { + const select = folderSelect(); + select.value = value; + select.dispatchEvent(new Event('change')); + }; + + beforeEach(async () => { + foldersService = new FakeExperimentFoldersService(); + loaded = signal<Experiment[]>([experiment('a'), experiment('b')]); + labelMaps = {}; + pendingLabels = null; + + await TestBed.configureTestingModule({ + imports: [ExperimentsListComponent], + providers: [ + provideZonelessChangeDetection(), + provideRouter([]), + { + provide: ExperimentsDashboardService, + useValue: { + experiments: loaded, + totalPages: signal(1), + totalExperiments: signal(2), + historyTruncated: signal(false), + fullHistoryCap: 2500, + isLoading: signal(false), + getUserExperiments: vi.fn(), + toggleExperimentShare: vi.fn().mockReturnValue(of({ shared: true })), + }, + }, + { + provide: ExperimentStudioService, + useValue: { loadAllDataModels: () => of([]), backendAlgorithms: signal({}) }, + }, + { provide: ExperimentFoldersService, useValue: foldersService }, + { + provide: ExperimentLabelService, + useValue: { + getLabelMap: (domain: string) => pendingLabels ?? Promise.resolve(labelMaps[domain] ?? {}), + }, + }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(ExperimentsListComponent); + component = fixture.componentInstance; + }); + + afterEach(() => { + component.ngOnDestroy(); + }); + + it('offers one option per folder with its member count, and wears the open folder', () => { + foldersService.seedFolder('ANOVA', ['a']); + foldersService.seedFolder('PCA'); + fixture.componentRef.setInput('selectedFolderId', foldersService.folders()[0].id); + fixture.detectChanges(); + + const texts = Array.from(folderSelect().options).map((option) => option.textContent!.trim()); + expect(texts).toContain('All folders'); + expect(texts).toContain('ANOVA (1)'); + expect(texts).toContain('PCA (0)'); + expect(folderSelect().value).toBe(foldersService.folders()[0].id); + }); + + it('names the history a client-side filter could not read, beside the count it limits', () => { + const dashboard = TestBed.inject(ExperimentsDashboardService) as any; + dashboard.historyTruncated.set(true); + fixture.detectChanges(); + + const summary = root().querySelector('.list-summary')!; + expect(summary.textContent).toContain('of'); + expect(summary.querySelector('.list-summary-note')?.textContent) + .toContain('searched your 2500 most recent experiments'); + + dashboard.historyTruncated.set(false); + fixture.detectChanges(); + expect(root().querySelector('.list-summary-note')).toBeNull(); + }); + + it('sits under the tabs, never above search: the folder select is part of the tabs row', () => { + foldersService.seedFolder('ANOVA'); + fixture.detectChanges(); + + const regions = Array.from( + root().querySelectorAll<HTMLElement>('.list-toolbar, .experiments-tabs-row'), + ).map((region) => (region.classList.contains('list-toolbar') ? 'search' : 'tabs')); + expect(regions).toEqual(['search', 'tabs']); + expect(folderSelect().closest('.experiments-tabs-row')).not.toBeNull(); + }); + + it('keeps the "+ New" control on the tabs row and the folder icon on the row', () => { + foldersService.seedFolder('ANOVA'); + fixture.detectChanges(); + + expect(root().querySelector('.folder-chip--new')).toBeTruthy(); + + // fa-object-group: fa-layer-group already means domain in this pane and is the compare placeholder. + const rowTriggerIcon = root().querySelector('.folder-menu-anchor .icon-btn i')!; + expect(rowTriggerIcon.classList.contains('fa-object-group')).toBe(true); + expect(rowTriggerIcon.classList.contains('fa-layer-group')).toBe(false); + }); + + it('tells the dashboard which folder is open, and "All folders" is the clear', () => { + const folder = foldersService.seedFolder('ANOVA'); + fixture.detectChanges(); + + const emitted: Array<string | null> = []; + component.folderSelected.subscribe((id) => emitted.push(id)); + + pickFolder(folder.id); + fixture.componentRef.setInput('selectedFolderId', folder.id); + fixture.detectChanges(); + pickFolder('all'); + + expect(emitted).toEqual([folder.id, null]); + }); + + it('creates a folder from the tabs row and opens it', () => { + fixture.detectChanges(); + + (root().querySelector('.folder-chip--new') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const input = root().querySelector('.folder-chip-input') as HTMLInputElement; + input.value = 'Sensitivity'; + input.dispatchEvent(new Event('input')); + (root().querySelector('.folder-chip-form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folders().map((folder) => folder.name)).toEqual(['Sensitivity']); + + const emitted: Array<string | null> = []; + component.folderSelected.subscribe((id) => emitted.push(id)); + pickFolder(foldersService.folders()[0].id); + expect(emitted).toEqual([foldersService.folders()[0].id]); + }); + + it('rejects a duplicate name instead of creating a second folder', () => { + foldersService.seedFolder('ANOVA'); + fixture.detectChanges(); + + (root().querySelector('.folder-chip--new') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const input = root().querySelector('.folder-chip-input') as HTMLInputElement; + input.value = 'anova'; + input.dispatchEvent(new Event('input')); + (root().querySelector('.folder-chip-form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folders().length).toBe(1); + expect(root().querySelector('.folder-strip-error')!.textContent).toContain('taken'); + }); + + it('adds and drops the run of an opened row menu', () => { + const folder = foldersService.seedFolder('ANOVA'); + fixture.detectChanges(); + + const trigger = root().querySelectorAll<HTMLButtonElement>('.folder-menu-anchor > .icon-btn')[0]; + trigger.click(); + fixture.detectChanges(); + + const items = root().querySelectorAll<HTMLElement>('.folder-menu__item'); + expect(items.length).toBe(2); // the folder plus "New folder…" + expect(items[0].querySelector('.folder-menu__check')!.classList.contains('folder-menu__check--off')).toBe(true); + + items[0].click(); + fixture.detectChanges(); + expect(foldersService.isMember(folder.id, 'a')).toBe(true); + + // A pick is a tick, not a navigation: the menu stays open so a run can join a second folder. + const checks = root().querySelectorAll<HTMLElement>('.folder-menu__item .folder-menu__check'); + expect(checks[0].classList.contains('folder-menu__check--off')).toBe(false); + expect(root().querySelector('.folder-menu')).toBeTruthy(); + expect(trigger.getAttribute('aria-expanded')).toBe('true'); + + (checks[0].closest('.folder-menu__item') as HTMLElement).click(); + fixture.detectChanges(); + expect(foldersService.isMember(folder.id, 'a')).toBe(false); + }); + + it('treats a folder pick as a folder choice, not as a row selection', () => { + foldersService.seedFolder('ANOVA'); + const selectExperiment = vi.spyOn(fixture.componentInstance, 'selectExperiment').mockReturnValue(undefined); + fixture.detectChanges(); + + rowTrigger().click(); + fixture.detectChanges(); + root().querySelector<HTMLElement>('.folder-menu__item')!.click(); + fixture.detectChanges(); + + expect(selectExperiment).not.toHaveBeenCalled(); + expect(foldersService.folders()[0].experimentIds).toEqual(['a']); + }); + + it('keeps the menu open for its own clicks and closes on a click outside it', () => { + foldersService.seedFolder('ANOVA'); + fixture.detectChanges(); + + rowTrigger().click(); + fixture.detectChanges(); + + // The panel hangs off the row, so "outside the trigger" must not yet mean "outside the menu". + root() + .querySelector<HTMLElement>('.folder-menu__name')! + .dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })); + fixture.detectChanges(); + expect(root().querySelector('.folder-menu')).toBeTruthy(); + + document.body.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })); + fixture.detectChanges(); + expect(root().querySelector('.folder-menu')).toBeNull(); + }); + + it('states the model on its own line and checks a run into compare without selecting it', () => { + loaded.set([experiment('a', { variables: ['MMSE'], covariates: ['Age'] }), experiment('b')]); + fixture.detectChanges(); + + expect(rows()[0].textContent).toContain('MMSE ~ Age'); + + const selected: Experiment[] = []; + const toggled: string[] = []; + component.experimentSelected.subscribe((exp) => selected.push(exp)); + component.compareToggled.subscribe((id) => toggled.push(id)); + + (rows()[0].querySelector('.row-check') as HTMLButtonElement).click(); + + expect(toggled).toEqual(['a']); + expect(selected).toEqual([]); + }); + + it('shows catalog labels on the model line and never the variable codes', async () => { + labelMaps['dementia:1'] = { age: 'Age', biol_sex: 'Biological sex' }; + loaded.set([experiment('a', { domain: 'dementia:1', variables: ['age'], covariates: ['biol_sex'] })]); + fixture.detectChanges(); + await fixture.whenStable(); + fixture.detectChanges(); + + const text = rows()[0].textContent ?? ''; + expect(text).toContain('Age ~ Biological sex'); + expect(text).not.toContain('biol_sex'); + expect(text).not.toContain('age ~'); + expect(root().querySelector('.row-model-line')?.textContent).toBe('Age ~ Biological sex'); + }); + + it('keeps the model line hidden until a real label exists', async () => { + let resolveMap!: (map: Record<string, string>) => void; + pendingLabels = new Promise((resolve) => { + resolveMap = resolve; + }); + loaded.set([experiment('a', { domain: 'dementia:1', variables: ['age'], covariates: ['biol_sex'] })]); + fixture.detectChanges(); + + expect(root().querySelector('.row-model-line')).toBeNull(); + expect(rows()[0].textContent).not.toContain('biol_sex'); + + resolveMap({ age: 'age', biol_sex: 'Biological sex' }); + await fixture.whenStable(); + fixture.detectChanges(); + + expect(root().querySelector('.row-model-line')?.textContent).toBe('Biological sex'); + expect(rows()[0].textContent).not.toContain('age'); + }); + + it('keeps the name form open when the create never reached the server', () => { + vi.spyOn(console, 'error').mockReturnValue(undefined); + foldersService.failWith('createFolder'); + fixture.detectChanges(); + + (root().querySelector('.folder-chip--new') as HTMLButtonElement).click(); + fixture.detectChanges(); + + const input = root().querySelector('.folder-chip-input') as HTMLInputElement; + input.value = 'Sensitivity'; + input.dispatchEvent(new Event('input')); + (root().querySelector('.folder-chip-form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folders()).toEqual([]); + expect(root().querySelector('.folder-chip-input')).toBeTruthy(); + expect(root().querySelector('.folder-strip-error')!.textContent).toContain('Could not create the folder'); + }); + + it('says the folders could not be read at all, rather than showing an empty strip as the truth', () => { + foldersService.loadError.set('Could not load your folders. Check the connection and reload the page.'); + fixture.detectChanges(); + + expect(root().querySelector('.folder-strip-error')!.textContent).toContain('Could not load your folders'); + expect(root().querySelector('.folder-strip-hint')).toBeNull(); + }); + + it('creates a folder straight from the row menu and files the run in it', () => { + fixture.detectChanges(); + + root().querySelectorAll<HTMLButtonElement>('.folder-menu-anchor > .icon-btn')[0].click(); + fixture.detectChanges(); + + const newItem = root().querySelector('.folder-menu__item--new') as HTMLButtonElement; + newItem.click(); + fixture.detectChanges(); + + const input = root().querySelector('.folder-menu__input') as HTMLInputElement; + input.value = 'Q3 meta'; + input.dispatchEvent(new Event('input')); + (root().querySelector('.folder-menu__form-action') as HTMLButtonElement).click(); + fixture.detectChanges(); + + expect(foldersService.folders().length).toBe(1); + expect(foldersService.folders()[0].experimentIds).toEqual(['a']); + }); + + describe('the tabs row', () => { + // Sort sits inside the filter panel, so the panel is opened before the orders are looked for. + const trigger = () => root().querySelector<HTMLButtonElement>('.sort-overflow__trigger')!; + + // A sort pick writes the query, and the test browser keeps one page across specs: start clean, leave clean. + beforeEach(() => { + history.replaceState(null, '', '/experiments-dashboard'); + component.sort.set('created-desc'); + }); + afterEach(() => history.replaceState(null, '', '/experiments-dashboard')); + + const openFilters = () => { + fixture.detectChanges(); + (root().querySelector('.filter-toggle-btn') as HTMLButtonElement).click(); + fixture.detectChanges(); + }; + + const orders = () => Array.from(root().querySelectorAll<HTMLElement>('.sort-overflow .row-overflow__item')); + const details = () => root().querySelector<HTMLDetailsElement>('.sort-overflow')!; + const checkOf = (item: HTMLElement) => + item.querySelector<HTMLElement>('.sort-overflow__check')!.classList.contains('sort-overflow__check--off'); + + it('keeps the tabs and the controls on one row, with the six orders behind the sort icon', () => { + fixture.detectChanges(); + // A select here measured 112px and pushed Filters onto a second line of its own. + expect(root().querySelector('select.sort-select')).toBeNull(); + + const rowEl = root().querySelector<HTMLElement>('.experiments-tabs-row')!; + const tallest = Math.max( + ...Array.from(rowEl.children).map((el) => (el as HTMLElement).getBoundingClientRect().height), + ); + if (window.innerWidth > 640) { + // Below that the two-row layout is the design, not an accident, and it re-wraps on purpose. + // A wrapped row would stand as tall as both of its lines together. + expect(rowEl.getBoundingClientRect().height).toBeLessThanOrEqual(tallest + 4); + } + + openFilters(); + + expect(trigger().title).toBe('Sort: Newest'); + expect(details().open).toBe(false); + + trigger().click(); + fixture.detectChanges(); + + expect(orders().map((b) => b.textContent!.trim())).toEqual([ + 'Newest', + 'Oldest', + 'Name A–Z', + 'Name Z–A', + 'Status', + 'Algorithm', + ]); + expect(checkOf(orders()[0])).toBe(false); + expect(checkOf(orders()[1])).toBe(true); + expect(details().open).toBe(true); + }); + + it('folds back into the icon once an order is taken, and says which one is on', () => { + openFilters(); + trigger().click(); + fixture.detectChanges(); + + orders()[2].click(); + fixture.detectChanges(); + + expect(component.sort()).toBe('name-asc'); + expect(details().open).toBe(false); + expect(trigger().title).toBe('Sort: Name A–Z'); + }); + }); + + describe('the page count', () => { + it('is paged once, with the range stated inside the pager', () => { + fixture.detectChanges(); + + expect(root().querySelectorAll('.list-pagination').length).toBe(1); + expect(root().querySelector('.list-pagination small')).toBeNull(); + expect(root().querySelector('.list-summary')!.textContent).toContain('1–2 of 2'); + }); + }); +}); + +describe('dayBucket', () => { + const now = new Date(2026, 8, 28, 10, 0); + + it('splits by local calendar day: today, the six days before, then earlier', () => { + expect(dayBucket(new Date(2026, 8, 28, 0, 5), now)).toBe('Today'); + expect(dayBucket(new Date(2026, 8, 27, 23, 59), now)).toBe('This week'); + expect(dayBucket(new Date(2026, 8, 22, 0, 0), now)).toBe('This week'); + expect(dayBucket(new Date(2026, 8, 21, 23, 59), now)).toBe('Earlier'); + expect(dayBucket('not a date', now)).toBe('Earlier'); + }); +}); diff --git a/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.ts b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.ts index 7c909c0e6..d156cfffa 100644 --- a/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.ts +++ b/src/app/pages/experiments-dashboard/experiment-list/experiment-list.component.ts @@ -1,72 +1,205 @@ -import { Component, Output, EventEmitter, computed, signal, Input, effect, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, Component, ElementRef, HostListener, computed, signal, effect, OnInit, OnDestroy, output, inject, input, viewChild, Renderer2, ChangeDetectorRef } from '@angular/core'; +import { CdkMenu, CdkMenuItem } from '@angular/cdk/menu'; import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentFoldersService } from '../../../services/experiment-folders.service'; +import { ExperimentLabelService } from '../../../services/experiment-label.service'; import { Experiment } from '../../../models/experiments-dashboard.model'; -import { CommonModule } from '@angular/common'; +import { ExperimentFolder } from '../../../models/experiment-folder.model'; +import { CommonModule, formatDate } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { ExperimentSearchComponent } from '../experiment-search/experiment-search.component'; -import { Router, RouterModule } from '@angular/router'; -import { ExperimentFilters } from '../experiment-search/experiment-filter.model'; +import { ExperimentStatusComponent } from '../shared/experiment-status/experiment-status.component'; +import { RouterModule } from '@angular/router'; +import { isExperimentOwner } from '../../../core/share.utils'; +import { beginExperimentDrag } from '../../../core/experiment-drag.utils'; +import { formulaLine } from '../../../core/result-label.utils'; +import { ExperimentFilters, EXPERIMENT_SORTS, EXPERIMENT_SORT_VALUES, ExperimentSort } from '../experiment-search/experiment-filter.model'; +import { readDashboardQuery, updateDashboardQuery } from '../dashboard-query.utils'; +import { InflightWrites } from '../../../core/inflight-writes'; +import { statusChip } from '../shared/experiment-status/experiment-status.component'; + +/** A query-string value is only trusted when it is one the UI can actually offer. */ +const pick = <T extends string>(value: string | null, allowed: readonly T[], fallback: T): T => + value !== null && (allowed as readonly string[]).includes(value) ? (value as T) : fallback; + +const DATE_PRESETS = ['any', 'today', '7d', '30d'] as const; +const STATUSES = ['any', 'success', 'error'] as const; +const SHARED = ['any', 'shared', 'private'] as const; + +const RELATIVE_TIME = new Intl.RelativeTimeFormat('en', { numeric: 'auto', style: 'short' }); + +/** Today, the six days before it, or earlier — by local calendar day. */ +export function dayBucket(value: Date | string, now = new Date()): string { + const date = value instanceof Date ? value : new Date(value); + const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime(); + const time = date.getTime(); + if (time >= startOfToday) return 'Today'; + if (time >= startOfToday - 6 * 86_400_000) return 'This week'; + return 'Earlier'; +} @Component({ selector: 'app-experiments-list', - imports: [CommonModule, FormsModule, RouterModule, ExperimentSearchComponent], + imports: [CommonModule, FormsModule, RouterModule, ExperimentSearchComponent, ExperimentStatusComponent, CdkMenu, CdkMenuItem], templateUrl: './experiment-list.component.html', - styleUrls: ['./experiment-list.component.css'] + styleUrl: './experiment-list.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class ExperimentsListComponent implements OnInit { - @Output() experimentSelected = new EventEmitter<Experiment>(); - @Output() deleteRequested = new EventEmitter<string>(); - @Output() editRequested = new EventEmitter<string>(); - - @Input() selectedExperimentId: string | null = null; - @Input() currentUserEmail: string | null = null; - @Input() compareIds: string[] = []; - @Input() compareMode: boolean = false; - - constructor( - public experimentsService: ExperimentsDashboardService, - private expStudio: ExperimentStudioService, - private router: Router - ) { - this.expStudio.loadAllDataModels().subscribe(models => { - const map: Record<string, string> = {}; - models.forEach(m => { - if (m.code) { - const key = m.version ? `${m.code}:${m.version}` : m.code; - map[key] = m.label || m.code; - } - }); - this.modelLabels.set(map); - }); - +export class ExperimentsListComponent implements OnInit, OnDestroy { + experimentsService = inject(ExperimentsDashboardService); + readonly foldersService = inject(ExperimentFoldersService); + readonly expStudio = inject(ExperimentStudioService); + private readonly labelService = inject(ExperimentLabelService); + /** Domain code → variable code → catalog label. Missing key means the map is still loading. */ + private readonly labelsByDomain = signal<Record<string, Record<string, string>>>({}); + private readonly labelLoads = new Set<string>(); + private renderer = inject(Renderer2); + private cdr = inject(ChangeDetectorRef); + + readonly experimentSelected = output<Experiment>(); + /** null clears the folder canvas; a folder and an experiment never share the centre pane. */ + readonly folderSelected = output<string | null>(); + readonly clearCompareSelection = output<void>(); + readonly compareToggled = output<string>(); + readonly compareOpened = output<void>(); + + readonly selectedExperimentId = input<string | null>(null); + readonly selectedFolderId = input<string | null>(null); + readonly currentUserEmail = input<string | null>(null); + readonly compareIds = input<string[]>([]); + + constructor() { effect(() => { this.experimentsService.getUserExperiments( this.pageIndex(), this.pageSize, this.onlyMine(), - this.filters() + this.filters(), + this.sort(), ); - }, { allowSignalWrites: true }); + }); + + effect(() => { + const domains = this.pagedExperiments() + .map((exp) => exp.domain) + .filter((domain): domain is string => !!domain); + for (const domain of domains) { + if (this.labelsByDomain()[domain] || this.labelLoads.has(domain)) continue; + this.labelLoads.add(domain); + void this.labelService.getLabelMap(domain).then((map) => { + this.labelsByDomain.update((current) => ({ ...current, [domain]: map })); + }); + } + }); + + // Running jobs change server-side; refresh the visible page while any row is still active. + this.statusPoll = setInterval(() => { + if (this.hasRunningExperiments()) { + this.refreshCurrentPage(); + } + }, 15_000); } ngOnInit(): void { - this.onlyMine.set(this.initialOnlyMine); - this.experimentsService.fetchGlobalTotal(); + this.applyUrlState(); + } + + @HostListener('window:popstate') + onDashboardPopState() { + this.applyUrlState(); + } + + private applyUrlState(): void { + const params = readDashboardQuery(); + const tab = params.get('tab'); + const page = Number(params.get('page') ?? '0'); + const query = params.get('q'); + const datePreset = params.get('date'); + const algorithm = params.get('algo'); + const author = params.get('author'); + const variable = params.get('variable'); + const status = params.get('status'); + const shared = params.get('shared'); + const sort = params.get('sort'); + + if (tab === 'mine' || tab === 'shared') { + this.onlyMine.set(tab === 'mine'); + } else { + this.onlyMine.set(this.initialOnlyMine()); + } + + this.pageIndex.set(Number.isFinite(page) && page > 0 ? Math.floor(page) : 0); + this.filters.set({ + query: query ?? '', + datePreset: pick(datePreset, DATE_PRESETS, 'any'), + algorithm: algorithm ?? null, + author: author ?? null, + variable: variable ?? null, + status: pick(status, STATUSES, 'any'), + shared: pick(shared, SHARED, 'any'), + }); + this.sort.set(pick(sort, EXPERIMENT_SORT_VALUES, 'created-desc')); + } + + private syncUrlState(): void { + const filters = this.filters(); + updateDashboardQuery({ + tab: this.onlyMine() ? 'mine' : 'shared', + page: this.pageIndex() > 0 ? String(this.pageIndex()) : null, + sort: this.sort() !== 'created-desc' ? this.sort() : null, + q: filters.query || null, + date: filters.datePreset !== 'any' ? filters.datePreset : null, + algo: filters.algorithm, + author: filters.author, + variable: filters.variable, + status: filters.status !== 'any' ? filters.status : null, + shared: filters.shared !== 'any' ? filters.shared : null, + }); + } + + ngOnDestroy(): void { + this.unlistenFolderMenuClick?.(); + this.unlistenFolderMenuClick = null; + if (this.statusPoll) { + clearInterval(this.statusPoll); + this.statusPoll = null; + } + } + + /** `pending` is the only status the backend can leave a run in, so it is the one worth polling. */ + private hasRunningExperiments(): boolean { + return this.experimentsService.experiments().some((experiment) => statusChip(experiment.status).tone === 'pending'); + } + + private refreshCurrentPage(): void { + this.experimentsService.invalidateListCache(); + this.experimentsService.getUserExperiments( + this.pageIndex(), + this.pageSize, + this.onlyMine(), + this.filters(), + this.sort(), + ); } // toggle - @Input() initialOnlyMine = true; + readonly initialOnlyMine = input(true); readonly onlyMine = signal(true); // pagination readonly pageSize = 10; readonly pageIndex = signal(0); - // share toast - readonly copyToastVisible = signal<boolean>(false); - readonly copyToastMessage = signal<string>('Link copied to clipboard'); - readonly lastSharedExperimentId = signal<string | null>(null); + // sort + readonly sort = signal<ExperimentSort>('created-desc'); + /** The menu and the trigger's tooltip both read the one list of orders. */ + readonly sortOptions = EXPERIMENT_SORT_VALUES.map((value) => ({ value, label: EXPERIMENT_SORTS[value].label })); + readonly sortLabel = computed(() => EXPERIMENT_SORTS[this.sort()].label); + private readonly sortMenu = viewChild<ElementRef<HTMLDetailsElement>>('sortMenu'); + + // advanced filters + readonly filtersOpen = signal(false); // filters (single source of truth) readonly filters = signal<ExperimentFilters>({ @@ -79,119 +212,319 @@ export class ExperimentsListComponent implements OnInit { shared: 'any', }); - private modelLabels = signal<Record<string, string>>({}); + readonly activeFilterCount = computed(() => { + const value = this.filters(); + return [ + value.query?.trim(), + value.datePreset !== 'any', + value.algorithm, + value.author, + value.variable, + value.status !== 'any', + value.shared !== 'any', + ].filter(Boolean).length; + }); + + readonly hasActiveFilters = computed(() => this.activeFilterCount() > 0); + + readonly algorithmOptions = computed(() => + Object.entries(this.expStudio.backendAlgorithms()) + .map(([value, config]) => ({ value, label: config?.label ?? value })) + .sort((a, b) => a.label.localeCompare(b.label)), + ); + + private statusPoll: ReturnType<typeof setInterval> | null = null; patchFilters(patch: Partial<ExperimentFilters>) { this.filters.update(f => ({ ...f, ...patch })); this.pageIndex.set(0); + this.syncUrlState(); + } + + /** The menu is a choice, not a hold: picking an order folds the <details> back into the icon. */ + setSort(sort: ExperimentSort) { + this.sortMenu()?.nativeElement.removeAttribute('open'); + if (this.sort() === sort) return; + this.sort.set(sort); + this.pageIndex.set(0); + this.syncUrlState(); + } + + toggleFilters() { + this.filtersOpen.update((open) => !open); + } + + clearFilters() { + this.patchFilters({ + query: '', + datePreset: 'any', + algorithm: null, + author: null, + variable: null, + status: 'any', + shared: 'any', + }); + this.filtersOpen.set(false); } // compare helper isInCompare(id: string): boolean { - return this.compareIds.includes(id); + return this.compareIds().includes(id); } - toggleOnlyMine() { - this.onlyMine.update(v => !v); + onCompareCheck(id: string, event: Event): void { + event.stopPropagation(); + this.compareToggled.emit(id); + } + + setTab(isMine: boolean) { + if (this.onlyMine() === isMine) return; + this.onlyMine.set(isMine); this.pageIndex.set(0); + this.syncUrlState(); } - // ---- share logic (unchanged) ---- - private showCopyToast(message: string, expId: string) { - this.copyToastMessage.set(message); - this.copyToastVisible.set(true); - this.lastSharedExperimentId.set(expId); + // ---- folders: the chip strip under the tabs ---- + readonly isCreatingFolder = signal(false); + readonly newFolderDraft = signal(''); + readonly folderFormError = signal<string | null>(null); + /** + * A folder write the server refused for a reason a name cannot explain. Nothing was cached + * locally, so the strip is the only place this could surface — and it has to surface, or a dead + * backend looks exactly like a folder that simply did not take the run. + */ + readonly folderWriteError = signal<string | null>(null); + /** + * Every folder mutation goes through the tracker: one request per gesture on the wire (a doubled + * click is one write, an add to folder A and an add to folder B may overlap), the server's copy + * written back by the service, and any failure the service could not name landing on the strip + * instead of disappearing into an unhandled subscription. + */ + private readonly writes = new InflightWrites((message) => this.folderWriteError.set(message)); + readonly folderBusy = this.writes.busy; + + onFolderPick(value: string): void { + this.closeFolderMenu(); + this.isCreatingFolder.set(false); + this.folderFormError.set(null); + this.folderSelected.emit(value === 'all' ? null : value); + } - setTimeout(() => { - this.copyToastVisible.set(false); - this.lastSharedExperimentId.set(null); - }, 2400); + startNewFolder() { + this.newFolderDraft.set(''); + this.folderFormError.set(null); + this.folderWriteError.set(null); + this.isCreatingFolder.set(true); } - private buildShareUrl(expId: string): string { - const tree = this.router.createUrlTree( - ['/experiments-dashboard'], - { queryParams: { experiment: expId } } + cancelNewFolder() { + this.isCreatingFolder.set(false); + this.folderFormError.set(null); + } + + commitNewFolder() { + const name = this.newFolderDraft(); + this.writes.run( + `create-folder:${name.trim().toLowerCase()}`, + this.foldersService.createFolder(name), + (created) => { + if (!created) { + this.folderFormError.set(name.trim() ? 'That name is taken.' : 'Give the folder a name.'); + return; + } + + // Open the new folder: its empty canvas is where adding runs gets explained. + this.isCreatingFolder.set(false); + this.folderFormError.set(null); + this.folderWriteError.set(null); + this.folderSelected.emit(created.id); + }, + 'Could not create the folder — nothing was saved.', ); + } + + // ---- folders: per-row add-to-folder menu ---- + readonly folderMenuExpId = signal<string | null>(null); + readonly folderMenuNewOpen = signal(false); + readonly folderMenuDraft = signal(''); + readonly folderMenuError = signal<string | null>(null); - const relative = this.router.serializeUrl(tree); - const origin = window.location.origin; + private readonly folderMenu = viewChild(CdkMenu); + private unlistenFolderMenuClick: (() => void) | null = null; - return origin + relative; + isFolderMember(folderId: string, experimentId: string): boolean { + return this.foldersService.isMember(folderId, experimentId); } - isOwner(exp: Experiment): boolean { - const currentEmail = this.currentUserEmail; - if (!currentEmail || !exp.authorEmail) return false; - return currentEmail === exp.authorEmail; + toggleFolderMenu(expId: string): void { + if (this.folderMenuExpId() === expId) { + this.closeFolderMenu(); + return; + } + this.openFolderMenu(expId); } - onCopyLinkClicked(exp: Experiment, event: MouseEvent) { - event.stopPropagation(); - const url = this.buildShareUrl(exp.id); - - if (navigator.clipboard?.writeText) { - navigator.clipboard.writeText(url).then( - () => this.showCopyToast('Link copied to clipboard', exp.id), - (err) => { - console.warn('Failed to copy share URL:', err); - this.showCopyToast('Could not copy link — check console.', exp.id); + private openFolderMenu(expId: string): void { + this.folderMenuExpId.set(expId); + this.folderMenuNewOpen.set(false); + this.folderMenuDraft.set(''); + this.folderMenuError.set(null); + // The menu is an @if, so CdkMenu's roving tabindex only exists after this render. + this.cdr.detectChanges(); + this.folderMenu()?.focusFirstItem(); + + this.unlistenFolderMenuClick?.(); + this.unlistenFolderMenuClick = this.renderer.listen(document, 'pointerdown', (event: PointerEvent) => { + // Every row carries a trigger and only one row carries a panel, so the click is judged by + // what was hit rather than by a per-row template ref. The trigger decides its own fate in + // its click handler; anything else outside the open menu is a click away. + const target = event.target as HTMLElement | null; + if (target?.closest('.folder-menu-anchor, .folder-menu')) return; + this.closeFolderMenu(); + }); + } + + closeFolderMenu(): void { + if (!this.folderMenuExpId()) return; + this.folderMenuExpId.set(null); + this.folderMenuNewOpen.set(false); + this.folderMenuError.set(null); + this.unlistenFolderMenuClick?.(); + this.unlistenFolderMenuClick = null; + } + + /** One pick adds or drops the run, and the menu stays open for a second one. */ + onFolderMenuPick(folder: ExperimentFolder, expId: string): void { + this.writes.run( + `toggle-member:${folder.id}:${expId}`, + this.foldersService.toggleExperiment(folder.id, expId), + () => this.folderMenuError.set(null), + 'Could not change this folder — nothing was saved.', + ); + } + + openFolderMenuNew(): void { + this.folderMenuNewOpen.set(true); + this.folderMenuDraft.set(''); + this.folderMenuError.set(null); + } + + commitFolderMenuNew(expId: string): void { + const name = this.folderMenuDraft(); + this.writes.run( + `create-folder-member:${expId}:${name.trim().toLowerCase()}`, + this.foldersService.createFolder(name, expId), + (created) => { + if (!created) { + this.folderMenuError.set(name.trim() ? 'That name is taken.' : 'Give the folder a name.'); + return; } - ); - } else { - console.warn('Clipboard API not available, share URL:', url); - this.showCopyToast('Clipboard not available — check console log.', exp.id); - } + this.folderMenuNewOpen.set(false); + this.folderMenuError.set(null); + this.folderWriteError.set(null); + }, + 'Could not create the folder — nothing was saved.', + ); } - onToggleShare(exp: Experiment, event: MouseEvent) { - event.stopPropagation(); + readonly draggingExperimentId = signal<string | null>(null); + + onExperimentDragStart(exp: Experiment, event: DragEvent): void { + beginExperimentDrag(event.dataTransfer, exp.id); + this.draggingExperimentId.set(exp.id); + this.closeFolderMenu(); + } - // Extra safety update check + onExperimentDragEnd(): void { + this.draggingExperimentId.set(null); + } + + isOwner(exp: Experiment): boolean { + return isExperimentOwner(this.currentUserEmail(), exp.authorEmail); + } + + /** Outcome and predictors on one line, using catalog labels. A bare variable code is never shown. */ + modelLine(exp: Experiment): string { + const domain = exp.domain; + const map = domain ? this.labelsByDomain()[domain] : undefined; + if (domain && !map) return ''; + const names = (codes: string[] | undefined) => + (codes ?? []).map((code) => this.displayName(code, map)).filter((name) => name.length > 0); + return formulaLine(names(exp.variables), names(exp.covariates)); + } + + private displayName(code: string, map: Record<string, string> | undefined): string { + const label = map?.[code]; + if (label && label !== code) return label; + // An empty map means the catalog could not be read (load failed or the model version is + // gone); showing the code beats a row that silently loses its variables. + if (map && Object.keys(map).length === 0) return code; + if (map || /^[a-z][a-z0-9_]*$/.test(code)) return ''; + return code; + } + + metaLine(exp: Experiment): string { + const parts = [this.getAlgorithmLabel(exp.algorithmName), this.relativeWhen(exp.dateCreated)]; if (!this.isOwner(exp)) { - console.warn('Cannot share/unshare experiment owned by someone else.'); - return; + const who = exp.authorEmail?.split('@')[0] || exp.author; + if (who) parts.push(who); } + return parts.filter(Boolean).join(' · '); + } - const newShared = !exp.isShared; - - this.experimentsService - .toggleExperimentShare(exp.id, newShared) - .subscribe({ - next: () => { - if (newShared) { - this.showCopyToast('Experiment is now shared', exp.id); - // Auto-copy link when enabling share? User request implies separation, but usually convenient. - // Requirement says "split the button". I will keep them separate as requested. - } else { - this.showCopyToast('Experiment is no longer shared', exp.id); - } - }, - error: (err) => { - console.error('Failed to toggle share:', err); - this.showCopyToast('Failed to update share state', exp.id); - }, - }); - } - - // counts - readonly totalExperiments = computed(() => this.experimentsService.totalExperiments()); - readonly visibleExperiments = computed(() => this.experimentsService.experiments().length); + private relativeWhen(value: Date | string): string { + const date = value instanceof Date ? value : new Date(value); + const minutes = Math.round((date.getTime() - Date.now()) / 60000); + if (!Number.isFinite(minutes)) return ''; + if (minutes > -1) return 'Just now'; + if (minutes > -60) return RELATIVE_TIME.format(minutes, 'minute'); + if (minutes > -1440) return RELATIVE_TIME.format(Math.round(minutes / 60), 'hour'); + if (minutes > -10080) return RELATIVE_TIME.format(Math.round(minutes / 1440), 'day'); + return formatDate(date, 'mediumDate', 'en-US'); + } // pages readonly totalPages = computed(() => this.experimentsService.totalPages()); + readonly isLoading = computed(() => this.experimentsService.isLoading()); readonly currentPage = computed(() => this.pageIndex() + 1); + readonly totalExperiments = computed(() => this.experimentsService.totalExperiments()); + readonly historyTruncated = computed(() => this.experimentsService.historyTruncated()); + readonly historyCap = computed(() => this.experimentsService.fullHistoryCap); readonly pagedExperiments = computed<Experiment[]>(() => { return this.experimentsService.experiments(); }); + /** Day buckets in list order. Only a date sort is bucketed; any other order is one unlabelled group. */ + readonly experimentGroups = computed(() => { + const rows = this.pagedExperiments(); + if (!this.sort().startsWith('created')) return [{ label: '', rows }]; + const groups: { label: string; rows: Experiment[] }[] = []; + for (const exp of rows) { + const label = dayBucket(exp.dateCreated); + const last = groups.at(-1); + if (last?.label === label) last.rows.push(exp); + else groups.push({ label, rows: [exp] }); + } + return groups; + }); + + readonly rangeStart = computed(() => + this.totalExperiments() === 0 ? 0 : this.pageIndex() * this.pageSize + 1, + ); + + readonly rangeEnd = computed(() => + Math.min(this.pageIndex() * this.pageSize + this.pagedExperiments().length, this.totalExperiments()), + ); + readonly guideExperimentId = computed(() => this.selectGuideExperiment(this.pagedExperiments())?.id ?? null); + // pagination helpers goToPage(page: number) { const max = this.totalPages(); if (page < 1) page = 1; if (page > max) page = max; this.pageIndex.set(page - 1); + this.syncUrlState(); } nextPage() { @@ -207,12 +540,15 @@ export class ExperimentsListComponent implements OnInit { this.experimentSelected.emit(exp); } - onEditRequested(id: string) { - this.editRequested.emit(id); + onRowKeydown(event: KeyboardEvent, exp: Experiment) { + if (event.target !== event.currentTarget) return; + if (event.key !== 'Enter' && event.key !== ' ') return; + event.preventDefault(); + this.selectExperiment(exp); } - onDeleteRequested(id: string) { - this.deleteRequested.emit(id); + isGuideExperiment(exp: Experiment): boolean { + return exp.id === this.guideExperimentId(); } getAlgorithmLabel(code: string | null | undefined): string { @@ -221,8 +557,50 @@ export class ExperimentsListComponent implements OnInit { return algoConfig?.label || code; } - getDomainLabel(code: string | null | undefined): string | null { - if (!code) return null; - return this.modelLabels()[code] || code; + private selectGuideExperiment(experiments: Experiment[]): Experiment | null { + if (!experiments.length) { + return null; + } + + let bestMatch = experiments[0]; + let bestScore = Number.NEGATIVE_INFINITY; + + for (const experiment of experiments) { + const name = experiment.name.toLowerCase(); + const algorithmName = experiment.algorithmName.toLowerCase(); + const variables = (experiment.variables ?? []).map((value) => value.toLowerCase()); + let score = 0; + + if (experiment.isShared) { + score += 2; + } + + if (name.includes('tutorial') || name.includes('guide') || name.includes('example')) { + score += 4; + } + + if (algorithmName.includes('anova') || name.includes('anova')) { + score += 3; + } + + if (name.includes('one-way') || name.includes('one way') || name.includes('oneway')) { + score += 2; + } + + if (variables.some((value) => value.includes('age'))) { + score += 1; + } + + if (variables.some((value) => value.includes('sex'))) { + score += 1; + } + + if (score > bestScore) { + bestMatch = experiment; + bestScore = score; + } + } + + return bestMatch; } } diff --git a/src/app/pages/experiments-dashboard/experiment-search/experiment-filter.model.ts b/src/app/pages/experiments-dashboard/experiment-search/experiment-filter.model.ts index 1dd672d64..33cad321b 100644 --- a/src/app/pages/experiments-dashboard/experiment-search/experiment-filter.model.ts +++ b/src/app/pages/experiments-dashboard/experiment-search/experiment-filter.model.ts @@ -1,5 +1,29 @@ export type ExperimentDatePreset = 'any' | 'today' | '7d' | '30d'; +export type ExperimentSort = + | 'created-desc' + | 'created-asc' + | 'name-asc' + | 'name-desc' + | 'status-desc' + | 'algorithm-asc'; + +/** + * Every sort the list offers, once: the menu label plus the `orderBy`/`descending` pair + * `/services/experiments` accepts (ExperimentOrderBy allows created/name/status/algorithm). + * Insertion order is the menu order. + */ +export const EXPERIMENT_SORTS: Record<ExperimentSort, { label: string; orderBy: string; descending: boolean }> = { + 'created-desc': { label: 'Newest', orderBy: 'created', descending: true }, + 'created-asc': { label: 'Oldest', orderBy: 'created', descending: false }, + 'name-asc': { label: 'Name A–Z', orderBy: 'name', descending: false }, + 'name-desc': { label: 'Name Z–A', orderBy: 'name', descending: true }, + 'status-desc': { label: 'Status', orderBy: 'status', descending: true }, + 'algorithm-asc': { label: 'Algorithm', orderBy: 'algorithm', descending: false }, +}; + +export const EXPERIMENT_SORT_VALUES = Object.keys(EXPERIMENT_SORTS) as ExperimentSort[]; + export interface ExperimentFilters { query: string; datePreset: ExperimentDatePreset; @@ -11,9 +35,3 @@ export interface ExperimentFilters { status: 'any' | 'success' | 'error'; shared: 'any' | 'shared' | 'private'; } - -export interface ExperimentFilterOptions { - algorithms: string[]; - authors: string[]; - variables: string[]; -} diff --git a/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.css b/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.css index ef47b6b67..16423885b 100644 --- a/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.css +++ b/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.css @@ -1,20 +1,18 @@ .list-search { display: flex; align-items: center; - gap: 12px; - padding: 8px 16px; - border-radius: 999px; - background: rgba(255, 255, 255, 0.75); - backdrop-filter: blur(8px); - -webkit-backdrop-filter: blur(8px); - box-shadow: 0 4px 20px rgba(15, 23, 42, 0.04); - border: 1px solid rgba(148, 163, 184, 0.2); - transition: border-color 0.2s ease, box-shadow 0.2s ease; + gap: 8px; + height: 36px; + box-sizing: border-box; + padding: 0 6px 0 12px; + border-radius: 8px; + background: #ffffff; + border: 1px solid rgba(43, 51, 233, 0.16); + transition: border-color 0.15s ease; } .list-search:focus-within { border-color: var(--primary-color); - box-shadow: 0 4px 24px rgba(2, 128, 144, 0.1); } .list-search i { @@ -43,29 +41,31 @@ } .pill-select { - border: 1px solid rgba(148, 163, 184, 0.3); - background: rgba(255, 255, 255, 0.8); - border-radius: 999px; - padding: 4px 12px; + flex-shrink: 0; + height: 24px; + border: none; + background: #f8fafc; + border-radius: 6px; + padding: 0 6px; + font: inherit; font-size: 0.75rem; font-weight: 600; color: #475569; outline: none; cursor: pointer; - transition: all 0.2s ease; } .pill-select:hover { - background: white; - border-color: var(--primary-color); + background: #eef1fc; } .clear-btn { border: none; background: #f1f5f9; - border-radius: 999px; - width: 26px; - height: 26px; + border-radius: 6px; + flex-shrink: 0; + width: 24px; + height: 24px; cursor: pointer; display: grid; place-items: center; diff --git a/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.html b/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.html index ac004b8ef..356511382 100644 --- a/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.html +++ b/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.html @@ -2,17 +2,17 @@ <i class="fas fa-search"></i> <input type="text" - [ngModel]="filters.query" + [ngModel]="filters().query" (ngModelChange)="onQueryInput($event)" - placeholder="Search…" + placeholder="Search name or variable" [title]=" - filters.query ? '' : 'Search by name, description, author, algorithm…' + filters().query ? '' : 'Search by name, description, author, algorithm…' " /> <select class="pill-select" - [ngModel]="filters.datePreset" + [ngModel]="filters().datePreset" (ngModelChange)="onDatePresetChange($event)" title="Date range" > @@ -22,7 +22,9 @@ <option value="30d">Last 30 days</option> </select> - <button type="button" class="clear-btn" (click)="onClear()" title="Clear"> - <i class="fas fa-times"></i> + @if (filters().query || filters().datePreset !== 'any') { + <button type="button" class="clear-btn" (click)="onClear()" title="Clear" aria-label="Clear search"> + <i class="fas fa-times" aria-hidden="true"></i> </button> + } </div> diff --git a/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.ts b/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.ts index d02d76796..462e0c9ff 100644 --- a/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.ts +++ b/src/app/pages/experiments-dashboard/experiment-search/experiment-search.component.ts @@ -1,4 +1,4 @@ -import { Component, EventEmitter, Input, OnDestroy, Output } from "@angular/core"; +import { ChangeDetectionStrategy, Component, OnDestroy, output, input } from "@angular/core"; import { FormsModule } from "@angular/forms"; import { CommonModule } from "@angular/common"; import { ExperimentDatePreset, ExperimentFilters, } from "./experiment-filter.model"; @@ -7,11 +7,12 @@ import { ExperimentDatePreset, ExperimentFilters, } from "./experiment-filter.mo selector: 'app-experiment-search', imports: [CommonModule, FormsModule], templateUrl: './experiment-search.component.html', - styleUrls: ['./experiment-search.component.css'] + styleUrl: './experiment-search.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class ExperimentSearchComponent implements OnDestroy { - @Input({ required: true }) filters!: ExperimentFilters; - @Output() filtersChange = new EventEmitter<Partial<ExperimentFilters>>(); + readonly filters = input.required<ExperimentFilters>(); + readonly filtersChange = output<Partial<ExperimentFilters>>(); private t: ReturnType<typeof setTimeout> | null = null; @@ -27,15 +28,6 @@ export class ExperimentSearchComponent implements OnDestroy { this.filtersChange.emit({ datePreset: value }); } - onAlgorithmInput(value: string) { - const v = value.trim(); - this.filtersChange.emit({ algorithm: v ? v : null }); - } - - onAuthorInput(value: string) { - const v = value.trim(); - this.filtersChange.emit({ author: v ? v : null }); - } onClear() { this.filtersChange.emit({ diff --git a/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.css b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.css index f97b5f0b0..c5eba1a6d 100644 --- a/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.css +++ b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.css @@ -1,9 +1,19 @@ +/* The strip scrolls tall results inside itself so the sticky column headers stay visible — + which needs the component to actually hold the pane's height (see .compare-workspace). */ +:host { + display: flex; + flex-direction: column; + height: 100%; + min-height: 0; +} + .compare-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; - margin-bottom: 24px; + margin-bottom: 18px; + padding: 0 12px; animation: fadeIn 0.5s ease-out; } @@ -22,195 +32,97 @@ font-weight: 500; } -/* Layout dropdown */ -.layout-select { - display: flex; - align-items: center; - gap: 6px; - font-size: 0.78rem; - color: #6b7280; -} - -.layout-select label { - font-size: 0.78rem; - color: #94a3b8; -} - -.layout-select select { - border-radius: 999px; - border: 1px solid rgba(148, 163, 184, 0.8); - padding: 4px 10px; - font-size: 0.78rem; - background: #ffffff; - color: #0f172a; - outline: none; - cursor: pointer; - transition: border-color 0.12s ease, box-shadow 0.12s ease, - transform 0.12s ease; -} - -.layout-select select:focus { - border-color: var(--primary-color); - box-shadow: 0 0 0 1px rgba(43, 51, 233, 0.3); -} - -.layout-select select:hover { - transform: translateY(-1px); -} - -/* Rows wrapper */ -.compare-rows { +/* The compare workspace is a destination that can now say where it came from. Same recipe as + the folder canvas back button, so one pane-level gesture never wears two looks. */ +.compare-heading { display: flex; flex-direction: column; - gap: 12px; -} - -/* Row base */ -.compare-row { - border-radius: 20px; - padding: 12px 14px 16px; - background: rgba(255, 255, 255, 0.55); - backdrop-filter: blur(10px); - -webkit-backdrop-filter: blur(10px); - box-shadow: 0 8px 32px rgba(15, 23, 42, 0.05); - border: 1px solid rgba(148, 163, 184, 0.18); - transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1); -} - -/* Expanded / collapsed accents */ -.compare-row.expanded { - border-color: var(--primary-light); - box-shadow: 0 16px 34px rgba(43, 51, 233, 0.16); - background: radial-gradient(circle at top left, #f0f7ff, #ffffff); - transform: translateY(-1px); -} - -.compare-row.collapsed { - opacity: 0.96; -} - -/* Row header */ -.row-header { - display: flex; - justify-content: space-between; - align-items: center; - gap: 8px; - margin-bottom: 10px; -} - -.compare-row.expanded .row-header { - border-radius: 10px; - padding: 6px 8px; -} - -/* Label (Set 1 / count) */ -.row-label { - display: flex; - align-items: baseline; - gap: 6px; -} - -.row-title { - font-size: 0.85rem; - font-weight: 600; - color: #0f172a; -} - -.row-count { - font-size: 0.78rem; - color: #94a3b8; + align-items: flex-start; + gap: 4px; + min-width: 0; } -/* Toggle button – base */ -.row-toggle-btn { - border-radius: 999px; - border: 1px solid rgba(148, 163, 184, 0.8); - background: #f9fafb; - padding: 4px 10px; - font-size: 0.75rem; +.compare-back-btn { display: inline-flex; align-items: center; gap: 6px; + padding: 4px 10px 4px 6px; + border: 1px solid transparent; + border-radius: 999px; + background: transparent; + color: #64748b; + font-size: 0.72rem; + font-weight: 700; + text-transform: uppercase; + letter-spacing: 0.12em; cursor: pointer; - color: #475569; - transition: background 0.12s ease, border-color 0.12s ease, - transform 0.12s ease, box-shadow 0.12s ease, color 0.12s ease; -} - -.row-toggle-btn i { - font-size: 0.75rem; -} - -/* Hover */ -.row-toggle-btn:hover { - background: #e0f2fe; - border-color: #0ea5e9; - transform: translateY(-1px); - box-shadow: 0 8px 18px rgba(15, 23, 42, 0.12); + transition: all 0.2s ease; } -/* Expanded state */ -.compare-row.expanded .row-toggle-btn { - background: linear-gradient(135deg, var(--primary-color), var(--primary-light)); - border-color: transparent; - color: #ecfeff; - box-shadow: 0 10px 22px rgba(15, 23, 42, 0.22); +.compare-back-btn i { + font-size: 0.7rem; } -/* Collapsed state */ -.compare-row.collapsed .row-toggle-btn { - background: #f1f5f9; - border-color: rgba(148, 163, 184, 0.9); - color: #64748b; +.compare-back-btn:hover { + color: var(--primary-color); + border-color: var(--primary-color); + background: rgba(148, 163, 184, 0.1); } -/* Grid layout per row */ -.compare-grid { +/* COLUMNS */ +/* The comparison shows two runs side by side; a third and any beyond that wrap underneath. + The strip is the scroller — vertical too — so sticky column headers pin to its top while + tall results move. */ +.compare-columns { display: grid; - gap: 14px; -} - -/* 2 or 3 cols per row */ -.compare-row.cols-2 .compare-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); + grid-auto-flow: row; + align-items: start; + align-content: start; + gap: 14px; + flex: 1; + min-height: 0; + overflow: auto; + padding: 2px 12px 12px; } -.compare-row.cols-3 .compare-grid { - grid-template-columns: repeat(3, minmax(0, 1fr)); -} - -/* Cards */ -.compare-card { - border-radius: 20px; - background: rgba(255, 255, 255, 0.7); - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - box-shadow: 0 4px 12px rgba(15, 23, 42, 0.04); - padding: 18px 20px; +.compare-column { display: flex; flex-direction: column; - gap: 12px; - border: 1px solid rgba(148, 163, 184, 0.15); - transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1); -} - -.compare-card:hover { - background: rgba(255, 255, 255, 0.95); - transform: translateY(-4px) scale(1.02); - box-shadow: 0 20px 40px rgba(15, 23, 42, 0.12), 0 0 0 4px rgba(2, 128, 144, 0.04); - border-color: var(--primary-color); + min-width: 0; + border: 1px solid rgba(148, 163, 184, 0.22); + border-radius: 14px; + background: #ffffff; + animation: fadeIn 0.4s ease-out; } -.compare-card-header { +/* Sticky inside the strip: the run's name stays at the top while its result scrolls, so two + columns two screens apart are still identifiable. No overflow:hidden on the column — a + clipping ancestor would trap the sticky header inside its own card. */ +.column-header { + position: sticky; + top: 0; + z-index: 1; display: flex; - justify-content: space-between; align-items: flex-start; gap: 8px; + padding: 10px 12px; + background: #ffffff; + border-bottom: 1px solid rgba(148, 163, 184, 0.28); + border-radius: 13px 13px 0 0; } -.compare-card-header h4 { - margin: 0; - font-size: 1rem; +.column-id { + display: flex; + flex-direction: column; + gap: 2px; + flex: 1; + min-width: 0; +} + +.column-name { + min-width: 0; + font-size: 0.88rem; font-weight: 700; color: #0f172a; overflow: hidden; @@ -218,59 +130,65 @@ white-space: nowrap; } -.algo-line { - margin: 2px 0 0; - font-size: 0.78rem; - color: #64748b; -} - -.algo-line i { - margin-right: 4px; +.run-index { + flex-shrink: 0; + width: 18px; + text-align: right; + font-family: var(--font-mono); + font-size: 0.72rem; + color: #94a3b8; + font-variant-numeric: tabular-nums; } -.status-chip { - font-size: 0.68rem; - font-weight: 700; - text-transform: uppercase; - letter-spacing: 0.06em; - padding: 3px 10px; - border-radius: 999px; - background: #f1f5f9; - color: #475569; - flex-shrink: 0; - border: 1px solid transparent; +.run-meta { + display: inline-flex; + align-items: center; + flex-wrap: wrap; + gap: 8px; + min-width: 0; } -.status-chip.completed { - background: #ecfdf5; - color: #059669; - border-color: rgba(16, 185, 129, 0.2); +.run-algo { + font-size: 0.74rem; + color: #64748b; } -.status-chip.error { - background: #fff1f2; - color: #e11d48; - border-color: rgba(225, 29, 72, 0.2); +.run-algo i { + margin-right: 4px; } -/* Details table */ -.details-table { - width: 100%; - border-collapse: collapse; - font-size: 0.78rem; +.run-date { + font-size: 0.72rem; + color: #94a3b8; + font-variant-numeric: tabular-nums; } -.details-table th { - text-align: left; - font-weight: 500; - color: #6b7280; - padding: 2px 4px 2px 0; +/* The one mark that a run belongs to the user's own set, wearing the count badge's pill so it + reads as the same species of mark. The set's name replaces the old section heading. */ +.column-set-tag { + flex-shrink: 0; + max-width: 40%; + padding: 2px 8px; + border: 1px solid rgba(43, 51, 233, 0.18); + border-radius: 999px; + background: rgba(43, 51, 233, 0.08); + color: var(--primary-color); + font-size: 0.62rem; + font-weight: 800; + text-transform: uppercase; + letter-spacing: 0.08em; + overflow: hidden; + text-overflow: ellipsis; white-space: nowrap; } -.details-table td { - padding: 2px 0; - color: #111827; +/* Wide result tables scroll sideways inside the column; nothing scales down to fit. */ +.column-body { + display: flex; + flex-direction: column; + padding: 10px 12px 12px; + min-width: 0; + overflow-x: auto; } /* Results */ @@ -288,58 +206,7 @@ } .compare-config { - margin-top: 10px; - border-radius: 12px; - background: #ffffff; - border: 1px solid rgba(148, 163, 184, 0.25); - overflow: hidden; -} - -.config-header { - padding: 8px 10px; - background: rgba(224, 242, 254, 0.55); - border-bottom: 1px solid rgba(148, 163, 184, 0.2); -} - -.config-title { - font-size: 0.75rem; - font-weight: 600; - color: #0f172a; - letter-spacing: 0.02em; -} - -.config-table { - width: 100%; - border-collapse: collapse; -} - -.config-table th, -.config-table td { - padding: 7px 10px; - font-size: 0.76rem; - border-bottom: 1px solid rgba(226, 232, 240, 0.7); - vertical-align: top; -} - -.config-table th { - width: 42%; - color: #64748b; - font-weight: 600; - text-transform: none; -} - -.config-table td { - color: #0f172a; - word-break: break-word; -} - -.config-table tr:last-child th, -.config-table tr:last-child td { - border-bottom: none; -} - -.compare-config { - margin: 10px 0 8px; + margin: 0 0 8px; border-radius: 12px; background: #ffffff; border: 1px solid rgba(148, 163, 184, 0.22); @@ -427,15 +294,20 @@ max-width: 100%; } -/* Mobile: always 1 col */ -@media (max-width: 960px) { +/* Narrow screens: side-by-side is unusable, so one column per run, stacked, with the page + scrolling normally again. */ +@media (max-width: 900px) { + :host { + height: auto; + } - .compare-row.cols-2 .compare-grid, - .compare-row.cols-3 .compare-grid { + .compare-columns { grid-template-columns: minmax(0, 1fr); + flex: none; + overflow: visible; } - .compare-card { - padding: 10px 12px; + .column-header { + position: static; } -} \ No newline at end of file +} diff --git a/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.html b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.html index d8a0ab9e4..82b1e6cdc 100644 --- a/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.html +++ b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.html @@ -1,171 +1,137 @@ -@if (experimentRows().length >= 1) { +@if (experiments().length) { <header class="compare-header"> - <div> - <span class="meta-pill" - style="margin-bottom: 8px; display: inline-block; background: rgba(43, 51, 233, 0.1); color: var(--primary-color); padding: 4px 10px; border-radius: 999px; font-size: 0.65rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.05em;">Comparison - Mode</span> + <div class="compare-heading"> + @if (originFolderName(); as folderName) { + <!-- Back to the analysis set that opened this compare. The dashboard keeps the folder + selected and restores the list selection the compare replaced. --> + <button type="button" class="compare-back-btn" (click)="backToFolder.emit()" + [title]="'Back to the ' + folderName + ' folder'"> + <i class="fas fa-arrow-left" aria-hidden="true"></i> + Back to {{ folderName }} + </button> + } <h3>Compare experiments</h3> - <p> - Select at least two experiments from the list to see them side by side. - </p> - </div> - - <!-- Layout selector --> - <div class="layout-select"> - <label for="layoutSelect">Layout</label> - <select id="layoutSelect" [ngModel]="selectedLayout" (ngModelChange)="onLayoutChange($event)"> - <option [ngValue]="2">2 per row</option> - <option [ngValue]="3">3 per row</option> - </select> + <p>{{ compareHeadline() }}</p> </div> </header> -<div class="compare-rows"> - @for (row of experimentRows(); track row.index) { - <section class="compare-row" [ngClass]="[ - 'cols-' + layoutCols(), - isRowExpanded(row.index) ? 'expanded' : 'collapsed' - ]"> - <header class="row-header"> - <div class="row-label"> - <span class="row-title"> Set {{ row.index + 1 }} </span> - <span class="row-count"> - ({{ row.items.length }} experiment{{ - row.items.length > 1 ? "s" : "" - }}) +<!-- Two runs side by side, read left to right: folder-set runs first, then the algorithm + groups. Any remaining runs wrap underneath. --> +<div class="compare-columns"> + @for (column of columns(); track column.item.exp.id) { + <section class="compare-column"> + <header class="column-header"> + <span class="run-index">{{ column.number }}</span> + <div class="column-id"> + <span class="column-name" [title]="column.item.exp.name"> + {{ column.item.exp.name || 'Untitled Investigation' }} + </span> + <span class="run-meta"> + <span class="run-algo"> + <i class="fas fa-flask"></i>{{ algorithmLabel(column.item.exp.algorithmName) }} + </span> + <app-experiment-status [status]="column.item.exp.status" /> + <span class="run-date">{{ column.item.exp.dateCreated | date : "mediumDate" }}</span> </span> </div> - - <button type="button" class="row-toggle-btn" (click)="toggleRow(row.index)"> - <span>{{ isRowExpanded(row.index) ? "Collapse" : "Expand" }}</span> - <i class="fas" [ngClass]="{ - 'fa-chevron-up': isRowExpanded(row.index), - 'fa-chevron-down': !isRowExpanded(row.index) - }"></i> - </button> + @if (column.setTag; as setTag) { + <span class="column-set-tag" [title]="'In set ' + setTag">{{ setTag }}</span> + } </header> - @if (isRowExpanded(row.index)) { - <div class="compare-grid"> - @for (item of row.items; track item.exp.id) { - <article class="compare-card"> - <header class="compare-card-header"> - <div class="title-block"> - <h4 [title]="item.exp.name"> - {{ item.exp.name || "Untitled Investigation" }} - </h4> - <p class="algo-line"> - <i class="fas fa-flask"></i> - {{ item.exp.algorithmName || "Unknown algorithm" }} - </p> - </div> - - <span class="status-chip" [class.completed]="item.exp.status === 'success'" - [class.error]="item.exp.status === 'error'"> - {{ item.exp.status }} - </span> - </header> - - <!-- compact details table --> - <table class="details-table"> - <tbody> - <tr> - <th>Date</th> - <td>{{ item.exp.dateCreated | date : "medium" }}</td> - </tr> - <tr> - <th>Author</th> - <td> - {{ item.exp.author || item.exp.authorEmail || "Unknown" }} - </td> - </tr> - </tbody> - </table> + <div class="column-body"> + <div class="compare-config"> + <button type="button" class="config-toggle" (click)="toggleConfig(column.item.exp.id)"> + <span>{{ isConfigExpanded(column.item.exp.id) ? "Hide Configuration" : "Show Configuration" }}</span> + <i class="fas" [class.fa-chevron-down]="!isConfigExpanded(column.item.exp.id)" + [class.fa-chevron-up]="isConfigExpanded(column.item.exp.id)"></i> + </button> - <!-- CONFIG (collapsible) --> - <section class="compare-config"> - <button type="button" class="config-toggle" (click)="toggleConfig(item.exp.id)"> - <span>Configuration</span> - <i class="fas" [ngClass]="{ - 'fa-chevron-up': isConfigExpanded(item.exp.id), - 'fa-chevron-down': !isConfigExpanded(item.exp.id) - }"></i> - </button> - - @if (isConfigExpanded(item.exp.id)) { - <div class="config-body"> - <!-- Top row: Domain + Datasets --> - <div class="config-kv-grid"> - <div class="kv"> - <span class="k">Domain</span> - <span class="v">{{ - item.exp.domain || "Not specified" - }}</span> - </div> + @if (isConfigExpanded(column.item.exp.id)) { + <div class="config-body"> + <div class="config-kv-grid"> + <div class="kv"> + <span class="k">Algorithm</span> + <span class="v">{{ column.item.exp.algorithmName }}</span> + </div> + <div class="kv"> + <span class="k">Domain</span> + <span class="v">{{ column.item.exp.domain || '—' }}</span> + </div> + </div> - <div class="kv"> - <span class="k">Datasets</span> - <span class="v"> - @if (item.exp.datasets?.length) { - {{ item.exp.datasets?.join(", ") }} - } @else { Not specified } - </span> - </div> + <div class="config-tags-row"> + <span class="k">Datasets</span> + @if (column.item.exp.datasets?.length) { + <div class="tag-list"> + @for (d of column.item.exp.datasets; track d) { + <span class="tag">{{ d }}</span> + } </div> + } @else { + <span class="v">—</span> + } + </div> - <!-- Tags: Variables/Covariates/Filters --> - @if (getVariablesWithLabels(item.exp).length) { - <div class="config-tags-row"> - <span class="k">Variables (Y)</span> - <div class="tag-list"> - @for (v of getVariablesWithLabels(item.exp); track v.code) { - <span class="tag" [title]="v.code">{{ v.label }}</span> - } - </div> + <div class="config-tags-row"> + <span class="k">Variables</span> + @if (getVariablesWithLabels(column.item.exp).length) { + <div class="tag-list"> + @for (opt of getVariablesWithLabels(column.item.exp); track opt.code) { + <span class="tag" [title]="opt.code">{{ opt.label }}</span> + } </div> - } @if (getCovariatesWithLabels(item.exp).length) { - <div class="config-tags-row"> - <span class="k">Covariates (X)</span> - <div class="tag-list"> - @for (c of getCovariatesWithLabels(item.exp); track c.code) { - <span class="tag" [title]="c.code">{{ c.label }}</span> - } - </div> + } @else { + <span class="v">—</span> + } + </div> + + <div class="config-tags-row"> + <span class="k">Covariates</span> + @if (getCovariatesWithLabels(column.item.exp).length) { + <div class="tag-list"> + @for (opt of getCovariatesWithLabels(column.item.exp); track opt.code) { + <span class="tag" [title]="opt.code">{{ opt.label }}</span> + } </div> - } @if (getFiltersWithLabels(item.exp).length) { - <div class="config-tags-row"> - <span class="k">Filters</span> - <div class="tag-list"> - @for (f of getFiltersWithLabels(item.exp); track f.code) { - <span class="tag" [title]="f.code">{{ f.label }}</span> - } - </div> + } @else { + <span class="v">—</span> + } + </div> + + <div class="config-tags-row"> + <span class="k">Filters</span> + @if (getFiltersWithLabels(column.item.exp).length) { + <div class="tag-list"> + @for (opt of getFiltersWithLabels(column.item.exp); track opt.code) { + <span class="tag" [title]="opt.code">{{ opt.label }}</span> + } </div> + } @else { + <span class="v">—</span> } </div> - } - </section> + </div> + } + </div> - <!-- results --> - <section class="compare-results"> - @if (item.state.loading) { - <p class="muted-text">Loading results...</p> - } @else if (item.state.error) { - <p class="error-text">{{ item.state.error }}</p> - } @else if (item.state.result) { - <app-algorithm-result [result]="enrichResult(item.exp, item.state.result)" - [schema]="getOutputSchemaFor(item.exp)" [algorithm]="item.exp.algorithmName" - [enumMaps]="getEnumMapsFor(item.exp)" [yVar]="getYVarFor(item.exp)" [xVar]="getXVarFor(item.exp)" - [labelMap]="getLabelMapFor(item.exp)"></app-algorithm-result> - } @else { - <p class="muted-text">No results available.</p> - } - </section> - </article> - } + <div class="compare-results"> + @if (column.item.state.loading) { + <span class="muted-text">Loading results…</span> + } @else if (column.item.state.error) { + <span class="error-text">{{ column.item.state.error }}</span> + } @else if (column.item.state.result) { + <app-algorithm-result [result]="enrichResult(column.item.exp, column.item.state.result)" + [schema]="getOutputSchemaFor(column.item.exp)" [algorithm]="column.item.exp.algorithmName" + [enumMaps]="getEnumMapsFor(column.item.exp)" [yVar]="getYVarFor(column.item.exp)" + [xVar]="getXVarFor(column.item.exp)" [labelMap]="getLabelMapFor(column.item.exp)" + [parameters]="column.item.state.parameters ?? null"></app-algorithm-result> + } @else { + <span class="muted-text">No results available.</span> + } + </div> </div> - } </section> } </div> -} \ No newline at end of file +} diff --git a/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.spec.ts b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.spec.ts new file mode 100644 index 000000000..443c8fa85 --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.spec.ts @@ -0,0 +1,221 @@ +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideEchartsCore } from 'ngx-echarts'; +import { EMPTY, of } from 'rxjs'; + +import { Experiment } from '../../../models/experiments-dashboard.model'; +import { ExperimentLabelService } from '../../../services/experiment-label.service'; +import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; +import { ExperimentFoldersService } from '../../../services/experiment-folders.service'; +import { FakeExperimentFoldersService } from '../experiment-folders.testing'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentsCompareComponent } from './experiments-compare.component'; + +const experiment = (id: string, algorithmName = 'mock_anova'): Experiment => ({ + id, + name: `Run ${id}`, + dateCreated: new Date('2026-01-01T00:00:00.000Z'), + status: 'success', + algorithmName, + author: 'Marie Curie', + authorEmail: 'marie.curie@chuv.ch', + isShared: false, +}); + +/** + * The compare page is read as columns side by side. Two things decide the order those columns + * lay out in: the sets the origin folder defines, and the algorithm of everything nobody + * grouped. Both are read here — sets are written on the folder canvas. + */ +describe('ExperimentsCompareComponent', () => { + let fixture: ComponentFixture<ExperimentsCompareComponent>; + let component: ExperimentsCompareComponent; + let foldersService: FakeExperimentFoldersService; + let dashboard: { experiments: unknown; getExperimentResult: (id: string) => unknown }; + + const root = () => fixture.nativeElement as HTMLElement; + const headline = () => root().querySelector('.compare-header p')!.textContent!.trim(); + const columnsOf = () => Array.from(root().querySelectorAll<HTMLElement>('.compare-column')); + const columnNames = () => columnsOf().map((column) => column.querySelector('.column-name')!.textContent!.trim()); + const numbers = () => columnsOf().map((column) => column.querySelector('.run-index')!.textContent!.trim()); + const algos = () => columnsOf().map((column) => column.querySelector('.run-algo')!.textContent!.trim()); + const setTags = () => columnsOf().map((column) => column.querySelector('.column-set-tag')?.textContent!.trim() ?? null); + + /** A folder the compare can read: the workspace writes nothing, so specs seed rather than click. */ + const makeFolder = (name: string, memberIds: string[] = []) => foldersService.seedFolder(name, memberIds); + const show = (ids: string[], originFolderId: string | null = null) => + showRuns(ids.map((id) => experiment(id)), originFolderId); + + const showRuns = (experiments: Experiment[], originFolderId: string | null = null) => { + fixture.componentRef.setInput('experiments', experiments); + fixture.componentRef.setInput('originFolderId', originFolderId); + fixture.detectChanges(); + }; + + beforeEach(async () => { + foldersService = new FakeExperimentFoldersService(); + dashboard = { experiments: signal<Experiment[]>([]), getExperimentResult: () => EMPTY }; + + await TestBed.configureTestingModule({ + imports: [ExperimentsCompareComponent], + providers: [ + provideZonelessChangeDetection(), + provideEchartsCore({ echarts: () => import('echarts') }), + { provide: ExperimentFoldersService, useValue: foldersService }, + { provide: ExperimentsDashboardService, useValue: dashboard }, + { + provide: ExperimentLabelService, + useValue: { getLabelMap: async () => ({}), getEnumMaps: async () => ({}) }, + }, + { + provide: ExperimentStudioService, + useValue: { + backendAlgorithms: signal<Record<string, { label: string }>>({ ttest: { label: 'T-test' } }), + algorithmAssignableVariables: () => [], + selectedFilters: () => [], + }, + }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(ExperimentsCompareComponent); + component = fixture.componentInstance; + }); + + describe('header', () => { + it('counts the runs it holds instead of asking for a selection it has', () => { + show(['a', 'b', 'c']); + + expect(headline()).toBe('Comparing your selection · 3 runs'); + }); + + it('names the analysis set the compare came from and offers the way back', () => { + const folder = makeFolder('Q3 meta'); + show(['a', 'b'], folder.id); + + expect(headline()).toBe('Comparing Q3 meta · 2 runs'); + + const back = root().querySelector('.compare-back-btn') as HTMLButtonElement; + expect(back.textContent).toContain('Back to Q3 meta'); + + let emissions = 0; + component.backToFolder.subscribe(() => emissions++); + back.click(); + + expect(emissions).toBe(1); + }); + + it('withholds the way back from a hand-built set', () => { + show(['a', 'b']); + + expect(root().querySelector('.compare-back-btn')).toBeNull(); + }); + }); + + describe('results', () => { + it('hands each run its own parameters, so a t-test column names the groups it compared', () => { + dashboard.getExperimentResult = () => of({ + result: { t_stat: -3.9, df: 120, p: 0.0001, mean_diff: -1.2, ci_lower: -1.8, ci_upper: -0.6 }, + analysis: { algorithm: { parameters: { groupA: 'M', groupB: 'F' } } }, + }); + showRuns([experiment('a', 'ttest_independent')]); + fixture.detectChanges(); + + expect(root().querySelector('.compare-column app-key-figures')?.textContent).toContain('M − F'); + }); + }); + + describe('columns', () => { + it('gives every ungrouped run a column, grouped and ordered by its algorithm', () => { + showRuns([ + experiment('a', 'ttest'), + experiment('b', 'chisq'), + experiment('c', 'ttest'), + experiment('d', 'describe'), + ]); + + // A code with no label still shows its own name — an ungrouped run needs a home, not a special case. + expect(columnNames()).toEqual(['Run a', 'Run c', 'Run b', 'Run d']); + expect(algos()).toEqual(['T-test', 'T-test', 'chisq', 'describe']); + expect(setTags()).toEqual([null, null, null, null]); + }); + + it('puts the folder sets first and the leftovers after them', () => { + const folder = makeFolder('Stroke', ['a', 'b', 'c']); + foldersService.seedSet(folder.id, 'Age tests', ['a', 'b']); + + show(['a', 'b', 'c'], folder.id); + + expect(columnNames()).toEqual(['Run a', 'Run b', 'Run c']); + expect(setTags()).toEqual(['Age tests', 'Age tests', null]); + }); + + it('numbers runs once across the whole comparison', () => { + const folder = makeFolder('Stroke', ['a', 'b']); + foldersService.seedSet(folder.id, 'Age tests', ['b']); + + showRuns([experiment('a', 'ttest'), experiment('b', 'ttest')], folder.id); + fixture.detectChanges(); + + expect(columnNames()).toEqual(['Run b', 'Run a']); + expect(numbers()).toEqual(['1', '2']); + expect(setTags()).toEqual(['Age tests', null]); + }); + + it('withholds a column for a set whose runs are not being compared', () => { + const folder = makeFolder('Stroke', ['a']); + foldersService.seedSet(folder.id, 'Age tests', ['a']); + + show(['b'], folder.id); + + expect(columnNames()).toEqual(['Run b']); + expect(setTags()).toEqual([null]); + }); + + it('keeps the configuration closed per column and opens only the one it toggled', () => { + show(['a', 'b']); + + expect(columnsOf()[0].querySelector('.config-body')).toBeNull(); + + columnsOf()[0].querySelector<HTMLButtonElement>('.config-toggle')!.click(); + fixture.detectChanges(); + + expect(columnsOf()[0].querySelector('.config-body')).toBeTruthy(); + expect(columnsOf()[1].querySelector('.config-body')).toBeNull(); + }); + + it('renders each column its own result state', () => { + show(['a', 'b']); + + // The stubbed result never answers, so every column sits in its own loading state. + for (const column of columnsOf()) { + expect(column.querySelector('.compare-results .muted-text')!.textContent).toContain('Loading'); + } + }); + + it('groups by algorithm alone when runs arrive with no folder at all', () => { + const folder = makeFolder('Stroke', ['a']); + foldersService.seedSet(folder.id, 'Age tests', ['a']); + + showRuns([experiment('a', 'ttest'), experiment('b', 'chisq')]); + + expect(columnNames()).toEqual(['Run a', 'Run b']); + expect(setTags()).toEqual([null, null]); + expect(columnsOf().length).toBe(2); + }); + + it('reads the partition without editing it', () => { + const folder = makeFolder('Stroke', ['a', 'b']); + foldersService.seedSet(folder.id, 'Age tests', ['a']); + const before = JSON.stringify(foldersService.folderById(folder.id)); + + show(['a', 'b'], folder.id); + columnsOf()[0].querySelector<HTMLButtonElement>('.config-toggle')!.click(); + fixture.detectChanges(); + columnsOf()[1].querySelector<HTMLButtonElement>('.config-toggle')!.click(); + fixture.detectChanges(); + + expect(JSON.stringify(foldersService.folderById(folder.id))).toBe(before); + }); + }); +}); diff --git a/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.ts b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.ts index d72a2b3f7..5737df3a9 100644 --- a/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.ts +++ b/src/app/pages/experiments-dashboard/experiments-compare/experiments-compare.component.ts @@ -1,6 +1,6 @@ import { CommonModule } from '@angular/common'; -import { Component, computed, effect, input, signal } from '@angular/core'; -import { FormsModule } from '@angular/forms'; +import { ChangeDetectionStrategy, Component, computed, effect, input, output, signal, inject, OnDestroy } from '@angular/core'; +import { Subject, takeUntil } from 'rxjs'; import { Experiment } from '../../../models/experiments-dashboard.model'; import { ExperimentsDashboardService } from '../../../services/experiments-dashboard.service'; @@ -8,11 +8,17 @@ import { AlgorithmResultComponent } from '../../experiment-studio/algorithm-pane import { getOutputSchema } from '../../../core/algorithm-mappers'; import { ExperimentLabelService } from '../../../services/experiment-label.service'; import { EnumMaps } from '../../../core/algorithm-result-enum-mapper'; +import { enrichPcaResult, withLabels } from '../../../core/result-label.utils'; +import { ExperimentFoldersService } from '../../../services/experiment-folders.service'; +import { ExperimentStudioService } from '../../../services/experiment-studio.service'; +import { ExperimentStatusComponent } from '../shared/experiment-status/experiment-status.component'; interface CompareResultState { loading: boolean; error: string | null; result: any | null; + /** The run's algorithm parameters, so a two-group test can name its groups. */ + parameters?: Record<string, unknown> | null; } interface CompareItem { @@ -20,30 +26,50 @@ interface CompareItem { state: CompareResultState; } -interface CompareRow { - index: number; - items: CompareItem[]; +/** One column: a run, its comparison-wide number, and the set tag when a folder set claimed it. */ +interface CompareColumn { + item: CompareItem; + number: number; + setTag: string | null; } @Component({ selector: 'app-experiments-compare', - imports: [CommonModule, FormsModule, AlgorithmResultComponent], + imports: [CommonModule, AlgorithmResultComponent, ExperimentStatusComponent], templateUrl: './experiments-compare.component.html', - styleUrls: ['./experiments-compare.component.css'] + styleUrl: './experiments-compare.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class ExperimentsCompareComponent { +export class ExperimentsCompareComponent implements OnDestroy { + private dashboardService = inject(ExperimentsDashboardService); + private labelService = inject(ExperimentLabelService); + private foldersService = inject(ExperimentFoldersService); + private expStudio = inject(ExperimentStudioService); + private destroy$ = new Subject<void>(); + experiments = input<Experiment[]>([]); - // Layout (2 or 3) - readonly layoutCols = signal<2 | 3>(2); - selectedLayout: 2 | 3 = 2; + /** + * The folder this compare was opened from, or null for a hand-picked selection. The id is enough: + * sets are read from the same signal-backed service the canvas edits, so a set created after the + * handoff still sections the workspace, and this view never has to write to it. + */ + readonly originFolderId = input<string | null>(null); + readonly backToFolder = output<void>(); + + readonly originFolder = computed(() => this.foldersService.folderById(this.originFolderId())); + readonly originFolderName = computed(() => this.originFolder()?.name ?? null); + + /** Says what the workspace is holding, so the header stops asking for a selection it already has. */ + readonly compareHeadline = computed(() => { + const count = this.experiments().length; + const runs = count === 1 ? 'run' : 'runs'; + return `Comparing ${this.originFolderName() ?? 'your selection'} · ${count} ${runs}`; + }); // Results map: expId -> state private resultMap = signal<Record<string, CompareResultState>>({}); - // Row expand: rowIndex -> expanded? - private rowExpandedMap = signal<Record<number, boolean>>({}); - // Config collapse per exp private configExpandedMap = signal<Record<string, boolean>>({}); @@ -60,24 +86,41 @@ export class ExperimentsCompareComponent { })); }); - readonly experimentRows = computed<CompareRow[]>(() => { - const cols = this.layoutCols(); - const items = this.experimentsWithState(); - const rows: CompareRow[] = []; + /** + * The comparison's order, flattened to one column per run: user sets come first, in folder order + * — the order the canvas numbered them in — and everything nobody grouped follows as one group + * per algorithm label, in the order the runs appear. Describe and histogram are ordinary + * algorithm groups: an ungrouped run needs a home, not a special case. Numbering runs across the + * whole comparison, so "run 7" names one column whichever group it landed in. + */ + readonly columns = computed<CompareColumn[]>(() => { + const groups: { setTag: string | null; items: CompareItem[] }[] = []; + const claimed = new Set<string>(); + + for (const set of this.originFolder()?.sets ?? []) { + const members = this.experimentsWithState().filter((item) => set.experimentIds.includes(item.exp.id)); + if (!members.length) continue; // An empty set has nothing to show; its tag can wait. + members.forEach((member) => claimed.add(member.exp.id)); + groups.push({ setTag: set.name, items: members }); + } - for (let i = 0; i < items.length; i += cols) { - rows.push({ - index: rows.length, - items: items.slice(i, i + cols), - }); + const byAlgorithm = new Map<string, CompareItem[]>(); + for (const item of this.experimentsWithState()) { + if (claimed.has(item.exp.id)) continue; + const key = item.exp.algorithmName ?? 'unknown'; + const bucket = byAlgorithm.get(key); + if (bucket) bucket.push(item); + else byAlgorithm.set(key, [item]); } - return rows; + for (const items of byAlgorithm.values()) groups.push({ setTag: null, items }); + + let number = 0; + return groups.flatMap((group) => + group.items.map((item) => ({ item, number: (number += 1), setTag: group.setTag })), + ); }); - constructor( - private dashboardService: ExperimentsDashboardService, - private labelService: ExperimentLabelService - ) { + constructor() { effect( () => { const exps = this.experiments(); @@ -99,8 +142,7 @@ export class ExperimentsCompareComponent { void this.loadLabels(domain); void this.loadEnumMaps(domain); }); - }, - { allowSignalWrites: true } + } ); } @@ -123,23 +165,6 @@ export class ExperimentsCompareComponent { } - onLayoutChange(value: number) { - const cols: 2 | 3 = value === 3 ? 3 : 2; - this.selectedLayout = cols; - this.layoutCols.set(cols); - } - - isRowExpanded(index: number): boolean { - return this.rowExpandedMap()[index] ?? true; // default expanded - } - - toggleRow(index: number) { - this.rowExpandedMap.update((map) => ({ - ...map, - [index]: !(map[index] ?? true), - })); - } - isConfigExpanded(expId: string): boolean { return this.configExpandedMap()[expId] ?? false; // default collapsed } @@ -157,7 +182,7 @@ export class ExperimentsCompareComponent { [uuid]: { loading: true, error: null, result: null }, })); - this.dashboardService.getExperimentResult(uuid).subscribe({ + this.dashboardService.getExperimentResult(uuid).pipe(takeUntil(this.destroy$)).subscribe({ next: (res) => { this.resultMap.update((map) => ({ ...map, @@ -165,6 +190,7 @@ export class ExperimentsCompareComponent { loading: false, error: null, result: res?.result ?? res, + parameters: res?.analysis?.algorithm?.parameters ?? null, }, })); }, @@ -182,6 +208,11 @@ export class ExperimentsCompareComponent { }); } + ngOnDestroy(): void { + this.destroy$.next(); + this.destroy$.complete(); + } + getOutputSchemaFor(exp: Experiment) { return getOutputSchema(exp.algorithmName) ?? []; } @@ -192,8 +223,13 @@ export class ExperimentsCompareComponent { } private withLabels(codes: string[] | undefined | null, domain?: string | null) { - const map = this.getLabelMapForDomain(domain); - return (codes ?? []).map((code) => ({ code, label: map[code] ?? code })); + return withLabels(codes, this.getLabelMapForDomain(domain)); + } + + /** The algorithm's human label, so a column's algorithm line reads like the list row above it. */ + algorithmLabel(code: string | null | undefined): string { + if (!code) return 'Unknown algorithm'; + return this.expStudio.backendAlgorithms()[code]?.label || code; } getVariablesWithLabels(exp: Experiment) { @@ -231,16 +267,9 @@ export class ExperimentsCompareComponent { enrichResult(exp: Experiment, result: any): any { if (!result || !exp) return result; - - const algo = exp.algorithmName; - if (algo !== 'pca' && algo !== 'pca_with_transformation') return result; - - const allNames = [ - ...this.getVariablesWithLabels(exp).map(v => v.label), - ...this.getCovariatesWithLabels(exp).map(c => c.label), - ]; - - if (allNames.length > 0) return { ...result, variable_names: allNames }; - return result; + return enrichPcaResult(result, exp.algorithmName, [ + ...this.getVariablesWithLabels(exp), + ...this.getCovariatesWithLabels(exp), + ]); } } diff --git a/src/app/pages/experiments-dashboard/experiments-dashboard-routing.module.ts b/src/app/pages/experiments-dashboard/experiments-dashboard-routing.module.ts deleted file mode 100644 index 7b3f825a4..000000000 --- a/src/app/pages/experiments-dashboard/experiments-dashboard-routing.module.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { NgModule } from '@angular/core'; -import { RouterModule, Routes } from '@angular/router'; -import { ExperimentsDashboardComponent } from './experiments-dashboard.component'; - -const routes: Routes = [ - { path: 'experiments-dashboard', component: ExperimentsDashboardComponent} -]; - -@NgModule({ - imports: [RouterModule.forChild(routes)], - exports: [RouterModule] -}) -export class ExperimentsDashboardRoutingModule { } diff --git a/src/app/pages/experiments-dashboard/experiments-dashboard.component.css b/src/app/pages/experiments-dashboard/experiments-dashboard.component.css index 8613e756a..ab9bd12d1 100644 --- a/src/app/pages/experiments-dashboard/experiments-dashboard.component.css +++ b/src/app/pages/experiments-dashboard/experiments-dashboard.component.css @@ -1,499 +1,310 @@ +/* Fills the leftover the shell gives this route; the bar is sticky and already paid for. */ +:host { + display: flex; + flex-direction: column; + flex: 1 1 auto; + min-height: 0; +} + .experiments-dashboard { - padding: calc(var(--header-height) + 20px) 24px 20px; - background: radial-gradient(circle at top left, #f8fafc, #edf2f7); - color: var(--text-main); box-sizing: border-box; - overflow-x: hidden; - font-family: 'Inter', system-ui, -apple-system, sans-serif; + height: calc(100dvh - var(--header-height)); + min-height: 0; + display: flex; + flex-direction: column; + padding: 20px var(--studio-page-gutter) 24px; + background: transparent; /* Use global background */ + position: relative; } -.studio-inner { - margin: 0; - width: 100%; - max-width: none; +/* Compact page toolbar: title, total and the one primary action. */ +.dashboard-toolbar { display: flex; - flex-direction: column; + align-items: center; + justify-content: space-between; + gap: 16px; + flex-shrink: 0; } -.dashboard-greeting { - padding: 12px 10px 24px; - animation: slideDownIn 0.6s ease-out; +.toolbar-heading { + display: flex; + align-items: baseline; + gap: 10px; } -.greeting-content h1 { +.toolbar-heading h1 { margin: 0; - font-size: 1.8rem; + font-size: 1.35rem; font-weight: 800; - background: linear-gradient(135deg, #0f172a 30%, var(--primary-color) 100%); - -webkit-background-clip: text; - background-clip: text; - -webkit-text-fill-color: transparent; - letter-spacing: -0.02em; + line-height: 1.3; + color: var(--text-main); + letter-spacing: 0.01em; } -.greeting-content p { - margin: 4px 0 0; - font-size: 0.95rem; - color: #64748b; - font-weight: 500; +.toolbar-count { + font-size: 0.8125rem; + color: var(--text-muted); + font-weight: 600; } -@keyframes slideDownIn { - from { - opacity: 0; - transform: translateY(-10px); - } - - to { - opacity: 1; - transform: translateY(0); - } +.dashboard-error-banner, +.pathology-warning-banner { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 10px; + margin: 0; + padding: 10px 14px; + border-radius: var(--radius-lg); } -/* Sidebar structure mirrored from Studio */ -.studio-layout { - position: relative; +.dashboard-error-banner { + border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); + background: color-mix(in srgb, var(--danger) 12%, white); + color: #7f1d1d; + box-shadow: none; } -.studio-sidenav { - position: fixed; - top: 52%; - left: max(16px, calc(50% - 700px - 240px)); - transform: translateY(-50%); - width: 210px; - height: fit-content; - display: flex; - flex-direction: column; - gap: 16px; - padding: 20px 14px; - background: rgba(255, 255, 255, 0.75); - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - border: 1px solid rgba(148, 163, 184, 0.25); - border-radius: 20px; - box-shadow: 0 8px 32px rgba(15, 23, 42, 0.08); - max-height: calc(100vh - 120px); - overflow: hidden; +.pathology-warning-banner { + position: relative; + width: 100%; z-index: 10; + border: 1px solid color-mix(in srgb, var(--accent-color) 35%, transparent); + background: color-mix(in srgb, var(--accent-color) 12%, white); + color: #7c2d12; + box-shadow: none; + border-radius: var(--radius-lg); + box-sizing: border-box; + padding: 12px 14px; } -.studio-rail-context { - display: flex; - flex-direction: column; - gap: 6px; - padding-left: 6px; - margin-bottom: 0; -} - -.studio-rail-context-title { - font-size: 1rem; - font-weight: 700; - color: #0f172a; -} +/* Removed bannerPulse to reduce 'too much' feel */ -.studio-rail-context-link { - font-size: 0.75rem; - color: var(--text-muted); - text-decoration: none; - padding: 8px 12px; - display: inline-flex; +.dashboard-error-banner .banner-content { + display: flex; align-items: center; gap: 8px; - background: #f1f5f9; - border: 1px solid var(--border-color); - border-radius: var(--radius-md); - margin-top: 8px; font-weight: 600; - transition: all 0.2s ease; } -.studio-rail-context-link:hover, -.studio-rail-context-link.active { - background: white; - color: var(--primary-color); - border-color: var(--primary-color); - box-shadow: var(--shadow-sm); +.pathology-warning-banner .banner-content { + display: flex; + align-items: flex-start; + gap: 8px; } -.studio-rail-scroll { - display: flex; - flex-direction: column; - gap: 14px; - overflow-y: auto; +.pathology-warning-banner .fa-exclamation-triangle { + color: #b45309; + font-size: 1rem; + margin-top: 2px; } -.studio-rail-section { +.warning-copy { display: flex; flex-direction: column; - gap: 6px; - padding-top: 10px; - border-top: 1px solid rgba(148, 163, 184, 0.2); + gap: 4px; } -.studio-rail-section-title { - font-size: 0.7rem; - text-transform: uppercase; - letter-spacing: 0.12em; - color: rgba(100, 116, 139, 0.9); +.warning-title { font-weight: 700; - padding-left: 6px; - padding-bottom: 2px; } -.studio-rail-item { - display: grid; - grid-template-columns: 18px 1fr; - gap: 10px; - align-items: center; - text-decoration: none; - color: #0f172a; - font-size: 0.88rem; - font-weight: 600; - padding: 10px; - border-radius: 12px; - transition: all 0.2s ease; - cursor: pointer; +.warning-copy p { + margin: 0; + line-height: 1.5; } -.studio-rail-item.clickable:hover { - background: rgba(2, 109, 109, 0.08); - color: var(--primary-color); +.warning-email { + color: #9a3412; + font-weight: 700; + white-space: nowrap; } -.studio-rail-item.is-active { - background: #f0f9ff; - color: var(--primary-color); - border-left: 3px solid var(--primary-color); - border-radius: 0 var(--radius-md) var(--radius-md) 0; +.banner-actions { + display: inline-flex; + align-items: center; + gap: 4px; + flex-shrink: 0; } -.studio-rail-item.is-active .studio-rail-icon { - color: var(--primary-color); +.banner-retry { + border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); + border-radius: var(--radius-md); + background: #ffffff; + color: #7f1d1d; + padding: 5px 10px; + font: inherit; + font-size: 0.76rem; + font-weight: 700; + cursor: pointer; } -.studio-rail-icon { - font-size: 0.9rem; - color: rgba(71, 85, 105, 0.75); +.banner-retry:hover { + background: color-mix(in srgb, var(--danger) 10%, white); } -.studio-main { - display: flex; - flex-direction: column; - gap: 20px; - width: 100%; - margin: 0; - padding-left: 0; - flex: 1; +.banner-close { + border: none; + background: transparent; + color: inherit; + font-size: 22px; + line-height: 1; + cursor: pointer; + padding: 4px 6px; + border-radius: 8px; + transition: background-color 0.15s ease, color 0.15s ease; } -/* COMMAND BAR */ -.command-bar { - display: flex; - justify-content: space-between; - align-items: center; - gap: 24px; - background: white; - padding: 14px 20px; - border-radius: var(--radius-md); - border: 1px solid var(--border-color); - box-shadow: var(--shadow-sm); +.banner-close:hover { + background: rgba(127, 29, 29, 0.08); } -.kpi-group { - display: flex; - gap: 16px; - flex: 1; - justify-content: center; +.banner-close:focus-visible { + background: rgba(127, 29, 29, 0.08); + outline: 2px solid var(--primary-color); + outline-offset: 2px; } -.kpi-card { - padding: 8px 16px; - background: #f1f5f9; - border-radius: 12px; - display: flex; - flex-direction: column; - min-width: 120px; -} +@media (max-width: 768px) { + .pathology-warning-banner { + left: 12px; + right: 12px; + } -.kpi-label { - font-size: 0.65rem; - text-transform: uppercase; + .pathology-warning-banner .warning-title { font-weight: 800; - color: var(--text-muted); - letter-spacing: 0.05em; + margin-bottom: 2px; + color: #9a3412; + font-size: 0.95rem; } - -.kpi-value { - font-size: 1.25rem; - font-weight: 900; - color: var(--primary-color); } -.kpi-card.success .kpi-value { - color: #10b981; +.studio-main { + display: flex; + flex-direction: column; + gap: 16px; + width: 100%; + max-width: var(--studio-inner-max, 1320px); + min-height: 0; + margin: 0 auto; + padding-left: 0; + flex: 1 1 auto; } /* REBALANCED DASHBOARD LAYOUT */ .command-center-layout { + flex: 1 1 auto; + min-height: 0; display: grid; - grid-template-columns: minmax(320px, 30%) 1fr; - gap: 24px; - align-items: stretch; - min-height: calc(100vh - 180px); - padding-bottom: 20px; -} - -.header-with-actions { - display: flex !important; - justify-content: space-between !important; - align-items: center; -} - -.workspace-pane-header { - padding: 18px 20px !important; - background: rgba(248, 250, 252, 0.5) !important; - border-bottom: 1px solid var(--border-color) !important; - display: flex; - flex-direction: row; - justify-content: space-between; - align-items: center; + grid-template-columns: minmax(320px, 30%) minmax(0, 1fr); gap: 16px; -} - -.workspace-label-group { - display: flex; - flex-direction: column; - gap: 0; - flex-shrink: 0; -} - -.pane-actions { - display: flex; - gap: 8px; - flex-wrap: wrap; - justify-content: flex-end; -} - -@media (min-width: 1400px) { - .pane-actions { - width: auto; - } + align-items: stretch; + overflow: hidden; } .btn-new-experiment.primary { display: inline-flex; align-items: center; - gap: 6px; - padding: 6px 12px; - border-radius: 8px; - font-size: 0.78rem; - font-weight: 700; - color: #ffffff; + justify-content: center; + gap: 8px; + height: 36px; + padding: 0 16px; + border: none; + border-radius: 999px; background: var(--primary-color); - border: 1px solid var(--primary-dark); - box-shadow: 0 4px 10px rgba(43, 51, 233, 0.1); - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); + color: #ffffff; + font: inherit; + font-size: 0.85rem; + font-weight: 600; + cursor: pointer; + white-space: nowrap; + box-shadow: 0 1px 2px rgba(43, 51, 233, 0.18); + transition: background-color 0.15s ease, box-shadow 0.15s ease; +} + +.btn-new-experiment.primary i { + font-size: 0.75rem; } .btn-new-experiment.primary:hover { - transform: translateY(-1px); - box-shadow: 0 16px 30px rgba(2, 128, 144, 0.3); background: var(--primary-dark); + box-shadow: var(--shadow-sm); } .side-pane { - background: rgba(255, 255, 255, 0.65); - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - border-radius: 20px; - border: 1px solid rgba(148, 163, 184, 0.2); + min-height: 0; + height: 100%; + background: var(--card-bg); + border-radius: var(--radius-lg); + border: 1px solid var(--border-color); display: flex; flex-direction: column; overflow: visible; - box-shadow: 0 4px 12px rgba(15, 23, 42, 0.03); + box-shadow: var(--studio-card-shadow); + z-index: 10; } .main-pane { - background: rgba(255, 255, 255, 0.8); - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - border-radius: 24px; - border: 1px solid rgba(148, 163, 184, 0.25); - overflow-y: auto; - box-shadow: 0 10px 40px rgba(15, 23, 42, 0.04); -} - -.pane-header { - padding: 12px 16px; - background: #f8fafc; - border-bottom: 1px solid var(--border-color); - display: flex; - justify-content: space-between; - align-items: center; -} - -.pane-header h2 { - margin: 0; - font-size: 0.75rem; - font-weight: 800; - text-transform: uppercase; - letter-spacing: 0.12em; - color: var(--text-muted); - white-space: nowrap; -} - -.pane-content { - padding: 12px; - flex: 1; + min-height: 0; + height: 100%; + background: var(--card-bg); + border-radius: var(--radius-lg); + border: 1px solid var(--border-color); overflow-y: auto; - max-height: calc(100vh - 260px); -} - -.meta-section { - margin-bottom: 20px; -} - -.meta-section label { - display: block; - font-size: 0.65rem; - font-weight: 800; - color: var(--text-muted); - margin-bottom: 8px; -} - -.meta-section p { - font-size: 0.85rem; - line-height: 1.5; - color: var(--text-main); -} - -/* Search */ - -.search-wrapper { + box-shadow: var(--studio-card-shadow); + z-index: 10; position: relative; - display: flex; - align-items: center; - background: #ffffff; - padding: 0 12px; - border-radius: 999px; - box-shadow: 0 10px 30px rgba(15, 23, 42, 0.08); - min-width: 260px; -} - -.search-wrapper i { - font-size: 0.9rem; - color: #94a3b8; } -.search-input { - border: none; - outline: none; - padding: 10px 8px 10px 10px; - width: 100%; - font-size: 0.9rem; - background: transparent; - color: #0f172a; +/* The compare workspace holds the pane's full height so the columns strip — not the pane — + scrolls tall results, which is what lets the sticky column headers pin. On the mobile + layout the pane is auto-height, so this resolves to natural flow. */ +.compare-workspace { + height: 100%; } -.search-input::placeholder { - color: #cbd5e1; -} - -/* New experiment button */ -.btn-new-experiment { +.btn-action { display: inline-flex; align-items: center; gap: 8px; - border: none; - outline: none; + padding: 8px 16px; + border-radius: 999px; + font-size: 0.82rem; + font-weight: 600; cursor: pointer; - border-radius: 12px; - padding: 10px 20px; - font-size: 0.85rem; - font-weight: 700; - color: #ffffff; - background: var(--primary-color); - box-shadow: var(--shadow-md); - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); + transition: all 0.15s ease; + border: 1px solid transparent; } -.btn-new-experiment i { - font-size: 0.85rem; +.btn-back { + background: var(--bg-neutral); + border-color: var(--border-color); + color: var(--text-main); } -.btn-new-experiment:hover { - background: var(--primary-dark); - transform: translateY(-2px); - box-shadow: var(--shadow-lg); +.btn-back:hover { + background: #e0f2fe; + border-color: var(--primary-color); + color: var(--primary-color); } -.btn-new-experiment:active { - transform: translateY(0); - box-shadow: 0 6px 16px rgba(15, 23, 42, 0.25); -} +@media (max-width: 768px) { -/* Main layout */ -.dashboard-layout { - display: grid; - grid-template-columns: minmax(280px, 340px) minmax(0, 1fr); - gap: 24px; - align-items: flex-start; + .btn-action { + width: 100%; + justify-content: center; + } } -/* Left panel – experiments list */ -.experiments-panel { - background: var(--card-bg); - border-radius: var(--radius-lg); - padding: 0; - box-shadow: var(--shadow-md); - border: 1px solid var(--border-color); +.pane-content { + flex: 1 1 auto; + min-height: 0; display: flex; flex-direction: column; - min-height: 480px; overflow: hidden; + max-height: none; } -.panel-header { - display: flex; - justify-content: space-between; - align-items: center; - padding: 12px 16px; - background: #f8fafc; - color: var(--text-main); - border-bottom: 1px solid var(--border-color); - border-radius: var(--radius-lg) var(--radius-lg) 0 0; -} - -.panel-header h2 { - margin: 0; - font-size: 0.85rem; - text-transform: uppercase; - letter-spacing: 0.05em; - font-weight: 700; - color: var(--text-muted); -} - -.panel-subtitle { - display: none; - /* Hide subtitle to match clean studio header */ -} - -.experiments-list-section { - margin-top: 8px; - flex: 1; - overflow: hidden; -} - -/* Right panel – details */ -.details-panel { - background: var(--card-bg); - border-radius: var(--radius-lg); - padding: 0; - box-shadow: var(--shadow-lg); - border: 1px solid var(--border-color); - min-height: 520px; - overflow: hidden; -} /* Modal */ .modal-overlay { @@ -508,13 +319,15 @@ top: 50%; left: 50%; transform: translate(-50%, -50%); - background: #ffffff; - padding: 22px 24px 20px; - border-radius: 18px; + box-sizing: border-box; width: 90%; max-width: 420px; + padding: 20px; + background: var(--card-bg); + border: 1px solid var(--border-color); + border-radius: var(--radius-lg, 12px); + box-shadow: var(--shadow-lg); z-index: 1100; - box-shadow: 0 20px 50px rgba(15, 23, 42, 0.35); animation: fadeIn 0.2s ease-out; } @@ -527,14 +340,15 @@ .modal-header h3 { margin: 0; font-size: 1rem; - font-weight: 600; - color: #111827; + font-weight: 700; + color: var(--text-main); } .modal-body { - margin-top: 16px; + margin-top: 14px; font-size: 0.9rem; - color: #4b5563; + line-height: 1.6; + color: var(--text-muted); } .close-btn { @@ -542,12 +356,12 @@ border: none; font-size: 1.5rem; cursor: pointer; - color: #9ca3af; + color: var(--text-muted); line-height: 1; } .close-btn:hover { - color: #ef4444; + color: var(--danger); } .dialog-actions { @@ -557,40 +371,12 @@ margin-top: 22px; } -.btn-cancel, -.btn-create { - padding: 8px 16px; - border-radius: 999px; - border: none; - cursor: pointer; - font-size: 0.85rem; - font-weight: 500; -} - -.btn-cancel { - background-color: #e5e7eb; - color: #374151; -} - -.btn-cancel:hover { - background-color: #d1d5db; -} - -.btn-create { - background-color: #4db8c9; - color: #ffffff; -} - -.btn-create:hover { - background-color: var(--primary-color); -} - .btn-create.danger { - background-color: #ef4444; + background: var(--danger); } .btn-create.danger:hover { - background-color: #b91c1c; + background: #b91c1c; } @keyframes fadeIn { @@ -605,132 +391,168 @@ } } -/* Responsive */ +/* Responsive: a master/detail flow instead of two stacked panes. */ @media (max-width: 1100px) { .experiments-dashboard { - padding: 16px; + height: auto; + min-height: calc(100dvh - var(--header-height)); + padding: 14px 16px 18px; } - .dashboard-greeting h1 { - font-size: 1.5rem; + .dashboard-toolbar { + align-items: flex-start; + } + + .toolbar-heading h1 { + font-size: 1.2rem; } .command-center-layout { grid-template-columns: 1fr; height: auto; - gap: 16px; + min-height: 0; + overflow: visible; + gap: 12px; } - .side-pane { - max-height: 500px; + .command-center-layout:not(.command-center-layout--detail) .main-pane { + display: none; } + .command-center-layout--detail .side-pane { + display: none; + } + + .side-pane, .main-pane { - min-height: 600px; + height: auto; + min-height: 0; + } + + .pane-content { + max-height: none; + overflow: visible; + } + + + .mobile-back-btn { + display: inline-flex; + } + + .main-pane { + min-height: calc(100dvh - var(--header-height) - 86px); } } -@media (max-width: 960px) { - .dashboard-header { +@media (max-width: 640px) { + .dashboard-toolbar { flex-direction: column; align-items: stretch; } - .header-actions { - justify-content: space-between; + .btn-new-experiment.primary { + justify-content: center; } - .experiments-panel, - .details-panel { - min-height: auto; + .side-pane, + .main-pane { + border-radius: 10px; } } -.panel-header { +.mobile-back-btn { + display: none; + align-items: center; + gap: 8px; + margin: 12px 12px 0; + padding: 8px 14px; + border: 1px solid rgba(127, 156, 232, 0.38); + border-radius: 999px; + background: rgba(255, 255, 255, 0.96); + color: #334155; + font: inherit; + font-size: 0.82rem; + font-weight: 600; + cursor: pointer; + transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease; +} + +.mobile-back-btn i { + color: var(--primary-color); + font-size: 0.78rem; +} + +.mobile-back-btn:hover { + border-color: rgba(43, 51, 233, 0.45); + background: rgba(43, 51, 233, 0.06); + color: var(--primary-color); +} + +.compare-diff-header { display: flex; - flex-direction: column; - justify-content: space-between; align-items: flex-start; + justify-content: space-between; gap: 12px; - padding: 20px 24px; + margin-bottom: 12px; } -.compare-mode-btn { - border-radius: 8px; - border: 1px solid rgba(148, 163, 184, 0.5); - background: white; - padding: 6px 12px; - font-size: 0.78rem; - display: inline-flex; - align-items: center; - gap: 6px; - cursor: pointer; - color: #475569; - font-weight: 700; - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); - position: relative; +.compare-diff-header h2 { + margin: 2px 0 0; + font-size: 1.15rem; + color: var(--text-main); } -.compare-mode-btn i { - font-size: 0.9rem; +.compare-diff-kicker { + font-size: 0.75rem; + font-weight: 700; + letter-spacing: 0.04em; + text-transform: uppercase; + color: var(--text-muted); } -.btn-label { - letter-spacing: 0.02em; +.compare-diff { + display: grid; + gap: 8px 12px; + align-items: start; } -.count-badge { - background: var(--primary-color); - color: white; - font-size: 0.6rem; - font-weight: 800; - min-width: 15px; - height: 15px; - padding: 0 4px; - border-radius: 999px; - display: flex; - align-items: center; - justify-content: center; +.compare-diff-head, +.compare-diff-key, +.compare-diff-val { + min-width: 0; + overflow-wrap: anywhere; } -.compare-mode-btn:hover { - background: #ffffff; - border-color: var(--primary-color); - color: var(--primary-color); - transform: translateY(-1px); - box-shadow: 0 4px 12px rgba(2, 128, 144, 0.1); +.compare-diff-name { + display: block; + font-weight: 600; + color: var(--text-main); } -.compare-mode-btn.active { - background: var(--primary-color); - border-color: var(--primary-dark); - color: white; - box-shadow: 0 8px 20px rgba(43, 51, 233, 0.25); +.compare-diff-algo, +.compare-diff-key, +.compare-diff-note, +.compare-short p { + color: var(--text-muted); + font-size: 0.82rem; } -.compare-mode-btn.has-selections:not(.active) { - border-color: var(--primary-color); - background: #f0fdfa; - color: #0d9488; +.compare-diff-val { + color: var(--text-main); + font-weight: 600; } -.compare-placeholder-card { - border-radius: 20px; - background: linear-gradient(135deg, #f1f5f9, #e0f2fe); - padding: 18px 20px; - box-shadow: inset 0 0 0 1px rgba(148, 163, 184, 0.4); - margin-bottom: 16px; +.compare-diff-val--same { + color: var(--text-muted); + font-weight: 400; } -.compare-placeholder-card h2 { - margin: 0 0 6px; - font-size: 1.1rem; - font-weight: 600; - color: #020617; +.compare-diff-note { + margin: 10px 0 16px; } -.compare-placeholder-card p { - margin: 0; - font-size: 0.86rem; - color: #4b5563; -} \ No newline at end of file +.compare-short-actions { + display: flex; + flex-wrap: wrap; + gap: 8px; +} diff --git a/src/app/pages/experiments-dashboard/experiments-dashboard.component.html b/src/app/pages/experiments-dashboard/experiments-dashboard.component.html index f8df8ac78..fd32dac46 100644 --- a/src/app/pages/experiments-dashboard/experiments-dashboard.component.html +++ b/src/app/pages/experiments-dashboard/experiments-dashboard.component.html @@ -1,81 +1,134 @@ <div class="experiments-dashboard full-bleed"> - @if (errorMessage()) { - <div class="dashboard-error-banner" role="alert" aria-live="assertive" aria-atomic="true"> - <div class="banner-content"> - <i class="fas fa-exclamation-triangle"></i> - <span>{{ errorMessage() }}</span> - </div> - <button type="button" class="banner-close" (click)="dismissError()"> - × - </button> - </div> - } <div class="studio-main full-width"> - <!-- TOP: Greeting Header --> - <header class="dashboard-greeting"> - <div class="greeting-content"> - <h1>Welcome back, {{ greetingName() }}!</h1> - <p>You have {{ experimentsService.experiments().length }} experiments in your history.</p> + @if (visiblePathologyAccessWarning(); as warning) { + <div class="pathology-warning-banner" role="status" aria-live="polite" aria-atomic="true"> + <div class="banner-content"> + <i class="fas fa-exclamation-triangle"></i> + <div class="warning-copy"> + <div class="warning-title">{{ warning.title }}</div> + <p> + {{ warning.message }} + Please contact us at + <a class="warning-email" href="mailto:mip@chuv.ch">mip@chuv.ch</a>. + </p> + </div> + </div> + <button type="button" class="banner-close" (click)="dismissPathologyWarning()" aria-label="Dismiss warning"> + × + </button> + </div> + } + + @if (errorMessage()) { + <div class="dashboard-error-banner" role="alert" aria-live="assertive" aria-atomic="true"> + <div class="banner-content"> + <i class="fas fa-exclamation-triangle"></i> + <span>{{ errorMessage() }}</span> </div> + <div class="banner-actions"> + <button type="button" class="banner-retry" (click)="experimentsService.retryLastRequest()"> + Retry + </button> + <button type="button" class="banner-close" (click)="dismissError()"> + × + </button> + </div> + </div> + } + + <!-- TOP: Compact page toolbar --> + <header class="dashboard-toolbar"> + <div class="toolbar-heading"> + <h1>Experiments</h1> + <span class="toolbar-count">{{ experimentsService.totalExperiments() }}</span> + </div> + + <button class="btn-new-experiment primary" (click)="goToNewExperiment()" title="New Experiment" + data-guide="dashboard-new"> + <i class="fas fa-plus"></i> + <span>New experiment</span> + </button> </header> <!-- MAIN TRIPLE PANE CONTENT (now integrated actions) --> - <div class="command-center-layout"> + <div class="command-center-layout" [class.command-center-layout--detail]="mobileDetailOpen()" data-guide="dashboard-overview"> <!-- LEFT: NAVIGATION & SELECTION --> - <aside class="side-pane experiments-pane"> - <div class="pane-header workspace-pane-header"> - <div class="workspace-label-group"> - <h2>Research Workspace</h2> - </div> - - <div class="pane-actions"> - <!-- Compare Toggle --> - <button class="compare-mode-btn" [class.active]="compareMode()" - [class.has-selections]="compareIds().length >= 1" (click)="toggleCompareMode()" title="Comparison Mode"> - <i class="fas fa-columns"></i> - <span class="btn-label">{{ compareMode() ? 'Exit' : 'Compare' }}</span> - @if (compareIds().length > 0) { - <span class="count-badge">{{ compareIds().length }}</span> - } - </button> - - <!-- New Experiment --> - <button class="btn-new-experiment primary" (click)="goToNewExperiment()" title="New Experiment"> - <i class="fas fa-plus"></i> - New - </button> - </div> - </div> + <aside class="side-pane experiments-pane" data-guide="dashboard-workspace"> <div class="pane-content"> <app-experiments-list [selectedExperimentId]="selectedExperiment()?.id ?? null" - [currentUserEmail]="currentUserEmail()" [compareIds]="compareIds()" [compareMode]="compareMode()" - [initialOnlyMine]="!hasDeepLink()" (experimentSelected)="onExperimentSelected($event)" - (editRequested)="onEditExperiment($event)" (deleteRequested)="onDeleteFromList($event)" /> + [selectedFolderId]="selectedFolderId()" [currentUserEmail]="currentUserEmail()" + [compareIds]="compareIds()" [initialOnlyMine]="!hasDeepLink()" + (experimentSelected)="onExperimentSelected($event)" (folderSelected)="onFolderSelected($event)" + (clearCompareSelection)="clearCompareSelection()" + (compareToggled)="onCompareToggled($event)" (compareOpened)="openCompare()" /> </div> </aside> <!-- CENTER: MAIN WORKSPACE --> - <main class="main-pane"> - @if (!compareMode()) { + <section class="main-pane" aria-label="Experiment workbench"> + @if (mobileDetailOpen()) { + <button type="button" class="mobile-back-btn" (click)="closeMobileDetail()"> + <i class="fas fa-arrow-left" aria-hidden="true"></i> + Back to experiments + </button> + } + + @if (selectedFolderId(); as folderId) { + <app-experiment-folder [folderId]="folderId" (back)="onFolderSelected(null)" + (openExperiment)="onExperimentSelected($event)" (compareRequested)="onFolderCompare($event)" /> + } @else if (!compareMode()) { <app-experiment-details [selectedExperiment]="selectedExperiment()" [currentUserEmail]="currentUserEmail()" (run)="onRunExperiment($event)" (deleteExperiment)="onDeleteRequested()" (nameUpdated)="onNameUpdated($event)" /> } @else { - <div class="compare-workspace"> - @if (experimentsForCompare().length < 2) { <div class="compare-placeholder"> - <div class="placeholder-icon"> - <i class="fas fa-layer-group"></i> + <div class="compare-workspace" data-guide="dashboard-compare-workspace"> + @if (experimentsForCompare().length < 2) { + <div class="compare-short"> + <p>Select at least two experiments.</p> + <div class="compare-short-actions"> + @if (compareOriginFolderName(); as originName) { + <button type="button" class="btn-action btn-back" (click)="onBackToOriginFolder()"> + <i class="fas fa-arrow-left" aria-hidden="true"></i> + Back to {{ originName }} + </button> + } + <button type="button" class="btn-action" (click)="closeCompare()">Done</button> + </div> + </div> + } @else { + <header class="compare-diff-header"> + <div> + <span class="compare-diff-kicker">Compare</span> + <h2>{{ experimentsForCompare().length }} experiments side by side</h2> + </div> + <button type="button" class="btn-action" (click)="closeCompare()">Done</button> + </header> + + <div class="compare-diff" + [style.grid-template-columns]="'140px repeat(' + experimentsForCompare().length + ', minmax(0, 1fr))'"> + <span></span> + @for (experiment of experimentsForCompare(); track experiment.id) { + <div class="compare-diff-head"> + <span class="compare-diff-name">{{ experiment.name || "Untitled Investigation" }}</span> + <span class="compare-diff-algo">{{ getAlgorithmLabel(experiment.algorithmName) }}</span> </div> - <h3>Comparison Workspace</h3> - <p>Select at least two experiments from the left panel to compare their results side by side.</p> + } + @for (row of compareDiffRows(); track row.k) { + <span class="compare-diff-key">{{ row.k }}</span> + @for (value of row.vals; track $index) { + <span class="compare-diff-val" [class.compare-diff-val--same]="row.same">{{ value }}</span> + } + } + </div> + <p class="compare-diff-note">Rows that differ are in ink, and rows that match are muted.</p> + + <app-experiments-compare [experiments]="experimentsForCompare()" [originFolderId]="compareOriginFolderId()" + (backToFolder)="onBackToOriginFolder()" /> + } </div> - } @else { - <app-experiments-compare [experiments]="experimentsForCompare()" /> } - </div> - } - </main> + </section> </div> @@ -93,7 +146,7 @@ <h3>Delete experiment?</h3> <div class="modal-body"> <p> - Permanently delete this experiment? + Permanently delete “{{ deleteTargetName() }}”? This action cannot be undone. </p> </div> @@ -112,3 +165,5 @@ <h3>Delete experiment?</h3> } </div> </div> + +<app-experiments-dashboard-guide></app-experiments-dashboard-guide> diff --git a/src/app/pages/experiments-dashboard/experiments-dashboard.component.spec.ts b/src/app/pages/experiments-dashboard/experiments-dashboard.component.spec.ts new file mode 100644 index 000000000..e666d01e1 --- /dev/null +++ b/src/app/pages/experiments-dashboard/experiments-dashboard.component.spec.ts @@ -0,0 +1,307 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { provideZonelessChangeDetection, signal } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { ActivatedRoute, provideRouter } from '@angular/router'; +import { EMPTY, of } from 'rxjs'; + +import { Experiment } from '../../models/experiments-dashboard.model'; +import { AuthService } from '../../services/auth.service'; +import { ExperimentStudioService } from '../../services/experiment-studio.service'; +import { ExperimentsDashboardService } from '../../services/experiments-dashboard.service'; +import { ExperimentFoldersService } from '../../services/experiment-folders.service'; +import { FakeExperimentFoldersService } from './experiment-folders.testing'; +import { ExperimentsDashboardComponent } from './experiments-dashboard.component'; + +const apiUrl = '/services/experiments'; + +const experiment = (id: string, overrides: Partial<Experiment> = {}): Experiment => ({ + id, + name: `Run ${id}`, + dateCreated: new Date('2026-01-01T00:00:00.000Z'), + status: 'success', + algorithmName: 'mock_anova', + author: 'Marie Curie', + authorEmail: 'marie.curie@chuv.ch', + isShared: false, + ...overrides, +}); + +const backendExperiment = (uuid: string) => ({ + uuid, + name: `Run ${uuid}`, + created: '2026-01-01T00:00:00.000Z', + finished: '2026-01-01T00:01:00.000Z', + shared: false, + viewed: false, + status: 'success', + description: '', + analysis: { algorithm: { name: 'mock_anova', y: ['AGE'], x: [] } }, + createdBy: { + username: 'mcurie', + fullname: 'Marie Curie', + email: 'marie.curie@chuv.ch', + subjectId: 'orcid-0000-0002', + agreeNDA: true, + }, +}); + +/** + * The centre-pane wiring around the folder canvas: how a folder reaches compare mode, and + * the one event allowed to forget a member. The component is built in an injection context + * rather than from its template — the pane children have their own specs, and this one is + * about which signals a handoff sets. + */ +describe('ExperimentsDashboardComponent folders', () => { + let component: ExperimentsDashboardComponent; + let dashboardService: ExperimentsDashboardService; + let foldersService: FakeExperimentFoldersService; + let httpMock: HttpTestingController; + + /** Seeded, not clicked: this spec is about which signals a handoff sets, not about the canvas. */ + const openFolder = (memberIds: string[]) => foldersService.seedFolder('Q3 meta', memberIds); + + beforeEach(async () => { + foldersService = new FakeExperimentFoldersService(); + + await TestBed.configureTestingModule({ + imports: [ExperimentsDashboardComponent, HttpClientTestingModule], + providers: [ + provideZonelessChangeDetection(), + provideRouter([]), + { provide: ActivatedRoute, useValue: { queryParamMap: EMPTY } }, + { + provide: AuthService, + useValue: { + authState: signal({ + status: 'authenticated', + // Folders are read for the username; sharing still asks for the email. + user: { username: 'mcurie', email: 'marie.curie@chuv.ch' }, + }), + }, + }, + { + provide: ExperimentStudioService, + useValue: { + pathologyAccessWarning: signal(null), + getAllDataModels: () => of([]), + backendAlgorithms: signal({ mock_anova: { label: 'Analysis of Variance' } }), + }, + }, + { provide: ExperimentFoldersService, useValue: foldersService }, + ], + }); + + component = TestBed.runInInjectionContext(() => new ExperimentsDashboardComponent())!; + dashboardService = TestBed.inject(ExperimentsDashboardService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + }); + + it('fetches off-page members before handing the folder to compare mode', () => { + // The list is server-paginated: only "a" is on the loaded page. + dashboardService.experiments.set([experiment('a')]); + const folder = openFolder(['a', 'b']); + TestBed.flushEffects(); + + component.onFolderSelected(folder.id); + expect(component.selectedFolderId()).toBe(folder.id); + expect(component.compareMode()).toBe(false); + + component.onFolderCompare(['a', 'b']); + expect(component.compareMode()).toBe(false); + + httpMock.expectOne(`${apiUrl}/b`).flush(backendExperiment('b')); + + // The off-page member had to join the loaded list: the workspace reads that page only. + expect(component.compareIds()).toEqual(['a', 'b']); + expect(component.compareMode()).toBe(true); + expect(component.selectedFolderId()).toBeNull(); + expect(dashboardService.experiments().map((exp) => exp.id).sort()).toEqual(['a', 'b']); + expect(component.experimentsForCompare().map((exp) => exp.id).sort()).toEqual(['a', 'b']); + }); + + it('leaves compare mode when a folder takes the centre pane', () => { + const folder = openFolder(['a']); + component.compareMode.set(true); + component.compareIds.set(['a']); + + component.onFolderSelected(folder.id); + + expect(component.compareMode()).toBe(false); + expect(component.compareIds()).toEqual([]); + expect(component.selectedFolderId()).toBe(folder.id); + }); + + it('gives up the folder canvas when compare is turned on by hand', () => { + // The template checks the folder branch first, so a folder left open would keep the pane + // and the Compare button would flip to Exit over an unchanged canvas. + dashboardService.experiments.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + component.onFolderSelected(folder.id); + + component.compareIds.set(['a', 'b']); + component.openCompare(); + + expect(component.compareMode()).toBe(true); + expect(component.selectedFolderId()).toBeNull(); + }); + + it('keeps the canvas order in the compare workspace', () => { + // The page lists them a, b; the canvas numbered them 1 = b, 2 = a. + dashboardService.experiments.set([experiment('a'), experiment('b')]); + const folder = openFolder(['b', 'a']); + TestBed.flushEffects(); + + component.onFolderSelected(folder.id); + component.onFolderCompare(['b', 'a']); + + expect(component.compareIds()).toEqual(['b', 'a']); + expect(component.experimentsForCompare().map((exp) => exp.id)).toEqual(['b', 'a']); + }); + + it('hands the workspace back to the folder it came from', () => { + dashboardService.experiments.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + + component.onFolderSelected(folder.id); + component.onFolderCompare(['a', 'b']); + expect(component.compareOriginFolderId()).toBe(folder.id); + + component.onBackToOriginFolder(); + + expect(component.selectedFolderId()).toBe(folder.id); + expect(component.compareMode()).toBe(false); + }); + + it('forgets the origin folder once compare is entered by hand', () => { + dashboardService.experiments.set([experiment('a'), experiment('b')]); + const folder = openFolder(['a', 'b']); + + component.onFolderSelected(folder.id); + component.onFolderCompare(['a', 'b']); + expect(component.compareOriginFolderId()).toBe(folder.id); + + // Leave the folder comparison, then open compare by hand: the set is no longer that folder's. + component.closeCompare(); + component.compareIds.set(['a', 'b']); + component.openCompare(); + + expect(component.compareOriginFolderId()).toBeNull(); + }); + + it('restores the compare origin folder from the URL', () => { + const folder = foldersService.seedFolder('Q3 meta', ['a', 'b']); + const originalUrl = window.location.href; + + try { + window.history.replaceState({}, '', `/dashboard?folder=${folder.id}&compare=a,b`); + + component.ngOnInit(); + + expect(component.compareMode()).toBe(true); + expect(component.compareIds()).toEqual(['a', 'b']); + expect(component.selectedFolderId()).toBeNull(); + expect(component.compareOriginFolderId()).toBe(folder.id); + + component.onBackToOriginFolder(); + + expect(component.selectedFolderId()).toBe(folder.id); + expect(component.compareMode()).toBe(false); + } finally { + window.history.replaceState({}, '', originalUrl); + } + }); + + it('points the folders at whoever the session turns out to be, once per user', () => { + TestBed.flushEffects(); + TestBed.flushEffects(); + + expect(foldersService.sessions).toEqual(['mcurie']); + }); + + it('prunes a deleted run from every folder', () => { + const first = openFolder(['a', 'off-page']); + const second = foldersService.seedFolder('PCA', ['a']); + component.compareIds.set(['a']); + TestBed.flushEffects(); + + component.confirmDelete('a'); + httpMock.expectOne(`${apiUrl}/a`).flush({}); + + // The cascade behind the experiment delete already dropped the membership rows: pruning the + // mirror is a local edit, so the folder API must not see a single request here. + httpMock.expectNone('/services/experiment-folders'); + expect(foldersService.folderById(first.id)?.experimentIds).toEqual(['off-page']); + expect(foldersService.folderById(second.id)?.experimentIds).toEqual([]); + expect(component.compareIds()).toEqual([]); + }); + + it('does not prune folder membership until the delete succeeds', () => { + const folder = openFolder(['a', 'off-page']); + TestBed.flushEffects(); + + component.confirmDelete('a'); + httpMock.expectOne(`${apiUrl}/a`).flush('delete failed', { + status: 500, + statusText: 'Server Error', + }); + + expect(foldersService.folderById(folder.id)?.experimentIds).toEqual(['a', 'off-page']); + }); + + it('never prunes folder members against the visible page', () => { + const folder = openFolder(['a', 'off-page']); + TestBed.flushEffects(); + + // A page refresh that does not contain the members is not evidence they are gone. + dashboardService.experiments.set([]); + TestBed.flushEffects(); + + expect(foldersService.folderById(folder.id)?.experimentIds).toEqual(['a', 'off-page']); + }); + + describe('compare empty state helpers', () => { + it('removes an experiment from compareIds and updates URL', () => { + component.compareIds.set(['a', 'b']); + component.removeFromCompare('a'); + + expect(component.compareIds()).toEqual(['b']); + }); + + it('adds a run from its checkbox and ignores a fourth', () => { + component.onCompareToggled('a'); + component.onCompareToggled('b'); + component.onCompareToggled('c'); + component.onCompareToggled('d'); + + expect(component.compareIds()).toEqual(['a', 'b', 'c']); + expect(component.compareMode()).toBe(false); + }); + + it('drops a checked run when its checkbox is pressed again', () => { + component.compareIds.set(['a', 'b']); + component.onCompareToggled('a'); + + expect(component.compareIds()).toEqual(['b']); + }); + + it('resolves compare origin folder name when origin folder exists', () => { + const folder = openFolder(['a']); + component.compareOriginFolderId.set(folder.id); + + expect(component.compareOriginFolderName()).toBe('Q3 meta'); + + component.compareOriginFolderId.set(null); + expect(component.compareOriginFolderName()).toBeNull(); + }); + + it('resolves algorithm human label or falls back to raw code', () => { + expect(component.getAlgorithmLabel('mock_anova')).toBe('Analysis of Variance'); + expect(component.getAlgorithmLabel('unknown_algo')).toBe('unknown_algo'); + expect(component.getAlgorithmLabel(null)).toBe('Unknown algorithm'); + }); + }); +}); diff --git a/src/app/pages/experiments-dashboard/experiments-dashboard.component.ts b/src/app/pages/experiments-dashboard/experiments-dashboard.component.ts index 15c097c29..89757f45c 100644 --- a/src/app/pages/experiments-dashboard/experiments-dashboard.component.ts +++ b/src/app/pages/experiments-dashboard/experiments-dashboard.component.ts @@ -1,6 +1,7 @@ import { AuthService } from './../../services/auth.service'; -import { Component, OnInit, OnDestroy, computed, effect, signal, inject, ChangeDetectionStrategy } from '@angular/core'; +import { Component, HostListener, OnInit, OnDestroy, computed, effect, signal, inject, ChangeDetectionStrategy } from '@angular/core'; import { Router, RouterModule } from '@angular/router'; +import { Title } from '@angular/platform-browser'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; @@ -11,12 +12,18 @@ import { Experiment } from '../../models/experiments-dashboard.model'; import { ExperimentsCompareComponent } from './experiments-compare/experiments-compare.component'; import { ActivatedRoute } from '@angular/router'; import { ErrorService } from '../../services/error.service'; +import { ExperimentStudioService } from '../../services/experiment-studio.service'; import { Subject, takeUntil } from 'rxjs'; +import { ExperimentsDashboardGuideComponent } from './guide/experiments-dashboard-guide.component'; +import { ExperimentFolderComponent } from './experiment-folder/experiment-folder.component'; +import { ExperimentFoldersService } from '../../services/experiment-folders.service'; +import { readDashboardQuery, updateDashboardQuery } from './dashboard-query.utils'; +import { statusChip } from './shared/experiment-status/experiment-status.component'; @Component({ selector: 'app-experiments-dashboard', templateUrl: './experiments-dashboard.component.html', - styleUrls: ['./experiments-dashboard.component.css'], + styleUrl: './experiments-dashboard.component.css', changeDetection: ChangeDetectionStrategy.OnPush, imports: [ RouterModule, @@ -24,56 +31,60 @@ import { Subject, takeUntil } from 'rxjs'; FormsModule, ExperimentDetailsComponent, ExperimentsListComponent, - ExperimentsCompareComponent + ExperimentsCompareComponent, + ExperimentsDashboardGuideComponent, + ExperimentFolderComponent, ] }) export class ExperimentsDashboardComponent implements OnInit, OnDestroy { private router = inject(Router); public experimentsService = inject(ExperimentsDashboardService); + private experimentStudioService = inject(ExperimentStudioService); private authService = inject(AuthService); private route = inject(ActivatedRoute); private errorService = inject(ErrorService); + private titleService = inject(Title); + readonly foldersService = inject(ExperimentFoldersService); selectedExperiment = signal<Experiment | null>(null); isConfirmingDelete = false; experimentToDeleteId: string | null = null; + /** Sharing and ownership speak email; folders are keyed off the user the service was given. */ currentUserEmail = computed(() => this.authService.authState().user?.email ?? null); + + readonly mobileDetailOpen = computed(() => + Boolean(this.selectedExperiment() || this.selectedFolderId() || this.compareMode()), + ); compareIds = signal<string[]>([]); compareMode = signal(false); + /** Folder canvas owns the centre pane while set; it never coexists with detail or compare. */ + selectedFolderId = signal<string | null>(null); + /** + * The folder the open compare set came from. The id is what travels: the workspace reads the + * way back off it, and reads the folder's sets off the same service the canvas edits. + */ + compareOriginFolderId = signal<string | null>(null); private sharedExperimentId = signal<string | null>(null); private sharedFetchInFlight = signal<string | null>(null); - - // Greeting name: default "researcher" - greetingName = computed(() => { - const user = this.authService.authState().user; - return this.deriveGreetingName(user); + errorMessage = computed(() => this.errorService.error()); + readonly pathologyAccessWarning = this.experimentStudioService.pathologyAccessWarning; + readonly dismissedPathologyWarning = signal(false); + readonly visiblePathologyAccessWarning = computed(() => { + const warning = this.pathologyAccessWarning(); + return warning && !this.dismissedPathologyWarning() ? warning : null; }); - errorMessage = signal<string | null>(null); private destroy$ = new Subject<void>(); - constructor() { } - - hasDeepLink = signal(false); - - ngOnInit(): void { - this.errorService.clearError(); - this.errorService.error$ - .pipe(takeUntil(this.destroy$)) - .subscribe((msg) => this.errorMessage.set(msg)); - - this.route.queryParamMap.pipe(takeUntil(this.destroy$)).subscribe(params => { - const expId = params.get('experiment'); - if (expId) { - this.sharedExperimentId.set(expId); - this.hasDeepLink.set(true); - } + constructor() { + // Folders belong to the signed-in user, and the session only resolves after the first render: + // the service is told who to read for, and it reads them from PostgreSQL for that user. + effect(() => { + this.foldersService.setActiveUser(this.authService.authState().user ?? null); }); - } - private selectSharedExperimentEffect = effect( - () => { + effect(() => { const targetId = this.sharedExperimentId(); const list = this.experimentsService.experiments(); @@ -88,8 +99,10 @@ export class ExperimentsDashboardComponent implements OnInit, OnDestroy { next: (exp) => { this.experimentsService.upsertExperiment(exp); this.selectedExperiment.set(exp); + this.selectedFolderId.set(null); this.compareMode.set(false); this.compareIds.set([]); + this.compareOriginFolderId.set(null); this.sharedExperimentId.set(null); this.sharedFetchInFlight.set(null); }, @@ -105,96 +118,293 @@ export class ExperimentsDashboardComponent implements OnInit, OnDestroy { // set selected experiment this.selectedExperiment.set(found); + this.selectedFolderId.set(null); // not in compare mode this.compareMode.set(false); this.compareIds.set([]); + this.compareOriginFolderId.set(null); this.sharedExperimentId.set(null); - }, - { allowSignalWrites: true } - ); + }); + + effect(() => { + if (!this.allowDefaultOpen() || this.suppressDefaultOpen() || this.openedDefault) return; + const loading = this.experimentsService.isLoading(); + const list = this.experimentsService.experiments(); + if (this.selectedExperiment() || this.selectedFolderId() || this.compareMode() || this.hasDeepLink()) { + this.openedDefault = true; + return; + } + const params = readDashboardQuery(); + if (params.get('experiment') || params.get('folder') || params.get('compare')) return; + const first = list[0]; + if (loading || !first) return; + this.openedDefault = true; + // At the stacked (master/detail) breakpoint a selection hides the list, so the list + // stays the landing view there. + if (window.matchMedia?.('(max-width: 1100px)').matches) return; + this.onExperimentSelected(first); + }); + } + + hasDeepLink = signal(false); + private readonly allowDefaultOpen = signal(false); + private readonly suppressDefaultOpen = signal(false); + private openedDefault = false; + + ngOnInit(): void { + this.titleService.setTitle('Experiments · MIP'); + this.errorService.clearError(); + this.experimentsService.invalidateListCache(); + this.dismissedPathologyWarning.set(false); + this.applyUrlSelection(); + this.experimentStudioService.getAllDataModels() + .pipe(takeUntil(this.destroy$)) + .subscribe(); + + this.route.queryParamMap.pipe(takeUntil(this.destroy$)).subscribe(params => { + const expId = params.get('experiment'); + if (expId) { + this.sharedExperimentId.set(expId); + this.hasDeepLink.set(true); + } + }); + this.allowDefaultOpen.set(true); + } ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); } - dismissError() { - this.errorService.clearError(); + @HostListener('window:popstate') + onDashboardPopState() { + this.applyUrlSelection(); } - private deriveGreetingName(user: any | null): string { - if (!user) { - return 'researcher'; - } + private syncSelectionUrl(experimentId: string) { + updateDashboardQuery({ + experiment: experimentId, + folder: null, + compare: null, + }); + } - const raw = - (user.fullname as string | undefined) || - (user.username as string | undefined) || - ''; + private syncCompareUrl(ids: string[]) { + updateDashboardQuery({ + compare: ids.length ? ids.join(',') : null, + experiment: null, + folder: null, + }); + } + + private applyUrlSelection() { + const params = readDashboardQuery(); + const experimentId = params.get('experiment'); + const folderId = params.get('folder'); + const compareParam = params.get('compare'); + + if (compareParam) { + const ids = compareParam.split(',').map((id) => id.trim()).filter(Boolean); + this.compareMode.set(ids.length > 0); + this.compareIds.set(ids); + this.selectedFolderId.set(null); + this.selectedExperiment.set(null); + this.compareOriginFolderId.set(folderId); + return; + } - const trimmed = raw.trim(); - if (!trimmed) { - return 'researcher'; + if (folderId) { + this.compareMode.set(false); + this.compareIds.set([]); + this.selectedExperiment.set(null); + this.selectedFolderId.set(folderId); + this.compareOriginFolderId.set(null); + return; } - const lower = trimmed.toLowerCase(); - // if no user name or anonymous, set it to generic - if (lower === 'anonymous' || lower === 'anon') { - return 'researcher'; + this.compareMode.set(false); + this.compareIds.set([]); + this.selectedFolderId.set(null); + this.compareOriginFolderId.set(null); + if (experimentId) { + this.sharedExperimentId.set(experimentId); + this.hasDeepLink.set(true); + } else { + this.selectedExperiment.set(null); } + } - // keep only first name for casual greeting - const firstPart = trimmed.split(' ')[0]; - return firstPart || 'researcher'; + dismissError() { + this.errorService.clearError(); + } + + dismissPathologyWarning() { + this.dismissedPathologyWarning.set(true); } // COMPARE MODE - toggleCompareMode() { - const isOn = this.compareMode(); + clearCompareSelection() { + this.compareIds.set([]); + this.syncCompareUrl([]); + } - if (isOn) { - // turn OFF -> clear - this.compareMode.set(false); - this.compareIds.set([]); - } else { - // turn ON -> set selected - const current = this.selectedExperiment(); - this.compareMode.set(true); - this.compareIds.set(current ? [current.id] : []); + onCompareToggled(id: string) { + const ids = this.compareIds(); + const next = ids.includes(id) + ? ids.filter((existing) => existing !== id) + : ids.length >= 3 + ? ids + : [...ids, id]; + if (next === ids) return; + this.compareIds.set(next); + if (!this.compareMode()) return; + if (next.length < 2) { + // Same exit as Done: drop ?compare= and put a run back in the pane openCompare cleared. + this.closeCompare(); + return; } + this.syncCompareUrl(next); + } + + openCompare() { + if (this.compareIds().length < 2) return; + this.selectedFolderId.set(null); + this.selectedExperiment.set(null); + this.compareOriginFolderId.set(null); + this.compareMode.set(true); + this.syncCompareUrl(this.compareIds()); + } + + closeCompare() { + this.compareMode.set(false); + this.compareOriginFolderId.set(null); + updateDashboardQuery({ compare: null }); + if (this.selectedExperiment()) return; + const ids = this.compareIds(); + const list = this.experimentsService.experiments(); + const restored = list.find((experiment) => ids.includes(experiment.id)) ?? list[0]; + if (!restored) return; + this.selectedExperiment.set(restored); + this.syncSelectionUrl(restored.id); + } + + readonly compareOriginFolderName = computed(() => { + const id = this.compareOriginFolderId(); + return id ? this.foldersService.folderById(id)?.name ?? null : null; + }); + + removeFromCompare(experimentId: string) { + const ids = this.compareIds().filter((id) => id !== experimentId); + this.compareIds.set(ids); + this.syncCompareUrl(ids); + } + + getAlgorithmLabel(code: string | null | undefined): string { + if (!code) return 'Unknown algorithm'; + return this.experimentStudioService.backendAlgorithms?.()?.[code]?.label || code; } + /** Order follows `compareIds`, so a folder keeps its numbered order in the workspace and + * manual picks keep the order they were clicked in. */ readonly experimentsForCompare = computed(() => { const ids = this.compareIds(); const list = this.experimentsService.experiments(); - return list.filter(exp => ids.includes(exp.id)); + const current = this.selectedExperiment(); + return ids + .map((id) => list.find((exp) => exp.id === id) || (current?.id === id ? current : undefined)) + .filter((exp): exp is Experiment => !!exp); + }); + + readonly compareDiffRows = computed(() => { + const experiments = this.experimentsForCompare(); + const fields: { k: string; value: (experiment: Experiment) => string }[] = [ + { k: 'Algorithm', value: (experiment) => this.getAlgorithmLabel(experiment.algorithmName) }, + { k: 'Outcome (y)', value: (experiment) => experiment.variables?.join(', ') || '—' }, + { k: 'Predictors (x)', value: (experiment) => experiment.covariates?.join(', ') || '—' }, + { k: 'Datasets', value: (experiment) => experiment.datasets?.join(', ') || '—' }, + { k: 'Filters', value: (experiment) => experiment.filters?.length ? experiment.filters.join(', ') : 'None' }, + { k: 'Status', value: (experiment) => statusChip(experiment.status).label }, + ]; + return fields.map((field) => { + const vals = experiments.map((experiment) => field.value(experiment)); + const same = vals.every((value) => value === vals[0]); + return { k: field.k, same, vals }; + }); }); // click on list row onExperimentSelected(experiment: Experiment) { this.selectedExperiment.set(experiment); + this.selectedFolderId.set(null); + this.compareMode.set(false); + this.syncSelectionUrl(experiment.id); + } - // if in compare mode, toggle comparison list - if (this.compareMode()) { - const ids = this.compareIds(); - if (ids.includes(experiment.id)) { - this.compareIds.set(ids.filter(id => id !== experiment.id)); - } else { - this.compareIds.set([...ids, experiment.id]); - } + onFolderSelected(folderId: string | null) { + this.selectedFolderId.set(folderId); + // A folder opened from the strip is a fresh look, not a compare back-path. + this.compareOriginFolderId.set(null); + if (!folderId) { + updateDashboardQuery({ folder: null }); + return; } + + // The canvas replaces both other centre-pane views, so leave compare mode cleanly. + this.compareMode.set(false); + this.compareIds.set([]); + this.selectedExperiment.set(null); + updateDashboardQuery({ folder: folderId, experiment: null, compare: null }); } - onRunExperiment(expId: string) { - this.router.navigate(['/experiment-studio'], { - queryParams: { experimentId: expId, mode: 'edit' } + /** + * Handoff to the compare workspace. Members on another page are fetched and upserted + * first: `experimentsForCompare` only resolves the loaded page, so without this an + * off-page member would vanish from the comparison. The folder id rides along, so its + * sets section the workspace — including sets made after this handoff. + */ + onFolderCompare(memberIds: string[]) { + if (memberIds.length < 2) return; + + const originFolderId = this.selectedFolderId(); + + this.experimentsService.hydrateExperiments(memberIds).subscribe({ + next: (experiments) => { + this.selectedFolderId.set(null); + this.selectedExperiment.set(null); + this.compareMode.set(true); + const ids = experiments.map((exp) => exp.id); + this.compareIds.set(ids); + this.compareOriginFolderId.set(originFolderId); + updateDashboardQuery({ folder: originFolderId, experiment: null, compare: ids.join(',') }); + }, + error: (err) => { + console.error('[Folders] Compare handoff failed', err); + this.errorService.setError('Could not load the experiments in this folder.'); + }, }); } - onEditExperiment(expId: string) { + /** Go back to the folder the compare set was built from; a deleted folder is simply no way back. */ + onBackToOriginFolder() { + const folderId = this.compareOriginFolderId(); + if (!folderId || !this.foldersService.folderById(folderId)) return; + this.onFolderSelected(folderId); + } + + closeMobileDetail() { + this.suppressDefaultOpen.set(true); + this.selectedExperiment.set(null); + this.selectedFolderId.set(null); + this.compareMode.set(false); + this.compareIds.set([]); + this.compareOriginFolderId.set(null); + updateDashboardQuery({ experiment: null, folder: null, compare: null }); + } + + onRunExperiment(expId: string) { this.router.navigate(['/experiment-studio'], { queryParams: { experimentId: expId, mode: 'edit' } }); @@ -219,26 +429,21 @@ export class ExperimentsDashboardComponent implements OnInit, OnDestroy { this.isConfirmingDelete = true; } - onDeleteFromList(expId: string) { - this.experimentToDeleteId = expId; - - const current = this.selectedExperiment(); - if (!current || current.id !== expId) { - const found = this.experimentsService - .experiments() - .find(e => e.id === expId); - if (found) this.selectedExperiment.set(found); - } - this.isConfirmingDelete = true; - } - confirmDelete(expId: string) { if (!expId) return; - this.experimentsService.deleteExperiment(expId); + // Membership is dropped from the mirror only after the backend confirms the delete: the + // delete request is optimistic and can roll back, and folder membership must not be lost + // on a failed delete. `pruneExperiment` is local-only — the cascade has already removed the + // rows behind it. + this.experimentsService.deleteExperiment(expId, (deletedId) => { + this.foldersService.pruneExperiment(deletedId); + }); if (this.selectedExperiment()?.id === expId) { - this.selectedExperiment.set(null); + const next = this.experimentsService.experiments().find((experiment) => experiment.id !== expId) ?? null; + this.selectedExperiment.set(next); + if (next) this.syncSelectionUrl(next.id); } this.compareIds.set(this.compareIds().filter(id => id !== expId)); @@ -247,6 +452,17 @@ export class ExperimentsDashboardComponent implements OnInit, OnDestroy { this.isConfirmingDelete = false; } + deleteTargetName(): string { + const id = this.experimentToDeleteId; + if (!id) return 'this experiment'; + const selected = this.selectedExperiment(); + if (selected?.id === id) return selected.name || 'Untitled Investigation'; + return ( + this.experimentsService.experiments().find((experiment) => experiment.id === id)?.name || + 'this experiment' + ); + } + cancelDelete() { this.isConfirmingDelete = false; this.experimentToDeleteId = null; diff --git a/src/app/pages/experiments-dashboard/experiments-dashboard.mapper.ts b/src/app/pages/experiments-dashboard/experiments-dashboard.mapper.ts index ef012d1d6..b28212c22 100644 --- a/src/app/pages/experiments-dashboard/experiments-dashboard.mapper.ts +++ b/src/app/pages/experiments-dashboard/experiments-dashboard.mapper.ts @@ -1,6 +1,7 @@ import { BackendExperiment } from "../../models/backend-experiment.model"; import { BackendFilter } from "../../models/filters.model"; -import { Experiment, AlgorithmDetails, UserDetails } from './../../models/experiments-dashboard.model'; +import { Experiment } from './../../models/experiments-dashboard.model'; +import { AnalysisPreprocessingStep } from "../../models/backend-algorithms.model"; function collectFilterVariableCodes( @@ -33,13 +34,23 @@ function normalizeToStringArray(value: string[] | string | null | undefined): st return [String(value)]; } +function preprocessingStepsToRecord( + steps: AnalysisPreprocessingStep[] | null | undefined +): Record<string, unknown> | null { + if (!steps?.length) return null; + return Object.fromEntries(steps.map((step) => [step.name, step.parameters])); +} + +export { preprocessingStepsToRecord }; + // Map BackendExperiment to Experiment export function mapBackendToFrontend(backend: BackendExperiment): Experiment { console.groupCollapsed('[Mapper] BackendExperiment → Experiment'); console.groupEnd(); - const input = backend.algorithm?.inputdata || {}; + const analysis = backend.analysis; + const input = analysis?.inputdata || {}; const filtersLogic = input.filters ?? null; return { @@ -49,7 +60,7 @@ export function mapBackendToFrontend(backend: BackendExperiment): Experiment { description: backend.description ?? '', status: backend.status, - algorithmName: backend.algorithm?.name, + algorithmName: analysis?.algorithm?.name, author: backend.createdBy.fullname || backend.createdBy.username || @@ -59,39 +70,12 @@ export function mapBackendToFrontend(backend: BackendExperiment): Experiment { domain: input.data_model ?? null, datasets: normalizeToStringArray(input.datasets), - variables: normalizeToStringArray(input.y), - covariates: normalizeToStringArray(input.x), + variables: normalizeToStringArray(analysis?.algorithm?.y), + covariates: normalizeToStringArray(analysis?.algorithm?.x), filters: collectFilterVariableCodes(filtersLogic), + filterLogic: filtersLogic, + preprocessing: preprocessingStepsToRecord(analysis?.preprocessing), mipVersion: backend.mipVersion ?? undefined, }; } - -// Map BackendExperiment to AlgorithmDetails -export function mapBackendToAlgorithmDetails(backend: BackendExperiment): AlgorithmDetails { - return { - name: backend.algorithm.name, - datasets: normalizeToStringArray(backend.algorithm.inputdata.datasets), - parameters: backend.algorithm.parameters, - dataModel: backend.algorithm.inputdata.data_model, - }; -} - -// Map BackendExperiment to UserDetails -export function mapBackendToUserDetails(backend: BackendExperiment): UserDetails { - return { - username: backend.createdBy.username, - fullname: backend.createdBy.fullname, - email: backend.createdBy.email, - }; -} - -// Map Experiment to BackendExperiment (Frontend to Backend) -export function mapFrontendToBackend(frontend: Experiment): Partial<BackendExperiment> { - return { - uuid: frontend.id, - name: frontend.name, - created: frontend.dateCreated.toISOString(), - status: frontend.status, - }; -} diff --git a/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.css b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.css new file mode 100644 index 000000000..10cb74c91 --- /dev/null +++ b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.css @@ -0,0 +1 @@ +@import '../../experiment-studio/guide/experiment-studio-guide.component.css'; diff --git a/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.html b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.html new file mode 100644 index 000000000..bdbf2f8da --- /dev/null +++ b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.html @@ -0,0 +1,111 @@ +@if (isOpen() && currentStep(); as step) { +<div class="guide-overlay" aria-hidden="false"> + @if (highlightRect(); as rect) { + @if (step.allowTargetInteraction) { + <div class="guide-mask guide-mask--top" [style.height.px]="rect.top"></div> + <div class="guide-mask guide-mask--left" [style.top.px]="rect.top" [style.width.px]="rect.left" + [style.height.px]="rect.height"></div> + <div class="guide-mask guide-mask--right" [style.top.px]="rect.top" [style.left.px]="rect.right" + [style.height.px]="rect.height"></div> + <div class="guide-mask guide-mask--bottom" [style.top.px]="rect.bottom"></div> + } @else { + <div class="guide-mask guide-mask--full" [style.background]="'transparent'"></div> + } + <div class="guide-highlight" [style.top.px]="rect.top" [style.left.px]="rect.left" [style.width.px]="rect.width" + [style.height.px]="rect.height"></div> + } @else if (!step.allowTargetInteraction) { + <div class="guide-mask guide-mask--full" [style.background]="step.maskBackground ?? 'rgba(2, 6, 23, 0.65)'"></div> + } + + <section + #guideCard + class="guide-banner" + role="dialog" + aria-labelledby="experiments-dashboard-guide-title" + [class.guide-banner--collapsed]="isCollapsed()" + tabindex="-1"> + @if (isCollapsed()) { + <div class="guide-banner-compact"> + <div class="guide-banner-meta"> + <span class="guide-section-pill">{{ step.section }}</span> + <span class="guide-step-count guide-step-count--compact">{{ currentStepNumber() }} / {{ totalSteps() }}</span> + </div> + + <div class="guide-banner-compact-copy"> + <h3 + id="experiments-dashboard-guide-title" + class="guide-step-title" + [innerHTML]="step.title"></h3> + </div> + + <div class="guide-banner-compact-nav"> + <button type="button" class="guide-nav-icon" [disabled]="!canGoToPrevious()" (click)="goToPreviousStep()" + [attr.aria-label]="labels.back" [attr.title]="labels.back"> + <i class="fas fa-chevron-left" aria-hidden="true"></i> + </button> + <button type="button" class="guide-nav-icon guide-nav-icon--primary" [disabled]="!canGoToNext()" + (click)="goToNextStep()" [attr.aria-label]="nextButtonLabel()" + [attr.title]="nextButtonLabel()"> + <i class="fas fa-chevron-right" aria-hidden="true"></i> + </button> + </div> + + <div class="guide-banner-utilities"> + <button type="button" class="guide-collapse guide-collapse-icon" (click)="toggleCollapsed()" + [attr.aria-label]="labels.expand" [attr.title]="labels.expand"> + <i class="fas fa-expand-alt" aria-hidden="true"></i> + </button> + <button type="button" class="guide-close guide-close-icon" (click)="closeGuide()" + [attr.aria-label]="labels.exit" [attr.title]="labels.exit"> + <i class="fas fa-times" aria-hidden="true"></i> + </button> + </div> + </div> + } @else { + <div class="guide-banner-top"> + <div class="guide-banner-meta"> + <span class="guide-section-pill">{{ step.section }}</span> + <span class="guide-step-count">Step {{ currentStepNumber() }} of {{ totalSteps() }}</span> + </div> + <div class="guide-banner-utilities"> + <button type="button" class="guide-collapse guide-window-control" (click)="toggleCollapsed()" + [attr.aria-label]="labels.collapse" [attr.title]="labels.collapse"> + <i class="fas fa-compress-alt" aria-hidden="true"></i> + </button> + <button type="button" class="guide-close guide-window-control" (click)="closeGuide()" + [attr.aria-label]="labels.exit" [attr.title]="labels.exit"> + <i class="fas fa-times" aria-hidden="true"></i> + </button> + </div> + </div> + + <div class="guide-progress" aria-hidden="true"> + <div class="guide-progress-bar" [style.--guide-progress]="progressPercent() / 100"></div> + </div> + + <div class="guide-banner-body"> + <div class="guide-banner-copy"> + <h3 + id="experiments-dashboard-guide-title" + [innerHTML]="step.title"></h3> + @if (step.body) { + <p [innerHTML]="step.body"></p> + } + @if (pendingRequirementHint(); as hint) { + <p class="guide-requirement">{{ hint }}</p> + } + </div> + + <div class="guide-banner-actions"> + <button type="button" class="guide-secondary" [disabled]="!canGoToPrevious()" (click)="goToPreviousStep()"> + {{ labels.back }} + </button> + <button type="button" class="guide-primary" [disabled]="!canGoToNext()" (click)="goToNextStep()"> + {{ nextButtonLabel() }} + </button> + </div> + </div> + } + </section> +</div> +} diff --git a/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.spec.ts b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.spec.ts new file mode 100644 index 000000000..abc8eb521 --- /dev/null +++ b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.spec.ts @@ -0,0 +1,163 @@ +import { provideZonelessChangeDetection } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { ExperimentsDashboardGuideComponent } from './experiments-dashboard-guide.component'; +import { EXPERIMENTS_DASHBOARD_GUIDE_STEPS } from './experiments-dashboard-guide.content'; + +describe('ExperimentsDashboardGuideComponent', () => { + let component: ExperimentsDashboardGuideComponent; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ExperimentsDashboardGuideComponent], + providers: [provideZonelessChangeDetection()], + }); + + component = TestBed.createComponent(ExperimentsDashboardGuideComponent).componentInstance; + }); + + afterEach(() => { + document.querySelectorAll('[data-guide]').forEach((element) => element.remove()); + }); + + it('keeps Guide Complete as the last content step', () => { + expect(EXPERIMENTS_DASHBOARD_GUIDE_STEPS.at(-1)?.id).toBe('dashboard-guide-complete'); + const compareIdx = EXPERIMENTS_DASHBOARD_GUIDE_STEPS.findIndex((s) => s.id === 'compare-workspace'); + const completeIdx = EXPERIMENTS_DASHBOARD_GUIDE_STEPS.findIndex((s) => s.id === 'dashboard-guide-complete'); + expect(compareIdx).toBeGreaterThanOrEqual(0); + expect(completeIdx).toBeGreaterThan(compareIdx); + }); + + it('freezes totalSteps to the full tour length', () => { + component.startGuide(true); + expect(component.totalSteps()).toBe(EXPERIMENTS_DASHBOARD_GUIDE_STEPS.length); + expect(component.currentStepNumber()).toBe(1); + }); + + it('advances step ordinal by one per forward navigation', () => { + component.startGuide(true); + const before = component.currentStepNumber(); + component.goToNextStep(true); + expect(component.currentStepNumber()).toBe(before + 1); + expect(component.totalSteps()).toBe(EXPERIMENTS_DASHBOARD_GUIDE_STEPS.length); + }); + + it('uses Skip for optional steps that are not last', () => { + component.activeSteps.set([...EXPERIMENTS_DASHBOARD_GUIDE_STEPS]); + const compareIdx = EXPERIMENTS_DASHBOARD_GUIDE_STEPS.findIndex((s) => s.id === 'compare-workspace'); + component.currentIndex.set(compareIdx); + (component as any).recountProgress(); + expect(component.nextButtonLabel()).toBe('Skip'); + }); + + it('prefers workbench over compare-workspace when both targets exist', () => { + const detail = document.createElement('div'); + detail.setAttribute('data-guide', 'dashboard-detail-card'); + Object.defineProperty(detail, 'getBoundingClientRect', { + value: () => ({ top: 10, right: 100, bottom: 50, left: 10, width: 90, height: 40, x: 10, y: 10, toJSON: () => ({}) }), + }); + document.body.appendChild(detail); + + const compare = document.createElement('div'); + compare.setAttribute('data-guide', 'dashboard-compare-workspace'); + Object.defineProperty(compare, 'getBoundingClientRect', { + value: () => ({ top: 10, right: 100, bottom: 50, left: 10, width: 90, height: 40, x: 10, y: 10, toJSON: () => ({}) }), + }); + document.body.appendChild(compare); + + component.activeSteps.set([...EXPERIMENTS_DASHBOARD_GUIDE_STEPS]); + const tutorialIdx = EXPERIMENTS_DASHBOARD_GUIDE_STEPS.findIndex((s) => s.id === 'tutorial-experiment'); + component.currentIndex.set(tutorialIdx); + + const next = (component as any).getNavigableStepIndex(tutorialIdx + 1, 1); + expect(EXPERIMENTS_DASHBOARD_GUIDE_STEPS[next].id).toBe('workbench'); + }); + + it('keeps the highlight aligned when the target scrolls above the viewport', () => { + const target = document.createElement('section'); + target.setAttribute('data-guide', 'dashboard-workspace'); + vi.spyOn(target, 'getBoundingClientRect').mockReturnValue({ + top: -18, + right: 680, + bottom: 212, + left: 48, + width: 632, + height: 230, + x: 48, + y: -18, + toJSON: () => ({}), + } as DOMRect); + document.body.appendChild(target); + + component.activeSteps.set([ + { + id: 'workspace', + section: 'Explore', + title: 'Experiment List', + body: '', + selector: '[data-guide="dashboard-workspace"]', + }, + ] as any); + + (component as any).updateLayout(); + + expect(component.highlightRect()).toEqual(expect.objectContaining({ + top: -28, + left: 38, + bottom: 222, + right: 690, + })); + }); + + it('points every content step with a selector at its data-guide target', () => { + const stepsWithSelectors = EXPERIMENTS_DASHBOARD_GUIDE_STEPS.filter((step) => !!step.selector); + expect(stepsWithSelectors.length).toBeGreaterThan(0); + + for (const step of stepsWithSelectors) { + document.querySelectorAll('[data-guide]').forEach((element) => element.remove()); + + const target = document.createElement('div'); + const guideValue = step.selector!.match(/data-guide="([^"]+)"/)?.[1]; + expect(guideValue, step.id).toBeTruthy(); + target.setAttribute('data-guide', guideValue!); + Object.defineProperty(target, 'getBoundingClientRect', { + configurable: true, + value: () => ({ + top: 120, + right: 420, + bottom: 200, + left: 40, + width: 380, + height: 80, + x: 40, + y: 120, + toJSON: () => ({}), + }), + }); + document.body.appendChild(target); + + component.activeSteps.set([...EXPERIMENTS_DASHBOARD_GUIDE_STEPS] as any); + component.currentIndex.set(EXPERIMENTS_DASHBOARD_GUIDE_STEPS.findIndex((entry) => entry.id === step.id)); + (component as any).updateLayout(); + + const highlight = component.highlightRect(); + expect(highlight, step.id).not.toBeNull(); + const centerX = 230; + const centerY = 160; + expect(highlight!.left <= centerX && centerX <= highlight!.right, step.id).toBe(true); + expect(highlight!.top <= centerY && centerY <= highlight!.bottom, step.id).toBe(true); + } + }); + + it('ignores arrow keys while a text field is focused', () => { + component.activeSteps.set([{ id: 'one', section: 'Explore', title: 'One', body: '' }]); + component.isOpen.set(true); + + const typing = new KeyboardEvent('keydown', { key: 'ArrowRight' }); + Object.defineProperty(typing, 'target', { configurable: true, value: document.createElement('input') }); + component.onWindowKeydown(typing); + + expect(component.currentIndex()).toBe(0); + component.closeGuide(); + }); + +}); diff --git a/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.ts b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.ts new file mode 100644 index 000000000..6f57c65d7 --- /dev/null +++ b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.component.ts @@ -0,0 +1,499 @@ +import { CommonModule, DOCUMENT } from '@angular/common'; +import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, OnDestroy, OnInit, ViewChild, computed, inject, signal } from '@angular/core'; +import { + EXPERIMENTS_DASHBOARD_GUIDE_STEPS, + EXPERIMENT_STUDIO_GUIDE_LABELS, + ExperimentsDashboardGuideStep, +} from './experiments-dashboard-guide.content'; +import { GuideLauncher, GuideLauncherService } from '../../../services/guide-launcher.service'; +import { isTypingTarget } from '../../../core/keyboard.utils'; + +interface GuideRect { + top: number; + left: number; + width: number; + height: number; + right: number; + bottom: number; +} + +@Component({ + selector: 'app-experiments-dashboard-guide', + imports: [CommonModule], + templateUrl: './experiments-dashboard-guide.component.html', + styleUrl: './experiments-dashboard-guide.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, + host: { + '(window:resize)': 'onViewportChange()', + '(window:scroll)': 'onViewportChange()', + '(window:keydown)': 'onWindowKeydown($event)', + '(document:click)': 'onDocumentClick($event)', + }, +}) +export class ExperimentsDashboardGuideComponent implements OnInit, AfterViewInit, OnDestroy { + private readonly document = inject(DOCUMENT); + private readonly guideLauncher = inject(GuideLauncherService); + private readonly autoStartStorageKey = 'mip.guide.experiments-dashboard.autostarted'; + private layoutTimer: number | null = null; + private advanceTimer: number | null = null; + + /** Handed to the bar, which draws the control (see GuideLauncherService). */ + private readonly launcherHandle: GuideLauncher = { + label: EXPERIMENT_STUDIO_GUIDE_LABELS.launcher, + start: () => this.startGuide(), + }; + + @ViewChild('guideCard') + private guideCard?: ElementRef<HTMLElement>; + + readonly labels = EXPERIMENT_STUDIO_GUIDE_LABELS; + readonly isOpen = signal(false); + readonly activeSteps = signal<ExperimentsDashboardGuideStep[]>([]); + readonly currentIndex = signal(0); + readonly highlightRect = signal<GuideRect | null>(null); + readonly isCollapsed = signal(false); + /** Recounted from currently navigable steps so skipped optionals do not create gaps. */ + readonly totalSteps = signal(0); + readonly currentStepOrdinal = signal(0); + + readonly currentStep = computed(() => this.activeSteps()[this.currentIndex()] ?? null); + readonly previousStep = computed(() => { + const previousIndex = this.getNavigableStepIndex(this.currentIndex() - 1, -1); + return previousIndex === null ? null : this.activeSteps()[previousIndex] ?? null; + }); + readonly currentStepNumber = computed(() => this.currentStepOrdinal()); + readonly progressPercent = computed(() => { + const total = this.totalSteps(); + return total ? (this.currentStepNumber() / total) * 100 : 0; + }); + readonly hasPrevious = computed(() => this.previousStep() !== null); + readonly canGoToPrevious = computed(() => this.hasPrevious() && !this.previousStep()?.advanceOnTargetClick); + readonly isLastStep = computed(() => this.getNavigableStepIndex(this.currentIndex() + 1, 1) === null); + readonly canGoToNext = computed(() => this.isStepRequirementSatisfied(this.currentStep())); + readonly stepNeedsAction = computed(() => !!this.currentStep()?.advanceOnTargetClick && !this.canGoToNext()); + readonly nextButtonLabel = computed(() => { + if (this.stepNeedsAction()) { + return 'Action required'; + } + if (this.isLastStep()) { + return this.labels.done; + } + // Only the optional compare workspace is an explicit skip; other optionals still say Next. + if (this.currentStep()?.id === 'compare-workspace') { + return this.labels.skip; + } + return this.labels.next; + }); + readonly pendingRequirementHint = computed(() => { + const step = this.currentStep(); + if (!step || this.canGoToNext()) { + return null; + } + + return step.requirementHint ?? 'Use the highlighted element to continue.'; + }); + + ngOnInit(): void { + this.guideLauncher.register(this.launcherHandle); + } + + ngAfterViewInit(): void { + window.setTimeout(() => this.startGuide(false), 900); + } + + ngOnDestroy(): void { + this.guideLauncher.unregister(this.launcherHandle); + this.clearLayoutTimer(); + this.clearAdvanceTimer(); + } + + startGuide(manual = true): void { + if (!manual && this.hasAutoStarted()) { + return; + } + + const resolvedSteps = this.resolveSteps(); + if (!resolvedSteps.length) { + return; + } + + if (!this.hasAutoStarted()) { + this.markAutoStarted(); + } + + // Workbench path must not start inside compare mode. + this.ensureCompareModeOff(); + + this.activeSteps.set(resolvedSteps); + this.totalSteps.set(resolvedSteps.length); + this.currentIndex.set(this.getNavigableStepIndex(0, 1) ?? 0); + this.currentStepOrdinal.set(1); + this.isCollapsed.set(false); + this.isOpen.set(true); + this.syncStepLayout(); + } + + closeGuide(): void { + this.isOpen.set(false); + this.activeSteps.set([]); + this.currentIndex.set(0); + this.totalSteps.set(0); + this.currentStepOrdinal.set(0); + this.highlightRect.set(null); + this.isCollapsed.set(false); + this.clearLayoutTimer(); + this.clearAdvanceTimer(); + } + + goToNextStep(force = false): void { + if (!force && !this.canGoToNext()) { + return; + } + + const step = this.currentStep(); + // Leaving the compare control without an active workspace: do not linger. + if (step?.id === 'compare') { + // keep whatever the user chose; compare-workspace is optional if inactive + } + + // Entering the open-experiment / workbench stretch: leave compare mode. + if (step?.id === 'new-experiment' || step?.id === 'tutorial-experiment') { + this.ensureCompareModeOff(); + } + + // Next from "Open an Experiment" must land on the workbench, same as the click path. + if (!force && step?.id === 'tutorial-experiment') { + this.clearAdvanceTimer(); + this.waitForWorkbenchThenAdvance(0); + return; + } + + const nextIndex = this.getNavigableStepIndex(this.currentIndex() + 1, 1); + if (nextIndex === null) { + this.closeGuide(); + return; + } + + const movingForward = nextIndex > this.currentIndex(); + this.currentIndex.set(nextIndex); + if (movingForward) { + this.currentStepOrdinal.update((value) => value + 1); + } else { + this.currentStepOrdinal.update((value) => Math.max(1, value - 1)); + } + this.ensureTotalSteps(this.activeSteps().length); + this.syncStepLayout(); + } + + goToPreviousStep(): void { + if (!this.canGoToPrevious()) { + return; + } + + const previousIndex = this.getNavigableStepIndex(this.currentIndex() - 1, -1); + if (previousIndex === null) { + return; + } + + this.currentIndex.set(previousIndex); + this.currentStepOrdinal.update((value) => Math.max(1, value - 1)); + this.ensureTotalSteps(this.activeSteps().length); + this.syncStepLayout(); + } + + toggleCollapsed(): void { + this.isCollapsed.update((collapsed) => !collapsed); + this.scheduleLayoutUpdate(0); + } + + onViewportChange(): void { + if (!this.isOpen()) { + return; + } + + this.scheduleLayoutUpdate(0); + } + + onWindowKeydown(event: KeyboardEvent): void { + if (!this.isOpen()) { + return; + } + + if (event.key === 'Escape') { + event.preventDefault(); + this.closeGuide(); + return; + } + + if (event.key === 'ArrowRight') { + if (isTypingTarget(event.target)) { + return; + } + event.preventDefault(); + this.goToNextStep(); + return; + } + + if (event.key === 'ArrowLeft') { + if (isTypingTarget(event.target)) { + return; + } + event.preventDefault(); + this.goToPreviousStep(); + } + } + + onDocumentClick(event: MouseEvent): void { + const step = this.currentStep(); + if (!this.isOpen() || !step?.advanceOnTargetClick || !step.selector) { + return; + } + + const clickedNode = event.target; + if (!(clickedNode instanceof Node)) { + return; + } + + const target = this.findTarget(step.selector); + if (!target || !target.contains(clickedNode)) { + return; + } + + // After opening a run, wait for the workbench target so we do not skip to compare-workspace. + this.clearAdvanceTimer(); + this.advanceTimer = window.setTimeout(() => { + if (!this.isOpen() || this.currentStep()?.id !== step.id) { + return; + } + + if (step.id === 'tutorial-experiment') { + this.ensureCompareModeOff(); + this.waitForWorkbenchThenAdvance(0); + return; + } + + this.goToNextStep(true); + }, 260); + } + + private waitForWorkbenchThenAdvance(attempt: number): void { + const maxAttempts = 12; + if (this.findTarget('[data-guide="dashboard-detail-card"]')) { + this.goToNextStep(true); + return; + } + + if (attempt >= maxAttempts) { + // Fall through to next navigable step (still prefers workbench when present). + this.goToNextStep(true); + return; + } + + this.advanceTimer = window.setTimeout(() => { + this.waitForWorkbenchThenAdvance(attempt + 1); + }, 150); + } + + private resolveSteps(): ExperimentsDashboardGuideStep[] { + return [...EXPERIMENTS_DASHBOARD_GUIDE_STEPS]; + } + + private syncStepLayout(): void { + const step = this.currentStep(); + if (!step) { + return; + } + + this.recountProgress(); + + if (step.id === 'tutorial-experiment' || step.id === 'workbench' || step.id === 'actions' || step.id === 'results') { + this.ensureCompareModeOff(); + } + + const target = step.selector ? this.findTarget(step.selector) : null; + if (target) { + this.scrollTargetIntoView(target); + } + + this.scheduleLayoutUpdate(target ? 260 : 0); + } + + /** The workbench scrolls inside `.main-pane`, not the window. */ + private scrollTargetIntoView(target: HTMLElement): void { + const scroller = this.scrollParent(target); + if (!scroller) { + const top = Math.max(window.scrollY + target.getBoundingClientRect().top - this.getHeaderOffset(), 0); + window.scrollTo({ top, behavior: 'auto' }); + return; + } + + const delta = target.getBoundingClientRect().top - scroller.getBoundingClientRect().top; + scroller.scrollTo({ top: Math.max(scroller.scrollTop + delta - 8, 0), behavior: 'auto' }); + } + + private scrollParent(element: HTMLElement): HTMLElement | null { + let parent = element.parentElement; + while (parent) { + const style = getComputedStyle(parent); + if (/(auto|scroll)/.test(style.overflowY) && parent.scrollHeight > parent.clientHeight + 1) { + return parent; + } + parent = parent.parentElement; + } + return null; + } + + private getHeaderOffset(): number { + const rootStyles = getComputedStyle(this.document.documentElement); + const headerHeight = Number.parseFloat(rootStyles.getPropertyValue('--header-height')) || 0; + return headerHeight + 12; + } + + private scheduleLayoutUpdate(delayMs: number): void { + this.clearLayoutTimer(); + this.layoutTimer = window.setTimeout(() => this.updateLayout(), delayMs); + } + + private clearLayoutTimer(): void { + if (this.layoutTimer !== null) { + window.clearTimeout(this.layoutTimer); + this.layoutTimer = null; + } + } + + private clearAdvanceTimer(): void { + if (this.advanceTimer !== null) { + window.clearTimeout(this.advanceTimer); + this.advanceTimer = null; + } + } + + private updateLayout(): void { + const step = this.currentStep(); + if (!step) { + return; + } + + const target = step.selector ? this.findTarget(step.selector) : null; + this.highlightRect.set(target ? this.expandRect(target.getBoundingClientRect()) : null); + + window.setTimeout(() => { + this.guideCard?.nativeElement.focus(); + }, 0); + } + + private isStepRequirementSatisfied(step: ExperimentsDashboardGuideStep | null): boolean { + return !step?.advanceOnTargetClick; + } + + /** Progress X advances one tick per navigation so optional skips cannot drop the counter. */ + private recountProgress(): void { + this.ensureTotalSteps(this.activeSteps().length); + } + + private ensureTotalSteps(length: number): void { + // Freeze Y at the full tour length once known; never shrink or grow mid-tour. + if (!this.totalSteps() && length) { + this.totalSteps.set(length); + } + } + + private isOptionalStepUnavailable(step: ExperimentsDashboardGuideStep): boolean { + return !!step.optional && !!step.selector && !this.findTarget(step.selector); + } + + /** + * Prefer the single-experiment workbench path over compare-workspace when both + * could be considered "available" after opening a card. + */ + private getNavigableStepIndex(startIndex: number, direction: 1 | -1): number | null { + const steps = this.activeSteps(); + + for (let index = startIndex; index >= 0 && index < steps.length; index += direction) { + const step = steps[index]; + if (this.isOptionalStepUnavailable(step)) { + continue; + } + + // If we are moving forward from the open-experiment step, never land on + // compare-workspace while the workbench target exists (or may still mount). + if ( + direction === 1 + && step.id === 'compare-workspace' + && this.findTarget('[data-guide="dashboard-detail-card"]') + ) { + continue; + } + + return index; + } + + return null; + } + + private ensureCompareModeOff(): void { + const done = [...this.document.querySelectorAll('.compare-workspace button')].find((button) => + /^done$/i.test((button.textContent || '').trim()) + ); + if (done instanceof HTMLElement) done.click(); + + const clear = [...this.document.querySelectorAll('.compare-selection-btn')].find((button) => + /^clear$/i.test((button.textContent || '').trim()) + ); + if (clear instanceof HTMLElement) clear.click(); + } + + private expandRect(rect: DOMRect, padding = 10): GuideRect { + let left = rect.left - padding; + let top = rect.top - padding; + let right = rect.right + padding; + let bottom = rect.bottom + padding; + + // A target taller than the screen (the workbench) must not draw its ring off the top. + if (rect.height > window.innerHeight) { + top = Math.max(top, 8); + bottom = Math.min(bottom, window.innerHeight - 8); + // Scrolled mostly off screen: the two clamps can cross, so keep the ring non-inverted. + bottom = Math.max(bottom, top); + } + + return { + top, + left, + right, + bottom, + width: Math.max(right - left, 0), + height: Math.max(bottom - top, 0), + }; + } + + private findTarget(selector: string): HTMLElement | null { + const target = this.document.querySelector(selector); + if (!(target instanceof HTMLElement)) { + return null; + } + + const rect = target.getBoundingClientRect(); + if (rect.width <= 0 || rect.height <= 0) { + return null; + } + + return target; + } + + private hasAutoStarted(): boolean { + try { + return localStorage.getItem(this.autoStartStorageKey) === 'true'; + } catch { + return false; + } + } + + private markAutoStarted(): void { + try { + localStorage.setItem(this.autoStartStorageKey, 'true'); + } catch { + // Ignore storage failures and keep the guide functional. + } + } +} diff --git a/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.content.ts b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.content.ts new file mode 100644 index 000000000..299cc4c06 --- /dev/null +++ b/src/app/pages/experiments-dashboard/guide/experiments-dashboard-guide.content.ts @@ -0,0 +1,137 @@ +import { + EXPERIMENT_STUDIO_GUIDE_LABELS, + GuideSection, +} from '../../experiment-studio/guide/experiment-studio-guide.content'; + +export { EXPERIMENT_STUDIO_GUIDE_LABELS }; + +type ExperimentsDashboardGuidePlacement = 'top' | 'right' | 'bottom' | 'left' | 'center'; + +export interface ExperimentsDashboardGuideStep { + id: string; + section: GuideSection; + title: string; + body: string; + selector?: string; + placement?: ExperimentsDashboardGuidePlacement; + allowTargetInteraction?: boolean; + advanceOnTargetClick?: boolean; + requirementHint?: string; + maskBackground?: string; + /** When true, skipped only if selector is missing/zero-size. Never put Complete behind optional steps. */ + optional?: boolean; +} + +/** + * Order matters: open → workbench → actions → results, then optional compare, + * then Guide Complete LAST. compare-workspace must not outrank Complete. + */ +export const EXPERIMENTS_DASHBOARD_GUIDE_STEPS: ExperimentsDashboardGuideStep[] = [ + { + id: 'dashboard-overview', + section: 'Explore', + title: 'My experiments', + body: 'This is your experiments home (<strong>My experiments</strong> in the header). From here you can create a new experiment, reopen an existing one, see experiments shared with you, or compare multiple runs. Open Studio anytime from the header <strong>Studio</strong> link.', + maskBackground: 'transparent', + }, + { + id: 'workspace', + section: 'Explore', + title: 'Experiment list', + body: 'The left pane is your experiment history. Select a run to inspect it in the workbench on the right, or use <strong>New</strong> to start from scratch in Studio.', + selector: '[data-guide="dashboard-workspace"]', + placement: 'right', + }, + { + id: 'search', + section: 'Explore', + title: 'Search and Date Filter', + body: 'Use the search input and date filter to narrow the experiment list.', + selector: '[data-guide="dashboard-search"]', + placement: 'bottom', + allowTargetInteraction: true, + }, + { + id: 'tabs', + section: 'Explore', + title: 'My Experiments and Shared', + body: 'Switch between experiments you own and experiments shared with you. Shared stays empty until someone shares a run with you.', + selector: '[data-guide="dashboard-tabs"]', + placement: 'bottom', + allowTargetInteraction: true, + }, + { + id: 'new-experiment', + section: 'Experiment', + title: 'New experiment', + body: 'Use <strong>New</strong> to open Experiment Studio and create an experiment from scratch. You can also jump there with the header <strong>Studio</strong> link.', + selector: '[data-guide="dashboard-new"]', + placement: 'bottom', + }, + { + id: 'tutorial-experiment', + section: 'Results', + title: 'Open an Experiment', + body: 'Click an experiment in the list to load it into the workbench. If the highlighted tutorial example is available, use that one to continue the flow.', + selector: '[data-guide="dashboard-experiment-card"]', + placement: 'right', + allowTargetInteraction: true, + advanceOnTargetClick: true, + requirementHint: 'Click the highlighted experiment row to open it in the workbench and continue.', + optional: true, + }, + { + id: 'workbench', + section: 'Explore', + title: 'Experiment Workbench', + body: 'After you select an experiment, this workbench shows its details, configuration, and stored results.', + selector: '[data-guide="dashboard-detail-card"]', + placement: 'left', + allowTargetInteraction: true, + optional: true, + }, + { + id: 'actions', + section: 'Results', + title: 'Experiment Actions', + body: 'From here you can open the run in Studio or use the toolbar actions on this experiment (export, copy link, share, or delete when available).', + selector: '[data-guide="dashboard-detail-actions"]', + placement: 'left', + optional: true, + }, + { + id: 'results', + section: 'Results', + title: 'Results Review', + body: 'This section displays the stored result for the selected experiment.', + selector: '[data-guide="dashboard-results"]', + placement: 'left', + allowTargetInteraction: true, + optional: true, + }, + { + id: 'compare', + section: 'Explore', + title: 'Compare Mode', + body: 'Optional: tick the checkbox on two or three runs. <strong>Compare</strong> appears under the list. You can skip this and finish the guide.', + selector: '[data-guide="dashboard-compare"]', + placement: 'bottom', + allowTargetInteraction: true, + }, + { + id: 'compare-workspace', + section: 'Results', + title: 'Comparison Workspace', + body: 'Press <strong>Compare</strong> to put the ticked runs side by side. Or press <strong>Skip</strong> to finish without comparing.', + selector: '[data-guide="dashboard-compare-workspace"]', + placement: 'left', + allowTargetInteraction: true, + optional: true, + }, + { + id: 'dashboard-guide-complete', + section: 'Results', + title: 'Guide Complete', + body: 'You successfully completed the dashboard guide.', + }, +]; diff --git a/src/app/pages/experiments-dashboard/shared/experiment-status/experiment-status.component.css b/src/app/pages/experiments-dashboard/shared/experiment-status/experiment-status.component.css new file mode 100644 index 000000000..44f4eabd5 --- /dev/null +++ b/src/app/pages/experiments-dashboard/shared/experiment-status/experiment-status.component.css @@ -0,0 +1,52 @@ +:host { + display: inline-flex; + flex-shrink: 0; +} + +.status-chip { + display: inline-flex; + align-items: center; + gap: 5px; + padding: 3px 10px; + border-radius: 999px; + border: 1px solid transparent; + background: #f1f5f9; + color: #475569; + font-size: 0.68rem; + font-weight: 700; + line-height: 1.25; + letter-spacing: 0.06em; + text-transform: uppercase; + white-space: nowrap; +} + +.status-chip i { + font-size: 0.68rem; +} + +.status-chip--compact { + padding: 2px 8px; + font-size: 0.65rem; +} + +.status-chip--compact i { + font-size: 0.65rem; +} + +.status-chip.completed { + background: #ecfdf5; + color: #065f46; + border-color: rgba(16, 185, 129, 0.2); +} + +.status-chip.error { + background: #fff1f2; + color: #991b1b; + border-color: rgba(225, 29, 72, 0.2); +} + +.status-chip.pending { + background: #fffbeb; + color: #92400e; + border-color: rgba(245, 158, 11, 0.24); +} diff --git a/src/app/pages/experiments-dashboard/shared/experiment-status/experiment-status.component.ts b/src/app/pages/experiments-dashboard/shared/experiment-status/experiment-status.component.ts new file mode 100644 index 000000000..fb6f41e0e --- /dev/null +++ b/src/app/pages/experiments-dashboard/shared/experiment-status/experiment-status.component.ts @@ -0,0 +1,50 @@ +import { ChangeDetectionStrategy, Component, computed, input } from '@angular/core'; + +export type StatusTone = 'success' | 'error' | 'pending' | 'neutral'; + +interface Chip { + tone: StatusTone; + label: string; + icon: string; +} + +/** Chip wording and icon per status. ExperimentDAO.Status is exactly these three values. */ +const STATUS_PRESENTATION: Record<string, Chip> = { + success: { tone: 'success', label: 'Completed', icon: 'fa-circle-check' }, + error: { tone: 'error', label: 'Failed', icon: 'fa-circle-exclamation' }, + pending: { tone: 'pending', label: 'Pending', icon: 'fa-clock' }, +}; + +/** What a status renders as, so the chip and the list's poll-for-updates agree on one word list. */ +export const statusChip = (status: string | null | undefined): Chip => { + const key = (status || '').toLowerCase(); + return STATUS_PRESENTATION[key] ?? { + tone: 'neutral', + label: key ? key.charAt(0).toUpperCase() + key.replace(/[_-]+/g, ' ').slice(1) : 'Unknown', + icon: 'fa-circle-info', + }; +}; + +@Component({ + selector: 'app-experiment-status', + template: ` + <span + class="status-chip" + [class.status-chip--compact]="compact()" + [class.completed]="chip().tone === 'success'" + [class.error]="chip().tone === 'error'" + [class.pending]="chip().tone === 'pending'" + > + <i class="fas" [class]="chip().icon" aria-hidden="true"></i> + <span [textContent]="chip().label"></span> + </span> + `, + styleUrl: './experiment-status.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class ExperimentStatusComponent { + readonly status = input.required<string>(); + readonly compact = input(false); + + protected readonly chip = computed(() => statusChip(this.status())); +} diff --git a/src/app/pages/notebook/hub-http.spec.ts b/src/app/pages/notebook/hub-http.spec.ts new file mode 100644 index 000000000..307e67c42 --- /dev/null +++ b/src/app/pages/notebook/hub-http.spec.ts @@ -0,0 +1,59 @@ +import { clearHubXsrfCache, hubFetch, hubUrl, readXsrfToken } from './hub-http'; + +const HUB_HOME_HTML = '<script>xsrf_token: "page-xsrf-token",</script>'; + +describe('hub-http', () => { + beforeEach(() => { + clearHubXsrfCache(); + document.cookie = '_xsrf=; Max-Age=0'; + }); + + afterEach(() => { + document.cookie = '_xsrf=; Max-Age=0'; + clearHubXsrfCache(); + }); + + it('hubUrl joins base path and hub route', () => { + expect(hubUrl('/notebook', '/hub/api/user')).toBe('/notebook/hub/api/user'); + }); + + it('readXsrfToken returns the _xsrf cookie', () => { + document.cookie = '_xsrf=test-token'; + expect(readXsrfToken()).toBe('test-token'); + }); + + it('hubFetch parses xsrf_token from /hub/home when the cookie is not readable', async () => { + const fetchSpy = vi.spyOn(window, 'fetch').mockImplementation(async (input: RequestInfo | URL) => { + const url = String(input); + if (url.includes('/hub/home')) { + return new Response(HUB_HOME_HTML, { status: 200 }); + } + if (url.includes('/hub/api/user')) { + return new Response(null, { status: 200 }); + } + return new Response(null, { status: 404 }); + }); + + await hubFetch('/notebook', '/hub/api/user'); + + const homeCall = fetchSpy.mock.calls.find(([url]) => String(url).includes('/hub/home')); + const apiCall = fetchSpy.mock.calls.find(([url]) => String(url).includes('/hub/api/user')); + expect(homeCall).toBeDefined(); + expect(apiCall).toBeDefined(); + expect(String(apiCall![0])).toContain('_xsrf=page-xsrf-token'); + expect((apiCall![1] as RequestInit).headers).toEqual(expect.any(Headers)); + expect(((apiCall![1] as RequestInit).headers as Headers).get('X-XSRFToken')).toBe('page-xsrf-token'); + }); + + it('hubFetch uses the readable cookie token when available', async () => { + document.cookie = '_xsrf=cookie-xsrf-token'; + const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 200 })); + + await hubFetch('/notebook', '/hub/api/user'); + + const apiCall = fetchSpy.mock.calls.find(([url]) => String(url).includes('/hub/api/user')); + expect(String(apiCall![0])).toContain('_xsrf=cookie-xsrf-token'); + expect(((apiCall![1] as RequestInit).headers as Headers).get('X-XSRFToken')).toBe('cookie-xsrf-token'); + expect(fetchSpy.mock.calls.some(([url]) => String(url).includes('/hub/home'))).toBe(false); + }); +}); diff --git a/src/app/pages/notebook/hub-http.ts b/src/app/pages/notebook/hub-http.ts new file mode 100644 index 000000000..d08a7b0a7 --- /dev/null +++ b/src/app/pages/notebook/hub-http.ts @@ -0,0 +1,128 @@ +function normalizeBasePath(basePath: string): string { + if (!basePath) { + return '/notebook'; + } + const withLeadingSlash = basePath.startsWith('/') ? basePath : `/${basePath}`; + return withLeadingSlash.endsWith('/') ? withLeadingSlash.slice(0, -1) : withLeadingSlash; +} + +export function hubUrl(basePath: string, path: string): string { + const normalizedBase = normalizeBasePath(basePath); + const normalizedPath = path.startsWith('/') ? path : `/${path}`; + return `${normalizedBase}${normalizedPath}`; +} + +const XSRF_TOKEN_PATTERN = /xsrf_token:\s*"([^"]+)"/; +const XSRF_CACHE_MS = 30_000; + +let cachedXsrf: { token: string; fetchedAt: number } | null = null; + +function readXsrfTokenFromDocument(doc: Document): string | null { + const match = doc.cookie.match(/(?:^|;\s*)_xsrf=([^;]*)/); + if (!match) { + return null; + } + return match[1]; +} + +export function readXsrfToken(): string | null { + return readXsrfTokenFromDocument(document); +} + +export function clearHubXsrfCache(): void { + cachedXsrf = null; +} + +function cacheXsrfToken(token: string): string { + cachedXsrf = { token, fetchedAt: Date.now() }; + return token; +} + +function parseXsrfTokenFromHtml(html: string): string | null { + const match = html.match(XSRF_TOKEN_PATTERN); + return match?.[1] ?? null; +} + +async function fetchXsrfTokenFromHubPage(basePath: string): Promise<string | null> { + try { + const response = await fetch(hubUrl(basePath, '/hub/home'), { + method: 'GET', + credentials: 'include', + cache: 'no-store', + }); + if (!response.ok) { + return null; + } + const html = await response.text(); + return parseXsrfTokenFromHtml(html); + } catch { + return null; + } +} + +export async function ensureHubXsrf(basePath: string, forceRefresh = false): Promise<string | null> { + if (!forceRefresh && cachedXsrf && (Date.now() - cachedXsrf.fetchedAt) < XSRF_CACHE_MS) { + return cachedXsrf.token; + } + + const cookieToken = readXsrfToken(); + if (cookieToken && !forceRefresh) { + return cacheXsrfToken(cookieToken); + } + + const pageToken = await fetchXsrfTokenFromHubPage(basePath); + if (pageToken) { + return cacheXsrfToken(pageToken); + } + + cachedXsrf = null; + return null; +} + +export async function primeHubSession(basePath: string): Promise<void> { + await ensureHubXsrf(basePath); +} + +function appendXsrfQuery(url: string, xsrf: string): string { + const separator = url.includes('?') ? '&' : '?'; + return `${url}${separator}_xsrf=${encodeURIComponent(xsrf)}`; +} + +export async function hubFetch( + basePath: string, + path: string, + init: RequestInit = {}, + retryOnXsrf = true, +): Promise<Response> { + const xsrf = await ensureHubXsrf(basePath); + const headers = new Headers(init.headers); + if (xsrf) { + headers.set('X-XSRFToken', xsrf); + } + + let url = hubUrl(basePath, path); + if (xsrf && (!init.method || init.method === 'GET')) { + url = appendXsrfQuery(url, xsrf); + } + + const response = await fetch(url, { + ...init, + credentials: 'include', + cache: 'no-store', + headers, + }); + + if ( + retryOnXsrf + && (response.status === 403 || response.status === 400) + && xsrf + ) { + const body = await response.clone().text(); + if (body.includes('_xsrf') || body.includes('XSRF')) { + clearHubXsrfCache(); + return hubFetch(basePath, path, init, false); + } + } + + return response; +} diff --git a/src/app/pages/notebook/hub-session.spec.ts b/src/app/pages/notebook/hub-session.spec.ts new file mode 100644 index 000000000..ba9693d76 --- /dev/null +++ b/src/app/pages/notebook/hub-session.spec.ts @@ -0,0 +1,146 @@ +import { + buildHubLoginUrl, + buildHubReturnPath, + clearHubLoginState, + ensureHubSession, + hasHubSession, + markHubLoginPending, + markHubLoginRedirect, + redirectToHubLogin, + shouldRedirectToHubLogin, + waitForHubSession, +} from './hub-session'; + +function isHubUserRequest(url: string): boolean { + return url.includes('/hub/api/user'); +} + +function isHubHomeRequest(url: string): boolean { + return url.includes('/hub/home'); +} + +describe('hub-session', () => { + beforeEach(() => { + document.cookie = '_xsrf=test-token'; + }); + + afterEach(() => { + window.history.replaceState({}, '', '/'); + sessionStorage.clear(); + document.cookie = '_xsrf=; Max-Age=0'; + }); + + it('buildHubReturnPath uses the Hub home route', () => { + expect(buildHubReturnPath('/notebook')).toBe('/notebook/hub/home'); + }); + + it('buildHubLoginUrl encodes the return path', () => { + expect(buildHubLoginUrl('/notebook')).toBe('/notebook/hub/login?next=%2Fnotebook%2Fhub%2Fhome'); + }); + + it('hasHubSession returns true when /hub/api/user responds with 200', async () => { + const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 200 })); + + await expect(hasHubSession('/notebook')).resolves.toEqual(true); + + const apiCall = fetchSpy.mock.calls.find(([url]) => isHubUserRequest(String(url))); + expect(apiCall).toBeDefined(); + expect(String(apiCall![0])).toContain('_xsrf='); + expect((apiCall![1] as RequestInit).credentials).toBe('include'); + }); + + it('hasHubSession returns false when /hub/api/user is unauthorized', async () => { + vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 401 })); + + await expect(hasHubSession('/notebook')).resolves.toEqual(false); + }); + + it('waitForHubSession retries until the Hub session is available', async () => { + let apiCalls = 0; + vi.spyOn(window, 'fetch').mockImplementation(async (input: RequestInfo | URL) => { + const url = String(input); + if (isHubUserRequest(url)) { + apiCalls += 1; + if (apiCalls < 3) { + return new Response(null, { status: 401 }); + } + return new Response(null, { status: 200 }); + } + if (isHubHomeRequest(url)) { + return new Response(null, { status: 200 }); + } + return new Response(null, { status: 404 }); + }); + + await expect(waitForHubSession('/notebook', { retries: 4, delayMs: 0 })) + .resolves.toBe(true); + expect(apiCalls).toBe(3); + }); + + it('redirectToHubLogin marks pending state and calls navigate', () => { + const navigate = vi.fn(); + + redirectToHubLogin('/notebook', undefined, navigate); + + expect(navigate).toHaveBeenCalledWith(buildHubLoginUrl('/notebook')); + expect(sessionStorage.getItem('mip_notebook_hub_login_pending')).toBe('1'); + }); + + it('shouldRedirectToHubLogin blocks repeated redirects within the cooldown window', () => { + markHubLoginRedirect(); + + expect(shouldRedirectToHubLogin()).toBe(false); + }); + + it('clearHubLoginState allows another redirect attempt', () => { + markHubLoginRedirect(); + markHubLoginPending(); + clearHubLoginState(); + + expect(shouldRedirectToHubLogin()).toBe(true); + expect(sessionStorage.getItem('mip_notebook_hub_login_pending')).toBeNull(); + }); + + it('ensureHubSession returns ready when the Hub session already exists', async () => { + vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 200 })); + + await expect(ensureHubSession('/notebook')).resolves.toEqual('ready'); + }); + + it('ensureHubSession uses extended wait when login is pending', async () => { + markHubLoginPending(); + let apiCalls = 0; + vi.spyOn(window, 'fetch').mockImplementation(async (input: RequestInfo | URL) => { + const url = String(input); + if (isHubUserRequest(url)) { + apiCalls += 1; + if (apiCalls < 5) { + return new Response(null, { status: 401 }); + } + return new Response(null, { status: 200 }); + } + if (isHubHomeRequest(url)) { + return new Response(null, { status: 200 }); + } + return new Response(null, { status: 404 }); + }); + + await expect(ensureHubSession('/notebook')).resolves.toEqual('ready'); + expect(apiCalls).toBe(5); + }, 15000); + + it('ensureHubSession returns failed when redirect is throttled', async () => { + markHubLoginRedirect(); + vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 401 })); + + await expect(ensureHubSession('/notebook')).resolves.toEqual('failed'); + }, 15000); + + it('ensureHubSession returns failed without redirecting once the caller is gone', async () => { + vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 401 })); + + await expect(ensureHubSession('/notebook', () => false)).resolves.toEqual('failed'); + // No redirect was recorded, so the page was never sent to the Hub login. + expect(shouldRedirectToHubLogin()).toBe(true); + }, 15000); +}); diff --git a/src/app/pages/notebook/hub-session.ts b/src/app/pages/notebook/hub-session.ts new file mode 100644 index 000000000..99324ad85 --- /dev/null +++ b/src/app/pages/notebook/hub-session.ts @@ -0,0 +1,154 @@ +import { clearHubXsrfCache, hubFetch, primeHubSession } from './hub-http'; + +export { primeHubSession } from './hub-http'; + +const HUB_LOGIN_REDIRECT_KEY = 'mip_notebook_hub_login_redirect_ts'; +const HUB_LOGIN_PENDING_KEY = 'mip_notebook_hub_login_pending'; +const HUB_LOGIN_COOLDOWN_MS = 15_000; +const DEFAULT_WAIT_RETRIES = 10; +const PENDING_WAIT_RETRIES = 30; +const WAIT_DELAY_MS = 500; + +export type EnsureHubSessionResult = 'ready' | 'redirected' | 'failed'; + +export async function hasHubSession(basePath: string): Promise<boolean> { + try { + const response = await hubFetch(basePath, '/hub/api/user'); + return response.status === 200; + } catch { + return false; + } +} + +interface WaitForHubSessionOptions { + retries?: number; + delayMs?: number; +} + +export async function waitForHubSession( + basePath: string, + options: WaitForHubSessionOptions = {}, +): Promise<boolean> { + const retries = options.retries ?? DEFAULT_WAIT_RETRIES; + const delayMs = options.delayMs ?? WAIT_DELAY_MS; + + for (let attempt = 0; attempt < retries; attempt += 1) { + await primeHubSession(basePath); + if (await hasHubSession(basePath)) { + return true; + } + if (attempt < retries - 1) { + await new Promise((resolve) => setTimeout(resolve, delayMs)); + } + } + + return false; +} + +export function buildHubReturnPath(basePath: string): string { + return `${basePath}/hub/home`; +} + +export function buildHubLoginUrl(basePath: string, returnPath?: string): string { + const nextPath = returnPath ?? buildHubReturnPath(basePath); + const next = encodeURIComponent(nextPath); + return `${basePath}/hub/login?next=${next}`; +} + +function hasHubLoginPending(): boolean { + try { + return sessionStorage.getItem(HUB_LOGIN_PENDING_KEY) === '1'; + } catch { + return false; + } +} + +export function markHubLoginPending(): void { + try { + sessionStorage.setItem(HUB_LOGIN_PENDING_KEY, '1'); + } catch { + // ignore storage errors + } +} + +function clearHubLoginPending(): void { + try { + sessionStorage.removeItem(HUB_LOGIN_PENDING_KEY); + } catch { + // ignore storage errors + } +} + +export function shouldRedirectToHubLogin(): boolean { + try { + const raw = sessionStorage.getItem(HUB_LOGIN_REDIRECT_KEY); + const last = raw ? Number(raw) : 0; + return !last || !Number.isFinite(last) || (Date.now() - last) >= HUB_LOGIN_COOLDOWN_MS; + } catch { + return true; + } +} + +export function markHubLoginRedirect(): void { + try { + sessionStorage.setItem(HUB_LOGIN_REDIRECT_KEY, String(Date.now())); + } catch { + // ignore storage errors + } +} + +function clearHubLoginRedirect(): void { + try { + sessionStorage.removeItem(HUB_LOGIN_REDIRECT_KEY); + } catch { + // ignore storage errors + } +} + +export function clearHubLoginState(): void { + clearHubLoginRedirect(); + clearHubLoginPending(); + clearHubXsrfCache(); +} + +export function redirectToHubLogin( + basePath: string, + returnPath?: string, + navigate: (url: string) => void = (url) => window.location.assign(url), +): void { + if (!shouldRedirectToHubLogin()) { + return; + } + markHubLoginPending(); + markHubLoginRedirect(); + navigate(buildHubLoginUrl(basePath, returnPath)); +} + +/** `canRedirect` lets a caller that has gone away (a destroyed component) skip the Hub login redirect. */ +export async function ensureHubSession( + basePath: string, + canRedirect: () => boolean = () => true, +): Promise<EnsureHubSessionResult> { + if (await hasHubSession(basePath)) { + clearHubLoginState(); + return 'ready'; + } + + const returningFromLogin = hasHubLoginPending(); + const sessionReady = await waitForHubSession(basePath, { + retries: returningFromLogin ? PENDING_WAIT_RETRIES : DEFAULT_WAIT_RETRIES, + delayMs: WAIT_DELAY_MS, + }); + + if (sessionReady) { + clearHubLoginState(); + return 'ready'; + } + + if (!shouldRedirectToHubLogin() || !canRedirect()) { + return 'failed'; + } + + redirectToHubLogin(basePath); + return 'redirected'; +} diff --git a/src/app/pages/notebook/jupyterhub-api.spec.ts b/src/app/pages/notebook/jupyterhub-api.spec.ts new file mode 100644 index 000000000..60813d841 --- /dev/null +++ b/src/app/pages/notebook/jupyterhub-api.spec.ts @@ -0,0 +1,119 @@ +import type { MockInstance } from 'vitest'; +import { + HubApiError, + buildLabUrl, + ensureServerRunning, + getHubUser, + isServerReady, + startServer, + waitForServer, +} from './jupyterhub-api'; + +function hubUserResponse(name: string, ready: boolean, pending: string | null = null): Response { + return new Response(JSON.stringify({ + name, + servers: { + '': { + ready, + pending, + }, + }, + }), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); +} + +describe('jupyterhub-api', () => { + let fetchSpy: MockInstance<typeof fetch>; + + beforeEach(() => { + fetchSpy = vi.spyOn(window, 'fetch').mockReturnValue(undefined as any); + document.cookie = '_xsrf=test-token'; + }); + + afterEach(() => { + document.cookie = '_xsrf=; Max-Age=0'; + }); + + it('buildLabUrl returns the lab path for a user', () => { + expect(buildLabUrl('/notebook', 'alice')).toBe('/notebook/user/alice/lab'); + }); + + it('getHubUser loads the current hub user', async () => { + fetchSpy.mockResolvedValue(hubUserResponse('alice', true)); + + const user = await getHubUser('/notebook'); + + expect(user.name).toBe('alice'); + expect(isServerReady(user)).toBe(true); + }); + + it('startServer sends POST with XSRF header', async () => { + fetchSpy.mockResolvedValue(new Response(null, { status: 202 })); + + await startServer('/notebook', 'alice'); + + expect(fetchSpy).toHaveBeenCalled(); + const [, init] = fetchSpy.mock.lastCall! as [string, RequestInit]; + expect(init.method).toBe('POST'); + expect((init.headers as Headers).get('X-XSRFToken')).toBe('test-token'); + }); + + it('ensureServerRunning skips POST when server is already ready', async () => { + fetchSpy.mockResolvedValue(hubUserResponse('alice', true)); + + const user = await ensureServerRunning('/notebook'); + + expect(user.name).toBe('alice'); + const userCalls = fetchSpy.mock.calls.filter(([url]) => String(url).includes('/hub/api/user')); + expect(userCalls.length).toBe(1); + }); + + it('ensureServerRunning does not repeat start POST while polling', async () => { + let userCalls = 0; + fetchSpy.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + if (url.includes('/hub/api/users/alice/server') && init?.method === 'POST') { + return new Response(null, { status: 202 }); + } + if (url.includes('/hub/api/user')) { + userCalls += 1; + return hubUserResponse('alice', userCalls >= 4, null); + } + return new Response(null, { status: 404 }); + }); + + const user = await ensureServerRunning('/notebook', { + pollIntervalMs: 10, + timeoutMs: 200, + }); + + const startCalls = fetchSpy.mock.calls.filter(([url, init]) => ( + String(url).includes('/hub/api/users/alice/server') + && (init as RequestInit | undefined)?.method === 'POST' + )); + expect(user.name).toBe('alice'); + expect(startCalls.length).toBe(1); + }); + + it('waitForServer times out when server never becomes ready', async () => { + fetchSpy.mockImplementation(async () => hubUserResponse('alice', false, 'spawn')); + + await expect(waitForServer('/notebook', { + pollIntervalMs: 10, + timeoutMs: 50, + })).rejects.toEqual(expect.objectContaining({ + name: 'HubApiError', + status: 408, + })); + }); + + it('getHubUser throws HubApiError on 401', async () => { + fetchSpy.mockResolvedValue(new Response(null, { status: 401 })); + + await expect(getHubUser('/notebook')).rejects.toEqual( + new HubApiError('Hub session is not authenticated', 401), + ); + }); +}); diff --git a/src/app/pages/notebook/jupyterhub-api.ts b/src/app/pages/notebook/jupyterhub-api.ts new file mode 100644 index 000000000..bd9448a70 --- /dev/null +++ b/src/app/pages/notebook/jupyterhub-api.ts @@ -0,0 +1,143 @@ +export interface HubServerState { + name?: string; + ready?: boolean; + pending?: string | null; + url?: string; + progress_url?: string; + started?: string; +} + +export interface HubUser { + name: string; + server?: string | null; + servers?: Record<string, HubServerState>; +} + +import { hubFetch, primeHubSession, readXsrfToken } from './hub-http'; + +export class HubApiError extends Error { + constructor( + message: string, + readonly status: number, + ) { + super(message); + this.name = 'HubApiError'; + } +} + +const DEFAULT_SERVER_NAME = ''; + +function getDefaultServerState(user: HubUser): HubServerState | undefined { + return user.servers?.[DEFAULT_SERVER_NAME]; +} + +export function isServerReady(user: HubUser): boolean { + const server = getDefaultServerState(user); + return Boolean(server?.ready); +} + +function isServerPending(user: HubUser): boolean { + const server = getDefaultServerState(user); + return Boolean(server?.pending); +} + +export async function getHubUser(basePath: string): Promise<HubUser> { + const response = await hubFetch(basePath, '/hub/api/user'); + if (response.status === 401 || response.status === 403) { + throw new HubApiError('Hub session is not authenticated', response.status); + } + if (!response.ok) { + throw new HubApiError(`Failed to load Hub user (${response.status})`, response.status); + } + return response.json() as Promise<HubUser>; +} + +async function postStartServer(basePath: string, username: string): Promise<Response> { + return hubFetch(basePath, `/hub/api/users/${encodeURIComponent(username)}/server`, { + method: 'POST', + }); +} + +export async function startServer(basePath: string, username: string): Promise<void> { + let response = await postStartServer(basePath, username); + + if ((response.status === 403 || response.status === 400) && !readXsrfToken()) { + await primeHubSession(basePath); + response = await postStartServer(basePath, username); + } + + if (response.status === 401 || response.status === 403) { + const body = await response.text(); + throw new HubApiError(body || 'Hub session is not authenticated', response.status); + } + if (response.status === 201 || response.status === 202 || response.status === 200) { + return; + } + if (response.status === 400) { + const body = await response.text(); + throw new HubApiError(body || 'Failed to start notebook server', response.status); + } + if (!response.ok) { + throw new HubApiError(`Failed to start notebook server (${response.status})`, response.status); + } +} + +export function buildLabUrl(basePath: string, username: string): string { + const normalizedBase = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath; + return `${normalizedBase}/user/${encodeURIComponent(username)}/lab`; +} + +interface WaitForServerOptions { + pollIntervalMs?: number; + timeoutMs?: number; + onProgress?: (message: string) => void; +} + +function sleep(ms: number): Promise<void> { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +export async function waitForServer( + basePath: string, + options: WaitForServerOptions = {}, +): Promise<HubUser> { + const pollIntervalMs = options.pollIntervalMs ?? 2000; + const timeoutMs = options.timeoutMs ?? 120_000; + const deadline = Date.now() + timeoutMs; + + while (Date.now() < deadline) { + const user = await getHubUser(basePath); + if (isServerReady(user)) { + return user; + } + + if (isServerPending(user)) { + options.onProgress?.('Starting your notebook server…'); + } else { + options.onProgress?.('Preparing your notebook server…'); + } + + await sleep(pollIntervalMs); + } + + throw new HubApiError('Timed out waiting for notebook server to start', 408); +} + +export async function ensureServerRunning( + basePath: string, + options: WaitForServerOptions = {}, +): Promise<HubUser> { + const user = await getHubUser(basePath); + if (isServerReady(user)) { + return user; + } + + if (!isServerPending(user)) { + options.onProgress?.('Starting your notebook server…'); + await startServer(basePath, user.name); + } else { + options.onProgress?.('Starting your notebook server…'); + } + + return waitForServer(basePath, options); +} diff --git a/src/app/pages/notebook/notebook.component.css b/src/app/pages/notebook/notebook.component.css index 84d720377..09e05cbbf 100644 --- a/src/app/pages/notebook/notebook.component.css +++ b/src/app/pages/notebook/notebook.component.css @@ -1,8 +1,12 @@ +:host { + display: block; + min-height: calc(100dvh - var(--header-height)); +} + .notebook-container { width: 100%; - margin-top: var(--header-height); - height: calc(100vh - var(--header-height) - var(--footer-height, 72px)); - padding: 0 32px; + min-height: calc(100dvh - var(--header-height)); + padding: 12px 32px; box-sizing: border-box; display: flex; flex-direction: column; @@ -10,56 +14,45 @@ } .jupyter-iframe { - flex: 1; width: 100%; + min-height: calc(100dvh - var(--header-height) - 24px); border: none; - height: 100%; } .loading-overlay { position: absolute; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - font-size: 1.2rem; - color: #555; - z-index: 10; -} - -.notebook-login-gate { - flex: 1; + inset: 12px 32px; display: flex; + flex-direction: column; align-items: center; justify-content: center; - padding: 24px 0; -} - -.notebook-login-card { - width: min(720px, 100%); - background: var(--card-bg, #fff); - border: 1px solid var(--border-color, rgba(0, 0, 0, 0.1)); - border-radius: 12px; - padding: 20px; - box-shadow: var(--shadow-md, 0 4px 6px -1px rgb(0 0 0 / 0.1)); + gap: 8px; + background: rgba(255, 255, 255, 0.92); + z-index: 10; } -.notebook-login-card h2 { - margin: 0 0 8px; +.loading-message { + margin: 0; + font-size: 1.2rem; + color: #334155; + font-weight: 600; } -.notebook-login-card p { - margin: 0 0 16px; - color: var(--text-muted, #475569); +.loading-subtext { + margin: 0; + font-size: 0.95rem; + color: #64748b; } -.notebook-login-actions { - display: flex; - align-items: center; - gap: 12px; - flex-wrap: wrap; +.loading-error { + margin: 0; + max-width: min(720px, 100%); + text-align: center; + font-size: 0.95rem; + color: #475569; } -.notebook-login-btn { +.notebook-retry-btn { background: #0d47a1; color: #fff; border: none; @@ -69,16 +62,16 @@ cursor: pointer; } -.notebook-login-btn:hover { +.notebook-retry-btn:hover { filter: brightness(1.05); } -.notebook-login-link { - color: #0d47a1; - text-decoration: none; - font-weight: 600; -} +@media (max-width: 768px) { + .notebook-container { + padding: 8px 16px; + } -.notebook-login-link:hover { - text-decoration: underline; + .loading-overlay { + inset: 8px 16px; + } } diff --git a/src/app/pages/notebook/notebook.component.html b/src/app/pages/notebook/notebook.component.html index 47a83cb8e..b2f36f965 100644 --- a/src/app/pages/notebook/notebook.component.html +++ b/src/app/pages/notebook/notebook.component.html @@ -1,24 +1,20 @@ <div class="notebook-container"> - <div *ngIf="isLoading" class="loading-overlay"> - <p>Loading Notebook Environment...</p> + @if (isLoading()) { + <div class="loading-overlay"> + <p class="loading-message">{{ loadingMessage() }}</p> + @if (spawnError()) { + <p class="loading-error">{{ spawnError() }}</p> + <button type="button" class="notebook-retry-btn" (click)="retrySpawn()"> + Try again + </button> + } @else { + <p class="loading-subtext">This usually takes 30–60 seconds.</p> + } </div> + } - <div *ngIf="needsHubLogin" class="notebook-login-gate"> - <div class="notebook-login-card"> - <h2>Sign in to Notebook</h2> - <p> - Your notebook session is not authenticated yet. We need to complete the login in the main window - (identity providers typically block login flows inside iframes). - </p> - <div class="notebook-login-actions"> - <button type="button" class="notebook-login-btn" (click)="continueToHubLogin()"> - Continue to Notebook Login - </button> - <a *ngIf="hubLoginUrl" class="notebook-login-link" [href]="hubLoginUrl">Open login link</a> - </div> - </div> - </div> - - <iframe *ngIf="jupyterUrl" [src]="jupyterUrl" (load)="onLoad()" title="Jupyter Notebook" class="jupyter-iframe"> + @if (jupyterUrl()) { + <iframe [src]="jupyterUrl()" (load)="onLoad()" title="Jupyter Notebook" class="jupyter-iframe"> </iframe> + } </div> diff --git a/src/app/pages/notebook/notebook.component.spec.ts b/src/app/pages/notebook/notebook.component.spec.ts new file mode 100644 index 000000000..6f3fa599e --- /dev/null +++ b/src/app/pages/notebook/notebook.component.spec.ts @@ -0,0 +1,293 @@ +import type { MockInstance } from 'vitest'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideZonelessChangeDetection } from '@angular/core'; +import { SafeResourceUrl } from '@angular/platform-browser'; +import { BrowserTestingModule } from '@angular/platform-browser/testing'; + +import { NotebookComponent } from './notebook.component'; +import { NotebookNavService } from '../../services/notebook-nav.service'; + +function unwrapSafeUrl(value: SafeResourceUrl | null): string | null { + if (!value) { + return null; + } + if (typeof value === 'string') { + return value; + } + return (value as unknown as { changingThisBreaksApplicationSecurity: string }) + .changingThisBreaksApplicationSecurity; +} + +async function settleComponent( + fixture: ComponentFixture<NotebookComponent>, + rounds = 8, +): Promise<void> { + for (let i = 0; i < rounds; i += 1) { + fixture.detectChanges(); + await fixture.whenStable(); + await new Promise((resolve) => setTimeout(resolve, 0)); + } +} + +function hubUserResponse(name: string, ready: boolean, pending: string | null = null): Response { + return new Response(JSON.stringify({ + name, + servers: { + '': { + ready, + pending, + }, + }, + }), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }); +} + +function isHubHomeRequest(url: string): boolean { + return url.includes('/hub/home'); +} + +function isHubUserRequest(url: string): boolean { + return url.includes('/hub/api/user') && !url.includes('/hub/api/users/'); +} + +function isSpawnRequest(url: string, init?: RequestInit): boolean { + return url.includes('/hub/api/users/alice/server') && init?.method === 'POST'; +} + +describe('NotebookComponent', () => { + let component: NotebookComponent; + let fixture: ComponentFixture<NotebookComponent>; + let fetchSpy: MockInstance<typeof fetch>; + + beforeEach(async () => { + localStorage.clear(); + (window as Window & { __env?: Record<string, string> }).__env = { + JUPYTER_CONTEXT_PATH: '/notebook', + }; + document.cookie = '_xsrf=test-token'; + + fetchSpy = vi.spyOn(window, 'fetch').mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + if (isHubHomeRequest(url)) { + return hubHomeResponse(); + } + if (isHubUserRequest(url)) { + return hubUserResponse('alice', true); + } + if (isSpawnRequest(url, init)) { + return new Response(null, { status: 202 }); + } + return new Response(null, { status: 404 }); + }); + + await TestBed.configureTestingModule({ + imports: [NotebookComponent, BrowserTestingModule], + providers: [provideZonelessChangeDetection()], + }).compileComponents(); + + fixture = TestBed.createComponent(NotebookComponent); + component = fixture.componentInstance; + }); + + afterEach(() => { + localStorage.clear(); + sessionStorage.clear(); + document.cookie = '_xsrf=; Max-Age=0'; + window.history.replaceState({}, '', '/'); + }); + + function hubHomeResponse(): Response { + return new Response('<script>xsrf_token: "test-token",</script>', { status: 200 }); + } + + it('marks the notebook nav as visited on init', async () => { + const notebookNav = TestBed.inject(NotebookNavService); + expect(notebookNav.hasVisited()).toBe(false); + + await settleComponent(fixture); + + expect(notebookNav.hasVisited()).toBe(true); + }); + + it('embeds JupyterLab when the Hub session is available', async () => { + await settleComponent(fixture); + + const userCall = fetchSpy.mock.calls.find(([url]) => isHubUserRequest(String(url))); + expect(userCall).toBeDefined(); + expect((userCall![1] as RequestInit).credentials).toBe('include'); + expect(unwrapSafeUrl(component.jupyterUrl())).toBe('/notebook/user/alice/lab'); + expect(component.spawnError()).toBeNull(); + expect(component.loadingMessage()).toBe('Opening JupyterLab…'); + }); + + it('retries opening JupyterLab after Hub session recovery succeeds', async () => { + let hubUserCalls = 0; + fetchSpy.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + if (isHubHomeRequest(url)) { + return hubHomeResponse(); + } + if (isSpawnRequest(url, init)) { + return new Response(null, { status: 202 }); + } + if (isHubUserRequest(url)) { + hubUserCalls += 1; + if (hubUserCalls === 2) { + return new Response(null, { status: 401 }); + } + return hubUserResponse('alice', true); + } + return new Response(null, { status: 404 }); + }); + + await settleComponent(fixture); + + expect(hubUserCalls).toBeGreaterThanOrEqual(4); + expect(unwrapSafeUrl(component.jupyterUrl())).toBe('/notebook/user/alice/lab'); + expect(component.spawnError()).toBeNull(); + }); + + it('starts the server when it is not running yet', async () => { + let userPolls = 0; + fetchSpy.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + if (isHubHomeRequest(url)) { + return hubHomeResponse(); + } + if (isSpawnRequest(url, init)) { + return new Response(null, { status: 202 }); + } + if (isHubUserRequest(url)) { + userPolls += 1; + if (userPolls <= 3) { + return hubUserResponse('alice', false, null); + } + if (userPolls === 4) { + return hubUserResponse('alice', false, 'spawn'); + } + return hubUserResponse('alice', true, null); + } + return new Response(null, { status: 404 }); + }); + + await settleComponent(fixture); + await new Promise((resolve) => setTimeout(resolve, 4500)); + fixture.detectChanges(); + + const spawnCall = fetchSpy.mock.calls.find(([url, init]) => isSpawnRequest(String(url), init as RequestInit)); + expect(spawnCall).toBeDefined(); + expect(unwrapSafeUrl(component.jupyterUrl())).toBe('/notebook/user/alice/lab'); + }); + + it('surfaces timeout errors inside the loading overlay', async () => { + fetchSpy.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + if (isHubHomeRequest(url)) { + return hubHomeResponse(); + } + if (isHubUserRequest(url)) { + return hubUserResponse('alice', false, 'spawn'); + } + if (isSpawnRequest(url, init)) { + return new Response(null, { status: 202 }); + } + return new Response(null, { status: 404 }); + }); + + await settleComponent(fixture, 2); + + component.spawnError.set('Timed out waiting for notebook server to start. Please try again.'); + component.loadingMessage.set('Notebook server unavailable'); + fixture.detectChanges(); + + expect(fixture.nativeElement.querySelector('.loading-overlay')).not.toBeNull(); + expect(fixture.nativeElement.querySelector('.loading-error')).not.toBeNull(); + expect(fixture.nativeElement.textContent).toContain('Try again'); + }); + + it('keeps the loading overlay visible while spawn is in progress', async () => { + fetchSpy.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + if (isHubHomeRequest(url)) { + return hubHomeResponse(); + } + if (isSpawnRequest(url, init)) { + return new Response(null, { status: 202 }); + } + if (isHubUserRequest(url)) { + return hubUserResponse('alice', false, 'spawn'); + } + return new Response(null, { status: 404 }); + }); + + await settleComponent(fixture, 3); + + expect(component.isLoading()).toBe(true); + expect(component.loadingMessage().toLowerCase()).toContain('notebook server'); + }); + + it('retrySpawn clears the previous error and re-enters loading', async () => { + component.spawnError.set('Timed out waiting for notebook server to start. Please try again.'); + component.isLoading.set(true); + + void component.retrySpawn(); + await settleComponent(fixture, 4); + + expect(component.spawnError()).toBeNull(); + expect(unwrapSafeUrl(component.jupyterUrl())).toBe('/notebook/user/alice/lab'); + }); + + it('shows an error when the Hub session never becomes available', async () => { + sessionStorage.setItem('mip_notebook_hub_login_redirect_ts', String(Date.now())); + fetchSpy.mockImplementation(async (input: RequestInfo | URL) => { + const url = String(input); + if (url.includes('/hub/api/user') || url.includes('/hub/home')) { + return new Response(null, { status: 401 }); + } + return new Response(null, { status: 404 }); + }); + + await settleComponent(fixture, 2); + await new Promise((resolve) => setTimeout(resolve, 5500)); + fixture.detectChanges(); + + expect(component.spawnError()).toContain('Notebook sign-in did not complete'); + expect(component.jupyterUrl()).toBeNull(); + }, 12000); + + it('waits longer when returning from a pending Hub login', async () => { + sessionStorage.setItem('mip_notebook_hub_login_pending', '1'); + let apiCalls = 0; + fetchSpy.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input); + if (url.includes('/hub/home')) { + return hubHomeResponse(); + } + if (url.includes('/hub/api/user')) { + apiCalls += 1; + if (apiCalls < 4) { + return new Response(null, { status: 401 }); + } + return hubUserResponse('alice', true); + } + if (isSpawnRequest(url, init)) { + return new Response(null, { status: 202 }); + } + return new Response(null, { status: 404 }); + }); + + await settleComponent(fixture, 2); + + const deadline = Date.now() + 8000; + while (!unwrapSafeUrl(component.jupyterUrl()) && Date.now() < deadline) { + fixture.detectChanges(); + await fixture.whenStable(); + await new Promise((resolve) => setTimeout(resolve, 250)); + } + + expect(apiCalls).toBeGreaterThanOrEqual(4); + expect(unwrapSafeUrl(component.jupyterUrl())).toBe('/notebook/user/alice/lab'); + }, 12000); +}); diff --git a/src/app/pages/notebook/notebook.component.ts b/src/app/pages/notebook/notebook.component.ts index 68d5f2466..4cc805881 100644 --- a/src/app/pages/notebook/notebook.component.ts +++ b/src/app/pages/notebook/notebook.component.ts @@ -1,143 +1,164 @@ -import { Component, OnInit, inject } from '@angular/core'; +import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject, signal } from '@angular/core'; import { CommonModule } from '@angular/common'; import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'; +import { + clearHubLoginState, + ensureHubSession, + primeHubSession, +} from './hub-session'; +import { + HubApiError, + buildLabUrl, + ensureServerRunning, +} from './jupyterhub-api'; +import { RuntimeEnvService } from '../../services/runtime-env.service'; +import { NotebookNavService } from '../../services/notebook-nav.service'; + @Component({ selector: 'app-notebook', - standalone: true, imports: [CommonModule], templateUrl: './notebook.component.html', - styleUrls: ['./notebook.component.css'] + styleUrl: './notebook.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class NotebookComponent implements OnInit { private sanitizer = inject(DomSanitizer); - private runtimeEnv = (window as any).__env || {}; - private jupyterBasePath = this.normalizeBasePath(this.runtimeEnv.JUPYTER_CONTEXT_PATH || '/notebook'); - private jupyterLandingPath = this.normalizeSubPath(this.runtimeEnv.JUPYTER_LANDING_PATH || '/hub/spawn'); - private hubLoginBounceKey = 'mip_notebook_hub_login_bounced_ts_v2'; + private runtimeEnv = inject(RuntimeEnvService); + private notebookNav = inject(NotebookNavService); + private jupyterBasePath = this.runtimeEnv.jupyterContextPath; - jupyterUrl: SafeResourceUrl | null = null; - isLoading = true; - needsHubLogin = false; - hubLoginUrl: string | null = null; + readonly jupyterUrl = signal<SafeResourceUrl | null>(null); + readonly isLoading = signal(true); + readonly loadingMessage = signal('Preparing your notebook…'); + readonly spawnError = signal<string | null>(null); - ngOnInit() { - // Do not try to run the OAuth/Keycloak flow inside the iframe. - // If the Hub session is missing, bounce the *top window* through the Hub login once, - // then come back and embed Jupyter normally. - void this.initAsync(); - } + /** False once the user leaves the page, so a slow Hub check never redirects a page they moved away from. */ + private active = true; - private buildJupyterUrl(path: string): string { - return `${this.jupyterBasePath}${path}`; + constructor() { + inject(DestroyRef).onDestroy(() => (this.active = false)); } - private normalizeBasePath(path: string): string { - if (!path) { - return '/notebook'; - } - const withLeadingSlash = path.startsWith('/') ? path : `/${path}`; - return withLeadingSlash.endsWith('/') ? withLeadingSlash.slice(0, -1) : withLeadingSlash; - } - - private normalizeSubPath(path: string): string { - if (!path) { - return '/hub/spawn'; + ngOnInit() { + if (this.redirectTopLevelLabToShell()) { + return; } - return path.startsWith('/') ? path : `/${path}`; + this.notebookNav.markVisited(); + void this.initAsync(); } onLoad() { - // Iframe load can fire during initial change detection; defer state mutation. + if (!this.jupyterUrl()) { + return; + } setTimeout(() => { - this.isLoading = false; + this.isLoading.set(false); + this.spawnError.set(null); + clearHubLoginState(); }); } + retrySpawn() { + clearHubLoginState(); + void this.initAsync(); + } + private async initAsync() { - // Check whether the user is logged into the hub. - // /hub/home returns 200 when authenticated, and 302 -> /hub/login otherwise. - const hasHubSession = await this.hasHubSession(); - if (!hasHubSession) { - this.hubLoginUrl = this.buildHubLoginUrl(); - if (this.bounceToHubLoginOnce()) { - return; - } - // Don't try embedding a login flow in an iframe (blocked by most IdPs/browsers). - this.needsHubLogin = true; - this.isLoading = false; + this.beginLoading('Preparing your notebook…'); + + this.loadingMessage.set('Connecting to your notebook…'); + const hubState = await ensureHubSession(this.jupyterBasePath, () => this.active); + if (hubState === 'redirected') { return; } - - // Successful hub session: clear bounce throttle so future expirations can redirect again. - try { - sessionStorage.removeItem(this.hubLoginBounceKey); - } catch { - // ignore storage errors + if (hubState === 'failed') { + this.showSpawnError(new HubApiError( + 'Notebook sign-in did not complete', + 401, + )); + return; } - this.isLoading = true; - this.jupyterUrl = this.sanitizer.bypassSecurityTrustResourceUrl( - this.buildJupyterUrl(this.jupyterLandingPath) - ); + await this.spawnAndOpenLab(); } - private async hasHubSession(): Promise<boolean> { + private async spawnAndOpenLab(sessionRecovered = false) { + this.spawnError.set(null); + this.jupyterUrl.set(null); + this.beginLoading('Checking notebook session…'); + try { - const url = `${this.jupyterBasePath}/hub/home`; - const r = await fetch(url, { - method: 'GET', - credentials: 'include', - redirect: 'manual', - cache: 'no-store', + await primeHubSession(this.jupyterBasePath); + const user = await ensureServerRunning(this.jupyterBasePath, { + onProgress: (message) => this.loadingMessage.set(message), }); - - // Some browsers represent manual redirects as "opaque redirect". - if ((r as any).type === 'opaqueredirect') { - return false; - } - // Some browsers report status 0 for manual redirects; treat as unauthenticated. - if (r.status === 0) { - return false; - } - if (r.status === 200) { - return true; - } - if (r.status >= 300 && r.status < 400) { - return false; + this.loadingMessage.set('Opening JupyterLab…'); + this.jupyterUrl.set(this.sanitizer.bypassSecurityTrustResourceUrl( + buildLabUrl(this.jupyterBasePath, user.name), + )); + } catch (error) { + if (error instanceof HubApiError && error.status === 401) { + const hubState = await ensureHubSession(this.jupyterBasePath, () => this.active); + if (hubState === 'redirected') { + return; + } + if (hubState === 'failed') { + this.showSpawnError(new HubApiError( + 'Unable to sign in to the notebook after multiple attempts', + 401, + )); + } + if (hubState === 'ready' && !sessionRecovered) { + await this.spawnAndOpenLab(true); + return; + } + if (hubState === 'ready') { + this.showSpawnError(new HubApiError( + 'Unable to sign in to the notebook after multiple attempts', + 401, + )); + } + return; } - return false; - } catch { - return false; + + console.error('Failed to start notebook server via Hub API', error); + this.showSpawnError(error); } } - private buildHubLoginUrl(): string { - // Login must happen in top-window, but we want to return to the Angular shell route - // so Jupyter is rendered inside iframe with UI header/footer. - const next = encodeURIComponent('/notebook?hub_auth=1'); - return `${this.jupyterBasePath}/hub/login?next=${next}`; + private beginLoading(message: string) { + this.isLoading.set(true); + this.spawnError.set(null); + this.loadingMessage.set(message); + } + + private showSpawnError(error: unknown) { + const message = error instanceof HubApiError + ? error.message + : 'Unable to start your notebook server'; + + this.jupyterUrl.set(null); + if (message.includes('Please try again')) { + this.spawnError.set(message.endsWith('.') ? message : `${message}.`); + } else { + const suffix = message.endsWith('.') ? '' : '.'; + this.spawnError.set(`${message}${suffix} Please try again.`); + } + this.isLoading.set(true); + this.loadingMessage.set('Notebook server unavailable'); } - private bounceToHubLoginOnce(): boolean { + private redirectTopLevelLabToShell(): boolean { try { - const raw = sessionStorage.getItem(this.hubLoginBounceKey); - const last = raw ? Number(raw) : 0; - const now = Date.now(); - // Avoid loops, but allow retry after a short cooldown (e.g., user cancelled login). - if (last && Number.isFinite(last) && (now - last) < 30_000) { + const path = window.location.pathname; + if (!path.startsWith(`${this.jupyterBasePath}/user/`)) { return false; } - sessionStorage.setItem(this.hubLoginBounceKey, String(now)); + window.location.replace('/notebook'); + return true; } catch { return false; } - - window.location.assign(this.buildHubLoginUrl()); - return true; - } - - continueToHubLogin() { - window.location.assign(this.buildHubLoginUrl()); } } diff --git a/src/app/pages/shared/footer/footer.component.css b/src/app/pages/shared/footer/footer.component.css index a2cd5a968..3df57ca26 100644 --- a/src/app/pages/shared/footer/footer.component.css +++ b/src/app/pages/shared/footer/footer.component.css @@ -1,181 +1,437 @@ +:host { + display: block; + flex-shrink: 0; + --footer-height: 420px; +} + .main-footer { - background: rgba(15, 23, 42, 0.85); - /* Sophisticated Slate-900 with glassmorphism */ - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - color: #ffffff; - padding: 16px 0; - border-top: 1px solid rgba(148, 163, 184, 0.2); - /* Sleek slate-400 border */ - font-family: 'Inter', sans-serif; - font-size: 12px; position: relative; - z-index: 1000; - width: 100%; + overflow: hidden; margin: 0; - box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.4); + color: #fff; + background: color-mix(in srgb, var(--primary-dark) 40%, black); + border-top: 1px solid color-mix(in srgb, var(--primary-light) 18%, transparent); + box-shadow: none; } -/* Subtle gradient top border for premium feel */ -.main-footer::before { - content: ''; - position: absolute; - top: 0; - left: 0; - right: 0; - height: 2px; - background: linear-gradient(90deg, transparent, rgba(51, 102, 255, 0.5), transparent); +.footer-shell { + display: grid; + grid-template-columns: minmax(0, 1.85fr) minmax(170px, 1fr) minmax(0, 1.35fr); + gap: 0; + max-width: 1200px; + margin: 0 auto; + padding: 44px 28px 34px; + position: relative; + z-index: 1; } -.footer-container { - max-width: 1600px; - margin: 0 auto; - padding: 0 40px; +.footer-column { display: flex; - justify-content: space-between; - align-items: center; + flex-direction: column; + min-width: 0; +} + +.footer-column:not(:first-child) { + padding-left: 32px; + border-left: 1px solid rgba(255, 255, 255, 0.08); } -.footer-left, -.footer-center, -.footer-right { +.footer-logo-row { display: flex; align-items: center; + gap: 14px; + margin-bottom: 24px; +} + +.footer-logo { + width: 132px; + height: auto; + filter: brightness(0) invert(1); +} + +.footer-brand-title { + margin: 0 0 16px; + font-size: 1.08rem; + font-weight: 700; + color: #fff; +} + +.footer-brand-desc { + margin: 0; + max-width: 34ch; + font-size: 0.96rem; + line-height: 1.78; + color: rgba(255, 255, 255, 0.68); +} + +.footer-col-title { + margin: 0 0 24px; + font-size: 0.77rem; + font-weight: 700; + text-transform: uppercase; + letter-spacing: 0.16em; + color: rgba(255, 255, 255, 0.42); +} + +.footer-links { + list-style: none; + padding: 0; + margin: 0; } -/* Logos Section */ -.footer-logos { +.footer-links li { + margin-bottom: 12px; +} + +.footer-links a { + color: rgba(255, 255, 255, 0.74); + text-decoration: none; + font-size: 0.95rem; + transition: color 0.2s ease, transform 0.2s ease; +} + +.footer-links a:hover { + color: #fff; + transform: translateX(2px); +} + +.institutional-logos { display: flex; align-items: center; - gap: 32px; + flex-wrap: wrap; + gap: 28px; + margin-bottom: 30px; } -.logo-link { +.partner-link { display: inline-flex; align-items: center; - transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); + justify-content: center; + min-height: 34px; + opacity: 0.56; + filter: grayscale(1) brightness(0) invert(1); + transition: opacity 0.28s ease, transform 0.28s ease; } -.footer-logo { - height: 26px; +.partner-link:hover, +.partner-link:focus-visible { + opacity: 0.92; + transform: translateY(-2px); +} + +.partner-link:focus-visible { + outline: 2px solid rgba(255, 255, 255, 0.35); + outline-offset: 6px; + border-radius: 6px; +} + +.partner-logo { + height: auto; width: auto; + max-width: 132px; + max-height: 34px; object-fit: contain; - opacity: 0.9; - transition: all 0.3s ease; } .athena-logo { - background-color: white; - padding: 1px; - border-radius: 2px; - box-sizing: border-box; - height: 20px; + background: none; + padding: 0; + border-radius: 0; + max-height: 28px; } +.chuv-logo { + max-height: 24px; + filter: none; +} +.footer-funding-section { + padding-top: 18px; + border-top: 1px solid rgba(255, 255, 255, 0.08); +} -.logo-link:hover .footer-logo { - opacity: 1; - transform: scale(1.05); +.footer-funding-title { + margin-bottom: 16px; } -/* Copyright & Legal Section */ -.footer-center { - flex-direction: column; - gap: 8px; +.footer-funding-logos { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 18px; } -.copyright-text { - margin: 0; - color: rgba(255, 255, 255, 0.7); - font-weight: 400; - letter-spacing: 0.01em; +.footer-funding-item { + position: relative; display: flex; - align-items: center; + flex-direction: column; + align-items: flex-start; gap: 6px; + min-width: 0; } -.copyright-symbol { +.footer-funding-item:focus-visible { + outline: 2px solid rgba(127, 156, 232, 0.48); + outline-offset: 6px; + border-radius: 8px; +} + +.footer-funding-visual { + display: inline-flex; + align-items: center; + justify-content: flex-start; + gap: 12px; + width: auto; + min-height: 0; + padding: 0; + border: none; + border-radius: 0; + background: transparent; + box-shadow: none; +} + +.footer-funding-visual-seri { + align-items: flex-start; +} + +.footer-funding-flag { + width: 56px; + height: 37px; + display: block; + border-radius: 4px; + flex-shrink: 0; + object-fit: cover; + box-shadow: 0 10px 18px rgba(0, 0, 0, 0.18); +} + +.footer-funding-box-name { + display: block; + margin: 0; + max-width: 17ch; + font-size: 0.84rem; font-weight: 600; - color: #ffffff; + line-height: 1.35; + color: rgba(255, 255, 255, 0.92); } -.hbp-link, -.ebrains-link { - color: #5d8aff; - text-decoration: none; - font-weight: 500; - position: relative; - padding: 2px 0; +.footer-funding-box-name-seri { + max-width: 16ch; + font-size: 0.78rem; + line-height: 1.32; } -.hbp-link::after, -.ebrains-link::after { - content: ''; +.footer-funding-item:hover .footer-funding-box-name, +.footer-funding-item:focus-visible .footer-funding-box-name { + color: #fff; +} + +.seri-mark { + display: inline-flex; + align-items: center; + gap: 0; + min-width: 0; + flex-shrink: 0; +} + +.seri-mark-flag { + width: 24px; + height: 24px; + border-radius: 6px; + background: + linear-gradient(90deg, transparent 39%, #fff 39%, #fff 61%, transparent 61%), + linear-gradient(180deg, transparent 39%, #fff 39%, #fff 61%, transparent 61%), + #d52b1e; + box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16), 0 6px 12px rgba(0, 0, 0, 0.14); +} + +.footer-funding-tooltip { position: absolute; - bottom: 0; left: 0; - width: 0; - height: 1px; - background: currentColor; - transition: width 0.3s ease; + bottom: calc(100% + 12px); + width: min(420px, calc(100vw - 48px)); + padding: 16px 18px; + border: 1px solid rgba(127, 156, 232, 0.24); + border-radius: 14px; + background: rgba(7, 13, 24, 0.98); + box-shadow: 0 18px 36px rgba(0, 0, 0, 0.36); + opacity: 0; + pointer-events: none; + transform: translateY(6px); + transition: opacity 0.18s ease, transform 0.18s ease; + z-index: 3; +} + +.footer-tooltip-title { + margin: 0 0 8px; + font-size: 0.67rem; + font-weight: 700; + letter-spacing: 0.12em; + text-transform: uppercase; + color: rgba(127, 156, 232, 0.86); +} + +.footer-tooltip-text { + margin: 0; + color: rgba(255, 255, 255, 0.9); + font-size: 0.83rem; + line-height: 1.6; +} + +.footer-funding-tooltip::after { + content: ''; + position: absolute; + left: 24px; + top: 100%; + width: 12px; + height: 12px; + background: rgba(7, 13, 24, 0.98); + border-right: 1px solid rgba(127, 156, 232, 0.24); + border-bottom: 1px solid rgba(127, 156, 232, 0.24); + transform: rotate(45deg) translateY(-6px); +} + +.footer-funding-item:hover .footer-funding-tooltip, +.footer-funding-item:focus-visible .footer-funding-tooltip { + opacity: 1; + transform: translateY(0); } -.hbp-link:hover::after, -.ebrains-link:hover::after { - width: 100%; +.footer-bottom { + max-width: 1200px; + margin: 0 auto; + padding: 20px 28px 28px; + border-top: 1px solid rgba(255, 255, 255, 0.08); + display: flex; + align-items: center; + justify-content: space-between; + gap: 20px; + position: relative; + z-index: 1; } -.footer-legal .legal-link { - color: rgba(255, 255, 255, 0.4); +.footer-copyright { + margin: 0; + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 6px; + font-size: 0.85rem; + color: rgba(255, 255, 255, 0.45); +} + +.copyright-symbol { + font-weight: 600; + color: rgba(255, 255, 255, 0.92); +} + +.footer-meta-link { + color: rgba(255, 255, 255, 0.78); text-decoration: none; - font-size: 11px; transition: color 0.2s ease; - padding: 4px 8px; - border-radius: 4px; } -.footer-legal .legal-link:hover { - color: #ffffff; - background: rgba(255, 255, 255, 0.05); +.footer-meta-link:hover { + color: #fff; +} + +.footer-divider { + color: rgba(255, 255, 255, 0.3); +} + +.footer-copy-end { + color: rgba(255, 255, 255, 0.45); } -/* Versions Section */ .footer-versions { - background: rgba(255, 255, 255, 0.05); - padding: 6px 14px; - border-radius: 20px; - border: 1px solid rgba(255, 255, 255, 0.1); display: flex; + flex-wrap: wrap; gap: 12px; - font-size: 11px; - font-weight: 500; - color: rgba(255, 255, 255, 0.6); - transition: all 0.3s ease; } -.footer-versions:hover { - background: rgba(255, 255, 255, 0.08); - border-color: rgba(255, 255, 255, 0.2); +.version-badge { + display: inline-flex; + align-items: center; + border-radius: 6px; + border: 1px solid rgba(255, 255, 255, 0.12); + background: rgba(0, 0, 0, 0.25); + overflow: hidden; + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); + transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease; + cursor: default; +} + +.version-badge:hover { transform: translateY(-1px); + border-color: rgba(255, 255, 255, 0.25); + box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2); } -.footer-versions span { - display: flex; - align-items: center; +.version-label { + background: rgba(255, 255, 255, 0.08); + padding: 5px 8px; + font-size: 0.65rem; + font-weight: 600; + letter-spacing: 0.06em; + text-transform: uppercase; + color: rgba(255, 255, 255, 0.7); + border-right: 1px solid rgba(255, 255, 255, 0.08); } -.footer-versions span:not(:last-child)::after { - content: '•'; - margin-left: 12px; - opacity: 0.3; +.version-value { + padding: 5px 10px; + font-family: var(--font-mono); + font-size: 0.7rem; + font-weight: 500; + color: #a3b8fa; } -/* Responsive Design */ @media (max-width: 1024px) { - .footer-container { + .footer-shell { + grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); + gap: 32px 0; + } + + .institutional-col { + grid-column: 1 / -1; + padding-left: 0; + padding-top: 28px; + border-left: none; + border-top: 1px solid rgba(255, 255, 255, 0.08); + } + + .footer-bottom { flex-direction: column; - gap: 20px; - padding: 30px 24px; + align-items: flex-start; + } + + .footer-funding-logos { + grid-template-columns: 1fr; } -} \ No newline at end of file +} + +@media (max-width: 768px) { + .footer-shell { + grid-template-columns: 1fr; + padding: 32px 24px; + gap: 0; + } + + .footer-column:not(:first-child) { + padding-left: 0; + padding-top: 24px; + margin-top: 24px; + border-left: none; + border-top: 1px solid rgba(255, 255, 255, 0.08); + } + + .footer-bottom { + padding: 20px 24px 28px; + } + + .footer-funding-visual { + gap: 10px; + } + + .footer-funding-tooltip { + width: min(320px, calc(100vw - 56px)); + } +} diff --git a/src/app/pages/shared/footer/footer.component.html b/src/app/pages/shared/footer/footer.component.html index 4d4bba489..b8ebd9a39 100644 --- a/src/app/pages/shared/footer/footer.component.html +++ b/src/app/pages/shared/footer/footer.component.html @@ -1,40 +1,102 @@ <footer class="main-footer"> - <div class="footer-container"> - <div class="footer-left"> - <div class="footer-logos"> - <a href="https://www.athenarc.gr/" target="_blank" class="logo-link"> - <img src="assets/athenarc.png" alt="Athena" class="footer-logo athena-logo"> + <div class="footer-shell"> + <div class="footer-column brand-col"> + <div class="footer-logo-row"> + <img src="assets/mip-logo-short.png" alt="MIP logo" class="footer-logo" width="480" height="162"> + @if (versionEntries.length) { + <div class="footer-versions"> + @for (version of versionEntries; track version.label) { + <div class="version-badge"> + <span class="version-label">{{ version.label }}</span> + <span class="version-value">{{ version.value }}</span> + </div> + } + </div> + } + </div> + <p class="footer-brand-title">Medical Informatics Platform</p> + <p class="footer-brand-desc"> + A federated privacy-preserving research infrastructure for clinical data analysis across Europe. + </p> + </div> + + <div class="footer-column"> + <p class="footer-col-title">Resources</p> + <ul class="footer-links"> + <li> + <a href="https://github.com/Medical-Informatics-Platform/mip" target="_blank" rel="noopener noreferrer"> + Documentation + </a> + </li> + <li><a routerLink="/terms">Terms of Use</a></li> + <li><a href="mailto:mip@chuv.ch">Support</a></li> + </ul> + </div> + + <div class="footer-column institutional-col"> + <p class="footer-col-title">Powered by</p> + <div class="institutional-logos"> + <a href="https://www.athenarc.gr/" target="_blank" rel="noopener noreferrer" class="partner-link"> + <img src="assets/footer/athenarc.png" alt="Athena Research Center logo" class="partner-logo athena-logo"> </a> - <a href="https://www.ebrains.eu/" target="_blank" class="logo-link"> - <img src="assets/logo_ebrains.png" alt="EBRAINS" class="footer-logo ebrains-logo"> + <a href="https://www.ebrains.eu/" target="_blank" rel="noopener noreferrer" class="partner-link"> + <img src="assets/footer/logo_ebrains.png" alt="EBRAINS logo" class="partner-logo ebrains-logo"> </a> - <a href="https://www.chuv.ch/" target="_blank" class="logo-link"> - <img src="assets/logo_chuv.png" alt="CHUV" class="footer-logo chuv-logo"> + <a href="https://www.chuv.ch/" target="_blank" rel="noopener noreferrer" class="partner-link"> + <img src="assets/footer/CHUV_Logo_Simple_BLANC.png" alt="CHUV logo" class="partner-logo chuv-logo"> </a> </div> - </div> - - <div class="footer-center"> - <div class="footer-copyright-group"> - <p class="copyright-text"> - <span class="copyright-symbol">© 2015-{{ currentYear }}</span> - <a href="https://www.humanbrainproject.eu/" target="_blank" class="hbp-link">Human Brain Project</a>/ - <a href="https://www.ebrains.eu/" target="_blank" class="ebrains-link">EBRAINS</a>. - All right reserved - </p> - <div class="footer-legal"> - <a routerLink="/terms" class="legal-link">Terms of Use</a> + <div class="footer-funding-section" aria-label="Project funding acknowledgements"> + <p class="footer-col-title footer-funding-title">Co-funded by</p> + <div class="footer-funding-logos"> + <div + class="footer-funding-item footer-funding-item-eu" + tabindex="0" + aria-describedby="footer-funding-tooltip" + > + <div class="footer-funding-visual footer-funding-visual-eu"> + <img src="assets/footer/eu-flag.svg" alt="European Union flag" class="footer-funding-flag"> + <span class="footer-funding-box-name">European Union</span> + </div> + <div class="footer-funding-tooltip" id="footer-funding-tooltip" role="tooltip"> + <p class="footer-tooltip-title">Funding acknowledgement</p> + <p class="footer-tooltip-text">{{ fundingAcknowledgement }}</p> + </div> + </div> + <div + class="footer-funding-item footer-funding-item-seri" + tabindex="0" + aria-describedby="footer-funding-tooltip-seri" + > + <div class="footer-funding-visual footer-funding-visual-seri"> + <div class="seri-mark" aria-hidden="true"> + <span class="seri-mark-flag"></span> + </div> + <span class="footer-funding-box-name footer-funding-box-name-seri"> + State Secretariat for Education, Research and Innovation + </span> + </div> + <div class="footer-funding-tooltip" id="footer-funding-tooltip-seri" role="tooltip"> + <p class="footer-tooltip-title">Funding acknowledgement</p> + <p class="footer-tooltip-text">{{ fundingAcknowledgement }}</p> + </div> + </div> </div> </div> </div> + </div> - <div class="footer-right"> - <div class="footer-versions"> - <span>Frontend: {{ versions.frontend }}, </span> - <span>Backend: {{ versions.backend }}, </span> - <span>Exaflow: {{ versions.exaflow }}, </span> - <span>MIP: {{ versions.mip }}</span> - </div> - </div> + <div class="footer-bottom"> + <p class="footer-copyright"> + <span class="copyright-symbol">© 2015-{{ currentYear }}</span> + <a href="https://www.humanbrainproject.eu/" target="_blank" rel="noopener noreferrer" class="footer-meta-link"> + Human Brain Project + </a> + <span class="footer-divider">/</span> + <a href="https://www.ebrains.eu/" target="_blank" rel="noopener noreferrer" class="footer-meta-link"> + EBRAINS + </a> + <span class="footer-copy-end">All rights reserved.</span> + </p> </div> -</footer> \ No newline at end of file +</footer> diff --git a/src/app/pages/shared/footer/footer.component.spec.ts b/src/app/pages/shared/footer/footer.component.spec.ts new file mode 100644 index 000000000..40ccc4bad --- /dev/null +++ b/src/app/pages/shared/footer/footer.component.spec.ts @@ -0,0 +1,91 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideZonelessChangeDetection } from '@angular/core'; +import { provideRouter } from '@angular/router'; + +import { FooterComponent } from './footer.component'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; + +describe('FooterComponent', () => { + let component: FooterComponent; + let fixture: ComponentFixture<FooterComponent>; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [FooterComponent], + providers: [ + provideZonelessChangeDetection(), + provideRouter([]), + { + provide: RuntimeEnvService, + useValue: { + versionEntries: [ + { label: 'MIP', value: 'testing' }, + ], + }, + }, + ], + }).compileComponents(); + + fixture = TestBed.createComponent(FooterComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should render the resources list with support as a mailto link', () => { + const resourceLinks = Array.from( + fixture.nativeElement.querySelectorAll('.footer-links a'), + ) as HTMLAnchorElement[]; + + expect(resourceLinks.length).toBe(3); + + const supportLink = resourceLinks.find((link) => link.textContent?.trim() === 'Support'); + const termsLink = resourceLinks.find((link) => link.textContent?.trim() === 'Terms of Use'); + + expect(supportLink?.getAttribute('href')).toBe('mailto:mip@chuv.ch'); + expect(termsLink).toBeTruthy(); + }); + + it('should render the EU co-funding badge', () => { + const fundingFlag = fixture.nativeElement.querySelector('.footer-funding-item-eu .footer-funding-flag') as HTMLImageElement | null; + const fundingText = fixture.nativeElement.querySelector('.footer-funding-item-eu .footer-funding-box-name') as HTMLElement | null; + const fundingTooltip = fixture.nativeElement.querySelector('.footer-funding-item-eu .footer-funding-tooltip') as HTMLElement | null; + + expect(fundingFlag?.getAttribute('src')).toBe('assets/footer/eu-flag.svg'); + expect(fundingFlag?.getAttribute('alt')).toBe('European Union flag'); + expect(fundingText?.textContent).toContain('European Union'); + expect(fundingTooltip?.textContent).toContain('Human Brain Project'); + expect(fundingTooltip?.textContent).toContain('EBRAINS 2.0'); + expect(fundingTooltip?.textContent).toContain('23.00638'); + }); + + it('should render the SERI co-funding mark with the shared acknowledgement text', () => { + const seriFlag = fixture.nativeElement.querySelector('.footer-funding-item-seri .seri-mark-flag') as HTMLElement | null; + const seriText = fixture.nativeElement.querySelector('.footer-funding-item-seri .footer-funding-box-name-seri') as HTMLElement | null; + const seriTooltip = fixture.nativeElement.querySelector('.footer-funding-item-seri .footer-funding-tooltip') as HTMLElement | null; + + expect(seriFlag).toBeTruthy(); + expect(seriText?.textContent).toContain('State Secretariat for Education, Research and Innovation'); + expect(seriTooltip?.textContent).toContain('SERI contract No. 23.00638'); + }); + + it('should render the selected CHUV blanc logo in the partner section', () => { + const chuvLogo = fixture.nativeElement.querySelector('.chuv-logo') as HTMLImageElement | null; + + expect(chuvLogo?.getAttribute('src')).toBe('assets/footer/CHUV_Logo_Simple_BLANC.png'); + }); + + it('should render only the MIP version in the footer bottom row', () => { + const versions = fixture.nativeElement.querySelector('.footer-versions') as HTMLElement | null; + const versionText = versions?.textContent ?? ''; + + expect(versionText).toContain('MIP'); + expect(versionText).toContain('testing'); + expect(versionText).not.toContain('Frontend:'); + expect(versionText).not.toContain('Backend:'); + expect(versionText).not.toContain('Exaflow:'); + }); +}); diff --git a/src/app/pages/shared/footer/footer.component.ts b/src/app/pages/shared/footer/footer.component.ts index 268d98327..d98ef4c87 100644 --- a/src/app/pages/shared/footer/footer.component.ts +++ b/src/app/pages/shared/footer/footer.component.ts @@ -1,19 +1,20 @@ import { CommonModule } from '@angular/common'; -import { Component } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; import { RouterModule } from '@angular/router'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; @Component({ selector: 'app-footer', imports: [CommonModule, RouterModule], templateUrl: './footer.component.html', - styleUrl: './footer.component.css' + styleUrl: './footer.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class FooterComponent { - currentYear = new Date().getFullYear(); - versions = { - frontend: (window as any).__env?.FRONTEND_VERSION || '10.0.1', - backend: (window as any).__env?.BACKEND_VERSION || '8.2.0', - exaflow: (window as any).__env?.EXAFLOW_VERSION || '0.28.0', - mip: (window as any).__env?.MIP_VERSION || '9.0.0' - }; + private readonly runtimeEnvService = inject(RuntimeEnvService); + + readonly currentYear = new Date().getFullYear(); + readonly versionEntries = this.runtimeEnvService.versionEntries; + readonly fundingAcknowledgement = + 'This work was co-funded by the European Union’s Horizon 2020 Framework Partnership Agreement (No. 650003) for the Human Brain Project, and by the Horizon Europe research and innovation programme through the EBRAINS 2.0 project (grant agreement No. 101147319; SERI contract No. 23.00638).'; } diff --git a/src/app/pages/shared/header/header.component.css b/src/app/pages/shared/header/header.component.css index 1fa86b2e8..403a0dc9c 100644 --- a/src/app/pages/shared/header/header.component.css +++ b/src/app/pages/shared/header/header.component.css @@ -1,41 +1,52 @@ -.header { - position: fixed; - inset: 0 0 auto 0; +/* + * The bar owns its own box. It is a sticky item of the app shell's column, so its + * var(--header-height) comes out of the flow and every route starts below it without + * computing an offset of its own. `position: fixed` here is what made each page root + * invent a top padding — and account/terms got it wrong (content under the bar). + * Sticky must sit on :host: on .header it could not travel, because its containing + * block would be this component's own 64px box. + */ +:host { + position: sticky; + top: 0; z-index: 10000; - display: flex; + display: block; + flex-shrink: 0; +} + +/* + * Top chrome, row 1 — ONE three-slot grid on every route: branding | nav | account. + * A route may replace slot-2 content and may never add a slot, a second bar, its own + * height or its own logo. Height is always var(--header-height); the ≤768px rule in + * styles.css changes the token, this rule never overrides the height. + */ +.header { + display: grid; + grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; - justify-content: space-between; gap: 1.5rem; - padding: 0 32px; + /* One inset with the studio sub-header, so the two rows of the bar start on the same + line. Matches the dashboard gutter, which is where the logo edge meets the grid. */ + padding: 0 var(--chrome-inline-padding); height: var(--header-height); - background: white; - border-bottom: 1px solid var(--border-color); - box-shadow: var(--shadow-sm); + background: var(--header-surface); + border-bottom: 1px solid var(--header-border); + /* Flat at rest — see .header--scrolled. Interpolates from an empty shadow list, so it + fades in rather than snapping in at the first pixel of scroll. */ + box-shadow: none; + transition: box-shadow var(--motion-exit) var(--ease-standard); color: var(--text-main); box-sizing: border-box; - transition: background 0.3s ease, box-shadow 0.3s ease; -} - -/* Transparency support removed for polished branding as per charter */ -.header.header--transparent { - background: white; - border-bottom: 1px solid var(--border-color); - box-shadow: var(--shadow-sm); - pointer-events: auto; } -/* Dark mode header */ -:host-context(body.theme-dark) .header, -:host-context(body.theme-dark) .header.header--transparent { - background: var(--glass-bg, #111827e6); - backdrop-filter: blur(12px); - -webkit-backdrop-filter: blur(12px); - border-bottom-color: var(--border-color); - box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2); +/* The shadow separates the bar from content that scrolled under it. At scrollTop 0 there + is nothing under it, so the hairline is the whole story (same rule Ferryhopper ships). */ +.header--scrolled { + box-shadow: var(--header-shadow); + transition-duration: var(--motion-enter); } -/* BRANDING (LEFT) */ - +/* SLOT 1 — BRANDING (left) */ .branding { display: flex; align-items: center; @@ -46,128 +57,260 @@ cursor: pointer; } +/* 32px on a 480x162 source is 95px wide — above the charter's 50px primary minimum. + The img carries width/height, so the box is known before decode (no layout shift). */ .logo { - height: 40px; - /* Increased slightly for clear identity */ + height: 32px; width: auto; - object-fit: contain; - transition: transform 0.2s ease, filter 0.3s ease; } -:host-context(body.theme-dark) .logo { - filter: brightness(0) invert(1) contrast(0.9); - /* Make logo white in dark mode */ +/* SLOT 2 — the navigation, left-aligned against the hairline so brand and product read + as two groups at any width. min-width:0 so a long label shrinks the track instead of + pushing slot 3 out of the bar. */ +.header-nav-slot { + display: flex; + align-items: center; + justify-content: flex-start; + gap: 1rem; + min-width: 0; } -.branding:hover .logo { - transform: scale(1.02); +/* Brand and navigation read as two groups because a hairline separates them — the same + move Ferryhopper's bar makes. :has() ties the line to real content: a bar with no + navigation draws no rule. If :has() ever goes away, the divider disappears and nothing + else moves. */ +.header-nav-slot:has(> .header-nav)::before { + content: ''; + align-self: center; + height: 22px; + border-inline-start: 1px solid var(--header-border); } -.branding-text { +.header-nav { display: flex; - flex-direction: column; - justify-content: center; + align-items: center; + gap: 1rem; + margin: 0; min-width: 0; } -.branding-text h1 { - margin: 0; - font-size: 0.85rem; - font-weight: 500; - letter-spacing: 0.05em; - text-transform: uppercase; +/* ONE pill recipe for everything the bar offers — the nav items and the Guide. A control + carries no resting chrome: the fill *is* the hover — so the bar reads as labels, not as + a row of boxes. Hover moves the background only; the foreground never changes, so + contrast can only stay the same or improve. + Exit on the short curve, enter on the long one (see --motion-* in styles.css). */ +.header-nav-link, +.header-guide { + display: inline-flex; + align-items: center; + gap: 8px; + box-sizing: border-box; + height: 36px; + padding: 0 16px; + background: transparent; + /* Transparent, not absent: --active and --discover paint this edge, and the box + stays the same size in every state. */ + border: 1px solid transparent; + border-radius: 999px; color: var(--primary-color); - opacity: 0.8; + /* A <button> defaults to the UA face; the Guide is one and must not stand out that way. */ + font-family: inherit; + font-size: 0.85rem; + font-weight: 600; + /* A nav label is one line. Without it the tight mobile track shrinks a pill to its + longest word and "My experiments" stacks into two lines inside a 36px pill. */ white-space: nowrap; + text-decoration: none; + cursor: pointer; + transition: + background-color var(--motion-exit) var(--ease-standard), + border-color var(--motion-exit) var(--ease-standard); } -.branding-text p { - margin: 0.1rem 0 0; - font-size: 0.75rem; - opacity: 0.8; +.header-nav-link:hover, +.header-guide:hover { + background: var(--tint-nav-hover); + transition-duration: var(--motion-enter); } -/* NAVBAR (CENTER) */ -.navbar-wrapper { - flex: 1; +/* First visit only: the border is the hint, the label is the same either way. The one + chrome value outside the named tint ladder — an edge over transparent, not a fill. + There is deliberately no current-page style; the bar marks nothing, see the spec. */ +.header-nav-link--discover { + border-color: color-mix(in srgb, var(--primary-color) 38%, transparent); +} + +.header-nav-link__text { display: flex; - align-items: center; - justify-content: center; - min-width: 0; + flex-direction: column; + align-items: flex-start; + gap: 1px; + line-height: 1.15; } -.navbar-wrapper>app-navbar { - max-width: 100%; +.header-nav-link__title { + font-size: 0.85rem; + font-weight: 600; + color: inherit; } -/* HEADER ACTIONS (right side) */ -.header-actions { - display: flex; - align-items: center; - gap: 12px; +.header-nav-link__icon { + flex-shrink: 0; + display: block; + width: 20px; + height: 20px; } -/* THEME TOGGLE BUTTON */ -.theme-toggle-btn { - background: transparent; - border: 1px solid var(--border-color); - border-radius: 50%; - width: 38px; - height: 38px; +/* SLOT 3 — the route's Guide and the account. The second hairline is what says "this + group is you, not the product". The nav is left-aligned now, so nothing to the left of + this group moves and no width floor is needed to keep it still. + It is a 22px pseudo-element, not a border on the box or on the first control: an `auto` + grid track absorbs leftover width, so a border on the box draws a stray rule mid-bar at + tablet widths, and on the pill a border-inline-start follows the 999px radius and comes + out as a curved sliver instead of a line. */ +.header-actions { display: flex; align-items: center; - justify-content: center; - cursor: pointer; - color: var(--text-muted); - font-size: 16px; - transition: all 0.2s ease; + justify-content: flex-end; + gap: 12px; } -.theme-toggle-btn:hover { - background: var(--primary-color); - color: #fff; - border-color: var(--primary-color); - transform: rotate(15deg); +.header-actions::before { + content: ""; + align-self: center; + height: 22px; + margin-inline-end: 4px; + border-inline-start: 1px solid var(--header-border); } -/* USER AREA */ -.user-icon { - display: flex; - align-items: center; - justify-content: flex-end; - min-width: auto; +.header-guide__icon { + flex-shrink: 0; } .sign-in-btn { background: transparent; border: none; - padding: 0; + padding: 4px 8px 4px 4px; cursor: pointer; border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; - transition: box-shadow 0.18s ease, transform 0.18s ease, - background-color 0.18s ease; + gap: 8px; + transition: box-shadow var(--motion-exit) var(--ease-standard); } +.account-guide-target { + display: inline-flex; + align-items: center; + justify-content: center; +} + +/* The signed-in label is the fullname and it is shown in full: 32ch is wider than any real + Keycloak name, so nothing ordinary is cut. The bound is not cosmetic. The label sits in + slot 3, an `auto` track, and an `auto` track cannot shrink below its own min-content — + under `white-space: nowrap` that minimum is the whole string, so an unbounded name is not + absorbed by slot 2's `minmax(0, 1fr)`, it pushes the account group past the right edge of + the bar. `22vw` keeps the cap under the width the navigation needs at tablet sizes, and the + ellipsis is the overflow of an exceptional name, never the normal state. Below 768px the + label is hidden outright. Check 768 / 1024 / 1440 with a 40-character fullname. */ +.sign-in-btn__label { + font-size: 0.85rem; + font-weight: 600; + color: var(--primary-color); + white-space: nowrap; + max-width: min(32ch, 22vw); + overflow: hidden; + text-overflow: ellipsis; +} + +/* 32px in the bar; 40px is the list/menu size. Ring and placeholder fill are tokens, + so the bar and the account menu cannot drift to two different rings. */ .account-picture, .sign-in-btn img { - width: 40px; - height: 40px; + width: 32px; + height: 32px; border-radius: 999px; object-fit: cover; - border: 2px solid rgba(224, 242, 254, 0.9); - background-color: rgba(15, 23, 42, 0.3); + border: 2px solid var(--ring-avatar); + background-color: var(--avatar-bg); + box-sizing: border-box; } -/* hover / focus state */ -.sign-in-btn:hover, -.sign-in-btn:focus-visible, -.user-icon a:hover { - transform: translateY(-1px); - box-shadow: 0 0 0 2px rgba(125, 211, 252, 0.6); - outline: none; +.sign-in-btn:hover { + box-shadow: 0 0 0 3px var(--ring-avatar); + transition-duration: var(--motion-enter); +} + +/* Focus is its own ring, never a share of the hover rule. The global ring is the same + token; it is restated here so the hover shadow can never be the only cue. */ +.header-nav-link:focus-visible, +.header-guide:focus-visible, +.sign-in-btn:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + +/* The menu hangs inside the bar's own box instead of a CDK overlay: the header is a + stacking context at z 10000 and the overlay container sits at 1000, so an overlay + would open *under* the bar it belongs to. CdkMenu still owns roles, the roving + tabindex and arrow/Home/End/typeahead — it does not need to be positioned to do that. */ +.account-control { + position: relative; + display: inline-flex; +} + +.account-menu { + position: absolute; + top: calc(100% + 8px); + /* Right-aligned: near the bar's right edge the menu can never hang off-screen. */ + right: 0; + z-index: 1; + display: flex; + flex-direction: column; + min-width: 240px; + padding: 8px 0; + background: var(--card-bg); + border: 1px solid var(--header-border); + border-radius: var(--radius-md); + box-shadow: var(--shadow-lg); + color: var(--text-main); +} + +.account-menu__item { + display: flex; + align-items: center; + gap: 10px; + padding: 12px 16px; + background: transparent; + border: none; + font: inherit; + font-size: 0.85rem; + font-weight: 600; + color: inherit; + text-align: start; + text-decoration: none; + cursor: pointer; + transition: background-color var(--motion-exit) var(--ease-standard); +} + +.account-menu__item:hover { + background: var(--tint-nav-hover); + transition-duration: var(--motion-enter); +} + +/* Ring inside the box so the panel's radius can never clip it. */ +.account-menu__item:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: -2px; +} + +/* Fixed measure so the two rows' labels start on one line. */ +.account-menu__icon { + width: 16px; + flex-shrink: 0; + text-align: center; + color: var(--primary-color); } /* RESPONSIVE */ @@ -175,59 +318,54 @@ /* Tablets */ @media (max-width: 1024px) { .header { - padding: 0 1.25rem; + padding: 0 1rem; gap: 1rem; } - - .logo { - width: 46px; - height: 46px; - } - - .branding-text h1 { - font-size: 1rem; - } - - .branding-text p { - font-size: 0.7rem; - } - - .account-picture, - .sign-in-btn img { - width: 34px; - height: 34px; - } } -/* Mobiles */ +/* Mobiles — height stays on the token (56px via :root). The bar is the same three slots; + what gives way is width: the logo, the Guide's word and the name go first, because the + navigation is the part a person still needs. */ @media (max-width: 768px) { .header { - flex-direction: row; - align-items: center; - justify-content: center; - height: 56px; padding: 0 12px; - gap: 0; + gap: 8px; + /* The logo is hidden here, and a display:none grid item is not a grid item — the two + survivors auto-place one column left, which parks the account in the 1fr track and + squeezes the avatar to 0px. Two columns, two items. */ + grid-template-columns: minmax(0, 1fr) auto; + } + + .branding { + display: none; } - .branding-text { - display: none !important; + /* Nothing on the brand side to divide from, so the hairline goes with the logo. */ + .header-nav-slot::before { + display: none; } - .branding { - display: flex !important; - gap: 0; + /* Three nav items + Guide + avatar need ~470px; a 390px bar does not have them. The nav + swipes, the bar and the account stay put. */ + .header-nav-slot { + gap: 8px; + overflow-x: auto; + overflow-y: hidden; + scrollbar-width: none; } - .user-icon { - display: flex !important; + .header-nav-link, + .header-guide { + padding: 0 10px; + gap: 6px; } - .navbar-wrapper { - flex: 1; - width: 100%; - justify-content: stretch; - margin-top: 0; - order: initial; + .header-nav-link__title { + font-size: 0.78rem; } -} \ No newline at end of file + + .header-guide__label, + .sign-in-btn__label { + display: none; + } +} diff --git a/src/app/pages/shared/header/header.component.html b/src/app/pages/shared/header/header.component.html index 970078518..f01cc58d7 100644 --- a/src/app/pages/shared/header/header.component.html +++ b/src/app/pages/shared/header/header.component.html @@ -1,25 +1,114 @@ -<header class="header header--transparent"> - <!-- Logo --> - <a class="branding" routerLink="/experiments-dashboard" (click)="goHome($event)" aria-label="Go to home"> - <img src="assets/mip-logo-short.png" alt="MIP Logo" class="logo" /> +<header class="header" [class.header--scrolled]="isScrolled()"> + <!-- SLOT 1 — branding. One asset, one size, on every route. --> + <a class="branding" href="/experiments-dashboard" (click)="goHome($event)" aria-label="Go to home"> + <img src="assets/mip-logo-short.png" alt="MIP logo" class="logo" width="480" height="162" /> </a> - <!-- Navbar (Removed to maximize space) --> - <!-- Theme Toggle + User Icon --> + <!-- SLOT 2 — the product's navigation: the SAME items on every route, left-aligned + against the hairline that ends the brand. The bar does not draw which page you are + on — aria-current carries that for assistive tech and nothing more — and items + never appear or disappear per route. --> + <div class="header-nav-slot"> + <nav class="header-nav" aria-label="Primary"> + <a + href="/experiments-dashboard" + [attr.aria-current]="isDashboardRoute() ? 'page' : null" + class="header-nav-link header-nav-link--dashboard" + data-guide="dashboard-nav" + (click)="goHome($event)" + > + <span class="header-nav-link__title">My experiments</span> + </a> + <a + routerLink="/experiment-studio" + [attr.aria-current]="isStudioRoute() ? 'page' : null" + class="header-nav-link header-nav-link--studio" + > + <span class="header-nav-link__title">Studio</span> + </a> + @if (runtimeEnv.notebookEnabled && isLoggedIn()) { + <a + routerLink="/notebook" + [attr.aria-current]="notebookRouteActive() ? 'page' : null" + class="header-nav-link header-nav-link--notebook" + [class.header-nav-link--discover]="!notebookNav.hasVisited()" + aria-label="Jupyter Notebook" + > + <span class="header-nav-link__text"> + <span class="header-nav-link__title">Jupyter Notebook</span> + </span> + <img src="assets/notebook-ai-icon.svg" alt="" class="header-nav-link__icon" width="20" height="20" /> + </a> + } + </nav> + </div> + + <!-- SLOT 3 — the route's Guide (only pages that have a tour register one) and the + account. Left of this group sits the bar's second hairline, so the group reads as + "you", not as part of the product navigation. --> <div class="header-actions"> + @if (guideLauncher(); as guide) { + <button + type="button" + class="header-guide" + data-guide="launcher" + (click)="guide.start()" + [attr.aria-label]="guide.label" + [attr.title]="guide.label" + > + <i class="fas fa-compass header-guide__icon" aria-hidden="true"></i> + <span class="header-guide__label">{{ guide.label }}</span> + </button> + } - <div class="user-icon"> - @if (isLoggedIn()) { - <!-- LOGGED IN: go to account --> - <a class="sign-in-btn" routerLink="/account"> - <img src="assets/user-icon.png" alt="User Picture" class="account-picture" /> - </a> - } @else { - <!-- LOGGED OUT: trigger login --> - <button class="sign-in-btn" (click)="login()"> - <img src="assets/grey-avatar.png" alt="Sign In" class="account-picture" /> + @if (isLoggedIn()) { + <!-- The avatar used to be a link to /account, whose real job was to hold a "Sign Out" + button. It is a trigger now: one click to the menu, one to the action. --> + <div class="account-control" #accountControl> + <button + type="button" + class="sign-in-btn" + #accountToggle + data-guide="header-account" + aria-haspopup="menu" + [attr.aria-expanded]="accountMenuOpen()" + [attr.aria-controls]="accountMenuId" + [attr.aria-label]="accountMenuOpen() ? 'Close account menu' : 'Account menu'" + (click)="toggleAccountMenu()" + (keydown.escape)="closeAccountMenu()" + > + <span class="account-guide-target"> + <img src="assets/user-icon.png" alt="" class="account-picture" width="32" height="32" /> + </span> + <span class="sign-in-btn__label">{{ displayName() }}</span> </button> + + @if (accountMenuOpen()) { + <!-- Menu geometry is the house surface (card bg + chrome border + shadow), its + shape is Ferryhopper's: right-aligned so it can never hang off the bar's right + edge, min-width so a short label still reads as a menu. --> + <div class="account-menu" [id]="accountMenuId" cdkMenu (keydown.escape)="closeAccountMenu()"> + <a class="account-menu__item" cdkMenuItem routerLink="/account"> + <i class="fas fa-user-gear account-menu__icon" aria-hidden="true"></i> + <span>Account settings</span> + </a> + <!-- cdkMenuItemTriggered, not (triggered): the output is aliased, and an + unrecognised name binds as a plain DOM event that never fires — silently, + with no template error and no failing build. --> + <button type="button" class="account-menu__item" cdkMenuItem (cdkMenuItemTriggered)="signOut()"> + <i class="fas fa-sign-out-alt account-menu__icon" aria-hidden="true"></i> + <span>Sign Out</span> + </button> + </div> } </div> + } @else { + <button class="sign-in-btn" data-guide="header-account" (click)="login()" aria-label="Sign in"> + <span class="account-guide-target"> + <img src="assets/grey-avatar.png" alt="" class="account-picture" width="32" height="32" /> + </span> + <span class="sign-in-btn__label">Sign in</span> + </button> + } </div> -</header> \ No newline at end of file +</header> diff --git a/src/app/pages/shared/header/header.component.spec.ts b/src/app/pages/shared/header/header.component.spec.ts new file mode 100644 index 000000000..5cd70ac96 --- /dev/null +++ b/src/app/pages/shared/header/header.component.spec.ts @@ -0,0 +1,370 @@ +import type { MockedObject } from 'vitest'; +import { Component } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { provideRouter, Router } from '@angular/router'; +import { HeaderComponent } from './header.component'; +import { AuthService } from '../../../services/auth.service'; +import { GuideLauncherService } from '../../../services/guide-launcher.service'; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { NotebookNavService } from '../../../services/notebook-nav.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; +import { User } from '../../../models/user.interface'; +import { createSpyObj } from '../../../testing/spy-obj.testing'; + +@Component({ template: '' }) +class StubStudioComponent {} + +describe('HeaderComponent', () => { + let fixture: ComponentFixture<HeaderComponent>; + let component: HeaderComponent; + let authService: MockedObject<AuthService>; + let runtimeEnv: { notebookEnabled: boolean }; + let notebookNav: MockedObject<NotebookNavService>; + let router: Router; + + beforeEach(async () => { + authService = createSpyObj<AuthService>('AuthService', ['isLoggedIn', 'login', 'logout']); + authService.isLoggedIn.mockReturnValue(true); + // currentUser is a getter on the real service; the spy needs the property itself. + Object.defineProperty(authService, 'currentUser', { + value: { + username: 'mcurie', + fullname: 'Marie Curie', + email: 'marie.curie@ebi.ac.uk', + subjectId: 'orcid-0000-0002', + } satisfies User, + }); + + notebookNav = createSpyObj<NotebookNavService>('NotebookNavService', ['hasVisited']); + notebookNav.hasVisited.mockReturnValue(true); + + runtimeEnv = { notebookEnabled: true }; + + await TestBed.configureTestingModule({ + imports: [HeaderComponent], + providers: [ + provideRouter([ + { path: 'experiments-dashboard', component: StubStudioComponent }, + { path: 'terms', component: StubStudioComponent }, + { path: 'account', component: StubStudioComponent }, + { path: 'experiment-studio', component: StubStudioComponent }, + { path: 'notebook', component: StubStudioComponent }, + ]), + { provide: AuthService, useValue: authService }, + { provide: NotebookNavService, useValue: notebookNav }, + { provide: RuntimeEnvService, useValue: runtimeEnv }, + ExperimentStudioNavigationService, + ], + }) + .compileComponents(); + + fixture = TestBed.createComponent(HeaderComponent); + component = fixture.componentInstance; + router = TestBed.inject(Router); + fixture.detectChanges(); + }); + + it('creates header component', () => { + expect(component).toBeTruthy(); + }); + + it('renders branding and user actions in standard non-studio mode', () => { + const compiled = fixture.nativeElement as HTMLElement; + expect(compiled.querySelector('.branding')).toBeTruthy(); + expect(compiled.querySelector('.header-actions')).toBeTruthy(); + expect(compiled.querySelector('app-studio-stepper')).toBeNull(); + }); + + it('keeps one three-slot bar on every route: branding, nav, account', async () => { + for (const path of ['/notebook', '/experiment-studio']) { + await router.navigateByUrl(path); + fixture.detectChanges(); + + const bar = (fixture.nativeElement as HTMLElement).querySelector<HTMLElement>('.header'); + const kids = Array.from(bar?.children ?? []).map((el) => el.className); + // The nav belongs to slot 2. Nesting it back into the account slot is what made + // the pill right-pinned, so it moved when the "Sign in" label appeared. + expect(kids).toEqual(['branding', 'header-nav-slot', 'header-actions']); + expect(getComputedStyle(bar as HTMLElement).display).toBe('grid'); + } + }); + + /** + * The header charter, as one assertion: one component, one box, one surface, one logo + * AND one set of navigation items on every route. Height, indent, surface and the nav + * labels all ride in the signature, so a bar that re-indents or swaps its items for a + * route — the old studio behaviour — fails here instead of in review comments. + */ + it('keeps one box, one surface, one logo and one nav across every route', async () => { + const signatureOf = () => { + const compiled = fixture.nativeElement as HTMLElement; + const bar = compiled.querySelector<HTMLElement>('.header'); + const s = bar ? getComputedStyle(bar) : null; + const logo = compiled.querySelector<HTMLImageElement>('.branding .logo'); + return [ + s?.height, + s?.paddingLeft, + s?.backgroundColor, + s?.borderBottomWidth, + s?.boxShadow, + logo?.getAttribute('src'), + logo ? getComputedStyle(logo).height : null, + Array.from(compiled.querySelectorAll('.header-nav-slot .header-nav-link__title')) + .map((el) => el.textContent?.trim()) + .join(','), + ].join('|'); + }; + + let baseline: string | undefined; + for (const path of ['/experiments-dashboard', '/terms', '/account', '/notebook', '/experiment-studio']) { + await router.navigateByUrl(path); + fixture.detectChanges(); + expect((fixture.nativeElement as HTMLElement).querySelector('.header')).toBeTruthy(); + + const signature = signatureOf(); + // A bar that fell back to `auto` would match itself everywhere and prove nothing. + expect(signature.split('|')[0]).not.toBe('auto'); + baseline ??= signature; + expect({ path, signature }).toEqual({ path, signature: baseline }); + } + + // Same items is not the same as same page: exactly one of them is current. + await router.navigateByUrl('/experiment-studio'); + fixture.detectChanges(); + const current = (fixture.nativeElement as HTMLElement).querySelectorAll('[aria-current="page"]'); + expect(current.length).toBe(1); + expect(current[0].textContent).toContain('Studio'); + }); + + it('keeps the account slot one control wide in both auth states', () => { + const slotOf = (el: HTMLElement) => el.querySelector('.header-actions'); + expect(slotOf(fixture.nativeElement)?.querySelector('.sign-in-btn')).toBeTruthy(); + + // A fresh fixture, not a re-render: the slot is OnPush behind a plain method call, + // so flipping the spy after the first check is not something the app can do either. + authService.isLoggedIn.mockReturnValue(false); + const signedOut = TestBed.createComponent(HeaderComponent); + signedOut.detectChanges(); + + const actions = slotOf(signedOut.nativeElement as HTMLElement); + expect(actions?.querySelector('.sign-in-btn__label')?.textContent).toContain('Sign in'); + expect(actions?.querySelector('.account-picture')).toBeTruthy(); + expect(actions?.querySelector('[data-guide="header-account"]')).toBeTruthy(); + expect(actions?.querySelector('.sign-in-btn')?.getAttribute('data-guide')).toBe('header-account'); + }); + + /** + * The avatar used to be a link whose destination held the only Sign Out button in the + * product. As a trigger it must stay a real menu: aria state on the toggle, CdkMenu's + * roles, and focus that comes back when the menu goes away. + */ + it('opens the account menu and hands focus back on Escape', async () => { + await router.navigateByUrl('/account'); + fixture.detectChanges(); + + const compiled = fixture.nativeElement as HTMLElement; + const toggle = compiled.querySelector<HTMLButtonElement>('button.sign-in-btn'); + expect(toggle).toBeTruthy(); + expect(toggle?.getAttribute('aria-haspopup')).toBe('menu'); + expect(toggle?.getAttribute('aria-expanded')).toBe('false'); + + toggle?.click(); + fixture.detectChanges(); + + const menu = compiled.querySelector<HTMLElement>('.account-menu'); + expect(menu).toBeTruthy(); + expect(menu?.getAttribute('role')).toBe('menu'); + expect(compiled.querySelectorAll('[role="menuitem"]').length).toBe(2); + expect(toggle?.getAttribute('aria-expanded')).toBe('true'); + expect(toggle?.getAttribute('aria-controls')).toBe(menu?.id); + + menu?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); + fixture.detectChanges(); + expect(compiled.querySelector('.account-menu')).toBeNull(); + expect(toggle?.getAttribute('aria-expanded')).toBe('false'); + expect(document.activeElement).toBe(toggle); + }); + + it('closes on an outside pointer and on navigation, and signs out from the menu', async () => { + await router.navigateByUrl('/account'); + fixture.detectChanges(); + + const compiled = fixture.nativeElement as HTMLElement; + const toggle = compiled.querySelector<HTMLButtonElement>('button.sign-in-btn'); + expect(toggle).toBeTruthy(); + + toggle?.click(); + fixture.detectChanges(); + expect(compiled.querySelector('.account-menu')).toBeTruthy(); + + // A click elsewhere is the user choosing something else: close, do not steal focus. + document.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })); + fixture.detectChanges(); + expect(compiled.querySelector('.account-menu')).toBeNull(); + + toggle?.click(); + fixture.detectChanges(); + await router.navigateByUrl('/notebook'); + fixture.detectChanges(); + expect(compiled.querySelector('.account-menu')).toBeNull(); + + toggle?.click(); + fixture.detectChanges(); + compiled.querySelectorAll<HTMLElement>('[role="menuitem"]')[1].click(); + expect(authService.logout).toHaveBeenCalledTimes(1); + }); + + it('wears the shadow only once the page has moved under the bar', () => { + const compiled = fixture.nativeElement as HTMLElement; + const bar = compiled.querySelector<HTMLElement>('.header'); + expect(bar).toBeTruthy(); + const original = Object.getOwnPropertyDescriptor(window, 'scrollY'); + + try { + Object.defineProperty(window, 'scrollY', { configurable: true, get: () => 420 }); + window.dispatchEvent(new Event('scroll')); + fixture.detectChanges(); + expect(bar?.classList.contains('header--scrolled')).toBe(true); + + Object.defineProperty(window, 'scrollY', { configurable: true, get: () => 0 }); + window.dispatchEvent(new Event('scroll')); + fixture.detectChanges(); + expect(bar?.classList.contains('header--scrolled')).toBe(false); + } finally { + // The last definition is 0, which is what the runner's window actually reports. + if (original) { + Object.defineProperty(window, 'scrollY', original); + } + } + }); + + it('marks Studio as current and keeps My experiments on the studio route', async () => { + await router.navigateByUrl('/experiment-studio'); + fixture.detectChanges(); + + const compiled = fixture.nativeElement as HTMLElement; + expect(component.isStudioRoute()).toBe(true); + expect(compiled.querySelector('app-studio-stepper')).toBeNull(); + const myExpBtn = compiled.querySelector<HTMLAnchorElement>('.header-nav-link--dashboard'); + expect(myExpBtn?.textContent).toContain('My experiments'); + expect(myExpBtn?.getAttribute('data-guide')).toBe('dashboard-nav'); + expect(compiled.querySelector('.header-nav-link--studio')?.getAttribute('aria-current')).toBe('page'); + expect(compiled.querySelector('.sign-in-btn__label')?.textContent?.trim()).toBe('Marie Curie'); + }); + + /** + * The nav draws two things and no third: hover, and the notebook's first-visit edge. + * The current page used to get a tinted, bordered pill — one nav item wearing three + * layers of chrome — and is now marked only by aria-current, which nothing here styles. + */ + it('draws nav chrome on hover and first visit, never on the current page', async () => { + await router.navigateByUrl('/experiment-studio'); + fixture.detectChanges(); + + const compiled = fixture.nativeElement as HTMLElement; + const flatOf = (sel: string) => { + const s = getComputedStyle(compiled.querySelector<HTMLElement>(sel) as HTMLElement); + return [s.backgroundColor, s.borderTopColor]; + }; + expect(flatOf('.header-nav-link')).toEqual(['rgba(0, 0, 0, 0)', 'rgba(0, 0, 0, 0)']); + expect(getComputedStyle(compiled.querySelector('.header-nav-link') as HTMLElement).borderRadius).toBe('999px'); + + // The route's own item is the current one and still draws nothing. + const current = compiled.querySelector<HTMLElement>('.header-nav-link--studio'); + expect(current?.getAttribute('aria-current')).toBe('page'); + expect(flatOf('.header-nav-link--studio')).toEqual(['rgba(0, 0, 0, 0)', 'rgba(0, 0, 0, 0)']); + + // The hairline that ends the brand is drawn because the nav is there — and the nav + // is there on every route now, so the bar never shows an un-divided middle. + const slot = compiled.querySelector<HTMLElement>('.header-nav-slot'); + expect(getComputedStyle(slot as HTMLElement, '::before').content).not.toBe('none'); + }); + + /** + * The Guide control moved out of the pages that owned it: it used to float over the + * bar at z 10001 with a hardcoded right offset, which the wider account slot would + * have landed on. Pages hand the bar a label and a start function instead. + */ + it('shows the Guide control only for a page that registered one', async () => { + const compiled = fixture.nativeElement as HTMLElement; + expect(compiled.querySelector('.header-guide')).toBeNull(); + + const start = vi.fn(); + const launchers = TestBed.inject(GuideLauncherService); + const handle = { label: 'Guide', start }; + launchers.register(handle); + fixture.detectChanges(); + + const guide = compiled.querySelector<HTMLButtonElement>('.header-guide'); + expect(guide?.textContent).toContain('Guide'); + // The studio tour spotlights this selector; moving the control kept the hook. + expect(guide?.getAttribute('data-guide')).toBe('launcher'); + guide?.click(); + expect(start).toHaveBeenCalled(); + + launchers.unregister(handle); + fixture.detectChanges(); + expect(compiled.querySelector('.header-guide')).toBeNull(); + }); + + it('delegates to studio navigation when clicking My experiments', async () => { + const studioNav = TestBed.inject(ExperimentStudioNavigationService); + vi.spyOn(studioNav, 'backToDashboard').mockReturnValue(undefined); + + await router.navigateByUrl('/experiment-studio'); + fixture.detectChanges(); + + const myExpBtn = (fixture.nativeElement as HTMLElement).querySelector<HTMLAnchorElement>('.header-nav-link--dashboard'); + const click = new MouseEvent('click', { bubbles: true, cancelable: true, button: 0 }); + myExpBtn?.dispatchEvent(click); + + expect(studioNav.backToDashboard).toHaveBeenCalled(); + expect(click.defaultPrevented).toBe(true); + await fixture.whenStable(); + // The host is the one that owns the exit path; the header must not race it with a + // second navigation or bypass the studio's running guard. + expect(router.url).toBe('/experiment-studio'); + }); + + it('leaves modifier and middle clicks to the browser', async () => { + const studioNav = TestBed.inject(ExperimentStudioNavigationService); + const back = vi.spyOn(studioNav, 'backToDashboard').mockReturnValue(undefined); + + await router.navigateByUrl('/experiment-studio'); + fixture.detectChanges(); + + const pill = (fixture.nativeElement as HTMLElement).querySelector<HTMLAnchorElement>('.header-nav-link--dashboard'); + if (!pill) throw new Error('studio pill missing'); + + // Registered after Angular's (click) listener, so it observes what the component + // decided and then cancels the navigation the browser would otherwise perform. + let preventedByComponent = true; + pill.addEventListener('click', (event) => { + preventedByComponent = event.defaultPrevented; + event.preventDefault(); + }); + + for (const init of [ + { button: 1 }, + { button: 0, metaKey: true }, + { button: 0, ctrlKey: true }, + { button: 0, shiftKey: true }, + ]) { + pill.dispatchEvent(new MouseEvent('click', { ...init, bubbles: true, cancelable: true })); + expect(preventedByComponent).toBe(false); + } + expect(back).not.toHaveBeenCalled(); + + // A plain left click is the component's to handle. + pill.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, button: 0 })); + expect(preventedByComponent).toBe(true); + }); + + it('marks the notebook link as the current page', async () => { + await router.navigate(['/notebook']); + fixture.detectChanges(); + + const link = (fixture.nativeElement as HTMLElement).querySelector<HTMLAnchorElement>('.header-nav-link--notebook'); + expect(link?.getAttribute('aria-current')).toBe('page'); + }); +}); diff --git a/src/app/pages/shared/header/header.component.ts b/src/app/pages/shared/header/header.component.ts index 007f1c98c..ab049190a 100644 --- a/src/app/pages/shared/header/header.component.ts +++ b/src/app/pages/shared/header/header.component.ts @@ -1,31 +1,108 @@ -import { CommonModule } from '@angular/common'; import { NavigationEnd, Router, RouterModule } from '@angular/router'; +import { CdkMenu, CdkMenuItem } from '@angular/cdk/menu'; import { AuthService } from '../../../services/auth.service'; -import { ThemeService } from '../../../services/theme.service'; -import { Component, OnInit } from "@angular/core"; -import { filter } from "rxjs"; +import { RuntimeEnvService } from '../../../services/runtime-env.service'; +import { NotebookNavService } from '../../../services/notebook-nav.service'; +import { ExperimentStudioNavigationService } from '../../../services/experiment-studio-navigation.service'; +import { GuideLauncherService } from '../../../services/guide-launcher.service'; +import { isRoutePath } from '../../../core/route-path.utils'; +import { + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + computed, + DestroyRef, + ElementRef, + inject, + Renderer2, + signal, + viewChild, +} from '@angular/core'; +import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop'; +import { distinctUntilChanged, filter, fromEvent, map, startWith } from 'rxjs'; + +/** + * Modifier and non-primary clicks belong to the browser: ⌘/Ctrl/middle-click on the logo + * or the studio pill must still be able to open a tab. Swallowing every click made the + * advertised routerLink/href a lie. + */ +function isPlainLeftClick(event?: Event): boolean { + if (!event) return true; + const { button, metaKey, ctrlKey, shiftKey, altKey } = event as MouseEvent; + return button === 0 && !metaKey && !ctrlKey && !shiftKey && !altKey; +} @Component({ selector: 'app-header', - imports: [CommonModule, RouterModule], + imports: [RouterModule, CdkMenu, CdkMenuItem], templateUrl: './header.component.html', - styleUrls: ['./header.component.css'] + styleUrl: './header.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, }) -export class HeaderComponent implements OnInit { - currentRoute: string | undefined; - constructor( - private router: Router, - public authService: AuthService, - public themeService: ThemeService - ) { } - - ngOnInit(): void { - // Listen to changes in the route +export class HeaderComponent { + private router = inject(Router); + authService = inject(AuthService); + readonly runtimeEnv = inject(RuntimeEnvService); + readonly notebookNav = inject(NotebookNavService); + readonly studioNav = inject(ExperimentStudioNavigationService); + + readonly notebookRouteActive = this.trackRouteActive(() => this.isNotebookRoute()); + readonly isStudioRoute = this.trackRouteActive(() => this.isExperimentStudioRoute()); + readonly isDashboardRoute = this.trackRouteActive(() => this.isDashboardPath()); + + /** Which guide (if any) owns the current page. The guide registers itself and the bar + * only draws what it is given, so the slot's shape never changes between routes. */ + private readonly guideLaunchers = inject(GuideLauncherService); + readonly guideLauncher = computed(() => this.guideLaunchers.launcher()); + + /** The signed-in label. `fullname` is what the account page shows, so the bar cannot + * disagree with it; username is the fallback, never the raw email. */ + readonly displayName = computed(() => { + const user = this.authService.currentUser; + return user?.fullname?.trim() || user?.username?.trim() || 'Account'; + }); + + /** + * Flat hairline at rest, elevation only once content has moved under the bar. The + * shadow exists to separate the chrome from what scrolled beneath it; at scrollTop 0 + * there is nothing there, so the bar was wearing depth it had no use for. One boolean + * off a passive listener — nothing reads scroll position per frame. + */ + readonly isScrolled = toSignal( + fromEvent(window, 'scroll', { passive: true }).pipe( + map(() => window.scrollY > 0), + startWith(window.scrollY > 0), + distinctUntilChanged(), + ), + { initialValue: window.scrollY > 0 }, + ); + + /** The account control is a menu trigger, not a link to a page whose real job was to + * hold a "Sign Out" button. One instance, so one static id wires aria-controls. */ + readonly accountMenuId = 'mip-account-menu'; + readonly accountMenuOpen = signal(false); + + private readonly renderer = inject(Renderer2); + private readonly cdr = inject(ChangeDetectorRef); + private readonly destroyRef = inject(DestroyRef); + private readonly accountMenu = viewChild(CdkMenu); + /* ElementRef is the read type for a string locator: typing these as the node itself + compiles and then hands back an ElementRef, so `.focus()`/`.contains()` blow up at + runtime with no type error anywhere to predict it. */ + private readonly accountToggle = viewChild<ElementRef<HTMLButtonElement>>('accountToggle'); + private readonly accountControl = viewChild<ElementRef<HTMLElement>>('accountControl'); + private unlistenOutsideClick: (() => void) | null = null; + + constructor() { + // Back/forward and any other route change close the menu too, so the outside-pointer + // listener never outlives the page the menu was opened on. this.router.events - .pipe(filter(event => event instanceof NavigationEnd)) - .subscribe((event: NavigationEnd) => { - this.currentRoute = event.urlAfterRedirects; // Update to the latest route - }); + .pipe( + filter((event): event is NavigationEnd => event instanceof NavigationEnd), + takeUntilDestroyed(), + ) + .subscribe(() => this.closeAccountMenu({ restoreFocus: false })); + this.destroyRef.onDestroy(() => this.unlistenOutsideClick?.()); } isLoggedIn(): boolean { @@ -37,12 +114,90 @@ export class HeaderComponent implements OnInit { this.authService.login(); } + signOut(): void { + // The menu goes first: logout is a hard redirect, so the surface should not sit open + // through the unload — and it stays closed if logout ever stops being one. + this.closeAccountMenu({ restoreFocus: false }); + this.authService.logout(); + } + + toggleAccountMenu(): void { + if (this.accountMenuOpen()) { + this.closeAccountMenu(); + } else { + this.openAccountMenu(); + } + } + + private openAccountMenu(): void { + this.accountMenuOpen.set(true); + // The menu is an @if, so its node — and CdkMenu's roving tabindex — exists only after + // this render. CdkMenu owns arrows/Home/End/typeahead; focus still has to arrive, or + // a keyboard user opens a menu made of tabindex="-1" rows they cannot reach. + this.cdr.detectChanges(); + this.accountMenu()?.focusFirstItem(); + + // The opening click is already past pointerdown, so the next one is either outside + // (close, and leave focus where the user put it) or the toggle itself, whose click + // handler closes. Registered imperatively so it exists exactly while the menu does. + this.unlistenOutsideClick = this.renderer.listen(document, 'pointerdown', (event: PointerEvent) => { + const control = this.accountControl()?.nativeElement; + if (control && !control.contains(event.target as Node)) { + this.closeAccountMenu({ restoreFocus: false }); + } + }); + } + + /** Escape returns focus to the trigger; a click elsewhere must not steal it. */ + closeAccountMenu(options: { restoreFocus?: boolean } = {}): void { + if (!this.accountMenuOpen()) return; + this.accountMenuOpen.set(false); + this.unlistenOutsideClick?.(); + this.unlistenOutsideClick = null; + if (options.restoreFocus !== false) { + this.accountToggle()?.nativeElement.focus(); + } + } + + /** One NavigationEnd subscription for each route flag the bar toggles. */ + private trackRouteActive(isActive: () => boolean) { + return toSignal( + this.router.events.pipe( + filter((event): event is NavigationEnd => event instanceof NavigationEnd), + map(() => isActive()), + startWith(isActive()), + ), + { initialValue: isActive() }, + ); + } + goHome(event?: Event): void { + if (!isPlainLeftClick(event)) return; event?.preventDefault(); - this.router.navigate(['/experiments-dashboard']); + if (this.isStudioRoute()) { + // The studio host owns its own exit path: it may need to block navigation while an + // experiment is running and it already resets studio state before routing away. + this.studioNav.backToDashboard(); + return; + } + void this.router.navigate(['/experiments-dashboard']); } - logout(): void { - this.authService.logout(); + private isNotebookRoute(): boolean { + return this.onPath('/notebook'); + } + + private isExperimentStudioRoute(): boolean { + // Same shape as the notebook check: a nested studio path must keep the nav's current + // state and the state-preserving back action, not silently drop out of them. + return this.onPath('/experiment-studio'); + } + + private isDashboardPath(): boolean { + return this.onPath('/experiments-dashboard'); + } + + private onPath(prefix: string): boolean { + return isRoutePath(this.router.url, prefix); } } diff --git a/src/app/pages/shared/navbar/navbar.component.css b/src/app/pages/shared/navbar/navbar.component.css deleted file mode 100644 index c729d3ef1..000000000 --- a/src/app/pages/shared/navbar/navbar.component.css +++ /dev/null @@ -1,126 +0,0 @@ -/* BASE */ -.navbar { - width: 100%; - display: flex; - align-items: center; - position: relative; - min-width: 0; - background: transparent; -} - -/* Desktop menu only */ -.nav-burger, -.nav-logo, -.nav-account { - display: none; -} - -.nav-list { - display: flex; - justify-content: flex-end; - align-items: flex-end; - list-style: none; - margin: 25px; - padding: 0; - gap: 14px; -} - -.nav-list li a { - color: #ffffff; - text-decoration: none; - font-size: 0.85em; - display: flex; - flex-direction: column; - align-items: center; - font-family: "Roboto", sans-serif; - font-weight: 300; - opacity: 0.75; - transition: opacity 0.25s ease, transform 0.2s ease; -} - -.nav-list li a i { - font-size: 1.6em; - margin-bottom: 2px; - opacity: 0.85; -} - -/* MOBILE */ -@media (max-width: 768px) { - .navbar { - height: 52px; - padding: 0 12px; - background: transparent; - display: grid; - grid-template-columns: 44px 1fr minmax(44px, 56px); - align-items: center; - grid-auto-rows: min-content; - box-shadow: 0 6px 14px rgba(0, 0, 0, 0.25); - } - - .nav-burger, - .nav-logo, - .nav-account { - display: inline-flex; - align-items: center; - justify-content: center; - } - - .nav-burger, - .nav-account { - width: 44px; - height: 44px; - border: none; - background: transparent; - color: #fff; - cursor: pointer; - } - - .nav-burger i, - .nav-account i { - font-size: 1.35em; - } - - .nav-account { - margin-right: 15px; - justify-self: end; - opacity: 0.95; - } - - .nav-logo img { - height: 30px; - width: auto; - display: block; - } - - .nav-list { - grid-column: 1 / -1; - margin: 0; - padding: 10px 12px 12px; - display: none; - flex-direction: column; - gap: 6px; - background: linear-gradient(180deg, #006d77 0%, #005c66 100%); - border-top: 1px solid rgba(255, 255, 255, 0.15); - } - - .navbar.is-open .nav-list { - display: flex; - } - - .nav-list li a { - flex-direction: row; - gap: 10px; - padding: 10px 12px; - border-radius: 10px; - font-size: 0.95em; - opacity: 0.95; - transform: none; - } - - .nav-list li a i { - margin: 0; - font-size: 1.15em; - width: 22px; - text-align: center; - } -} diff --git a/src/app/pages/shared/navbar/navbar.component.html b/src/app/pages/shared/navbar/navbar.component.html deleted file mode 100644 index 38a89274a..000000000 --- a/src/app/pages/shared/navbar/navbar.component.html +++ /dev/null @@ -1,9 +0,0 @@ -<nav class="navbar"> - <ul> - <!-- <li><a routerLink="/home"><i class="fas fa-home"></i>Home</a></li> --> - <!-- <li><a routerLink="/federations"><i class="fas fa-users"></i>Federations</a></li> --> - <!-- <li><a routerLink="/experiments-dashboard"><i class="fas fa-flask"></i>Experiments</a></li> --> - <!-- <li><a routerLink="/experiment-studio"><i class="fas fa-flask"></i>Experiments</a></li> --> - <!-- <li><a routerLink="/about"><i class="fas fa-info"></i>About</a></li> --> - </ul> -</nav> diff --git a/src/app/pages/shared/navbar/navbar.component.ts b/src/app/pages/shared/navbar/navbar.component.ts deleted file mode 100644 index 98706aa91..000000000 --- a/src/app/pages/shared/navbar/navbar.component.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Component, HostListener } from '@angular/core'; -import { RouterModule } from '@angular/router'; - -@Component({ - selector: 'app-navbar', - imports: [RouterModule], - templateUrl: './navbar.component.html', - styleUrls: ['./navbar.component.css'] -}) -export class NavbarComponent { - isNavOpen = false; - - toggleNav(): void { - this.isNavOpen = !this.isNavOpen; - } - - closeNav(): void { - this.isNavOpen = false; - } - - @HostListener('window:resize') - onResize(): void { - if (window.innerWidth > 768 && this.isNavOpen) { - this.isNavOpen = false; - } - } -} diff --git a/src/app/pages/shared/spinner/spinner.component.html b/src/app/pages/shared/spinner/spinner.component.html index 4322057ff..88fef5c15 100644 --- a/src/app/pages/shared/spinner/spinner.component.html +++ b/src/app/pages/shared/spinner/spinner.component.html @@ -1,7 +1,7 @@ -<div class="spinner-container" [class.spinner-overlay]="overlay" [class.spinner-inline]="!overlay"> +<div class="spinner-container" [class.spinner-overlay]="overlay()" [class.spinner-inline]="!overlay()" [style.--spinner-size]="size()" [style.--spinner-color]="color()"> <div class="lds-spinner"> <div></div><div></div><div></div><div></div><div></div><div></div> <div></div><div></div><div></div><div></div><div></div><div></div> </div> - <p class="spinner-text"> {{ text }} </p> + <p class="spinner-text"> {{ text() }} </p> </div> diff --git a/src/app/pages/shared/spinner/spinner.component.scss b/src/app/pages/shared/spinner/spinner.component.scss index ebea19b99..b9a9fc1c1 100644 --- a/src/app/pages/shared/spinner/spinner.component.scss +++ b/src/app/pages/shared/spinner/spinner.component.scss @@ -9,7 +9,7 @@ position: fixed; inset: 0; background-color: rgba(255, 255, 255, 0.801); - backdrop-filter: blur(5px); + backdrop-filter: var(--glass-blur); z-index: 9999; } @@ -17,19 +17,18 @@ position: static; background: transparent; inset: auto; - min-height: 120px; + min-height: 96px; } .lds-spinner { - color: #008080; display: inline-block; position: relative; - width: 64px; - height: 64px; + width: var(--spinner-size, 64px); + height: var(--spinner-size, 64px); } .lds-spinner div { - transform-origin: 32px 32px; + transform-origin: calc(var(--spinner-size, 64px) / 2) calc(var(--spinner-size, 64px) / 2); animation: fade 1.2s linear infinite; } @@ -37,12 +36,12 @@ content: ""; display: block; position: absolute; - top: 3px; - left: 29px; - width: 6px; - height: 18px; + top: calc(var(--spinner-size, 64px) * 0.047); + left: calc(var(--spinner-size, 64px) * 0.453); + width: calc(var(--spinner-size, 64px) * 0.094); + height: calc(var(--spinner-size, 64px) * 0.281); border-radius: 20%; - background: #008080; + background: var(--spinner-color, #2b33e9); } /* rotation + delay per child */ @@ -66,9 +65,9 @@ } .spinner-text { - margin-top: 1.2rem; + margin-top: 0.75rem; letter-spacing: 0.3px; - font-weight: 600; - color: #006666; - font-size: 1.2rem; + font-weight: 700; + color: var(--spinner-color, #2b33e9); + font-size: 0.95rem; } diff --git a/src/app/pages/shared/spinner/spinner.component.ts b/src/app/pages/shared/spinner/spinner.component.ts index 9e0fcb47c..55ae85c15 100644 --- a/src/app/pages/shared/spinner/spinner.component.ts +++ b/src/app/pages/shared/spinner/spinner.component.ts @@ -1,15 +1,15 @@ -import { Component, Input } from '@angular/core'; +import { ChangeDetectionStrategy, Component, input } from '@angular/core'; @Component({ selector: 'app-spinner', - standalone: true, templateUrl: './spinner.component.html', - styleUrls: ['./spinner.component.scss'] + styleUrl: './spinner.component.scss', + changeDetection: ChangeDetectionStrategy.OnPush, }) export class SpinnerComponent { - @Input() size: string = '64px'; - @Input() color: string = 'teal'; - @Input() text?: string; + readonly size = input<string>('64px'); + readonly color = input<string>('teal'); + readonly text = input<string>(); // When false, renders inline rather than a full-screen overlay - @Input() overlay: boolean = true; + readonly overlay = input<boolean>(true); } diff --git a/src/app/pages/shared/utils/form-control.factory.ts b/src/app/pages/shared/utils/form-control.factory.ts index 03b73205c..d4a4299e8 100644 --- a/src/app/pages/shared/utils/form-control.factory.ts +++ b/src/app/pages/shared/utils/form-control.factory.ts @@ -20,6 +20,9 @@ function inOptionsValidator(options: any[]) { // If empty, it is validated by the "required" value if (v === null || v === undefined || v === '') return null; + if (Array.isArray(v)) { + return v.every(item => allowed.has(item)) ? null : { invalidOption: true }; + } return allowed.has(v) ? null : { invalidOption: true }; }; @@ -35,15 +38,27 @@ export function buildFormControl(field: any, initialValue: any = ''): FormContro if (field.pattern) validators.push(Validators.pattern(field.pattern)); // Select validator (options as objects or primitives) - if (field.type === 'select' && Array.isArray(field.options)) { + if ((field.type === 'select' || field.type === 'multi-select') && Array.isArray(field.options)) { validators.push(inOptionsValidator(field.options)); } - // Initial value for select with no value. null to show required - const startValue = - field.type === 'select' && (initialValue === undefined || initialValue === '') - ? null - : (initialValue ?? ''); + let startValue: any; + if (field.type === 'multi-select') { + startValue = Array.isArray(initialValue) + ? initialValue + : initialValue === undefined || initialValue === null || initialValue === '' + ? [] + : [initialValue]; + } else if (field.type === 'select' && (initialValue === undefined || initialValue === '')) { + startValue = null; + } else { + startValue = initialValue ?? ''; + } + + if (field.type === 'number' && typeof startValue === 'string' && startValue.trim() !== '') { + const parsed = Number(startValue); + if (Number.isFinite(parsed)) startValue = parsed; + } return new FormControl(startValue, { validators, diff --git a/src/app/pages/terms-page/terms-page.component.css b/src/app/pages/terms-page/terms-page.component.css index 8b32d5020..1c768adb3 100644 --- a/src/app/pages/terms-page/terms-page.component.css +++ b/src/app/pages/terms-page/terms-page.component.css @@ -1,17 +1,24 @@ +/* Fills the leftover the shell gives this route; no `100vh - 120px` guess. */ +:host { + display: flex; + flex-direction: column; + flex: 1 1 auto; +} + .tos-page { - background: #f4f6f9; - min-height: calc(100vh - 120px); + flex: 1 1 auto; + background: transparent; padding: 32px 20px 48px; } .tos-card { max-width: 1024px; margin: 0 auto; - background: #ffffff; - border: 1px solid #e2e6ef; - border-radius: 12px; - padding: 28px 32px 24px; - box-shadow: 0 10px 26px rgba(20, 20, 40, 0.05); + background: var(--card-bg); + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-lg); + padding: 40px; + box-shadow: var(--studio-card-shadow); } .tos-intro { @@ -122,14 +129,15 @@ .tos-proceed { border: none; - border-radius: 8px; - background: #2563eb; + border-radius: 999px; + background: var(--primary-color); color: #fff; - padding: 10px 20px; + padding: 12px 28px; font-size: 14px; - font-weight: 600; + font-weight: 700; cursor: pointer; - transition: transform 0.1s ease, box-shadow 0.2s ease, background 0.2s ease; + transition: all 0.2s ease; + box-shadow: 0 4px 12px rgba(43, 51, 233, 0.2); } .tos-proceed:disabled { diff --git a/src/app/pages/terms-page/terms-page.component.ts b/src/app/pages/terms-page/terms-page.component.ts index 99967806d..a8a3a25a2 100644 --- a/src/app/pages/terms-page/terms-page.component.ts +++ b/src/app/pages/terms-page/terms-page.component.ts @@ -9,7 +9,7 @@ import { TermsService } from '../../services/terms.service'; selector: 'app-terms-page', imports: [FormsModule], templateUrl: './terms-page.component.html', - styleUrls: ['./terms-page.component.css'], + styleUrl: './terms-page.component.css', changeDetection: ChangeDetectionStrategy.OnPush }) export class TermsPageComponent implements OnInit { @@ -74,16 +74,8 @@ export class TermsPageComponent implements OnInit { this.http.post('/services/activeUser/agreeNDA', {}).subscribe({ next: () => { - this.authService.refreshAuthState().subscribe({ - next: () => { - navigateToDashboard(); - }, - error: () => { - this.submitting = false; - this.acceptError = true; - this.cdr.markForCheck(); - } - }); + this.authService.markTermsAccepted(); + navigateToDashboard(); }, error: () => { this.submitting = false; @@ -251,12 +243,14 @@ export class TermsPageComponent implements OnInit { .replace(/\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>') .replace(/\[([^\]]+)\]\((mailto:[^)]+)\)/g, '<a href="$2">$1</a>') .replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>') - .replace(/_([^_]+)_/g, '<em>$1</em>'); + .replace(/_([^_]+)_/g, '<em>$1</em>') + .replace(/<u>/gi, '<u>') + .replace(/<\/u>/gi, '</u>'); } private escapeHtml(value: string): string { return value - .replace(/&/g, '&') + .replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') diff --git a/src/app/services/algorithm-rules.service.spec.ts b/src/app/services/algorithm-rules.service.spec.ts index 9817dd4db..f36df7dc6 100644 --- a/src/app/services/algorithm-rules.service.spec.ts +++ b/src/app/services/algorithm-rules.service.spec.ts @@ -1,13 +1,16 @@ -import { AlgorithmNames, AlgorithmRoles, VariableTypes } from '../core/constants/algorithm.constants'; import { AlgorithmConfig } from '../models/algorithm-definition.model'; import { AlgorithmRulesService } from './algorithm-rules.service'; describe('AlgorithmRulesService', () => { let service: AlgorithmRulesService; + const baseInputdata = { + data_model: { label: 'data_model', desc: '', types: ['text'], required: true, max_count: 1 }, + datasets: { label: 'datasets', desc: '', types: ['text'], required: true }, + }; - const buildAlgo = (name: string): AlgorithmConfig => ({ - name, - label: name, + const buildAlgo = (inputdata: Record<string, any>): AlgorithmConfig => ({ + name: 'mock_algo', + label: 'mock_algo', description: '', requiredVariable: [], covariate: [], @@ -15,10 +18,8 @@ describe('AlgorithmRulesService', () => { configSchema: [], isDisabled: false, inputdata: { - data_model: { label: 'data_model', desc: '', types: ['text'], required: true, multiple: false }, - datasets: { label: 'datasets', desc: '', types: ['text'], required: true, multiple: true }, - y: { label: 'y', desc: '', types: ['real'], required: true, multiple: false }, - x: { label: 'x', desc: '', types: ['text'], required: true, multiple: true }, + ...baseInputdata, + ...inputdata, }, } as AlgorithmConfig); @@ -26,120 +27,253 @@ describe('AlgorithmRulesService', () => { service = new AlgorithmRulesService(); }); - it('does not disable an algorithm when a filter variable is selected and filter types are payload metadata', () => { - const algo = { - name: 'mock_algo', - label: 'Mock Algo', - description: '', - requiredVariable: [], - covariate: [], - category: 'Mock', - configSchema: [], - isDisabled: false, - inputdata: { - data_model: { label: 'data_model', desc: '', types: ['text'], required: true, multiple: false }, - datasets: { label: 'datasets', desc: '', types: ['text'], required: true, multiple: true }, - y: { label: 'y', desc: '', types: ['real'], required: true, multiple: false }, - x: { label: 'x', desc: '', types: ['real'], required: false, multiple: true }, - filter: { label: 'filter', desc: '', types: ['json'], required: false, multiple: true }, - }, - } as AlgorithmConfig; + it('validates required roles from the JSON specification', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['real'], required: true, max_count: 1 }, + x: { label: 'x', desc: '', types: ['text'], required: false }, + }); + + expect(service.isAlgorithmAvailable(algo, { y: [], x: [] })).toBe(false); + expect(service.evaluateAlgorithmAvailability(algo, { y: [], x: [] }).summary) + .toBe('Outcome needs at least 1 (none assigned).'); + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'v1', label: 'var1', type: 'continuous' } as any], + x: [], + })).toBe(true); + }); + + it('validates max_count from the JSON specification', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['real'], required: true, max_count: 1 }, + }); const available = service.isAlgorithmAvailable(algo, { - y: [{ code: 'v1', label: 'var1', type: 'real' } as any], + y: [ + { code: 'v1', label: 'var1', type: 'continuous' } as any, + { code: 'v2', label: 'var2', type: 'continuous' } as any, + ], x: [], - filters: [{ code: 'f1', label: 'filter1', type: 'real' } as any], }); - expect(available).toBeTrue(); + expect(available).toBe(false); }); - it('still enforces required filter presence when required is true', () => { + it('validates selected variable types from the JSON specification', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['real'], required: true, max_count: 1 }, + }); + + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'v1', label: 'var1', type: 'nominal', enumerations: [{ code: 'a', label: 'A' }] } as any], + x: [], + })).toBe(false); + + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'v1', label: 'var1', type: 'continuous' } as any], + x: [], + })).toBe(true); + }); + + it('uses normalized metadata type aliases only for matching specification types', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['text'], required: true, max_count: 1 }, + }); + + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'v1', label: 'var1', type: 'nominal', enumerations: [{ code: 'a', label: 'A' }] } as any], + x: [], + })).toBe(true); + }); + + it('rejects a free-text CDE (categorical without enumerations) but not a derived column', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['text'], required: true, max_count: 1 }, + }); + + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'subjectid', label: 'Subject', type: 'nominal' } as any], + x: [], + })).toBe(false); + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'derived', label: 'Derived', type: 'nominal', enumerations: [], isCreatedColumn: true } as any], + x: [], + })).toBe(true); + }); + + it('does not apply algorithm-name-specific restrictions beyond the JSON specification for unspecified algorithms', () => { const algo = { + ...buildAlgo({ + y: { label: 'y', desc: '', types: ['real'], required: true, max_count: 1 }, + x: { label: 'x', desc: '', types: ['text'], required: true }, + }), name: 'mock_algo', - label: 'Mock Algo', - description: '', - requiredVariable: [], - covariate: [], - category: 'Mock', - configSchema: [], - isDisabled: false, - inputdata: { - data_model: { label: 'data_model', desc: '', types: ['text'], required: true, multiple: false }, - datasets: { label: 'datasets', desc: '', types: ['text'], required: true, multiple: true }, - y: { label: 'y', desc: '', types: ['real'], required: true, multiple: false }, - [AlgorithmRoles.FILTER]: { label: 'filter', desc: '', types: ['json'], required: true, multiple: true }, - }, } as AlgorithmConfig; - const available = service.isAlgorithmAvailable(algo, { - y: [{ code: 'v1', label: 'var1', type: 'real' } as any], - x: [], - filters: [], + const availableWithOneCovariate = service.isAlgorithmAvailable(algo, { + y: [{ code: 'age', label: 'Age', type: 'continuous' } as any], + x: [{ code: 'sex', label: 'Sex', type: 'nominal', enumerations: [{ code: 'a', label: 'A' }] } as any], }); - expect(available).toBeFalse(); + expect(availableWithOneCovariate).toBe(true); }); - it('enforces mandatory role when legacy notblank=true is provided', () => { + it('limits linear regression to one target variable when backend omits max_count', () => { const algo = { - ...buildAlgo('mock_notblank_algo'), - inputdata: { - data_model: { label: 'data_model', desc: '', types: ['text'], required: true, multiple: false }, - datasets: { label: 'datasets', desc: '', types: ['text'], required: true, multiple: true }, - y: { label: 'y', desc: '', types: ['real'], notblank: true, multiple: false }, - x: { label: 'x', desc: '', types: ['text'], required: false, multiple: true }, - }, + ...buildAlgo({ + y: { label: 'y', desc: '', types: ['real', 'int'], required: true }, + x: { label: 'x', desc: '', types: ['real', 'int', 'text'], required: false }, + }), + name: 'linear_regression', } as AlgorithmConfig; - const available = service.isAlgorithmAvailable(algo, { - y: [], + const result = service.evaluateAlgorithmAvailability(algo, { + y: [ + { code: 'age', label: 'Age', type: 'continuous' } as any, + { code: 'bmi', label: 'BMI', type: 'continuous' } as any, + ], + x: [{ code: 'sex', label: 'Sex', type: 'nominal', enumerations: [{ code: 'a', label: 'A' }] } as any], + }); + + expect(result.available).toBe(false); + expect(result.summary).toBe('Outcome allows at most 1, selected 2.'); + expect(result.details.find((detail) => detail.role === 'y')).toEqual(expect.objectContaining({ + selectedCount: 2, + maxCount: 1, + satisfied: false, + })); + }); + + it('ignores filter input declarations when evaluating availability', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['real'], required: true, max_count: 1 }, + filter: { label: 'filter', desc: '', types: ['json'], required: true, max_count: 1 }, + }); + + const result = service.evaluateAlgorithmAvailability(algo, { + y: [{ code: 'v1', label: 'var1', type: 'continuous' } as any], x: [], - filters: [], }); - expect(available).toBeFalse(); + expect(result.available).toBe(true); + expect(result.details.map((detail) => detail.role)).not.toContain('filter' as any); }); - it('enforces strict 2-way ANOVA rule for canonical anova_twoway', () => { - const algo = buildAlgo(AlgorithmNames.ANOVA_TWOWAY); + it('validates canonical min_count and max_count limits', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['real'], min_count: 2, max_count: 3 }, + }); - const valid = service.isAlgorithmAvailable(algo, { - y: [{ code: 'age', label: 'age', type: VariableTypes.REAL } as any], - x: [ - { code: 'sex', label: 'sex', type: VariableTypes.NOMINAL } as any, - { code: 'dataset', label: 'dataset', type: VariableTypes.TEXT } as any, + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'v1', label: 'var1', type: 'continuous' } as any], + x: [], + })).toBe(false); + + expect(service.isAlgorithmAvailable(algo, { + y: [ + { code: 'v1', label: 'var1', type: 'continuous' } as any, + { code: 'v2', label: 'var2', type: 'continuous' } as any, ], - filters: [], + x: [], + })).toBe(true); + }); + + it('requires at least two covariates for Binary GLMM when min_count is omitted', () => { + const algo = { + ...buildAlgo({ + y: { label: 'y', desc: '', types: ['int', 'text'], stattypes: ['nominal'], required: true }, + x: { label: 'x', desc: '', types: ['real', 'int', 'text'], stattypes: ['numerical', 'nominal'], required: true }, + }), + name: 'glmm_binary', + } as AlgorithmConfig; + + const unavailable = service.evaluateAlgorithmAvailability(algo, { + y: [{ code: 'outcome', label: 'Outcome', type: 'binary', stattype: 'nominal', enumerations: [{ code: 'a', label: 'A' }] } as any], + x: [{ code: 'age', label: 'Age', type: 'continuous', stattype: 'numerical' } as any], }); - const wrongCovariateCount = service.isAlgorithmAvailable(algo, { - y: [{ code: 'age', label: 'age', type: VariableTypes.REAL } as any], - x: [{ code: 'sex', label: 'sex', type: VariableTypes.NOMINAL } as any], - filters: [], + expect(unavailable.available).toBe(false); + expect(unavailable.summary).toBe('Predictor needs at least 2, selected 1.'); + expect(unavailable.details.find((detail) => detail.role === 'y')).toEqual(expect.objectContaining({ + minCount: 1, + maxCount: 1, + })); + expect(unavailable.details.find((detail) => detail.role === 'x')).toEqual(expect.objectContaining({ + minCount: 2, + maxCount: null, + })); + }); + + it('rejects duplicate selections and y/x overlap', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['real'], required: true }, + x: { label: 'x', desc: '', types: ['real'], required: false }, }); - const wrongCovariateType = service.isAlgorithmAvailable(algo, { - y: [{ code: 'age', label: 'age', type: VariableTypes.REAL } as any], - x: [ - { code: 'sex', label: 'sex', type: VariableTypes.NOMINAL } as any, - { code: 'cholesterol', label: 'cholesterol', type: VariableTypes.REAL } as any, + expect(service.isAlgorithmAvailable(algo, { + y: [ + { code: 'age', label: 'Age', type: 'continuous' } as any, + { code: 'age', label: 'Age', type: 'continuous' } as any, ], - filters: [], + x: [], + })).toBe(false); + + expect(service.isAlgorithmAvailable(algo, { + y: [{ code: 'age', label: 'Age', type: 'continuous' } as any], + x: [{ code: 'age', label: 'Age', type: 'continuous' } as any], + })).toBe(false); + }); + + it('displays nominal as the user-facing type when stattype validation fails for text inputs', () => { + const algo = buildAlgo({ + y: { label: 'y', desc: '', types: ['text'], stattypes: ['nominal'], required: true, max_count: 1 }, + }); + + const result = service.evaluateAlgorithmAvailability(algo, { + y: [{ code: 'v1', label: 'var1', type: 'nominal', stattype: 'ordinal', enumerations: [{ code: 'a', label: 'A' }] } as any], + x: [], + }); + + expect(result.available).toBe(false); + expect(result.summary).toBe('Outcome type must be one of nominal.'); + }); + + it('displays text as nominal in availability type errors', () => { + const algo = buildAlgo({ + x: { label: 'x', desc: '', types: ['real', 'int', 'text'], required: true }, + }); + + const result = service.evaluateAlgorithmAvailability(algo, { + y: [], + x: [{ code: 'v1', label: 'var1', type: 'boolean' } as any], }); - expect(valid).toBeTrue(); - expect(wrongCovariateCount).toBeFalse(); - expect(wrongCovariateType).toBeFalse(); + expect(result.available).toBe(false); + expect(result.summary).toBe('Predictor type must be one of real, int, nominal.'); + expect(result.summary).not.toContain('text'); }); - it('returns requirement override for canonical anova_twoway', () => { - const override = service.getAlgorithmRequirementOverrides({ - name: AlgorithmNames.ANOVA_TWOWAY, - inputdata: {}, + + it('returns structured details for optional covariate max_count failures', () => { + const algo = buildAlgo({ + x: { label: 'x', desc: '', types: ['real'], required: false, max_count: 1 }, }); - expect(override?.x).toContain('exactly 2'); - expect(override?.y).toContain('exactly 1'); + const result = service.evaluateAlgorithmAvailability(algo, { + y: [], + x: [ + { code: 'v1', label: 'var1', type: 'continuous' } as any, + { code: 'v2', label: 'var2', type: 'continuous' } as any, + ], + }); + + expect(result.available).toBe(false); + expect(result.summary).toBe('Predictor allows at most 1, selected 2.'); + expect(result.details.find((detail) => detail.role === 'x')).toEqual(expect.objectContaining({ + label: 'Predictor', + selectedCount: 2, + maxCount: 1, + satisfied: false, + })); }); + }); diff --git a/src/app/services/algorithm-rules.service.ts b/src/app/services/algorithm-rules.service.ts index 098806737..9b2ccdae6 100644 --- a/src/app/services/algorithm-rules.service.ts +++ b/src/app/services/algorithm-rules.service.ts @@ -1,202 +1,215 @@ - import { Injectable } from '@angular/core'; -import { AlgorithmConfig } from '../models/algorithm-definition.model'; +import { + AlgorithmAvailability, + AlgorithmAvailabilityDetail, + AlgorithmAvailabilityRole, + AlgorithmConfig, +} from '../models/algorithm-definition.model'; import { D3HierarchyNode } from '../models/data-model.interface'; -import { AlgorithmNames, VariableTypes, AlgorithmRoles } from '../core/constants/algorithm.constants'; +import { VariableTypes, AlgorithmRoles } from '../core/constants/algorithm.constants'; +import { resolveInputMaxCount, resolveInputMinCount } from '../core/algorithm-input-counts'; +import { isFreeTextCde, specificationAcceptsVariableType } from '../core/data-model.utils'; -type SelectionRole = 'y' | 'x' | 'filters'; -type RoleSelections = Record<SelectionRole, D3HierarchyNode[]>; +type SelectionRole = 'y' | 'x'; +type AvailabilitySelections = Record<SelectionRole, D3HierarchyNode[]>; @Injectable({ providedIn: 'root' }) export class AlgorithmRulesService { - constructor() { } - private isTwoWayAnova(name: string | undefined | null): boolean { - return name === AlgorithmNames.ANOVA_TWOWAY; - } - - isAlgorithmAvailable( + evaluateAlgorithmAvailability( algo: AlgorithmConfig, - selections: RoleSelections - ): boolean { - if (!algo?.inputdata) return false; - const name = algo.name; - - // SPECIAL CASES - - // 1-way ANOVA: 1 dependent (real/int) + 1 factor (nominal/text) - if (name === AlgorithmNames.ANOVA_ONEWAY) { - const { y: vars, x: covs } = selections; - - if (vars.length !== 1) return false; - if (covs.length !== 1) return false; - - const yType = vars[0].type; - const xType = covs[0].type; + selections: AvailabilitySelections + ): AlgorithmAvailability { + if (!algo?.inputdata) { + return { + available: false, + summary: 'Algorithm has no input data specification.', + details: [], + }; + } - // y: real/int, x: nominal/text - const yOk = [VariableTypes.REAL, VariableTypes.INTEGER, VariableTypes.INT].includes(yType ?? ''); - const xOk = [VariableTypes.NOMINAL, VariableTypes.TEXT].includes(xType ?? ''); + const details: AlgorithmAvailabilityDetail[] = []; + const hasRole = (role: string) => Object.prototype.hasOwnProperty.call(algo.inputdata, role); - return yOk && xOk; + if (hasRole(AlgorithmRoles.Y)) { + details.push(this.evaluateRole('y', (algo.inputdata as any).y, selections.y ?? [], algo)); + } else if ((selections.y ?? []).length > 0) { + details.push(this.evaluateUnsupportedRole('y', selections.y.length)); } - // 2-way ANOVA: 1 dependent + 2 nominal factors - if (this.isTwoWayAnova(name)) { - const { y: vars, x: covs } = selections; - - if (vars.length !== 1) return false; - if (covs.length !== 2) return false; + if (hasRole(AlgorithmRoles.X)) { + details.push(this.evaluateRole('x', (algo.inputdata as any).x, selections.x ?? [], algo)); + } else if ((selections.x ?? []).length > 0) { + details.push(this.evaluateUnsupportedRole('x', selections.x.length)); + } - const allCovsNominal = covs.every(c => - [VariableTypes.NOMINAL, VariableTypes.TEXT].includes(c.type ?? '') - ); + this.addOverlapMessages(details, selections.y ?? [], selections.x ?? []); + + const available = details.every((detail) => detail.messages.length === 0); + return { + available, + summary: details.find((detail) => detail.messages.length > 0)?.messages[0] ?? null, + details: details.map((detail) => ({ + ...detail, + satisfied: detail.messages.length === 0, + })), + }; + } - const yOk = [VariableTypes.REAL, VariableTypes.INTEGER, VariableTypes.INT].includes(vars[0].type ?? ''); + isAlgorithmAvailable( + algo: AlgorithmConfig, + selections: AvailabilitySelections + ): boolean { + return this.evaluateAlgorithmAvailability(algo, selections).available; + } - return yOk && allCovsNominal; + private evaluateRole( + role: Extract<AlgorithmAvailabilityRole, 'y' | 'x'>, + field: any, + selected: D3HierarchyNode[], + algo: AlgorithmConfig + ): AlgorithmAvailabilityDetail { + const label = this.roleLabel(role); + const minCount = resolveInputMinCount(field, role, algo.name); + const maxCount = resolveInputMaxCount(field, role, algo.name); + const types = this.normalizeStringList(field?.types); + const stattypes = this.normalizeStringList(field?.stattypes); + const messages: string[] = []; + + if (selected.length < minCount) { + if (selected.length === 0) { + messages.push(label + ' needs at least ' + minCount + ' (none assigned).'); + } else { + messages.push(label + ' needs at least ' + minCount + ', selected ' + selected.length + '.'); + } } - // One-sample t-test: exactly 1 dependent variable, no covariates - if (name === AlgorithmNames.TTEST_ONESAMPLE) { - const { y: vars, x: covs } = selections; - - if (vars.length !== 1) return false; - if (covs.length !== 0) return false; - - const yType = vars[0].type; - const yOk = [VariableTypes.REAL, VariableTypes.INTEGER, VariableTypes.INT].includes(yType ?? ''); - return yOk; + if (maxCount !== null && selected.length > maxCount) { + messages.push(label + ' allows at most ' + maxCount + ', selected ' + selected.length + '.'); } - if (name === AlgorithmNames.PCA || name === AlgorithmNames.PCA_WITH_TRANSFORMATION) { - const { y: vars, x: covs } = selections; - if (vars.length < 2) return false; - if (covs.length !== 0) return false; + if (this.hasDuplicateCodes(selected)) { + messages.push(label + ' contains duplicate selections.'); + } - // PCA requires numeric variables only; block if any nominal/text is selected. - const invalidType = vars.find((v) => { - const t = this.normalizeType(v.type); - return t === VariableTypes.TEXT || t === VariableTypes.NOMINAL || !t || ![VariableTypes.REAL, VariableTypes.INT, VariableTypes.INTEGER].includes(t); + if (selected.length > 0 && types.length > 0) { + const invalid = selected.find((node) => { + const type = (node as any).type; + return !!type && !specificationAcceptsVariableType(types, type); }); - - return !invalidType; + if (invalid) { + this.pushUnique(messages, this.typeRequirementMessage(label, types)); + } } - const hasRole = (role: string) => Object.prototype.hasOwnProperty.call(algo.inputdata, role); + // Derived columns get their categories from the creator config, not the CDE. + if (selected.some((node) => !(node as any).isCreatedColumn && isFreeTextCde(node as any))) { + messages.push(label + ' contains a free-text variable, which cannot be analysed.'); + } - const filterReq = hasRole(AlgorithmRoles.FILTERS) - ? (algo.inputdata as any).filters - : hasRole(AlgorithmRoles.FILTER) - ? (algo.inputdata as any).filter - : null; + if (selected.length > 0 && stattypes.length > 0) { + const reqStatTypes = stattypes.map((type) => this.normalizeStatType(type)); + const invalid = selected.find((node) => { + const normalized = this.normalizeStatType((node as any).stattype ?? (node as any).statType ?? (node as any).stat_type); + return !!normalized && !reqStatTypes.includes(normalized); + }); + if (invalid) { + this.pushUnique(messages, this.typeRequirementMessage(label, stattypes)); + } + } - for (const [role, req] of Object.entries(algo.inputdata)) { - if (![AlgorithmRoles.Y, AlgorithmRoles.X].includes(role)) continue; + return { + role, + label, + selectedCount: selected.length, + minCount, + maxCount, + required: minCount > 0 || this.isFieldRequired(field), + types, + stattypes, + messages, + satisfied: messages.length === 0, + }; + } - const sel: D3HierarchyNode[] = selections[role as SelectionRole] ?? []; - const isRequired = this.isFieldRequired(req); - const multiple = this.normalizeBool((req as any)?.multiple); + private evaluateUnsupportedRole( + role: Extract<AlgorithmAvailabilityRole, 'y' | 'x'>, + selectedCount: number + ): AlgorithmAvailabilityDetail { + const label = this.roleLabel(role); + return { + role, + label, + selectedCount, + minCount: 0, + maxCount: 0, + required: false, + types: [], + stattypes: [], + messages: [label + ' is not accepted by this algorithm.'], + satisfied: false, + }; + } - if (isRequired && sel.length === 0) { - console.warn(`✘ ${name}: role ${role} is required but selection is empty.`); - return false; - } + private addOverlapMessages( + details: AlgorithmAvailabilityDetail[], + y: D3HierarchyNode[], + x: D3HierarchyNode[] + ): void { + if (!this.hasOverlap(y, x)) return; + const yDetail = details.find((detail) => detail.role === 'y'); + const xDetail = details.find((detail) => detail.role === 'x'); + yDetail?.messages.push('Outcome is already selected as a predictor.'); + xDetail?.messages.push('Predictor is already selected as an outcome.'); + } - if (multiple === false && sel.length > 1) { - console.warn(`✘ ${name}: role ${role} only allows single selection but has ${sel.length}.`); - return false; - } + private roleLabel(role: AlgorithmAvailabilityRole): string { + return role === 'y' ? 'Outcome' : 'Predictor'; + } - if (sel.length === 0) continue; - - // type check - if ((req as any).types?.length) { - const reqTypes = ((req as any).types as string[]).map(t => this.normalizeType(t)); - const selTypes = sel - .map(v => this.normalizeType(v.type)) - .filter((t): t is string => !!t); - - const badTypeIndex = selTypes.findIndex(t => !reqTypes.includes(t)); - if (badTypeIndex !== -1) { - console.warn( - `✘ ${name}: invalid type on role ${role}: "${selTypes[badTypeIndex]}" (from variable "${sel[badTypeIndex].label}") not in [${reqTypes.join(', ')}]` - ); - return false; - } - } + private typeRequirementMessage(label: string, types: string[]): string { + if (types.length > 0) { + return label + ' type must be one of ' + this.formatDisplayTypes(types).join(', ') + '.'; } + return label + ' type is not accepted by this algorithm.'; + } - const yConfigured = Object.prototype.hasOwnProperty.call(algo.inputdata, AlgorithmRoles.Y); - const xConfigured = Object.prototype.hasOwnProperty.call(algo.inputdata, AlgorithmRoles.X); - - if (!yConfigured && selections.y.length > 0) return false; - if (!xConfigured && selections.x.length > 0) return false; - - if (filterReq) { - const sel: D3HierarchyNode[] = selections[AlgorithmRoles.FILTERS as SelectionRole] ?? []; - const isRequired = this.isFieldRequired(filterReq); - const multiple = this.normalizeBool((filterReq as any).multiple); + private formatDisplayTypes(types: string[]): string[] { + return Array.from(new Set( + types + .map((type) => this.normalizeDisplayType(type)) + .filter((type): type is string => !!type) + )); + } - if (isRequired && sel.length === 0) { - console.warn(`✘ ${name}: filters are required but none selected.`); - return false; - } - if (multiple === false && sel.length > 1) { - console.warn(`✘ ${name}: filters role only allows single selection but has ${sel.length}.`); - return false; - } + private normalizeDisplayType(type: string | undefined | null): string | undefined { + const normalized = String(type ?? '').trim().toLowerCase(); + if (!normalized) return undefined; + + switch (normalized) { + case VariableTypes.TEXT: + case 'polynominal': + case 'ordinal': + return VariableTypes.NOMINAL; + case VariableTypes.INTEGER: + return VariableTypes.INT; + default: + return normalized; } + } - return true; - } - - getAlgorithmRequirementOverrides(algo: { name?: string; inputdata?: any } | null | undefined): { y?: string; x?: string; filters?: string } | null { - const name = algo?.name; - const formatTypes = (types?: string[] | null) => - Array.isArray(types) && types.length ? ` • types: ${types.join(',')}` : ''; - const yTypes = Array.isArray(algo?.inputdata?.y?.types) ? algo?.inputdata?.y?.types : null; - const xTypes = Array.isArray(algo?.inputdata?.x?.types) ? algo?.inputdata?.x?.types : null; - - switch (name) { - case AlgorithmNames.ANOVA_ONEWAY: - return { - y: `Variable: exactly 1${formatTypes([VariableTypes.REAL, VariableTypes.INT])}`, - x: `Covariate: exactly 1${formatTypes([VariableTypes.NOMINAL, VariableTypes.TEXT])}`, - }; - case AlgorithmNames.ANOVA_TWOWAY: - return { - y: `Variable: exactly 1${formatTypes([VariableTypes.REAL, VariableTypes.INT])}`, - x: `Covariate: exactly 2${formatTypes([VariableTypes.NOMINAL, VariableTypes.TEXT])}`, - }; - case AlgorithmNames.TTEST_ONESAMPLE: - return { - y: `Variable: exactly 1${formatTypes([VariableTypes.REAL, VariableTypes.INT])}`, - x: 'Covariate: none', - }; - case AlgorithmNames.PCA: - return { - y: `Variable: 2+${formatTypes(yTypes ?? [VariableTypes.REAL, VariableTypes.INT])}`, - x: 'Covariate: none', - }; - default: - return null; + private pushUnique(messages: string[], message: string): void { + if (!messages.includes(message)) { + messages.push(message); } } - // helper: normalize types from UI -> backend - private normalizeType(t: string | undefined | null): string | undefined { + private normalizeStatType(t: string | undefined | null): string | undefined { if (!t) return undefined; - switch (t) { - case VariableTypes.NOMINAL: return VariableTypes.TEXT; - case VariableTypes.INTEGER: return VariableTypes.INT; - case 'polynominal': return VariableTypes.TEXT; - case 'ordinal': return VariableTypes.TEXT; - default: return t; // real, text, binary... - } + return String(t).trim().toLowerCase() || undefined; } private normalizeBool(value: boolean | string | undefined | null): boolean | null { @@ -208,10 +221,34 @@ export class AlgorithmRulesService { return null; } + private normalizeStringList(value: unknown): string[] { + if (!Array.isArray(value)) return []; + return Array.from(new Set( + value + .map((item) => String(item ?? '').trim()) + .filter((item) => item.length > 0) + )); + } + + private hasDuplicateCodes(selection: D3HierarchyNode[]): boolean { + const codes = selection + .map((item: any) => item?.code) + .filter((code): code is string => code !== undefined && code !== null && String(code).trim() !== '') + .map((code) => String(code)); + return new Set(codes).size !== codes.length; + } + + private hasOverlap(left: D3HierarchyNode[], right: D3HierarchyNode[]): boolean { + const leftCodes = new Set( + left + .map((item: any) => item?.code) + .filter((code) => code !== undefined && code !== null && String(code).trim() !== '') + .map((code) => String(code)) + ); + return right.some((item: any) => leftCodes.has(String(item?.code ?? ''))); + } + private isFieldRequired(field: any): boolean { - // Prefer canonical `required`, but keep legacy `notblank` compatibility. - const required = this.normalizeBool(field?.required); - if (required !== null) return required; - return this.normalizeBool(field?.notblank) === true; + return this.normalizeBool(field?.required) === true; } } diff --git a/src/app/services/auth.interceptor.spec.ts b/src/app/services/auth.interceptor.spec.ts new file mode 100644 index 000000000..1c434b465 --- /dev/null +++ b/src/app/services/auth.interceptor.spec.ts @@ -0,0 +1,75 @@ +import type { Mock } from 'vitest'; +import { HttpClient, provideHttpClient, withInterceptors } from '@angular/common/http'; +import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; + +import { AuthService } from './auth.service'; +import { withCredentialsInterceptor } from './auth.interceptor'; + +describe('withCredentialsInterceptor', () => { + let http: HttpClient; + let httpMock: HttpTestingController; + let login: Mock; + + beforeEach(() => { + login = vi.fn(); + TestBed.configureTestingModule({ + providers: [ + provideHttpClient(withInterceptors([withCredentialsInterceptor])), + provideHttpClientTesting(), + { provide: AuthService, useValue: { login } }, + ], + }); + http = TestBed.inject(HttpClient); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => httpMock.verify()); + + /** Sends a request and answers it the way a dead session answers: no usable status. */ + const failWith = (status: number, url = '/services/experiment-folders') => { + let failed = false; + http.get(url).subscribe({ error: () => (failed = true) }); + httpMock.expectOne(url).error(new ErrorEvent('network error'), { status, statusText: 'Session expired' }); + return () => failed; + }; + + it('sends cookies on same-origin API calls', () => { + http.get('/services/experiment-folders').subscribe(); + const request = httpMock.expectOne('/services/experiment-folders'); + expect(request.request.withCredentials).toBe(true); + request.flush({}); + }); + + it('sends the browser session back to Keycloak when the session died (status 0)', () => { + expect(failWith(0)()).toBe(true); + expect(login).toHaveBeenCalled(); + }); + + it('sends the browser session back to Keycloak on 401', () => { + expect(failWith(401)()).toBe(true); + expect(login).toHaveBeenCalled(); + }); + + it('leaves real backend failures to the caller', () => { + expect(failWith(500)()).toBe(true); + expect(login).not.toHaveBeenCalled(); + }); + + it('leaves the first-load auth check to AuthGuard', () => { + expect(failWith(401, '/services/activeUser')()).toBe(true); + expect(login).not.toHaveBeenCalled(); + }); + + it('leaves the first-load auth check to AuthGuard when it carries query params', () => { + expect(failWith(401, '/services/activeUser?cache=1')()).toBe(true); + expect(login).not.toHaveBeenCalled(); + }); + + it('does not treat an offline zero-status failure as an expired session', () => { + vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(false); + + expect(failWith(0)()).toBe(true); + expect(login).not.toHaveBeenCalled(); + }); +}); diff --git a/src/app/services/auth.interceptor.ts b/src/app/services/auth.interceptor.ts index 9f1c23871..7944e54a8 100644 --- a/src/app/services/auth.interceptor.ts +++ b/src/app/services/auth.interceptor.ts @@ -1,10 +1,35 @@ -import { HttpInterceptorFn } from '@angular/common/http'; +import { HttpErrorResponse, HttpInterceptorFn } from '@angular/common/http'; +import { inject } from '@angular/core'; +import { catchError, throwError } from 'rxjs'; + +import { AuthService } from './auth.service'; const absoluteUrlPattern = /^https?:\/\//i; +const authProbePath = '/services/activeUser'; export const withCredentialsInterceptor: HttpInterceptorFn = (req, next) => { - if (!req.withCredentials && !absoluteUrlPattern.test(req.url)) { - return next(req.clone({ withCredentials: true })); + if (req.withCredentials || absoluteUrlPattern.test(req.url)) { + return next(req); } - return next(req); + + // inject() only works while the interceptor itself runs, not inside async operators. + const auth = inject(AuthService); + + return next(req.clone({ withCredentials: true })).pipe( + catchError((error: unknown) => { + // A stale session is answered with a 302 to the IdP, which the XHR follows cross-origin, + // so Angular reports status 0; a request that carried no cookie sees 401. Both mean "sign + // in again", and only a top-level navigation can do that — HttpClient will not. + // /services/activeUser is excluded: AuthGuard owns that first-load decision. + // A zero status while the browser is offline is a network failure, not an expired session. + const status = error instanceof HttpErrorResponse ? error.status : -1; + const offlineNetworkFailure = status === 0 && typeof navigator !== 'undefined' && !navigator.onLine; + const sessionLost = (status === 401 || status === 0) && !offlineNetworkFailure; + const requestPath = req.url.split('?')[0]; + if (sessionLost && requestPath.startsWith('/services/') && requestPath !== authProbePath) { + auth.login(`${window.location.pathname}${window.location.search}${window.location.hash}`); + } + return throwError(() => error); + }), + ); }; diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index c157564f4..25a54dac5 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -3,12 +3,12 @@ import { toObservable } from '@angular/core/rxjs-interop'; import { Router } from '@angular/router'; import { HttpClient } from '@angular/common/http'; import { Observable, of } from 'rxjs'; -import { catchError, map, tap, filter } from 'rxjs/operators'; +import { catchError, tap, filter } from 'rxjs/operators'; import { User } from '../models/user.interface'; -export type AuthStatus = 'checking' | 'authenticated' | 'unauthenticated'; +type AuthStatus = 'checking' | 'authenticated' | 'unauthenticated'; -export interface AuthState { +interface AuthState { status: AuthStatus; user?: User | null; } @@ -38,7 +38,7 @@ export class AuthService { this.refreshAuthState().subscribe(); } - refreshAuthState(): Observable<User | null> { + private refreshAuthState(): Observable<User | null> { this.authStateSignal.set({ status: 'checking' }); return this.refreshUser(); } @@ -89,6 +89,16 @@ export class AuthService { return this.authState().user ?? null; } + markTermsAccepted(): void { + const state = this.authState(); + if (state.status === 'authenticated' && state.user) { + this.authStateSignal.set({ + ...state, + user: { ...state.user, agreeNDA: true } + }); + } + } + onAuthResolved(): Observable<AuthState> { return this.authState$.pipe(filter((state) => state.status !== 'checking')); } @@ -145,7 +155,4 @@ export class AuthService { return trimmed.startsWith('/') ? trimmed : `/${trimmed}`; } - getToken(): Observable<string> { - return this.http.get('/services/activeUser/token', { responseType: 'text' }); - } } diff --git a/src/app/services/csv-export.service.spec.ts b/src/app/services/csv-export.service.spec.ts new file mode 100644 index 000000000..228d277fb --- /dev/null +++ b/src/app/services/csv-export.service.spec.ts @@ -0,0 +1,89 @@ +import JSZip from 'jszip'; +import { CsvExportService, toCsv } from './csv-export.service'; + +describe('CsvExportService', () => { + it('quotes only the cells that need it', () => { + expect(toCsv(['Variable', 'Value'], [['age, years', 'say "hi"'], ['sex', null]])) + .toBe('Variable,Value\r\n"age, years","say ""hi"""\r\nsex,'); + }); + + it('adds per-group and residual columns when the histogram is split', () => { + const service = new CsvExportService(); + let saved: { content: string; name: string } | null = null; + vi.spyOn(service, 'downloadCsv').mockImplementation((content, name) => { + saved = { content, name }; + }); + + service.exportHistogramCsv( + { + bins: ['0', '10'], + counts: [10, 20], + groups: [{ key: 'a', label: 'Dataset A', counts: [4, 8] }], + }, + 'Age' + ); + + expect(saved!.name).toBe('Age_distribution.csv'); + expect(saved!.content).toBe( + 'Category,Count,Dataset A,Not shown (missing or < privacy threshold)\r\n0,10,4,6\r\n10,20,8,12' + ); + }); + + it('zips one CSV per table, the raw result and a README describing the setup', async () => { + const service = new CsvExportService(); + let saved: { blob: Blob; name: string } | null = null; + vi.spyOn(service as any, 'download').mockImplementation((blob: any, name: any) => (saved = { blob, name })); + + await service.exportExperimentZip({ + filename: 'My experiment', + details: { + experimentName: 'My experiment', + algorithm: 'Logistic Regression', + datasets: ['IST'], + filters: [], + preprocessing: 'none', + mipVersion: '9.0.0', + }, + tables: [{ title: 'Coefficients', columns: ['Variable', 'Estimate'], rows: [['age', '0.12']] }], + result: { coefficients: [0.123456789] }, + }); + + expect(saved!.name).toBe('My_experiment.zip'); + const zip = await JSZip.loadAsync(saved!.blob); + expect(Object.keys(zip.files).sort()).toEqual(['README.txt', 'result.json', 'tables/', 'tables/01_coefficients.csv']); + + const csv = await zip.file('tables/01_coefficients.csv')!.async('string'); + expect(csv).toBe('\uFEFFVariable,Estimate\r\nage,0.12'); + expect(JSON.parse(await zip.file('result.json')!.async('string'))).toEqual({ coefficients: [0.123456789] }); + + const readme = await zip.file('README.txt')!.async('string'); + expect(readme).toContain('Algorithm: Logistic Regression'); + expect(readme).toContain('Datasets: IST'); + expect(readme).toContain('Coefficients: 1 row; columns: Variable, Estimate'); + expect(readme).toContain('from MIP 9.0.0'); + // Empty setup fields are left out instead of printed as "none". + expect(readme).not.toContain('Filters'); + expect(readme).not.toContain('Preprocessing'); + }); + + it('zips exact values where the screen rounds or merges them', async () => { + const service = new CsvExportService(); + let saved: { blob: Blob; name: string } | null = null; + vi.spyOn(service as any, 'download').mockImplementation((blob: any, name: any) => (saved = { blob, name })); + + await service.exportExperimentZip({ + filename: 'run', + details: { experimentName: 'run', algorithm: 'T-test', datasets: [], filters: [], preprocessing: '', mipVersion: null } as any, + tables: [ + { title: 'Summary', columns: ['Metric', 'Value'], rows: [['p-value', { text: '< 0.001', value: 1.2e-9 }]] }, + { title: 'Merged', columns: ['Category', 'A'], rows: [['x', '3 · 50.0%']], export: { columns: ['Category', 'A count', 'A share'], rows: [['x', 3, 0.5]] } }, + ], + result: null, + }); + + const zip = await JSZip.loadAsync(saved!.blob); + expect(await zip.file('tables/01_summary.csv')!.async('string')).toBe('\uFEFFMetric,Value\r\np-value,1.2e-9'); + expect(await zip.file('tables/02_merged.csv')!.async('string')).toBe('\uFEFFCategory,A count,A share\r\nx,3,0.5'); + expect(await zip.file('README.txt')!.async('string')).toContain('Merged: 1 row; columns: Category, A count, A share'); + }); +}); diff --git a/src/app/services/csv-export.service.ts b/src/app/services/csv-export.service.ts index 4caec40ed..dbed30e16 100644 --- a/src/app/services/csv-export.service.ts +++ b/src/app/services/csv-export.service.ts @@ -1,62 +1,147 @@ import { Injectable } from '@angular/core'; +import { + experimentContextRows, + ExperimentExportDetails, + filledContextRows, + fileSlug, + formatExportDate, +} from '../core/export.utils'; +import { exportTable, TableSpec } from '../pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry'; +import { + HISTOGRAM_RESIDUAL_LABEL, + HistogramGroupSeries, + residualCount, +} from '../pages/experiment-studio/visualisations/histogram/histogram-data'; -@Injectable({ - providedIn: 'root' -}) -export class CsvExportService { +/** Excel only reads UTF-8 (accented labels, "≥") when the file starts with a BOM. */ +const BOM = '\uFEFF'; - constructor() { } - - /** - * Export distribution data (bins and counts) to CSV. - */ - exportDistributionCsv(data: { bins: string[]; counts: number[] }, variableName: string): void { - if (!data.bins || !data.counts || data.bins.length !== data.counts.length) { - console.warn('Invalid data for CSV export'); - return; - } - - const headers = ['Category', 'Count']; - const rows = data.bins.map((bin, index) => [ - `"${bin.replace(/"/g, '""')}"`, // Escape quotes for CSV - data.counts[index] - ]); - - const csvContent = [ - headers.join(','), - ...rows.map(row => row.join(',')) - ].join('\n'); - - this.downloadCsv(csvContent, `${variableName.replace(/[^\w\s-]/g, '').trim()}_distribution.csv`); - } +function csvCell(value: unknown): string { + const text = value === null || value === undefined ? '' : String(value); + return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text; +} - /** - * General purpose CSV export for array of objects. - */ - exportToCsv(data: any[], headers: string[], filename: string): void { - if (!data || !data.length) return; - - const csvContent = [ - headers.join(','), - ...data.map(row => headers.map(header => { - const value = row[header] ?? ''; - return `"${String(value).replace(/"/g, '""')}"`; - }).join(',')) - ].join('\n'); - - this.downloadCsv(csvContent, filename); - } +/** RFC 4180 CSV text for a header row plus data rows. */ +export function toCsv(columns: unknown[], rows: unknown[][]): string { + return [columns, ...rows].map((row) => row.map(csvCell).join(',')).join('\r\n'); +} + +interface ExperimentZipPayload { + filename: string; + details: ExperimentExportDetails; + tables: TableSpec[]; + /** The raw engine result, kept at full precision next to the rounded tables. */ + result: unknown; +} - private downloadCsv(content: string, filename: string): void { - const blob = new Blob([content], { type: 'text/csv;charset=utf-8;' }); - const url = URL.createObjectURL(blob); - const link = document.createElement('a'); - link.setAttribute('href', url); - link.setAttribute('download', filename); - link.style.visibility = 'hidden'; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - URL.revokeObjectURL(url); +@Injectable({ providedIn: 'root' }) +export class CsvExportService { + /** Distribution (bins and counts) of one variable, with optional stacked group columns. */ + exportHistogramCsv( + data: { bins: string[]; counts: Array<number | null>; groups?: HistogramGroupSeries[] }, + variableName: string + ): void { + if (!data.bins || !data.counts || data.bins.length !== data.counts.length) { + console.warn('Invalid data for CSV export'); + return; } + const groups = data.groups ?? []; + const residuals = data.counts.map((total, index) => residualCount(total, groups, index)); + const hasResidual = groups.length > 0 && residuals.some((count) => count > 0); + const columns = [ + 'Category', + 'Count', + ...groups.map((group) => group.label), + ...(hasResidual ? [HISTOGRAM_RESIDUAL_LABEL] : []), + ]; + const rows = data.bins.map((bin, index) => [ + bin, + data.counts[index], + ...groups.map((group) => group.counts[index] ?? ''), + ...(hasResidual ? [residuals[index]] : []), + ]); + this.downloadCsv(toCsv(columns, rows), `${fileSlug(variableName, 'variable')}_distribution.csv`); + } + + /** Array of objects, one column per header. */ + exportToCsv(data: Array<Record<string, unknown>>, headers: string[], filename: string): void { + if (!data?.length) return; + this.downloadCsv(toCsv(headers, data.map((row) => headers.map((header) => row[header]))), filename); + } + + /** One result table with exact values: what the screen rounds (p-values, shares) is exported as sent. */ + exportTableCsv(table: TableSpec, fallbackName: string): void { + if (!table?.columns || !table.rows) return; + const exported = exportTable(table); + this.downloadCsv(toCsv(exported.columns, exported.rows), `${fileSlug(table.title || fallbackName, 'table').toLowerCase()}.csv`); + } + + /** + * Every result table as its own CSV, the raw result as JSON, and a README that + * says which experiment, data and setup produced them, all in one ZIP. + */ + async exportExperimentZip(payload: ExperimentZipPayload): Promise<void> { + const { default: JSZip } = await import('jszip'); + const zip = new JSZip(); + const base = fileSlug(payload.filename || payload.details.experimentName, 'experiment'); + + const tableFiles = payload.tables.map((table, index) => { + const title = table.title || `Results table ${index + 1}`; + const name = `tables/${String(index + 1).padStart(2, '0')}_${fileSlug(title, 'table').toLowerCase()}.csv`; + const exported = exportTable(table); + zip.file(name, BOM + toCsv(exported.columns, exported.rows)); + return { name, title, table: exported }; + }); + zip.file('result.json', JSON.stringify(payload.result ?? null, null, 2)); + zip.file('README.txt', this.experimentReadme(payload.details, tableFiles)); + + const blob = await zip.generateAsync({ type: 'blob', compression: 'DEFLATE', compressionOptions: { level: 6 } }); + this.download(blob, `${base}.zip`); + } + + downloadCsv(content: string, filename: string): void { + this.download(new Blob([BOM + content], { type: 'text/csv;charset=utf-8;' }), filename); + } + + private experimentReadme( + details: ExperimentExportDetails, + tableFiles: Array<{ name: string; title: string; table: { columns: string[]; rows: unknown[][] } }>, + ): string { + const setup = filledContextRows(experimentContextRows(details)).flatMap(([label, values]) => + values.length > 1 ? [`${label}:`, ...values.map((value) => ` - ${value}`)] : [`${label}: ${values[0]}`] + ); + const files = [ + ...tableFiles.map(({ name, title, table }) => + `${name}\n ${title}: ${table.rows.length} row${table.rows.length === 1 ? '' : 's'}; columns: ${table.columns.join(', ')}` + ), + 'result.json\n The full result returned by the engine, unrounded. The tables round most values for reading; p-values, shares and probabilities in them are exact.', + ]; + return [ + details.experimentName, + '='.repeat(Math.min(details.experimentName.length, 80)), + '', + 'Setup', + '-----', + ...setup, + '', + 'Files', + '-----', + ...files, + '', + `Exported ${formatExportDate(new Date())}${details.mipVersion ? ` from MIP ${details.mipVersion}` : ''}.`, + '', + ].join('\r\n'); + } + + private download(blob: Blob, filename: string): void { + const url = URL.createObjectURL(blob); + const link = document.createElement('a'); + link.href = url; + link.download = filename; + link.style.visibility = 'hidden'; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + URL.revokeObjectURL(url); + } } diff --git a/src/app/services/error.service.ts b/src/app/services/error.service.ts index 270dae48a..91e5e7ad9 100644 --- a/src/app/services/error.service.ts +++ b/src/app/services/error.service.ts @@ -1,20 +1,17 @@ -import { Injectable } from '@angular/core'; -import { BehaviorSubject } from 'rxjs'; +import { Injectable, signal } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class ErrorService { - private errorSubject = new BehaviorSubject<string | null>(null); - error$ = this.errorSubject.asObservable(); + private readonly errorSignal = signal<string | null>(null); + readonly error = this.errorSignal.asReadonly(); - // Method to set the error message setError(message: string): void { - this.errorSubject.next(message); + this.errorSignal.set(message); } - // Method to clear the error message clearError(): void { - this.errorSubject.next(null); + this.errorSignal.set(null); } } diff --git a/src/app/services/experiment-folders.service.spec.ts b/src/app/services/experiment-folders.service.spec.ts new file mode 100644 index 000000000..e6d290611 --- /dev/null +++ b/src/app/services/experiment-folders.service.spec.ts @@ -0,0 +1,418 @@ +import { provideHttpClient } from '@angular/common/http'; +import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; +import { Observable } from 'rxjs'; + +import { ExperimentFolder } from '../models/experiment-folder.model'; +import { User } from '../models/user.interface'; +import { ExperimentFoldersService } from './experiment-folders.service'; + +/** The one surface the service speaks; see `backend-experiment-folder.model.ts` for the table. */ +const apiUrl = '/services/experiment-folders'; + +const user = (username: string): User => ({ + username, + fullname: username, + email: `${username}@chuv.ch`, + subjectId: `orcid-${username}`, +}); + +const folder = (overrides: Partial<ExperimentFolder> = {}): ExperimentFolder => ({ + id: 'folder-1', + name: 'Q3 meta', + experimentIds: ['exp-1'], + sets: [], + ...overrides, +}); + +describe('ExperimentFoldersService', () => { + let service: ExperimentFoldersService; + let httpMock: HttpTestingController; + + /** Signs in and hands back the GET the service owes us, so each test flushes its own answer. */ + const signIn = (username = 'mcurie') => { + service.setActiveUser(user(username)); + return httpMock.expectOne(apiUrl); + }; + + /** Collects what a mutation answers, the way a component subscription would. */ + const record = <T>(request: Observable<T>) => { + const values: T[] = []; + const errors: unknown[] = []; + request.subscribe({ next: (value) => values.push(value), error: (err) => errors.push(err) }); + return { values, errors }; + }; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [provideHttpClient(), provideHttpClientTesting()], + }); + service = TestBed.inject(ExperimentFoldersService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + }); + + describe('reading the signed-in user', () => { + it('reads the folders of whoever the session turns out to be', () => { + service.setActiveUser(user('mcurie')); + const read = httpMock.expectOne(apiUrl); + + expect(read.request.method).toBe('GET'); + expect(service.loading()).toBe(true); + + read.flush({ + folders: [folder(), folder({ id: 'folder-2', name: 'PCA', experimentIds: ['exp-2', 'exp-3'] })], + }); + + expect(service.loading()).toBe(false); + expect(service.folders().map((candidate) => candidate.id)).toEqual(['folder-1', 'folder-2']); + expect(service.folderById('folder-2')?.name).toBe('PCA'); + expect(service.isMember('folder-2', 'exp-3')).toBe(true); + }); + + it('carries the sets across with the folder that holds them', () => { + signIn().flush({ + folders: [folder({ sets: [{ id: 'set-1', name: 'T-tests', experimentIds: ['exp-1'] }] })], + }); + + expect(service.folders()[0].sets.map((set) => set.name)).toEqual(['T-tests']); + expect(service.setOf('folder-1', 'exp-1')).toBe('set-1'); + expect(service.setOf('folder-1', 'exp-9')).toBeNull(); + }); + + it('asks again for a different user, having forgotten the rows of the one before', () => { + signIn('mcurie').flush({ folders: [folder()] }); + + service.setActiveUser(user('hermita')); + expect(service.folders()).toEqual([]); + + const second = httpMock.expectOne(apiUrl); + second.flush({ folders: [folder({ id: 'folder-9', name: 'Stroke' })] }); + + expect(service.folders().map((candidate) => candidate.id)).toEqual(['folder-9']); + }); + + it('does not read twice for the same user, however often the session is re-read', () => { + signIn('mcurie').flush({ folders: [folder()] }); + + // The session object is replaced on every refresh; the username inside it is what matters. + service.setActiveUser(user('mcurie')); + service.setActiveUser(null); + service.setActiveUser(null); + + expect(service.folders()).toEqual([]); + httpMock.expectNone(apiUrl); + }); + + it('signing out empties the mirror without going looking for anything', () => { + signIn('mcurie').flush({ folders: [folder()] }); + + service.setActiveUser(null); + + expect(service.folders()).toEqual([]); + expect(service.loading()).toBe(false); + httpMock.expectNone(apiUrl); + }); + + it('waits for a session instead of reading folders for nobody', () => { + service.setActiveUser(null); + + expect(service.folders()).toEqual([]); + httpMock.expectNone(apiUrl); + }); + + it('drops an answer that arrives after the user has already changed', () => { + service.setActiveUser(user('mcurie')); + const stale = httpMock.expectOne(apiUrl); + + service.setActiveUser(user('hermita')); + const current = httpMock.expectOne(apiUrl); + + current.flush({ folders: [folder({ id: 'b', name: 'Hers' })] }); + stale.flush({ folders: [folder({ id: 'a', name: 'Mine' })] }); + + expect(service.folders().map((candidate) => candidate.id)).toEqual(['b']); + }); + + it('says out loud when the read fails, rather than showing an empty list as if it were the truth', () => { + vi.spyOn(console, 'error').mockReturnValue(undefined); + signIn().flush('database down', { status: 500, statusText: 'Server Error' }); + + expect(service.loading()).toBe(false); + expect(service.loadError()).toContain('Could not load your folders'); + }); + + it('keeps a payload that lost a field from taking the whole read with it', () => { + signIn().flush({ + folders: [folder({ experimentIds: ['exp-1', 7, null] as unknown as string[] }), { name: 'no id' }, null], + }); + + expect(service.folders().map((candidate) => candidate.id)).toEqual(['folder-1']); + expect(service.folders()[0].experimentIds).toEqual(['exp-1']); + }); + }); + + describe('folders', () => { + beforeEach(() => { + signIn().flush({ folders: [folder()] }); + }); + + it('creates a folder with a normalised name and takes the signal from the answer', () => { + const created = record(service.createFolder(' Q3 meta two ')); + expect(created.values).toEqual([]); + + const write = httpMock.expectOne(apiUrl); + expect(write.request.method).toBe('POST'); + expect(write.request.body).toEqual({ name: 'Q3 meta two' }); + + write.flush(folder({ id: 'folder-2', name: 'Q3 meta two', experimentIds: [] })); + + expect(created.values[0]).toEqual(expect.objectContaining({ id: 'folder-2', name: 'Q3 meta two' })); + expect(service.folders().map((candidate) => candidate.id)).toEqual(['folder-1', 'folder-2']); + }); + + it('brings a run with a folder made from the row menu', () => { + service.createFolder('ANOVA', 'exp-7').subscribe(); + + const write = httpMock.expectOne(apiUrl); + expect(write.request.body).toEqual({ name: 'ANOVA', experimentUuid: 'exp-7' }); + write.flush(folder({ id: 'folder-2', name: 'ANOVA', experimentIds: ['exp-7'] })); + + expect(service.folderById('folder-2')?.experimentIds).toEqual(['exp-7']); + }); + + it('maps a name a sibling already took to null, and leaves the mirror alone', () => { + const created = record(service.createFolder('q3 meta')); + + httpMock.expectOne(apiUrl).flush('taken', { status: 409, statusText: 'Conflict' }); + + expect(created.values).toEqual([null]); + expect(service.folders().map((candidate) => candidate.id)).toEqual(['folder-1']); + }); + + it('never sends a blank name to the server', () => { + const created = record(service.createFolder(' ')); + + expect(created.values).toEqual([null]); + httpMock.expectNone(apiUrl); + }); + + it('renames through PATCH, and is true only when the server agreed', () => { + const renamed = record(service.renameFolder('folder-1', ' ANOVA one ')); + + const write = httpMock.expectOne(`${apiUrl}/folder-1`); + expect(write.request.method).toBe('PATCH'); + expect(write.request.body).toEqual({ name: 'ANOVA one' }); + + write.flush(folder({ name: 'ANOVA one' })); + + expect(renamed.values).toEqual([true]); + expect(service.folderById('folder-1')?.name).toBe('ANOVA one'); + }); + + it('a taken new name comes back false, so the inline line can say so', () => { + const renamed = record(service.renameFolder('folder-1', 'PCA')); + + httpMock.expectOne(`${apiUrl}/folder-1`).flush('taken', { status: 409, statusText: 'Conflict' }); + + expect(renamed.values).toEqual([false]); + expect(service.folderById('folder-1')?.name).toBe('Q3 meta'); + }); + + it('deletes a folder out of the mirror once the server has it', () => { + const gone = record(service.deleteFolder('folder-1')); + + const write = httpMock.expectOne(`${apiUrl}/folder-1`); + expect(write.request.method).toBe('DELETE'); + write.flush(null, { status: 204, statusText: 'No Content' }); + + expect(gone.values.length).toBe(1); + expect(service.folders()).toEqual([]); + }); + + it('propagates a failure that is not about the name, so the component can say something', () => { + const renamed = record(service.renameFolder('folder-1', 'ANOVA one')); + + httpMock.expectOne(`${apiUrl}/folder-1`).flush('database down', { status: 500, statusText: 'Server Error' }); + + expect(renamed.values).toEqual([]); + expect(renamed.errors.length).toBe(1); + expect(service.folderById('folder-1')?.name).toBe('Q3 meta'); + }); + }); + + describe('membership', () => { + beforeEach(() => { + signIn().flush({ folders: [folder({ experimentIds: ['exp-1'], sets: [{ id: 'set-1', name: 'T-tests', experimentIds: ['exp-1'] }] })] }); + }); + + it('adds a run to the folder it was dropped on', () => { + const added = record(service.addExperiment('folder-1', 'exp-2')); + + const write = httpMock.expectOne(`${apiUrl}/folder-1/members`); + expect(write.request.method).toBe('POST'); + expect(write.request.body).toEqual({ experimentUuid: 'exp-2' }); + + write.flush(folder({ experimentIds: ['exp-1', 'exp-2'] })); + + expect(added.values[0]).toEqual(expect.objectContaining({ id: 'folder-1' })); + expect(service.folderById('folder-1')?.experimentIds).toEqual(['exp-1', 'exp-2']); + }); + + it('removes a run, and the server decides what its set loses', () => { + service.removeExperiment('folder-1', 'exp-1').subscribe(); + + const write = httpMock.expectOne(`${apiUrl}/folder-1/members/exp-1`); + expect(write.request.method).toBe('DELETE'); + write.flush(folder({ experimentIds: [], sets: [{ id: 'set-1', name: 'T-tests', experimentIds: [] }] })); + + expect(service.folderById('folder-1')?.experimentIds).toEqual([]); + expect(service.folderById('folder-1')?.sets[0].experimentIds).toEqual([]); + }); + + it('a row-menu tick goes whichever way the mirror says the run stands', () => { + service.toggleExperiment('folder-1', 'exp-1').subscribe(); + httpMock.expectOne(`${apiUrl}/folder-1/members/exp-1`).flush(folder({ experimentIds: [] })); + + service.toggleExperiment('folder-1', 'exp-2').subscribe(); + const added = httpMock.expectOne(`${apiUrl}/folder-1/members`); + expect(added.request.method).toBe('POST'); + added.flush(folder({ experimentIds: ['exp-2'] })); + + expect(service.isMember('folder-1', 'exp-1')).toBe(false); + expect(service.isMember('folder-1', 'exp-2')).toBe(true); + }); + + it('a drop the server refused is an error the canvas hears, not a silent no-op', () => { + const added = record(service.addExperiment('folder-1', 'exp-2')); + + httpMock.expectOne(`${apiUrl}/folder-1/members`).flush('down', { status: 503, statusText: 'Unavailable' }); + + expect(added.values).toEqual([]); + expect(added.errors.length).toBe(1); + expect(service.folderById('folder-1')?.experimentIds).toEqual(['exp-1']); + }); + }); + + describe('sets', () => { + beforeEach(() => { + signIn().flush({ folders: [folder({ experimentIds: ['exp-1', 'exp-2'], sets: [{ id: 'set-1', name: 'T-tests', experimentIds: ['exp-1'] }] })] }); + }); + + it('creates a set with the run that asked for it', () => { + const created = record(service.createSet('folder-1', ' Chi-squared ', 'exp-2')); + + const write = httpMock.expectOne(`${apiUrl}/folder-1/sets`); + expect(write.request.method).toBe('POST'); + expect(write.request.body).toEqual({ name: 'Chi-squared', experimentUuid: 'exp-2' }); + + write.flush( + folder({ + experimentIds: ['exp-1', 'exp-2'], + sets: [ + { id: 'set-1', name: 'T-tests', experimentIds: ['exp-1'] }, + { id: 'set-2', name: 'Chi-squared', experimentIds: ['exp-2'] }, + ], + }), + ); + + expect(created.values[0]?.sets.map((set) => set.name)).toEqual(['T-tests', 'Chi-squared']); + expect(service.setOf('folder-1', 'exp-2')).toBe('set-2'); + }); + + it('a set name a sibling took comes back null, with the partition untouched', () => { + const created = record(service.createSet('folder-1', 't-TESTS', 'exp-2')); + + httpMock.expectOne(`${apiUrl}/folder-1/sets`).flush('taken', { status: 409, statusText: 'Conflict' }); + + expect(created.values).toEqual([null]); + expect(service.folderById('folder-1')?.sets.length).toBe(1); + }); + + it('never sends a blank set name either', () => { + const created = record(service.createSet('folder-1', ' ', 'exp-2')); + + expect(created.values).toEqual([null]); + httpMock.expectNone(apiUrl); + }); + + it('renames a set through PATCH on the set', () => { + const renamed = record(service.renameSet('folder-1', 'set-1', ' Age tests ')); + + const write = httpMock.expectOne(`${apiUrl}/folder-1/sets/set-1`); + expect(write.request.method).toBe('PATCH'); + expect(write.request.body).toEqual({ name: 'Age tests' }); + + write.flush(folder({ sets: [{ id: 'set-1', name: 'Age tests', experimentIds: ['exp-1'] }] })); + + expect(renamed.values).toEqual([true]); + expect(service.folderById('folder-1')?.sets[0].name).toBe('Age tests'); + }); + + it('a sibling name on a rename is false', () => { + const renamed = record(service.renameSet('folder-1', 'set-1', 'Chi-squared')); + + httpMock.expectOne(`${apiUrl}/folder-1/sets/set-1`).flush('taken', { status: 409, statusText: 'Conflict' }); + + expect(renamed.values).toEqual([false]); + }); + + it('deleting a set returns the folder with its runs back ungrouped', () => { + service.deleteSet('folder-1', 'set-1').subscribe(); + + const write = httpMock.expectOne(`${apiUrl}/folder-1/sets/set-1`); + expect(write.request.method).toBe('DELETE'); + write.flush(folder({ experimentIds: ['exp-1', 'exp-2'], sets: [] })); + + expect(service.folderById('folder-1')?.sets).toEqual([]); + expect(service.folderById('folder-1')?.experimentIds).toEqual(['exp-1', 'exp-2']); + }); + + it('moves a run into a set by writing its set id', () => { + const moved = record(service.moveToSet('folder-1', 'exp-2', 'set-1')); + + const write = httpMock.expectOne(`${apiUrl}/folder-1/members/exp-2/set`); + expect(write.request.method).toBe('PATCH'); + expect(write.request.body).toEqual({ setId: 'set-1' }); + + write.flush(folder({ experimentIds: ['exp-1', 'exp-2'], sets: [{ id: 'set-1', name: 'T-tests', experimentIds: ['exp-1', 'exp-2'] }] })); + + expect(moved.values[0]?.sets[0].experimentIds).toEqual(['exp-1', 'exp-2']); + expect(service.setOf('folder-1', 'exp-2')).toBe('set-1'); + }); + + it('leaving a set is the same write with the set id cleared', () => { + service.removeFromSets('folder-1', 'exp-1').subscribe(); + + const write = httpMock.expectOne(`${apiUrl}/folder-1/members/exp-1/set`); + expect(write.request.method).toBe('PATCH'); + expect(write.request.body).toEqual({ setId: null }); + + write.flush(folder({ experimentIds: ['exp-1', 'exp-2'], sets: [{ id: 'set-1', name: 'T-tests', experimentIds: [] }] })); + + expect(service.setOf('folder-1', 'exp-1')).toBeNull(); + expect(service.isMember('folder-1', 'exp-1')).toBe(true); + }); + }); + + describe('pruning a run the experiment API has already lost', () => { + it('clears the mirror without asking the folder API to do it', () => { + signIn().flush({ + folders: [ + folder({ experimentIds: ['exp-1', 'exp-2'], sets: [{ id: 'set-1', name: 'T-tests', experimentIds: ['exp-1'] }] }), + folder({ id: 'folder-2', name: 'PCA', experimentIds: ['exp-1'] }), + ], + }); + + service.pruneExperiment('exp-1'); + + expect(service.folderById('folder-1')?.experimentIds).toEqual(['exp-2']); + expect(service.folderById('folder-1')?.sets[0].experimentIds).toEqual([]); + expect(service.folderById('folder-2')?.experimentIds).toEqual([]); + httpMock.expectNone(apiUrl); + }); + }); +}); diff --git a/src/app/services/experiment-folders.service.ts b/src/app/services/experiment-folders.service.ts new file mode 100644 index 000000000..e1deed351 --- /dev/null +++ b/src/app/services/experiment-folders.service.ts @@ -0,0 +1,330 @@ +import { HttpClient } from '@angular/common/http'; +import { Injectable, inject, signal } from '@angular/core'; +import { Observable, catchError, map, of, throwError } from 'rxjs'; + +import { ExperimentFolder, ExperimentSet } from '../models/experiment-folder.model'; +import { BackendExperimentFoldersResponse } from '../models/backend-experiment-folder.model'; +import { User } from '../models/user.interface'; + +/** The one backend surface for folders and the sets inside them; see `backend-experiment-folder.model.ts`. */ +const FOLDERS_URL = '/services/experiment-folders'; +const MAX_NAME_LENGTH = 60; + +/** Every mutation answers with the folder itself (ExperimentFolderAPI returns a bare DTO). */ + +/** + * Experiment folders ("analysis sets"): a named group of runs the user wants side by side. + * + * PostgreSQL is the store and `/services/experiment-folders` is the only way in or out. The signal + * below is a mirror of the server's rows, not a second source of truth: every mutation sends its + * request, and only the folder the server returns is written back into the signal. Nothing is kept + * in the browser, so a folder made here is a folder the next tab, device, or reload finds. + * + * Ownership is not a query parameter — the session picks the rows — so the username here is only a + * cache key: it tells the service when the mirror has to be thrown away and read again. + * + * Folders hold member ids only, never experiment copies, so the only drift risk is an id that no + * longer resolves. Membership is pruned after a confirmed delete or a real 404, never against the + * visible page: the dashboard list is server-paginated, so a page is never the full truth and + * pruning against it would silently empty folders whose members sit on another page. + * + * Inside a folder, sets are named subsets of the members — the grouping the compare workspace + * renders as sections. They partition the members: a run sits in at most one set, and anything + * in no set is Ungrouped, which the compare derives per algorithm instead. + */ +@Injectable({ providedIn: 'root' }) +export class ExperimentFoldersService { + readonly folders = signal<ExperimentFolder[]>([]); + /** True while the folder rows for the active user are on their way in. */ + readonly loading = signal(false); + /** Why the mirror above is not the truth; the folder strip says so instead of showing nothing. */ + readonly loadError = signal<string | null>(null); + + private readonly http = inject(HttpClient); + + /** Whose rows `folders` mirrors; null means nobody is signed in and there is nothing to mirror. */ + private activeUsername: string | null = null; + /** Rises with every load, so a response that arrives after a newer one is dropped, not applied. */ + private loadToken = 0; + + /** + * Points the mirror at the signed-in user. Called from the dashboard once the session resolves. + * + * A signed-out user gets an empty mirror and no request; a new username clears the mirror and + * reads the server again; the same username coming back through change detection does neither. + */ + setActiveUser(user: User | null): void { + const username = user?.username?.trim() || null; + if (username === this.activeUsername) return; + + this.activeUsername = username; + const token = ++this.loadToken; + this.folders.set([]); + this.loadError.set(null); + if (!username) { + this.loading.set(false); + return; + } + + this.loading.set(true); + this.http.get<BackendExperimentFoldersResponse>(FOLDERS_URL).subscribe({ + next: (response) => { + if (token !== this.loadToken) return; + this.loading.set(false); + this.folders.set(this.foldersFrom(response)); + }, + error: (err) => { + if (token !== this.loadToken) return; + console.error('[ExperimentFoldersService] Could not load folders', err); + this.loading.set(false); + this.loadError.set('Could not load your folders. Check the connection and reload the page.'); + }, + }); + } + + /** Blank names are rejected here; a name another folder of this user's already took comes back as 409, which becomes null. */ + createFolder(name: string, experimentId?: string): Observable<ExperimentFolder | null> { + const cleanName = this.sanitizeName(name); + if (!cleanName) return of(null); + + return this.post(FOLDERS_URL, { + name: cleanName, + ...(experimentId ? { experimentUuid: experimentId } : {}), + }).pipe( + catchError((error) => this.nameTaken<ExperimentFolder | null>(error, null)), + ); + } + + /** True on success, false when the name is taken or blank; anything else the server said is rethrown. */ + renameFolder(folderId: string, name: string): Observable<boolean> { + const cleanName = this.sanitizeName(name); + if (!cleanName) return of(false); + if (!folderId) return this.invalid('renameFolder needs a folder id'); + + return this.http + .patch<ExperimentFolder>(this.folderUrl(folderId), { name: cleanName }) + .pipe( + map((response) => this.confirmRename(response)), + catchError((error) => this.nameTaken<boolean>(error, false)), + ); + } + + /** The server answers with no body, so the mirror is what drops the folder. */ + deleteFolder(folderId: string): Observable<void> { + if (!folderId) return this.invalid('deleteFolder needs a folder id'); + + return this.http.delete<void>(this.folderUrl(folderId)).pipe( + map(() => { + this.folders.update((current) => current.filter((folder) => folder.id !== folderId)); + }), + ); + } + + /** The row menu ticks a folder on or off, so one entry point decides which way this call goes. */ + toggleExperiment(folderId: string, experimentId: string): Observable<ExperimentFolder> { + return this.isMember(folderId, experimentId) + ? this.removeExperiment(folderId, experimentId) + : this.addExperiment(folderId, experimentId); + } + + addExperiment(folderId: string, experimentId: string): Observable<ExperimentFolder> { + if (!folderId || !experimentId) return this.invalid('addExperiment needs a folder and a run'); + + return this.post(this.folderUrl(folderId, 'members'), { experimentUuid: experimentId }); + } + + /** Leaving the folder ends the membership outright: a set cannot keep a run the folder lost. */ + removeExperiment(folderId: string, experimentId: string): Observable<ExperimentFolder> { + if (!folderId || !experimentId) return this.invalid('removeExperiment needs a folder and a run'); + + return this.http + .delete<ExperimentFolder>(this.folderUrl(folderId, 'members', experimentId)) + .pipe(map((response) => this.applyFolder(response))); + } + + /** + * Prune a deleted or 404'd experiment out of the mirror, sets included. Local on purpose: the + * caller has already been told by the experiment API that the run is gone, and + * `ON DELETE CASCADE` dropped the membership rows in the same transaction, so there is nothing + * left to ask the server. It is never a write — pruning on an unconfirmed delete would let a + * failed request cost the user a folder. + */ + pruneExperiment(experimentId: string): void { + if (!experimentId) return; + if (!this.folders().some((folder) => this.folderHolds(folder, experimentId))) return; + + this.folders.update((current) => + current.map((folder) => ({ + ...folder, + experimentIds: folder.experimentIds.filter((id) => id !== experimentId), + sets: this.dropFromSets(folder.sets, experimentId), + })), + ); + } + + folderById(folderId: string | null): ExperimentFolder | null { + if (!folderId) return null; + return this.folders().find((folder) => folder.id === folderId) ?? null; + } + + isMember(folderId: string, experimentId: string): boolean { + return this.folders().find((folder) => folder.id === folderId)?.experimentIds.includes(experimentId) ?? false; + } + + // ---- sets: named subsets of a folder, one level deep by design ---- + + /** Same name rules as folders, in a namespace of their own: a set may share its folder's name. */ + createSet(folderId: string, name: string, experimentId?: string): Observable<ExperimentFolder | null> { + const cleanName = this.sanitizeName(name); + if (!folderId || !cleanName) return of(null); + + return this.post(this.folderUrl(folderId, 'sets'), { + name: cleanName, + ...(experimentId ? { experimentUuid: experimentId } : {}), + }).pipe( + catchError((error) => this.nameTaken<ExperimentFolder | null>(error, null)), + ); + } + + renameSet(folderId: string, setId: string, name: string): Observable<boolean> { + const cleanName = this.sanitizeName(name); + if (!cleanName) return of(false); + if (!folderId || !setId) return this.invalid('renameSet needs a folder and a set'); + + return this.http + .patch<ExperimentFolder>(this.folderUrl(folderId, 'sets', setId), { name: cleanName }) + .pipe( + map((response) => this.confirmRename(response)), + catchError((error) => this.nameTaken<boolean>(error, false)), + ); + } + + /** Deleting a set retires the name, never the runs: its members fall back to Ungrouped. */ + deleteSet(folderId: string, setId: string): Observable<ExperimentFolder> { + if (!folderId || !setId) return this.invalid('deleteSet needs a folder and a set'); + + return this.http + .delete<ExperimentFolder>(this.folderUrl(folderId, 'sets', setId)) + .pipe(map((response) => this.applyFolder(response))); + } + + /** The strict partition: the server repoints the membership row, so the set that had it gives it up. */ + moveToSet(folderId: string, experimentId: string, setId: string): Observable<ExperimentFolder> { + if (!folderId || !experimentId || !setId) return this.invalid('moveToSet needs a folder, a run and a set'); + + return this.assignSet(folderId, experimentId, setId); + } + + /** The way out of a set that is not "leave the folder": back to the Ungrouped group. */ + removeFromSets(folderId: string, experimentId: string): Observable<ExperimentFolder> { + if (!folderId || !experimentId) return this.invalid('removeFromSets needs a folder and a run'); + + return this.assignSet(folderId, experimentId, null); + } + + /** Which set holds this run; null when it sits in the folder ungrouped. */ + setOf(folderId: string, experimentId: string): string | null { + return this.folderById(folderId)?.sets.find((set) => set.experimentIds.includes(experimentId))?.id ?? null; + } + + /** Joining a set and leaving one are the same write: the member row's set id, set or cleared. */ + private assignSet(folderId: string, experimentId: string, setId: string | null): Observable<ExperimentFolder> { + return this.http + .patch<ExperimentFolder>(this.folderUrl(folderId, 'members', experimentId, 'set'), { setId }) + .pipe(map((response) => this.applyFolder(response))); + } + + /** A rename answers with the renamed folder — the mirror takes it, and the caller gets a true. */ + private confirmRename(payload: ExperimentFolder | null | undefined): boolean { + this.applyFolder(payload); + return true; + } + + private post(url: string, body: unknown): Observable<ExperimentFolder> { + return this.http.post<ExperimentFolder>(url, body).pipe(map((response) => this.applyFolder(response))); + } + + /** + * The server's copy wins, so the mirror is replaced rather than edited. A folder the mirror has + * never seen — one created in this tab a moment ago — takes its place at the end of the list, + * which is also where the server's own ordering puts it. + */ + private applyFolder(payload: ExperimentFolder | null | undefined): ExperimentFolder { + const folder = this.folderFrom(payload); + if (!folder) { + throw new Error('[ExperimentFoldersService] The server answered a folder write without a folder'); + } + + this.folders.update((current) => + current.some((candidate) => candidate.id === folder.id) + ? current.map((candidate) => (candidate.id === folder.id ? folder : candidate)) + : [...current, folder], + ); + return folder; + } + + private folderUrl(folderId: string, ...rest: string[]): string { + return [FOLDERS_URL, folderId, ...rest].join('/'); + } + + /** A 409 is the server saying "a sibling already has that name", which the inline message speaks. */ + private nameTaken<T>(error: unknown, taken: T): Observable<T> { + if ((error as { status?: number } | null)?.status === 409) return of(taken); + return throwError(() => error); + } + + /** A call the UI should never have made: failing loudly beats the silent no-op it used to be. */ + private invalid(message: string): Observable<never> { + const error = new Error(`[ExperimentFoldersService] ${message}`); + console.error(error.message); + return throwError(() => error); + } + + private sanitizeName(name: string): string { + return name.replace(/\s+/g, ' ').trim().slice(0, MAX_NAME_LENGTH); + } + + private folderHolds(folder: ExperimentFolder, experimentId: string): boolean { + return folder.experimentIds.includes(experimentId) || folder.sets.some((set) => set.experimentIds.includes(experimentId)); + } + + private dropFromSets(sets: ExperimentSet[], experimentId: string): ExperimentSet[] { + return sets.map((set) => ({ ...set, experimentIds: set.experimentIds.filter((id) => id !== experimentId) })); + } + + /** + * The mirror is written from what the server sent, so a payload that lost a field degrades to + * "no members" or "no sets" instead of throwing inside a response handler. A row with neither an + * id nor a name is not a folder and is dropped. + */ + private foldersFrom(response: BackendExperimentFoldersResponse | null): ExperimentFolder[] { + const value = response?.folders; + if (!Array.isArray(value)) return []; + + return value.map((folder) => this.folderFrom(folder)).filter((folder): folder is ExperimentFolder => !!folder); + } + + private folderFrom(value: unknown): ExperimentFolder | null { + const candidate = value as Partial<ExperimentFolder> | null; + if (!candidate || typeof candidate.id !== 'string' || typeof candidate.name !== 'string') return null; + + return { + id: candidate.id, + name: candidate.name, + experimentIds: this.stringsFrom(candidate.experimentIds), + sets: this.setsFrom(candidate.sets), + }; + } + + private setsFrom(value: unknown): ExperimentSet[] { + if (!Array.isArray(value)) return []; + + return value + .filter((set): set is ExperimentSet => !!set && typeof set.id === 'string' && typeof set.name === 'string') + .map((set) => ({ id: set.id, name: set.name, experimentIds: this.stringsFrom(set.experimentIds) })); + } + + private stringsFrom(value: unknown): string[] { + return Array.isArray(value) ? value.filter((id): id is string => typeof id === 'string') : []; + } +} diff --git a/src/app/services/experiment-label.service.ts b/src/app/services/experiment-label.service.ts index e55f8bf34..a9916b7ba 100644 --- a/src/app/services/experiment-label.service.ts +++ b/src/app/services/experiment-label.service.ts @@ -3,6 +3,7 @@ import { ExperimentStudioService } from './experiment-studio.service'; import { DataModel } from '../models/data-model.interface'; import { firstValueFrom } from 'rxjs'; import { EnumMaps } from '../core/algorithm-result-enum-mapper'; +import { buildEnumMapForVariables, findDataModelByCodeVersion } from '../core/data-model.utils'; @Injectable({ providedIn: 'root' }) export class ExperimentLabelService { @@ -13,119 +14,65 @@ export class ExperimentLabelService { private expStudio = inject(ExperimentStudioService); - constructor() { } - - private findDataModelByCodeVersion(codeVersion: string, models: DataModel[]): DataModel | null { - if (!codeVersion) return null; - const [code, version] = codeVersion.split(':'); - return models.find(m => m.code === code && String(m.version) === String(version)) ?? null; - } - async getLabelMap(domain: string | null | undefined): Promise<Record<string, string>> { - if (!domain) return {}; - - const cached = this.cache.get(domain); - if (cached) return cached; - - const inflight = this.inflight.get(domain); - if (inflight) return inflight; - - const p = (async () => { - try { - const models = await firstValueFrom(this.expStudio.loadAllDataModels()) as DataModel[]; - const model = this.findDataModelByCodeVersion(domain, models); - - if (!model) return {}; - - const converted = this.expStudio.convertToD3Hierarchy(model); - const map: Record<string, string> = { - [domain]: model.label || domain - }; - - converted.allVariables.forEach((v: any) => { - if (v?.code) { - map[v.code] = v.label || v.code; - - // Also add enumerations to the flat map (especially useful for datasets) - if (v.code.toLowerCase() === 'dataset' && Array.isArray(v.enumerations)) { - v.enumerations.forEach((e: any) => { - const eCode = e?.code ?? e?.label ?? e?.name; - if (eCode) map[eCode] = e.label || e.name || eCode; - }); - } + return this.cached(domain, this.cache, this.inflight, {}, (model, converted) => { + const map: Record<string, string> = { [domain!]: model.label || domain! }; + for (const v of converted.allVariables) { + if (!v?.code) continue; + map[v.code] = v.label || v.code; + if (v.code.toLowerCase() === 'dataset' && Array.isArray(v.enumerations)) { + for (const e of v.enumerations) { + const eCode = e?.code ?? e?.label ?? e?.name; + if (eCode) map[eCode] = e.label || e.name || eCode; } - }); - - return map; - } catch (err) { - console.error('[ExperimentLabelService] failed to load data models', err); - return {}; - } finally { - this.inflight.delete(domain); + } } - })(); - - this.inflight.set(domain, p); - - const result = await p; - this.cache.set(domain, result); - return result; + return map; + }, 'data models'); } async getEnumMaps(domain: string | null | undefined): Promise<EnumMaps> { - if (!domain) return {}; - - const cached = this.enumCache.get(domain); - if (cached) return cached; + return this.cached( + domain, + this.enumCache, + this.enumInflight, + {}, + (_model, converted) => buildEnumMapForVariables(converted.allVariables), + 'enum maps', + ); + } - const inflight = this.enumInflight.get(domain); - if (inflight) return inflight; + private async cached<T>( + domain: string | null | undefined, + cache: Map<string, T>, + inflight: Map<string, Promise<T>>, + empty: T, + build: (model: DataModel, converted: ReturnType<ExperimentStudioService['convertToD3Hierarchy']>) => T, + errorLabel: string, + ): Promise<T> { + if (!domain) return empty; + const hit = cache.get(domain); + if (hit) return hit; + const pending = inflight.get(domain); + if (pending) return pending; const p = (async () => { try { const models = await firstValueFrom(this.expStudio.loadAllDataModels()) as DataModel[]; - const model = this.findDataModelByCodeVersion(domain, models); - - if (!model) return {}; - - const converted = this.expStudio.convertToD3Hierarchy(model); - const maps: EnumMaps = {}; - - converted.allVariables.forEach((v: any) => { - const enums = Array.isArray(v?.enumerations) ? v.enumerations : []; - if (!enums.length) return; - - const code = String(v?.code ?? ''); - if (!code) return; - - const enumMap: Record<string, string> = {}; - enums.forEach((e: any) => { - const raw = e?.code ?? e?.label ?? e?.name; - if (raw === null || raw === undefined) return; - const key = String(raw); - const label = e?.label ?? e?.name ?? String(raw); - enumMap[key] = label; - }); - - if (Object.keys(enumMap).length > 0) { - maps[code] = enumMap; - } - }); - - return maps; + const model = findDataModelByCodeVersion(domain, models); + if (!model) return empty; + return build(model, this.expStudio.convertToD3Hierarchy(model)); } catch (err) { - console.error('[ExperimentLabelService] failed to load enum maps', err); - return {}; + console.error('[ExperimentLabelService] failed to load', errorLabel, err); + return empty; } finally { - this.enumInflight.delete(domain); + inflight.delete(domain); } })(); - this.enumInflight.set(domain, p); - + inflight.set(domain, p); const result = await p; - this.enumCache.set(domain, result); + cache.set(domain, result); return result; } - } diff --git a/src/app/services/experiment-studio-navigation.service.ts b/src/app/services/experiment-studio-navigation.service.ts new file mode 100644 index 000000000..5e45c6418 --- /dev/null +++ b/src/app/services/experiment-studio-navigation.service.ts @@ -0,0 +1,82 @@ +import { Injectable, signal } from '@angular/core'; + +export type ExperimentStudioSection = + | 'variables-top' + | 'statistics-section' + | 'algorithm-section' + | 'execution-section'; +export type DescriptiveStep = 'raw' | 'setup' | 'filters' | 'processed' | 'transformation'; + +export type StepState = 'active' | 'available' | 'complete' | 'locked'; + +interface StudioStepperState { + activeSection: ExperimentStudioSection; + railStatus: Record<ExperimentStudioSection, StepState>; + lockedStepReason: string; + /** Per-step override of `lockedStepReason` for steps that unlock on something else. */ + lockedStepReasons: Partial<Record<ExperimentStudioSection, string>>; + isRunning: boolean; +} + +export interface ExperimentStudioNavigationHost { + navigateToSection(sectionId: ExperimentStudioSection, anchorId?: string): void; + navigateToDescriptiveStep(step: DescriptiveStep): void; + runExperiment(): void; + backToDashboard(): void; +} + +const DEFAULT_STATE: StudioStepperState = { + activeSection: 'variables-top', + railStatus: { + 'variables-top': 'active', + 'statistics-section': 'locked', + 'algorithm-section': 'locked', + 'execution-section': 'locked', + }, + lockedStepReason: 'Add a dataset to unlock', + lockedStepReasons: {}, + isRunning: false, +}; + +@Injectable({ providedIn: 'root' }) +export class ExperimentStudioNavigationService { + private host: ExperimentStudioNavigationHost | null = null; + + readonly state = signal<StudioStepperState>(DEFAULT_STATE); + + register(host: ExperimentStudioNavigationHost): void { + this.host = host; + } + + unregister(host: ExperimentStudioNavigationHost): void { + if (this.host === host) { + this.host = null; + this.state.set(DEFAULT_STATE); + } + } + + publishState(partial: Partial<StudioStepperState>): void { + this.state.update((current) => ({ ...current, ...partial })); + } + + navigateToSection(sectionId: ExperimentStudioSection, anchorId?: string): void { + this.host?.navigateToSection(sectionId, anchorId); + } + + navigateToDescriptiveStep(step: DescriptiveStep): void { + this.host?.navigateToDescriptiveStep(step); + } + + runExperiment(): void { + this.host?.runExperiment(); + } + + backToDashboard(): void { + this.host?.backToDashboard(); + } + + goToPreprocessing(): void { + this.host?.navigateToSection('statistics-section'); + this.host?.navigateToDescriptiveStep('setup'); + } +} diff --git a/src/app/services/experiment-studio.service.spec.ts b/src/app/services/experiment-studio.service.spec.ts index 2c43bf84b..88c02f4d2 100644 --- a/src/app/services/experiment-studio.service.spec.ts +++ b/src/app/services/experiment-studio.service.spec.ts @@ -1,9 +1,17 @@ +import { provideZonelessChangeDetection } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { ExperimentStudioService } from './experiment-studio.service'; import { SessionStorageService } from './session-storage.service'; import { ErrorService } from './error.service'; import { DataModel } from '../models/data-model.interface'; +import { AnalysisPreprocessingStep } from '../models/backend-algorithms.model'; +import { CreatorName } from '../core/transformation-steps.utils'; + +/** Store creator cards the way the Transformation stage does: one step per card, ids by position. */ +function setCreators(service: ExperimentStudioService, name: CreatorName, creators: Record<string, unknown>[]): void { + creators.forEach((parameters, index) => service.upsertTransformation({ id: `${name}-${index}`, name, parameters })); +} describe('ExperimentStudioService', () => { let service: ExperimentStudioService; @@ -17,77 +25,1081 @@ describe('ExperimentStudioService', () => { inputdata: { data_model: { label: '', desc: '', types: [] }, datasets: { label: '', desc: '', types: [] }, - y: { label: '', desc: '', types: ['real'] }, + y: { label: '', desc: '', types: ['real'], required: true, max_count: 1 }, x: { label: '', desc: '', types: ['real'] }, - filter: { label: '', desc: '', types: [] } + filter: { label: '', desc: '', types: [], required: false, max_count: 1 } }, - parameters: {} + parameters: { + alpha: { + label: 'Alpha', + desc: '', + types: ['real'], + required: false, + default_value: '0.05', + }, + } }; const mockHistogramAlgo = { name: 'histogram', - label: 'Histograms', + label: 'Histogram', desc: '', enabled: true, inputdata: { data_model: { label: '', desc: '', types: [] }, datasets: { label: '', desc: '', types: [] }, - y: { label: '', desc: '', types: ['text'], required: true, multiple: true }, + y: { label: '', desc: '', types: ['text'], required: true }, x: { label: '', desc: '', types: ['text'] }, filter: { label: '', desc: '', types: [] } }, parameters: {} }; + const mockDescribeAlgo = { + name: 'describe', + label: 'Describe', + desc: '', + enabled: true, + inputdata: { + data_model: { label: '', desc: '', types: [] }, + datasets: { label: '', desc: '', types: [] }, + y: { label: '', desc: '', types: ['real'], required: true }, + x: { label: '', desc: '', types: ['real'] }, + filter: { label: '', desc: '', types: [] } + }, + parameters: {}, + preprocessing: [ + { + name: 'missing_values_handler', + label: 'Missing Values Handler', + desc: '', + order: 1, + parameters: {} + } + ] + }; + + const mockOutlierReportAlgo = { + name: 'outlier_report', + label: 'Outlier Report', + desc: '', + enabled: true, + inputdata: { + data_model: { label: '', desc: '', types: [] }, + datasets: { label: '', desc: '', types: [] }, + y: { label: '', desc: '', types: ['real'], required: true }, + x: { label: '', desc: '', types: ['real'], required: false }, + filter: { label: '', desc: '', types: [], required: false, max_count: 1 }, + }, + parameters: {}, + }; + + const mockLinearSvmAlgo = { + name: 'linear_svm', + label: 'Linear SVM', + desc: '', + enabled: true, + inputdata: { + data_model: { label: '', desc: '', types: [] }, + datasets: { label: '', desc: '', types: [] }, + y: { label: '', desc: '', types: ['real'], required: true }, + x: { label: '', desc: '', types: ['real'], required: true }, + filter: { label: '', desc: '', types: [], required: false, max_count: 1 }, + }, + parameters: {}, + }; + + const mockKmeansAlgo = { + name: 'kmeans', + label: 'K-means', + desc: '', + enabled: true, + inputdata: { + data_model: { label: '', desc: '', types: [] }, + datasets: { label: '', desc: '', types: [] }, + y: { label: 'Variables', desc: '', types: ['real', 'int'], required: true }, + filter: { label: '', desc: '', types: [] }, + }, + parameters: { + k_selection: { + label: 'K selection', + types: ['text'], + required: true, + default: 'manual', + enums: { type: 'list', source: ['manual', 'elbow'] }, + }, + k: { label: 'Number of clusters', types: ['int'], required: false, default: 4 }, + maxiter: { label: 'Maximum iterations', types: ['int'], required: true, default: 100 }, + tol: { label: 'Convergence tolerance', types: ['real'], required: true, default: 0.0001 }, + }, + }; + + const mockPcaAlgo = { + name: 'pca', + label: 'Principal Component Analysis', + desc: '', + enabled: true, + inputdata: { + data_model: { label: '', desc: '', types: [] }, + datasets: { label: '', desc: '', types: [] }, + y: { label: 'Variables', desc: '', types: ['real', 'int'], required: true }, + filter: { label: '', desc: '', types: [] }, + }, + parameters: {}, + }; + + const mockInputdataSpec = { + data_model: { label: '', desc: '', types: ['text'], required: true }, + datasets: { label: '', desc: '', types: ['text'], required: true }, + filters: { label: '', desc: '', types: ['jsonObject'], required: false }, + variables: { label: '', desc: '', types: ['real', 'int', 'text'], required: true, min_count: 1 }, + }; + + function toAlgorithmSpec(mock: any) { + return { + name: mock.name, + label: mock.label, + desc: mock.desc, + type: mock.type, + flags: mock.flags, + y: mock.inputdata.y, + x: mock.inputdata.x, + requires_validation_datasets: false, + parameters: mock.parameters ?? {}, + required_preprocessing: [], + }; + } + + function preprocessingSteps(config: Record<string, unknown> | null): AnalysisPreprocessingStep[] | null { + if (!config) return null; + return Object.entries(config).map(([name, parameters]) => ({ + name, + parameters: parameters as Record<string, unknown>, + })); + } + + function flushSpecifications(algorithms: any[], preprocessing: any[] = []) { + httpMock.expectOne('/services/specifications/inputdata').flush(mockInputdataSpec); + httpMock.expectOne('/services/specifications/preprocessing').flush(preprocessing); + httpMock.expectOne('/services/specifications/algorithms').flush(algorithms); + } + const mockDataModel: DataModel = { uuid: 'dm-uuid', code: 'dm', version: '1', label: 'Data Model', - variables: [], + variables: [ + { code: 'age', label: 'Age', type: 'continuous' } as any, + { code: 'sex', label: 'Sex', type: 'nominal', enumerations: [{ code: 'M', label: 'Male' }, { code: 'F', label: 'Female' }] } as any, + { code: 'site', label: 'Site', type: 'nominal', enumerations: [{ code: 'A', label: 'A' }] } as any, + ], groups: [], datasets: ['ds1'], released: true, }; beforeEach(() => { + sessionStorage.clear(); TestBed.configureTestingModule({ imports: [HttpClientTestingModule], - providers: [SessionStorageService, ErrorService] + providers: [provideZonelessChangeDetection(), SessionStorageService, ErrorService] }); service = TestBed.inject(ExperimentStudioService); httpMock = TestBed.inject(HttpTestingController); - const req = httpMock.expectOne('/services/algorithms'); - req.flush([mockRawAlgo, mockHistogramAlgo]); + flushSpecifications( + [ + toAlgorithmSpec(mockRawAlgo), + toAlgorithmSpec(mockHistogramAlgo), + toAlgorithmSpec(mockDescribeAlgo), + toAlgorithmSpec(mockOutlierReportAlgo), + toAlgorithmSpec(mockLinearSvmAlgo), + toAlgorithmSpec(mockKmeansAlgo), + toAlgorithmSpec(mockPcaAlgo), + ], + [ + { + name: 'categorical_column_creator', + label: 'Categorical Column Creator', + desc: '', + parameters: {}, + outputs: { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'text', + stattype: 'nominal', + column_count_param: null, + enumerations: { + source_parameter_name: 'rules', + source_type: 'dict_keys', + additional_value_parameter_name: 'default_enumeration', + prefix_parameter_name: null, + }, + }, + ], + }, + }, + { + name: 'kmeans_cluster_creator', + label: 'K-means Cluster Creator', + desc: '', + parameters: {}, + outputs: { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'int', + stattype: 'nominal', + column_count_param: null, + enumerations: { + source_parameter_name: 'k', + source_type: 'prefixed_range', + additional_value_parameter_name: null, + prefix_parameter_name: 'enumeration_prefix', + }, + }, + ], + }, + }, + { + name: 'pca_column_creator', + label: 'PCA Column Creator', + desc: '', + parameters: {}, + outputs: { + new_column_names_param: 'new_column_names', + columns: [ + { + type: 'real', + stattype: 'numerical', + column_count_param: 'n_components', + enumerations: null, + }, + ], + }, + }, + ], + ); }); afterEach(() => { httpMock.verify(); + sessionStorage.clear(); }); + it('builds request body for histogram with active data model and datasets', () => { // Arrange - service.setSelectedDataModel(mockDataModel); + service.selectedDataModel.set(mockDataModel); service.setSelectedDatasets(['ds1']); // Act const body = service.buildRequestBody('histogram', ['var1']); // Assert - expect(body.algorithm.name).toBe('histogram'); - expect(body.algorithm.inputdata.data_model).toBe('dm:1'); - expect(body.algorithm.inputdata.datasets).toEqual(['ds1']); - expect(body.algorithm.inputdata.y).toEqual(['var1']); - expect(body.algorithm.inputdata.filters).toBeNull(); + expect(body.analysis.algorithm.name).toBe('histogram'); + expect(body.analysis.inputdata.data_model).toBe('dm:1'); + expect(body.analysis.inputdata.datasets).toEqual(['ds1']); + expect(body.analysis.algorithm.y).toEqual(['var1']); + expect(body.analysis.inputdata.variables).toEqual(['var1']); + expect(body.analysis.inputdata.filters).toBeNull(); + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { var1: 'drop' }, + }, + })); + expect(body.analysis.algorithm.parameters).toEqual({ histogram_type: 'wilkinson' }); + expect(body.mipVersion).toBeUndefined(); + }); + + it('includes a previewed histogram variable in inputdata.variables when the pool is empty', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + const body = service.buildRequestBody('histogram', ['inr']); + + expect(body.analysis.algorithm.y).toEqual(['inr']); + expect(body.analysis.inputdata.variables).toEqual(['inr']); + }); + + it('sends histogram grouping as algorithm.x without a drop strategy for the grouping variable', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + const byDataset = service.buildRequestBody('histogram', ['age'], ['dataset']); + expect(byDataset.analysis.algorithm.y).toEqual(['age']); + expect(byDataset.analysis.algorithm.x).toEqual(['dataset']); + expect(byDataset.analysis.inputdata.variables).toEqual(['age', 'dataset']); + expect(byDataset.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { age: 'drop' }, + }, + })); + + const bySex = service.buildRequestBody('histogram', ['age'], ['sex']); + expect(bySex.analysis.algorithm.x).toEqual(['sex']); + expect(bySex.analysis.inputdata.variables).toEqual(['age', 'sex']); + expect(bySex.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { age: 'drop' }, + }, + })); + }); + + it('forwards histogram grouping from getAlgorithmResults onto algorithm.x', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + service.getAlgorithmResults( + 'histogram', + ['age'], + null, + undefined, + undefined, + ['dataset'] + ).subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm.x).toEqual(['dataset']); + expect(req.request.body.analysis.algorithm.y).toEqual(['age']); + expect(req.request.body.analysis.inputdata.variables).toEqual(['age', 'dataset']); + expect(req.request.body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { age: 'drop' }, + }, + })); + req.flush({ result: { histogram: [] } }); + }); + + it('keeps dataset on histogram preview variables and the missing-values drop', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + const body = service.buildRequestBody('histogram', ['dataset']); + + expect(body.analysis.algorithm.y).toEqual(['dataset']); + expect(body.analysis.inputdata.variables).toEqual(['dataset']); + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { dataset: 'drop' }, + }, + })); + }); + + it('unions the previewed histogram variable with existing pool CDEs', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age' }]); + + const body = service.buildRequestBody('histogram', ['inr']); + + expect(body.analysis.inputdata.variables).toEqual(['age', 'inr']); + expect(body.analysis.algorithm.y).toEqual(['inr']); + }); + + it('does not apply stored descriptive preprocessing to histogram preview requests without an override', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setAppliedDescriptivePreprocessing({ + missing_values_handler: { + strategies: { var1: 'mean' }, + }, + }); + + const body = service.buildRequestBody('histogram', ['var1']); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { var1: 'drop' }, + }, + })); + }); + + it('uses an explicit preprocessing override for histogram preview requests', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const applied = { + missing_values_handler: { + strategies: { var1: 'median' }, + }, + }; + + const body = service.buildRequestBody('histogram', ['var1'], null, null, null, null, applied); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps(applied)); + }); + + it('filters applied descriptive preprocessing to histogram y variables only', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const applied = { + missing_values_handler: { + strategies: { age: 'drop', sex: 'drop' }, + }, + outlier_winsorizer: { + strategies: { age: 'iqr' }, + tails: { age: 'both' }, + folds: { age: 1.5 }, + }, + }; + + const body = service.buildRequestBody('histogram', ['age'], null, null, null, null, applied); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { age: 'drop' }, + }, + outlier_winsorizer: { + strategies: { age: 'iqr' }, + tails: { age: 'both' }, + folds: { age: 1.5 }, + }, + })); + }); + + it('skips preprocessing for histogram preview when override is explicitly null', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setAppliedDescriptivePreprocessing({ + missing_values_handler: { + strategies: { var1: 'mean' }, + }, + }); + + const body = service.buildRequestBody('histogram', ['var1'], null, null, null, null, null); + + expect(body.analysis.preprocessing).toBeNull(); + }); + + it('does not send validation_datasets for histogram when inputdata spec includes validation_datasets slot', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const histogram = service.backendAlgorithms()['histogram']; + service.backendAlgorithms.set({ + ...service.backendAlgorithms(), + histogram: { + ...histogram, + requires_validation_datasets: false, + inputdata: { + ...histogram.inputdata, + validation_datasets: { label: '', desc: '', types: ['text'], required: false }, + }, + }, + }); + + const body = service.buildRequestBody('histogram', ['var1']); + + expect(body.analysis.inputdata.validation_datasets).toBeNull(); + }); + + it('builds spec-driven inputdata with array roles and validation datasets', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.backendAlgorithms.set({ + validation_algo: { + name: 'validation_algo', + label: 'Validation Algo', + description: '', + requiredVariable: [], + covariate: [], + category: 'Mock', + configSchema: [], + isDisabled: false, + requires_validation_datasets: true, + inputdata: { + data_model: { label: '', desc: '', types: [] }, + datasets: { label: '', desc: '', types: [] }, + validation_datasets: { label: '', desc: '', types: [] }, + y: { label: '', desc: '', types: ['real'], max_count: 1 }, + x: { label: '', desc: '', types: ['real'] }, + filter: { label: '', desc: '', types: [] }, + }, + } as any, + }); + + service.setVariables([ + { code: 'y1', label: 'Y1', type: 'continuous' } as any, + { code: 'x1', label: 'X1', type: 'continuous' } as any, + ]); + + const body = service.buildRequestBody('validation_algo', ['y1'], ['x1']); + + expect(body.analysis.inputdata).toEqual(expect.objectContaining({ + data_model: 'dm:1', + datasets: ['ds1'], + validation_datasets: ['ds1'], + variables: ['y1', 'x1'], + filters: null, + })); + expect(body.analysis.algorithm).toEqual(expect.objectContaining({ + y: ['y1'], + x: ['x1'], + })); + }); + + it('adds default drop preprocessing for non-describe algorithm requests', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + const body = service.buildRequestBody('mock_algo', ['var1'], ['cov1']); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { + var1: 'drop', + cov1: 'drop', + }, + }, + })); + }); + + it('keeps dataset in variables and the missing-values drop when it is an algorithm role', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([ + { code: 'age', label: 'Age' }, + { code: 'dataset', label: 'Dataset Registry origin' }, + ]); + + const body = service.buildRequestBody('mock_algo', ['age'], ['dataset']); + + expect(body.analysis.algorithm.y).toEqual(['age']); + expect(body.analysis.algorithm.x).toEqual(['dataset']); + expect(body.analysis.inputdata.variables).toEqual(['age', 'dataset']); + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { age: 'drop', dataset: 'drop' }, + }, + })); }); - it('resetStudioState clears selections and errors', (done) => { + it('does not add default preprocessing for describe requests', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + const body = service.buildRequestBody('describe', ['var1']); + + expect(body.analysis.preprocessing).toBeNull(); + }); + + it('does not add default preprocessing for outlier_report requests', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + + const body = service.buildRequestBody('outlier_report', ['age']); + + expect(body.analysis.algorithm.name).toBe('outlier_report'); + expect(body.analysis.preprocessing).toBeNull(); + }); + + it('excludes histogram, describe, outlier_report, and linear_svm from the algorithm picker', () => { + const grouped = service.availableGroupedAlgorithms(); + const names = Object.values(grouped).flat().map((algo) => algo.name); + + expect(names).toContain('mock_algo'); + expect(names).not.toContain('histogram'); + expect(names).not.toContain('histogram_sql'); + expect(names).not.toContain('describe'); + expect(names).not.toContain('outlier_report'); + expect(names).not.toContain('linear_svm'); + expect(names).not.toContain('cox_regression_stacked'); + }); + + it('attaches structured availability details to grouped algorithms', () => { + const grouped = service.availableGroupedAlgorithms(); + const mock = Object.values(grouped).flat().find((algo) => algo.name === 'mock_algo'); + + expect(mock?.isDisabled).toBe(true); + expect(mock?.availability?.available).toBe(false); + expect(mock?.availability?.summary).toBe('Outcome needs at least 1 (none assigned).'); + expect(mock?.availability?.details.find((detail) => detail.role === 'y')).toEqual(expect.objectContaining({ + label: 'Outcome', + minCount: 1, + selectedCount: 0, + satisfied: false, + })); + }); + + + it('keeps the selected algorithm when current selections make it unavailable', () => { + service.setAlgorithmY([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + const selected = service.backendAlgorithms()['mock_algo']; + service.selectedAlgorithm.set(selected); + + expect(service.isAlgorithmAvailable('mock_algo')).toBe(true); + + service.setAlgorithmY([]); + + expect(service.isAlgorithmAvailable('mock_algo')).toBe(false); + expect(service.selectedAlgorithm()?.name).toBe('mock_algo'); + }); + + it('sends raw descriptive overview requests without preprocessing', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + service.loadDescriptiveOverview(['age']).subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm.name).toBe('describe'); + expect(req.request.body.analysis.preprocessing).toBeNull(); + req.flush({ result: { featurewise: [] } }); + }); + + it('keeps dataset on descriptive overview variables when it is in the pool', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([ + { code: 'age', label: 'Age' }, + { code: 'dataset', label: 'Dataset Registry origin' }, + ]); + + service.loadDescriptiveOverview(['age', 'dataset']).subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm.y).toEqual(['age', 'dataset']); + expect(req.request.body.analysis.inputdata.variables).toEqual(['age', 'dataset']); + req.flush({ result: { featurewise: [] } }); + }); + + it('keeps the cohort filter out of the step 0 source snapshot only', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + // A filter on a CDE that is not in the variable pool. + service.setFilterLogic({ condition: 'AND', rules: [{ field: 'site', operator: 'equal', value: 'A' }] } as any); + + service.loadDescriptiveOverview(['age']).subscribe(); + const rawReq = httpMock.expectOne('/services/experiments/preview'); + expect(rawReq.request.body.analysis.inputdata.filters).not.toBeNull(); + expect(rawReq.request.body.analysis.inputdata.variables).toContain('site'); + rawReq.flush({ result: { featurewise: [] } }); + + // Step 0 describes the data as selected: no filter payload, and the filter + // field is no longer dragged into the input pool. + service.loadDescriptiveOverview(['age'], null, null, null).subscribe(); + const sourceReq = httpMock.expectOne('/services/experiments/preview'); + expect(sourceReq.request.body.analysis.inputdata.filters).toBeNull(); + expect(sourceReq.request.body.analysis.inputdata.variables).not.toContain('site'); + expect(sourceReq.request.body.analysis.preprocessing).toBeNull(); + sourceReq.flush({ result: { featurewise: [] } }); + }); + + it('describes a filter override without touching the stored cohort', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const stored = { condition: 'AND', rules: [{ field: 'site', operator: 'equal', value: 'A' }] } as any; + const pending = { condition: 'AND', rules: [{ field: 'score', operator: 'greater', value: 65 }] } as any; + service.setFilterLogic(stored); + + // The Cohort Filtering preview sends what the editor holds, unapplied. + service.loadDescriptiveOverview(['age'], null, null, pending).subscribe(); + const pendingReq = httpMock.expectOne('/services/experiments/preview'); + expect(pendingReq.request.body.analysis.inputdata.filters).toEqual(pending); + expect(pendingReq.request.body.analysis.inputdata.variables).toContain('score'); + expect(pendingReq.request.body.analysis.inputdata.variables).not.toContain('site'); + pendingReq.flush({ result: { featurewise: [] } }); + + // An explicit null is "no rules", not "fall back to the store". + service.loadDescriptiveOverview(['age'], null, null, null).subscribe(); + const clearedReq = httpMock.expectOne('/services/experiments/preview'); + expect(clearedReq.request.body.analysis.inputdata.filters).toBeNull(); + expect(clearedReq.request.body.analysis.inputdata.variables).not.toContain('site'); + clearedReq.flush({ result: { featurewise: [] } }); + + // Leaving the override alone keeps the stored cohort. + service.loadDescriptiveOverview(['age']).subscribe(); + const storedReq = httpMock.expectOne('/services/experiments/preview'); + expect(storedReq.request.body.analysis.inputdata.filters).toEqual(stored); + storedReq.flush({ result: { featurewise: [] } }); + + // Previewing never writes the cohort. + expect(service.filterLogic()).toEqual(stored); + }); + + it('plots a histogram preview on the overridden cohort', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setFilterLogic({ condition: 'AND', rules: [{ field: 'site', operator: 'equal', value: 'A' }] } as any); + const pending = { condition: 'AND', rules: [{ field: 'score', operator: 'greater', value: 65 }] } as any; + const defaultDrop = { missing_values_handler: { strategies: { var1: 'drop' } } }; + + const body = service.buildRequestBody( + 'histogram', + ['var1'], + null, + null, + null, + null, + defaultDrop, + pending + ); + + expect(body.analysis.inputdata.filters).toEqual(pending); + expect(body.analysis.preprocessing).toEqual(preprocessingSteps(defaultDrop)); + }); + + it('forwards the cohort override from getAlgorithmResults to the preview request', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setFilterLogic({ condition: 'AND', rules: [{ field: 'site', operator: 'equal', value: 'A' }] } as any); + const pending = { condition: 'AND', rules: [{ field: 'score', operator: 'greater', value: 65 }] } as any; + + service.getAlgorithmResults( + 'histogram', + ['var1'], + null, + { missing_values_handler: { strategies: { var1: 'drop' } } }, + pending + ).subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.inputdata.filters).toEqual(pending); + req.flush({ result: { histogram: [] } }); + }); + + it('sends processed descriptive overview requests with explicit preprocessing', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const preprocessing = { + missing_values_handler: { + strategies: { age: 'mean' }, + }, + }; + + service.loadDescriptiveOverview(['age'], preprocessing).subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.preprocessing).toEqual(preprocessingSteps(preprocessing)); + req.flush({ result: { featurewise: [] } }); + }); + + it('does not use descriptive preview preprocessing in algorithm requests until it is stored as applied', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + + service.loadDescriptiveOverview(['age'], { + missing_values_handler: { + strategies: { age: 'mean' }, + }, + }).subscribe(); + const previewReq = httpMock.expectOne('/services/experiments/preview'); + previewReq.flush({ result: { featurewise: [] } }); + + const body = service.buildRequestBody('mock_algo', ['age']); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { age: 'drop' }, + }, + })); + }); + + it('uses applied descriptive preprocessing for later algorithm requests', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const applied = { + missing_values_handler: { + strategies: { age: 'median' }, + }, + }; + + service.setAppliedDescriptivePreprocessing(applied); + const body = service.buildRequestBody('mock_algo', ['age']); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps(applied)); + }); + + it('keeps the drop default ahead of a created column when no missing-value step was applied', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const creator = { + new_column_names: ['sex_group'], + strategy: 'filter_rules', + rules: { m: { condition: 'AND', rules: [{ id: 'sex', field: 'sex', type: 'string', input: 'select', operator: 'equal', value: '1' }] } }, + }; + setCreators(service, 'categorical_column_creator', [creator]); + + const body = service.buildRequestBody('mock_algo', ['sex_group'], ['age']); + const steps = body.analysis.preprocessing as AnalysisPreprocessingStep[]; + + // The created column is not a CDE, so only the real covariate gets a strategy. + expect(steps.map((step) => step.name)).toEqual(['missing_values_handler', 'categorical_column_creator']); + expect(steps[0].parameters).toEqual({ strategies: { age: 'drop' } }); + }); + + it('uses applied descriptive preprocessing with multiple preprocessing steps for later algorithm requests', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const applied = { + missing_values_handler: { + strategies: { age: 'drop', sex: 'drop' }, + }, + longitudinal_transformer: { + visit1: 'BL', + visit2: 'FL1', + strategies: { + age: 'diff', + sex: 'first', + }, + }, + }; + + service.setAppliedDescriptivePreprocessing(applied); + const body = service.buildRequestBody('mock_algo', ['age'], ['sex']); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps(applied)); + }); + + it('forwards applied outlier preprocessing together with missing values', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const applied = { + missing_values_handler: { + strategies: { age: 'drop' }, + }, + outlier_winsorizer: { + strategies: { age: 'iqr' }, + tails: { age: 'both' }, + folds: { age: 1.5 }, + }, + }; + + service.setAppliedDescriptivePreprocessing(applied); + const body = service.buildRequestBody('mock_algo', ['age']); + + expect(body.analysis.preprocessing).toEqual(preprocessingSteps(applied)); + }); + + it('summarizes preprocessing with variable labels and human-readable actions', () => { + const summary = service.formatPreprocessingConfig({ + missing_values_handler: { + strategies: { gender: 'drop', subjectageyears: 'drop' }, + }, + longitudinal_transformer: { + visit1: 'BL', + visit2: 'FL1', + strategies: { + gender: 'first', + subjectageyears: 'diff', + }, + }, + }, { + gender: 'Gender', + subjectageyears: 'Subject Age Years', + }); + + expect(summary).toBe( + 'Missing values: Gender: remove rows, Subject Age Years: remove rows\nLongitudinal transformation: Gender: use first visit, Subject Age Years: difference between visits (BL to FL1)' + ); + expect(service.formatPreprocessingEntries({ + missing_values_handler: { + strategies: { gender: 'drop' }, + }, + }, { gender: 'Gender' })).toEqual([ + { label: 'Missing values', value: 'Gender: remove rows', lines: ['Gender: remove rows'] }, + ]); + }); + + it('summarizes outlier winsorizer preprocessing explicitly', () => { + const summary = service.formatPreprocessingConfig({ + outlier_winsorizer: { + strategies: { age: 'iqr', bmi: 'quantile' }, + tails: { age: 'both', bmi: 'right' }, + folds: { age: 1.5, bmi: 0.05 }, + }, + }, { + age: 'Age', + bmi: 'BMI', + }); + + expect(summary).toBe( + 'Outlier winsorizer: Age: IQR, both tails, fold 1.5; BMI: Quantile, right tail, fold 0.05' + ); + }); + + it('loads outlier report previews with outlier parameters and upstream missing preprocessing', () => new Promise<void>((done) => { + service.selectedDataModel.set(mockDataModel); + service.setVariables([ + { code: 'age', label: 'Age', type: 'continuous' } as any, + { code: 'bmi', label: 'BMI', type: 'continuous' } as any, + ]); + service.setAlgorithmY([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + service.setAlgorithmX([{ code: 'bmi', label: 'BMI', type: 'continuous' } as any]); + + service.loadOutlierReportPreview( + ['age', 'bmi'], + { + strategies: { age: 'iqr', bmi: 'quantile' }, + tails: { age: 'both', bmi: 'right' }, + folds: { age: 1.5, bmi: 0.05 }, + }, + { + missing_values_handler: { + strategies: { age: 'median' }, + }, + } + ).subscribe((response) => { + expect(response.result.featurewise).toEqual([]); + done(); + }); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.method).toBe('POST'); + expect(req.request.body.analysis.algorithm).toEqual(expect.objectContaining({ + name: 'outlier_report', + y: ['age'], + x: ['bmi'], + parameters: { + strategies: { age: 'iqr', bmi: 'quantile' }, + tails: { age: 'both', bmi: 'right' }, + folds: { age: 1.5, bmi: 0.05 }, + }, + })); + expect(req.request.body.analysis.inputdata).toEqual(expect.objectContaining({ + data_model: 'dm:1', + datasets: [], + filters: null, + variables: expect.arrayContaining(['age', 'bmi']), + })); + expect(req.request.body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { age: 'median' }, + }, + })); + req.flush({ featurewise: [] }); + })); + + it('keeps nominal variables and numeric covariates in distinct outlier report input roles', () => new Promise<void>((done) => { + service.selectedDataModel.set(mockDataModel); + service.setVariables([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + service.setAlgorithmY([{ code: 'sex', label: 'Sex', type: 'nominal' } as any]); + service.setAlgorithmX([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + + service.loadOutlierReportPreview( + ['age'], + { + strategies: { age: 'iqr' }, + tails: { age: 'both' }, + folds: { age: 1.5 }, + }, + ).subscribe((response) => { + expect(response.result.featurewise).toEqual([]); + done(); + }); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.method).toBe('POST'); + expect(req.request.body.analysis.inputdata).toEqual(expect.objectContaining({ + data_model: 'dm:1', + datasets: [], + filters: null, + variables: ['age'], + })); + expect(req.request.body.analysis.algorithm).toEqual(expect.objectContaining({ + y: ['age'], + x: null, + })); + req.flush({ featurewise: [] }); + })); + + it('sends covariate-only outlier report codes through y only', () => new Promise<void>((done) => { + service.selectedDataModel.set(mockDataModel); + service.setVariables([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + service.setAlgorithmX([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + + service.loadOutlierReportPreview( + ['age'], + { + strategies: { age: 'iqr' }, + tails: { age: 'both' }, + folds: { age: 1.5 }, + }, + ).subscribe((response) => { + expect(response.result.featurewise).toEqual([]); + done(); + }); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm).toEqual(expect.objectContaining({ + y: ['age'], + x: null, + })); + req.flush({ featurewise: [] }); + })); + + it('resetStudioStateForGuide restores the default pathology after clearing guide progress', () => { + const defaultModel: DataModel = { + ...mockDataModel, + code: 'stroke', + label: 'Stroke', + }; + const alternateModel: DataModel = { + ...mockDataModel, + code: 'alternate', + label: 'Alternate', + }; + + service.crossSectionalModels.set([defaultModel]); + service.longitudinalModels.set([alternateModel]); + service.selectedDataModel.set(alternateModel); + service.setVariables([{ code: 'v1', label: 'V1' } as any]); + + service.resetStudioStateForGuide(); + + expect(service.selectedVariables()).toEqual([]); + expect(service.selectedDataModel()).toEqual(defaultModel); + expect(service.selectedDatasets()).toEqual(['ds1']); + }); + + it('preselectAllDatasetsForModel selects every dataset enumeration for the pathology', () => { + const model: DataModel = { + ...mockDataModel, + variables: [ + { + code: 'dataset', + label: 'Dataset', + type: 'nominal', + enumerations: [ + { code: 'ds-a', label: 'Dataset A' }, + { code: 'ds-b', label: 'Dataset B' }, + ], + } as any, + ], + datasets: ['ds-a', 'ds-b'], + }; + + service.preselectAllDatasetsForModel(model); + + expect(service.availableDatasets().map((dataset) => dataset.code)).toEqual(['ds-a', 'ds-b']); + expect(service.selectedDatasets()).toEqual(['ds-a', 'ds-b']); + }); + + it('clears variables once when datasets become empty, without retriggering', () => { + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age' } as any]); + service.setAlgorithmY([{ code: 'age', label: 'Age' } as any]); + + const warn = vi.spyOn(console, 'warn').mockReturnValue(undefined); + service.setSelectedDatasets([]); + TestBed.flushEffects(); + + expect(service.selectedVariables()).toEqual([]); + expect(service.algorithmY()).toEqual([]); + const resetWarnings = warn.mock.calls.filter((args) => + String(args[0]).includes('No datasets selected') + ); + expect(resetWarnings.length).toBe(1); + }); + + it('resetStudioState clears selections and errors', () => { const errorService = TestBed.inject(ErrorService); // Arrange - service.setSelectedDataModel(mockDataModel); + service.selectedDataModel.set(mockDataModel); service.setSelectedDatasets(['ds1']); service.setVariables([{ code: 'v1', label: 'V1' } as any]); errorService.setError('Oops'); @@ -99,10 +1111,19 @@ describe('ExperimentStudioService', () => { expect(service.selectedDataModel()).toBeNull(); expect(service.selectedDatasets()).toEqual([]); expect(service.selectedVariables()).toEqual([]); - errorService.error$.subscribe((msg) => { - expect(msg).toBeNull(); - done(); + expect(errorService.error()).toBeNull(); + }); + + it('resetStudioState clears the ordered transformation steps', () => { + service.upsertTransformation({ + id: 'cat', + name: 'categorical_column_creator', + parameters: { new_column_names: ['grp'], rules: {} }, }); + + service.resetStudioState(); + + expect(service.appliedTransformations()).toEqual([]); }); it('returns null from runSelectedAlgorithm when no algorithm is selected', () => { @@ -112,4 +1133,1311 @@ describe('ExperimentStudioService', () => { // Assert expect(result).toBeNull(); }); + + it('runs the selected algorithm through the kept-run endpoint', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.selectedAlgorithm.set(service.backendAlgorithms()['mock_algo']); + let response: any; + + service.runSelectedAlgorithm()!.subscribe((res) => (response = res)); + + const req = httpMock.expectOne('/services/experiments/runs'); + expect(req.request.method).toBe('POST'); + expect(req.request.body.analysis.algorithm.name).toBe('mock_algo'); + req.flush({ uuid: 'run-1', status: 'success', result: { value: 1 } }); + expect(response).toEqual({ uuid: 'run-1', status: 'success', result: { value: 1 } }); + }); + + it('runs describe through the kept-run endpoint so it can be saved too', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + service.selectedAlgorithm.set(service.backendAlgorithms()['describe']); + + service.runSelectedAlgorithm()!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/runs'); + expect(req.request.body.analysis.algorithm.name).toBe('describe'); + req.flush({ uuid: 'run-describe', status: 'success', result: { featurewise: [] } }); + }); + + it('saves the kept run without running the experiment again', () => { + service.runResult.set({ value: 1 }); + service.runId.set('run-1'); + + service.saveRun('My experiment'); + + expect(service.isSaving()).toBe(true); + const req = httpMock.expectOne('/services/experiments'); + expect(req.request.method).toBe('POST'); + expect(req.request.body).toEqual({ runId: 'run-1', name: 'My experiment' }); + req.flush({ uuid: 'experiment-1', shared: false, status: 'success', result: null }); + + expect(service.isSaving()).toBe(false); + expect(service.currentExperimentUUID()).toBe('experiment-1'); + expect(service.saveSucceeded()).toBe(true); + }); + + it('asks for a fresh run when the kept run has expired', () => { + service.runResult.set({ value: 1 }); + service.runId.set('run-1'); + + service.saveRun('My experiment'); + httpMock + .expectOne('/services/experiments') + .flush({ message: 'gone' }, { status: 404, statusText: 'Not Found' }); + + expect(service.isSaving()).toBe(false); + expect(service.saveSucceeded()).toBe(false); + expect(service.saveError()).toContain('Run the experiment again'); + }); + + it('does not save when no run result is on screen', () => { + service.runId.set('run-1'); + + service.saveRun('My experiment'); + + httpMock.expectNone('/services/experiments'); + expect(service.saveError()).toContain('Run the experiment'); + }); + + it('keeps an algorithm available when multiple filter variables are selected', () => { + service.setAlgorithmY([{ code: 'age', label: 'Age', type: 'continuous' } as any]); + service.setFilters([ + { code: 'age', label: 'Age', type: 'continuous' } as any, + { code: 'event_type', label: 'Event Type', type: 'nominal' } as any, + ]); + + expect(service.isAlgorithmAvailable('mock_algo')).toBe(true); + }); + + it('coerces numeric parameter strings before building request payloads', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.algorithmConfigurations.set({ + mock_algo: { alpha: '0.05' }, + }); + + const body = service.buildRequestBody('mock_algo', ['age']); + + expect(body.analysis.algorithm.parameters.alpha).toBe(0.05); + }); + + it('keeps enum select parameter strings even when their schema type is int', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const algo = service.backendAlgorithms()['mock_algo']; + service.backendAlgorithms.set({ + ...service.backendAlgorithms(), + mock_algo: { + ...algo, + configSchema: [ + ...algo.configSchema, + { + key: 'positive_class', + label: 'Positive class (y=1)', + type: 'select', + types: ['int'], + enumType: 'input_var_CDE_enums', + enumSource: ['y'], + options: [ + { code: '0', label: '0' }, + { code: '1', label: '1' }, + { code: '9', label: '9' }, + ], + }, + ], + }, + }); + service.algorithmConfigurations.set({ + mock_algo: { positive_class: '1' }, + }); + + const body = service.buildRequestBody('mock_algo', ['acute_treat_evt']); + + expect(body.analysis.algorithm.parameters.positive_class).toBe('1'); + }); + + it('omits unset positive_class from experiment request parameters', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.algorithmConfigurations.set({ + mock_algo: { alpha: 0.05 }, + }); + + const body = service.buildRequestBody('mock_algo', ['acute_treat_evt']); + + expect(body.analysis.algorithm.parameters.positive_class).toBeUndefined(); + expect(body.analysis.algorithm.parameters.alpha).toBe(0.05); + }); + + it('builds binned Mann-Whitney requests with group enum codes and missing-value preprocessing', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.backendAlgorithms.set({ + ...service.backendAlgorithms(), + binned_mann_whitney_u_test: { + name: 'binned_mann_whitney_u_test', + label: 'Binned Mann-Whitney U Test', + description: '', + inputdata: { + y: { label: 'Outcome', desc: '', types: ['real', 'int'], required: true, max_count: 1 }, + x: { label: 'Grouping variable', desc: '', types: ['text'], stattypes: ['nominal'], required: true, max_count: 1 }, + }, + configSchema: [ + { key: 'alt_hypothesis', label: 'Alternative hypothesis', type: 'select', options: ['two-sided', 'less', 'greater'], required: true }, + { key: 'groupA', label: 'Group A', type: 'select', types: ['text', 'int'], enumType: 'input_var_CDE_enums', enumSource: ['x'], required: true, options: [ + { code: '0', label: 'Control' }, + { code: '1', label: 'Case' }, + ] }, + { key: 'groupB', label: 'Group B', type: 'select', types: ['text', 'int'], enumType: 'input_var_CDE_enums', enumSource: ['x'], required: true, options: [ + { code: '0', label: 'Control' }, + { code: '1', label: 'Case' }, + ] }, + { key: 'num_bins', label: 'Number of bins', type: 'number', types: ['int'], required: false, default: 40, min: 2, max: 200 }, + ], + requiredVariable: ['real', 'int'], + covariate: ['text'], + category: 'Statistical Tests', + preprocessing: [], + required_preprocessing: ['missing_values_handler'], + isDisabled: false, + } as any, + }); + service.algorithmConfigurations.set({ + binned_mann_whitney_u_test: { + alt_hypothesis: 'two-sided', + groupA: 'Control', + groupB: { code: '1', label: 'Case' }, + num_bins: '40', + }, + }); + + const body = service.buildRequestBody('binned_mann_whitney_u_test', ['age'], ['sex']); + + expect(body.analysis.algorithm.name).toBe('binned_mann_whitney_u_test'); + expect(body.analysis.algorithm.y).toEqual(['age']); + expect(body.analysis.algorithm.x).toEqual(['sex']); + expect(body.analysis.algorithm.parameters).toEqual({ + alt_hypothesis: 'two-sided', + groupA: '0', + groupB: '1', + num_bins: 40, + }); + expect(body.analysis.preprocessing).toEqual(preprocessingSteps({ + missing_values_handler: { + strategies: { + age: 'drop', + sex: 'drop', + }, + }, + })); + }); + + it('keeps enum multi-select parameter values as strings even when their schema type is int', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + const algo = service.backendAlgorithms()['mock_algo']; + service.backendAlgorithms.set({ + ...service.backendAlgorithms(), + mock_algo: { + ...algo, + configSchema: [ + ...algo.configSchema, + { + key: 'category_order', + label: 'Category order', + type: 'multi-select', + types: ['int'], + enumType: 'input_var_CDE_enums', + enumSource: ['y'], + options: [ + { code: '0', label: '0' }, + { code: '1', label: '1' }, + { code: '9', label: '9' }, + ], + }, + ], + }, + }); + service.algorithmConfigurations.set({ + mock_algo: { category_order: ['0', '1', '9'] }, + }); + + const body = service.buildRequestBody('mock_algo', ['acute_treat_evt']); + + expect(body.analysis.algorithm.parameters.category_order).toEqual(['0', '1', '9']); + }); + + it('hydrates edit state with filters and applied preprocessing from backend experiment', () => { + const filters = { + condition: 'AND' as const, + rules: [ + { + id: 'site', + field: 'site', + type: 'string' as const, + input: 'select' as const, + operator: 'equal', + value: 'athens', + }, + ], + valid: true, + }; + const preprocessing = { + missing_values_handler: { + strategies: { + age: 'median', + sex: 'drop', + }, + }, + }; + + service.hydrateFromBackendExperiment({ + uuid: 'exp-1', + name: 'Saved experiment', + created: '', + finished: '', + shared: true, + viewed: false, + status: 'success', + analysis: { + inputdata: { + data_model: 'dm:1', + datasets: ['ds1'], + variables: ['age', 'sex'], + filters, + }, + preprocessing: preprocessingSteps(preprocessing)!, + algorithm: { + name: 'mock_algo', + y: ['age'], + x: ['sex'], + parameters: { alpha: 0.01 }, + }, + }, + createdBy: { + username: 'user', + fullname: 'User', + email: 'user@example.org', + subjectId: 'subject', + agreeNDA: true, + }, + }); + + const req = httpMock.expectOne('/services/data-models'); + req.flush([mockDataModel]); + + expect(service.selectedDataModel()?.code).toBe('dm'); + expect(service.selectedDatasets()).toEqual(['ds1']); + // Pool holds the unified y + x CDEs. + expect(service.selectedVariables().map((variable) => variable.code)).toEqual(['age', 'sex']); + expect(service.algorithmY().map((variable) => variable.code)).toEqual(['age']); + expect(service.algorithmX().map((variable) => variable.code)).toEqual(['sex']); + expect(service.selectedFilters().map((variable) => variable.code)).toEqual(['site']); + expect(service.filterLogic()).toEqual(filters); + expect(service.algorithmConfigurations()['mock_algo']).toEqual({ alpha: 0.01 }); + expect(service.appliedPreprocessingConfig()).toEqual(preprocessing); + expect(service.isShared()).toBe(true); + + // A column created after loading must reach the run: the saved preprocessing + // must not shadow the shared applied config. + const creator = { + new_column_names: ['sex_group'], + strategy: 'filter_rules', + rules: { m: { condition: 'AND', rules: [{ id: 'sex', field: 'sex', type: 'string', input: 'select', operator: 'equal', value: '1' }] } }, + }; + setCreators(service, 'categorical_column_creator', [creator]); + const steps = service.buildRequestBody('mock_algo').analysis.preprocessing as AnalysisPreprocessingStep[]; + expect(steps.find((step) => step.name === 'categorical_column_creator')?.parameters).toEqual(creator); + }); + + it('keeps y/x roles separated from the pool when a variable is added to the pool', () => { + service.setAlgorithmY([{ code: 'age', label: 'Age' }]); + service.setAlgorithmX([{ code: 'sex', label: 'Sex' }]); + + // Adding a new pool item does not assign it to either role. + service.setVariables([ + { code: 'age', label: 'Age' }, + { code: 'sex', label: 'Sex' }, + { code: 'bmi', label: 'BMI' }, + ]); + + expect(service.selectedVariables().map((v) => v.code)).toEqual(['age', 'sex', 'bmi']); + expect(service.algorithmY().map((v) => v.code)).toEqual(['age']); + expect(service.algorithmX().map((v) => v.code)).toEqual(['sex']); + }); + + it('exposes the created transformation column in the assignable pool but not in inputdata.variables', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age' }]); + service.setAlgorithmY([{ code: 'age', label: 'Age' }]); + setCreators(service, 'categorical_column_creator', [{ + new_column_names: ['derived_col'], + rules: { A: {}, B: {} }, + default_enumeration: 'A', + }]); + service.setAlgorithmX([{ code: 'derived_col', label: 'derived_col' }]); + + const assignable = service.algorithmAssignableVariables(); + const created = assignable.find((v) => v.code === 'derived_col'); + expect(created).toBeTruthy(); + expect(created?.isCreatedColumn).toBe(true); + expect(created?.enumerations).toEqual([ + { code: 'A', label: 'A' }, + { code: 'B', label: 'B' }, + ]); + + const body = service.buildRequestBody('mock_algo'); + expect(body.analysis.algorithm.y).toEqual(['age']); + expect(body.analysis.algorithm.x).toEqual(['derived_col']); + // Derived column codes are stripped from the source CDE list. + expect(body.analysis.inputdata.variables).toEqual(['age']); + expect(body.analysis.inputdata.variables).not.toContain('derived_col'); + }); + + it('sends two applied creators as two steps of the same name', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([ + { code: 'mrs', label: 'mrs' }, + { code: 'age', label: 'Age' }, + ]); + service.setAlgorithmY([{ code: 'age', label: 'Age' }]); + const good = { + new_column_names: ['mrs_good_outcome'], + strategy: 'filter_rules', + rules: { + good: { + condition: 'AND', + rules: [{ id: 'mrs-le-2', field: 'mrs', type: 'real', input: 'number', operator: '<=', value: 2 }], + }, + }, + }; + const bad = { + new_column_names: ['mrs_bad_outcome'], + strategy: 'filter_rules', + rules: { + bad: { + condition: 'AND', + rules: [{ id: 'mrs-gt-2', field: 'mrs', type: 'real', input: 'number', operator: '>', value: 2 }], + }, + }, + default_enumeration: 'unknown', + }; + + setCreators(service, 'categorical_column_creator', [good, bad]); + + // Two or more creators persist as an ordered array under the one config key. + expect(service.appliedPreprocessingConfig()?.['categorical_column_creator']).toEqual([good, bad]); + + service.setAlgorithmX([ + { code: 'mrs_good_outcome', label: 'mrs_good_outcome', isCreatedColumn: true }, + { code: 'mrs_bad_outcome', label: 'mrs_bad_outcome', isCreatedColumn: true }, + ]); + + // Every derived column is assignable. + expect(service.algorithmAssignableVariables().map((v) => v.code)) + .toEqual(['mrs', 'age', 'mrs_good_outcome', 'mrs_bad_outcome']); + + const body = service.buildRequestBody('mock_algo'); + const creators = (body.analysis.preprocessing as AnalysisPreprocessingStep[]) + .filter((step) => step.name === 'categorical_column_creator'); + // The engine takes the repeated step name in draft order. + expect(creators.map((step) => step.parameters['new_column_names'])).toEqual([ + ['mrs_good_outcome'], + ['mrs_bad_outcome'], + ]); + expect(creators[1].parameters['default_enumeration']).toBe('unknown'); + + expect(body.analysis.algorithm.y).toEqual(['age']); + expect(body.analysis.algorithm.x).toEqual(['mrs_good_outcome', 'mrs_bad_outcome']); + // Both derived codes are stripped from the source CDE list; their rule + // sources are still sent. + expect(body.analysis.inputdata.variables).toEqual(['mrs', 'age']); + }); + + it('keeps a stored cohort filter tree exactly as stored', () => { + service.hydrateFromBackendExperiment({ + uuid: 'exp-filter-shape', + name: 'Saved cohort', + created: '', + finished: '', + shared: false, + viewed: false, + status: 'success', + analysis: { + inputdata: { + data_model: 'dm:1', + datasets: ['ds1'], + filters: { + condition: 'AND', + rules: [ + { id: 'sex', field: 'sex', operator: 'in', value: ['1', '2'], type: 'string' }, + // Stored without its value key; the reference filter client sends null for these. + { id: 'age', field: 'age', operator: 'is_not_null', type: 'integer' }, + ], + }, + variables: ['sex', 'age'], + }, + preprocessing: null, + algorithm: { name: 'mock_algo', y: ['age'], x: ['sex'], parameters: {} }, + }, + } as any); + httpMock.expectOne('/services/data-models').flush([mockDataModel]); + + // Loading never rewrites or fills in a stored rule… + expect((service.filterLogic()?.rules as any[])[0]) + .toEqual(expect.objectContaining({ field: 'sex', operator: 'in', value: ['1', '2'] })); + // …including a unary rule whose own client omitted the value key. + expect((service.filterLogic()?.rules as any[])[1]) + .toEqual(expect.objectContaining({ field: 'age', operator: 'is_not_null', type: 'integer' })); + expect('value' in ((service.filterLogic()?.rules as any[])[1])).toBe(false); + + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([ + { code: 'sex', label: 'Sex' }, + { code: 'age', label: 'Age' }, + ]); + service.setAlgorithmY([{ code: 'age', label: 'Age' }]); + service.setAlgorithmX([{ code: 'sex', label: 'Sex' }]); + + const sentRules = service.buildRequestBody('mock_algo').analysis.inputdata.filters.rules as any[]; + expect(sentRules[0].value).toEqual(['1', '2']); + expect('value' in sentRules[1]).toBe(false); + }); + + it('hydrates every saved creator step, not just the last one with that name', () => { + const good = { new_column_names: ['mrs_good_outcome'], strategy: 'filter_rules', rules: { good: {} } }; + const bad = { new_column_names: ['mrs_bad_outcome'], strategy: 'filter_rules', rules: { bad: {} } }; + + service.hydrateFromBackendExperiment({ + uuid: 'exp-derived-pair', + name: 'Saved experiment', + created: '', + finished: '', + shared: false, + viewed: false, + status: 'success', + analysis: { + inputdata: { + data_model: 'dm:1', + datasets: ['ds1'], + variables: ['age'], + }, + preprocessing: [ + { name: 'categorical_column_creator', parameters: good }, + { name: 'categorical_column_creator', parameters: bad }, + ], + algorithm: { + name: 'mock_algo', + y: ['age'], + x: ['mrs_good_outcome', 'mrs_bad_outcome'], + parameters: {}, + }, + }, + createdBy: { + username: 'user', + fullname: 'User', + email: 'user@example.org', + subjectId: 'subject', + agreeNDA: true, + }, + }); + + const req = httpMock.expectOne('/services/data-models'); + req.flush([mockDataModel]); + + // Repeated steps of the same name collect into an array so the edit form + // can rebuild both cards. + expect(service.appliedPreprocessingConfig()?.['categorical_column_creator']).toEqual([good, bad]); + expect(service.algorithmAssignableVariables().map((v) => v.code)) + .toEqual(['age', 'mrs_good_outcome', 'mrs_bad_outcome']); + }); + + it('includes unassigned pool CDEs used as transformation sources in inputdata.variables', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([ + { code: 'mrs', label: 'mrs' }, + { code: 'age', label: 'Age' }, + ]); + service.setAlgorithmY([{ code: 'age', label: 'Age' }]); + setCreators(service, 'categorical_column_creator', [{ + new_column_names: ['mrs_good_outcome'], + strategy: 'filter_rules', + rules: { + good: { + condition: 'AND', + rules: [{ id: 'mrs', field: 'mrs', type: 'real', input: 'number', operator: '<=', value: 2 }], + }, + }, + }]); + service.setAlgorithmX([{ code: 'mrs_good_outcome', label: 'mrs_good_outcome' }]); + + const body = service.buildRequestBody('mock_algo'); + // 'mrs' is in the pool and referenced by the transformation rule filters, so + // it must be sent as a source CDE even though it is not assigned to y/x. + expect(body.analysis.inputdata.variables).toEqual(['mrs', 'age']); + expect(body.analysis.inputdata.variables).not.toContain('mrs_good_outcome'); + expect(body.analysis.algorithm.y).toEqual(['age']); + expect(body.analysis.algorithm.x).toEqual(['mrs_good_outcome']); + }); + + it('drops a renamed created column from roles when the transformation code changes', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age' }]); + service.setAlgorithmY([{ code: 'age', label: 'Age' }]); + setCreators(service, 'categorical_column_creator', [{ + new_column_names: ['old_col'], + rules: { A: {} }, + default_enumeration: 'A', + }]); + // The role UI assigns the live transformation node (isCreatedColumn: true). + service.setAlgorithmX([{ code: 'old_col', label: 'old_col', isCreatedColumn: true }]); + + expect(service.algorithmX().map((v) => v.code)).toEqual(['old_col']); + + // Renaming the created column makes the old code unassignable, so it is + // pruned from the x role automatically. + setCreators(service, 'categorical_column_creator', [{ + new_column_names: ['new_col'], + rules: { A: {} }, + default_enumeration: 'A', + }]); + + expect(service.algorithmX()).toEqual([]); + expect(service.algorithmY().map((v) => v.code)).toEqual(['age']); + }); + + it('hydrates saved y/x roles including a derived code as a synthetic node', () => { + service.hydrateFromBackendExperiment({ + uuid: 'exp-derived', + name: 'Saved experiment', + created: '', + finished: '', + shared: false, + viewed: false, + status: 'success', + analysis: { + inputdata: { + data_model: 'dm:1', + datasets: ['ds1'], + variables: ['age', 'sex', 'derived_col'], + }, + preprocessing: null, + algorithm: { + name: 'mock_algo', + y: ['age'], + x: ['derived_col'], + parameters: {}, + }, + }, + createdBy: { + username: 'user', + fullname: 'User', + email: 'user@example.org', + subjectId: 'subject', + agreeNDA: true, + }, + }); + + const req = httpMock.expectOne('/services/data-models'); + req.flush([mockDataModel]); + + // Only real CDEs populate the variables-panel pool; the derived code is a + // synthetic assignable role node, not a pool member. Real CDEs saved in + // inputdata.variables (here 'sex') are restored to the pool too, even when + // they are not assigned to y/x. + expect(service.selectedVariables().map((v) => v.code)).toEqual(['age', 'sex']); + expect(service.algorithmY().map((v) => v.code)).toEqual(['age']); + expect(service.algorithmX().map((v) => v.code)).toEqual(['derived_col']); + expect(service.algorithmX()[0]?.isCreatedColumn).toBe(true); + expect(service.algorithmAssignableVariables().map((v) => v.code)).toEqual(['age', 'sex', 'derived_col']); + }); + + it('gives a hydrated created-column role the categories of its live creator', () => { + const rule = (operator: string) => ({ + condition: 'AND', rules: [{ id: 'age', field: 'age', type: 'integer', input: 'number', operator, value: 60 }], + }); + service.hydrateFromBackendExperiment({ + uuid: 'exp-created', + name: 'Saved experiment', + created: '', + finished: '', + shared: false, + viewed: false, + status: 'success', + analysis: { + inputdata: { data_model: 'dm:1', datasets: ['ds1'], variables: ['age'] }, + preprocessing: preprocessingSteps({ + categorical_column_creator: { + new_column_names: ['old'], strategy: 'filter_rules', rules: { '0': rule('less'), '1': rule('greater_or_equal') }, + }, + }), + algorithm: { name: 'mock_algo', y: ['old'], x: ['age'], parameters: {} }, + }, + createdBy: { + username: 'user', + fullname: 'User', + email: 'user@example.org', + subjectId: 'subject', + agreeNDA: true, + }, + }); + httpMock.expectOne('/services/data-models').flush([mockDataModel]); + + // The saved y code carries no categories; they come from the creator config. + expect(service.algorithmY()[0].enumerations.map((e: any) => e.code)).toEqual(['0', '1']); + + // Renaming a category later is reflected in the assigned role too. + setCreators(service, 'categorical_column_creator', [{ + new_column_names: ['old'], strategy: 'filter_rules', rules: { young: rule('less'), older: rule('greater_or_equal') }, default_enumeration: 'na', + }]); + expect(service.algorithmY()[0].enumerations.map((e: any) => e.code)).toEqual(['young', 'older', 'na']); + }); + + it('removes an item from the pool and prunes it from assigned y/x roles', () => { + service.setAlgorithmY([{ code: 'age', label: 'Age' }]); + service.setAlgorithmX([{ code: 'sex', label: 'Sex' }]); + service.setVariables([ + { code: 'age', label: 'Age' }, + { code: 'sex', label: 'Sex' }, + ]); + + // Removing 'sex' from the pool should prune it from algorithmX. + service.setVariables([{ code: 'age', label: 'Age' }]); + + expect(service.algorithmY().map((v) => v.code)).toEqual(['age']); + expect(service.algorithmX()).toEqual([]); + }); + + describe('kmeans_cluster_creator preprocessing', () => { + const clusterFixture = (clusterVariables: string[]) => ({ + new_column_names: ['kmeans_cluster'] as [string], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { + enumeration_prefix: 'cluster_', + preprocessing_name: 'kmeans_cluster_creator', + cluster_variables: clusterVariables, + centers: { cluster_0: { a: 1, b: 2 }, cluster_1: { a: 3, b: 4 } }, + source_context: { data_model: 'dm:1', datasets: ['ds1'], input_fingerprint: 'fp' }, + available_outputs: [], + cluster_choices: [ + { cluster_id: 'cluster_0', label: 'Cluster 0' }, + { cluster_id: 'cluster_1', label: 'Cluster 1' }, + ], + }, + }); + + function stepNames(body: any): string[] { + return (body.analysis.preprocessing as AnalysisPreprocessingStep[]).map((step) => step.name); + } + + it('exposes the cluster column in the assignable pool and reads back the applied creator', () => { + setCreators(service, 'kmeans_cluster_creator', [clusterFixture(['a', 'b'])]); + + expect(service.appliedPreprocessingConfig()?.['kmeans_cluster_creator']) + .toEqual([clusterFixture(['a', 'b'])]); + + const clusterNode = service.algorithmAssignableVariables().find((v) => v.code === 'kmeans_cluster'); + expect(clusterNode).toBeTruthy(); + expect(clusterNode?.isCreatedColumn).toBe(true); + // The creator derives cluster categories from `prefixed_range` on k + enumeration_prefix, + // so they are 1-indexed and code === label (matching the backend's stored shape). + expect(clusterNode?.enumerations).toEqual([ + { code: 'cluster_1', label: 'cluster_1' }, + { code: 'cluster_2', label: 'cluster_2' }, + ]); + }); + + it('drops the cluster column from the pool when cleared', () => { + setCreators(service, 'kmeans_cluster_creator', [clusterFixture(['a', 'b'])]); + + expect(service.algorithmAssignableVariables().map((v) => v.code)).toContain('kmeans_cluster'); + + service.clearTransformations(); + + expect(service.appliedPreprocessingConfig()).toBeNull(); + // Nothing was assigned to a role, so the code is no longer assignable. + expect(service.algorithmAssignableVariables().map((v) => v.code)).not.toContain('kmeans_cluster'); + }); + + it('keeps a role node hydrated from a saved experiment when no creator is applied', () => { + service.setAlgorithmY([{ code: 'kmeans_cluster', label: 'kmeans_cluster', isCreatedColumn: true }]); + + expect(service.algorithmY().map((v) => v.code)).toEqual(['kmeans_cluster']); + + setCreators(service, 'kmeans_cluster_creator', [clusterFixture(['a', 'b'])]); + service.clearTransformations(); + + expect(service.appliedTransformations()).toEqual([]); + // Same contract as the transformation column: a synthetic role node stays + // assignable while it is assigned. + expect(service.algorithmY().map((v) => v.code)).toEqual(['kmeans_cluster']); + expect(service.algorithmAssignableVariables().map((v) => v.code)).toContain('kmeans_cluster'); + }); + + it('accepts a fit-mode creator (variables + k, no recipe) for applied and request wiring', () => { + const fitCreator = { + new_column_names: ['km_fit'] as [string], + variables: ['a', 'b'], + k: 3, + enumeration_prefix: 'cluster_', + }; + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'a', label: 'A' }, { code: 'b', label: 'B' }]); + setCreators(service, 'kmeans_cluster_creator', [fitCreator]); + service.setAlgorithmY([{ code: 'km_fit', label: 'km_fit', isCreatedColumn: true }]); + + const body = service.buildRequestBody('mock_algo'); + expect(body.analysis.inputdata.variables).toEqual(['a', 'b']); + const steps = body.analysis.preprocessing as AnalysisPreprocessingStep[]; + expect(steps.map((step) => step.name)).toEqual(['missing_values_handler', 'kmeans_cluster_creator']); + expect(steps[1].parameters['variables']).toEqual(['a', 'b']); + expect(steps[1].parameters).not.toHaveProperty('reusable_preprocessing'); + }); + + it('sends the cluster variables as source CDEs and the cluster step after missing value handling', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'a', label: 'A' }, { code: 'b', label: 'B' }]); + service.setAppliedDescriptivePreprocessing({ + missing_values_handler: { strategies: { a: 'mean' } }, + }); + setCreators(service, 'kmeans_cluster_creator', [clusterFixture(['a', 'b'])]); + service.setAlgorithmY([{ code: 'kmeans_cluster', label: 'kmeans_cluster', isCreatedColumn: true }]); + + const body = service.buildRequestBody('mock_algo'); + + expect(body.analysis.algorithm.y).toEqual(['kmeans_cluster']); + expect(body.analysis.inputdata.variables).toEqual(['a', 'b']); + expect(body.analysis.inputdata.variables).not.toContain('kmeans_cluster'); + + const names = stepNames(body); + expect(names.indexOf('missing_values_handler')) + .toBeLessThan(names.indexOf('kmeans_cluster_creator')); + + const missingStep = (body.analysis.preprocessing as AnalysisPreprocessingStep[]) + .find((step) => step.name === 'missing_values_handler'); + // The user's own strategy for 'a' survives; 'b' gets the required drop. + expect(missingStep?.parameters['strategies']).toEqual({ a: 'mean', b: 'drop' }); + + // The request-only adjustments never reach the store. + expect(service.appliedPreprocessingConfig()?.['missing_values_handler']) + .toEqual({ strategies: { a: 'mean' } }); + }); + + it('adds a missing values handler for the cluster variables when none is set', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'a', label: 'A' }, { code: 'b', label: 'B' }]); + setCreators(service, 'kmeans_cluster_creator', [clusterFixture(['a', 'b'])]); + service.setAlgorithmY([{ code: 'kmeans_cluster', label: 'kmeans_cluster', isCreatedColumn: true }]); + + const body = service.buildRequestBody('mock_algo'); + + const steps = body.analysis.preprocessing as AnalysisPreprocessingStep[]; + expect(steps.map((step) => step.name)).toEqual(['missing_values_handler', 'kmeans_cluster_creator']); + expect(steps[0].parameters['strategies']).toEqual({ a: 'drop', b: 'drop' }); + expect(steps[1].parameters['new_column_names']).toEqual(['kmeans_cluster']); + expect(steps[1].parameters['k']).toBe(2); + expect(steps[1].parameters['enumeration_prefix']).toBe('cluster_'); + }); + + it('summarizes a categorical column as its categories and default', () => { + expect(service.formatPreprocessingEntries({ + categorical_column_creator: { + new_column_names: ['age_band'], + rules: { + under_60: { + condition: 'AND', + rules: [{ id: 'age', field: 'age', type: 'integer', input: 'number', operator: 'less', value: 60 }], + }, + }, + default_enumeration: '60_plus', + }, + }, { age: 'Age' })).toContainEqual(expect.objectContaining({ + label: 'Nominal column', + value: 'age_band; under_60: Age < 60; else 60_plus', + })); + }); + + it('labels the enumeration values of a categorical column rule', () => { + expect(service.formatPreprocessingEntries({ + categorical_column_creator: { + new_column_names: ['territory'], + rules: { + ACS: { id: 'clinical_sdr', field: 'clinical_sdr', operator: 'in', value: ['1', '2'], type: 'string' }, + }, + }, + }, { clinical_sdr: 'Clinical syndrome' }, { clinical_sdr: { 1: 'TACS', 2: 'PACS' } })).toContainEqual(expect.objectContaining({ + value: 'territory; ACS: Clinical syndrome IN TACS, PACS', + })); + }); + + it('summarizes an applied cluster creator as the cluster column it writes', () => { + expect(service.formatPreprocessingEntries({ + kmeans_cluster_creator: { new_column_names: ['kmeans_cluster'], k: 1, enumeration_prefix: 'cluster_', reusable_preprocessing: {} }, + })).toContainEqual(expect.objectContaining({ + label: 'K-means cluster column', + value: 'kmeans_cluster', + })); + }); + + it('lists the variables an applied cluster column was clustered on', () => { + expect(service.formatPreprocessingEntries({ + kmeans_cluster_creator: { + new_column_names: ['kmeans_cluster'], + k: 1, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { cluster_variables: ['age', 'nihss_24h'] }, + }, + }, { age: 'Age', nihss_24h: '24h score' })).toContainEqual(expect.objectContaining({ + label: 'K-means cluster column', + lines: ['kmeans_cluster', 'Clustered on Age, 24h score'], + })); + }); + + it('lists fit-mode variables when summarizing a cluster column', () => { + expect(service.formatPreprocessingEntries({ + kmeans_cluster_creator: { + new_column_names: ['km_fit'], + variables: ['age', 'bmi'], + k: 2, + enumeration_prefix: 'cluster_', + }, + }, { age: 'Age', bmi: 'BMI' })).toContainEqual(expect.objectContaining({ + label: 'K-means cluster column', + lines: ['km_fit', 'Clustered on Age, BMI'], + })); + }); + + it('summarizes every repeated creator of a run, its PCA columns and their drops', () => { + service.upsertTransformation({ + id: 'km1', + name: 'kmeans_cluster_creator', + parameters: { new_column_names: ['km_a'], variables: ['age'], k: 2, enumeration_prefix: 'cluster_' }, + }); + service.upsertTransformation({ + id: 'km2', + name: 'kmeans_cluster_creator', + parameters: { new_column_names: ['km_b'], variables: ['age'], k: 3, enumeration_prefix: 'cluster_' }, + }); + service.upsertTransformation({ + id: 'pca', + name: 'pca_column_creator', + parameters: { new_column_names: ['pca_pc1'], selected_components: ['PC1'], n_components: 1, variables: ['age', 'bmi'] }, + }); + + const entries = service.getEffectivePreprocessingEntries('mock_algo', { age: 'Age', bmi: 'BMI' }); + + expect(entries.filter((entry) => entry.label === 'K-means cluster column').map((entry) => entry.lines?.[0])) + .toEqual(['km_a', 'km_b']); + expect(entries).toContainEqual(expect.objectContaining({ + label: 'PCA component columns', + lines: ['pca_pc1', 'Components PC1 from Age, BMI'], + })); + expect(entries).toContainEqual(expect.objectContaining({ + label: 'Missing values', + lines: ['Age: remove rows', 'BMI: remove rows'], + })); + }); + + }); + + describe('loadKMeansReport', () => { + beforeEach(() => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age' }]); + }); + + it('sends spec defaults when the Algorithm step has no kmeans parameters', () => { + const request$ = service.loadKMeansReport(['age']); + expect(request$).toBeTruthy(); + request$!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm.name).toBe('kmeans'); + expect(req.request.body.analysis.algorithm.y).toEqual(['age']); + expect(req.request.body.analysis.algorithm.parameters).toEqual(expect.objectContaining({ + k_selection: 'manual', + k: 4, + maxiter: 100, + tol: 0.0001, + })); + expect(req.request.body.analysis.preprocessing).toEqual([ + { name: 'missing_values_handler', parameters: { strategies: { age: 'drop' } } }, + ]); + req.flush({ status: 'success', result: {} }); + }); + + it('adds missing_values_handler when a categorical transformation is applied', () => { + setCreators(service, 'categorical_column_creator', [{ + new_column_names: ['grp'], + rules: { A: { condition: 'AND', rules: [{ field: 'age', operator: 'less', value: 60 }] } }, + }]); + service.loadKMeansReport(['age'])!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + const steps = req.request.body.analysis.preprocessing as AnalysisPreprocessingStep[]; + expect(steps.map((step) => step.name)).toContain('missing_values_handler'); + expect(steps.find((step) => step.name === 'missing_values_handler')?.parameters['strategies']) + .toEqual({ age: 'drop' }); + expect(steps.find((step) => step.name === 'categorical_column_creator')?.parameters['new_column_names']) + .toEqual(['grp']); + req.flush({ status: 'success', result: {} }); + }); + + it('keeps an applied missing-value strategy for a clustering variable', () => { + service.setAppliedDescriptivePreprocessing({ + missing_values_handler: { strategies: { age: 'mean' } }, + }); + service.loadKMeansReport(['age'])!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + const missing = (req.request.body.analysis.preprocessing as AnalysisPreprocessingStep[]) + .find((step) => step.name === 'missing_values_handler'); + expect(missing?.parameters['strategies']).toEqual({ age: 'mean' }); + req.flush({ status: 'success', result: {} }); + }); + + it('lets an explicit parameter override the spec default', () => { + service.loadKMeansReport(['age'], { k: 3 })!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm.parameters.k).toBe(3); + expect(req.request.body.analysis.algorithm.parameters.k_selection).toBe('manual'); + req.flush({ status: 'success', result: {} }); + }); + + it('keeps the run step order when a creator reads an earlier creator column', () => { + const ageRule = { condition: 'AND', rules: [{ field: 'age', operator: 'less', value: 60 }] }; + const clusterRule = { condition: 'AND', rules: [{ field: 'km', operator: 'equal', value: 'cluster_1' }] }; + service.upsertTransformation({ + id: 'catA', + name: 'categorical_column_creator', + parameters: { new_column_names: ['grpA'], rules: { young: ageRule } }, + }); + service.upsertTransformation({ + id: 'km', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { cluster_variables: ['age'] }, + }, + }); + service.upsertTransformation({ + id: 'catB', + name: 'categorical_column_creator', + parameters: { new_column_names: ['grpB'], rules: { first: clusterRule } }, + }); + service.loadKMeansReport(['age'])!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + const steps = req.request.body.analysis.preprocessing as AnalysisPreprocessingStep[]; + expect(steps.map((step) => step.name)).toEqual([ + 'missing_values_handler', + 'categorical_column_creator', + 'kmeans_cluster_creator', + 'categorical_column_creator', + ]); + expect(steps[0].parameters['strategies']).toEqual({ age: 'drop' }); + req.flush({ status: 'success', result: {} }); + }); + + it('prefers Algorithm-step kmeans parameters over spec defaults', () => { + service.algorithmConfigurations.set({ + kmeans: { k_selection: 'elbow', k_min: 2, k_max: 5, maxiter: 50, tol: 0.01 }, + }); + service.loadKMeansReport(['age'])!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm.parameters).toEqual(expect.objectContaining({ + k_selection: 'elbow', + k_min: 2, + k_max: 5, + maxiter: 50, + tol: 0.01, + k: 4, + })); + req.flush({ status: 'success', result: {} }); + }); + }); + + describe('loadPcaReport', () => { + beforeEach(() => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setVariables([{ code: 'age', label: 'Age' }]); + }); + + it('previews pca on the chosen variables without saving an experiment', () => { + const request$ = service.loadPcaReport(['age', 'bmi']); + expect(request$).toBeTruthy(); + request$!.subscribe(); + + const req = httpMock.expectOne('/services/experiments/preview'); + expect(req.request.body.analysis.algorithm.name).toBe('pca'); + expect(req.request.body.analysis.algorithm.y).toEqual(['age', 'bmi']); + expect(req.request.body.analysis.preprocessing).toEqual([ + { name: 'missing_values_handler', parameters: { strategies: { age: 'drop', bmi: 'drop' } } }, + ]); + req.flush({ status: 'success', result: {} }); + }); + }); + + describe('ordered transformation list', () => { + const pcaAStep: any = { + id: 'pcaA', + name: 'pca_column_creator', + parameters: { + new_column_names: ['pca_a1', 'pca_a2'], + n_components: 2, + reusable_preprocessing: { base_parameters: { variables: ['lefthippocampus', 'righthippocampus'] } }, + }, + }; + const categoricalStep: any = { + id: 'cat', + name: 'categorical_column_creator', + parameters: { new_column_names: ['grp'], rules: {} }, + }; + const kmeansOnPcaA: any = { + id: 'kmeans', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { cluster_variables: ['pca_a1', 'pca_a2'] }, + }, + }; + + it('emits singletons first, then transformations in list order', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.setAppliedDescriptivePreprocessing({ + missing_values_handler: { strategies: { age: 'drop' } }, + }); + service.upsertTransformation(pcaAStep); + service.upsertTransformation(categoricalStep); + service.upsertTransformation(kmeansOnPcaA); + + const body = service.buildRequestBody('mock_algo'); + expect(body.analysis.preprocessing.map((s: AnalysisPreprocessingStep) => s.name)).toEqual([ + 'missing_values_handler', + 'pca_column_creator', + 'categorical_column_creator', + 'kmeans_cluster_creator', + ]); + }); + + it('adds drops and inputdata for CDE sources, never for created columns', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.upsertTransformation(pcaAStep); + service.upsertTransformation(kmeansOnPcaA); + + const body = service.buildRequestBody('mock_algo'); + expect(body.analysis.inputdata.variables).toEqual( + expect.arrayContaining(['lefthippocampus', 'righthippocampus']), + ); + // The K-means sources are the PCA outputs, so they are not re-sent as CDEs. + expect(body.analysis.inputdata.variables).not.toContain('pca_a1'); + const missing = body.analysis.preprocessing.find( + (s: AnalysisPreprocessingStep) => s.name === 'missing_values_handler', + ); + expect(Object.keys(missing!.parameters['strategies'] as object)).not.toContain('pca_a1'); + expect(missing!.parameters['strategies']).toEqual({ + lefthippocampus: 'drop', + righthippocampus: 'drop', + }); + }); + + it('describe preview for K-means-on-PCA keeps pca then kmeans and CDE-only sources', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.upsertTransformation(pcaAStep); + service.upsertTransformation(kmeansOnPcaA); + + const steps = service.buildTransformationDescribePreprocessing(null); + expect(steps?.map((step) => step.name)).toEqual([ + 'missing_values_handler', + 'pca_column_creator', + 'kmeans_cluster_creator', + ]); + const missing = steps?.find((step) => step.name === 'missing_values_handler'); + expect(Object.keys(missing!.parameters['strategies'] as object)).not.toContain('pca_a1'); + expect(Object.keys(missing!.parameters['strategies'] as object)).not.toContain('pca_a2'); + expect(service.transformationSourceCodesForInput()).toEqual([ + 'lefthippocampus', + 'righthippocampus', + ]); + + service.loadDescriptiveOverview(['km'], steps, service.transformationSourceCodesForInput()) + .subscribe(); + const req = httpMock.expectOne('/services/experiments/preview'); + const body = req.request.body; + expect(body.analysis.preprocessing.map((s: AnalysisPreprocessingStep) => s.name)).toEqual([ + 'missing_values_handler', + 'pca_column_creator', + 'kmeans_cluster_creator', + ]); + expect(body.analysis.inputdata.variables).toEqual( + expect.arrayContaining(['lefthippocampus', 'righthippocampus']), + ); + expect(body.analysis.inputdata.variables).not.toContain('pca_a1'); + expect(body.analysis.inputdata.variables).not.toContain('pca_a2'); + const previewMissing = body.analysis.preprocessing.find( + (s: AnalysisPreprocessingStep) => s.name === 'missing_values_handler', + ); + expect(Object.keys(previewMissing!.parameters['strategies'] as object)).not.toContain('pca_a1'); + req.flush({ status: 'success', result: { featurewise: [] } }); + }); + + it('reports dependents of a step', () => { + service.upsertTransformation(pcaAStep); + service.upsertTransformation(kmeansOnPcaA); + + expect(service.transformationDependents(pcaAStep.id).map((s) => s.id)).toEqual([kmeansOnPcaA.id]); + expect(service.transformationDependents(kmeansOnPcaA.id)).toEqual([]); + }); + + it('upsert replaces by id in place and remove drops by id', () => { + service.upsertTransformation(pcaAStep); + service.upsertTransformation(kmeansOnPcaA); + expect(service.appliedTransformations().map((s) => s.id)).toEqual(['pcaA', 'kmeans']); + + // Re-upserting the same id with new parameters replaces it IN PLACE; it keeps its index. + service.upsertTransformation({ ...pcaAStep, parameters: { ...pcaAStep.parameters, n_components: 3 } }); + expect(service.appliedTransformations().map((s) => s.id)).toEqual(['pcaA', 'kmeans']); + + service.removeTransformation('kmeans'); + expect(service.appliedTransformations().map((s) => s.id)).toEqual(['pcaA']); + }); + + it('upsert keeps two same-name steps when their ids differ', () => { + const pcaBStep = { + ...pcaAStep, + id: 'pcaB', + parameters: { + ...pcaAStep.parameters, + new_column_names: ['pca_b1', 'pca_b2'], + }, + }; + service.upsertTransformation(pcaAStep); + service.upsertTransformation(pcaBStep); + expect(service.appliedTransformations().map((s) => s.id)).toEqual(['pcaA', 'pcaB']); + expect(service.appliedTransformations().map((s) => s.name)).toEqual([ + 'pca_column_creator', + 'pca_column_creator', + ]); + }); + + it('leaves out the steps that read a column of a step the deployment cannot run', () => { + service.selectedDataModel.set(mockDataModel); + service.setSelectedDatasets(['ds1']); + service.upsertTransformation(pcaAStep); + service.upsertTransformation({ + id: 'cat-on-pca', + name: 'categorical_column_creator', + parameters: { + new_column_names: ['grp_pca'], + rules: { high: { condition: 'AND', rules: [{ field: 'pca_a1', operator: 'greater', value: 0 }] } }, + }, + }); + service.upsertTransformation(categoricalStep); + // A catalog without pca_column_creator: the aggregation server is not deployed. + service.loadBackendAlgorithms().subscribe(); + flushSpecifications( + [toAlgorithmSpec(mockRawAlgo)], + [{ name: 'categorical_column_creator', label: '', desc: '', parameters: {} }], + ); + + const body = service.buildRequestBody('mock_algo'); + const creators = (body.analysis.preprocessing as AnalysisPreprocessingStep[]) + .filter((step) => step.name !== 'missing_values_handler') + .map((step) => step.parameters['new_column_names']); + expect(creators).toEqual([['grp']]); + }); + + it('types PCA columns numerical before the preprocessing catalog has loaded', () => { + service.upsertTransformation({ + id: 'pca-x', + name: 'pca_column_creator', + parameters: { new_column_names: ['pca_pc1'], selected_components: ['PC1'], n_components: 1, variables: ['a', 'b'] }, + }); + + const node = service.algorithmAssignableVariables().find((v) => v.code === 'pca_pc1'); + expect(node?.type).toBe('real'); + }); + + it('clears every transformation step in one write', () => { + service.upsertTransformation(categoricalStep); + service.upsertTransformation(pcaAStep); + + service.clearTransformations(); + + expect(service.appliedTransformations()).toEqual([]); + }); + + it('keeps a re-edited categorical step before a dependent K-means step', () => { + // A K-means step that clusters on the categorical output ('grp') so it is a real dependent. + const kmeansOnCat: any = { + id: 'kmeans-cat', + name: 'kmeans_cluster_creator', + parameters: { + new_column_names: ['km'], + k: 2, + enumeration_prefix: 'cluster_', + reusable_preprocessing: { cluster_variables: ['grp'] }, + }, + }; + service.upsertTransformation(categoricalStep); + service.upsertTransformation(kmeansOnCat); + expect(service.appliedTransformations().map((s) => s.id)).toEqual(['cat', 'kmeans-cat']); + + // Re-editing the categorical step (keeping its output 'grp') must not relocate it + // after the K-means step. + service.upsertTransformation({ + ...categoricalStep, + parameters: { ...categoricalStep.parameters, default_enumeration: 'other_edited' }, + }); + expect(service.appliedTransformations().map((s) => s.id)).toEqual(['cat', 'kmeans-cat']); + + // The K-means step still depends on the categorical output at its original index. + expect(service.transformationDependents(categoricalStep.id).map((s) => s.id)).toEqual(['kmeans-cat']); + }); + + it('refuses an upsert that drops a column a later step reads', () => { + service.upsertTransformation(pcaAStep); + service.upsertTransformation(kmeansOnPcaA); + + const accepted = service.upsertTransformation({ + ...pcaAStep, + parameters: { ...pcaAStep.parameters, new_column_names: ['pca_a1'], n_components: 1 }, + }); + + expect(accepted).toBe(false); + expect(service.appliedTransformations()[0].parameters['new_column_names']).toEqual(['pca_a1', 'pca_a2']); + expect(service.outputsInUse('pcaA', ['pca_a1'])).toEqual(['pca_a2']); + }); + + it('writes nothing when an upsert repeats the stored step', () => { + service.upsertTransformation(pcaAStep); + const before = service.appliedTransformations(); + + expect(service.upsertTransformation({ ...pcaAStep })).toBe(true); + expect(service.appliedTransformations()).toBe(before); + }); + }); }); diff --git a/src/app/services/experiment-studio.service.ts b/src/app/services/experiment-studio.service.ts index 92e0e46da..7345b7c6f 100644 --- a/src/app/services/experiment-studio.service.ts +++ b/src/app/services/experiment-studio.service.ts @@ -1,18 +1,82 @@ -import { Injectable, computed, effect, inject, signal } from '@angular/core'; +import { Injectable, computed, effect, inject, signal, untracked } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { Observable, Subject, catchError, defaultIfEmpty, filter, map, of, switchMap, take, takeUntil, tap, timer } from 'rxjs'; +import { Observable, Subject, catchError, finalize, forkJoin, map, of, shareReplay, takeUntil, tap } from 'rxjs'; import { SessionStorageService } from './session-storage.service'; import { D3HierarchyNode, DataModel, Group, Variable } from '../models/data-model.interface'; -import { mapRawAlgorithmToAlgorithmConfig } from '../core/algorithm-mappers'; +import { mapSpecificationsToAlgorithmConfigs } from '../core/algorithm-mappers'; import { EnumMaps } from '../core/algorithm-result-enum-mapper'; -import { RawAlgorithmDefinition } from '../models/backend-algorithms.model'; -import { BackendFilter } from '../models/filters.model'; -import { AlgorithmConfig } from '../models/algorithm-definition.model'; +import { + AnalysisInputData, + AnalysisPreprocessingStep, + AnalysisRequest, + ExperimentCreateRequest, + RawPreprocessingStep, +} from '../models/backend-algorithms.model'; +import { BackendFilter, BackendRule } from '../models/filters.model'; +import { ExperimentRunSetup, RunSetupSummaryRow } from '../models/experiment-run-setup.model'; +import { AlgorithmAvailability, AlgorithmConfig } from '../models/algorithm-definition.model'; import { BackendExperiment } from '../models/backend-experiment.model'; import { ErrorService } from './error.service'; import { AlgorithmRulesService } from './algorithm-rules.service'; -import { AlgorithmNames, VariableTypes } from '../core/constants/algorithm.constants'; +import { AlgorithmNames, HistogramBinningType, VariableTypes } from '../core/constants/algorithm.constants'; +import { + omitEmptyOptionalParameters, + parameterDefaultsFromSchema, + serializeAlgorithmParameterValue, +} from '../core/algorithm-parameter.utils'; +import { formatFilterExpression } from '../core/filter-display.utils'; +import { outlierStrategyLabel, outlierTailLabel } from '../core/outlier-rules'; +import { + buildEnumMapForVariables, + findDataModelByCodeVersion, + isCategoricalVariableType, + isFreeTextCde, + specificationAcceptsVariableType, +} from '../core/data-model.utils'; +import { + UnavailableVariablesMap, + collectVariableCodes, + computeUnavailableVariables, +} from '../core/variable-availability.utils'; +import { + KMEANS_CLUSTER_CREATOR, + kmeansCreatorVariables, +} from '../core/kmeans-cluster-source.utils'; +import { resolvePreprocessingOutputs } from '../core/preprocessing-outputs.utils'; +import { + CREATOR_NAMES, + CreatorName, + PreprocessingConfig, + stepOutputNames, + stepSourceVariables, + splitPreprocessingSteps, + TransformationStep, +} from '../core/transformation-steps.utils'; + +export type { PreprocessingConfig }; + +type PathologyAccessWarningKind = 'no-pathologies' | 'no-access'; + +interface PathologyAccessWarning { + kind: PathologyAccessWarningKind; + title: string; + message: string; +} +const MISSING_VALUES_HANDLER = 'missing_values_handler'; +const OUTLIER_WINSORIZER = 'outlier_winsorizer'; +const APPLIED_DESCRIPTIVE_PREPROCESSING = '__applied_descriptive_preprocessing__'; +/** Session-storage key for the ordered column-creating transformation steps. */ +const APPLIED_TRANSFORMATIONS = '__applied_transformations__'; + +/** Quick-preview / diagnostic algorithms hidden from the experiment algorithm picker. */ +const ALGORITHM_PANEL_EXCLUDED = new Set<string>([ + AlgorithmNames.HISTOGRAM, + AlgorithmNames.DESCRIBE, + AlgorithmNames.OUTLIER_REPORT, + AlgorithmNames.LINEAR_SVM, + 'cox_regression_stacked', +]); @Injectable({ providedIn: 'root' }) export class ExperimentStudioService { @@ -25,58 +89,106 @@ export class ExperimentStudioService { private experimentUrl = '/services/experiments'; private dataModels: any[] = []; private dataModelsLoaded = false; + private dataModelsRequest$: Observable<any[]> | null = null; private selectedVariablesSignal = signal<any[]>(this.sessionStorage.getItem('selectedVariables') || []); - private selectedCovariatesSignal = signal<any[]>(this.sessionStorage.getItem('selectedCovariates') || []); private selectedFiltersSignal = signal<any[]>(this.sessionStorage.getItem('selectedFilters') || []); + private _filterLogic = signal<BackendFilter | null>(this.sessionStorage.getItem('filterLogic')); + private algorithmYSignal = signal<any[]>(this.sessionStorage.getItem('algorithmY') || []); + private algorithmXSignal = signal<any[]>(this.sessionStorage.getItem('algorithmX') || []); readonly selectedVariables = computed(() => this.selectedVariablesSignal()); - readonly selectedCovariates = computed(() => this.selectedCovariatesSignal()); readonly selectedFilters = computed(() => this.selectedFiltersSignal()); + /** Variables assigned to the y role on the Algorithm panel. */ + readonly algorithmY = computed(() => this.withLiveCreatedColumns(this.algorithmYSignal())); + /** Covariates assigned to the x role on the Algorithm panel. */ + readonly algorithmX = computed(() => this.withLiveCreatedColumns(this.algorithmXSignal())); + + /** + * A created-column role node is a snapshot (a saved experiment hydrates it with no + * categories); swap in the live node so its categories follow the creator config. + */ + private withLiveCreatedColumns(nodes: any[]): any[] { + const live = new Map(this.transformationColumnNodes().map((node) => [node.code, node])); + return nodes.map((node) => (node?.isCreatedColumn && live.get(node.code)) || node); + } + + /** + * Assignable pool for the Algorithm role UI: the data-model variable pool, the + * synthetic node for the applied transformation column, plus any synthetic + * derived role nodes hydrated from a saved experiment. Synthetic nodes never + * belong in the variables-panel pool (real CDE nodes only). + */ + readonly algorithmAssignableVariables = computed(() => { + const nodes = this.selectedVariables().filter((v) => !isFreeTextCde(v)); + const created = this.transformationColumnNodes(); + if (created.length) { + const knownCodes = new Set(nodes.map((v) => v?.code)); + for (const node of created) { + if (node?.code && !knownCodes.has(node.code)) { + nodes.push(node); + knownCodes.add(node.code); + } + } + } else { + // No live transformation creator: re-include synthetic derived nodes that + // were hydrated into the y/x roles (e.g. editing a saved experiment that + // has a created column but no active transformation step). Synthetic nodes + // never belong in the variables-panel pool (real CDE nodes only). + const knownCodes = new Set(nodes.map((v) => v?.code)); + for (const role of [this.algorithmY(), this.algorithmX()]) { + for (const node of role) { + if (node?.isCreatedColumn && !knownCodes.has(node.code)) { + nodes.push(node); + knownCodes.add(node.code); + } + } + } + } + return nodes; + }); getCategoricalEnumMaps(): EnumMaps { const items = [ - ...this.selectedVariables(), - ...this.selectedCovariates(), + ...this.algorithmAssignableVariables(), ...this.selectedFilters(), ]; - - const maps: EnumMaps = {}; - - items.forEach((item) => { - const enums = Array.isArray(item?.enumerations) ? item.enumerations : []; - if (!enums.length) return; - - const code = String(item?.code ?? ''); - if (!code) return; - - const enumMap: Record<string, string> = {}; - enums.forEach((e: any) => { - const raw = e?.code ?? e?.label ?? e?.name; - if (raw === null || raw === undefined) return; - const key = String(raw); - const label = e?.label ?? e?.name ?? String(raw); - enumMap[key] = label; - }); - - if (Object.keys(enumMap).length > 0) { - maps[code] = enumMap; - } - }); - - return maps; + return buildEnumMapForVariables(items); } private selectedDatasetsSignal = signal<string[]>(this.sessionStorage.getItem('selectedDatasets') || []); - private transientUrl = '/services/experiments/transient'; + /** Stateless previews: computed and returned, nothing kept. */ + private previewUrl = '/services/experiments/preview'; + /** Studio runs: the backend keeps a successful result here so saving never re-executes it. */ + private runsUrl = '/services/experiments/runs'; - lastUsedAlgorithm = signal<string | null>(null); + lastUsedAlgorithm = signal<string | null>(this.sessionStorage.getItem('lastUsedAlgorithm') || null); selectedDatasets = computed(() => this.selectedDatasetsSignal()); backendAlgorithms = signal<Record<string, AlgorithmConfig>>({}); + /** Preprocessing step name → its specification (parameters schema + outputs), from the catalog. */ + private preprocessingSpecs = signal<Record<string, RawPreprocessingStep>>({}); + /** True once `/services/specifications/preprocessing` has been fetched successfully. */ + private preprocessingCatalogLoaded = signal(false); selectedDataModel = signal<DataModel | null>(this.sessionStorage.getItem('selectedDataModel')); readonly crossSectionalModels = signal<DataModel[]>([]); readonly longitudinalModels = signal<DataModel[]>([]); readonly availableDatasets = signal<{ code: string; label: string }[]>([]); + /** + * Variable code → labels of the selected datasets that lack it, from the data model's + * `datasetsVariables`. Views gray these out and refuse to select them; nothing already + * in the pool is removed when a dataset change makes a variable unavailable. + */ + readonly unavailableVariables = computed<UnavailableVariablesMap>(() => { + const model = this.selectedDataModel(); + return computeUnavailableVariables( + model?.datasetsVariables, + this.selectedDatasets(), + collectVariableCodes(model), + this.availableDatasets() + ); + }); + private pathologyAccessWarningSignal = signal<PathologyAccessWarning | null>(null); + readonly pathologyAccessWarning = this.pathologyAccessWarningSignal.asReadonly(); private currentExperimentUUIDSignal = signal<string | null>(null); readonly currentExperimentUUID = this.currentExperimentUUIDSignal.asReadonly(); @@ -88,7 +200,78 @@ export class ExperimentStudioService { private readonly _isRunning = signal(false); readonly isRunning = this._isRunning.asReadonly(); - readonly isFilterConfigOpen = signal<boolean>(false); + + private readonly _isSaving = signal(false); + readonly isSaving = this._isSaving.asReadonly(); + + /** + * Run outcome for the Execution Results step. Lives here (not on the algorithm panel) + * because the result outlives the parameters view and is read from another step. + */ + readonly runResult = signal<any | null>(null); + /** + * Backend id of the kept run that produced `runResult`. Saving persists that run as it is, + * so the saved experiment is exactly the reviewed result and nothing is executed again. + */ + readonly runId = signal<string | null>(null); + readonly runError = signal<string | null>(null); + readonly lastRunSchema = signal<any[]>([]); + /** Execution unlocks the first time a run is dispatched in this studio session. */ + readonly hasRunStarted = signal(false); + readonly runStatusText = signal('Processing experiment...'); + /** + * Frozen inputs of the run that produced `runResult`; null until a run is dispatched. + * Read this instead of live state: editing a parameter does not clear a result that is + * already on screen, so live state can describe a run that never happened. Not persisted, + * exactly like `runResult`. + */ + readonly runSetup = signal<ExperimentRunSetup | null>(null); + /** Save As toast host: rendered by the Execution step, triggered from anywhere. */ + readonly saveSucceeded = signal(false); + /** Why the last save failed, shown next to the Save As form; null while none failed. */ + readonly saveError = signal<string | null>(null); + + notifySaveSucceeded(): void { + this.saveError.set(null); + this.saveSucceeded.set(true); + setTimeout(() => this.saveSucceeded.set(false), 8000); + } + + /** code → human label for every variable the algorithm can see, shared by result views. */ + readonly variableLabelMap = computed(() => { + const map: Record<string, string> = {}; + [...this.algorithmAssignableVariables(), ...this.selectedFilters()].forEach((item) => { + if (item?.code && item?.label) map[item.code] = item.label; + }); + return map; + }); + private dataExclusionWarningsSignal = signal<string[]>([]); + readonly dataExclusionWarnings = this.dataExclusionWarningsSignal.asReadonly(); + private excludedDatasetsSignal = signal<string[]>([]); + readonly excludedDatasets = this.excludedDatasetsSignal.asReadonly(); + + /** + * Freezes what the next run will send. The algorithm panel calls this once the final + * parameter values are stored, so datasets, filters, roles, preprocessing and parameters + * are all read from the same instant the request is built from. + */ + captureRunSetup(algorithmName: string): void { + const configuredParameters = this.algorithmConfigurations()[algorithmName] ?? {}; + this.runSetup.set({ + algorithmKey: algorithmName, + dataModel: this.selectedDataModel()?.label ?? this.selectedDataModel()?.code ?? null, + datasets: [...this.selectedDatasets()], + outcome: this.roleCodes(this.algorithmY()), + covariates: this.roleCodes(this.algorithmX()), + filterLogic: this.filterLogic(), + preprocessing: this.getEffectivePreprocessingEntries(algorithmName, this.variableLabelMap(), this.getCategoricalEnumMaps()), + parameters: { ...configuredParameters }, + }); + } + + private roleCodes(nodes: any[]): string[] { + return (nodes ?? []).map((node) => String(node?.code ?? '').trim()).filter(Boolean); + } // teardown for transient requests private destroy$ = new Subject<void>(); @@ -107,20 +290,14 @@ export class ExperimentStudioService { return acc; }, {} as Record<string, string>); - private readonly transformationVariants: Record<string, string> = { - pca: 'pca_with_transformation', - }; + /** The specifications request, made once. Edit hydration waits on it before it resolves the saved algorithm. */ + private readonly catalogLoaded$ = this.loadBackendAlgorithms().pipe(shareReplay(1)); + + constructor() { + this.catalogLoaded$.subscribe(); - private readonly transformationBases: Record<string, string> = Object.entries( - this.transformationVariants - ).reduce((acc, [base, variant]) => { - acc[variant] = base; - return acc; - }, {} as Record<string, string>); - constructor() { - this.loadBackendAlgorithms().subscribe(); // Reset filters when no filter variables OR no rules effect(() => { @@ -135,66 +312,178 @@ export class ExperimentStudioService { this._filterLogic.set(null); } } - }, { allowSignalWrites: true }); + }); effect(() => { const selected = this.selectedDatasetsSignal(); if (!selected) return; + // untracked: setVariables/prune used to write new y/x arrays and retrigger this effect. if (selected.length === 0) { - console.warn('No datasets selected — resetting state'); - this.setVariables([]); - this.setCovariates([]); - this.setFilters([]); + untracked(() => { + if (!this.selectedVariablesSignal().length && !this.selectedFiltersSignal().length) { + return; + } + console.warn('No datasets selected — resetting state'); + this.setVariables([]); + this.setFilters([]); + }); } else { - this.refreshDataModel(); + untracked(() => this.refreshDataModel()); } - }, { allowSignalWrites: true }); + }); - // Auto-deselect algorithm when it becomes unavailable due to variable/covariate changes - effect(() => { - const currentAlgo = this.selectedAlgorithm(); - if (!currentAlgo) return; + // --- State Persistence --- + const persistencePairs: Array<[string, () => unknown]> = [ + ['selectedVariables', () => this.selectedVariablesSignal()], + ['selectedFilters', () => this.selectedFiltersSignal()], + ['selectedDatasets', () => this.selectedDatasetsSignal()], + ['selectedDataModel', () => this.selectedDataModel()], + ['filterLogic', () => this._filterLogic()], + ['algorithmY', () => this.algorithmYSignal()], + ['algorithmX', () => this.algorithmXSignal()], + ['algorithmConfigurations', () => this.algorithmConfigurations()], + ['algorithmPreprocessingConfigurations', () => this.algorithmPreprocessingConfigurations()], + ['lastUsedAlgorithm', () => this.lastUsedAlgorithm()], + ]; + for (const [key, read] of persistencePairs) { + effect(() => { + this.sessionStorage.setItem(key, read()); + }); + } + } + setAlgorithmY(nodes: any[]): void { + this.setRole('y', nodes); + } - // Read these to establish reactive dependencies - const _variables = this.selectedVariables(); - const _covariates = this.selectedCovariates(); + setAlgorithmX(nodes: any[]): void { + this.setRole('x', nodes); + } - // Check if the currently selected algorithm is still available - const isStillAvailable = this.isAlgorithmAvailable(currentAlgo.name); + private setRole(role: 'y' | 'x', nodes: any[]): void { + const unique = this.uniqueByCode(nodes); + const thisSignal = role === 'y' ? this.algorithmYSignal : this.algorithmXSignal; + const otherSignal = role === 'y' ? this.algorithmXSignal : this.algorithmYSignal; + thisSignal.set(unique); + const codes = new Set(unique.map((v) => v.code)); + otherSignal.set(otherSignal().filter((v) => !codes.has(v.code))); + } - if (!isStillAvailable) { - console.log( - `[ExperimentStudioService] Algorithm "${currentAlgo.name}" is no longer available — deselecting.` - ); - this.clearSelectedAlgorithm(); - } - }, { allowSignalWrites: true }); + private uniqueByCode(nodes: any[]): any[] { + const seen = new Set<string>(); + const out: any[] = []; + for (const node of nodes ?? []) { + const code = node?.code; + if (code === null || code === undefined || code === '') continue; + const key = String(code); + if (seen.has(key)) continue; + seen.add(key); + out.push(node); + } + return out; + } - // --- State Persistence --- - effect(() => { - this.sessionStorage.setItem('selectedVariables', this.selectedVariablesSignal()); - }); + /** Codes of derived columns (e.g. transformation output) that must not be sent as source CDEs. */ + private derivedVariableCodes(): Set<string> { + const codes = new Set<string>(); + for (const node of this.transformationColumnNodes()) { + if (node?.code) codes.add(String(node.code)); + } + return codes; + } - effect(() => { - this.sessionStorage.setItem('selectedCovariates', this.selectedCovariatesSignal()); - }); + /** Minimal synthetic node for a derived column code found in a saved experiment. */ + private syntheticDerivedNode(code: string): any { + return { + code, + label: code, + name: code, + type: 'nominal', + enumerations: [], + isCreatedColumn: true, + }; + } - effect(() => { - this.sessionStorage.setItem('selectedFilters', this.selectedFiltersSignal()); - }); + /** + * Collapse created-column enumerations to their backend-stored shape: Exaflow keeps them as a + * `{code: code}` map, so a rule key equal to the default enumeration appears once. + */ + private static dedupeEnumerations(enumerations: { code: string; label: string }[]): { code: string; label: string }[] { + const seen = new Set<string>(); + const out: { code: string; label: string }[] = []; + for (const entry of enumerations) { + const code = String(entry?.code ?? ''); + if (!code || seen.has(code)) continue; + seen.add(code); + out.push(entry); + } + return out; + } - effect(() => { - this.sessionStorage.setItem('selectedDatasets', this.selectedDatasetsSignal()); - }); + /** + * Synthetic node for every applied transformation column, exposed in the assignable pool. + * The created column codes always come from `new_column_names` (so they are detected as + * derived even before the catalog specs load); the category enumerations come from the generic + * `resolvePreprocessingOutputs` (Task 1) when its spec outputs are available. + */ + private transformationColumnNodes(): any[] { + const nodes: any[] = []; + for (const step of this.requestableTransformations()) { + const spec = this.preprocessingSpecs()[step.name]; + const resolved = spec?.outputs + ? resolvePreprocessingOutputs(spec.outputs, step.parameters) + : null; + const names = stepOutputNames(step); + for (let i = 0; i < names.length; i++) { + const column = resolved?.[i]; + // Exaflow stores a created column's enumerations as a `{code: code}` map, so a + // rule key equal to the default enumeration collapses to one category. Dedupe here + // to match the backend's stored shape. + nodes.push({ + code: names[i], + label: names[i], + name: names[i], + // Without a resolved spec (catalog pending or failed) PCA scores are still numerical. + type: (column ? column.stattype === 'numerical' : step.name === 'pca_column_creator') ? 'real' : 'nominal', + enumerations: ExperimentStudioService.dedupeEnumerations(column?.enumerations ?? []), + isCreatedColumn: true, + }); + } + } + return nodes; + } - effect(() => { - this.sessionStorage.setItem('selectedDataModel', this.selectedDataModel()); + /** + * Transformation steps that reach the next request: carrying at least one output name, and + * (once the catalog has loaded) present in `/services/specifications/preprocessing`. + * Incomplete drafts and unavailable creators stay in the store for the stage UI. A step left + * out takes its columns with it, so later steps that read them are left out too. + */ + private requestableTransformations(): TransformationStep[] { + const specs = this.preprocessingSpecs(); + const catalogLoaded = this.preprocessingCatalogLoaded(); + const leftOut = new Set<string>(); + return this.appliedTransformationsSignal().filter((step) => { + const outputs = stepOutputNames(step); + const sent = outputs.length > 0 + && (!catalogLoaded || !!specs[step.name]) + && !stepSourceVariables(step).some((code) => leftOut.has(code)); + if (!sent) outputs.forEach((name) => leftOut.add(name)); + return sent; }); } - setSelectedDataModel(model: DataModel | null): void { - this.selectedDataModel.set(model); + /** Datasets the next analysis request sends: the selection minus excluded datasets. */ + requestDatasets(): string[] { + return this.selectedDatasetsSignal().filter((ds) => !this.excludedDatasetsSignal().includes(ds)); + } + + /** Cohort filters the next analysis request sends, or null when there are no rules. */ + requestFilters(filterOverride?: BackendFilter | null): BackendFilter | null { + const filterLogic = this.resolveFilterPayload(filterOverride); + return filterLogic && Array.isArray(filterLogic.rules) && filterLogic.rules.length > 0 + ? filterLogic + : null; } getActiveDataModelCode(): string { @@ -215,25 +504,16 @@ export class ExperimentStudioService { .subscribe(models => { const active = models.filter(m => selected.includes(m.code)); if (!active.length) return; - const model = active[0]; this.selectedDataModel.set(model); - const converted = this.convertToD3Hierarchy(model); - - // enrich variables - const enrichedVariables = converted.allVariables.map(v => ({ - ...v, - supportedAlgos: this.algorithmEnabled(v.type ?? 'unknown') - })); - - this.selectedDataModel.set(model); }); } setSelectedDatasets(datasets: string[]) { this.selectedDatasetsSignal.set(datasets); + this.clearDataExclusionWarnings(); } algorithmEnabled(variableType: string): string[] { @@ -251,13 +531,13 @@ export class ExperimentStudioService { return false; } - const varIsNominal = varTypes.includes(VariableTypes.NOMINAL); + const varIsNominal = varTypes.some(isCategoricalVariableType); if (varIsNominal && yReq.stattypes?.includes(VariableTypes.NOMINAL)) { return true; } - const yExists = varTypes.some(t => yReq.types.includes(t)); + const yExists = varTypes.some(t => specificationAcceptsVariableType(yReq.types, t)); if (!yExists) { return false; @@ -268,7 +548,7 @@ export class ExperimentStudioService { return false; } - const xExists = varTypes.some(t => xReq.types.includes(t)); + const xExists = varTypes.some(t => specificationAcceptsVariableType(xReq.types, t)); if (!xExists) { return false; } @@ -304,19 +584,59 @@ export class ExperimentStudioService { // update signal this.selectedVariablesSignal.set([...currentVars, enrichedNode]); + this.pruneRolesToAssignable(); } setVariables(vars: D3HierarchyNode[]) { this.selectedVariablesSignal.set(vars); + this.pruneRolesToAssignable(); + this.clearDataExclusionWarnings(); } - setCovariates(covs: D3HierarchyNode[]): void { - this.selectedCovariatesSignal.set(covs); - } - setFilters(filters: D3HierarchyNode[]): void { this.selectedFiltersSignal.set(filters); + this.clearDataExclusionWarnings(); + } + + /** Pathology/data-model change invalidates variables, datasets, and review state. */ + clearSelectionsForDataModelChange(): void { + this.setVariables([]); + this.setFilters([]); + this.setSelectedDatasets([]); + this.setFilterLogic(null); + } + + /** Keep y/x roles limited to currently-assignable items (pool + created column). */ + private pruneRolesToAssignable(): void { + untracked(() => { + const assignable = new Set(this.algorithmAssignableVariables().map((v) => v.code)); + const y = this.algorithmYSignal(); + const nextY = y.filter((v) => assignable.has(v.code)); + if (nextY.length !== y.length) { + this.algorithmYSignal.set(nextY); + } + const x = this.algorithmXSignal(); + const nextX = x.filter((v) => assignable.has(v.code)); + if (nextX.length !== x.length) { + this.algorithmXSignal.set(nextX); + } + }); + } + + setDataExclusionWarnings(warnings: string[], excludedDatasets: string[] = []): void { + const sanitized = Array.from( + new Set((warnings ?? []) + .map((warning) => String(warning ?? '').trim()) + .filter((warning) => warning.length > 0)) + ); + this.dataExclusionWarningsSignal.set(sanitized); + this.excludedDatasetsSignal.set(excludedDatasets); + } + + clearDataExclusionWarnings(): void { + this.dataExclusionWarningsSignal.set([]); + this.excludedDatasetsSignal.set([]); } selectedAlgorithm = signal<AlgorithmConfig | null>( @@ -326,6 +646,166 @@ export class ExperimentStudioService { algorithmConfigurations = signal<Record<string, Record<string, any>>>( this.sessionStorage.getItem('algorithmConfigurations') || {} ); + algorithmPreprocessingConfigurations = signal<Record<string, PreprocessingConfig | null>>( + this.sessionStorage.getItem('algorithmPreprocessingConfigurations') || {} + ); + readonly appliedPreprocessingConfig = computed( + () => this.mergedAppliedPreprocessingConfig() + ); + + /** + * Singletons from the applied config plus the ordered transformation steps, grouped by name. + * The ordered list is the source of truth for column creators; the singletons stay put. + * The request itself emits the ordered list separately via + * `preprocessingStepsWithTransformations`, so this view is only for the panel's read paths. + */ + private mergedAppliedPreprocessingConfig(): PreprocessingConfig | null { + const singletons = this.algorithmPreprocessingConfigurations()[APPLIED_DESCRIPTIVE_PREPROCESSING] ?? {}; + const merged = ExperimentStudioService.withCreatorLists(singletons, this.appliedTransformationsSignal()); + return Object.keys(merged).length ? merged : null; + } + + /** Name-keyed view of ordered steps: each creator name holds the list of its steps, in order. */ + private static withCreatorLists( + base: PreprocessingConfig, + steps: AnalysisPreprocessingStep[], + ): PreprocessingConfig { + const merged: PreprocessingConfig = { ...base }; + for (const step of steps) { + if (!CREATOR_NAMES.has(step.name as CreatorName)) { + merged[step.name] = step.parameters; + continue; + } + const existing = merged[step.name]; + merged[step.name] = [...(Array.isArray(existing) ? existing : []), step.parameters]; + } + return merged; + } + + /** + * Ordered column-creating steps (categorical, K-means, and the future PCA), stored under + * their own session key. The singletons (missing values, outlier, longitudinal) stay in + * `appliedPreprocessingConfig`. The request emits singletons first, then these in order. + */ + private appliedTransformationsSignal = signal<TransformationStep[]>( + this.loadTransformationsFromSession() + ); + readonly appliedTransformations = this.appliedTransformationsSignal.asReadonly(); + + private loadTransformationsFromSession(): TransformationStep[] { + const stored = this.sessionStorage.getItem<unknown>(APPLIED_TRANSFORMATIONS); + return Array.isArray(stored) + ? stored.filter((step): step is TransformationStep => + !!step && typeof step === 'object' && typeof (step as TransformationStep).name === 'string' + && CREATOR_NAMES.has((step as TransformationStep).name as any) + ) + : []; + } + + /** + * Latest applied transformation steps, with a stable id when the caller passes one. Used by + * upsert/remove so the same step keeps its identity across edits. + */ + private applyTransformations(steps: TransformationStep[]): void { + this.appliedTransformationsSignal.set(steps); + this.sessionStorage.setItem(APPLIED_TRANSFORMATIONS, steps); + this.pruneRolesToAssignable(); + } + + /** + * Insert or replace a transformation step by id (same id replaces in place; a fresh id appends). + * Returns false, writing nothing, when the replacement drops a column a later step reads. + */ + upsertTransformation(step: TransformationStep): boolean { + const current = this.appliedTransformationsSignal(); + const index = current.findIndex((existing) => existing.id === step.id); + if (index !== -1 && JSON.stringify(current[index]) === JSON.stringify(step)) return true; + if (index !== -1 && this.outputsInUse(step.id, stepOutputNames(step)).length) return false; + // Replace in place to keep request-order/dependency guarantees; append only when new. + const next = index === -1 + ? [...current, step] + : current.map((existing, i) => (i === index ? step : existing)); + this.applyTransformations(next); + return true; + } + + /** This step's columns that a later step reads and that `nextOutputs` no longer writes. */ + outputsInUse(id: string, nextOutputs: string[]): string[] { + const step = this.appliedTransformationsSignal().find((candidate) => candidate.id === id); + if (!step) return []; + const read = new Set(this.transformationDependents(id).flatMap((dependent) => stepSourceVariables(dependent))); + const kept = new Set(nextOutputs); + return stepOutputNames(step).filter((name) => read.has(name) && !kept.has(name)); + } + + /** Whether `/services/specifications/preprocessing` includes this creator (chooser + request gate). */ + hasPreprocessingSpec(name: string): boolean { + return !!this.preprocessingSpecs()[name]; + } + + /** + * Whether the preprocessing catalog fetch has completed successfully. Empty specs before + * that mean "not loaded yet", not "every creator is missing". + */ + catalogLoaded(): boolean { + return this.preprocessingCatalogLoaded(); + } + + /** Creator names present in the preprocessing catalog, in CREATOR_NAMES order. */ + availableCreatorNames(): CreatorName[] { + return [...CREATOR_NAMES].filter((name) => this.hasPreprocessingSpec(name)); + } + + /** Remove a transformation step by id. */ + removeTransformation(id: string): void { + const remaining = this.appliedTransformationsSignal().filter((step) => step.id !== id); + if (remaining.length === this.appliedTransformationsSignal().length) return; + this.applyTransformations(remaining); + } + + /** Remove every transformation step in one write. */ + clearTransformations(): void { + if (this.appliedTransformationsSignal().length) this.applyTransformations([]); + } + + /** Later steps whose sources include this step's output codes. */ + transformationDependents(id: string): TransformationStep[] { + const step = this.appliedTransformationsSignal().find((candidate) => candidate.id === id); + if (!step) return []; + const outputs = new Set(stepOutputNames(step)); + if (!outputs.size) return []; + const index = this.appliedTransformationsSignal().indexOf(step); + return this.appliedTransformationsSignal() + .slice(index + 1) + .filter((candidate) => stepSourceVariables(candidate).some((code) => outputs.has(code))); + } + + setAppliedDescriptivePreprocessing(preprocessing: PreprocessingConfig | null): void { + this.algorithmPreprocessingConfigurations.set({ + ...this.algorithmPreprocessingConfigurations(), + [APPLIED_DESCRIPTIVE_PREPROCESSING]: this.normalizePreprocessingConfig(preprocessing), + }); + this.pruneRolesToAssignable(); + } + + getAppliedDescriptivePreprocessing(): PreprocessingConfig | null { + return this.normalizePreprocessingConfig(this.appliedPreprocessingConfig()); + } + + hasAppliedDescriptivePreprocessing(): boolean { + const applied = this.algorithmPreprocessingConfigurations()[APPLIED_DESCRIPTIVE_PREPROCESSING]; + return !!applied && Object.keys(applied).length > 0; + } + + /** + * Whether the next run for this algorithm carries a missing-value strategy. + * Same truth source as the run payload: a stored config, otherwise the + * request-time per-variable drop default (describe/outlier-report send none). + */ + hasRequestPreprocessingForRun(algorithmName: string): boolean { + if (algorithmName === AlgorithmNames.DESCRIBE || algorithmName === AlgorithmNames.OUTLIER_REPORT) return true; + return this.getEffectivePreprocessingSummary(algorithmName) !== 'none'; + } async setAlgorithm(algorithm: AlgorithmConfig) { const algo = this.backendAlgorithms()[algorithm.name]; @@ -334,15 +814,15 @@ export class ExperimentStudioService { return; } - const selectedVariables = this.selectedVariables(); - if (selectedVariables.length !== 1) { - console.warn("Enrichment skipped: need exactly 1 selected variable for enums."); + const algorithmY = this.algorithmY(); + if (algorithmY.length !== 1) { + console.warn("Enrichment skipped: need exactly 1 algorithm variable for enums."); this.selectedAlgorithm.set(algo); this.sessionStorage.setItem('selectedAlgorithm', algo); return; } - const selectedY = selectedVariables[0]; + const selectedY = algorithmY[0]; let enums = selectedY.enumerations ?? []; const enrichedConfig = algo.configSchema.map((field) => { @@ -358,34 +838,60 @@ export class ExperimentStudioService { } loadBackendAlgorithms(): Observable<Record<string, AlgorithmConfig>> { - return this.http.get<RawAlgorithmDefinition[]>('/services/algorithms').pipe( - map((rawAlgorithms) => { - const mapped: Record<string, AlgorithmConfig> = {}; - - rawAlgorithms.forEach((raw) => { - const algo = mapRawAlgorithmToAlgorithmConfig(raw); - mapped[algo.name] = algo; - }); + return forkJoin({ + inputdata: this.http.get('/services/specifications/inputdata'), + preprocessing: this.http.get('/services/specifications/preprocessing'), + algorithms: this.http.get('/services/specifications/algorithms'), + }).pipe( + map(({ inputdata, preprocessing, algorithms }) => { + const mapped = mapSpecificationsToAlgorithmConfigs( + inputdata as any, + preprocessing as any[], + algorithms as any[], + ); this.backendAlgorithms.set(mapped); + this.refreshSelectedAlgorithmFromCatalog(mapped); + const specs = (preprocessing as RawPreprocessingStep[] ?? []).reduce( + (acc, step) => { + if (step?.name) acc[step.name] = step; + return acc; + }, + {} as Record<string, RawPreprocessingStep>, + ); + this.preprocessingSpecs.set(specs); + this.preprocessingCatalogLoaded.set(true); return mapped; }), catchError((error) => { - console.error('Failed to fetch backend algorithms:', error); + console.error('Failed to fetch backend specifications:', error); + // Leave catalogLoaded false so UI does not treat empty specs as "unavailable" + // and requestableTransformations still includes incomplete-catalog steps. return of({}); }) ); } + private refreshSelectedAlgorithmFromCatalog(mapped: Record<string, AlgorithmConfig>): void { + const selected = this.selectedAlgorithm(); + if (!selected) return; + const fresh = mapped[selected.name]; + if (!fresh) return; + + const refreshed = { + ...fresh, + configSchema: selected.configSchema?.length ? selected.configSchema : fresh.configSchema, + }; + this.selectedAlgorithm.set(refreshed); + this.sessionStorage.setItem('selectedAlgorithm', refreshed); + } + getCrossValidationVariant(baseName: string): string | null { return this.crossValidationVariants[baseName] ?? null; } getCrossValidationBase(name: string): string | null { if (this.crossValidationBases[name]) return this.crossValidationBases[name]; - if (name.endsWith('_cv_fedaverage')) { - return name.replace('_cv_fedaverage', ''); - } if (name.endsWith('_cv')) { return name.slice(0, -3); } @@ -395,8 +901,7 @@ export class ExperimentStudioService { isCrossValidationAlgorithm(name: string): boolean { return ( name in this.crossValidationBases || - name.endsWith('_cv') || - name.endsWith('_cv_fedaverage') + name.endsWith('_cv') ); } @@ -406,89 +911,159 @@ export class ExperimentStudioService { return !!base && !this.backendAlgorithms()[base]; } - getTransformationVariant(baseName: string): string | null { - return this.transformationVariants[baseName] ?? null; - } - - getTransformationBase(name: string): string | null { - if (this.transformationBases[name]) return this.transformationBases[name]; - if (name.endsWith('_with_transformation')) { - return name.replace('_with_transformation', ''); - } - return null; - } - - isTransformationAlgorithm(name: string): boolean { - return ( - name in this.transformationBases || - name.endsWith('_with_transformation') - ); - } - availableGroupedAlgorithms = computed(() => { - // Explicitly read selection signals to establish reactive dependencies. - // Without this, the computed only re-runs when backendAlgorithms() changes, - // not when variable/covariate selections change. - const _variables = this.selectedVariables(); - const _covariates = this.selectedCovariates(); - const _filters = this.selectedFilters(); - - // Hide quick-preview algorithms from the selection list. - const hidden = new Set([ - AlgorithmNames.HISTOGRAM, - AlgorithmNames.DESCRIBE, - AlgorithmNames.LOGISTIC_REGRESSION_FEDAVERAGE_FLOWER - ]); + // Explicitly read role signals to establish reactive dependencies. + this.algorithmY(); + this.algorithmX(); return Object.values(this.backendAlgorithms()) - .filter(algo => !hidden.has(algo.name)) + .filter(algo => !ALGORITHM_PANEL_EXCLUDED.has(algo.name)) .filter(algo => { if (!this.isCrossValidationAlgorithm(algo.name)) return true; const base = this.getCrossValidationBase(algo.name); return !base || !this.backendAlgorithms()[base]; }) - .filter(algo => !this.isTransformationAlgorithm(algo.name)) .reduce((acc, algo) => { const cat = algo.category || 'Other'; if (!acc[cat]) acc[cat] = []; + const availability = this.getAlgorithmAvailability(algo.name); acc[cat].push({ ...algo, - isDisabled: !this.isAlgorithmAvailable(algo.name) + availability, + isDisabled: !availability.available, }); return acc; }, {} as Record<string, AlgorithmConfig[]>); }); - isAlgorithmAvailable(name: string): boolean { + getAlgorithmAvailability(name: string): AlgorithmAvailability { const algo = this.backendAlgorithms()[name]; - if (!algo?.inputdata) return false; + const emptyAvailability: AlgorithmAvailability = { + available: false, + summary: 'Algorithm is not in the backend catalog.', + details: [], + }; + if (!algo?.inputdata) return emptyAvailability; - return this.algorithmRulesService.isAlgorithmAvailable(algo, { - y: this.selectedVariables(), - x: this.selectedCovariates(), - filters: this.selectedFilters(), + return this.algorithmRulesService.evaluateAlgorithmAvailability(algo, { + y: this.algorithmY(), + x: this.algorithmX(), }); } - getAlgorithmRequirementOverrides(algo: { name?: string; inputdata?: any } | null | undefined): { y?: string; x?: string; filters?: string } | null { - return this.algorithmRulesService.getAlgorithmRequirementOverrides(algo); + isAlgorithmAvailable(name: string): boolean { + return this.getAlgorithmAvailability(name).available; + } + + private buildSharedInputDataPayload( + algo: AlgorithmConfig, + filtersPayload: BackendFilter | null, + datasetsOverride?: string[] | null, + extraVariableCodes?: string[] | null, + ): AnalysisInputData { + const inputdata = algo.inputdata ?? {}; + const datasets = datasetsOverride ?? this.requestDatasets(); + const variables = this.collectSourceVariables(filtersPayload, extraVariableCodes); + + return { + data_model: this.getActiveDataModelCode(), + datasets, + validation_datasets: algo.requires_validation_datasets ? datasets : null, + filters: ( + Object.prototype.hasOwnProperty.call(inputdata, 'filter') || + Object.prototype.hasOwnProperty.call(inputdata, 'filters') + ) ? filtersPayload : null, + variables, + }; + } + + private collectSourceVariables( + filtersPayload: BackendFilter | null, + extraCodes: string[] | null = null, + ): string[] { + const derived = this.derivedVariableCodes(); + return Array.from( + new Set( + [ + // Pool CDEs (including unassigned members used as transformation sources). + ...this.selectedVariables().map((v) => v.code), + // Experiment filter fields. + ...this.collectFilterVariableCodes(filtersPayload), + // Source CDEs referenced by every transformation step (categorical rules, K-means and + // PCA clustering variables). + ...this.appliedTransformationsSignal().flatMap((step) => stepSourceVariables(step)), + // Previewed CDEs (histogram inspect happens before add-to-pool). + ...this.toArray(extraCodes), + ] + .map((code) => String(code).trim()) + .filter((code) => code && !derived.has(code)), + ), + ); + } + + private preprocessingConfigToSteps( + config: PreprocessingConfig | null | undefined, + ): AnalysisPreprocessingStep[] | null { + if (!config) return null; + // An array value (e.g. several categorical_column_creator configs) expands to + // one ordered step per entry; the engine accepts repeated steps with the same name. + const steps: AnalysisPreprocessingStep[] = []; + for (const [name, parameters] of Object.entries(config)) { + if (Array.isArray(parameters)) { + for (const item of parameters) { + if (item && typeof item === 'object') { + steps.push({ name, parameters: item as Record<string, unknown> }); + } + } + continue; + } + if (parameters && typeof parameters === 'object') { + steps.push({ name, parameters: parameters as Record<string, unknown> }); + } + } + return steps.length ? steps : null; + } + + private buildExperimentRequest( + name: string, + analysis: AnalysisRequest, + ): ExperimentCreateRequest { + return { + name, + analysis, + }; } private rolePayload( algo: AlgorithmConfig, role: 'y' | 'x', codes: string[] - ): string[] | string | null { + ): string[] | null { const req = algo.inputdata?.[role]; if (!req) return null; - if (!codes || codes.length === 0) return null; + return codes; + } - // multiple=false => scalar - if (req.multiple === false) return codes[0]; + private resolveAlgorithmConfig(algorithmName: string): AlgorithmConfig | undefined { + const algorithms = this.backendAlgorithms(); + const direct = algorithms[algorithmName]; + if (direct) return direct; - // multiple=true (undefined) => array - return codes; + if (algorithmName === AlgorithmNames.HISTOGRAM) { + return algorithms[AlgorithmNames.HISTOGRAM]; + } + + return undefined; + } + + /** + * Cohort filters for one request. `undefined` keeps the stored rules, `null` + * means "run with no rules" (the step-0 source snapshot), and a filter object + * replaces the store without touching it — the Cohort Filtering preview. + */ + private resolveFilterPayload(filterOverride?: BackendFilter | null): BackendFilter | null { + return filterOverride === undefined ? this._filterLogic() : filterOverride; } buildRequestBody( @@ -497,12 +1072,14 @@ export class ExperimentStudioService { xVariables: string[] | null = null, effectiveAlgorithmName: string | null = null, customName: string | null = null, - bins: number | null = null + bins: number | null = null, + preprocessingOverride?: PreprocessingConfig | null, + filterOverride?: BackendFilter | null ): any { let algoConfig: AlgorithmConfig | undefined; if (algorithmName) { - algoConfig = this.backendAlgorithms()[algorithmName]; + algoConfig = this.resolveAlgorithmConfig(algorithmName); } else { algoConfig = this.selectedAlgorithm() ?? undefined; } @@ -517,13 +1094,13 @@ export class ExperimentStudioService { // unified signals const variables = yVariables?.length ? yVariables - : this.selectedVariables().map((v) => v.code); + : this.algorithmY().map((v) => v.code); const covariates = - xVariables ?? this.selectedCovariates().map((c) => c.code); + xVariables ?? this.algorithmX().map((c) => c.code); const allConfigs = this.algorithmConfigurations(); - const config = { ...(allConfigs[algoConfig.name ?? ''] || {}) }; + let config = { ...(allConfigs[algoConfig.name ?? ''] || {}) }; const isCvRequest = this.isCrossValidationAlgorithm(requestAlgorithmName); if (isCvRequest && config['n_splits'] === undefined) { config['n_splits'] = 5; @@ -531,71 +1108,635 @@ export class ExperimentStudioService { if (!isCvRequest && config['n_splits'] !== undefined) { delete config['n_splits']; } + config = this.normalizeParameterConfig(algoConfig, config); - // filters logic - const filterLogic = this._filterLogic(); - const hasFilters = - !!( - filterLogic && - Array.isArray(filterLogic.rules) && - filterLogic.rules.length > 0 - ); + // filters logic - a `null` filterOverride is the step-0 source snapshot, + // which previews the selection with no cohort filter attached. + const yPayload = this.rolePayload(algoConfig, 'y', variables); + const xPayload = this.rolePayload(algoConfig, 'x', covariates); + const filtersPayload = this.requestFilters(filterOverride); - // special case for histogram (no filters, transient) + // special case for histogram (transient preview) if (algorithmName === AlgorithmNames.HISTOGRAM) { - return { - name: expName, - algorithm: { - name: requestAlgorithmName, - inputdata: { - data_model: this.getActiveDataModelCode(), - y: yVariables ?? null, - datasets: this.selectedDatasetsSignal(), - filters: null, + const histogramY = yVariables?.length ? yVariables : null; + // Grouping is optional x. Do not fall back to algorithm-panel covariates, and + // do not add a missing-value drop for x: that would drop rows from the overall + // total. Exaflow requires the missing_values_handler step, not a strategy per x. + const histogramX = xVariables?.length ? xVariables : null; + const histogramPreprocessing = this.resolveHistogramPreprocessing( + requestAlgorithmName, + histogramY, + preprocessingOverride + ); + const extraCodes = [...(histogramY ?? []), ...(histogramX ?? [])]; + const inputdata = this.buildSharedInputDataPayload( + algoConfig, + filtersPayload, + this.selectedDatasetsSignal(), + extraCodes, + ); + return this.buildExperimentRequest( + expName, + { + inputdata, + preprocessing: this.preprocessingConfigToSteps(histogramPreprocessing), + algorithm: { + name: requestAlgorithmName, + y: histogramY, + x: histogramX, + parameters: { + histogram_type: HistogramBinningType.WILKINSON, + ...(bins ? { bins } : {}), + }, }, - parameters: bins ? { bins } : {}, - preprocessing: null, }, - }; + ); } - const yPayload = this.rolePayload(algoConfig, 'y', variables); - const xPayload = this.rolePayload(algoConfig, 'x', covariates); - // generic build - const body = { - name: expName, - algorithm: { - name: requestAlgorithmName, - inputdata: { - data_model: this.getActiveDataModelCode(), - y: yPayload, + // Singletons first (missing values, outlier, longitudinal), then the ordered column-creating + // transformations. Every K-means/PCA source that is not an earlier output gets a drop strategy + // and goes into inputdata.variables; created columns never do. + const singletons = this.resolveSingletonsPreprocessing( + requestAlgorithmName, + yPayload, + xPayload, + this.appliedPreprocessingConfig() + ); + return this.buildExperimentRequest( + expName, + { + inputdata: this.buildSharedInputDataPayload( + algoConfig, + filtersPayload, + undefined, + this.transformationSourceCodesForInput(), + ), + preprocessing: this.preprocessingStepsWithTransformations(singletons), + algorithm: { + name: requestAlgorithmName, x: xPayload, - datasets: this.selectedDatasetsSignal(), - filters: hasFilters ? filterLogic : null, + y: yPayload, + parameters: config, }, - parameters: config, - preprocessing: null, }, - mipVersion: (window as any).__env?.MIP_VERSION || '9.0.0' + ); + } + + /** + * Request-time singletons for the run: the applied singletons plus the per-variable drop + * default, with the column-creating transformations stripped out (they are appended in order + * by `preprocessingStepsWithTransformations`). + */ + private resolveSingletonsPreprocessing( + algorithmName: string, + yPayload: string[] | string | null, + xPayload: string[] | string | null, + explicitPreprocessing: unknown = null, + ): PreprocessingConfig | null { + return this.singletonsFromConfig( + this.resolveRequestPreprocessing(algorithmName, yPayload, xPayload, explicitPreprocessing), + ); + } + + private singletonsFromConfig(config: PreprocessingConfig | null): PreprocessingConfig | null { + if (!config) return null; + const singletons: PreprocessingConfig = {}; + for (const [key, value] of Object.entries(config)) { + if (!CREATOR_NAMES.has(key as CreatorName)) singletons[key] = value; + } + return Object.keys(singletons).length ? singletons : null; + } + + /** + * Build the request preprocessing steps: the singletons in their stored order, then the ordered + * column-creating transformations. K-means and PCA sources that are not an earlier output gain a + * `drop` strategy in the missing-values step. + */ + private preprocessingStepsWithTransformations( + singletons: PreprocessingConfig | null, + ): AnalysisPreprocessingStep[] | null { + const steps = this.preprocessingConfigToSteps(singletons) ?? []; + const outputs = new Set<string>(); + for (const step of this.requestableTransformations()) { + if (step.name === 'kmeans_cluster_creator' || step.name === 'pca_column_creator') { + const newSources = stepSourceVariables(step).filter((code) => !outputs.has(code)); + if (newSources.length) this.addTransformationDrops(steps, newSources); + } + steps.push({ name: step.name, parameters: step.parameters }); + for (const name of stepOutputNames(step)) outputs.add(name); + } + return steps.length ? steps : null; + } + + /** + * Describe/preview preprocessing that matches a run: singletons first, then ordered creators. + * Creator keys in `singletons` are stripped so PCA/K-means are not collapsed into a name map. + */ + buildTransformationDescribePreprocessing( + singletons: PreprocessingConfig | null = null, + ): AnalysisPreprocessingStep[] | null { + return this.preprocessingStepsWithTransformations(this.singletonsFromConfig(singletons)); + } + + /** Add a `drop` strategy for the given codes to the request's missing-values step. */ + private addTransformationDrops(steps: AnalysisPreprocessingStep[], codes: string[]): void { + const missing = steps.find((step) => step.name === MISSING_VALUES_HANDLER); + if (!missing) { + steps.unshift({ + name: MISSING_VALUES_HANDLER, + parameters: { strategies: Object.fromEntries(codes.map((code) => [code, 'drop'])) }, + }); + return; + } + const strategies = { ...((missing.parameters['strategies'] as Record<string, unknown>) ?? {}) }; + for (const code of codes) { + if (strategies[code] === undefined) strategies[code] = 'drop'; + } + missing.parameters = { ...missing.parameters, strategies }; + } + + /** + * Source CDEs K-means/PCA steps read from that are not an earlier output, for inputdata.variables. + * Categorical sources are filter fields handled elsewhere and are intentionally excluded. + * Created columns (e.g. `pca_pc*`) never appear here. + */ + transformationSourceCodesForInput(): string[] { + const outputs = new Set<string>(); + const sources: string[] = []; + for (const step of this.requestableTransformations()) { + if (step.name === 'kmeans_cluster_creator' || step.name === 'pca_column_creator') { + for (const code of stepSourceVariables(step)) { + if (!outputs.has(code) && !sources.includes(code)) sources.push(code); + } + } + for (const name of stepOutputNames(step)) outputs.add(name); + } + return sources; + } + + private normalizeParameterConfig( + algoConfig: AlgorithmConfig, + config: Record<string, any> + ): Record<string, any> { + const normalized = { ...config }; + const numericKeys = new Set( + (algoConfig.configSchema ?? []) + .filter((field: any) => field?.type === 'number') + .map((field: any) => String(field.key)) + ); + + numericKeys.forEach((key) => { + const value = normalized[key]; + if (typeof value !== 'string') return; + const trimmed = value.trim(); + if (!trimmed) return; + const parsed = Number(trimmed); + if (Number.isFinite(parsed)) normalized[key] = parsed; + }); + + (algoConfig.configSchema ?? []).forEach((field: any) => { + if (field?.type !== 'select' && field?.type !== 'multi-select') return; + const key = String(field.key); + if (!(key in normalized)) return; + normalized[key] = serializeAlgorithmParameterValue(normalized[key], field); + }); + + if (normalized['positive_class'] !== undefined && normalized['positive_class'] !== null && normalized['positive_class'] !== '') { + normalized['positive_class'] = serializeAlgorithmParameterValue(normalized['positive_class'], { + key: 'positive_class', + type: 'select', + }); + } + + if (normalized['event_var'] !== undefined && normalized['event_var'] !== null && normalized['event_var'] !== '') { + normalized['event_var'] = serializeAlgorithmParameterValue(normalized['event_var'], { + key: 'event_var', + type: 'select', + }); + } + + return omitEmptyOptionalParameters( + normalized, + algoConfig.configSchema ?? [] + ) as Record<string, any>; + } + + private resolveHistogramPreprocessing( + algorithmName: string, + histogramY: string[] | null, + preprocessingOverride: PreprocessingConfig | null | undefined + ): PreprocessingConfig | null { + if (preprocessingOverride === undefined) { + return this.resolveRequestPreprocessing(algorithmName, histogramY, null, null); + } + if (preprocessingOverride === null) { + return null; + } + + const normalized = this.normalizePreprocessingConfig(preprocessingOverride); + if (!normalized) { + return this.resolveRequestPreprocessing(algorithmName, histogramY, null, null); + } + if (!histogramY?.length) { + return normalized; + } + + return ( + this.filterPreprocessingConfigForVariables(normalized, histogramY) + ?? this.resolveRequestPreprocessing(algorithmName, histogramY, null, null) + ); + } + + private resolveRequestPreprocessing( + algorithmName: string, + yPayload: string[] | string | null, + xPayload: string[] | string | null, + explicitPreprocessing: unknown = null + ): PreprocessingConfig | null { + if ( + algorithmName === AlgorithmNames.DESCRIBE || + algorithmName === AlgorithmNames.OUTLIER_REPORT + ) { + return null; + } + + // Created columns are not CDEs: Exaflow rejects missing-value strategies for them. + const derived = this.derivedVariableCodes(); + const variables = Array.from( + new Set( + [...this.toArray(yPayload), ...this.toArray(xPayload)] + .map((code) => String(code).trim()) + .filter((code) => code && !derived.has(code)) + ) + ); + const dropDefault = { + [MISSING_VALUES_HANDLER]: { + strategies: Object.fromEntries(variables.map((code) => [code, 'drop'])), + }, }; - return body; + + const explicit = this.normalizePreprocessingConfig(explicitPreprocessing); + if (explicit) { + // A config holding only created-column steps has no missing-value step, and + // algorithms such as logistic_regression require one. Keep the drop default, run first. + return explicit[MISSING_VALUES_HANDLER] ? explicit : { ...dropDefault, ...explicit }; + } + + return variables.length ? dropDefault : null; + } + + private normalizePreprocessingConfig(value: unknown): PreprocessingConfig | null { + if (!value || typeof value !== 'object' || Array.isArray(value)) return null; + return Object.keys(value as PreprocessingConfig).length > 0 ? { ...(value as PreprocessingConfig) } : null; + } + + /** Keep only preprocessing entries for variables present in the algorithm input (y/x). */ + private filterPreprocessingConfigForVariables( + preprocessing: PreprocessingConfig, + variableCodes: string[] + ): PreprocessingConfig | null { + const allowed = new Set( + variableCodes.map((code) => String(code).trim()).filter((code) => code.length > 0) + ); + if (!allowed.size) return null; + + const filtered: PreprocessingConfig = {}; + + const missing = preprocessing[MISSING_VALUES_HANDLER]; + if (missing && typeof missing === 'object' && !Array.isArray(missing)) { + const next = this.filterPreprocessingStepRecordMaps( + missing as Record<string, unknown>, + allowed, + ['strategies', 'fill_values'] + ); + if (next) filtered[MISSING_VALUES_HANDLER] = next; + } + + const outlier = preprocessing[OUTLIER_WINSORIZER]; + if (outlier && typeof outlier === 'object' && !Array.isArray(outlier)) { + const next = this.filterPreprocessingStepRecordMaps( + outlier as Record<string, unknown>, + allowed, + ['strategies', 'tails', 'folds'] + ); + if (next) filtered[OUTLIER_WINSORIZER] = next; + } + + const longitudinal = preprocessing['longitudinal_transformer']; + if (longitudinal && typeof longitudinal === 'object' && !Array.isArray(longitudinal)) { + const longitudinalRecord = longitudinal as Record<string, unknown>; + const strategies = longitudinalRecord['strategies']; + if (strategies && typeof strategies === 'object' && !Array.isArray(strategies)) { + const filteredStrategies = Object.fromEntries( + Object.entries(strategies as Record<string, unknown>).filter(([code]) => allowed.has(code)) + ); + if (Object.keys(filteredStrategies).length) { + filtered['longitudinal_transformer'] = { + ...(longitudinalRecord['visit1'] !== undefined ? { visit1: longitudinalRecord['visit1'] } : {}), + ...(longitudinalRecord['visit2'] !== undefined ? { visit2: longitudinalRecord['visit2'] } : {}), + strategies: filteredStrategies, + }; + } + } + } + + return Object.keys(filtered).length > 0 ? filtered : null; + } + + private filterPreprocessingStepRecordMaps( + step: Record<string, unknown>, + allowed: Set<string>, + perVariableKeys: string[] + ): Record<string, unknown> | null { + const next: Record<string, unknown> = {}; + let hasContent = false; + + Object.entries(step).forEach(([key, value]) => { + if (perVariableKeys.includes(key) && value && typeof value === 'object' && !Array.isArray(value)) { + const filteredMap = Object.fromEntries( + Object.entries(value as Record<string, unknown>).filter(([code]) => allowed.has(code)) + ); + if (Object.keys(filteredMap).length > 0) { + next[key] = filteredMap; + hasContent = true; + } + return; + } + next[key] = value; + hasContent = true; + }); + + return hasContent ? next : null; + } + + /** + * Effective preprocessing of a run for `algorithmName` as labelled rows — the same truth + * source as the request payload, including the defaults Exaflow applies anyway. + */ + getEffectivePreprocessingEntries( + algorithmName: string | null | undefined, + labelMap: Record<string, string> = {}, + enumMaps: EnumMaps = {} + ): RunSetupSummaryRow[] { + if (!algorithmName) return []; + const poolCodes = this.selectedVariables().map((variable) => variable.code); + const steps = this.preprocessingStepsWithTransformations( + this.resolveSingletonsPreprocessing(algorithmName, poolCodes, [], this.appliedPreprocessingConfig()), + ); + return this.formatPreprocessingEntries( + ExperimentStudioService.withCreatorLists({}, steps ?? []), + labelMap, + enumMaps, + ); + } + + getEffectivePreprocessingSummary(algorithmName: string | null | undefined): string { + return this.summarizePreprocessingEntries(this.getEffectivePreprocessingEntries(algorithmName)); + } + + formatPreprocessingConfig( + preprocessing: unknown, + labelMap: Record<string, string> = {}, + enumMaps: EnumMaps = {} + ): string { + return this.summarizePreprocessingEntries(this.formatPreprocessingEntries(preprocessing, labelMap, enumMaps)); + } + + /** `Label: value` rows as the multi-line summary text, or 'none' when nothing was set. */ + private summarizePreprocessingEntries(entries: RunSetupSummaryRow[]): string { + return entries.length + ? entries.map((entry) => `${entry.label}: ${entry.value}`).join('\n') + : 'none'; + } + + /** `enumMaps` labels the category values in created-column rules; codes show without it. */ + formatPreprocessingEntries( + preprocessing: unknown, + labelMap: Record<string, string> = {}, + enumMaps: EnumMaps = {} + ): RunSetupSummaryRow[] { + return this.summarizePreprocessingConfig(this.normalizePreprocessingConfig(preprocessing), labelMap, enumMaps); + } + + private summarizePreprocessingConfig( + preprocessing: PreprocessingConfig | null, + labelMap: Record<string, string> = {}, + enumMaps: EnumMaps = {} + ): RunSetupSummaryRow[] { + if (!preprocessing) return []; + + const entries: RunSetupSummaryRow[] = []; + const missingValues = preprocessing[MISSING_VALUES_HANDLER] as { strategies?: Record<string, unknown> } | undefined; + const strategies = missingValues?.strategies ?? {}; + const strategyEntries = Object.entries(strategies); + if (strategyEntries.length) { + const labels = strategyEntries.map(([code, strategy]) => + `${this.preprocessingVariableLabel(code, labelMap)}: ${this.humanizePreprocessingValue(String(strategy))}` + ); + entries.push({ label: 'Missing values', value: labels.join(', '), lines: labels }); + } + + const outlier = preprocessing[OUTLIER_WINSORIZER] as { + strategies?: Record<string, unknown>; + tails?: Record<string, unknown>; + folds?: Record<string, unknown>; + } | undefined; + const outlierStrategies = outlier?.strategies ?? {}; + const outlierEntries = Object.entries(outlierStrategies); + if (outlierEntries.length) { + const labels = outlierEntries.map(([code, strategy]) => { + const tail = String(outlier?.tails?.[code] ?? 'both'); + const fold = outlier?.folds?.[code]; + const foldText = fold === undefined || fold === null || fold === '' ? 'fold unavailable' : `fold ${fold}`; + return `${this.preprocessingVariableLabel(code, labelMap)}: ${outlierStrategyLabel(String(strategy))}, ${outlierTailLabel(tail)} ${tail === 'both' ? 'tails' : 'tail'}, ${foldText}`; + }); + entries.push({ label: 'Outlier winsorizer', value: labels.join('; '), lines: labels }); + } + + const longitudinal = preprocessing['longitudinal_transformer'] as Record<string, unknown> | undefined; + if (longitudinal) { + const visit1 = longitudinal['visit1']; + const visit2 = longitudinal['visit2']; + const visitLabel = visit1 && visit2 ? ` (${visit1} to ${visit2})` : ''; + const longitudinalStrategies = longitudinal['strategies'] as Record<string, unknown> | undefined; + const strategyEntries = Object.entries(longitudinalStrategies ?? {}); + const strategySummary = strategyEntries.length + ? strategyEntries.map( + ([code, strategy]) => + `${this.preprocessingVariableLabel(code, labelMap)}: ${this.humanizeLongitudinalStrategy(String(strategy))}` + ) + .join(', ') + : 'configured'; + entries.push({ label: 'Longitudinal transformation', value: `${strategySummary}${visitLabel}` }); + } + + const knownKeys = new Set([MISSING_VALUES_HANDLER, OUTLIER_WINSORIZER, 'longitudinal_transformer']); + Object.keys(preprocessing) + .filter((key) => !knownKeys.has(key)) + .forEach((key) => { + const creators = (Array.isArray(preprocessing[key]) ? preprocessing[key] : [preprocessing[key]]) as + Array<Record<string, unknown> | undefined>; + const label = (codes: string[]) => codes.map((code) => this.preprocessingVariableLabel(code, labelMap)); + if (key === KMEANS_CLUSTER_CREATOR) { + for (const creator of creators) { + const names = creator?.['new_column_names']; + const code = Array.isArray(names) ? String(names[0] ?? '').trim() : ''; + const variables = label(kmeansCreatorVariables(creator)); + const lines = [code || 'configured']; + if (variables.length) lines.push(`Clustered on ${variables.join(', ')}`); + entries.push({ label: 'K-means cluster column', value: lines.join('; '), lines }); + } + return; + } + if (key === 'pca_column_creator') { + for (const creator of creators) { + const step = { id: '', name: key, parameters: creator ?? {} } as TransformationStep; + const components = creator?.['selected_components']; + const lines = [stepOutputNames(step).join(', ') || 'configured']; + const from = label(stepSourceVariables(step)); + if (Array.isArray(components) && components.length) { + lines.push(`Components ${components.join(', ')}${from.length ? ` from ${from.join(', ')}` : ''}`); + } + entries.push({ label: 'PCA component columns', value: lines.join('; '), lines }); + } + return; + } + if (key === 'categorical_column_creator') { + const lines = this.categoricalCreatorLines(preprocessing[key], labelMap, enumMaps); + entries.push({ label: 'Nominal column', value: lines.join('; ') || 'configured', lines }); + return; + } + entries.push({ label: key.replace(/_/g, ' '), value: 'configured' }); + }); + + return entries; + } + + private categoricalCreatorLines(value: unknown, labelMap: Record<string, string>, enumMaps: EnumMaps): string[] { + const creators = Array.isArray(value) ? value : [value]; + return creators.flatMap((creator) => { + if (!creator || typeof creator !== 'object') return []; + const config = creator as { + code?: unknown; + new_column_names?: unknown; + rules?: Record<string, BackendFilter>; + default_enumeration?: unknown; + }; + const lines: string[] = []; + const code = Array.isArray(config.new_column_names) ? String(config.new_column_names[0] ?? '').trim() : ''; + if (code) lines.push(this.preprocessingVariableLabel(code, labelMap)); + for (const [category, filter] of Object.entries(config.rules ?? {})) { + const expression = formatFilterExpression(filter, { labelMap, enumMaps }); + lines.push(expression ? `${category}: ${expression}` : category); + } + const fallback = String(config.default_enumeration ?? '').trim(); + if (fallback) lines.push(`else ${fallback}`); + return lines; + }); + } + + private preprocessingVariableLabel(code: string, labelMap: Record<string, string>): string { + return labelMap[code] ?? code; + } + + private humanizePreprocessingValue(value: string): string { + switch (value) { + case 'drop': + return 'remove rows'; + case 'mean': + return 'mean imputation'; + case 'median': + return 'median imputation'; + case 'constant': + return 'constant value'; + default: + return value.replace(/_/g, ' '); + } + } + + private humanizeLongitudinalStrategy(value: string): string { + switch (value) { + case 'diff': + return 'difference between visits'; + case 'first': + return 'use first visit'; + case 'second': + return 'use second visit'; + default: + return value.replace(/_/g, ' '); + } } loadAllDataModels(): Observable<any[]> { - if (!this.dataModelsLoaded) { - return this.http.get<any[]>(this.apiUrl).pipe( + if (this.dataModelsLoaded) { + return of(this.dataModels); + } + + if (!this.dataModelsRequest$) { + this.dataModelsRequest$ = this.http.get<any[]>(this.apiUrl).pipe( tap((models) => { + this.pathologyAccessWarningSignal.set(null); this.dataModels = models; this.dataModelsLoaded = true; }), catchError((err) => { + const warning = this.resolvePathologyAccessWarning(err); + if (warning) { + this.pathologyAccessWarningSignal.set(warning); + this.clearLoadedDataModelState(); + return of([]); + } + + this.pathologyAccessWarningSignal.set(null); console.error('Error fetching data models:', err); this.errorService.setError('Failed to load data models. Please try again.'); return of([]); - }) + }), + finalize(() => { + this.dataModelsRequest$ = null; + }), + shareReplay({ bufferSize: 1, refCount: false }) ); } - return of(this.dataModels); + + return this.dataModelsRequest$; + } + + private clearLoadedDataModelState(): void { + this.dataModels = []; + this.dataModelsLoaded = false; + this.crossSectionalModels.set([]); + this.longitudinalModels.set([]); + this.availableDatasets.set([]); + this.selectedDataModel.set(null); + this.selectedDatasetsSignal.set([]); + this.selectedVariablesSignal.set([]); + this.algorithmYSignal.set([]); + this.algorithmXSignal.set([]); + this.selectedFiltersSignal.set([]); + this.selectedAlgorithm.set(null); + } + + private resolvePathologyAccessWarning(err: any): PathologyAccessWarning | null { + const status = Number(err?.status ?? 0); + + if (status === 404) { + return { + kind: 'no-pathologies', + title: 'No Pathologies Available', + message: 'This federation does not currently expose any pathologies to the platform.', + }; + } + + if (status === 403) { + return { + kind: 'no-access', + title: 'No Access to Federation Pathologies', + message: 'You do not have access to any of the pathologies available in this federation.', + }; + } + + return null; } getAllDataModels(): Observable<any[]> { @@ -614,7 +1755,11 @@ export class ExperimentStudioService { value: 1, type: v.type ?? 'unknown', description: v.description ?? '', - enumerations: v.enumerations ?? [] + enumerations: v.enumerations ?? [], + methodology: v.methodology, + units: v.units, + minValue: v.minValue, + maxValue: v.maxValue, })); const convertGroups = (groups: Group[] = []): D3HierarchyNode[] => @@ -665,32 +1810,12 @@ export class ExperimentStudioService { }; } - submitRequest(requestBody: any, cacheHandler?: (response: any) => void): Observable<any> { - return this.http.post<any>(this.experimentUrl, requestBody).pipe( - switchMap((res) => { - const uuid = res?.uuid || res?.algorithm?.execution_id; - if (!uuid) { - throw new Error('UUID not found in response'); - } - this.currentExperimentUUIDSignal.set(uuid); - return this.pollForResults(`${this.experimentUrl}/${uuid}`); - }), - tap((response) => { - if (cacheHandler && response?.result) { - cacheHandler(response.result); - } - }), - catchError(() => { - this.errorService.setError('Failed to run experiment. Please try again.'); - this.setRunning(false); - return of({ status: 'error', result: { message: 'Experiment request failed.' } }); - }) - ); - } - - private isTransientAlgorithm(name: string): boolean { - return ['histogram', 'describe'].includes(name); + return ( + name === AlgorithmNames.HISTOGRAM || + name === AlgorithmNames.DESCRIBE || + name === AlgorithmNames.OUTLIER_REPORT + ); } private normalizeResponse(algoName: string, resp: any): any { @@ -706,8 +1831,8 @@ export class ExperimentStudioService { return { result: resp }; } - private submitTransientRequest(requestBody: any, cacheHandler?: (result: any) => void): Observable<any> { - return this.http.post<any>(this.transientUrl, requestBody).pipe( + private submitPreviewRequest(requestBody: any, cacheHandler?: (result: any) => void): Observable<any> { + return this.http.post<any>(this.previewUrl, requestBody).pipe( tap((resp) => { if (cacheHandler && resp) cacheHandler(resp); }), @@ -718,49 +1843,139 @@ export class ExperimentStudioService { ); } - //Runs transient or standard algorithm calls. + //Runs preview algorithm calls. //Used for fetching quick results like histograms or descriptive stats. - getAlgorithmResults(algorithmName: string, nodeCodes: string[] | null = null, bins: number | null = null): Observable<any> { - if (algorithmName === 'histogram') { - const requestBody = this.buildRequestBody(algorithmName, nodeCodes, null, null, null, bins); - return this.submitTransientRequest(requestBody).pipe( + getAlgorithmResults( + algorithmName: string, + nodeCodes: string[] | null = null, + bins: number | null = null, + preprocessingOverride?: PreprocessingConfig | null, + filterOverride?: BackendFilter | null, + groupingCodes: string[] | null = null + ): Observable<any> { + if (algorithmName === AlgorithmNames.HISTOGRAM) { + const requestBody = this.buildRequestBody( + algorithmName, + nodeCodes, + groupingCodes?.length ? groupingCodes : null, + null, + null, + bins, + preprocessingOverride, + filterOverride + ); + return this.submitPreviewRequest(requestBody).pipe( map(resp => this.normalizeResponse(algorithmName, resp)) ); } - // non-transient: ignore nodeCodes + // other algorithms: ignore nodeCodes const requestBody = this.buildRequestBody(algorithmName); - return this.submitRequest(requestBody); + return this.submitPreviewRequest(requestBody); } - private buildDescriptiveRequestBody(variableCodes: string[]): any { - const filters = this.filterLogic(); - const hasFilters = !!(filters && Array.isArray(filters.rules) && filters.rules.length > 0); - - return { - name: `experiment_describe_${variableCodes.join('_')}`, - algorithm: { - name: "describe", + private buildDescriptiveRequestBody( + variableCodes: string[], + preprocessing: PreprocessingConfig | AnalysisPreprocessingStep[] | null = null, + sourceVariableCodes: string[] | null = null, + filterOverride?: BackendFilter | null + ): ExperimentCreateRequest { + // A `null` filterOverride is the step-0 source snapshot: the same describe + // run with no cohort filter attached, i.e. the data exactly as selected. + const filtersPayload = this.requestFilters(filterOverride); + // Exaflow rejects free-text CDEs (categorical without enumerations) as describe inputs. + const freeText = new Set(this.selectedVariables().filter(isFreeTextCde).map((v) => v.code)); + const yCodes = variableCodes.filter((code) => !freeText.has(code)); + const yPayload = yCodes.length ? yCodes : null; + // Derived columns (e.g. categorical_column_creator output) belong in algorithm.y + // only. Source inputdata.variables must be real data-model CDEs. + const derived = this.derivedVariableCodes(); + const sourceCodes = sourceVariableCodes?.length + ? Array.from( + new Set([ + ...sourceVariableCodes + .map((code) => String(code).trim()) + .filter((code) => !!code && !derived.has(code)), + ...this.collectFilterVariableCodes(filtersPayload), + ]) + ) + : this.collectSourceVariables(filtersPayload); + // Callers that already built ordered steps (transformation preview) pass them through; + // name-keyed configs stay on the legacy path used by raw/processed summaries. + const preprocessingSteps = Array.isArray(preprocessing) + ? preprocessing + : this.preprocessingConfigToSteps(this.normalizePreprocessingConfig(preprocessing)); + + return this.buildExperimentRequest( + `experiment_describe_${variableCodes.join('_')}`, + { inputdata: { data_model: this.getActiveDataModelCode(), - y: variableCodes, - x: null, datasets: this.selectedDatasetsSignal(), - filters: hasFilters ? filters : null, + validation_datasets: null, + filters: filtersPayload, + variables: sourceCodes, + }, + preprocessing: preprocessingSteps, + algorithm: { + name: 'describe', + y: yPayload, + x: null, + parameters: {}, }, - parameters: {}, - preprocessing: null, }, - }; + ); + } + + private buildOutlierReportRequestBody( + variableCodes: string[], + parameters: Record<string, unknown>, + preprocessing: PreprocessingConfig | null = null + ): ExperimentCreateRequest { + const filtersPayload = this.requestFilters(); + const selectedYCodes = new Set(this.algorithmY().map((variable) => String(variable?.code ?? ''))); + const selectedXCodes = new Set(this.algorithmX().map((variable) => String(variable?.code ?? ''))); + const y = variableCodes.filter((code) => selectedYCodes.has(code)); + const covariateOnly = variableCodes.filter((code) => selectedXCodes.has(code) && !selectedYCodes.has(code)); + const unassigned = variableCodes.filter((code) => !selectedYCodes.has(code) && !selectedXCodes.has(code)); + const yPayload = y.length ? y : [...covariateOnly, ...unassigned]; + const xPayload = y.length ? covariateOnly : []; + + return this.buildExperimentRequest( + `experiment_outlier_report_${variableCodes.join('_')}`, + { + inputdata: { + data_model: this.getActiveDataModelCode(), + datasets: this.requestDatasets(), + validation_datasets: null, + filters: filtersPayload, + variables: this.collectSourceVariables(filtersPayload), + }, + preprocessing: this.preprocessingConfigToSteps(this.normalizePreprocessingConfig(preprocessing)), + algorithm: { + name: AlgorithmNames.OUTLIER_REPORT, + y: yPayload?.length ? yPayload : null, + x: xPayload.length ? xPayload : null, + parameters, + }, + }, + ); } - loadDescriptiveOverview(variableCodes: string[]): Observable<any> { - const requestBody = this.buildDescriptiveRequestBody(variableCodes); + loadDescriptiveOverview( + variableCodes: string[], + preprocessing: PreprocessingConfig | AnalysisPreprocessingStep[] | null = null, + sourceVariableCodes: string[] | null = null, + filterOverride?: BackendFilter | null + ): Observable<any> { + const requestBody = this.buildDescriptiveRequestBody( + variableCodes, + preprocessing, + sourceVariableCodes, + filterOverride + ); - return this.submitTransientRequest(requestBody).pipe( - tap((response) => { - console.log("Raw descriptive overview response:", response); - }), + return this.submitPreviewRequest(requestBody).pipe( map(resp => this.normalizeResponse("describe", resp)), catchError((error) => { console.error("Error fetching descriptive overview:", error); @@ -770,35 +1985,92 @@ export class ExperimentStudioService { ); } - runSelectedAlgorithm( - algorithmNameOverride: string | null = null, - effectiveAlgorithmName: string | null = null, - customName: string | null = null + /** Public helper for Transformation stats: collect CDE codes referenced by a filter tree. */ + /** Accepts a rule tree or the bare condition a category rule can hold. */ + filterVariableCodes(logic: BackendFilter | BackendRule | null): string[] { + return this.collectFilterVariableCodes(logic); + } + + /** Run the K-means report on the current selection; its reusable_preprocessing is + * the creator input. Uses the K-means parameters configured in the Algorithm step, + * or the spec defaults when none were set (Exaflow rejects an empty map). */ + loadKMeansReport( + variableCodes: string[], + parameters: Record<string, unknown> = {} ): Observable<any> | null { - const selectedAlgo = this.selectedAlgorithm(); - if (!selectedAlgo) { - console.error('No algorithm selected.'); - return null; - } + return this.loadTransientReport('kmeans', variableCodes, parameters); + } - const baseAlgorithmName = algorithmNameOverride ?? selectedAlgo.name; - const requestAlgorithmName = effectiveAlgorithmName ?? baseAlgorithmName; + /** Run PCA on the current selection without saving an experiment; its + * reusable_preprocessing is the pca_column_creator input, as for K-means. */ + loadPcaReport(variableCodes: string[]): Observable<any> | null { + return this.loadTransientReport('pca', variableCodes, {}); + } - if (baseAlgorithmName === 'describe') { - const variableCodes = this.selectedVariables().map((v) => v.code); - if (!variableCodes.length) { - console.warn('Descriptive stats: no variables selected.'); - return null; + /** Transient run of a report algorithm (`y` = the chosen variables) for a creator card. */ + private loadTransientReport( + algorithmName: 'kmeans' | 'pca', + variableCodes: string[], + parameters: Record<string, unknown>, + ): Observable<any> | null { + try { + const requestBody = this.buildRequestBody(algorithmName, variableCodes, null); + const algoConfig = this.resolveAlgorithmConfig(algorithmName); + if (!algoConfig) { + throw new Error(`No algorithm config found for ${algorithmName}`); } - return this.loadDescriptiveOverview(variableCodes); + const algorithm = requestBody?.analysis?.algorithm ?? {}; + // Add the drops to the run's own ordered steps: regrouping them by name would move a + // creator ahead of the creator whose column it reads. + const preprocessing = [...(requestBody?.analysis?.preprocessing ?? [])]; + const codes = [...new Set( + variableCodes.map((code) => String(code).trim()).filter((code) => code && code !== 'dataset'), + )]; + this.addTransformationDrops(preprocessing, codes); + requestBody.analysis = { + ...requestBody.analysis, + preprocessing, + algorithm: { + ...algorithm, + y: variableCodes, + parameters: this.normalizeParameterConfig(algoConfig, { + ...parameterDefaultsFromSchema(algoConfig.configSchema ?? []), + ...(algorithm.parameters ?? {}), + ...parameters, + }), + }, + }; + return this.submitPreviewRequest(requestBody).pipe( + map((resp) => this.normalizeTransientResponse(resp)), + ); + } catch (error) { + console.error(`${algorithmName} report is not available:`, error); + return null; } + } - const requestBody = this.buildRequestBody(baseAlgorithmName, null, null, requestAlgorithmName, customName); + loadOutlierReportPreview( + variableCodes: string[], + parameters: Record<string, unknown>, + preprocessing: PreprocessingConfig | null = null + ): Observable<any> { + const requestBody = this.buildOutlierReportRequestBody(variableCodes, parameters, preprocessing); - return this.submitRequest(requestBody); + return this.submitPreviewRequest(requestBody).pipe( + map(resp => this.normalizeTransientResponse(resp)), + catchError((error) => { + console.error("Error fetching outlier report preview:", error); + this.errorService.setError('Failed to load outlier report preview.'); + return of(null); + }) + ); } - runSelectedAlgorithmTransient( + /** + * Runs the selected algorithm for review. The backend keeps a successful result under the + * returned `uuid`, which `saveRun` persists without running the analysis again. + */ + runSelectedAlgorithm( algorithmNameOverride: string | null = null, effectiveAlgorithmName: string | null = null ): Observable<any> | null { @@ -817,9 +2089,7 @@ export class ExperimentStudioService { console.warn('Descriptive stats: no variables selected.'); return null; } - return this.loadDescriptiveOverview(variableCodes).pipe( - map(resp => this.normalizeTransientResponse(resp)) - ); + return this.submitRunRequest(this.buildDescriptiveRequestBody(variableCodes)); } const requestBody = this.buildRequestBody( @@ -829,45 +2099,48 @@ export class ExperimentStudioService { requestAlgorithmName ); - return this.submitTransientRequest(requestBody).pipe( - map(resp => this.normalizeTransientResponse(resp)) - ); + return this.submitRunRequest(requestBody); } - getCurrentExperimentUUID() { - return this.currentExperimentUUID(); + private submitRunRequest(requestBody: ExperimentCreateRequest): Observable<any> { + return this.http.post<any>(this.runsUrl, requestBody).pipe( + map((resp) => this.normalizeTransientResponse(resp)) + ); } - pollForResults(url: string): Observable<any> { - const pollingInterval = 5000; - const maxRetries = 60; - return timer(0, pollingInterval).pipe( - take(maxRetries), - switchMap(() => - this.http.get<any>(url).pipe( - map((response) => { - if (response.status === 'success' || response.status === 'error') { - return response; - } - return null; - }), - catchError(() => - of({ - status: 'error', - result: { message: 'Network or server error while polling results.' } - }) - ) - ) - ), - filter((result) => result !== null), - take(1), - defaultIfEmpty({ - status: 'error', - result: { message: 'Experiment run timed out before completion.' } - }) - ); + /** + * Saves the run behind the current result under `name`. Only the run id travels: the backend + * copies the result it kept, so saving is instant and stores exactly what was reviewed. + */ + saveRun(name: string): void { + const runId = this.runId(); + if (!runId || !this.runResult()) { + this.saveError.set('Run the experiment before saving it.'); + return; + } + + this.saveError.set(null); + this._isSaving.set(true); + this.http + .post<BackendExperiment>(this.experimentUrl, { runId, name }) + .pipe(finalize(() => this._isSaving.set(false))) + .subscribe({ + next: (saved) => { + this.currentExperimentUUIDSignal.set(saved?.uuid ?? null); + this.isShared.set(!!saved?.shared); + this.notifySaveSucceeded(); + }, + error: (err) => { + this.saveError.set( + err?.status === 404 + ? 'This result is no longer available to save. Run the experiment again.' + : 'Failed to save the experiment. Please try again.' + ); + }, + }); } + categorizeDataModels(dataModels: DataModel[]): { crossSectional: DataModel[]; longitudinal: DataModel[]; @@ -882,9 +2155,6 @@ export class ExperimentStudioService { return this.http.patch(`/services/experiments/${uuid}`, { name: newName }); } - // Filters and rules - private _filterLogic = signal<BackendFilter | null>(null); - get filterLogic() { return this._filterLogic.asReadonly(); } @@ -905,12 +2175,34 @@ export class ExperimentStudioService { ); } + getDatasetLabelMap(): Record<string, string> { + const available = this.availableDatasets(); + if (available.length) { + return Object.fromEntries(available.map((dataset) => [dataset.code, dataset.label])); + } + + const model = this.selectedDataModel(); + if (!model) return {}; + + const { allVariables } = this.convertToD3Hierarchy(model); + const datasetVariable = allVariables.find( + (variable: any) => String(variable?.code ?? '').toLowerCase() === 'dataset' + ); + const map: Record<string, string> = {}; + (datasetVariable?.enumerations ?? []).forEach((dataset: any) => { + const code = String(dataset?.code ?? ''); + if (!code) return; + map[code] = String(dataset?.label ?? dataset?.name ?? code); + }); + return map; + } + updateAvailableDatasets(model: DataModel | null): void { if (!model) { this.availableDatasets.set([]); return; } - const { hierarchy, allVariables } = this.convertToD3Hierarchy(model); + const { allVariables } = this.convertToD3Hierarchy(model); const datasetVariable = allVariables.find( (variable: any) => String(variable?.code ?? '').toLowerCase() === 'dataset' ); @@ -936,46 +2228,39 @@ export class ExperimentStudioService { } hydrateFromBackendExperiment(exp: BackendExperiment): void { - if (!exp || !exp.algorithm) { + if (!exp?.analysis?.algorithm) { console.warn('hydrateFromBackendExperiment called with invalid exp:', exp); return; } this.isShared.set(!!exp.shared); - // flag as editing mode this.setEditingExistingExperiment(true); - - // Basic metadata this.currentExperimentUUIDSignal.set(exp.uuid ?? null); - const algoName = exp.algorithm.name; - const input = exp.algorithm.inputdata || {}; - const params = exp.algorithm.parameters || {}; - + const analysis = exp.analysis; + const algoName = analysis.algorithm.name; + const input = analysis.inputdata || {}; + const params = analysis.algorithm.parameters || {}; + const { singletons, transformations } = splitPreprocessingSteps(analysis.preprocessing ?? []); const filters = input.filters ?? null; // Selected datasets this.setSelectedDatasets(this.toArray(input.datasets)); - // Filters this.setFilterLogic(filters); - this.loadAllDataModels() + // The experiment can arrive before the specifications; resolving the algorithm then would find nothing. + forkJoin([this.loadAllDataModels(), this.catalogLoaded$]) .pipe(takeUntil(this.destroy$)) - .subscribe((models) => { + .subscribe(([models]) => { if (!models || !models.length) { console.warn('No data models available for hydration.'); return; } - const model = this.findDataModelByCodeVersion(input.data_model, models); + const model = findDataModelByCodeVersion(input.data_model, models); if (!model) { - console.warn( - 'No matching data model found for', - input.data_model, - 'in', - models - ); + this.openFreshStudioForMissingDataModel(input.data_model); return; } @@ -984,15 +2269,15 @@ export class ExperimentStudioService { const converted = this.convertToD3Hierarchy(model); const allVariables = converted.allVariables; - const yCodes = this.toArray(input.y); - const xCodes = this.toArray(input.x); + const yCodes = this.toArray(analysis.algorithm.y); + const xCodes = this.toArray(analysis.algorithm.x); const filterCodes = this.collectFilterVariableCodes(filters); - const yNodes = allVariables + const yRealNodes = allVariables .filter((v: any) => yCodes.includes(v.code)) .map((n) => this.enrichVariableNode(n)); - const xNodes = allVariables + const xRealNodes = allVariables .filter((v: any) => xCodes.includes(v.code)) .map((n) => this.enrichVariableNode(n)); @@ -1000,8 +2285,31 @@ export class ExperimentStudioService { filterCodes.includes(v.code) ); - this.setVariables(yNodes); - this.setCovariates(xNodes); + // Saved y/x codes not present in the data model are derived columns + // (e.g. the transformation output). Represent them as synthetic nodes. + // They belong only in the role signals and the assignable computed, never + // in the variables-panel pool (which holds real CDE nodes only). + const knownCodes = new Set(allVariables.map((v: any) => v.code)); + const yNodes = [...yRealNodes]; + const xNodes = [...xRealNodes]; + yCodes + .filter((code: string) => !knownCodes.has(code)) + .forEach((code: string) => yNodes.push(this.syntheticDerivedNode(code))); + xCodes + .filter((code: string) => !knownCodes.has(code)) + .forEach((code: string) => xNodes.push(this.syntheticDerivedNode(code))); + + // Pool = unique real CDE nodes from saved inputdata.variables plus saved y + x. + // input.variables may contain derived codes that are absent from the data + // model (synthetics), so filtering against allVariables excludes them. + const inputVariables = this.toArray(input.variables || analysis.inputdata?.variables || []); + const inputRealNodes = allVariables + .filter((v: any) => inputVariables.includes(v.code)) + .map((n) => this.enrichVariableNode(n)); + + this.setVariables(this.uniqueByCode([...inputRealNodes, ...yRealNodes, ...xRealNodes])); + this.setAlgorithmY(yNodes); + this.setAlgorithmX(xNodes); this.setFilters(filterNodes); const algoConfig = this.backendAlgorithms()[algoName]; @@ -1016,11 +2324,36 @@ export class ExperimentStudioService { ...existingConfigs, [algoName]: params, }); + this.algorithmPreprocessingConfigurations.set({ + ...this.algorithmPreprocessingConfigurations(), + [APPLIED_DESCRIPTIVE_PREPROCESSING]: Object.keys(singletons).length ? singletons : null, + }); + this.applyTransformations(transformations); this.setAlgorithm(algoConfig); }); } + /** + * The saved pathology is not served to this user any more, so the experiment + * cannot be rebuilt. Drop the half-loaded edit state and start on the first available pathology. + */ + private openFreshStudioForMissingDataModel(dataModel: string | undefined): void { + this.resetStudioState(); + const fallback = this.getDefaultDataModel(); + if (fallback) { + this.selectedDataModel.set(fallback); + this.preselectAllDatasetsForModel(fallback); + } + const name = String(dataModel ?? '').replace(':', ' '); + const lost = `You no longer have access to the data this experiment uses${name ? ` (${name})` : ''}.`; + this.errorService.setError( + fallback + ? `${lost} Selected ${fallback.label || fallback.code}, the first available pathology. You can change it from the pathology menu.` + : `${lost} Select another pathology to continue.` + ); + } + onToggleShare(): void { const nextShared = !this.isShared(); const uuid = this.currentExperimentUUID(); @@ -1038,26 +2371,13 @@ export class ExperimentStudioService { }); } - // helpers for edit experiment - private findDataModelByCodeVersion( - codeVersion: string, - models: DataModel[] - ): DataModel | null { - if (!codeVersion) return null; - const [code, version] = codeVersion.split(':'); - return ( - models.find( - (m) => m.code === code && String(m.version) === String(version) - ) ?? null - ); - } private enrichVariableNode(node: D3HierarchyNode): any { const supported = this.algorithmEnabled(node.type ?? 'unknown'); return { ...node, supportedAlgos: supported }; } - private collectFilterVariableCodes(logic: BackendFilter | null): string[] { + private collectFilterVariableCodes(logic: BackendFilter | BackendRule | null): string[] { if (!logic) return []; const codes = new Set<string>(); @@ -1079,38 +2399,106 @@ export class ExperimentStudioService { this.editingExistingExperimentSignal.set(isEditing); } + clearCurrentExperimentUUID(): void { + this.currentExperimentUUIDSignal.set(null); + } + + hasPersistedStudioWork(): boolean { + return ( + this.selectedVariables().length > 0 + || this.algorithmY().length > 0 + || this.algorithmX().length > 0 + || this.selectedFilters().length > 0 + || !!this.selectedAlgorithm() + || !!this.currentExperimentUUID() + || this.hasAppliedDescriptivePreprocessing() + || Object.keys(this.algorithmConfigurations()).length > 0 + ); + } + + getDefaultDataModel(): DataModel | null { + if (this.editingExistingExperiment()) { + return null; + } + + const models = [...this.crossSectionalModels(), ...this.longitudinalModels()]; + return models[0] ?? null; + } + + preselectAllDatasetsForModel(model: DataModel): void { + this.updateAvailableDatasets(model); + const codes = this.availableDatasets().map((dataset) => dataset.code); + if (codes.length > 0) { + this.setSelectedDatasets(codes); + return; + } + + const fallbackCodes = (model.datasets ?? []) + .map((item) => String(item)) + .filter((code) => code); + if (fallbackCodes.length > 0) { + this.setSelectedDatasets(fallbackCodes); + } + } + + resetStudioStateForGuide(): void { + const defaultModel = this.getDefaultDataModel(); + this.resetStudioState(); + if (defaultModel) { + this.selectedDataModel.set(defaultModel); + this.preselectAllDatasetsForModel(defaultModel); + } + } + resetStudioState(): void { // basic signals this.selectedDatasetsSignal.set([]); this.selectedDataModel.set(null); this.setVariables([]); - this.setCovariates([]); + this.setAlgorithmY([]); + this.setAlgorithmX([]); this.setFilters([]); this._filterLogic.set(null); this.errorService.clearError(); + this.clearDataExclusionWarnings(); // algorithm state this.selectedAlgorithm.set(null); this.algorithmConfigurations.set({}); + this.algorithmPreprocessingConfigurations.set({}); + this.appliedTransformationsSignal.set([]); this.lastUsedAlgorithm.set(null); + // execution step state (results are session-scoped, see hasRunStarted) + this.runResult.set(null); + this.runId.set(null); + this.runError.set(null); + this.runSetup.set(null); + this.lastRunSchema.set([]); + this.hasRunStarted.set(false); + this.runStatusText.set('Processing experiment...'); + this.saveSucceeded.set(false); + // meta info this.currentExperimentUUIDSignal.set(null); this.setEditingExistingExperiment(false); // share flag for safety this.isShared.set(false); - - // Clear session storage this.sessionStorage.removeItem('selectedVariables'); - this.sessionStorage.removeItem('selectedCovariates'); + this.sessionStorage.removeItem('algorithmY'); + this.sessionStorage.removeItem('algorithmX'); this.sessionStorage.removeItem('selectedFilters'); this.sessionStorage.removeItem('selectedDatasets'); this.sessionStorage.removeItem('selectedDataModel'); this.sessionStorage.removeItem('selectedAlgorithm'); this.sessionStorage.removeItem('algorithmConfigurations'); + this.sessionStorage.removeItem('algorithmPreprocessingConfigurations'); + this.sessionStorage.removeItem(APPLIED_TRANSFORMATIONS); + this.sessionStorage.removeItem('filterLogic'); + this.sessionStorage.removeItem('lastUsedAlgorithm'); // cancel any in-flight transient requests this.destroy$.next(); @@ -1120,14 +2508,6 @@ export class ExperimentStudioService { this._isRunning.set(isRunning); } - clearSelectedAlgorithm(): void { - this.selectedAlgorithm.set(null); - this.sessionStorage.removeItem('selectedAlgorithm'); - } - - toggleFilterConfigModal(open: boolean): void { - this.isFilterConfigOpen.set(open); - } ngOnDestroy(): void { this.destroy$.next(); diff --git a/src/app/services/experiments-dashboard.service.spec.ts b/src/app/services/experiments-dashboard.service.spec.ts new file mode 100644 index 000000000..d0a05bc44 --- /dev/null +++ b/src/app/services/experiments-dashboard.service.spec.ts @@ -0,0 +1,249 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; + +import { BackendExperiment } from '../models/backend-experiment.model'; +import { Experiment } from '../models/experiments-dashboard.model'; +import { ExperimentsDashboardService } from './experiments-dashboard.service'; + +const apiUrl = '/services/experiments'; + +const backendExperiment = (uuid: string, name: string) => ({ + uuid, + name, + created: '2026-01-01T00:00:00.000Z', + finished: '2026-01-01T00:01:00.000Z', + shared: false, + viewed: false, + status: 'success', + description: '', + analysis: { algorithm: { name: 'mock_anova', y: ['AGE'], x: [] } }, + createdBy: { + username: 'mcurie', + fullname: 'Marie Curie', + email: 'marie.curie@chuv.ch', + subjectId: 'orcid-0000-0002', + agreeNDA: true, + }, +}); + +const frontendExperiment = (id: string, name = id): Experiment => ({ + id, + name, + dateCreated: new Date('2026-01-01T00:00:00.000Z'), + status: 'success', + algorithmName: 'mock_anova', + author: 'Marie Curie', + authorEmail: 'marie.curie@chuv.ch', + isShared: false, +}); + +describe('ExperimentsDashboardService hydrateExperiments', () => { + let service: ExperimentsDashboardService; + let httpMock: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [ExperimentsDashboardService], + }); + service = TestBed.inject(ExperimentsDashboardService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + }); + + describe('list requests with filters', () => { + it('keeps the client-filter snapshot for repeat views within a visit', () => { + service.getUserExperiments(0, 10, true, { status: 'success' }); + httpMock.expectOne((request) => request.url === apiUrl).flush({ + experiments: [backendExperiment('a', 'A'), backendExperiment('b', 'B')], + totalExperiments: 2, + totalPages: 1, + currentPage: 0, + }); + + expect(service.experiments().map((experiment) => experiment.id)).toEqual(['a', 'b']); + + // A second client-only filter is answered from the same full-history snapshot. + service.getUserExperiments(0, 10, true, { status: 'error' }); + httpMock.expectNone((request) => request.url === apiUrl); + expect(service.experiments()).toEqual([]); + expect(service.totalExperiments()).toBe(0); + }); + + it('names the runs a client-side filter searched once the history cap cut it short', () => { + service.getUserExperiments(0, 10, true, { status: 'success' }); + + // 51 pages of 50 exist; the snapshot reads 50 of them. + const first = httpMock.expectOne((request) => request.url === apiUrl); + expect(first.request.params.get('page')).toBe('0'); + first.flush({ + experiments: [backendExperiment('a', 'A')], + totalExperiments: 2550, + totalPages: 51, + currentPage: 0, + }); + + const rest = httpMock.match((request) => request.url === apiUrl); + expect(rest.length).toBe(49); + rest.forEach((request, index) => request.flush({ + experiments: [], + totalExperiments: 2550, + totalPages: 51, + currentPage: index + 1, + })); + + expect(service.fullHistoryCap).toBe(2500); + expect(service.historyTruncated()).toBe(true); + }); + + it('does not claim a truncated history the snapshot actually covered', () => { + service.getUserExperiments(0, 10, true, { status: 'success' }); + httpMock.expectOne((request) => request.url === apiUrl).flush({ + experiments: [backendExperiment('a', 'A')], + totalExperiments: 1, + totalPages: 1, + currentPage: 0, + }); + + expect(service.historyTruncated()).toBe(false); + }); + + it('invalidates the client-filter snapshot on demand', () => { + service.getUserExperiments(0, 10, true, { status: 'success' }); + httpMock.expectOne((request) => request.url === apiUrl).flush({ + experiments: [backendExperiment('a', 'A')], + totalExperiments: 1, + totalPages: 1, + currentPage: 0, + }); + expect(service.experiments().map((experiment) => experiment.id)).toEqual(['a']); + + service.invalidateListCache(); + service.getUserExperiments(0, 10, true, { status: 'success' }); + httpMock.expectOne((request) => request.url === apiUrl).flush({ + experiments: [backendExperiment('b', 'B')], + totalExperiments: 1, + totalPages: 1, + currentPage: 0, + }); + + expect(service.experiments().map((experiment) => experiment.id)).toEqual(['b']); + }); + + it('sends algorithm and shared filters to the server as request params', () => { + service.getUserExperiments(0, 10, true, { algorithm: 'mock_anova', shared: 'shared' }); + + const request = httpMock.expectOne((candidate) => candidate.url === apiUrl); + expect(request.request.params.get('algorithm')).toBe('mock_anova'); + expect(request.request.params.get('shared')).toBe('true'); + expect(request.request.params.get('mine')).toBe('true'); + + request.flush({ + experiments: [backendExperiment('a', 'A')], + totalExperiments: 1, + totalPages: 1, + currentPage: 0, + }); + + expect(service.experiments().map((experiment) => experiment.id)).toEqual(['a']); + }); + }); + + it('lists K-means experiments newest first', () => { + let emitted: BackendExperiment[] | undefined; + + service.listKMeansExperiments().subscribe((experiments) => (emitted = experiments)); + + const req = httpMock.expectOne((candidate) => candidate.url === apiUrl); + expect(req.request.params.get('algorithm')).toBe('kmeans'); + expect(req.request.params.get('page')).toBe('0'); + expect(req.request.params.get('size')).toBe('50'); + // Own scope only: the shared tab would hide the caller's own runs and expose others'. + expect(req.request.params.get('mine')).toBe('true'); + expect(req.request.params.get('notMine')).toBe('false'); + + req.flush({ + experiments: [{ uuid: 'u1' }], + totalExperiments: 1, + totalPages: 1, + currentPage: 0, + }); + + expect(emitted![0].uuid).toBe('u1'); + }); + + it('lists PCA experiments newest first', () => { + let emitted: BackendExperiment[] | undefined; + + service.listPcaExperiments().subscribe((experiments) => (emitted = experiments)); + + const req = httpMock.expectOne((candidate) => candidate.url === apiUrl); + expect(req.request.params.get('algorithm')).toBe('pca'); + expect(req.request.params.get('mine')).toBe('true'); + expect(req.request.params.get('notMine')).toBe('false'); + + req.flush({ + experiments: [{ uuid: 'pca-1' }], + totalExperiments: 1, + totalPages: 1, + currentPage: 0, + }); + + expect(emitted![0].uuid).toBe('pca-1'); + }); + + it('resolves members that are already loaded without asking the backend', () => { + service.experiments.set([frontendExperiment('a'), frontendExperiment('b')]); + let resolved: Experiment[] | undefined; + + service.hydrateExperiments(['b', 'a']).subscribe((experiments) => (resolved = experiments)); + + expect(resolved!.map((exp) => exp.id)).toEqual(['b', 'a']); + }); + + it('fetches a member from another page, upserts it, and keeps folder order', () => { + service.experiments.set([frontendExperiment('a')]); + let resolved: Experiment[] | undefined; + + service.hydrateExperiments(['a', 'b', 'c']).subscribe((experiments) => (resolved = experiments)); + + httpMock.expectNone(`${apiUrl}/a`); + httpMock.expectOne(`${apiUrl}/b`).flush(backendExperiment('b', 'Off page b')); + httpMock.expectOne(`${apiUrl}/c`).flush(backendExperiment('c', 'Off page c')); + + expect(resolved!.map((exp) => exp.id)).toEqual(['a', 'b', 'c']); + // The compare workspace only sees the loaded list, so the members had to be added to it. + expect(service.experiments().map((exp) => exp.id).sort()).toEqual(['a', 'b', 'c']); + }); + + it('drops a member the backend no longer returns instead of failing the comparison', () => { + service.experiments.set([frontendExperiment('a')]); + let resolved: Experiment[] | undefined; + + service.hydrateExperiments(['a', 'gone']).subscribe((experiments) => (resolved = experiments)); + + httpMock.expectOne(`${apiUrl}/gone`).flush('not found', { status: 404, statusText: 'Not Found' }); + + expect(resolved!.map((exp) => exp.id)).toEqual(['a']); + }); + + it('propagates a real fetch failure instead of treating it as a deleted member', () => { + service.experiments.set([frontendExperiment('a')]); + let error: unknown; + + service.hydrateExperiments(['a', 'unreachable']).subscribe({ + error: (err) => (error = err), + }); + + httpMock.expectOne(`${apiUrl}/unreachable`).flush('server down', { + status: 500, + statusText: 'Server Error', + }); + + expect((error as { status?: number } | undefined)?.status).toBe(500); + expect(service.experiments().map((exp) => exp.id)).toEqual(['a']); + }); +}); diff --git a/src/app/services/experiments-dashboard.service.ts b/src/app/services/experiments-dashboard.service.ts index 9b2aaf2c2..e179e7791 100644 --- a/src/app/services/experiments-dashboard.service.ts +++ b/src/app/services/experiments-dashboard.service.ts @@ -3,8 +3,49 @@ import { Experiment } from '../models/experiments-dashboard.model'; import { HttpClient } from '@angular/common/http'; import { BackendExperiment, BackendExperimentWithResult } from '../models/backend-experiment.model'; import { mapBackendToFrontend } from '../pages/experiments-dashboard/experiments-dashboard.mapper'; -import { Subscription, map, tap } from 'rxjs'; +import { + EXPERIMENT_SORTS, + ExperimentDatePreset, + ExperimentFilters, + ExperimentSort, +} from '../pages/experiments-dashboard/experiment-search/experiment-filter.model'; +import { Observable, Subscription, catchError, forkJoin, map, of, switchMap, tap, throwError } from 'rxjs'; import { ErrorService } from './error.service'; +import { isNotFoundError } from '../core/http-error.utils'; + +interface ExperimentsPage { + experiments: BackendExperiment[]; + totalExperiments: number; + totalPages: number; + currentPage: number; +} + +const PAGE_SIZE_FOR_FULL_LOAD = 50; +/** + * A filter the backend contract cannot express (substring search, author, variable, status, + * date) is answered from one ownership-scoped snapshot of the history, sliced here. That + * snapshot is capped, so past `FULL_LOAD_HISTORY_CAP` runs a search is honest about the + * range it covered rather than implying it read everything. Retiring the cap needs a backend + * substring/date filter, which retires loadAllExperiments entirely. + */ +const MAX_FULL_LOAD_PAGES = 50; +const FULL_LOAD_HISTORY_CAP = PAGE_SIZE_FOR_FULL_LOAD * MAX_FULL_LOAD_PAGES; + +/** One ownership-scoped read of the history, and whether the cap cut it short. */ +interface Snapshot { + experiments: Experiment[]; + truncated: boolean; +} + +const DEFAULT_FILTERS: ExperimentFilters = { + query: '', + datePreset: 'any', + algorithm: null, + author: null, + variable: null, + status: 'any', + shared: 'any', +}; @Injectable({ providedIn: 'root', @@ -12,82 +53,296 @@ import { ErrorService } from './error.service'; export class ExperimentsDashboardService { private apiUrl = '/services/experiments'; - // WritableSignal experiments: WritableSignal<Experiment[]> = signal<Experiment[]>([]); totalExperiments = signal<number>(0); - globalTotalExperiments = signal<number>(0); + /** True when a client-side filter had to stop at `fullHistoryCap` runs. */ + historyTruncated = signal<boolean>(false); + readonly fullHistoryCap = FULL_LOAD_HISTORY_CAP; totalPages = signal<number>(0); currentPage = signal<number>(0); + isLoading = signal<boolean>(false); private http = inject(HttpClient); private experimentsRequestSub: Subscription | null = null; + private lastListRequest: (() => void) | null = null; - constructor() { } + /** Full per-tab cache used only when a filter cannot be pushed to the backend contract. */ + private allExperimentsCache = new Map<'mine' | 'shared', Snapshot>(); + + /** Clears the client-filter snapshot so the next load reads the backend again. */ + invalidateListCache(): void { + this.allExperimentsCache.clear(); + } private errorService = inject(ErrorService); - // Fetch all experiments from the backend and update the signal - getUserExperiments(page: number = 0, size: number = 10, onlyMine: boolean = false, filters?: any): void { - const params: any = { - page: page.toString(), - size: size.toString(), - mine: onlyMine.toString(), - includeShared: (!onlyMine).toString() - }; + constructor() { } - if (filters) { - if (filters.query) params.name = filters.query; - if (filters.algorithm) params.algorithm = filters.algorithm; - if (filters.shared === 'shared') params.shared = 'true'; - if (filters.shared === 'private') params.shared = 'false'; - const dateRange = this.resolveDateRange(filters.datePreset); - if (dateRange.dateFrom) params.dateFrom = dateRange.dateFrom; - if (dateRange.dateTo) params.dateTo = dateRange.dateTo; - } + /** + * Loads a dashboard page. + * + * The backend supports ownership, name-prefix, popularity and a few flags directly, but not + * description/author/variable/status/date filters and not true substring search. When the + * user asks for one of those, load the ownership-scoped history once and slice it here so + * the controls do what they say. Normal browsing stays server-paginated. + */ + getUserExperiments( + page: number = 0, + size: number = 10, + onlyMine: boolean = false, + filters?: Partial<ExperimentFilters>, + sort: ExperimentSort = 'created-desc', + ): void { + const effectiveFilters: ExperimentFilters = { ...DEFAULT_FILTERS, ...(filters ?? {}) }; + this.lastListRequest = () => this.getUserExperiments(page, size, onlyMine, effectiveFilters, sort); this.experimentsRequestSub?.unsubscribe(); + this.isLoading.set(true); + + if (this.needsClientFiltering(effectiveFilters)) { + this.experimentsRequestSub = this.loadAllExperiments(onlyMine, sort).subscribe({ + next: (all) => this.applyClientView(all, page, size, effectiveFilters), + error: (err) => this.handleListError(err), + }); + return; + } + this.experimentsRequestSub = this.http - .get<{ experiments: BackendExperiment[], totalExperiments: number, totalPages: number, currentPage: number }>(this.apiUrl, { - params: params + .get<ExperimentsPage>(this.apiUrl, { + params: this.buildServerParams(page, size, onlyMine, sort, effectiveFilters), }) .subscribe({ - next: (response) => { - const mappedExperiments = (response?.experiments || []).map(mapBackendToFrontend); - this.experiments.set(mappedExperiments); - this.totalExperiments.set(response?.totalExperiments || 0); - this.totalPages.set(response?.totalPages || 0); - this.currentPage.set(response?.currentPage || 0); - }, - error: (err) => { - console.error('[ExperimentsDashboardService] getUserExperiments error', err); - this.errorService.setError('Failed to load experiments.'); - this.experiments.set([]); - this.totalExperiments.set(0); - this.totalPages.set(0); - this.currentPage.set(0); - } + next: (response) => this.applyServerPage(response), + error: (err) => this.handleListError(err), }); } - fetchGlobalTotal(): void { - const params = { - page: '0', - size: '1', - mine: 'false', - includeShared: 'true' - }; - this.http - .get<{ experiments: BackendExperiment[], totalExperiments: number, totalPages: number, currentPage: number }>(this.apiUrl, { - params: params + retryLastRequest(): void { + this.lastListRequest?.(); + } + + /** + * The user's own K-means experiments (newest first) that can provide cluster columns. + * The picker replays the run's stored preprocessing, so it must not read the shared + * scope: another user's clusters are not a column this user may inject. + */ + listKMeansExperiments(): Observable<BackendExperiment[]> { + return this.http + .get<ExperimentsPage>(this.apiUrl, { + params: this.buildServerParams(0, 50, true, 'created-desc', { + ...DEFAULT_FILTERS, + algorithm: 'kmeans', + }), }) - .subscribe({ - next: (response) => { - this.globalTotalExperiments.set(response?.totalExperiments || 0); - }, - error: (err) => { - console.error('[ExperimentsDashboardService] fetchGlobalTotal error', err); - } - }); + .pipe(map((page) => page?.experiments ?? [])); + } + + /** + * The user's own PCA experiments (newest first) that can provide component columns. + * Own scope only — same rationale as {@link listKMeansExperiments}. + */ + listPcaExperiments(): Observable<BackendExperiment[]> { + return this.http + .get<ExperimentsPage>(this.apiUrl, { + params: this.buildServerParams(0, 50, true, 'created-desc', { + ...DEFAULT_FILTERS, + algorithm: 'pca', + }), + }) + .pipe(map((page) => page?.experiments ?? [])); + } + + private applyServerPage(response: ExperimentsPage | null): void { + this.isLoading.set(false); + const mappedExperiments = (response?.experiments || []).map(mapBackendToFrontend); + this.experiments.set(mappedExperiments); + this.totalExperiments.set(response?.totalExperiments || 0); + this.totalPages.set(response?.totalPages || 0); + this.currentPage.set(response?.currentPage || 0); + // Server-paginated browsing never reads a capped snapshot. + this.historyTruncated.set(false); + } + + /** The snapshot arrives already in the requested order (the server sorted it), so filtering and + * slicing preserve that order — there is nothing to re-sort here. */ + private applyClientView( + all: Experiment[], + page: number, + size: number, + filters: ExperimentFilters, + ): void { + this.isLoading.set(false); + const sorted = all.filter((experiment) => this.matchesFilters(experiment, filters)); + const total = sorted.length; + const totalPages = total === 0 ? 0 : Math.ceil(total / size); + const safePage = total === 0 ? 0 : Math.min(page, Math.max(0, totalPages - 1)); + + this.experiments.set(sorted.slice(safePage * size, safePage * size + size)); + this.totalExperiments.set(total); + this.totalPages.set(totalPages); + this.currentPage.set(safePage); + } + + private buildServerParams( + page: number, + size: number, + onlyMine: boolean, + sort: ExperimentSort, + filters: ExperimentFilters, + ): Record<string, string> { + const params: Record<string, string> = { + ...this.baseListParams(page, size, onlyMine), + ...this.sortParams(sort), + }; + + if (filters.algorithm) params['algorithm'] = filters.algorithm; + if (filters.shared !== 'any') params['shared'] = (filters.shared === 'shared').toString(); + + return params; + } + + private baseListParams(page: number, size: number, onlyMine: boolean): Record<string, string> { + return { + page: page.toString(), + size: size.toString(), + mine: onlyMine.toString(), + notMine: (!onlyMine).toString(), + includeShared: (!onlyMine).toString(), + }; + } + + private sortParams(sort: ExperimentSort): Record<string, string> { + const { orderBy, descending } = EXPERIMENT_SORTS[sort] ?? EXPERIMENT_SORTS['created-desc']; + return { orderBy, descending: descending.toString() }; + } + + private buildFullLoadParams(page: number, onlyMine: boolean, sort: ExperimentSort): Record<string, string> { + return { + ...this.baseListParams(page, PAGE_SIZE_FOR_FULL_LOAD, onlyMine), + ...this.sortParams(sort), + }; + } + + private loadAllExperiments(onlyMine: boolean, sort: ExperimentSort): Observable<Experiment[]> { + const cacheKey: 'mine' | 'shared' = onlyMine ? 'mine' : 'shared'; + const cached = this.allExperimentsCache.get(cacheKey); + if (cached) { + this.historyTruncated.set(cached.truncated); + return of(cached.experiments); + } + + const fetchPage = (page: number): Observable<{ page: ExperimentsPage; experiments: Experiment[] }> => + this.http + .get<ExperimentsPage>(this.apiUrl, { params: this.buildFullLoadParams(page, onlyMine, sort) }) + .pipe( + map((response) => ({ + page: response, + experiments: (response?.experiments || []).map(mapBackendToFrontend), + })), + ); + + return fetchPage(0).pipe( + switchMap((first) => { + const serverPages = first.page?.totalPages || 1; + const pageCount = Math.min(Math.max(1, serverPages), MAX_FULL_LOAD_PAGES); + const pageRequests = Array.from({ length: pageCount - 1 }, (_, index) => fetchPage(index + 1)); + + return (pageRequests.length ? forkJoin(pageRequests) : of([])).pipe( + map((rest): Snapshot => ({ + experiments: [ + ...first.experiments, + ...rest.flatMap((entry) => entry.experiments), + ], + // Pages the history had that the cap refused to read: the search did not see them. + truncated: serverPages > pageCount, + })), + ); + }), + tap((snapshot) => { + this.allExperimentsCache.set(cacheKey, snapshot); + this.historyTruncated.set(snapshot.truncated); + }), + map((snapshot) => snapshot.experiments), + ); + } + + private needsClientFiltering(filters: ExperimentFilters): boolean { + return Boolean( + filters.query?.trim() || + filters.author || + filters.variable || + filters.status !== 'any' || + filters.datePreset !== 'any', + ); + } + + private matchesFilters(experiment: Experiment, filters: ExperimentFilters): boolean { + if (!this.matchesQuery(experiment, filters.query)) return false; + if (filters.algorithm && experiment.algorithmName !== filters.algorithm) return false; + if (filters.author && !this.normalize(experiment.author).includes(this.normalize(filters.author))) return false; + if (filters.variable && !this.matchesVariable(experiment, filters.variable)) return false; + if (filters.status !== 'any' && experiment.status !== filters.status) return false; + if (filters.shared === 'shared' && !experiment.isShared) return false; + if (filters.shared === 'private' && experiment.isShared) return false; + if (!this.matchesDatePreset(experiment.dateCreated, filters.datePreset)) return false; + return true; + } + + private matchesQuery(experiment: Experiment, query: string): boolean { + const needle = this.normalize(query); + if (!needle) return true; + + const fields = [ + experiment.name, + experiment.description, + experiment.author, + experiment.algorithmName, + experiment.domain, + ...(experiment.datasets ?? []), + ...(experiment.variables ?? []), + ...(experiment.covariates ?? []), + ]; + + return fields.some((value) => this.normalize(value).includes(needle)); + } + + private matchesVariable(experiment: Experiment, variable: string): boolean { + const needle = this.normalize(variable); + if (!needle) return true; + + return [...(experiment.variables ?? []), ...(experiment.covariates ?? [])].some((value) => + this.normalize(value).includes(needle), + ); + } + + private matchesDatePreset(date: Date, preset: ExperimentDatePreset): boolean { + if (!preset || preset === 'any') return true; + if (!(date instanceof Date) || Number.isNaN(date.getTime())) return false; + + const now = new Date(); + const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + + if (preset === 'today') return date >= startOfToday; + + const days = preset === '7d' ? 7 : 30; + const from = new Date(startOfToday); + from.setDate(from.getDate() - days); + return date >= from; + } + + private handleListError(err: unknown): void { + this.isLoading.set(false); + console.error('[ExperimentsDashboardService] getUserExperiments error', err); + this.errorService.setError('Failed to load experiments.'); + this.experiments.set([]); + this.totalExperiments.set(0); + this.totalPages.set(0); + this.currentPage.set(0); + this.historyTruncated.set(false); + } + + private normalize(value: string | null | undefined): string { + return (value ?? '').toLowerCase(); } // For edit / hydrate (metadata) @@ -112,6 +367,45 @@ export class ExperimentsDashboardService { next[idx] = { ...current[idx], ...experiment }; return next; }); + + // The snapshot is a server read, so a local edit drops it rather than being merged into it. + this.allExperimentsCache.clear(); + } + + /** + * Resolves `ids` to experiments in folder order, fetching the members that sit on another + * page. The compare workspace reads the loaded page only, so without this an off-page + * member of a folder would drop out of the comparison silently. A member the backend no + * longer returns is skipped here; the folder canvas reports and prunes it. + */ + hydrateExperiments(ids: string[]): Observable<Experiment[]> { + const known = new Map(this.experiments().map((exp) => [exp.id, exp] as const)); + const missing = ids.filter((id) => !known.has(id)); + + const resolved = (fetched: Experiment[]): Experiment[] => { + fetched.forEach((exp) => this.upsertExperiment(exp)); + const byId = new Map(known); + fetched.forEach((exp) => byId.set(exp.id, exp)); + return ids + .map((id) => byId.get(id)) + .filter((exp): exp is Experiment => !!exp); + }; + + if (!missing.length) return of(resolved([])); + + return forkJoin( + missing.map((id) => + this.fetchExperimentById(id).pipe( + // A 404 means the member is gone and may be dropped. Any other failure is a real + // fetch failure and must reach the caller so the UI can say the handoff failed. + catchError((err) => isNotFoundError(err) ? of<Experiment | null>(null) : throwError(() => err)), + ), + ), + ).pipe( + map((fetched) => + resolved(fetched.filter((exp): exp is Experiment => !!exp)), + ), + ); } // For compare / results view @@ -119,10 +413,6 @@ export class ExperimentsDashboardService { return this.http.get<BackendExperimentWithResult>(`${this.apiUrl}/${uuid}`); } - updateExperimentShared(uuid: string, shared: boolean) { - return this.http.patch(`/services/experiments/${uuid}`, { shared }); - } - toggleExperimentShare(experimentId: string, newShared: boolean) { return this.http .patch<BackendExperiment>(`${this.apiUrl}/${experimentId}`, { shared: newShared }) @@ -155,11 +445,12 @@ export class ExperimentsDashboardService { ); } - deleteExperiment(experimentId: string): void { + deleteExperiment(experimentId: string, onDeleted?: (experimentId: string) => void): void { if (!experimentId) return; const previousExperiments = this.experiments(); const previousTotal = this.totalExperiments(); + this.allExperimentsCache.clear(); // Optimistic update UI this.experiments.update((current: Experiment[]) => @@ -170,36 +461,15 @@ export class ExperimentsDashboardService { // Backend call with rollback on failure this.http.delete<void>(`${this.apiUrl}/${experimentId}`).subscribe({ next: () => { + onDeleted?.(experimentId); }, error: (err) => { console.error('Error deleting experiment', err); this.experiments.set(previousExperiments); this.totalExperiments.set(previousTotal); + this.allExperimentsCache.clear(); this.errorService.setError('Failed to delete experiment.'); }, }); } - - private resolveDateRange( - preset: 'any' | 'today' | '7d' | '30d' | string | null | undefined - ): { dateFrom?: string; dateTo?: string } { - if (!preset || preset === 'any') return {}; - - const now = new Date(); - const toIso = (d: Date) => d.toISOString(); - const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()); - - if (preset === 'today') { - return { dateFrom: toIso(startOfToday), dateTo: toIso(now) }; - } - if (preset === '7d' || preset === '30d') { - const days = preset === '7d' ? 7 : 30; - const from = new Date(now); - from.setDate(now.getDate() - days); - return { dateFrom: toIso(from), dateTo: toIso(now) }; - } - - return {}; - } - } diff --git a/src/app/services/export-results-pdf.service.ts b/src/app/services/export-results-pdf.service.ts deleted file mode 100644 index 7ed7e0f91..000000000 --- a/src/app/services/export-results-pdf.service.ts +++ /dev/null @@ -1,621 +0,0 @@ -import { Injectable } from '@angular/core'; -import jsPDF from 'jspdf'; -import html2canvas from 'html2canvas'; -import autoTable from 'jspdf-autotable'; -import { AlgorithmTableRegistry, TableSpec } from '../pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry'; - -export interface ExperimentPdfDetails { - experimentName: string; - createdBy?: string | null; - createdAt?: Date | string | null; - algorithm?: string | null; - params?: Record<string, unknown> | null; - preprocessing?: string | null; - domain?: string | null; - datasets?: string[] | null; - variables?: string[] | null; - covariates?: string[] | null; - filters?: string[] | null; - interactions?: string | string[] | null; - transformations?: string | string[] | null; - mipVersion?: string | null; -} - -export interface ExperimentPdfPayload { - filename: string; - details: ExperimentPdfDetails; - algorithmKey?: string | null; - result?: any; - chartContainer?: HTMLElement | null; -} - -@Injectable({ providedIn: 'root' }) -export class ResultsPdfExportService { - private readonly platformName = 'Medical Informatics Platform'; - private readonly logoUrl = '/assets/mip-logo.png'; - - async exportExperimentPdf(payload: ExperimentPdfPayload): Promise<void> { - if (!payload?.details?.experimentName) { - console.warn('PdfExportService: missing experiment details.'); - return; - } - - const doc = new jsPDF('p', 'mm', 'a4'); - const pageWidth = doc.internal.pageSize.getWidth(); - const pageHeight = doc.internal.pageSize.getHeight(); - const margin = { left: 18, right: 18, top: 24, bottom: 18 }; - const contentWidth = pageWidth - margin.left - margin.right; - - const logoData = await this.loadImageData(this.logoUrl); - const createdAt = this.formatDate(payload.details.createdAt); - const timestamp = this.formatTimestamp(new Date()); - - // Cover page - this.renderCoverPage(doc, { - margin, - experimentName: payload.details.experimentName, - createdBy: payload.details.createdBy, - createdAt, - }); - - // Details page - doc.addPage(); - this.renderDetailsPage(doc, { - margin, - contentWidth, - details: payload.details, - }); - - // Results pages - const tables = this.getTablesForAlgorithm(payload.algorithmKey, payload.result); - const chartImages = await this.captureChartImages(payload.chartContainer); - - const resultItems: Array< - { type: 'table'; table: TableSpec } | { type: 'chart'; title: string; image: string } - > = []; - - tables.forEach((table) => resultItems.push({ type: 'table', table })); - chartImages.forEach((image, index) => - resultItems.push({ type: 'chart', title: `Chart ${index + 1}`, image }) - ); - - if (resultItems.length === 0) { - doc.addPage(); - this.renderResultEmptyPage(doc, { pageWidth, margin }); - } else { - for (const item of resultItems) { - doc.addPage(); - if (item.type === 'table') { - this.renderTablePage(doc, { - margin, - contentWidth, - table: item.table, - }); - } else { - this.renderChartPage(doc, { - pageWidth, - pageHeight, - margin, - contentWidth, - title: item.title, - image: item.image, - }); - } - } - } - - this.applyHeadersAndFooters(doc, { - logoData, - pageWidth, - pageHeight, - margin, - timestamp, - platformName: this.platformName, - }); - - if (payload.details.mipVersion) { - this.renderMipVersion(doc, payload.details.mipVersion, { - pageWidth, - pageHeight, - margin, - }); - } - - const safeName = (payload.filename || 'experiment-report').replace(/\s+/g, '_'); - doc.save(safeName.endsWith('.pdf') ? safeName : `${safeName}.pdf`); - } - - private renderCoverPage( - doc: jsPDF, - options: { - margin: { left: number; right: number; top: number }; - experimentName: string; - createdBy?: string | null; - createdAt: string; - } - ): void { - const { margin, experimentName, createdBy, createdAt } = options; - const pageWidth = doc.internal.pageSize.getWidth(); - doc.setTextColor(0); - doc.setFontSize(22); - - const nameLines: string[] = doc.splitTextToSize(experimentName, pageWidth - margin.left - margin.right); - let currentY = 110; - - nameLines.forEach((line) => { - doc.text(line, margin.left, currentY); - currentY += 10; - }); - - doc.setDrawColor(2, 122, 122); - doc.setLineWidth(0.6); - doc.line(margin.left, currentY - 8, margin.left + 50, currentY - 8); - - doc.setFontSize(11); - doc.setTextColor(110); - const createdLine = this.buildCreatedLine(createdBy, createdAt); - const createdLines: string[] = doc.splitTextToSize(createdLine, pageWidth - margin.left - margin.right); - - createdLines.forEach((line) => { - doc.text(line, margin.left, currentY - 2); - currentY += 6; - }); - } - - private renderDetailsPage( - doc: jsPDF, - options: { - margin: { left: number; right: number; top: number }; - contentWidth: number; - details: ExperimentPdfDetails; - } - ): void { - const { margin, contentWidth, details } = options; - let y = margin.top + 6; - - doc.setFont('helvetica', 'bold'); - doc.setFontSize(15); - doc.setTextColor(0); - doc.text('Experiment details', margin.left, y); - doc.setDrawColor(2, 122, 122); - doc.setLineWidth(0.6); - doc.line(margin.left, y + 2, margin.left + 52, y + 2); - y += 8; - - const cardTop = y + 2; - const cardPadding = 8; - const cardWidth = contentWidth; - const gutter = 8; - const columnWidth = (cardWidth - gutter) / 2; - const lineHeight = 5.5; - - const paramLines = this.formatParams(details.params); - const entries = [ - { label: 'Algorithm', value: details.algorithm || 'Unknown' }, - { label: 'Params', value: paramLines.length ? paramLines : ['none'] }, - { label: 'Preprocessing', value: details.preprocessing || 'none' }, - { label: 'Domain', value: details.domain || 'none' }, - { label: 'Datasets', value: this.formatList(details.datasets) }, - { label: 'Variables', value: this.formatList(details.variables) }, - { label: 'Covariates', value: this.formatList(details.covariates) }, - { label: 'Filter', value: this.formatList(details.filters) }, - ].filter(e => e.value && e.value !== 'none' && (!Array.isArray(e.value) || e.value.length > 0)); - - const normalizeLines = (value: string | string[]) => { - if (Array.isArray(value)) { - return value.flatMap((item: string) => { - const bullet = '• '; - const bulletWidth = doc.getTextWidth(bullet); - const wrapped = doc.splitTextToSize(String(item), columnWidth - 4 - bulletWidth); - return wrapped.map((line: string, index: number) => - index === 0 ? bullet + line : ' ' + line - ); - }); - } - return doc.splitTextToSize(String(value), columnWidth - 4); - }; - - const getBlockHeight = (value: string | string[]) => { - const lines = normalizeLines(value); - return lineHeight + lines.length * lineHeight + 2; - }; - - let rowY = cardTop + cardPadding; - doc.setFontSize(11); - doc.setTextColor(30); - - for (let i = 0; i < entries.length; i += 2) { - const left = entries[i]; - const right = entries[i + 1]; - const leftHeight = getBlockHeight(left.value); - const rightHeight = right ? getBlockHeight(right.value) : 0; - const rowHeight = Math.max(leftHeight, rightHeight); - - const renderCell = (entry: { label: string; value: string | string[] }, x: number) => { - doc.setFont('helvetica', 'bold'); - doc.text(entry.label, x, rowY); - doc.setFont('helvetica', 'normal'); - const lines = normalizeLines(entry.value); - let textY = rowY + lineHeight; - lines.forEach((line: string) => { - doc.text(String(line), x, textY); - textY += lineHeight; - }); - }; - - renderCell(left, margin.left + cardPadding); - if (right) { - renderCell(right, margin.left + cardPadding + columnWidth + gutter); - } - - rowY += rowHeight + 2; - } - - const cardHeight = rowY - cardTop + cardPadding; - doc.setDrawColor(218); - doc.setFillColor(249, 251, 251); - doc.roundedRect(margin.left, cardTop, cardWidth, cardHeight, 3, 3, 'FD'); - - // Redraw text on top of the card - rowY = cardTop + cardPadding; - doc.setTextColor(30); - for (let i = 0; i < entries.length; i += 2) { - const left = entries[i]; - const right = entries[i + 1]; - const leftHeight = getBlockHeight(left.value); - const rightHeight = right ? getBlockHeight(right.value) : 0; - const rowHeight = Math.max(leftHeight, rightHeight); - - const renderCell = (entry: { label: string; value: string | string[] }, x: number) => { - doc.setFont('helvetica', 'bold'); - doc.text(entry.label, x, rowY); - doc.setFont('helvetica', 'normal'); - const lines = normalizeLines(entry.value); - let textY = rowY + lineHeight; - lines.forEach((line: string) => { - doc.text(String(line), x, textY); - textY += lineHeight; - }); - }; - - renderCell(left, margin.left + cardPadding); - if (right) { - renderCell(right, margin.left + cardPadding + columnWidth + gutter); - } - - rowY += rowHeight + 2; - } - - y = cardTop + cardHeight + 10; - - const hasInteractions = details.interactions && details.interactions !== 'none' && (!Array.isArray(details.interactions) || details.interactions.length > 0); - const hasTransformations = details.transformations && details.transformations !== 'none' && (!Array.isArray(details.transformations) || details.transformations.length > 0); - - if (hasInteractions || hasTransformations) { - doc.setFont('helvetica', 'bold'); - doc.setTextColor(0); - doc.text('Formula', margin.left, y); - doc.setDrawColor(230); - doc.setLineWidth(0.4); - doc.line(margin.left, y + 2, margin.left + 26, y + 2); - y += 8; - doc.setFont('helvetica', 'normal'); - - if (hasInteractions) { - y = this.renderKeyValue(doc, { - label: 'Interactions', - value: details.interactions ?? 'none', - x: margin.left, - y, - maxWidth: contentWidth, - }); - } - - if (hasTransformations) { - y = this.renderKeyValue(doc, { - label: 'Transformations', - value: details.transformations ?? 'none', - x: margin.left, - y, - maxWidth: contentWidth, - }); - } - } - } - - private renderTablePage( - doc: jsPDF, - options: { - margin: { left: number; right: number; top: number; bottom: number }; - contentWidth: number; - table: TableSpec; - } - ): void { - const { margin, contentWidth, table } = options; - let y = margin.top + 6; - - doc.setFont('helvetica', 'bold'); - doc.setFontSize(14); - doc.setTextColor(0); - doc.text(table.title || 'Results Table', margin.left, y); - y += 6; - - const rows = (table.rows || []).map((row) => - row.map((cell) => (cell === null || cell === undefined ? '' : String(cell))) - ); - - autoTable(doc, { - head: [table.columns || []], - body: rows, - startY: y, - margin: { left: margin.left, right: margin.right }, - styles: { fontSize: 8, cellPadding: 2, lineColor: [220, 230, 230] }, - headStyles: { fillColor: [2, 122, 122], textColor: 255, fontStyle: 'bold' }, - alternateRowStyles: { fillColor: [245, 249, 249] }, - tableWidth: contentWidth, - rowPageBreak: 'avoid', - }); - } - - private renderChartPage( - doc: jsPDF, - options: { - pageWidth: number; - pageHeight: number; - margin: { left: number; right: number; top: number; bottom: number }; - contentWidth: number; - title: string; - image: string; - } - ): void { - const { pageWidth, pageHeight, margin, contentWidth, title, image } = options; - let y = margin.top + 6; - - doc.setFont('helvetica', 'bold'); - doc.setFontSize(14); - doc.setTextColor(0); - doc.text(title, margin.left, y); - y += 6; - - const availableHeight = pageHeight - y - margin.bottom; - - const imgProps = (doc as any).getImageProperties(image); - const imgWidth = contentWidth; - const imgHeight = (imgProps.height * imgWidth) / imgProps.width; - - const finalHeight = Math.min(imgHeight, availableHeight); - const finalWidth = (imgProps.width * finalHeight) / imgProps.height; - const x = (pageWidth - finalWidth) / 2; - - doc.addImage(image, 'PNG', x, y, finalWidth, finalHeight); - } - - private renderResultEmptyPage( - doc: jsPDF, - options: { pageWidth: number; margin: { left: number; right: number; top: number } } - ): void { - const { pageWidth, margin } = options; - doc.setFont('helvetica', 'bold'); - doc.setFontSize(14); - doc.text('Results', margin.left, margin.top + 6); - doc.setFont('helvetica', 'normal'); - doc.setFontSize(11); - doc.text('No result data available for this experiment.', margin.left, margin.top + 18); - } - - private applyHeadersAndFooters( - doc: jsPDF, - options: { - logoData: string | null; - pageWidth: number; - pageHeight: number; - margin: { left: number; right: number; top: number; bottom: number }; - platformName: string; - timestamp: string; - } - ): void { - const { logoData, pageWidth, pageHeight, margin, platformName, timestamp } = options; - const totalPages = (doc as any).getNumberOfPages(); - - for (let page = 1; page <= totalPages; page += 1) { - doc.setPage(page); - if (logoData) { - doc.addImage(logoData, 'PNG', margin.left, 8, 10, 10); - } - - doc.setFont('helvetica', 'normal'); - doc.setFontSize(9); - doc.setTextColor(130); - doc.text(platformName, pageWidth - margin.right, 14, { align: 'right' }); - - doc.setDrawColor(230); - doc.line(margin.left, 20, pageWidth - margin.right, 20); - - doc.setTextColor(140); - doc.text(`${page}/${totalPages}`, pageWidth / 2, pageHeight - 10, { align: 'center' }); - doc.text(timestamp, pageWidth - margin.right, pageHeight - 10, { align: 'right' }); - } - } - - private renderKeyValue( - doc: jsPDF, - options: { - label: string; - value: string | string[]; - x: number; - y: number; - maxWidth: number; - indent?: number; - } - ): number { - const { label, value, x, y, maxWidth, indent = 0 } = options; - let currentY = y; - const labelText = `${label}:`; - const labelWidth = doc.getTextWidth(labelText); - - doc.setFont('helvetica', 'bold'); - doc.text(labelText, x, currentY); - doc.setFont('helvetica', 'normal'); - - const startX = x + labelWidth + 2; - const availableWidth = maxWidth - (startX - x); - const lines = Array.isArray(value) ? value : doc.splitTextToSize(value, availableWidth); - - if (Array.isArray(lines)) { - if (lines.length === 0) { - doc.text('none', startX, currentY); - return currentY + 6; - } - - if (Array.isArray(value)) { - const bullet = '• '; - const bulletWidth = doc.getTextWidth(bullet); - value.forEach((item) => { - const innerLines = doc.splitTextToSize(String(item), availableWidth - bulletWidth); - innerLines.forEach((line: string, index: number) => { - const lineX = index === 0 ? startX : startX + 3; - const prefix = index === 0 ? bullet : ''; - doc.text(prefix + line, lineX, currentY); - currentY += 6; - }); - }); - return currentY; - } - - lines.forEach((line, index) => { - const lineX = index === 0 ? startX : startX + indent; - doc.text(String(line), lineX, currentY); - currentY += 6; - }); - return currentY; - } - - doc.text(String(value), startX, currentY); - return currentY + 6; - } - - private formatParams(params?: Record<string, unknown> | null): string[] { - if (!params || Object.keys(params).length === 0) return []; - return Object.entries(params).map(([key, value]) => { - const prettyKey = this.prettyLabel(key); - return `${prettyKey}: ${this.formatValue(value)}`; - }); - } - - private formatList(list?: string[] | null): string[] | 'none' { - if (!list || list.length === 0) return 'none'; - return list; - } - - private formatValue(value: unknown): string { - if (value === null || value === undefined) return 'none'; - if (typeof value === 'string') return value; - if (typeof value === 'number' || typeof value === 'boolean') return String(value); - if (Array.isArray(value)) return value.map((item) => this.formatValue(item)).join(', '); - try { - return JSON.stringify(value); - } catch { - return String(value); - } - } - - private prettyLabel(label: string): string { - return label.replace(/_/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase()); - } - - private getTablesForAlgorithm(algorithmKey?: string | null, result?: any): TableSpec[] { - if (!algorithmKey || !result) return []; - const builder = AlgorithmTableRegistry[algorithmKey]; - if (!builder) return []; - try { - return builder(result) || []; - } catch (error) { - console.warn('[PdfExportService] Table builder failed', error); - return []; - } - } - - private async captureChartImages(container?: HTMLElement | null): Promise<string[]> { - if (!container) return []; - const chartElements = Array.from( - container.querySelectorAll('.chart-container') - ) as HTMLElement[]; - - if (chartElements.length === 0) return []; - - const images: string[] = []; - for (const el of chartElements) { - try { - const canvas = await html2canvas(el, { - backgroundColor: '#ffffff', - scale: 2, - useCORS: true, - }); - images.push(canvas.toDataURL('image/png')); - } catch (error) { - console.warn('[PdfExportService] Chart capture failed', error); - } - } - return images; - } - - private async loadImageData(url: string): Promise<string | null> { - try { - const response = await fetch(url, { cache: 'force-cache' }); - if (!response.ok) return null; - const blob = await response.blob(); - return await new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.onload = () => resolve(String(reader.result)); - reader.onerror = () => reject(new Error('Failed to read image.')); - reader.readAsDataURL(blob); - }); - } catch (error) { - console.warn('[PdfExportService] Logo load failed', error); - return null; - } - } - - private buildCreatedLine(createdBy?: string | null, createdAt?: string): string { - if (createdBy && createdAt) return `Created by ${createdBy} on ${createdAt}.`; - if (createdBy) return `Created by ${createdBy}.`; - if (createdAt) return `Created on ${createdAt}.`; - return ''; - } - - private formatDate(value?: Date | string | null): string { - if (!value) return ''; - const date = value instanceof Date ? value : new Date(value); - if (Number.isNaN(date.getTime())) return ''; - return date.toUTCString(); - } - - private formatTimestamp(date: Date): string { - return date.toLocaleString(); - } - - private renderMipVersion( - doc: jsPDF, - version: string, - options: { - pageWidth: number; - pageHeight: number; - margin: { left: number; right: number; bottom: number }; - } - ): void { - const { pageWidth, pageHeight, margin } = options; - const totalPages = (doc as any).getNumberOfPages(); - doc.setPage(totalPages); - - doc.setFont('helvetica', 'italic'); - doc.setFontSize(9); - doc.setTextColor(150); - - const versionText = `MIP Version: ${version}`; - const textWidth = doc.getTextWidth(versionText); - const x = pageWidth - margin.right - textWidth; - const y = pageHeight - margin.bottom - 2; // Just above the footer line or timestamp - - doc.text(versionText, x, y); - } -} diff --git a/src/app/services/guide-launcher.service.ts b/src/app/services/guide-launcher.service.ts new file mode 100644 index 000000000..3e9177f99 --- /dev/null +++ b/src/app/services/guide-launcher.service.ts @@ -0,0 +1,30 @@ +import { Injectable, signal } from '@angular/core'; + +/** What the header needs to draw a Guide control: a label and a way to start. */ +export interface GuideLauncher { + label: string; + start: () => void; +} + +/** + * The guide lives in the page that owns it (dashboard, studio); the bar that shows its + * control lives in the shell. The two halves meet here instead of the guide reaching into + * the header or the header guessing which route has a tour. + * + * Each guide registers its own object and unregisters that same object, so a route change + * cannot let the outgoing guide clear the incoming one's entry. + */ +@Injectable({ providedIn: 'root' }) +export class GuideLauncherService { + readonly launcher = signal<GuideLauncher | null>(null); + + register(launcher: GuideLauncher): void { + this.launcher.set(launcher); + } + + unregister(launcher: GuideLauncher): void { + if (this.launcher() === launcher) { + this.launcher.set(null); + } + } +} diff --git a/src/app/services/guide-onboarding.service.ts b/src/app/services/guide-onboarding.service.ts new file mode 100644 index 000000000..05f6fa552 --- /dev/null +++ b/src/app/services/guide-onboarding.service.ts @@ -0,0 +1,27 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ providedIn: 'root' }) +export class GuideOnboardingService { + private readonly studioSeenStorageKey = 'mip.guide.experiment-studio.seen'; + private readonly legacyStudioAutoStartStorageKey = 'mip.guide.experiment-studio.autostarted'; + + hasSeenStudioGuide(): boolean { + try { + return localStorage.getItem(this.studioSeenStorageKey) === 'true' + || localStorage.getItem(this.legacyStudioAutoStartStorageKey) === 'true'; + } catch { + return false; + } + } + + markStudioGuideSeen(): void { + try { + localStorage.setItem(this.studioSeenStorageKey, 'true'); + // Backwards compatibility: users who already dismissed or auto-started the old guide + // should not be forced through onboarding after the storage key rename. + localStorage.setItem(this.legacyStudioAutoStartStorageKey, 'true'); + } catch { + // Ignore storage failures and keep the guide functional. + } + } +} diff --git a/src/app/services/notebook-nav.service.spec.ts b/src/app/services/notebook-nav.service.spec.ts new file mode 100644 index 000000000..b40c16703 --- /dev/null +++ b/src/app/services/notebook-nav.service.spec.ts @@ -0,0 +1,51 @@ +import { TestBed } from '@angular/core/testing'; + +import { NotebookNavService } from './notebook-nav.service'; + +describe('NotebookNavService', () => { + let service: NotebookNavService; + + beforeEach(() => { + localStorage.clear(); + TestBed.configureTestingModule({}); + service = TestBed.inject(NotebookNavService); + }); + + afterEach(() => { + localStorage.clear(); + }); + + it('defaults to not visited', () => { + expect(service.hasVisited()).toBe(false); + }); + + it('marks visited and persists to localStorage', () => { + service.markVisited(); + + expect(service.hasVisited()).toBe(true); + expect(localStorage.getItem('mip.notebook.nav.seen')).toBe('true'); + }); + + it('does not throw when markVisited is called twice', () => { + service.markVisited(); + service.markVisited(); + + expect(service.hasVisited()).toBe(true); + }); + + it('reads visited state from localStorage on init', () => { + localStorage.setItem('mip.notebook.nav.seen', 'true'); + const fresh = new NotebookNavService(); + + expect(fresh.hasVisited()).toBe(true); + }); + + it('does not throw when localStorage is unavailable', () => { + vi.spyOn(localStorage, 'setItem').mockImplementation(() => { + throw new Error('quota'); + }); + + expect(() => service.markVisited()).not.toThrow(); + expect(service.hasVisited()).toBe(false); + }); +}); diff --git a/src/app/services/notebook-nav.service.ts b/src/app/services/notebook-nav.service.ts new file mode 100644 index 000000000..63209a159 --- /dev/null +++ b/src/app/services/notebook-nav.service.ts @@ -0,0 +1,29 @@ +import { Injectable, signal } from '@angular/core'; + +@Injectable({ providedIn: 'root' }) +export class NotebookNavService { + private readonly seenStorageKey = 'mip.notebook.nav.seen'; + + readonly hasVisited = signal(this.readVisited()); + + + markVisited(): void { + if (this.hasVisited()) { + return; + } + try { + localStorage.setItem(this.seenStorageKey, 'true'); + this.hasVisited.set(true); + } catch { + // Ignore storage failures; glow may reappear on next visit. + } + } + + private readVisited(): boolean { + try { + return localStorage.getItem(this.seenStorageKey) === 'true'; + } catch { + return false; + } + } +} diff --git a/src/app/services/pdf-export.service.ts b/src/app/services/pdf-export.service.ts index 85c3fc961..0cf56c072 100644 --- a/src/app/services/pdf-export.service.ts +++ b/src/app/services/pdf-export.service.ts @@ -1,337 +1,412 @@ import { Injectable } from '@angular/core'; import jsPDF from 'jspdf'; -import html2canvas from 'html2canvas'; import autoTable from 'jspdf-autotable'; +import { + captureChart, + captureCharts, + ChartImage, + ContextRow, + experimentContextRows, + ExperimentExportDetails, + filledContextRows, + fileSlug, + formatExportDate, +} from '../core/export.utils'; +import { exportTable, getAlgorithmTableBuilder, TableSpec } from '../pages/experiment-studio/visualisations/auto-renderer/algorithm-table-registry'; + +interface ExperimentPdfPayload { + filename: string; + details: ExperimentExportDetails; + algorithmKey?: string | null; + result?: unknown; + chartContainer?: HTMLElement | null; +} + +interface HistogramPdfOptions { + title: string; + nodeLabel: string; + modelLabel: string; + datasetLabels: string[]; + description?: string; + meta?: { + pathNodes?: Array<{ code: string; label: string }>; + groupCount: number; + hasGroups: boolean; + } | null; + isGroupView: boolean; + mipVersion?: string | null; +} + +interface DescriptiveStatsBlock { + name: string; + columns: string[]; + rows: Array<{ metric: string; values: Record<string, unknown> }>; +} + +interface DescriptiveStatsData { + /** e.g. "Raw data" or "Processed data". */ + title: string; + pathologyName?: string; + /** Setup the statistics describe: datasets, filters, preprocessing. */ + context?: ContextRow[]; + variables: DescriptiveStatsBlock[]; + charts?: NodeListOf<HTMLElement>; + nonNominalVariables?: Array<{ name?: string; label?: string }>; + nominalCharts?: NodeListOf<HTMLElement>; + nominalVariables?: Array<{ name?: string; label?: string }>; + mipVersion?: string | null; +} + +const BRAND: [number, number, number] = [43, 51, 233]; +const PLATFORM_NAME = 'Medical Informatics Platform'; +const LOGO_URL = '/assets/mip-logo.png'; + +/** + * One A4 document with a moving cursor: blocks flow onto the current page and + * break only when they do not fit, instead of one table or chart per page. + */ +class PdfReport { + readonly doc = new jsPDF({ unit: 'mm', format: 'a4', compress: true }); + private readonly margin = { left: 18, right: 18, top: 26, bottom: 20 }; + private y = this.margin.top; + /** Drawn with the next block, so a heading never ends a page alone. */ + private pendingHeading: string | null = null; + + private get pageWidth(): number { return this.doc.internal.pageSize.getWidth(); } + private get pageHeight(): number { return this.doc.internal.pageSize.getHeight(); } + private get contentWidth(): number { return this.pageWidth - this.margin.left - this.margin.right; } + + newPage(): void { + this.doc.addPage(); + this.y = this.margin.top; + } + + /** Make room for a block (plus any pending heading), then draw the heading. */ + private ensure(height: number): void { + const headingHeight = this.pendingHeading ? 9 : 0; + if (this.y + height + headingHeight > this.pageHeight - this.margin.bottom) this.newPage(); + this.flushHeading(); + } + + title(text: string, subtitle?: string): void { + const { doc } = this; + doc.setFont('helvetica', 'bold'); + doc.setFontSize(18); + doc.setTextColor(15, 23, 42); + const lines: string[] = doc.splitTextToSize(text, this.contentWidth); + doc.text(lines, this.margin.left, this.y + 4); + this.y += 4 + lines.length * 7; + doc.setDrawColor(...BRAND); + doc.setLineWidth(0.6); + doc.line(this.margin.left, this.y - 3, this.margin.left + 40, this.y - 3); + if (subtitle) { + doc.setFont('helvetica', 'normal'); + doc.setFontSize(10); + doc.setTextColor(100); + const sub: string[] = doc.splitTextToSize(subtitle, this.contentWidth); + doc.text(sub, this.margin.left, this.y + 2); + this.y += sub.length * 5; + } + this.y += 6; + } + + heading(text: string): void { + this.flushHeading(); + this.pendingHeading = text; + } + + private flushHeading(): void { + if (!this.pendingHeading) return; + const { doc } = this; + doc.setFont('helvetica', 'bold'); + doc.setFontSize(13); + doc.setTextColor(15, 23, 42); + doc.text(this.pendingHeading, this.margin.left, this.y + 4); + this.y += 9; + this.pendingHeading = null; + } + + /** Two-column label/value block; empty rows are left out. */ + context(rows: ContextRow[]): void { + const body = filledContextRows(rows).map(([label, values]) => [ + label, + values.length > 1 ? values.map((value) => `• ${value}`).join('\n') : values[0], + ]); + if (!body.length) return; + this.ensure(12); + autoTable(this.doc, { + startY: this.y, + body, + theme: 'plain', + margin: { left: this.margin.left, right: this.margin.right, top: this.margin.top, bottom: this.margin.bottom }, + styles: { fontSize: 9, cellPadding: { top: 1.2, bottom: 1.2, left: 0, right: 3 }, textColor: 40 }, + columnStyles: { 0: { fontStyle: 'bold', cellWidth: 38, textColor: 15 } }, + }); + this.y = this.lastTableEnd() + 6; + } + + /** What the document holds, so a reader knows before paging through it. */ + contents(items: string[]): void { + this.heading('Contents'); + const { doc } = this; + const body = () => { + doc.setFont('helvetica', 'normal'); + doc.setFontSize(9); + doc.setTextColor(40); + }; + items.forEach((item, index) => { + body(); + const lines: string[] = doc.splitTextToSize(`${index + 1}. ${item}`, this.contentWidth); + this.ensure(lines.length * 4.5); + body(); + doc.text(lines, this.margin.left, this.y + 3); + this.y += lines.length * 4.5; + }); + this.y += 6; + } + + paragraph(text: string): void { + const { doc } = this; + doc.setFont('helvetica', 'normal'); + doc.setFontSize(10); + const lines: string[] = doc.splitTextToSize(text, this.contentWidth); + this.ensure(lines.length * 5); + doc.setFont('helvetica', 'normal'); + doc.setFontSize(10); + doc.setTextColor(60); + doc.text(lines, this.margin.left, this.y + 3); + this.y += lines.length * 5 + 4; + } + + table(spec: TableSpec, fallbackTitle = 'Results table'): void { + this.ensure(30); + this.subheading(spec.title || fallbackTitle); + // Exact values, like the CSV export: the report is for checking numbers, not reading them at a glance. + const exported = exportTable(spec); + autoTable(this.doc, { + startY: this.y, + head: [exported.columns ?? []], + // jsPDF's built-in Helvetica has no narrow no-break space (the on-screen thousands separator). + body: exported.rows.map((row) => row.map((cell) => (cell === null || cell === undefined ? '' : String(cell).replace(/\u202f/g, ' ')))), + margin: { left: this.margin.left, right: this.margin.right, top: this.margin.top, bottom: this.margin.bottom }, + styles: { fontSize: 8, cellPadding: 1.8, lineColor: [226, 232, 240], lineWidth: 0.1 }, + headStyles: { fillColor: BRAND, textColor: 255, fontStyle: 'bold' }, + alternateRowStyles: { fillColor: [246, 247, 254] }, + rowPageBreak: 'avoid', + }); + this.y = this.lastTableEnd() + 8; + } + + /** Charts are capped at half the printable height, so two share a page. */ + chart(image: ChartImage, title: string): void { + const maxHeight = (this.pageHeight - this.margin.top - this.margin.bottom) / 2 - 14; + let width = this.contentWidth; + let height = (image.height * width) / image.width; + if (height > maxHeight) { + height = maxHeight; + width = (image.width * height) / image.height; + } + this.ensure(height + 10); + this.subheading(title); + this.doc.addImage(image.dataUrl, 'JPEG', (this.pageWidth - width) / 2, this.y, width, height, undefined, 'FAST'); + this.y += height + 8; + } + + private subheading(text: string): void { + const { doc } = this; + doc.setFont('helvetica', 'bold'); + doc.setFontSize(10.5); + doc.setTextColor(15, 23, 42); + const lines: string[] = doc.splitTextToSize(text, this.contentWidth); + doc.text(lines, this.margin.left, this.y + 3); + this.y += lines.length * 5 + 1; + } + + private lastTableEnd(): number { + return (this.doc as unknown as { lastAutoTable?: { finalY: number } }).lastAutoTable?.finalY ?? this.y; + } + + /** Header and footer on every page, then download. */ + async save(filename: string, mipVersion?: string | null): Promise<void> { + this.flushHeading(); + const { doc } = this; + const logo = await loadImageData(LOGO_URL); + const exported = `Exported ${formatExportDate(new Date())}`; + const total = doc.getNumberOfPages(); + for (let page = 1; page <= total; page += 1) { + doc.setPage(page); + // A fixed alias embeds the logo once, not once per page. + if (logo) doc.addImage(logo, 'PNG', this.margin.left, 8, 9, 9, 'mip-logo', 'FAST'); + doc.setFont('helvetica', 'normal'); + doc.setFontSize(8.5); + doc.setTextColor(120); + doc.text(PLATFORM_NAME, this.pageWidth - this.margin.right, 14, { align: 'right' }); + doc.setDrawColor(226, 232, 240); + doc.setLineWidth(0.2); + doc.line(this.margin.left, 19, this.pageWidth - this.margin.right, 19); + + const footerY = this.pageHeight - 10; + if (mipVersion) doc.text(`MIP ${mipVersion}`, this.margin.left, footerY); + doc.text(`${page} / ${total}`, this.pageWidth / 2, footerY, { align: 'center' }); + doc.text(exported, this.pageWidth - this.margin.right, footerY, { align: 'right' }); + } + doc.save(`${fileSlug(filename, 'export')}.pdf`); + } +} -export interface PdfExportOptions { - title: string; - nodeLabel: string; - modelLabel: string; - datasetLabels: string[]; - description?: string; - meta?: { - pathNodes?: Array<{ code: string; label: string }>; - groupCount: number; - hasGroups: boolean; - } | null; - isGroupView: boolean; +async function loadImageData(url: string): Promise<string | null> { + try { + const response = await fetch(url, { cache: 'force-cache' }); + if (!response.ok) return null; + const blob = await response.blob(); + return await new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => resolve(String(reader.result)); + reader.onerror = () => reject(new Error('Failed to read image.')); + reader.readAsDataURL(blob); + }); + } catch (error) { + console.warn('[PdfExportService] Logo load failed', error); + return null; + } } -export interface DescriptiveStatsData { - pathologyName?: string; - variables: any[]; - models: any[]; - charts?: NodeListOf<HTMLElement>; - nonNominalVariables?: any[]; - nominalCharts?: NodeListOf<HTMLElement>; - nominalVariables?: any[]; - mipVersion?: string | null; +function plural(count: number, noun: string): string { + return `${count} ${noun}${count === 1 ? '' : 's'}`; } -@Injectable({ - providedIn: 'root' -}) +@Injectable({ providedIn: 'root' }) export class PdfExportService { + /** Experiment report: setup, contents, every result table, then every result chart. */ + async exportExperimentPdf(payload: ExperimentPdfPayload): Promise<void> { + const { details } = payload; + if (!details?.experimentName) { + console.warn('PdfExportService: missing experiment details.'); + return; + } + + const tables = experimentTables(payload.algorithmKey, payload.result); + const charts = payload.chartContainer ? await captureCharts(payload.chartContainer) : []; + const chartTitles = charts.map((chart, index) => chart.title || `Figure ${index + 1}`); + + const report = new PdfReport(); + report.title(details.experimentName, 'Experiment report'); + report.heading('About this experiment'); + report.context(experimentContextRows(details)); + report.contents( + tables.length || charts.length + ? [ + ...tables.map((table, index) => `Table: ${table.title || `Results table ${index + 1}`}`), + ...chartTitles.map((title) => `Figure: ${title}`), + ] + : ['No result data available for this experiment.'], + ); + + if (tables.length || charts.length) { + report.newPage(); + report.heading('Results'); + tables.forEach((table, index) => report.table(table, `Results table ${index + 1}`)); + charts.forEach((chart, index) => report.chart(chart, chartTitles[index])); + } - constructor() { } - - async exportDistributionPdf( - element: HTMLElement, - options: PdfExportOptions - ): Promise<void> { - document.body.classList.add('pdf-exporting'); - await new Promise(res => setTimeout(res, 50)); - - try { - const doc = new jsPDF('p', 'mm', 'a4'); - const { title, nodeLabel, modelLabel, datasetLabels, description, meta, isGroupView } = options; - - doc.setFontSize(16); - const titleLines = doc.splitTextToSize(title, 180); - doc.text(titleLines, 12, 14); - - let cursorY = 14 + (titleLines.length * 6); - - if (nodeLabel) { - doc.setFontSize(11); - const nodeLabelLines = doc.splitTextToSize(nodeLabel, 180); - doc.text(nodeLabelLines, 12, cursorY); - cursorY += (nodeLabelLines.length * 5); - } - - cursorY += 2; - if (modelLabel) { - doc.setFontSize(9); - doc.text(`Data model: ${modelLabel}`, 12, cursorY); - cursorY += 5; - } - if (!isGroupView && datasetLabels.length) { - doc.setFontSize(9); - const datasetLines = doc.splitTextToSize(`Datasets: ${datasetLabels.join(', ')}`, 180); - doc.text(datasetLines, 12, cursorY); - cursorY += datasetLines.length * 4 + 1; - } - if (!isGroupView && description) { - doc.setFontSize(9); - const descriptionLines = doc.splitTextToSize(`Description: ${description}`, 180); - doc.text(descriptionLines, 12, cursorY); - cursorY += descriptionLines.length * 4 + 1; - } - if (!isGroupView && meta?.pathNodes?.length) { - const pathText = meta.pathNodes.map((node) => node.label).join(' > '); - doc.setFontSize(9); - const pathLines = doc.splitTextToSize(`Path: ${pathText}`, 180); - doc.text(pathLines, 12, cursorY); - cursorY += pathLines.length * 4 + 2; - } - if (!isGroupView && meta) { - doc.setFontSize(9); - const countLabel = meta.hasGroups ? 'Number of groups in' : 'Number of variables in'; - doc.text(`${countLabel} ${nodeLabel}: ${meta.groupCount}`, 12, cursorY); - cursorY += 6; - } - - const svgElement = element.querySelector('#histogram-chart svg') as SVGSVGElement | null; - let imgData: string | null = null; - let rawWidth = 0; - let rawHeight = 0; - - if (svgElement) { - const cloned = svgElement.cloneNode(true) as SVGSVGElement; - if (!cloned.getAttribute('xmlns')) { - cloned.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); - } - const widthAttr = cloned.getAttribute('width'); - const heightAttr = cloned.getAttribute('height'); - rawWidth = widthAttr ? parseFloat(widthAttr) : svgElement.getBoundingClientRect().width; - rawHeight = heightAttr ? parseFloat(heightAttr) : svgElement.getBoundingClientRect().height; - - const serializer = new XMLSerializer(); - const svgData = serializer.serializeToString(cloned); - const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' }); - const url = URL.createObjectURL(svgBlob); - - imgData = await new Promise<string | null>((resolve) => { - const img = new Image(); - img.onload = () => { - const qualityScale = 3; // Increase resolution - const canvas = document.createElement('canvas'); - const width = (rawWidth || img.width) * qualityScale; - const height = (rawHeight || img.height) * qualityScale; - canvas.width = width; - canvas.height = height; - const ctx = canvas.getContext('2d'); - if (ctx) { - ctx.fillStyle = '#ffffff'; - ctx.fillRect(0, 0, width, height); - ctx.drawImage(img, 0, 0, width, height); - resolve(canvas.toDataURL('image/png', 1.0)); - } else { - resolve(null); - } - URL.revokeObjectURL(url); - }; - img.onerror = () => { - URL.revokeObjectURL(url); - resolve(null); - }; - img.src = url; - }); - } - - if (!imgData) { - const canvas = await html2canvas(element, { - backgroundColor: '#ffffff', - scale: 3.0, - useCORS: true, - logging: false, - }); - imgData = canvas.toDataURL('image/png'); - rawWidth = canvas.width; - rawHeight = canvas.height; - } - - const pageWidth = 210; - const margin = 12; - const maxWidth = 150; // Reduced from 186 for better proportion and quality - const imgHeight = rawWidth && rawHeight ? (rawHeight * maxWidth) / rawWidth : 120; - const startY = cursorY; - const maxHeight = 297 - startY - margin; - - let renderWidth = maxWidth; - let renderHeight = imgHeight; - if (imgHeight > maxHeight) { - renderHeight = maxHeight; - renderWidth = rawWidth && rawHeight ? (rawWidth * renderHeight) / rawHeight : maxWidth; - } - - const renderX = (pageWidth - renderWidth) / 2; - doc.addImage(imgData, 'PNG', renderX, startY, renderWidth, renderHeight); - doc.save(`${nodeLabel ? nodeLabel.replace(/[^\w\s-]/g, '').trim() : 'distribution'}_summary.pdf`); - } catch (err) { - console.error('Distribution PDF export failed:', err); - throw err; - } finally { - document.body.classList.remove('pdf-exporting'); - } + await report.save(payload.filename || details.experimentName || 'experiment-report', details.mipVersion); + } + + /** Descriptive statistics: setup, one table per variable, then box plots and frequency charts. */ + async exportDescriptiveStatisticsPdf(data: DescriptiveStatsData): Promise<void> { + const numeric = await this.labelledCharts(data.charts, data.nonNominalVariables, 'Variable'); + const nominal = await this.labelledCharts(data.nominalCharts, data.nominalVariables, 'Nominal variable'); + + const report = new PdfReport(); + report.title(`Descriptive statistics · ${data.title}`, data.pathologyName); + report.heading('About this export'); + report.context([ + ['Pathology', data.pathologyName], + ...(data.context ?? []), + ['Variables', data.variables.map((variable) => variable.name)], + ]); + report.contents([ + `Summary tables (${plural(data.variables.length, 'variable')})`, + ...(numeric.length ? [`Box plots (${plural(numeric.length, 'chart')})`] : []), + ...(nominal.length ? [`Frequency charts (${plural(nominal.length, 'chart')})`] : []), + ]); + + report.newPage(); + report.heading('Summary tables'); + data.variables.forEach((variable) => + report.table({ + title: variable.name, + columns: ['Metric', ...variable.columns], + rows: variable.rows.map((row) => [row.metric, ...variable.columns.map((column) => row.values[column])]), + }) + ); + if (numeric.length) { + report.heading('Box plots (numeric variables)'); + numeric.forEach(({ image, title }) => report.chart(image, title)); + } + if (nominal.length) { + report.heading('Frequency charts (nominal variables)'); + nominal.forEach(({ image, title }) => report.chart(image, title)); + } + + await report.save(`descriptive_statistics_${data.title}`, data.mipVersion); + } + + /** One variable's (or group's) chart with where it sits in the data model. */ + async exportHistogramPdf(element: HTMLElement, options: HistogramPdfOptions): Promise<void> { + const { meta, isGroupView } = options; + const image = await captureChart(element.querySelector<HTMLElement>('#histogram-chart') ?? element); + + const report = new PdfReport(); + report.title(options.nodeLabel || options.title, options.title); + report.heading('About this export'); + report.context([ + ['Pathology', options.modelLabel], + ['Datasets', isGroupView ? null : options.datasetLabels], + ['Path', isGroupView ? null : meta?.pathNodes?.map((node) => node.label).join(' > ')], + ['Description', isGroupView ? null : options.description], + [meta?.hasGroups ? 'Groups' : 'Variables', !isGroupView && meta ? String(meta.groupCount) : null], + ['Chart', isGroupView + ? `Number of variables in each group of ${options.nodeLabel || 'this group'}.` + : 'Number of records in each category or value range, across the selected datasets.'], + ]); + if (image) { + report.chart(image, image.title || options.nodeLabel || options.title); + } else { + report.paragraph('The chart could not be captured.'); } - async exportDescriptiveStatisticsPdf(data: DescriptiveStatsData): Promise<void> { - document.body.classList.add('pdf-exporting'); - await new Promise(res => setTimeout(res, 50)); - - const doc = new jsPDF(); - let yOffset = 10; - - try { - // Add pathology name header if provided - if (data.pathologyName) { - doc.setFontSize(16); - doc.setFont('helvetica', 'bold'); - const pathLines = doc.splitTextToSize(data.pathologyName, 180); - doc.text(pathLines, 10, yOffset); - yOffset += (pathLines.length * 7); - doc.setFont('helvetica', 'normal'); - } - - const addSection = (title: string, tableData: any[]) => { - if (!tableData?.length) return; - doc.setFontSize(14); - doc.text(title, 10, yOffset); - yOffset += 8; - tableData.forEach((v: any) => { - doc.setFontSize(12); - doc.text(v.name, 10, yOffset); - yOffset += 6; - const head = [['Metric', ...v.columns]]; - const body = v.rows.map((r: any) => [ - r.metric, - ...v.columns.map((ds: any) => r.values[ds]) - ]); - autoTable(doc, { - startY: yOffset, - head, - body, - styles: { fontSize: 9 }, - margin: { left: 10, right: 10 }, - }); - const last = (doc as any).lastAutoTable?.finalY ?? yOffset; - yOffset = last + 10; - if (yOffset > 270) { doc.addPage(); yOffset = 20; } - }); - }; - - // Variables + Model - addSection('Variables', data.variables || []); - addSection('Model', data.models || []); - - // Boxplots (Numeric Variables) - if (data.charts?.length && data.nonNominalVariables?.length) { - for (let i = 0; i < data.charts.length; i++) { - doc.addPage(); - yOffset = 20; - - if (i === 0) { - doc.setFontSize(14); - doc.text('Box Plot Charts (Numeric)', 10, yOffset); - yOffset += 10; - } - - const label = - data.nonNominalVariables[i]?.name || - data.nonNominalVariables[i]?.label || - `Variable ${i + 1}`; - const chartEl = data.charts[i]; - if (!chartEl) continue; - - try { - const canvas = await html2canvas(chartEl, { - backgroundColor: '#ffffff', - scale: 3, - useCORS: true, - logging: false, - }); - const imgData = canvas.toDataURL('image/png'); - const imgWidth = 180; - const imgHeight = (canvas.height * imgWidth) / canvas.width; - - doc.setFontSize(11); - doc.text(label, 15, yOffset); - yOffset += 6; - - doc.addImage(imgData, 'PNG', 15, yOffset, imgWidth, imgHeight); - yOffset += imgHeight + 12; - } catch (err) { - console.warn(`Failed to render chart for ${label}`, err); - doc.text(`${label} — (chart not ready)`, 15, yOffset); - yOffset += 10; - } - } - } - - // Nominal Charts (Grouped Bar Charts) - if (data.nominalCharts?.length && data.nominalVariables?.length) { - for (let i = 0; i < data.nominalCharts.length; i++) { - doc.addPage(); - yOffset = 20; - - if (i === 0) { - doc.setFontSize(14); - doc.text('Frequency Charts (Nominal)', 10, yOffset); - yOffset += 10; - } - - const label = - data.nominalVariables[i]?.name || - data.nominalVariables[i]?.label || - `Nominal Variable ${i + 1}`; - const chartEl = data.nominalCharts[i]; - if (!chartEl) continue; - - try { - const canvas = await html2canvas(chartEl, { - backgroundColor: '#ffffff', - scale: 3, - useCORS: true, - logging: false, - }); - const imgData = canvas.toDataURL('image/png'); - const imgWidth = 180; - const imgHeight = (canvas.height * imgWidth) / canvas.width; - - doc.setFontSize(11); - doc.text(label, 15, yOffset); - yOffset += 6; - - doc.addImage(imgData, 'PNG', 15, yOffset, imgWidth, imgHeight); - yOffset += imgHeight + 12; - } catch (err) { - console.warn(`Failed to render nominal chart for ${label}`, err); - doc.text(`${label} — (chart not ready)`, 15, yOffset); - yOffset += 10; - } - } - } - - if (data.mipVersion) { - const totalPages = (doc as any).getNumberOfPages(); - doc.setPage(totalPages); - doc.setFont('helvetica', 'italic'); - doc.setFontSize(9); - doc.setTextColor(150); - const versionText = `MIP Version: ${data.mipVersion}`; - const textWidth = doc.getTextWidth(versionText); - const pageWidth = doc.internal.pageSize.getWidth(); - const pageHeight = doc.internal.pageSize.getHeight(); - doc.text(versionText, pageWidth - 10 - textWidth, pageHeight - 10); - } - - doc.save('descriptive_statistics.pdf'); - } catch (err) { - console.error('Descriptive statistics PDF export failed:', err); - throw err; - } finally { - document.body.classList.remove('pdf-exporting'); - } + await report.save(`${options.nodeLabel || 'histogram'}_summary`, options.mipVersion); + } + + private async labelledCharts( + elements: NodeListOf<HTMLElement> | undefined, + variables: Array<{ name?: string; label?: string }> | undefined, + fallback: string, + ): Promise<Array<{ image: ChartImage; title: string }>> { + const out: Array<{ image: ChartImage; title: string }> = []; + for (const [index, element] of Array.from(elements ?? []).entries()) { + const label = variables?.[index]?.name || variables?.[index]?.label || `${fallback} ${index + 1}`; + try { + const images = await captureCharts(element); + images.forEach((image) => out.push({ image, title: image.title && image.title !== label ? `${label} · ${image.title}` : label })); + } catch (error) { + console.warn('[PdfExportService] Chart capture failed', label, error); + } } + return out; + } +} + +/** Result tables through the same registry the result view uses. */ +export function experimentTables(algorithmKey?: string | null, result?: unknown): TableSpec[] { + if (!algorithmKey || !result) return []; + const builder = getAlgorithmTableBuilder(algorithmKey); + if (!builder) return []; + try { + return builder(result) || []; + } catch (error) { + console.warn('[PdfExportService] Table builder failed', error); + return []; + } } diff --git a/src/app/services/runtime-env.service.spec.ts b/src/app/services/runtime-env.service.spec.ts new file mode 100644 index 000000000..d0fd8a160 --- /dev/null +++ b/src/app/services/runtime-env.service.spec.ts @@ -0,0 +1,108 @@ +import { isDevMode } from '@angular/core'; +import { RuntimeEnvService } from './runtime-env.service'; + +type RuntimeWindow = Window & { + __env?: { + MIP_VERSION?: unknown; + GUIDE_COVARIATE?: unknown; + GUIDE_VARIABLE?: unknown; + NOTEBOOK_ENABLED?: unknown; + JUPYTER_CONTEXT_PATH?: unknown; + }; +}; + +describe('RuntimeEnvService', () => { + let originalEnv: RuntimeWindow['__env']; + + beforeEach(() => { + originalEnv = (window as RuntimeWindow).__env; + vi.spyOn(console, 'warn').mockReturnValue(undefined); + }); + + afterEach(() => { + if (originalEnv === undefined) { + delete (window as RuntimeWindow).__env; + return; + } + + (window as RuntimeWindow).__env = originalEnv; + }); + + it('builds footer version entries from the MIP version only', () => { + (window as RuntimeWindow).__env = { + MIP_VERSION: 'mip-testing', + }; + + const service = new RuntimeEnvService(); + + expect(service.mipVersion).toBe('mip-testing'); + expect(service.versionEntries).toEqual([{ label: 'Version', value: 'mip-testing' }]); + }); + + it('uses the expected missing-version behavior for the current Angular mode', () => { + (window as RuntimeWindow).__env = { + MIP_VERSION: '', + }; + + const service = new RuntimeEnvService(); + + if (isDevMode()) { + expect(service.mipVersion).toBe('9.0.0'); + expect(service.versionEntries).toEqual([{ label: 'Version', value: '9.0.0' }]); + expect(console.warn).not.toHaveBeenCalled(); + return; + } + + expect(service.mipVersion).toBeNull(); + expect(service.versionEntries).toEqual([]); + expect(console.warn).toHaveBeenCalledWith( + '[RuntimeEnv] Expected MIP version in window.__env.MIP_VERSION, but it was not provided. The UI will hide that version until it is set.' + ); + }); + + it('reads configurable guide targets from the runtime env', () => { + (window as RuntimeWindow).__env = { + MIP_VERSION: 'mip-testing', + GUIDE_COVARIATE: 'biol_sex', + GUIDE_VARIABLE: 'age_years', + }; + + const service = new RuntimeEnvService(); + + expect(service.guideCovariate).toEqual({ value: 'biol_sex', label: 'Biol Sex' }); + expect(service.guideVariable).toEqual({ value: 'age_years', label: 'Age Years' }); + }); + + it('falls back to the default guide targets when runtime guide env vars are missing', () => { + (window as RuntimeWindow).__env = { + MIP_VERSION: 'mip-testing', + }; + + const service = new RuntimeEnvService(); + + expect(service.guideCovariate).toEqual({ value: 'Sex', label: 'Sex' }); + expect(service.guideVariable).toEqual({ value: 'Age', label: 'Age' }); + }); + + it('reads notebook enablement from the runtime env', () => { + (window as RuntimeWindow).__env = { + MIP_VERSION: 'mip-testing', + NOTEBOOK_ENABLED: '1', + }; + + const service = new RuntimeEnvService(); + + expect(service.notebookEnabled).toBe(true); + }); + + it('normalizes the notebook context path from the runtime env', () => { + (window as RuntimeWindow).__env = { + MIP_VERSION: 'mip-testing', + JUPYTER_CONTEXT_PATH: 'notebook/', + }; + + const service = new RuntimeEnvService(); + + expect(service.jupyterContextPath).toBe('/notebook'); + }); +}); diff --git a/src/app/services/runtime-env.service.ts b/src/app/services/runtime-env.service.ts new file mode 100644 index 000000000..2d347e19d --- /dev/null +++ b/src/app/services/runtime-env.service.ts @@ -0,0 +1,135 @@ +import { Injectable, isDevMode } from '@angular/core'; + +interface RuntimeEnv { + MIP_VERSION?: unknown; + GUIDE_COVARIATE?: unknown; + GUIDE_VARIABLE?: unknown; + NOTEBOOK_ENABLED?: unknown; + JUPYTER_CONTEXT_PATH?: unknown; +} + +interface RuntimeVersionEntry { + label: string; + value: string; +} + +interface RuntimeGuideTarget { + value: string; + label: string; +} + +interface RuntimeVersions { + mip: string | null; +} + +@Injectable({ providedIn: 'root' }) +export class RuntimeEnvService { + private readonly runtimeEnv = this.readRuntimeEnv(); + + readonly versions = this.readVersions(); + readonly versionEntries = this.buildVersionEntries(); + readonly mipVersion = this.versions.mip; + readonly guideCovariate = this.readGuideTarget('GUIDE_COVARIATE', 'Sex'); + readonly guideVariable = this.readGuideTarget('GUIDE_VARIABLE', 'Age'); + readonly notebookEnabled = this.readBoolean('NOTEBOOK_ENABLED', false); + readonly jupyterContextPath = this.readPath('JUPYTER_CONTEXT_PATH', '/notebook'); + + private readRuntimeEnv(): RuntimeEnv { + if (typeof window === 'undefined') { + return {}; + } + + return (window as Window & { __env?: RuntimeEnv }).__env ?? {}; + } + + private readVersions(): RuntimeVersions { + return { + mip: this.readVersion('MIP_VERSION', 'MIP'), + }; + } + + private buildVersionEntries(): RuntimeVersionEntry[] { + const entries = [ + { label: 'Version', value: this.versions.mip }, + ]; + + return entries.filter((entry): entry is RuntimeVersionEntry => entry.value !== null); + } + + private readVersion(key: keyof RuntimeEnv, label: string): string | null { + const value = this.readString(key); + if (value) { + return value; + } + + if (isDevMode()) { + return '9.0.0'; + } + + console.warn( + `[RuntimeEnv] Expected ${label} version in window.__env.${key}, but it was not provided. The UI will hide that version until it is set.` + ); + return null; + } + + private readGuideTarget(key: keyof RuntimeEnv, fallback: string): RuntimeGuideTarget { + const value = this.readString(key) ?? fallback; + + return { + value, + label: this.formatGuideTargetLabel(value), + }; + } + + private readString(key: keyof RuntimeEnv): string | null { + const rawValue = this.runtimeEnv[key]; + if (rawValue === null || rawValue === undefined) { + return null; + } + + const value = String(rawValue).trim(); + return value.length > 0 ? value : null; + } + + private readPath(key: keyof RuntimeEnv, fallback: string): string { + const value = this.readString(key) ?? fallback; + const withLeadingSlash = value.startsWith('/') ? value : '/' + value; + return withLeadingSlash.endsWith('/') && withLeadingSlash.length > 1 + ? withLeadingSlash.slice(0, -1) + : withLeadingSlash; + } + + private readBoolean(key: keyof RuntimeEnv, defaultValue: boolean): boolean { + const rawValue = this.runtimeEnv[key]; + if (rawValue === null || rawValue === undefined) { + return defaultValue; + } + if (typeof rawValue === 'boolean') { + return rawValue; + } + const normalized = String(rawValue).trim().toLowerCase(); + if (normalized === '1' || normalized === 'true' || normalized === 'yes' || normalized === 'on') { + return true; + } + if (normalized === '0' || normalized === 'false' || normalized === 'no' || normalized === 'off') { + return false; + } + return defaultValue; + } + + private formatGuideTargetLabel(value: string): string { + const normalized = value + .trim() + .replace(/[_-]+/g, ' ') + .replace(/\s+/g, ' '); + + if (!normalized) { + return value; + } + + return normalized + .split(' ') + .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) + .join(' '); + } +} diff --git a/src/app/services/theme.service.ts b/src/app/services/theme.service.ts deleted file mode 100644 index 4381569e0..000000000 --- a/src/app/services/theme.service.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Injectable, signal, effect } from '@angular/core'; - -export type Theme = 'light' | 'dark'; - -@Injectable({ - providedIn: 'root' -}) -export class ThemeService { - private readonly STORAGE_KEY = 'mip-theme'; - - // Reactive signal for current theme - theme = signal<Theme>(this.getInitialTheme()); - - // Apply theme class to body whenever theme changes - private _themeEffect = effect(() => { - this.applyTheme(this.theme()); - }); - - constructor() { } - - private getInitialTheme(): Theme { - // Check localStorage first - const stored = localStorage.getItem(this.STORAGE_KEY) as Theme | null; - if (stored === 'light' || stored === 'dark') { - return stored; - } - // Fall back to system preference - if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { - return 'dark'; - } - return 'light'; - } - - private applyTheme(theme: Theme): void { - document.body.classList.remove('theme-light', 'theme-dark'); - document.body.classList.add(`theme-${theme}`); - localStorage.setItem(this.STORAGE_KEY, theme); - } - - toggleTheme(): void { - this.theme.update(current => current === 'light' ? 'dark' : 'light'); - } - - isDark(): boolean { - return this.theme() === 'dark'; - } -} diff --git a/src/app/testing/spy-obj.testing.ts b/src/app/testing/spy-obj.testing.ts new file mode 100644 index 000000000..17385fecd --- /dev/null +++ b/src/app/testing/spy-obj.testing.ts @@ -0,0 +1,14 @@ +import type { MockedObject } from 'vitest'; + +/** + * Vitest stand-in for `jasmine.createSpyObj`: one named `vi.fn()` per method, + * plus plain properties (signals, values) copied as they are. + */ +export function createSpyObj<T = any>( + baseName: string, + methods: readonly string[], + properties: object = {}, +): MockedObject<T> { + const spies = Object.fromEntries(methods.map((method) => [method, vi.fn().mockName(`${baseName}.${method}`)])); + return { ...spies, ...properties } as unknown as MockedObject<T>; +} diff --git a/src/assets/env.js b/src/assets/env.js new file mode 100644 index 000000000..a7a27d9c1 --- /dev/null +++ b/src/assets/env.js @@ -0,0 +1,10 @@ +(function (window) { + window.__env = window.__env || {}; + + // Environment variables + window.__env.MIP_VERSION = ''; + window.__env.NOTEBOOK_ENABLED = '0'; + window.__env.JUPYTER_CONTEXT_PATH = '/notebook'; + window.__env.GUIDE_COVARIATE = 'Sex'; + window.__env.GUIDE_VARIABLE = 'Age'; +}(this)); diff --git a/src/assets/footer/CHUV_Logo_Simple_BLANC.png b/src/assets/footer/CHUV_Logo_Simple_BLANC.png new file mode 100644 index 000000000..28205b90c Binary files /dev/null and b/src/assets/footer/CHUV_Logo_Simple_BLANC.png differ diff --git a/src/assets/athenarc.png b/src/assets/footer/athenarc.png similarity index 100% rename from src/assets/athenarc.png rename to src/assets/footer/athenarc.png diff --git a/src/assets/footer/eu-flag.svg b/src/assets/footer/eu-flag.svg new file mode 100644 index 000000000..b23b36e8a --- /dev/null +++ b/src/assets/footer/eu-flag.svg @@ -0,0 +1,15 @@ +<svg width="56" height="38" viewBox="0 0 56 38" fill="none" xmlns="http://www.w3.org/2000/svg"> + <rect width="56" height="38" rx="2" fill="#004494"/> + <circle cx="28" cy="7" r="1.4" fill="#FFCC00"/> + <circle cx="34.5" cy="8.75" r="1.4" fill="#FFCC00"/> + <circle cx="39.25" cy="13.5" r="1.4" fill="#FFCC00"/> + <circle cx="41" cy="20" r="1.4" fill="#FFCC00"/> + <circle cx="39.25" cy="26.5" r="1.4" fill="#FFCC00"/> + <circle cx="34.5" cy="31.25" r="1.4" fill="#FFCC00"/> + <circle cx="28" cy="33" r="1.4" fill="#FFCC00"/> + <circle cx="21.5" cy="31.25" r="1.4" fill="#FFCC00"/> + <circle cx="16.75" cy="26.5" r="1.4" fill="#FFCC00"/> + <circle cx="15" cy="20" r="1.4" fill="#FFCC00"/> + <circle cx="16.75" cy="13.5" r="1.4" fill="#FFCC00"/> + <circle cx="21.5" cy="8.75" r="1.4" fill="#FFCC00"/> +</svg> diff --git a/src/assets/footer/logo_ebrains.png b/src/assets/footer/logo_ebrains.png new file mode 100644 index 000000000..65def5ee3 Binary files /dev/null and b/src/assets/footer/logo_ebrains.png differ diff --git a/src/assets/logo_chuv.png b/src/assets/logo_chuv.png deleted file mode 100644 index c27289fad..000000000 Binary files a/src/assets/logo_chuv.png and /dev/null differ diff --git a/src/assets/logo_ebrains.png b/src/assets/logo_ebrains.png deleted file mode 100644 index 1adc2e10f..000000000 Binary files a/src/assets/logo_ebrains.png and /dev/null differ diff --git a/src/assets/mip-logo-extracted.svg b/src/assets/mip-logo-extracted.svg deleted file mode 100644 index 115d4da69..000000000 --- a/src/assets/mip-logo-extracted.svg +++ /dev/null @@ -1,925 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1190.55pt" height="841.89pt" viewBox="0 0 1190.55 841.89" version="1.2"> -<defs> -<g> -<symbol overflow="visible" id="glyph0-0"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph0-1"> -<path style="stroke:none;" d="M 6.1875 0 C 6.71875 0 7.125 -0.046875 7.609375 -0.125 C 10.140625 -0.53125 12.25 -2.765625 12.25 -5.484375 C 12.25 -8.21875 10.4375 -10.40625 7.703125 -10.84375 C 7.21875 -10.921875 6.71875 -10.96875 6.1875 -10.96875 L 0.65625 -10.96875 L 0.65625 -10.09375 L 1.796875 -10.078125 L 1.796875 -0.875 L 0.65625 -0.859375 L 0.65625 0 Z M 3.203125 -0.875 L 3.203125 -10.078125 L 6.109375 -10.078125 C 8.796875 -10.078125 10.796875 -8.265625 10.796875 -5.484375 C 10.796875 -2.671875 8.859375 -0.875 6.109375 -0.875 Z M 3.203125 -0.875 "/> -</symbol> -<symbol overflow="visible" id="glyph0-2"> -<path style="stroke:none;" d="M 0.53125 -5.3125 C 0.53125 -2.234375 2.875 0.125 6.046875 0.125 C 9.328125 0.125 11.78125 -2.546875 11.78125 -5.703125 C 11.78125 -8.796875 9.4375 -11.15625 6.265625 -11.15625 C 2.984375 -11.15625 0.53125 -8.484375 0.53125 -5.3125 Z M 1.984375 -5.703125 C 1.984375 -8.296875 3.359375 -10.15625 6 -10.15625 C 8.78125 -10.15625 10.328125 -7.875 10.328125 -5.3125 C 10.328125 -2.71875 8.953125 -0.875 6.3125 -0.875 C 3.515625 -0.875 1.984375 -3.15625 1.984375 -5.703125 Z M 1.984375 -5.703125 "/> -</symbol> -<symbol overflow="visible" id="glyph0-3"> -<path style="stroke:none;" d="M 11.421875 0 L 12.40625 0 L 12.40625 -10.078125 L 13.546875 -10.09375 L 13.546875 -10.96875 L 10.25 -10.96875 L 10.25 -10.09375 L 11.421875 -10.078125 L 11.421875 -2.75 L 4.703125 -10.09375 L 5.953125 -10.09375 L 5.953125 -10.96875 L 0.65625 -10.96875 L 0.65625 -10.09375 L 1.796875 -10.078125 L 1.796875 -0.875 L 0.65625 -0.859375 L 0.65625 0 L 4.25 0 L 4.25 -0.859375 L 2.78125 -0.875 L 2.78125 -10.078125 L 11.421875 -0.84375 Z M 11.421875 0 "/> -</symbol> -<symbol overflow="visible" id="glyph0-4"> -<path style="stroke:none;" d="M 2.46875 -10.078125 L 3.578125 -10.078125 L 3.578125 -10.953125 L 0 -10.953125 L 0 -10.078125 L 0.96875 -10.078125 L 5.265625 0.125 L 5.765625 0.125 L 10.125 -10.078125 L 11.265625 -10.078125 L 11.265625 -10.953125 L 7.90625 -10.953125 L 7.90625 -10.078125 L 9.09375 -10.078125 L 5.75 -2.25 Z M 2.46875 -10.078125 "/> -</symbol> -<symbol overflow="visible" id="glyph0-5"> -<path style="stroke:none;" d="M 6.21875 -11.140625 L 5.078125 -11.140625 L 1.140625 -0.875 L 0 -0.859375 L 0 0 L 3.359375 0 L 3.359375 -0.859375 L 2.171875 -0.875 L 3.1875 -3.5625 L 7.640625 -3.5625 L 8.625 -0.875 L 7.515625 -0.859375 L 7.515625 0 L 11.109375 0 L 11.109375 -0.859375 L 10.140625 -0.875 Z M 3.515625 -4.421875 L 5.421875 -9.515625 L 7.328125 -4.421875 Z M 3.515625 -4.421875 "/> -</symbol> -<symbol overflow="visible" id="glyph0-6"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph0-7"> -<path style="stroke:none;" d="M 4.984375 -4.734375 C 6.359375 -4.109375 6.90625 -3.59375 6.90625 -2.640625 C 6.90625 -1.515625 5.890625 -0.703125 4.40625 -0.703125 C 2.75 -0.703125 1.734375 -1.546875 0.8125 -3.28125 L 0.296875 -3.125 L 0.953125 0.03125 L 1.34375 0.03125 L 1.71875 -0.484375 C 2.546875 -0.109375 3.46875 0.125 4.375 0.125 C 6.421875 0.125 8.265625 -1.015625 8.265625 -3.015625 C 8.265625 -4.25 7.484375 -5.21875 5.953125 -5.875 L 4.015625 -6.734375 C 2.671875 -7.328125 2.265625 -8.1875 2.265625 -8.828125 C 2.265625 -9.59375 2.875 -10.328125 4.296875 -10.328125 C 5.953125 -10.328125 6.921875 -9.390625 7.28125 -7.953125 L 7.78125 -8.046875 L 7.78125 -11.046875 L 7.40625 -11.046875 L 7.046875 -10.484375 C 6.203125 -10.921875 5.28125 -11.15625 4.3125 -11.15625 C 2.6875 -11.15625 1.03125 -10.234375 1.03125 -8.40625 C 1.03125 -7.0625 1.859375 -6.109375 3.65625 -5.3125 Z M 4.984375 -4.734375 "/> -</symbol> -<symbol overflow="visible" id="glyph0-8"> -<path style="stroke:none;" d="M 8.609375 -10.015625 C 9.328125 -10 9.796875 -9.875 9.796875 -9.15625 L 9.796875 -8.984375 L 10.25 -8.984375 L 10.921875 -11.53125 L 10.40625 -11.6875 C 9.953125 -11.125 9.6875 -10.96875 9.1875 -10.96875 C 7.828125 -10.921875 6.6875 -10.921875 5.390625 -10.921875 C 4.265625 -10.921875 3.171875 -10.9375 2.140625 -10.96875 C 1.4375 -10.96875 1.296875 -11.171875 1.171875 -11.53125 L 0.734375 -11.53125 L 0.0625 -9.0625 L 0.59375 -8.90625 C 1.203125 -9.796875 1.453125 -10 2.625 -10.03125 C 3.046875 -10.046875 3.609375 -10.0625 4.078125 -10.0625 L 4.6875 -10.0625 L 4.6875 -0.875 L 3.546875 -0.859375 L 3.546875 0 L 7.234375 0 L 7.234375 -0.859375 L 6.09375 -0.875 L 6.09375 -10.0625 L 6.78125 -10.0625 C 7.453125 -10.0625 7.921875 -10.046875 8.609375 -10.015625 Z M 8.609375 -10.015625 "/> -</symbol> -<symbol overflow="visible" id="glyph0-9"> -<path style="stroke:none;" d="M 9.0625 -4.046875 C 9.0625 -3.65625 9.0625 -3.359375 8.96875 -2.984375 C 8.625 -1.625 7.5 -1.03125 5.9375 -1.03125 C 3.90625 -1.03125 2.9375 -2.140625 2.9375 -4.046875 L 2.9375 -10.078125 L 4.078125 -10.09375 L 4.078125 -10.96875 L 0.390625 -10.96875 L 0.390625 -10.09375 L 1.53125 -10.078125 L 1.53125 -3.78125 C 1.53125 -1.109375 2.953125 0.125 5.703125 0.125 C 8.34375 0.125 10.046875 -1.140625 10.046875 -3.78125 L 10.046875 -10.078125 L 11.1875 -10.09375 L 11.1875 -10.96875 L 7.890625 -10.96875 L 7.890625 -10.09375 L 9.0625 -10.078125 Z M 9.0625 -4.046875 "/> -</symbol> -<symbol overflow="visible" id="glyph0-10"> -<path style="stroke:none;" d="M 4.328125 0 L 4.328125 -0.859375 L 3.203125 -0.875 L 3.203125 -10.078125 L 4.328125 -10.09375 L 4.328125 -10.96875 L 0.65625 -10.96875 L 0.65625 -10.09375 L 1.796875 -10.078125 L 1.796875 -0.875 L 0.65625 -0.859375 L 0.65625 0 Z M 4.328125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph1-0"> -<path style="stroke:none;" d="M 0.453125 -2.15625 L 3.46875 -5.171875 C 3.40625 -5.25 3.328125 -5.328125 3.234375 -5.375 L 0.453125 -2.59375 Z M 0.453125 -1.28125 L 2.421875 -3.25 C 2.5 -3.4375 2.609375 -3.59375 2.75 -3.734375 C 3.0625 -4.046875 3.578125 -4.296875 3.578125 -4.703125 L 3.578125 -4.84375 L 0.453125 -1.71875 Z M 0.9375 0 L 2.234375 -1.3125 L 2.234375 -1.515625 L 2.453125 -1.515625 L 3.28125 -2.34375 L 2.84375 -2.34375 L 0.5 0 Z M 2.890625 -1.515625 L 3.328125 -1.515625 L 5.34375 -3.53125 L 5.34375 -3.96875 Z M 0.453125 -3.046875 L 2.875 -5.46875 L 2.8125 -5.46875 C 2.359375 -5.46875 2.140625 -5.125 2.140625 -4.796875 L 1.78125 -4.8125 L 0.453125 -3.484375 Z M 3.578125 0 L 5.34375 -1.765625 L 5.34375 -2.203125 L 3.453125 -0.3125 L 3.453125 -0.296875 L 3.4375 -0.296875 L 3.140625 0 Z M 3.453125 -1.203125 L 3.453125 -0.75 L 5.34375 -2.640625 L 5.34375 -3.09375 Z M 0.453125 -0.390625 L 2.40625 -2.34375 L 2.296875 -2.34375 L 2.296875 -2.6875 L 0.453125 -0.84375 Z M 0.453125 -5.25 L 0.453125 -4.8125 L 0.9375 -5.296875 C 1.109375 -5.9375 1.609375 -6.4375 2.265625 -6.609375 L 2.65625 -7.015625 L 2.21875 -7.015625 Z M 4.828125 -4.34375 C 4.6875 -3.71875 4.21875 -3.4375 4.03125 -3.296875 C 3.640625 -3 3.40625 -2.90625 3.40625 -2.53125 L 3.40625 -2.46875 L 5.34375 -4.40625 L 5.34375 -4.84375 Z M 0.453125 -6.125 L 0.453125 -5.6875 L 1.765625 -7.015625 L 1.328125 -7.015625 Z M 4.46875 0 L 5.34375 -0.875 L 5.34375 -1.328125 L 4.015625 0 Z M 4.125 -6.28125 C 4.21875 -6.21875 4.28125 -6.140625 4.359375 -6.078125 L 5.296875 -7.015625 L 4.859375 -7.015625 Z M 0.453125 -3.921875 L 1.375 -4.84375 L 0.953125 -4.875 L 0.453125 -4.359375 Z M 4.546875 -5.828125 C 4.609375 -5.71875 4.671875 -5.640625 4.71875 -5.546875 L 5.34375 -6.171875 L 5.34375 -6.609375 Z M 1.8125 0 L 2.234375 -0.421875 L 2.234375 -0.859375 L 1.375 0 Z M 3.5625 -6.59375 C 3.65625 -6.546875 3.765625 -6.5 3.859375 -6.453125 L 4.421875 -7.015625 L 3.984375 -7.015625 Z M 4.828125 -5.21875 C 4.84375 -5.078125 4.875 -4.953125 4.875 -4.828125 L 5.34375 -5.296875 L 5.34375 -5.734375 Z M 2.765625 -6.671875 L 2.828125 -6.671875 C 2.859375 -6.671875 2.90625 -6.6875 2.9375 -6.6875 C 3.03125 -6.6875 3.109375 -6.671875 3.1875 -6.671875 L 3.53125 -7.015625 L 3.09375 -7.015625 Z M 2.703125 0 L 3 -0.296875 L 2.5625 -0.296875 L 2.265625 0 Z M 5.34375 -0.4375 L 4.90625 0 L 5.34375 0 Z M 0.453125 -7.015625 L 0.453125 -6.5625 L 0.890625 -7.015625 Z M 0.453125 -7.015625 "/> -</symbol> -<symbol overflow="visible" id="glyph1-1"> -<path style="stroke:none;" d="M 4.703125 0 L 4.703125 -0.8125 L 1.625 -0.8125 C 1.84375 -1.03125 2.28125 -1.34375 2.53125 -1.5625 L 3.046875 -1.984375 C 3.890625 -2.703125 4.671875 -3.5625 4.671875 -4.609375 C 4.671875 -5.734375 3.921875 -6.5625 2.5625 -6.5625 C 1.203125 -6.5625 0.453125 -5.671875 0.421875 -4.375 L 1.375 -4.375 C 1.40625 -5.25 1.8125 -5.71875 2.5625 -5.71875 C 3.25 -5.71875 3.65625 -5.28125 3.65625 -4.609375 C 3.65625 -3.921875 3.28125 -3.375 2.453125 -2.625 L 0.96875 -1.28125 C 0.6875 -1.03125 0.5 -0.84375 0.5 -0.8125 L 0.5 0 Z M 4.703125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph1-2"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph1-3"> -<path style="stroke:none;" d="M 0.21875 0.9375 L 1.09375 0.9375 L 3.6875 -6.4375 L 2.828125 -6.4375 Z M 0.21875 0.9375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-4"> -<path style="stroke:none;" d="M 2.234375 0 C 2.8125 -1.609375 3.65625 -3.59375 4.453125 -5.21875 L 4.671875 -5.65625 L 4.671875 -6.4375 L 0.640625 -6.4375 L 0.640625 -5.59375 L 3.46875 -5.59375 C 3.546875 -5.59375 3.640625 -5.59375 3.71875 -5.59375 C 3.328125 -4.828125 2.890625 -3.9375 2.546875 -3.15625 C 2.0625 -2.078125 1.609375 -1.09375 1.1875 0 Z M 2.234375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph1-5"> -<path style="stroke:none;" d="M 5.53125 -1.875 C 5.171875 -1.109375 4.546875 -0.671875 3.609375 -0.671875 C 2.21875 -0.671875 1.40625 -1.734375 1.40625 -3.234375 C 1.40625 -4.734375 2.21875 -5.765625 3.59375 -5.765625 C 4.609375 -5.765625 5.21875 -5.28125 5.5625 -4.484375 L 6.53125 -4.484375 C 6.125 -5.828125 5.109375 -6.5625 3.609375 -6.5625 C 1.703125 -6.5625 0.390625 -5.203125 0.390625 -3.21875 C 0.390625 -1.234375 1.6875 0.125 3.5625 0.125 C 5.078125 0.125 6.078125 -0.640625 6.53125 -1.875 Z M 5.53125 -1.875 "/> -</symbol> -<symbol overflow="visible" id="glyph1-6"> -<path style="stroke:none;" d="M 0.65625 -6.4375 L 0.65625 0 L 1.5625 0 L 1.5625 -2.75 C 1.5625 -3.46875 2 -4.09375 2.734375 -4.09375 C 3.484375 -4.09375 3.78125 -3.484375 3.78125 -2.5625 L 3.78125 0 L 4.703125 0 L 4.703125 -2.9375 C 4.703125 -4.125 4.125 -4.859375 3.046875 -4.859375 C 2.21875 -4.859375 1.734375 -4.421875 1.5625 -4.109375 C 1.5625 -4.28125 1.5625 -4.5 1.5625 -4.71875 L 1.5625 -6.4375 Z M 0.65625 -6.4375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-7"> -<path style="stroke:none;" d="M 4.53125 0 C 4.453125 -0.4375 4.40625 -0.984375 4.40625 -1.75 L 4.40625 -2.671875 C 4.40625 -4.515625 3.53125 -4.84375 2.53125 -4.84375 C 1.40625 -4.84375 0.59375 -4.25 0.546875 -3.25 L 1.4375 -3.25 C 1.5 -3.8125 1.828125 -4.125 2.515625 -4.125 C 3.125 -4.125 3.53125 -3.859375 3.53125 -2.90625 C 3.21875 -2.875 2.875 -2.84375 2.5625 -2.796875 C 1.296875 -2.625 0.328125 -2.28125 0.328125 -1.28125 C 0.328125 -0.390625 1.046875 0.09375 2.03125 0.09375 C 2.796875 0.09375 3.265625 -0.140625 3.625 -0.640625 C 3.640625 -0.296875 3.640625 -0.1875 3.671875 0 Z M 3.546875 -2.25 C 3.53125 -2.21875 3.53125 -2.1875 3.53125 -2.140625 L 3.53125 -2 C 3.53125 -1.078125 3.09375 -0.5625 2.234375 -0.5625 C 1.671875 -0.5625 1.3125 -0.84375 1.3125 -1.28125 C 1.3125 -1.9375 2 -2.109375 3.546875 -2.25 Z M 3.546875 -2.25 "/> -</symbol> -<symbol overflow="visible" id="glyph1-8"> -<path style="stroke:none;" d="M 0.640625 -4.71875 C 0.65625 -4.578125 0.65625 -4.4375 0.65625 -4.296875 L 0.65625 0 L 1.5625 0 L 1.5625 -2.453125 C 1.5625 -3.390625 1.734375 -4.015625 2.578125 -4.015625 C 2.6875 -4.015625 2.8125 -4.015625 2.890625 -4 L 2.890625 -4.828125 C 2.828125 -4.828125 2.78125 -4.84375 2.65625 -4.84375 C 2.125 -4.84375 1.671875 -4.5625 1.515625 -4.125 L 1.515625 -4.71875 Z M 0.640625 -4.71875 "/> -</symbol> -<symbol overflow="visible" id="glyph1-9"> -<path style="stroke:none;" d="M 1.796875 -6 L 0.875 -6 L 0.875 -4.71875 L 0.15625 -4.71875 L 0.15625 -4.015625 L 0.875 -4.015625 L 0.875 -1.140625 C 0.875 -0.1875 1.40625 0.09375 2.234375 0.09375 C 2.359375 0.09375 2.453125 0.078125 2.59375 0.046875 L 2.59375 -0.625 C 2.53125 -0.609375 2.4375 -0.609375 2.359375 -0.609375 C 2.015625 -0.609375 1.796875 -0.734375 1.796875 -1.1875 L 1.796875 -4.015625 L 2.671875 -4.015625 L 2.671875 -4.71875 L 1.796875 -4.71875 Z M 1.796875 -6 "/> -</symbol> -<symbol overflow="visible" id="glyph1-10"> -<path style="stroke:none;" d="M 4.984375 -2.125 L 4.984375 -2.421875 C 4.984375 -3.6875 4.265625 -4.859375 2.734375 -4.859375 C 1.3125 -4.859375 0.359375 -3.8125 0.359375 -2.34375 C 0.359375 -0.859375 1.265625 0.109375 2.734375 0.109375 C 3.859375 0.109375 4.609375 -0.5 4.875 -1.34375 L 3.96875 -1.34375 C 3.71875 -0.9375 3.40625 -0.640625 2.765625 -0.640625 C 1.859375 -0.640625 1.3125 -1.296875 1.28125 -2.125 Z M 1.3125 -2.84375 C 1.40625 -3.640625 1.96875 -4.125 2.703125 -4.125 C 3.484375 -4.125 3.953125 -3.59375 4.03125 -2.84375 Z M 1.3125 -2.84375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-11"> -<path style="stroke:none;" d="M 5.03125 -4.71875 L 4.125 -4.71875 L 4.125 -4.28125 C 4.125 -4.234375 4.125 -4.171875 4.140625 -4.125 C 3.859375 -4.515625 3.390625 -4.875 2.5625 -4.875 C 1.28125 -4.875 0.375 -3.921875 0.375 -2.515625 C 0.375 -1.171875 1.21875 -0.203125 2.546875 -0.203125 C 3.375 -0.203125 3.890625 -0.5625 4.125 -0.96875 L 4.15625 -0.96875 C 4.15625 0.328125 3.765625 0.984375 2.6875 0.984375 C 1.984375 0.984375 1.640625 0.609375 1.46875 0.171875 L 0.515625 0.171875 C 0.78125 1.109375 1.46875 1.734375 2.65625 1.734375 C 4.25 1.734375 5.03125 0.8125 5.03125 -0.90625 Z M 2.75 -4.109375 C 3.625 -4.109375 4.15625 -3.484375 4.15625 -2.5625 C 4.15625 -1.515625 3.546875 -0.953125 2.75 -0.953125 C 1.875 -0.953125 1.359375 -1.609375 1.359375 -2.53125 C 1.359375 -3.484375 1.890625 -4.109375 2.75 -4.109375 Z M 2.75 -4.109375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-12"> -<path style="stroke:none;" d="M 1.53125 -4.71875 L 0.65625 -4.71875 L 0.65625 1.625 L 1.5625 1.625 L 1.5625 0.515625 C 1.5625 -0.046875 1.546875 -0.5625 1.546875 -0.59375 C 1.828125 -0.1875 2.28125 0.109375 3.109375 0.109375 C 4.40625 0.109375 5.296875 -0.90625 5.296875 -2.375 C 5.296875 -3.8125 4.421875 -4.875 3.078125 -4.875 C 2.328125 -4.875 1.8125 -4.5 1.53125 -4.109375 Z M 2.921875 -4.109375 C 3.8125 -4.109375 4.3125 -3.375 4.3125 -2.359375 C 4.3125 -1.3125 3.8125 -0.640625 2.921875 -0.640625 C 2.03125 -0.640625 1.515625 -1.328125 1.515625 -2.359375 C 1.515625 -3.375 2.046875 -4.109375 2.921875 -4.109375 Z M 2.921875 -4.109375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-13"> -<path style="stroke:none;" d="M 0.65625 -4.71875 L 0.65625 0 L 1.5625 0 L 1.5625 -4.71875 Z M 0.640625 -5.40625 L 1.578125 -5.40625 L 1.578125 -6.4375 L 0.640625 -6.4375 Z M 0.640625 -5.40625 "/> -</symbol> -<symbol overflow="visible" id="glyph1-14"> -<path style="stroke:none;" d="M 4.109375 -4.71875 L 4.109375 -4.3125 C 4.109375 -4.265625 4.109375 -4.1875 4.109375 -4.109375 C 3.859375 -4.5 3.296875 -4.875 2.578125 -4.875 C 1.234375 -4.875 0.359375 -3.8125 0.359375 -2.375 C 0.359375 -0.90625 1.21875 0.109375 2.5625 0.109375 C 3.375 0.109375 3.84375 -0.234375 4.109375 -0.625 C 4.109375 -0.59375 4.09375 -0.125 4.09375 0.4375 L 4.09375 1.625 L 5 1.625 L 5 -4.71875 Z M 2.734375 -4.109375 C 3.609375 -4.109375 4.140625 -3.390625 4.140625 -2.390625 C 4.140625 -1.34375 3.625 -0.640625 2.734375 -0.640625 C 1.84375 -0.640625 1.328125 -1.3125 1.328125 -2.359375 C 1.328125 -3.375 1.84375 -4.109375 2.734375 -4.109375 Z M 2.734375 -4.109375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-15"> -<path style="stroke:none;" d="M 3.75 0 L 4.609375 0 C 4.609375 -0.109375 4.609375 -0.234375 4.609375 -0.34375 L 4.609375 -4.71875 L 3.703125 -4.71875 L 3.703125 -2.5625 C 3.703125 -1.515625 3.53125 -0.640625 2.5625 -0.640625 C 1.8125 -0.640625 1.515625 -1.1875 1.515625 -2.125 L 1.515625 -4.71875 L 0.609375 -4.71875 L 0.609375 -1.78125 C 0.609375 -0.546875 1.140625 0.109375 2.234375 0.109375 C 3.03125 0.109375 3.5 -0.265625 3.75 -0.671875 Z M 3.75 0 "/> -</symbol> -<symbol overflow="visible" id="glyph1-16"> -<path style="stroke:none;" d="M 1.734375 0 L 1.734375 -2.90625 L 4.71875 -2.90625 L 4.71875 0 L 5.65625 0 L 5.65625 -6.4375 L 4.71875 -6.4375 L 4.71875 -3.703125 L 1.734375 -3.703125 L 1.734375 -6.4375 L 0.78125 -6.4375 L 0.78125 0 Z M 1.734375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph1-17"> -<path style="stroke:none;" d="M 0.78125 0 L 1.734375 0 L 1.734375 -6.4375 L 0.78125 -6.4375 Z M 0.78125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph1-18"> -<path style="stroke:none;" d="M 0.78125 0 L 1.734375 0 L 1.734375 -2.53125 L 2.515625 -2.53125 C 3.265625 -2.53125 3.828125 -2.578125 4.328125 -2.890625 C 4.875 -3.25 5.125 -3.828125 5.125 -4.515625 C 5.125 -5.234375 4.796875 -5.828125 4.25 -6.125 C 3.75 -6.375 3.34375 -6.4375 2.515625 -6.4375 L 0.78125 -6.4375 Z M 1.734375 -5.59375 L 2.65625 -5.609375 C 3.640625 -5.609375 4.125 -5.28125 4.125 -4.5 C 4.125 -3.65625 3.515625 -3.34375 2.734375 -3.34375 L 1.734375 -3.34375 Z M 1.734375 -5.59375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-19"> -<path style="stroke:none;" d="M 0.625 -2.34375 L 4.90625 -2.34375 L 4.90625 -3.09375 L 0.625 -3.09375 Z M 0.625 -2.34375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-20"> -<path style="stroke:none;" d="M 0.78125 0 L 1.6875 0 L 1.6875 -3.40625 C 1.6875 -4.3125 1.6875 -5.046875 1.671875 -5.578125 L 1.6875 -5.578125 C 1.796875 -5.140625 1.921875 -4.65625 2.125 -4.046875 L 3.484375 0 L 4.484375 0 L 5.625 -3.390625 C 5.90625 -4.28125 6.234375 -5.25 6.3125 -5.578125 L 6.328125 -5.578125 C 6.328125 -5.3125 6.3125 -4.921875 6.3125 -4.5 L 6.296875 -3.875 L 6.296875 0 L 7.21875 0 L 7.21875 -6.4375 L 5.734375 -6.4375 L 4.609375 -2.984375 L 4.46875 -2.578125 L 4.3125 -2.109375 L 4.15625 -1.609375 L 4.015625 -1.15625 L 4 -1.15625 L 3.875 -1.578125 L 3.71875 -2.0625 L 3.453125 -2.921875 L 2.3125 -6.4375 L 0.78125 -6.4375 Z M 0.78125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph1-21"> -<path style="stroke:none;" d="M 2.578125 -6.5625 C 0.734375 -6.5625 0.3125 -4.9375 0.3125 -3.203125 C 0.3125 -1.296875 0.78125 0.140625 2.578125 0.140625 C 4.390625 0.140625 4.859375 -1.296875 4.859375 -3.203125 C 4.859375 -4.9375 4.4375 -6.5625 2.578125 -6.5625 Z M 2.578125 -5.796875 C 3.625 -5.796875 3.828125 -4.609375 3.828125 -3.203125 C 3.828125 -1.6875 3.59375 -0.625 2.578125 -0.625 C 1.578125 -0.625 1.328125 -1.6875 1.328125 -3.203125 C 1.328125 -4.609375 1.546875 -5.796875 2.578125 -5.796875 Z M 2.578125 -5.796875 "/> -</symbol> -<symbol overflow="visible" id="glyph1-22"> -<path style="stroke:none;" d="M 2.75 -4.84375 C 1.28125 -4.84375 0.359375 -3.8125 0.359375 -2.34375 C 0.359375 -0.90625 1.21875 0.109375 2.734375 0.109375 C 4.25 0.109375 5.140625 -0.90625 5.140625 -2.34375 C 5.140625 -3.8125 4.21875 -4.84375 2.75 -4.84375 Z M 2.734375 -4.09375 C 3.625 -4.09375 4.140625 -3.359375 4.140625 -2.359375 C 4.140625 -1.3125 3.625 -0.640625 2.734375 -0.640625 C 1.828125 -0.640625 1.328125 -1.3125 1.328125 -2.359375 C 1.328125 -3.359375 1.84375 -4.09375 2.734375 -4.09375 Z M 2.734375 -4.09375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-23"> -<path style="stroke:none;" d="M 4 -1.515625 C 3.78125 -0.96875 3.34375 -0.640625 2.734375 -0.640625 C 1.84375 -0.640625 1.328125 -1.3125 1.328125 -2.359375 C 1.328125 -3.359375 1.84375 -4.09375 2.734375 -4.09375 C 3.359375 -4.09375 3.8125 -3.75 4.015625 -3.171875 L 4.953125 -3.171875 C 4.734375 -4.171875 4 -4.84375 2.765625 -4.84375 C 1.3125 -4.84375 0.359375 -3.78125 0.359375 -2.375 C 0.359375 -0.9375 1.28125 0.109375 2.765625 0.109375 C 3.953125 0.109375 4.703125 -0.578125 4.9375 -1.515625 Z M 4 -1.515625 "/> -</symbol> -<symbol overflow="visible" id="glyph1-24"> -<path style="stroke:none;" d="M 0.65625 0 L 1.515625 0 L 1.515625 -0.671875 C 1.796875 -0.25 2.296875 0.109375 3.109375 0.109375 C 4.40625 0.109375 5.3125 -0.90625 5.3125 -2.375 C 5.3125 -3.8125 4.4375 -4.875 3.109375 -4.875 C 2.34375 -4.875 1.84375 -4.515625 1.5625 -4.125 C 1.578125 -4.296875 1.578125 -4.453125 1.578125 -4.59375 L 1.578125 -6.4375 L 0.65625 -6.4375 Z M 2.921875 -4.109375 C 3.8125 -4.109375 4.328125 -3.375 4.328125 -2.359375 C 4.328125 -1.3125 3.828125 -0.640625 2.9375 -0.640625 C 2.03125 -0.640625 1.515625 -1.328125 1.515625 -2.359375 C 1.515625 -3.375 2.046875 -4.109375 2.921875 -4.109375 Z M 2.921875 -4.109375 "/> -</symbol> -<symbol overflow="visible" id="glyph1-25"> -<path style="stroke:none;" d="M 0.3125 -1.859375 C 0.40625 -0.609375 1.1875 0.125 2.53125 0.125 C 3.890625 0.125 4.78125 -0.6875 4.78125 -1.859375 C 4.78125 -2.765625 4.296875 -3.3125 3.421875 -3.53125 C 4.046875 -3.765625 4.453125 -4.21875 4.453125 -4.96875 C 4.453125 -5.9375 3.703125 -6.5625 2.5625 -6.5625 C 1.359375 -6.5625 0.640625 -5.859375 0.609375 -4.828125 L 1.5625 -4.828125 C 1.609375 -5.40625 1.953125 -5.71875 2.53125 -5.71875 C 3 -5.71875 3.453125 -5.4375 3.453125 -4.84375 C 3.453125 -4.109375 2.890625 -3.859375 2.140625 -3.859375 L 2.140625 -3.078125 L 2.34375 -3.078125 C 3.125 -3.078125 3.734375 -2.734375 3.734375 -1.90625 C 3.734375 -1.21875 3.296875 -0.6875 2.546875 -0.6875 C 1.796875 -0.6875 1.375 -1.109375 1.296875 -1.859375 Z M 0.3125 -1.859375 "/> -</symbol> -<symbol overflow="visible" id="glyph2-0"> -<path style="stroke:none;" d="M 1.609375 -7.75 L 12.4375 -18.578125 C 12.21875 -18.875 11.953125 -19.125 11.59375 -19.328125 L 1.609375 -9.34375 Z M 1.609375 -4.59375 L 8.6875 -11.671875 C 8.984375 -12.34375 9.375 -12.921875 9.859375 -13.40625 C 10.984375 -14.546875 12.828125 -15.453125 12.828125 -16.90625 L 12.828125 -17.390625 L 1.609375 -6.171875 Z M 3.359375 0 L 8.046875 -4.6875 L 8.046875 -5.421875 L 8.78125 -5.421875 L 11.796875 -8.4375 L 10.203125 -8.4375 L 1.78125 0 Z M 10.375 -5.421875 L 11.953125 -5.421875 L 19.1875 -12.671875 L 19.1875 -14.25 Z M 1.609375 -10.921875 L 10.34375 -19.640625 L 10.078125 -19.640625 C 8.5 -19.640625 7.65625 -18.390625 7.65625 -17.21875 L 6.390625 -17.28125 L 1.609375 -12.5 Z M 12.859375 0 L 19.1875 -6.328125 L 19.1875 -7.921875 L 12.40625 -1.125 L 12.40625 -1.0625 L 12.34375 -1.0625 L 11.28125 0 Z M 12.40625 -4.296875 L 12.40625 -2.71875 L 19.1875 -9.5 L 19.1875 -11.078125 Z M 1.609375 -1.421875 L 8.625 -8.4375 L 8.265625 -8.4375 L 8.265625 -9.65625 L 1.609375 -3 Z M 1.609375 -18.84375 L 1.609375 -17.25 L 3.359375 -19 C 4 -21.328125 5.78125 -23.140625 8.109375 -23.75 L 9.53125 -25.171875 L 7.953125 -25.171875 Z M 17.359375 -15.578125 C 16.828125 -13.34375 15.15625 -12.34375 14.46875 -11.828125 C 13.09375 -10.796875 12.21875 -10.4375 12.21875 -9.078125 L 12.21875 -8.859375 L 19.1875 -15.828125 L 19.1875 -17.421875 Z M 1.609375 -22 L 1.609375 -20.421875 L 6.359375 -25.171875 L 4.78125 -25.171875 Z M 16.03125 0 L 19.1875 -3.171875 L 19.1875 -4.75 L 14.4375 0 Z M 14.828125 -22.546875 C 15.125 -22.328125 15.375 -22.0625 15.671875 -21.8125 L 19.03125 -25.171875 L 17.453125 -25.171875 Z M 1.609375 -14.09375 L 4.90625 -17.390625 L 3.421875 -17.484375 L 1.609375 -15.671875 Z M 16.34375 -20.90625 C 16.578125 -20.546875 16.765625 -20.234375 16.9375 -19.90625 L 19.1875 -22.171875 L 19.1875 -23.75 Z M 6.53125 0 L 8.046875 -1.515625 L 8.046875 -3.109375 L 4.9375 0 Z M 12.765625 -23.65625 C 13.125 -23.515625 13.5 -23.359375 13.859375 -23.171875 L 15.859375 -25.171875 L 14.28125 -25.171875 Z M 17.3125 -18.703125 C 17.421875 -18.21875 17.515625 -17.796875 17.515625 -17.3125 L 19.1875 -19 L 19.1875 -20.578125 Z M 9.921875 -23.96875 L 10.140625 -23.96875 C 10.28125 -23.96875 10.4375 -24.015625 10.5625 -24.015625 C 10.859375 -24.015625 11.1875 -23.96875 11.46875 -23.9375 L 12.703125 -25.171875 L 11.109375 -25.171875 Z M 9.6875 0 L 10.765625 -1.0625 L 9.171875 -1.0625 L 8.109375 0 Z M 19.1875 -1.578125 L 17.609375 0 L 19.1875 0 Z M 1.609375 -25.171875 L 1.609375 -23.59375 L 3.203125 -25.171875 Z M 1.609375 -25.171875 "/> -</symbol> -<symbol overflow="visible" id="glyph2-1"> -<path style="stroke:none;" d="M 9.28125 -23.5625 C 2.65625 -23.5625 1.125 -17.703125 1.125 -11.5 C 1.125 -4.65625 2.78125 0.484375 9.28125 0.484375 C 15.734375 0.484375 17.453125 -4.65625 17.453125 -11.5 C 17.453125 -17.703125 15.9375 -23.5625 9.28125 -23.5625 Z M 9.28125 -20.8125 C 13.015625 -20.8125 13.765625 -16.578125 13.765625 -11.5 C 13.765625 -6.078125 12.890625 -2.234375 9.28125 -2.234375 C 5.6875 -2.234375 4.78125 -6.078125 4.78125 -11.5 C 4.78125 -16.546875 5.5625 -20.8125 9.28125 -20.8125 Z M 9.28125 -20.8125 "/> -</symbol> -<symbol overflow="visible" id="glyph2-2"> -<path style="stroke:none;" d="M 9.203125 0 L 12.5625 0 L 12.5625 -23.109375 L 9.71875 -23.109375 C 9.375 -20.515625 7.65625 -19.515625 3.6875 -19.421875 L 3.6875 -16.96875 L 7.890625 -16.96875 C 8.296875 -16.96875 8.75 -16.96875 9.203125 -17 C 9.171875 -16.1875 9.203125 -15.40625 9.203125 -14.640625 Z M 9.203125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph2-3"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph2-4"> -<path style="stroke:none;" d="M 2.8125 0 L 16.375 0 L 16.375 -2.9375 L 6.234375 -2.9375 L 6.234375 -23.109375 L 2.8125 -23.109375 Z M 2.8125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph2-5"> -<path style="stroke:none;" d="M 9.890625 -17.421875 C 4.59375 -17.421875 1.296875 -13.703125 1.296875 -8.4375 C 1.296875 -3.265625 4.390625 0.421875 9.828125 0.421875 C 15.25 0.421875 18.453125 -3.265625 18.453125 -8.4375 C 18.453125 -13.703125 15.15625 -17.421875 9.890625 -17.421875 Z M 9.796875 -14.703125 C 12.984375 -14.703125 14.859375 -12.078125 14.859375 -8.46875 C 14.859375 -4.6875 13.015625 -2.296875 9.796875 -2.296875 C 6.5625 -2.296875 4.75 -4.6875 4.75 -8.46875 C 4.75 -12.078125 6.59375 -14.703125 9.796875 -14.703125 Z M 9.796875 -14.703125 "/> -</symbol> -<symbol overflow="visible" id="glyph2-6"> -<path style="stroke:none;" d="M 18.0625 -16.96875 L 14.828125 -16.96875 L 14.828125 -15.34375 C 14.828125 -15.1875 14.828125 -15 14.859375 -14.828125 C 13.859375 -16.21875 12.15625 -17.484375 9.171875 -17.484375 C 4.625 -17.484375 1.328125 -14.09375 1.328125 -9.046875 C 1.328125 -4.203125 4.359375 -0.75 9.140625 -0.75 C 12.109375 -0.75 13.953125 -2.03125 14.828125 -3.484375 L 14.890625 -3.453125 C 14.890625 1.15625 13.5 3.546875 9.625 3.546875 C 7.109375 3.546875 5.90625 2.171875 5.296875 0.609375 L 1.875 0.609375 C 2.78125 3.96875 5.296875 6.203125 9.5625 6.203125 C 15.28125 6.203125 18.0625 2.90625 18.0625 -3.265625 Z M 9.890625 -14.734375 C 12.984375 -14.734375 14.921875 -12.5 14.921875 -9.171875 C 14.921875 -5.421875 12.734375 -3.421875 9.890625 -3.421875 C 6.71875 -3.421875 4.875 -5.78125 4.875 -9.078125 C 4.875 -12.53125 6.78125 -14.734375 9.890625 -14.734375 Z M 9.890625 -14.734375 "/> -</symbol> -<symbol overflow="visible" id="glyph3-0"> -<path style="stroke:none;" d="M 0.75 -3.59375 L 5.78125 -8.625 C 5.671875 -8.765625 5.546875 -8.875 5.390625 -8.96875 L 0.75 -4.328125 Z M 0.75 -2.125 L 4.03125 -5.421875 C 4.171875 -5.734375 4.34375 -6 4.578125 -6.21875 C 5.09375 -6.75 5.953125 -7.171875 5.953125 -7.84375 L 5.953125 -8.0625 L 0.75 -2.859375 Z M 1.5625 0 L 3.734375 -2.171875 L 3.734375 -2.515625 L 4.078125 -2.515625 L 5.46875 -3.921875 L 4.734375 -3.921875 L 0.828125 0 Z M 4.8125 -2.515625 L 5.546875 -2.515625 L 8.90625 -5.875 L 8.90625 -6.609375 Z M 0.75 -5.0625 L 4.796875 -9.125 L 4.6875 -9.125 C 3.9375 -9.125 3.5625 -8.53125 3.5625 -8 L 2.96875 -8.03125 L 0.75 -5.8125 Z M 5.96875 0 L 8.90625 -2.9375 L 8.90625 -3.671875 L 5.765625 -0.53125 L 5.765625 -0.5 L 5.734375 -0.5 L 5.234375 0 Z M 5.765625 -2 L 5.765625 -1.265625 L 8.90625 -4.40625 L 8.90625 -5.140625 Z M 0.75 -0.65625 L 4 -3.921875 L 3.84375 -3.921875 L 3.84375 -4.484375 L 0.75 -1.390625 Z M 0.75 -8.75 L 0.75 -8.015625 L 1.5625 -8.8125 C 1.859375 -9.90625 2.6875 -10.734375 3.765625 -11.03125 L 4.421875 -11.6875 L 3.6875 -11.6875 Z M 8.0625 -7.234375 C 7.8125 -6.1875 7.03125 -5.734375 6.71875 -5.484375 C 6.078125 -5.015625 5.671875 -4.84375 5.671875 -4.21875 L 5.671875 -4.109375 L 8.90625 -7.34375 L 8.90625 -8.078125 Z M 0.75 -10.21875 L 0.75 -9.484375 L 2.953125 -11.6875 L 2.21875 -11.6875 Z M 7.4375 0 L 8.90625 -1.46875 L 8.90625 -2.203125 L 6.703125 0 Z M 6.890625 -10.46875 C 7.015625 -10.359375 7.140625 -10.25 7.28125 -10.125 L 8.828125 -11.6875 L 8.09375 -11.6875 Z M 0.75 -6.546875 L 2.28125 -8.0625 L 1.59375 -8.109375 L 0.75 -7.28125 Z M 7.59375 -9.703125 C 7.6875 -9.546875 7.78125 -9.390625 7.859375 -9.234375 L 8.90625 -10.296875 L 8.90625 -11.03125 Z M 3.03125 0 L 3.734375 -0.703125 L 3.734375 -1.4375 L 2.296875 0 Z M 5.921875 -10.984375 C 6.09375 -10.921875 6.265625 -10.84375 6.4375 -10.75 L 7.359375 -11.6875 L 6.625 -11.6875 Z M 8.046875 -8.6875 C 8.078125 -8.453125 8.125 -8.265625 8.125 -8.046875 L 8.90625 -8.8125 L 8.90625 -9.5625 Z M 4.609375 -11.125 L 4.703125 -11.125 C 4.765625 -11.125 4.84375 -11.140625 4.90625 -11.140625 C 5.046875 -11.140625 5.1875 -11.125 5.328125 -11.109375 L 5.890625 -11.6875 L 5.15625 -11.6875 Z M 4.5 0 L 5 -0.5 L 4.265625 -0.5 L 3.765625 0 Z M 8.90625 -0.734375 L 8.171875 0 L 8.90625 0 Z M 0.75 -11.6875 L 0.75 -10.953125 L 1.484375 -11.6875 Z M 0.75 -11.6875 "/> -</symbol> -<symbol overflow="visible" id="glyph3-1"> -<path style="stroke:none;" d="M 4.28125 0 L 5.828125 0 L 5.828125 -10.71875 L 4.515625 -10.71875 C 4.34375 -9.53125 3.5625 -9.0625 1.703125 -9.015625 L 1.703125 -7.875 L 3.65625 -7.875 C 3.859375 -7.875 4.0625 -7.875 4.28125 -7.890625 C 4.265625 -7.515625 4.28125 -7.15625 4.28125 -6.796875 Z M 4.28125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph3-2"> -<path style="stroke:none;" d="M 0.953125 0 L 2.5625 0 L 2.5625 -1.796875 L 0.953125 -1.796875 Z M 0.953125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph3-3"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph3-4"> -<path style="stroke:none;" d="M 9.21875 -3.140625 C 8.625 -1.859375 7.59375 -1.109375 6.015625 -1.109375 C 3.6875 -1.109375 2.359375 -2.890625 2.359375 -5.390625 C 2.359375 -7.890625 3.6875 -9.59375 6 -9.59375 C 7.6875 -9.59375 8.6875 -8.8125 9.265625 -7.46875 L 10.890625 -7.46875 C 10.21875 -9.703125 8.515625 -10.9375 6.015625 -10.9375 C 2.828125 -10.9375 0.640625 -8.671875 0.640625 -5.375 C 0.640625 -2.0625 2.8125 0.203125 5.9375 0.203125 C 8.46875 0.203125 10.140625 -1.078125 10.875 -3.140625 Z M 9.21875 -3.140625 "/> -</symbol> -<symbol overflow="visible" id="glyph3-5"> -<path style="stroke:none;" d="M 4.59375 -8.078125 C 2.125 -8.078125 0.59375 -6.359375 0.59375 -3.921875 C 0.59375 -1.515625 2.046875 0.1875 4.5625 0.1875 C 7.078125 0.1875 8.5625 -1.515625 8.5625 -3.921875 C 8.5625 -6.359375 7.03125 -8.078125 4.59375 -8.078125 Z M 4.546875 -6.828125 C 6.03125 -6.828125 6.90625 -5.609375 6.90625 -3.9375 C 6.90625 -2.171875 6.046875 -1.0625 4.546875 -1.0625 C 3.046875 -1.0625 2.203125 -2.171875 2.203125 -3.9375 C 2.203125 -5.609375 3.0625 -6.828125 4.546875 -6.828125 Z M 4.546875 -6.828125 "/> -</symbol> -<symbol overflow="visible" id="glyph3-6"> -<path style="stroke:none;" d="M 2.578125 -7.875 L 1.078125 -7.875 C 1.09375 -7.6875 1.09375 -7.46875 1.09375 -7.28125 L 1.09375 0 L 2.609375 0 L 2.609375 -4.546875 C 2.609375 -5.796875 3.296875 -6.8125 4.5625 -6.8125 C 5.8125 -6.8125 6.3125 -5.828125 6.3125 -4.28125 L 6.3125 0 L 7.84375 0 L 7.84375 -4.84375 C 7.84375 -6.890625 6.90625 -8.09375 5.078125 -8.09375 C 3.765625 -8.09375 2.984375 -7.484375 2.578125 -6.8125 Z M 2.578125 -7.875 "/> -</symbol> -<symbol overflow="visible" id="glyph3-7"> -<path style="stroke:none;" d="M 0.5 -2.40625 C 0.8125 -0.640625 1.9375 0.1875 3.859375 0.1875 C 5.65625 0.1875 6.953125 -0.796875 6.953125 -2.3125 C 6.953125 -3.625 6.140625 -4.109375 4.5625 -4.5625 L 3.71875 -4.796875 C 2.765625 -5.078125 2.296875 -5.265625 2.296875 -5.90625 C 2.296875 -6.5 2.765625 -6.890625 3.71875 -6.890625 C 4.640625 -6.890625 5.15625 -6.5 5.390625 -5.796875 L 6.828125 -5.796875 C 6.640625 -7.328125 5.5 -8.078125 3.765625 -8.078125 C 2.015625 -8.078125 0.75 -7.171875 0.75 -5.78125 C 0.75 -4.40625 1.71875 -3.828125 3.03125 -3.515625 L 3.578125 -3.359375 C 4.6875 -3.09375 5.4375 -2.890625 5.4375 -2.15625 C 5.4375 -1.453125 4.796875 -1.046875 3.90625 -1.046875 C 2.8125 -1.046875 2.25 -1.5625 2 -2.40625 Z M 0.5 -2.40625 "/> -</symbol> -<symbol overflow="visible" id="glyph3-8"> -<path style="stroke:none;" d="M 2.984375 -10 L 1.453125 -10 L 1.453125 -7.875 L 0.265625 -7.875 L 0.265625 -6.703125 L 1.453125 -6.703125 L 1.453125 -1.890625 C 1.453125 -0.3125 2.34375 0.15625 3.734375 0.15625 C 3.9375 0.15625 4.09375 0.140625 4.3125 0.09375 L 4.3125 -1.03125 C 4.21875 -1.015625 4.046875 -1 3.9375 -1 C 3.359375 -1 2.984375 -1.21875 2.984375 -1.984375 L 2.984375 -6.703125 L 4.453125 -6.703125 L 4.453125 -7.875 L 2.984375 -7.875 Z M 2.984375 -10 "/> -</symbol> -<symbol overflow="visible" id="glyph3-9"> -<path style="stroke:none;" d="M 1.078125 -7.875 C 1.09375 -7.640625 1.09375 -7.390625 1.09375 -7.171875 L 1.09375 0 L 2.609375 0 L 2.609375 -4.078125 C 2.609375 -5.65625 2.875 -6.703125 4.3125 -6.703125 C 4.46875 -6.703125 4.6875 -6.703125 4.828125 -6.65625 L 4.828125 -8.046875 C 4.71875 -8.0625 4.640625 -8.0625 4.4375 -8.0625 C 3.546875 -8.0625 2.796875 -7.609375 2.53125 -6.875 L 2.53125 -7.875 Z M 1.078125 -7.875 "/> -</symbol> -<symbol overflow="visible" id="glyph3-10"> -<path style="stroke:none;" d="M 6.234375 0 L 7.6875 0 C 7.6875 -0.1875 7.6875 -0.390625 7.6875 -0.578125 L 7.6875 -7.875 L 6.171875 -7.875 L 6.171875 -4.265625 C 6.171875 -2.53125 5.875 -1.0625 4.265625 -1.0625 C 3.015625 -1.0625 2.515625 -1.984375 2.515625 -3.546875 L 2.515625 -7.875 L 1 -7.875 L 1 -2.96875 C 1 -0.921875 1.890625 0.1875 3.734375 0.1875 C 5.0625 0.1875 5.828125 -0.453125 6.234375 -1.125 Z M 6.234375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph3-11"> -<path style="stroke:none;" d="M 6.65625 -2.515625 C 6.3125 -1.609375 5.578125 -1.0625 4.5625 -1.0625 C 3.0625 -1.0625 2.203125 -2.1875 2.203125 -3.9375 C 2.203125 -5.609375 3.078125 -6.828125 4.5625 -6.828125 C 5.59375 -6.828125 6.34375 -6.234375 6.6875 -5.28125 L 8.25 -5.28125 C 7.890625 -6.9375 6.65625 -8.078125 4.625 -8.078125 C 2.171875 -8.078125 0.59375 -6.3125 0.59375 -3.953125 C 0.59375 -1.578125 2.125 0.1875 4.609375 0.1875 C 6.59375 0.1875 7.828125 -0.953125 8.234375 -2.515625 Z M 6.65625 -2.515625 "/> -</symbol> -<symbol overflow="visible" id="glyph3-12"> -<path style="stroke:none;" d="M 1.09375 -7.875 L 1.09375 0 L 2.609375 0 L 2.609375 -7.875 Z M 1.0625 -9.015625 L 2.640625 -9.015625 L 2.640625 -10.71875 L 1.0625 -10.71875 Z M 1.0625 -9.015625 "/> -</symbol> -<symbol overflow="visible" id="glyph3-13"> -<path style="stroke:none;" d="M 8.34375 0 L 8.34375 -10.71875 L 6.828125 -10.71875 L 6.828125 -7.640625 C 6.828125 -7.390625 6.828125 -7.140625 6.84375 -6.859375 C 6.40625 -7.5 5.53125 -8.109375 4.3125 -8.109375 C 2.046875 -8.109375 0.59375 -6.34375 0.59375 -3.953125 C 0.59375 -1.5 2.09375 0.1875 4.265625 0.1875 C 5.625 0.1875 6.46875 -0.40625 6.9375 -1.109375 L 6.9375 0 Z M 4.578125 -6.84375 C 6.03125 -6.84375 6.90625 -5.625 6.90625 -3.9375 C 6.90625 -2.203125 6.0625 -1.0625 4.5625 -1.0625 C 3.078125 -1.0625 2.234375 -2.1875 2.234375 -3.9375 C 2.234375 -5.625 3.09375 -6.84375 4.578125 -6.84375 Z M 4.578125 -6.84375 "/> -</symbol> -<symbol overflow="visible" id="glyph3-14"> -<path style="stroke:none;" d="M 1.15625 0 L 2.671875 0 L 2.671875 -10.71875 L 1.15625 -10.71875 Z M 1.15625 0 "/> -</symbol> -<symbol overflow="visible" id="glyph3-15"> -<path style="stroke:none;" d="M 8.390625 -7.875 L 6.890625 -7.875 L 6.890625 -7.125 C 6.890625 -7.046875 6.890625 -6.953125 6.90625 -6.890625 C 6.4375 -7.53125 5.640625 -8.109375 4.265625 -8.109375 C 2.140625 -8.109375 0.609375 -6.546875 0.609375 -4.203125 C 0.609375 -1.953125 2.03125 -0.34375 4.25 -0.34375 C 5.625 -0.34375 6.484375 -0.9375 6.890625 -1.625 L 6.921875 -1.609375 C 6.921875 0.546875 6.265625 1.65625 4.46875 1.65625 C 3.296875 1.65625 2.75 1 2.453125 0.28125 L 0.875 0.28125 C 1.296875 1.84375 2.453125 2.875 4.4375 2.875 C 7.09375 2.875 8.390625 1.34375 8.390625 -1.515625 Z M 4.59375 -6.84375 C 6.03125 -6.84375 6.9375 -5.8125 6.9375 -4.265625 C 6.9375 -2.515625 5.90625 -1.59375 4.59375 -1.59375 C 3.125 -1.59375 2.265625 -2.6875 2.265625 -4.21875 C 2.265625 -5.8125 3.15625 -6.84375 4.59375 -6.84375 Z M 4.59375 -6.84375 "/> -</symbol> -<symbol overflow="visible" id="glyph4-0"> -<path style="stroke:none;" d="M 0.546875 -2.640625 L 4.234375 -6.328125 C 4.15625 -6.421875 4.0625 -6.515625 3.953125 -6.578125 L 0.546875 -3.171875 Z M 0.546875 -1.5625 L 2.953125 -3.96875 C 3.0625 -4.203125 3.1875 -4.40625 3.359375 -4.5625 C 3.734375 -4.953125 4.359375 -5.265625 4.359375 -5.75 L 4.359375 -5.921875 L 0.546875 -2.09375 Z M 1.140625 0 L 2.734375 -1.59375 L 2.734375 -1.84375 L 2.984375 -1.84375 L 4.015625 -2.875 L 3.46875 -2.875 L 0.609375 0 Z M 3.53125 -1.84375 L 4.0625 -1.84375 L 6.53125 -4.3125 L 6.53125 -4.84375 Z M 0.546875 -3.71875 L 3.515625 -6.6875 L 3.4375 -6.6875 C 2.890625 -6.6875 2.609375 -6.265625 2.609375 -5.859375 L 2.171875 -5.890625 L 0.546875 -4.25 Z M 4.375 0 L 6.53125 -2.15625 L 6.53125 -2.6875 L 4.21875 -0.390625 L 4.21875 -0.359375 L 4.203125 -0.359375 L 3.84375 0 Z M 4.21875 -1.46875 L 4.21875 -0.921875 L 6.53125 -3.234375 L 6.53125 -3.765625 Z M 0.546875 -0.484375 L 2.9375 -2.875 L 2.8125 -2.875 L 2.8125 -3.28125 L 0.546875 -1.015625 Z M 0.546875 -6.40625 L 0.546875 -5.875 L 1.140625 -6.46875 C 1.359375 -7.265625 1.96875 -7.875 2.765625 -8.078125 L 3.25 -8.5625 L 2.703125 -8.5625 Z M 5.90625 -5.296875 C 5.734375 -4.546875 5.15625 -4.203125 4.921875 -4.03125 C 4.453125 -3.671875 4.15625 -3.546875 4.15625 -3.09375 L 4.15625 -3.015625 L 6.53125 -5.390625 L 6.53125 -5.921875 Z M 0.546875 -7.484375 L 0.546875 -6.953125 L 2.171875 -8.5625 L 1.625 -8.5625 Z M 5.453125 0 L 6.53125 -1.078125 L 6.53125 -1.609375 L 4.921875 0 Z M 5.046875 -7.671875 C 5.140625 -7.59375 5.234375 -7.515625 5.328125 -7.421875 L 6.484375 -8.5625 L 5.9375 -8.5625 Z M 0.546875 -4.796875 L 1.671875 -5.921875 L 1.171875 -5.953125 L 0.546875 -5.328125 Z M 5.5625 -7.109375 C 5.640625 -7 5.703125 -6.890625 5.765625 -6.78125 L 6.53125 -7.546875 L 6.53125 -8.078125 Z M 2.21875 0 L 2.734375 -0.515625 L 2.734375 -1.0625 L 1.6875 0 Z M 4.34375 -8.046875 C 4.46875 -8.015625 4.59375 -7.953125 4.71875 -7.890625 L 5.40625 -8.5625 L 4.859375 -8.5625 Z M 5.890625 -6.375 C 5.921875 -6.203125 5.96875 -6.0625 5.96875 -5.890625 L 6.53125 -6.46875 L 6.53125 -7 Z M 3.375 -8.15625 L 3.453125 -8.15625 C 3.5 -8.15625 3.546875 -8.171875 3.59375 -8.171875 C 3.703125 -8.171875 3.8125 -8.15625 3.90625 -8.15625 L 4.328125 -8.5625 L 3.78125 -8.5625 Z M 3.296875 0 L 3.65625 -0.359375 L 3.125 -0.359375 L 2.765625 0 Z M 6.53125 -0.53125 L 6 0 L 6.53125 0 Z M 0.546875 -8.5625 L 0.546875 -8.03125 L 1.09375 -8.5625 Z M 0.546875 -8.5625 "/> -</symbol> -<symbol overflow="visible" id="glyph4-1"> -<path style="stroke:none;" d="M 3.140625 0 L 4.28125 0 L 4.28125 -7.859375 L 3.3125 -7.859375 C 3.1875 -6.984375 2.609375 -6.640625 1.25 -6.609375 L 1.25 -5.78125 L 2.6875 -5.78125 C 2.828125 -5.78125 2.984375 -5.78125 3.140625 -5.78125 C 3.125 -5.515625 3.140625 -5.25 3.140625 -4.984375 Z M 3.140625 0 "/> -</symbol> -<symbol overflow="visible" id="glyph4-2"> -<path style="stroke:none;" d="M 0.703125 0 L 1.875 0 L 1.875 -1.3125 L 0.703125 -1.3125 Z M 0.703125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph4-3"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph4-4"> -<path style="stroke:none;" d="M 0.953125 0 L 5.578125 0 L 5.578125 -1 L 2.125 -1 L 2.125 -7.859375 L 0.953125 -7.859375 Z M 0.953125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph4-5"> -<path style="stroke:none;" d="M 3.359375 -5.921875 C 1.5625 -5.921875 0.4375 -4.65625 0.4375 -2.875 C 0.4375 -1.109375 1.5 0.140625 3.34375 0.140625 C 5.1875 0.140625 6.28125 -1.109375 6.28125 -2.875 C 6.28125 -4.65625 5.15625 -5.921875 3.359375 -5.921875 Z M 3.328125 -5 C 4.421875 -5 5.0625 -4.109375 5.0625 -2.875 C 5.0625 -1.59375 4.4375 -0.78125 3.328125 -0.78125 C 2.234375 -0.78125 1.609375 -1.59375 1.609375 -2.875 C 1.609375 -4.109375 2.25 -5 3.328125 -5 Z M 3.328125 -5 "/> -</symbol> -<symbol overflow="visible" id="glyph4-6"> -<path style="stroke:none;" d="M 6.15625 -5.78125 L 5.046875 -5.78125 L 5.046875 -5.21875 C 5.046875 -5.171875 5.046875 -5.109375 5.0625 -5.046875 C 4.71875 -5.515625 4.140625 -5.953125 3.125 -5.953125 C 1.578125 -5.953125 0.453125 -4.796875 0.453125 -3.078125 C 0.453125 -1.4375 1.484375 -0.25 3.109375 -0.25 C 4.125 -0.25 4.75 -0.6875 5.046875 -1.1875 L 5.078125 -1.171875 C 5.078125 0.390625 4.59375 1.203125 3.28125 1.203125 C 2.421875 1.203125 2.015625 0.734375 1.796875 0.203125 L 0.640625 0.203125 C 0.953125 1.359375 1.796875 2.109375 3.25 2.109375 C 5.203125 2.109375 6.15625 0.984375 6.15625 -1.109375 Z M 3.359375 -5.015625 C 4.421875 -5.015625 5.078125 -4.25 5.078125 -3.125 C 5.078125 -1.84375 4.328125 -1.171875 3.359375 -1.171875 C 2.28125 -1.171875 1.65625 -1.96875 1.65625 -3.09375 C 1.65625 -4.265625 2.3125 -5.015625 3.359375 -5.015625 Z M 3.359375 -5.015625 "/> -</symbol> -<symbol overflow="visible" id="glyph4-7"> -<path style="stroke:none;" d="M 5.734375 0 L 5.734375 -0.984375 L 1.984375 -0.984375 C 2.25 -1.25 2.78125 -1.65625 3.109375 -1.90625 L 3.71875 -2.4375 C 4.765625 -3.296875 5.703125 -4.359375 5.703125 -5.640625 C 5.703125 -7.015625 4.796875 -8.015625 3.140625 -8.015625 C 1.46875 -8.015625 0.546875 -6.9375 0.515625 -5.34375 L 1.6875 -5.34375 C 1.71875 -6.40625 2.21875 -6.984375 3.140625 -6.984375 C 3.96875 -6.984375 4.46875 -6.453125 4.46875 -5.640625 C 4.46875 -4.796875 4 -4.125 3 -3.21875 L 1.171875 -1.5625 C 0.84375 -1.25 0.609375 -1.015625 0.609375 -0.984375 L 0.609375 0 Z M 5.734375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph4-8"> -<path style="stroke:none;" d="M 0.71875 -3.0625 L 2.671875 -3.0625 L 2.671875 -1.015625 L 3.625 -1.015625 L 3.625 -3.0625 L 5.59375 -3.0625 L 5.59375 -4.015625 L 3.625 -4.015625 L 3.625 -6.046875 L 2.671875 -6.046875 L 2.671875 -4.015625 L 0.71875 -4.015625 Z M 0.71875 -3.0625 "/> -</symbol> -<symbol overflow="visible" id="glyph4-9"> -<path style="stroke:none;" d="M 0.953125 0 L 3.453125 0 C 4.953125 0 6.5625 -0.40625 6.5625 -2.25 C 6.5625 -3.40625 5.8125 -3.9375 5.078125 -4.140625 C 5.65625 -4.359375 6.21875 -4.890625 6.21875 -5.9375 C 6.21875 -7.34375 5.203125 -7.859375 3.3125 -7.859375 L 0.953125 -7.859375 Z M 2.09375 -3.546875 L 3.5 -3.546875 C 4.625 -3.546875 5.359375 -3.234375 5.359375 -2.265625 C 5.359375 -1.296875 4.625 -0.96875 3.734375 -0.96875 L 2.09375 -0.96875 Z M 2.09375 -6.890625 L 3 -6.890625 C 4.1875 -6.890625 4.984375 -6.78125 4.984375 -5.71875 C 4.984375 -4.8125 4.4375 -4.5 3.375 -4.5 L 2.09375 -4.5 Z M 2.09375 -6.890625 "/> -</symbol> -<symbol overflow="visible" id="glyph4-10"> -<path style="stroke:none;" d="M 5.53125 0 C 5.4375 -0.53125 5.390625 -1.203125 5.390625 -2.140625 L 5.390625 -3.265625 C 5.390625 -5.515625 4.328125 -5.921875 3.09375 -5.921875 C 1.71875 -5.921875 0.71875 -5.203125 0.671875 -3.96875 L 1.765625 -3.96875 C 1.84375 -4.65625 2.234375 -5.046875 3.0625 -5.046875 C 3.828125 -5.046875 4.3125 -4.71875 4.3125 -3.546875 C 3.921875 -3.515625 3.515625 -3.46875 3.125 -3.421875 C 1.578125 -3.21875 0.390625 -2.796875 0.390625 -1.5625 C 0.390625 -0.484375 1.28125 0.109375 2.484375 0.109375 C 3.40625 0.109375 4 -0.171875 4.4375 -0.796875 C 4.4375 -0.359375 4.453125 -0.234375 4.484375 0 Z M 4.328125 -2.75 C 4.328125 -2.71875 4.328125 -2.671875 4.328125 -2.625 L 4.328125 -2.4375 C 4.328125 -1.3125 3.78125 -0.6875 2.734375 -0.6875 C 2.03125 -0.6875 1.59375 -1.03125 1.59375 -1.578125 C 1.59375 -2.359375 2.453125 -2.578125 4.328125 -2.75 Z M 4.328125 -2.75 "/> -</symbol> -<symbol overflow="visible" id="glyph4-11"> -<path style="stroke:none;" d="M 0.359375 -1.765625 C 0.59375 -0.46875 1.421875 0.125 2.828125 0.125 C 4.140625 0.125 5.109375 -0.578125 5.109375 -1.6875 C 5.109375 -2.65625 4.5 -3.015625 3.34375 -3.34375 L 2.734375 -3.515625 C 2.03125 -3.734375 1.6875 -3.859375 1.6875 -4.328125 C 1.6875 -4.765625 2.03125 -5.046875 2.734375 -5.046875 C 3.40625 -5.046875 3.78125 -4.765625 3.953125 -4.25 L 5 -4.25 C 4.875 -5.375 4.03125 -5.921875 2.765625 -5.921875 C 1.46875 -5.921875 0.546875 -5.265625 0.546875 -4.234375 C 0.546875 -3.234375 1.265625 -2.8125 2.21875 -2.578125 L 2.625 -2.46875 C 3.4375 -2.265625 4 -2.125 4 -1.578125 C 4 -1.0625 3.515625 -0.765625 2.859375 -0.765625 C 2.0625 -0.765625 1.65625 -1.140625 1.46875 -1.765625 Z M 0.359375 -1.765625 "/> -</symbol> -<symbol overflow="visible" id="glyph4-12"> -<path style="stroke:none;" d="M 6.078125 -2.59375 L 6.078125 -2.953125 C 6.078125 -4.515625 5.21875 -5.9375 3.328125 -5.9375 C 1.609375 -5.9375 0.4375 -4.65625 0.4375 -2.859375 C 0.4375 -1.0625 1.546875 0.140625 3.328125 0.140625 C 4.703125 0.140625 5.640625 -0.609375 5.96875 -1.65625 L 4.84375 -1.65625 C 4.546875 -1.15625 4.171875 -0.78125 3.375 -0.78125 C 2.28125 -0.78125 1.59375 -1.578125 1.5625 -2.59375 Z M 1.609375 -3.46875 C 1.734375 -4.4375 2.390625 -5.03125 3.3125 -5.03125 C 4.265625 -5.03125 4.828125 -4.40625 4.921875 -3.46875 Z M 1.609375 -3.46875 "/> -</symbol> -<symbol overflow="visible" id="glyph4-13"> -<path style="stroke:none;" d="M 0.84375 0 L 1.953125 0 L 1.953125 -7.859375 L 0.84375 -7.859375 Z M 0.84375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph4-14"> -<path style="stroke:none;" d="M 0.796875 -5.78125 L 0.796875 0 L 1.90625 0 L 1.90625 -5.78125 Z M 0.78125 -6.609375 L 1.9375 -6.609375 L 1.9375 -7.859375 L 0.78125 -7.859375 Z M 0.78125 -6.609375 "/> -</symbol> -<symbol overflow="visible" id="glyph4-15"> -<path style="stroke:none;" d="M 1.890625 -5.78125 L 0.796875 -5.78125 C 0.796875 -5.625 0.796875 -5.484375 0.796875 -5.328125 L 0.796875 0 L 1.90625 0 L 1.90625 -3.328125 C 1.90625 -4.25 2.421875 -5 3.34375 -5 C 4.25 -5 4.625 -4.28125 4.625 -3.140625 L 4.625 0 L 5.75 0 L 5.75 -3.546875 C 5.75 -5.046875 5.0625 -5.9375 3.734375 -5.9375 C 2.765625 -5.9375 2.1875 -5.484375 1.890625 -5 Z M 1.890625 -5.78125 "/> -</symbol> -<symbol overflow="visible" id="glyph5-0"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph5-1"> -<path style="stroke:none;" d="M 1.546875 0 L 3.40625 0 L 3.40625 -9.84375 L 7.4375 -3.15625 L 8.671875 -3.15625 L 12.703125 -9.84375 L 12.703125 0 L 14.546875 0 L 14.546875 -12.953125 L 12.625 -12.953125 L 8.046875 -5.34375 L 3.46875 -12.953125 L 1.546875 -12.953125 Z M 1.546875 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-2"> -<path style="stroke:none;" d="M 5.625 0.25 C 6.0625 0.25 6.488281 0.203125 6.90625 0.109375 C 7.332031 0.0234375 7.734375 -0.109375 8.109375 -0.296875 C 8.484375 -0.484375 8.835938 -0.71875 9.171875 -1 C 9.515625 -1.289062 9.828125 -1.632812 10.109375 -2.03125 L 8.75 -3 C 8.550781 -2.71875 8.335938 -2.472656 8.109375 -2.265625 C 7.878906 -2.054688 7.632812 -1.882812 7.375 -1.75 C 7.113281 -1.613281 6.835938 -1.515625 6.546875 -1.453125 C 6.265625 -1.390625 5.960938 -1.359375 5.640625 -1.359375 C 5.347656 -1.359375 5.070312 -1.390625 4.8125 -1.453125 C 4.550781 -1.515625 4.304688 -1.597656 4.078125 -1.703125 C 3.847656 -1.816406 3.640625 -1.953125 3.453125 -2.109375 C 3.273438 -2.273438 3.117188 -2.457031 2.984375 -2.65625 C 2.847656 -2.863281 2.738281 -3.085938 2.65625 -3.328125 C 2.570312 -3.566406 2.519531 -3.820312 2.5 -4.09375 L 9.984375 -4.09375 C 10.023438 -4.664062 10.003906 -5.191406 9.921875 -5.671875 C 9.835938 -6.160156 9.703125 -6.601562 9.515625 -7 C 9.328125 -7.394531 9.09375 -7.742188 8.8125 -8.046875 C 8.539062 -8.347656 8.226562 -8.601562 7.875 -8.8125 C 7.519531 -9.019531 7.128906 -9.175781 6.703125 -9.28125 C 6.285156 -9.382812 5.847656 -9.4375 5.390625 -9.4375 C 4.941406 -9.4375 4.507812 -9.378906 4.09375 -9.265625 C 3.675781 -9.160156 3.289062 -9.003906 2.9375 -8.796875 C 2.59375 -8.585938 2.28125 -8.335938 2 -8.046875 C 1.71875 -7.753906 1.472656 -7.425781 1.265625 -7.0625 C 1.066406 -6.707031 0.910156 -6.320312 0.796875 -5.90625 C 0.691406 -5.488281 0.640625 -5.046875 0.640625 -4.578125 C 0.640625 -4.117188 0.691406 -3.679688 0.796875 -3.265625 C 0.898438 -2.847656 1.050781 -2.457031 1.25 -2.09375 C 1.457031 -1.738281 1.707031 -1.414062 2 -1.125 C 2.289062 -0.84375 2.617188 -0.597656 2.984375 -0.390625 C 3.359375 -0.191406 3.765625 -0.0351562 4.203125 0.078125 C 4.648438 0.191406 5.125 0.25 5.625 0.25 Z M 2.5625 -5.5625 C 2.601562 -5.820312 2.660156 -6.0625 2.734375 -6.28125 C 2.816406 -6.507812 2.921875 -6.710938 3.046875 -6.890625 C 3.171875 -7.066406 3.3125 -7.222656 3.46875 -7.359375 C 3.632812 -7.492188 3.816406 -7.601562 4.015625 -7.6875 C 4.210938 -7.78125 4.421875 -7.847656 4.640625 -7.890625 C 4.867188 -7.941406 5.109375 -7.96875 5.359375 -7.96875 C 5.734375 -7.96875 6.078125 -7.914062 6.390625 -7.8125 C 6.710938 -7.707031 6.988281 -7.550781 7.21875 -7.34375 C 7.457031 -7.144531 7.65625 -6.894531 7.8125 -6.59375 C 7.96875 -6.300781 8.070312 -5.957031 8.125 -5.5625 Z M 2.5625 -5.5625 "/> -</symbol> -<symbol overflow="visible" id="glyph5-3"> -<path style="stroke:none;" d="M 5.28125 0.25 C 5.65625 0.25 6.003906 0.207031 6.328125 0.125 C 6.648438 0.0507812 6.945312 -0.0507812 7.21875 -0.1875 C 7.488281 -0.320312 7.726562 -0.476562 7.9375 -0.65625 C 8.144531 -0.832031 8.320312 -1.019531 8.46875 -1.21875 L 8.46875 0 L 10.25 0 L 10.25 -13.828125 L 8.46875 -13.828125 L 8.46875 -7.953125 C 8.375 -8.085938 8.257812 -8.21875 8.125 -8.34375 C 8 -8.476562 7.859375 -8.597656 7.703125 -8.703125 C 7.554688 -8.804688 7.394531 -8.898438 7.21875 -8.984375 C 7.039062 -9.078125 6.847656 -9.15625 6.640625 -9.21875 C 6.429688 -9.289062 6.210938 -9.34375 5.984375 -9.375 C 5.765625 -9.40625 5.53125 -9.421875 5.28125 -9.421875 C 4.832031 -9.421875 4.40625 -9.367188 4 -9.265625 C 3.59375 -9.160156 3.21875 -9.003906 2.875 -8.796875 C 2.53125 -8.585938 2.21875 -8.335938 1.9375 -8.046875 C 1.664062 -7.765625 1.429688 -7.441406 1.234375 -7.078125 C 1.046875 -6.722656 0.898438 -6.332031 0.796875 -5.90625 C 0.691406 -5.488281 0.640625 -5.050781 0.640625 -4.59375 C 0.640625 -4.125 0.691406 -3.679688 0.796875 -3.265625 C 0.898438 -2.847656 1.046875 -2.457031 1.234375 -2.09375 C 1.429688 -1.738281 1.664062 -1.414062 1.9375 -1.125 C 2.21875 -0.84375 2.53125 -0.597656 2.875 -0.390625 C 3.21875 -0.179688 3.59375 -0.0234375 4 0.078125 C 4.40625 0.191406 4.832031 0.25 5.28125 0.25 Z M 5.5625 -1.359375 C 5.28125 -1.359375 5.007812 -1.390625 4.75 -1.453125 C 4.488281 -1.523438 4.242188 -1.625 4.015625 -1.75 C 3.796875 -1.882812 3.59375 -2.046875 3.40625 -2.234375 C 3.226562 -2.421875 3.070312 -2.628906 2.9375 -2.859375 C 2.8125 -3.097656 2.710938 -3.363281 2.640625 -3.65625 C 2.578125 -3.945312 2.546875 -4.257812 2.546875 -4.59375 C 2.546875 -4.925781 2.578125 -5.234375 2.640625 -5.515625 C 2.710938 -5.804688 2.8125 -6.070312 2.9375 -6.3125 C 3.070312 -6.550781 3.226562 -6.765625 3.40625 -6.953125 C 3.59375 -7.140625 3.796875 -7.296875 4.015625 -7.421875 C 4.242188 -7.554688 4.488281 -7.65625 4.75 -7.71875 C 5.007812 -7.789062 5.28125 -7.828125 5.5625 -7.828125 C 5.84375 -7.828125 6.113281 -7.789062 6.375 -7.71875 C 6.632812 -7.65625 6.875 -7.554688 7.09375 -7.421875 C 7.320312 -7.296875 7.523438 -7.140625 7.703125 -6.953125 C 7.890625 -6.765625 8.046875 -6.550781 8.171875 -6.3125 C 8.304688 -6.070312 8.410156 -5.804688 8.484375 -5.515625 C 8.554688 -5.234375 8.59375 -4.925781 8.59375 -4.59375 C 8.59375 -4.257812 8.554688 -3.945312 8.484375 -3.65625 C 8.410156 -3.363281 8.304688 -3.097656 8.171875 -2.859375 C 8.046875 -2.628906 7.890625 -2.421875 7.703125 -2.234375 C 7.523438 -2.046875 7.320312 -1.882812 7.09375 -1.75 C 6.875 -1.625 6.632812 -1.523438 6.375 -1.453125 C 6.113281 -1.390625 5.84375 -1.359375 5.5625 -1.359375 Z M 5.5625 -1.359375 "/> -</symbol> -<symbol overflow="visible" id="glyph5-4"> -<path style="stroke:none;" d="M 2.265625 -11.171875 C 2.429688 -11.171875 2.585938 -11.195312 2.734375 -11.25 C 2.878906 -11.300781 3.003906 -11.378906 3.109375 -11.484375 C 3.222656 -11.597656 3.3125 -11.726562 3.375 -11.875 C 3.4375 -12.019531 3.46875 -12.179688 3.46875 -12.359375 C 3.46875 -12.535156 3.4375 -12.695312 3.375 -12.84375 C 3.3125 -12.988281 3.222656 -13.113281 3.109375 -13.21875 C 3.003906 -13.332031 2.878906 -13.414062 2.734375 -13.46875 C 2.585938 -13.519531 2.429688 -13.546875 2.265625 -13.546875 C 2.085938 -13.546875 1.921875 -13.519531 1.765625 -13.46875 C 1.617188 -13.414062 1.492188 -13.332031 1.390625 -13.21875 C 1.285156 -13.113281 1.203125 -12.988281 1.140625 -12.84375 C 1.078125 -12.695312 1.046875 -12.535156 1.046875 -12.359375 C 1.046875 -12.179688 1.078125 -12.019531 1.140625 -11.875 C 1.203125 -11.726562 1.285156 -11.597656 1.390625 -11.484375 C 1.492188 -11.378906 1.617188 -11.300781 1.765625 -11.25 C 1.921875 -11.195312 2.085938 -11.171875 2.265625 -11.171875 Z M 1.359375 0 L 3.15625 0 L 3.15625 -9.1875 L 1.359375 -9.1875 Z M 1.359375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-5"> -<path style="stroke:none;" d="M 5.484375 0.25 C 5.960938 0.25 6.425781 0.191406 6.875 0.078125 C 7.320312 -0.0234375 7.738281 -0.191406 8.125 -0.421875 C 8.519531 -0.648438 8.875 -0.941406 9.1875 -1.296875 C 9.507812 -1.660156 9.78125 -2.085938 10 -2.578125 L 8.359375 -3.28125 C 8.222656 -2.945312 8.054688 -2.65625 7.859375 -2.40625 C 7.671875 -2.164062 7.453125 -1.96875 7.203125 -1.8125 C 6.953125 -1.65625 6.679688 -1.539062 6.390625 -1.46875 C 6.097656 -1.394531 5.789062 -1.359375 5.46875 -1.359375 C 5.1875 -1.359375 4.914062 -1.394531 4.65625 -1.46875 C 4.394531 -1.539062 4.15625 -1.644531 3.9375 -1.78125 C 3.71875 -1.914062 3.519531 -2.078125 3.34375 -2.265625 C 3.164062 -2.460938 3.015625 -2.679688 2.890625 -2.921875 C 2.773438 -3.160156 2.679688 -3.421875 2.609375 -3.703125 C 2.546875 -3.992188 2.515625 -4.289062 2.515625 -4.59375 C 2.515625 -4.914062 2.546875 -5.210938 2.609375 -5.484375 C 2.679688 -5.765625 2.773438 -6.023438 2.890625 -6.265625 C 3.015625 -6.515625 3.164062 -6.734375 3.34375 -6.921875 C 3.519531 -7.109375 3.71875 -7.269531 3.9375 -7.40625 C 4.15625 -7.539062 4.394531 -7.644531 4.65625 -7.71875 C 4.914062 -7.789062 5.1875 -7.828125 5.46875 -7.828125 C 5.789062 -7.828125 6.097656 -7.789062 6.390625 -7.71875 C 6.679688 -7.644531 6.953125 -7.53125 7.203125 -7.375 C 7.453125 -7.21875 7.671875 -7.019531 7.859375 -6.78125 C 8.054688 -6.539062 8.222656 -6.25 8.359375 -5.90625 L 10 -6.625 C 9.78125 -7.113281 9.507812 -7.535156 9.1875 -7.890625 C 8.875 -8.242188 8.519531 -8.535156 8.125 -8.765625 C 7.738281 -8.992188 7.320312 -9.160156 6.875 -9.265625 C 6.425781 -9.378906 5.960938 -9.4375 5.484375 -9.4375 C 5.015625 -9.4375 4.566406 -9.378906 4.140625 -9.265625 C 3.722656 -9.160156 3.332031 -9.003906 2.96875 -8.796875 C 2.613281 -8.585938 2.289062 -8.335938 2 -8.046875 C 1.71875 -7.753906 1.472656 -7.425781 1.265625 -7.0625 C 1.066406 -6.707031 0.910156 -6.320312 0.796875 -5.90625 C 0.691406 -5.488281 0.640625 -5.050781 0.640625 -4.59375 C 0.640625 -4.132812 0.691406 -3.695312 0.796875 -3.28125 C 0.910156 -2.863281 1.066406 -2.472656 1.265625 -2.109375 C 1.472656 -1.753906 1.71875 -1.429688 2 -1.140625 C 2.289062 -0.847656 2.613281 -0.597656 2.96875 -0.390625 C 3.332031 -0.191406 3.722656 -0.0351562 4.140625 0.078125 C 4.566406 0.191406 5.015625 0.25 5.484375 0.25 Z M 5.484375 0.25 "/> -</symbol> -<symbol overflow="visible" id="glyph5-6"> -<path style="stroke:none;" d="M 5.28125 0.25 C 5.65625 0.25 6.003906 0.207031 6.328125 0.125 C 6.648438 0.0507812 6.945312 -0.0507812 7.21875 -0.1875 C 7.488281 -0.320312 7.726562 -0.476562 7.9375 -0.65625 C 8.144531 -0.832031 8.320312 -1.019531 8.46875 -1.21875 L 8.46875 0 L 10.25 0 L 10.25 -9.1875 L 8.46875 -9.1875 L 8.46875 -7.953125 C 8.375 -8.085938 8.257812 -8.21875 8.125 -8.34375 C 8 -8.476562 7.859375 -8.597656 7.703125 -8.703125 C 7.554688 -8.804688 7.394531 -8.898438 7.21875 -8.984375 C 7.039062 -9.078125 6.847656 -9.15625 6.640625 -9.21875 C 6.429688 -9.289062 6.210938 -9.34375 5.984375 -9.375 C 5.765625 -9.40625 5.53125 -9.421875 5.28125 -9.421875 C 4.832031 -9.421875 4.40625 -9.367188 4 -9.265625 C 3.59375 -9.160156 3.21875 -9.003906 2.875 -8.796875 C 2.53125 -8.585938 2.21875 -8.335938 1.9375 -8.046875 C 1.664062 -7.765625 1.429688 -7.441406 1.234375 -7.078125 C 1.046875 -6.722656 0.898438 -6.332031 0.796875 -5.90625 C 0.691406 -5.488281 0.640625 -5.050781 0.640625 -4.59375 C 0.640625 -4.125 0.691406 -3.679688 0.796875 -3.265625 C 0.898438 -2.847656 1.046875 -2.457031 1.234375 -2.09375 C 1.429688 -1.738281 1.664062 -1.414062 1.9375 -1.125 C 2.21875 -0.84375 2.53125 -0.597656 2.875 -0.390625 C 3.21875 -0.179688 3.59375 -0.0234375 4 0.078125 C 4.40625 0.191406 4.832031 0.25 5.28125 0.25 Z M 5.5625 -1.359375 C 5.28125 -1.359375 5.007812 -1.390625 4.75 -1.453125 C 4.488281 -1.523438 4.242188 -1.625 4.015625 -1.75 C 3.796875 -1.882812 3.59375 -2.046875 3.40625 -2.234375 C 3.226562 -2.421875 3.070312 -2.628906 2.9375 -2.859375 C 2.8125 -3.097656 2.710938 -3.363281 2.640625 -3.65625 C 2.578125 -3.945312 2.546875 -4.257812 2.546875 -4.59375 C 2.546875 -4.925781 2.578125 -5.234375 2.640625 -5.515625 C 2.710938 -5.804688 2.8125 -6.070312 2.9375 -6.3125 C 3.070312 -6.550781 3.226562 -6.765625 3.40625 -6.953125 C 3.59375 -7.140625 3.796875 -7.296875 4.015625 -7.421875 C 4.242188 -7.554688 4.488281 -7.65625 4.75 -7.71875 C 5.007812 -7.789062 5.28125 -7.828125 5.5625 -7.828125 C 5.84375 -7.828125 6.113281 -7.789062 6.375 -7.71875 C 6.632812 -7.65625 6.875 -7.554688 7.09375 -7.421875 C 7.320312 -7.296875 7.523438 -7.140625 7.703125 -6.953125 C 7.890625 -6.765625 8.046875 -6.550781 8.171875 -6.3125 C 8.304688 -6.070312 8.410156 -5.804688 8.484375 -5.515625 C 8.554688 -5.234375 8.59375 -4.925781 8.59375 -4.59375 C 8.59375 -4.257812 8.554688 -3.945312 8.484375 -3.65625 C 8.410156 -3.363281 8.304688 -3.097656 8.171875 -2.859375 C 8.046875 -2.628906 7.890625 -2.421875 7.703125 -2.234375 C 7.523438 -2.046875 7.320312 -1.882812 7.09375 -1.75 C 6.875 -1.625 6.632812 -1.523438 6.375 -1.453125 C 6.113281 -1.390625 5.84375 -1.359375 5.5625 -1.359375 Z M 5.5625 -1.359375 "/> -</symbol> -<symbol overflow="visible" id="glyph5-7"> -<path style="stroke:none;" d="M 1.359375 0 L 3.15625 0 L 3.15625 -13.828125 L 1.359375 -13.828125 Z M 1.359375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-8"> -<path style="stroke:none;" d="M 1.546875 0 L 3.40625 0 L 3.40625 -12.953125 L 1.546875 -12.953125 Z M 1.546875 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-9"> -<path style="stroke:none;" d="M 1.359375 0 L 3.15625 0 L 3.15625 -5.078125 C 3.15625 -5.523438 3.203125 -5.921875 3.296875 -6.265625 C 3.398438 -6.609375 3.546875 -6.894531 3.734375 -7.125 C 3.921875 -7.363281 4.15625 -7.539062 4.4375 -7.65625 C 4.726562 -7.78125 5.054688 -7.84375 5.421875 -7.84375 C 5.785156 -7.84375 6.109375 -7.78125 6.390625 -7.65625 C 6.671875 -7.539062 6.90625 -7.363281 7.09375 -7.125 C 7.289062 -6.894531 7.4375 -6.609375 7.53125 -6.265625 C 7.632812 -5.921875 7.6875 -5.523438 7.6875 -5.078125 L 7.6875 0 L 9.484375 0 L 9.484375 -5.5 C 9.484375 -6.113281 9.398438 -6.660156 9.234375 -7.140625 C 9.078125 -7.628906 8.847656 -8.039062 8.546875 -8.375 C 8.253906 -8.71875 7.894531 -8.976562 7.46875 -9.15625 C 7.039062 -9.332031 6.554688 -9.421875 6.015625 -9.421875 C 5.671875 -9.421875 5.351562 -9.390625 5.0625 -9.328125 C 4.78125 -9.273438 4.519531 -9.191406 4.28125 -9.078125 C 4.050781 -8.960938 3.84375 -8.820312 3.65625 -8.65625 C 3.46875 -8.5 3.300781 -8.3125 3.15625 -8.09375 L 3.15625 -9.1875 L 1.359375 -9.1875 Z M 1.359375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-10"> -<path style="stroke:none;" d="M 2.28125 0 L 4.078125 0 L 4.078125 -7.59375 L 7.375 -7.59375 L 7.375 -9.1875 L 4.078125 -9.1875 L 4.078125 -10.640625 C 4.078125 -10.890625 4.097656 -11.101562 4.140625 -11.28125 C 4.191406 -11.46875 4.257812 -11.625 4.34375 -11.75 C 4.4375 -11.882812 4.546875 -11.992188 4.671875 -12.078125 C 4.796875 -12.171875 4.929688 -12.242188 5.078125 -12.296875 C 5.222656 -12.347656 5.378906 -12.382812 5.546875 -12.40625 C 5.722656 -12.425781 5.898438 -12.4375 6.078125 -12.4375 C 6.179688 -12.4375 6.285156 -12.429688 6.390625 -12.421875 C 6.503906 -12.421875 6.613281 -12.414062 6.71875 -12.40625 C 6.832031 -12.40625 6.941406 -12.394531 7.046875 -12.375 C 7.160156 -12.363281 7.269531 -12.351562 7.375 -12.34375 L 7.375 -13.90625 C 7.164062 -13.9375 6.953125 -13.957031 6.734375 -13.96875 C 6.515625 -13.988281 6.296875 -14 6.078125 -14 C 5.742188 -14 5.421875 -13.972656 5.109375 -13.921875 C 4.796875 -13.878906 4.5 -13.804688 4.21875 -13.703125 C 3.9375 -13.597656 3.675781 -13.457031 3.4375 -13.28125 C 3.207031 -13.113281 3.003906 -12.90625 2.828125 -12.65625 C 2.660156 -12.414062 2.523438 -12.128906 2.421875 -11.796875 C 2.328125 -11.472656 2.28125 -11.101562 2.28125 -10.6875 L 2.28125 -9.1875 L 0.21875 -9.1875 L 0.21875 -7.59375 L 2.28125 -7.59375 Z M 2.28125 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-11"> -<path style="stroke:none;" d="M 5.546875 0.25 C 6.023438 0.25 6.476562 0.191406 6.90625 0.078125 C 7.332031 -0.0351562 7.726562 -0.191406 8.09375 -0.390625 C 8.457031 -0.597656 8.785156 -0.84375 9.078125 -1.125 C 9.367188 -1.414062 9.613281 -1.738281 9.8125 -2.09375 C 10.019531 -2.457031 10.179688 -2.847656 10.296875 -3.265625 C 10.410156 -3.691406 10.46875 -4.132812 10.46875 -4.59375 C 10.46875 -5.0625 10.410156 -5.503906 10.296875 -5.921875 C 10.179688 -6.335938 10.019531 -6.722656 9.8125 -7.078125 C 9.613281 -7.441406 9.367188 -7.769531 9.078125 -8.0625 C 8.785156 -8.351562 8.457031 -8.597656 8.09375 -8.796875 C 7.726562 -9.003906 7.332031 -9.160156 6.90625 -9.265625 C 6.476562 -9.378906 6.023438 -9.4375 5.546875 -9.4375 C 5.066406 -9.4375 4.613281 -9.378906 4.1875 -9.265625 C 3.757812 -9.160156 3.363281 -9.003906 3 -8.796875 C 2.632812 -8.597656 2.304688 -8.351562 2.015625 -8.0625 C 1.734375 -7.769531 1.488281 -7.441406 1.28125 -7.078125 C 1.082031 -6.722656 0.925781 -6.335938 0.8125 -5.921875 C 0.695312 -5.503906 0.640625 -5.0625 0.640625 -4.59375 C 0.640625 -4.132812 0.695312 -3.691406 0.8125 -3.265625 C 0.925781 -2.847656 1.082031 -2.457031 1.28125 -2.09375 C 1.488281 -1.738281 1.734375 -1.414062 2.015625 -1.125 C 2.304688 -0.84375 2.632812 -0.597656 3 -0.390625 C 3.363281 -0.191406 3.757812 -0.0351562 4.1875 0.078125 C 4.613281 0.191406 5.066406 0.25 5.546875 0.25 Z M 5.546875 -1.359375 C 5.242188 -1.359375 4.957031 -1.394531 4.6875 -1.46875 C 4.425781 -1.539062 4.179688 -1.644531 3.953125 -1.78125 C 3.722656 -1.914062 3.519531 -2.078125 3.34375 -2.265625 C 3.164062 -2.460938 3.015625 -2.679688 2.890625 -2.921875 C 2.773438 -3.160156 2.679688 -3.421875 2.609375 -3.703125 C 2.546875 -3.992188 2.515625 -4.289062 2.515625 -4.59375 C 2.515625 -4.914062 2.546875 -5.210938 2.609375 -5.484375 C 2.679688 -5.765625 2.773438 -6.023438 2.890625 -6.265625 C 3.015625 -6.515625 3.164062 -6.734375 3.34375 -6.921875 C 3.519531 -7.109375 3.722656 -7.269531 3.953125 -7.40625 C 4.179688 -7.539062 4.425781 -7.644531 4.6875 -7.71875 C 4.957031 -7.789062 5.242188 -7.828125 5.546875 -7.828125 C 5.847656 -7.828125 6.132812 -7.789062 6.40625 -7.71875 C 6.675781 -7.644531 6.921875 -7.539062 7.140625 -7.40625 C 7.367188 -7.269531 7.570312 -7.109375 7.75 -6.921875 C 7.9375 -6.734375 8.085938 -6.515625 8.203125 -6.265625 C 8.328125 -6.023438 8.421875 -5.765625 8.484375 -5.484375 C 8.554688 -5.203125 8.59375 -4.90625 8.59375 -4.59375 C 8.59375 -4.289062 8.554688 -3.992188 8.484375 -3.703125 C 8.421875 -3.421875 8.328125 -3.160156 8.203125 -2.921875 C 8.085938 -2.679688 7.9375 -2.460938 7.75 -2.265625 C 7.570312 -2.078125 7.367188 -1.914062 7.140625 -1.78125 C 6.921875 -1.644531 6.675781 -1.539062 6.40625 -1.46875 C 6.132812 -1.394531 5.847656 -1.359375 5.546875 -1.359375 Z M 5.546875 -1.359375 "/> -</symbol> -<symbol overflow="visible" id="glyph5-12"> -<path style="stroke:none;" d="M 1.359375 0 L 3.15625 0 L 3.15625 -4.640625 C 3.15625 -5.023438 3.195312 -5.363281 3.28125 -5.65625 C 3.363281 -5.957031 3.484375 -6.21875 3.640625 -6.4375 C 3.804688 -6.664062 3.992188 -6.851562 4.203125 -7 C 4.421875 -7.15625 4.660156 -7.269531 4.921875 -7.34375 C 5.179688 -7.425781 5.453125 -7.472656 5.734375 -7.484375 C 6.015625 -7.503906 6.300781 -7.488281 6.59375 -7.4375 L 6.59375 -9.34375 C 6.300781 -9.382812 5.988281 -9.382812 5.65625 -9.34375 C 5.320312 -9.300781 5 -9.210938 4.6875 -9.078125 C 4.375 -8.953125 4.082031 -8.769531 3.8125 -8.53125 C 3.539062 -8.300781 3.320312 -8.007812 3.15625 -7.65625 L 3.15625 -9.1875 L 1.359375 -9.1875 Z M 1.359375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-13"> -<path style="stroke:none;" d="M 1.359375 0 L 3.15625 0 L 3.15625 -5.078125 C 3.15625 -5.523438 3.195312 -5.921875 3.28125 -6.265625 C 3.375 -6.609375 3.507812 -6.894531 3.6875 -7.125 C 3.875 -7.363281 4.101562 -7.539062 4.375 -7.65625 C 4.644531 -7.78125 4.960938 -7.84375 5.328125 -7.84375 C 5.710938 -7.84375 6.039062 -7.78125 6.3125 -7.65625 C 6.59375 -7.539062 6.820312 -7.363281 7 -7.125 C 7.175781 -6.894531 7.304688 -6.609375 7.390625 -6.265625 C 7.472656 -5.921875 7.515625 -5.523438 7.515625 -5.078125 L 7.515625 0 L 9.296875 0 L 9.296875 -5.078125 C 9.296875 -5.523438 9.34375 -5.921875 9.4375 -6.265625 C 9.53125 -6.609375 9.664062 -6.894531 9.84375 -7.125 C 10.03125 -7.363281 10.257812 -7.539062 10.53125 -7.65625 C 10.800781 -7.78125 11.117188 -7.84375 11.484375 -7.84375 C 11.867188 -7.84375 12.195312 -7.78125 12.46875 -7.65625 C 12.75 -7.539062 12.976562 -7.363281 13.15625 -7.125 C 13.332031 -6.894531 13.460938 -6.609375 13.546875 -6.265625 C 13.628906 -5.921875 13.671875 -5.523438 13.671875 -5.078125 L 13.671875 0 L 15.453125 0 L 15.453125 -5.5 C 15.453125 -6.113281 15.378906 -6.660156 15.234375 -7.140625 C 15.097656 -7.628906 14.890625 -8.039062 14.609375 -8.375 C 14.328125 -8.71875 13.972656 -8.976562 13.546875 -9.15625 C 13.128906 -9.332031 12.640625 -9.421875 12.078125 -9.421875 C 11.722656 -9.421875 11.390625 -9.390625 11.078125 -9.328125 C 10.773438 -9.265625 10.488281 -9.160156 10.21875 -9.015625 C 9.945312 -8.878906 9.695312 -8.703125 9.46875 -8.484375 C 9.25 -8.265625 9.054688 -8 8.890625 -7.6875 C 8.765625 -7.96875 8.609375 -8.21875 8.421875 -8.4375 C 8.234375 -8.65625 8.015625 -8.835938 7.765625 -8.984375 C 7.523438 -9.128906 7.25 -9.238281 6.9375 -9.3125 C 6.625 -9.382812 6.285156 -9.421875 5.921875 -9.421875 C 5.566406 -9.421875 5.242188 -9.390625 4.953125 -9.328125 C 4.671875 -9.273438 4.421875 -9.191406 4.203125 -9.078125 C 3.992188 -8.960938 3.800781 -8.820312 3.625 -8.65625 C 3.457031 -8.5 3.300781 -8.3125 3.15625 -8.09375 L 3.15625 -9.1875 L 1.359375 -9.1875 Z M 1.359375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph5-14"> -<path style="stroke:none;" d="M 5.4375 0.125 C 5.550781 0.125 5.671875 0.117188 5.796875 0.109375 C 5.921875 0.109375 6.039062 0.101562 6.15625 0.09375 C 6.28125 0.0820312 6.398438 0.0664062 6.515625 0.046875 C 6.640625 0.0351562 6.769531 0.0195312 6.90625 0 L 6.90625 -1.546875 C 6.820312 -1.535156 6.734375 -1.523438 6.640625 -1.515625 C 6.554688 -1.503906 6.46875 -1.492188 6.375 -1.484375 C 6.28125 -1.484375 6.179688 -1.476562 6.078125 -1.46875 C 5.972656 -1.46875 5.867188 -1.46875 5.765625 -1.46875 C 5.523438 -1.46875 5.300781 -1.488281 5.09375 -1.53125 C 4.882812 -1.582031 4.703125 -1.664062 4.546875 -1.78125 C 4.390625 -1.90625 4.265625 -2.070312 4.171875 -2.28125 C 4.085938 -2.5 4.046875 -2.773438 4.046875 -3.109375 L 4.046875 -7.59375 L 6.96875 -7.59375 L 6.96875 -9.1875 L 4.046875 -9.1875 L 4.046875 -12.125 L 2.265625 -12.125 L 2.265625 -9.1875 L 0.203125 -9.1875 L 0.203125 -7.59375 L 2.265625 -7.59375 L 2.265625 -2.953125 C 2.265625 -2.335938 2.34375 -1.832031 2.5 -1.4375 C 2.664062 -1.039062 2.890625 -0.726562 3.171875 -0.5 C 3.460938 -0.269531 3.800781 -0.109375 4.1875 -0.015625 C 4.570312 0.078125 4.988281 0.125 5.4375 0.125 Z M 5.4375 0.125 "/> -</symbol> -<symbol overflow="visible" id="glyph5-15"> -<path style="stroke:none;" d="M 4.3125 0.234375 C 4.78125 0.234375 5.222656 0.171875 5.640625 0.046875 C 6.066406 -0.0664062 6.4375 -0.242188 6.75 -0.484375 C 7.0625 -0.722656 7.3125 -1.015625 7.5 -1.359375 C 7.6875 -1.703125 7.78125 -2.085938 7.78125 -2.515625 C 7.78125 -2.941406 7.710938 -3.304688 7.578125 -3.609375 C 7.441406 -3.921875 7.253906 -4.1875 7.015625 -4.40625 C 6.773438 -4.625 6.476562 -4.804688 6.125 -4.953125 C 5.78125 -5.109375 5.390625 -5.238281 4.953125 -5.34375 L 4.046875 -5.578125 C 3.828125 -5.628906 3.628906 -5.691406 3.453125 -5.765625 C 3.273438 -5.835938 3.125 -5.921875 3 -6.015625 C 2.875 -6.117188 2.78125 -6.234375 2.71875 -6.359375 C 2.65625 -6.492188 2.625 -6.648438 2.625 -6.828125 C 2.625 -6.992188 2.660156 -7.148438 2.734375 -7.296875 C 2.816406 -7.441406 2.925781 -7.5625 3.0625 -7.65625 C 3.195312 -7.757812 3.359375 -7.835938 3.546875 -7.890625 C 3.742188 -7.941406 3.945312 -7.96875 4.15625 -7.96875 C 4.300781 -7.96875 4.441406 -7.957031 4.578125 -7.9375 C 4.710938 -7.914062 4.84375 -7.882812 4.96875 -7.84375 C 5.09375 -7.800781 5.210938 -7.742188 5.328125 -7.671875 C 5.441406 -7.597656 5.550781 -7.507812 5.65625 -7.40625 C 5.769531 -7.3125 5.867188 -7.195312 5.953125 -7.0625 C 6.046875 -6.9375 6.132812 -6.789062 6.21875 -6.625 L 7.59375 -7.4375 C 7.4375 -7.769531 7.238281 -8.0625 7 -8.3125 C 6.769531 -8.5625 6.503906 -8.769531 6.203125 -8.9375 C 5.910156 -9.101562 5.59375 -9.226562 5.25 -9.3125 C 4.90625 -9.394531 4.550781 -9.4375 4.1875 -9.4375 C 3.882812 -9.4375 3.59375 -9.410156 3.3125 -9.359375 C 3.03125 -9.304688 2.765625 -9.226562 2.515625 -9.125 C 2.265625 -9.019531 2.035156 -8.890625 1.828125 -8.734375 C 1.628906 -8.578125 1.457031 -8.398438 1.3125 -8.203125 C 1.164062 -8.015625 1.050781 -7.800781 0.96875 -7.5625 C 0.882812 -7.332031 0.84375 -7.078125 0.84375 -6.796875 C 0.84375 -6.390625 0.910156 -6.035156 1.046875 -5.734375 C 1.179688 -5.429688 1.363281 -5.171875 1.59375 -4.953125 C 1.832031 -4.734375 2.101562 -4.550781 2.40625 -4.40625 C 2.71875 -4.257812 3.050781 -4.140625 3.40625 -4.046875 L 4.28125 -3.828125 C 4.582031 -3.753906 4.835938 -3.671875 5.046875 -3.578125 C 5.265625 -3.492188 5.441406 -3.394531 5.578125 -3.28125 C 5.722656 -3.175781 5.828125 -3.054688 5.890625 -2.921875 C 5.960938 -2.785156 6 -2.628906 6 -2.453125 C 6 -2.265625 5.953125 -2.09375 5.859375 -1.9375 C 5.773438 -1.78125 5.660156 -1.648438 5.515625 -1.546875 C 5.367188 -1.441406 5.191406 -1.359375 4.984375 -1.296875 C 4.773438 -1.242188 4.550781 -1.21875 4.3125 -1.21875 C 4.03125 -1.21875 3.765625 -1.253906 3.515625 -1.328125 C 3.273438 -1.398438 3.046875 -1.507812 2.828125 -1.65625 C 2.617188 -1.800781 2.429688 -1.976562 2.265625 -2.1875 C 2.097656 -2.40625 1.945312 -2.65625 1.8125 -2.9375 L 0.453125 -2.046875 C 0.628906 -1.671875 0.847656 -1.34375 1.109375 -1.0625 C 1.367188 -0.78125 1.664062 -0.539062 2 -0.34375 C 2.332031 -0.15625 2.691406 -0.015625 3.078125 0.078125 C 3.472656 0.179688 3.882812 0.234375 4.3125 0.234375 Z M 4.3125 0.234375 "/> -</symbol> -<symbol overflow="visible" id="glyph5-16"> -<path style="stroke:none;" d="M 1.546875 0 L 3.40625 0 L 3.40625 -5.265625 L 5.75 -5.265625 C 6.5625 -5.265625 7.253906 -5.359375 7.828125 -5.546875 C 8.410156 -5.742188 8.890625 -6.007812 9.265625 -6.34375 C 9.640625 -6.6875 9.914062 -7.09375 10.09375 -7.5625 C 10.28125 -8.039062 10.375 -8.554688 10.375 -9.109375 C 10.375 -9.671875 10.28125 -10.1875 10.09375 -10.65625 C 9.914062 -11.125 9.640625 -11.523438 9.265625 -11.859375 C 8.890625 -12.203125 8.410156 -12.46875 7.828125 -12.65625 C 7.253906 -12.851562 6.5625 -12.953125 5.75 -12.953125 L 1.546875 -12.953125 Z M 3.40625 -6.890625 L 3.40625 -11.328125 L 5.609375 -11.328125 C 6.078125 -11.328125 6.484375 -11.28125 6.828125 -11.1875 C 7.179688 -11.101562 7.472656 -10.96875 7.703125 -10.78125 C 7.929688 -10.601562 8.101562 -10.375 8.21875 -10.09375 C 8.332031 -9.8125 8.390625 -9.488281 8.390625 -9.125 C 8.390625 -8.75 8.332031 -8.421875 8.21875 -8.140625 C 8.101562 -7.859375 7.929688 -7.625 7.703125 -7.4375 C 7.472656 -7.257812 7.179688 -7.125 6.828125 -7.03125 C 6.484375 -6.9375 6.078125 -6.890625 5.609375 -6.890625 Z M 3.40625 -6.890625 "/> -</symbol> -<symbol overflow="visible" id="glyph6-0"> -<path style="stroke:none;" d="M 0.4375 -2.125 L 3.40625 -5.078125 C 3.34375 -5.15625 3.265625 -5.21875 3.171875 -5.28125 L 0.4375 -2.546875 Z M 0.4375 -1.25 L 2.375 -3.1875 C 2.453125 -3.375 2.5625 -3.53125 2.6875 -3.65625 C 3 -3.96875 3.5 -4.21875 3.5 -4.609375 L 3.5 -4.75 L 0.4375 -1.6875 Z M 0.921875 0 L 2.203125 -1.28125 L 2.203125 -1.484375 L 2.40625 -1.484375 L 3.21875 -2.296875 L 2.796875 -2.296875 L 0.484375 0 Z M 2.828125 -1.484375 L 3.265625 -1.484375 L 5.25 -3.453125 L 5.25 -3.890625 Z M 0.4375 -2.984375 L 2.828125 -5.375 L 2.75 -5.375 C 2.328125 -5.375 2.09375 -5.015625 2.09375 -4.703125 L 1.75 -4.71875 L 0.4375 -3.421875 Z M 3.515625 0 L 5.25 -1.734375 L 5.25 -2.15625 L 3.390625 -0.3125 L 3.390625 -0.296875 L 3.375 -0.296875 L 3.078125 0 Z M 3.390625 -1.171875 L 3.390625 -0.734375 L 5.25 -2.59375 L 5.25 -3.03125 Z M 0.4375 -0.390625 L 2.359375 -2.296875 L 2.265625 -2.296875 L 2.265625 -2.640625 L 0.4375 -0.828125 Z M 0.4375 -5.140625 L 0.4375 -4.71875 L 0.921875 -5.1875 C 1.09375 -5.828125 1.578125 -6.328125 2.21875 -6.484375 L 2.609375 -6.875 L 2.171875 -6.875 Z M 4.734375 -4.25 C 4.59375 -3.640625 4.140625 -3.375 3.953125 -3.234375 C 3.578125 -2.953125 3.34375 -2.84375 3.34375 -2.484375 L 3.34375 -2.421875 L 5.25 -4.328125 L 5.25 -4.765625 Z M 0.4375 -6.015625 L 0.4375 -5.578125 L 1.734375 -6.875 L 1.3125 -6.875 Z M 4.375 0 L 5.25 -0.859375 L 5.25 -1.296875 L 3.953125 0 Z M 4.046875 -6.15625 C 4.125 -6.09375 4.203125 -6.03125 4.28125 -5.953125 L 5.203125 -6.875 L 4.765625 -6.875 Z M 0.4375 -3.84375 L 1.34375 -4.75 L 0.9375 -4.78125 L 0.4375 -4.28125 Z M 4.46875 -5.71875 C 4.53125 -5.609375 4.578125 -5.53125 4.625 -5.4375 L 5.25 -6.0625 L 5.25 -6.484375 Z M 1.78125 0 L 2.203125 -0.421875 L 2.203125 -0.84375 L 1.34375 0 Z M 3.484375 -6.46875 C 3.578125 -6.421875 3.6875 -6.375 3.78125 -6.328125 L 4.328125 -6.875 L 3.90625 -6.875 Z M 4.734375 -5.109375 C 4.765625 -4.984375 4.78125 -4.859375 4.78125 -4.734375 L 5.25 -5.1875 L 5.25 -5.625 Z M 2.703125 -6.546875 L 2.765625 -6.546875 C 2.8125 -6.546875 2.84375 -6.5625 2.890625 -6.5625 C 2.96875 -6.5625 3.046875 -6.546875 3.140625 -6.546875 L 3.46875 -6.875 L 3.03125 -6.875 Z M 2.65625 0 L 2.9375 -0.296875 L 2.5 -0.296875 L 2.21875 0 Z M 5.25 -0.4375 L 4.8125 0 L 5.25 0 Z M 0.4375 -6.875 L 0.4375 -6.4375 L 0.875 -6.875 Z M 0.4375 -6.875 "/> -</symbol> -<symbol overflow="visible" id="glyph6-1"> -<path style="stroke:none;" d="M 0.296875 -1.828125 C 0.40625 -0.59375 1.171875 0.125 2.484375 0.125 C 3.828125 0.125 4.703125 -0.671875 4.703125 -1.828125 C 4.703125 -2.703125 4.21875 -3.25 3.359375 -3.453125 C 3.96875 -3.703125 4.375 -4.140625 4.375 -4.875 C 4.375 -5.8125 3.640625 -6.4375 2.5 -6.4375 C 1.328125 -6.4375 0.625 -5.75 0.59375 -4.734375 L 1.53125 -4.734375 C 1.578125 -5.296875 1.921875 -5.609375 2.484375 -5.609375 C 2.953125 -5.609375 3.375 -5.34375 3.375 -4.75 C 3.375 -4.03125 2.828125 -3.78125 2.09375 -3.78125 L 2.09375 -3.015625 L 2.296875 -3.015625 C 3.078125 -3.015625 3.65625 -2.6875 3.65625 -1.859375 C 3.65625 -1.203125 3.234375 -0.6875 2.5 -0.6875 C 1.765625 -0.6875 1.34375 -1.09375 1.265625 -1.828125 Z M 0.296875 -1.828125 "/> -</symbol> -<symbol overflow="visible" id="glyph6-2"> -<path style="stroke:none;" d="M 3.359375 -4.640625 L 2.296875 -3.03125 C 2.046875 -3.40625 1.671875 -3.96875 1.4375 -4.34375 L 1.25 -4.640625 L 0.1875 -4.640625 L 1.78125 -2.34375 L 0.828125 -1.015625 L 0.09375 0 L 1.078125 0 C 1.375 -0.421875 1.921875 -1.234375 2.265625 -1.765625 C 2.6875 -1.078125 3.03125 -0.578125 3.421875 0 L 4.484375 0 C 4.015625 -0.65625 3.25 -1.734375 2.78125 -2.421875 C 3.234375 -3.0625 3.921875 -3.984375 4.359375 -4.640625 Z M 3.359375 -4.640625 "/> -</symbol> -<symbol overflow="visible" id="glyph6-3"> -<path style="stroke:none;" d="M 2.515625 0 L 3.4375 0 L 3.4375 -6.3125 L 2.65625 -6.3125 C 2.5625 -5.609375 2.09375 -5.328125 1 -5.3125 L 1 -4.640625 L 2.15625 -4.640625 C 2.265625 -4.640625 2.390625 -4.640625 2.515625 -4.640625 C 2.5 -4.421875 2.515625 -4.21875 2.515625 -4 Z M 2.515625 0 "/> -</symbol> -<symbol overflow="visible" id="glyph6-4"> -<path style="stroke:none;" d="M 0.359375 -1.625 C 0.515625 -0.59375 1.171875 0.125 2.390625 0.125 C 4.015625 0.125 4.765625 -1.171875 4.765625 -3.421875 C 4.765625 -5.421875 4.015625 -6.4375 2.484375 -6.4375 C 1.171875 -6.4375 0.296875 -5.578125 0.296875 -4.3125 C 0.296875 -3.0625 1.078125 -2.28125 2.265625 -2.28125 C 3.15625 -2.28125 3.640625 -2.71875 3.828125 -3.03125 L 3.84375 -3.03125 C 3.8125 -2.765625 3.8125 -2.53125 3.765625 -2.296875 C 3.609375 -1.328125 3.171875 -0.625 2.40625 -0.625 C 1.65625 -0.625 1.390625 -1.171875 1.34375 -1.625 Z M 2.53125 -5.703125 C 3.296875 -5.703125 3.78125 -5.171875 3.78125 -4.359375 C 3.78125 -3.546875 3.296875 -3.015625 2.53125 -3.015625 C 1.75 -3.015625 1.28125 -3.5625 1.28125 -4.375 C 1.28125 -5.15625 1.75 -5.703125 2.53125 -5.703125 Z M 2.53125 -5.703125 "/> -</symbol> -<symbol overflow="visible" id="glyph6-5"> -<path style="stroke:none;" d=""/> -</symbol> -<symbol overflow="visible" id="glyph6-6"> -<path style="stroke:none;" d="M 4.609375 0 L 4.609375 -0.796875 L 1.59375 -0.796875 C 1.8125 -1 2.234375 -1.328125 2.484375 -1.53125 L 2.984375 -1.953125 C 3.828125 -2.65625 4.578125 -3.5 4.578125 -4.53125 C 4.578125 -5.625 3.84375 -6.4375 2.515625 -6.4375 C 1.171875 -6.4375 0.4375 -5.5625 0.421875 -4.296875 L 1.34375 -4.296875 C 1.375 -5.140625 1.78125 -5.609375 2.515625 -5.609375 C 3.1875 -5.609375 3.578125 -5.171875 3.578125 -4.53125 C 3.578125 -3.84375 3.21875 -3.3125 2.40625 -2.578125 L 0.9375 -1.25 C 0.671875 -1 0.484375 -0.828125 0.484375 -0.796875 L 0.484375 0 Z M 4.609375 0 "/> -</symbol> -<symbol overflow="visible" id="glyph6-7"> -<path style="stroke:none;" d="M 1.453125 -2.75 C 1.609375 -3.046875 1.9375 -3.359375 2.53125 -3.359375 C 3.28125 -3.359375 3.71875 -2.765625 3.71875 -2.03125 C 3.71875 -1.21875 3.28125 -0.65625 2.5 -0.65625 C 1.671875 -0.65625 1.390625 -1.234375 1.3125 -1.734375 L 0.328125 -1.734375 C 0.5 -0.515625 1.28125 0.125 2.53125 0.125 C 3.828125 0.125 4.71875 -0.796875 4.71875 -2.0625 C 4.71875 -3.296875 3.921875 -4.15625 2.734375 -4.15625 C 2.203125 -4.15625 1.78125 -3.96875 1.5 -3.75 L 1.71875 -5.5 L 4.265625 -5.5 L 4.265625 -6.3125 L 0.953125 -6.3125 C 0.828125 -5.15625 0.703125 -4.03125 0.578125 -2.890625 Z M 1.453125 -2.75 "/> -</symbol> -</g> -<filter id="alpha" filterUnits="objectBoundingBox" x="0%" y="0%" width="100%" height="100%"> - <feColorMatrix type="matrix" in="SourceGraphic" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/> -</filter> -<mask id="mask0"> - <g filter="url(#alpha)"> -<rect x="0" y="0" width="1190.55" height="841.89" style="fill:rgb(0%,0%,0%);fill-opacity:0.399994;stroke:none;"/> - </g> -</mask> -<clipPath id="clip1"> - <rect x="0" y="0" width="1120" height="3"/> -</clipPath> -<g id="surface5" clip-path="url(#clip1)"> -<path style="fill:none;stroke-width:0.553;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(13.729858%,12.159729%,12.548828%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000539379 0.000257056 L 1116.851132 0.000257056 " transform="matrix(0.999622,0,0,-0.999622,1.836477,1.320569)"/> -</g> -<mask id="mask1"> - <g filter="url(#alpha)"> -<rect x="0" y="0" width="1190.55" height="841.89" style="fill:rgb(0%,0%,0%);fill-opacity:0.460007;stroke:none;"/> - </g> -</mask> -<clipPath id="clip3"> - <path d="M 1 0.0429688 L 2 0.0429688 L 2 157 L 1 157 Z M 1 0.0429688 "/> -</clipPath> -<clipPath id="clip2"> - <rect x="0" y="0" width="3" height="158"/> -</clipPath> -<g id="surface8" clip-path="url(#clip2)"> -<g clip-path="url(#clip3)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00158346 0.00186086 L -0.00158346 -155.728854 " transform="matrix(0.999622,0,0,-0.999622,1.716427,1.040923)"/> -</g> -</g> -<mask id="mask2"> - <g filter="url(#alpha)"> -<rect x="0" y="0" width="1190.55" height="841.89" style="fill:rgb(0%,0%,0%);fill-opacity:0.460007;stroke:none;"/> - </g> -</mask> -<clipPath id="clip5"> - <path d="M 1 0.128906 L 2 0.128906 L 2 158 L 1 158 Z M 1 0.128906 "/> -</clipPath> -<clipPath id="clip4"> - <rect x="0" y="0" width="3" height="159"/> -</clipPath> -<g id="surface11" clip-path="url(#clip4)"> -<g clip-path="url(#clip5)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000749137 -0.000116848 L -0.000749137 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,1.563249,1.128789)"/> -</g> -</g> -<mask id="mask3"> - <g filter="url(#alpha)"> -<rect x="0" y="0" width="1190.55" height="841.89" style="fill:rgb(0%,0%,0%);fill-opacity:0.460007;stroke:none;"/> - </g> -</mask> -<clipPath id="clip7"> - <path d="M 1 0.128906 L 2 0.128906 L 2 158 L 1 158 Z M 1 0.128906 "/> -</clipPath> -<clipPath id="clip6"> - <rect x="0" y="0" width="3" height="159"/> -</clipPath> -<g id="surface14" clip-path="url(#clip6)"> -<g clip-path="url(#clip7)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.0000529986 -0.000116848 L -0.0000529986 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,1.67974,1.128789)"/> -</g> -</g> -<clipPath id="clip8"> - <path d="M 540 597 L 603.53125 597 L 603.53125 646.199219 L 540 646.199219 Z M 540 597 "/> -</clipPath> -<clipPath id="clip9"> - <path d="M 553 584 L 603.53125 584 L 603.53125 646.199219 L 553 646.199219 Z M 553 584 "/> -</clipPath> -<clipPath id="clip10"> - <path d="M 540 428.164062 L 603.53125 428.164062 L 603.53125 480 L 540 480 Z M 540 428.164062 "/> -</clipPath> -<clipPath id="clip11"> - <path d="M 553 428.164062 L 603.53125 428.164062 L 603.53125 493 L 553 493 Z M 553 428.164062 "/> -</clipPath> -<clipPath id="clip12"> - <path d="M 385.492188 428.164062 L 449 428.164062 L 449 479 L 385.492188 479 Z M 385.492188 428.164062 "/> -</clipPath> -<clipPath id="clip13"> - <path d="M 385.492188 428.164062 L 436 428.164062 L 436 493 L 385.492188 493 Z M 385.492188 428.164062 "/> -</clipPath> -<clipPath id="clip14"> - <path d="M 385.492188 597 L 449 597 L 449 646.199219 L 385.492188 646.199219 Z M 385.492188 597 "/> -</clipPath> -<clipPath id="clip15"> - <path d="M 385.492188 584 L 436 584 L 436 646.199219 L 385.492188 646.199219 Z M 385.492188 584 "/> -</clipPath> -<mask id="mask4"> - <g filter="url(#alpha)"> -<rect x="0" y="0" width="1190.55" height="841.89" style="fill:rgb(0%,0%,0%);fill-opacity:0.460007;stroke:none;"/> - </g> -</mask> -<clipPath id="clip17"> - <path d="M 1 0.0234375 L 2 0.0234375 L 2 219 L 1 219 Z M 1 0.0234375 "/> -</clipPath> -<clipPath id="clip16"> - <rect x="0" y="0" width="3" height="220"/> -</clipPath> -<g id="surface17" clip-path="url(#clip16)"> -<g clip-path="url(#clip17)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00169931 -0.000918842 L 0.00169931 -217.637837 " transform="matrix(0.999622,0,0,-0.999622,1.502208,1.022519)"/> -</g> -</g> -<clipPath id="clip18"> - <path d="M 385.492188 347 L 449 347 L 449 395.589844 L 385.492188 395.589844 Z M 385.492188 347 "/> -</clipPath> -<clipPath id="clip19"> - <path d="M 385.492188 334 L 436 334 L 436 395.589844 L 385.492188 395.589844 Z M 385.492188 334 "/> -</clipPath> -<clipPath id="clip20"> - <path d="M 385.492188 177.554688 L 449 177.554688 L 449 229 L 385.492188 229 Z M 385.492188 177.554688 "/> -</clipPath> -<clipPath id="clip21"> - <path d="M 385.492188 177.554688 L 436 177.554688 L 436 242 L 385.492188 242 Z M 385.492188 177.554688 "/> -</clipPath> -<clipPath id="clip22"> - <path d="M 541 177.554688 L 603.53125 177.554688 L 603.53125 229 L 541 229 Z M 541 177.554688 "/> -</clipPath> -<clipPath id="clip23"> - <path d="M 554 177.554688 L 603.53125 177.554688 L 603.53125 242 L 554 242 Z M 554 177.554688 "/> -</clipPath> -<clipPath id="clip24"> - <path d="M 541 347 L 603.53125 347 L 603.53125 395.589844 L 541 395.589844 Z M 541 347 "/> -</clipPath> -<clipPath id="clip25"> - <path d="M 554 334 L 603.53125 334 L 603.53125 395.589844 L 554 395.589844 Z M 554 334 "/> -</clipPath> -<image id="image32" width="1191" height="842" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABKcAAANKCAYAAABf/S2vAAAABmJLR0QA/wD/AP+gvaeTAAAgAElEQVR4nOzdYVIjOZeGUQHVs47ZyOx/F7OR6YL50Z1fCSEplWlTbzl9ToTDiaHBuH408cS98ksp5X8LAAAAAPx+//NSSvlIPwsAAAAAntJ/v6afAQAAAADPS5wCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAg5kf6CQAAXR+D69bL4BoAAB6COAUAWR+TW+nc11469+31a/M1AADwRxGnAOD3qcPTe3N/NEzV2iDVu70292IVAAB/BHEKAL7XFqDeB9dtsCrla6QqZRyoZlNTbYx6LZ8jVf0xAABEiFMAcH9bWPpZfsWo+rYyOdWbmqqve2dNrU5NvU5uQhUAAL+VOAUA91MHqVGYGk1O1delcz2yOjnVhqnt+q18jlP1xwAA8O3EKQC4XR2jRnHqZ/m81nd0curIgehHJ6feOtdv1TUAAHwbcQoAztui03Z7b65HE1Tt2VO3TE6VMp6aOrLS99a5r28iFQAA30KcAoDj2ig1ilOz1b57vGNfe+7UyuTU7FaHqDZOiVQAAHwLcQoA1m1nSv0spfxdbotTe4GqNNelue6Fqfp69Z36Zit9o9uP5ucDAMBp4hQArOlFqTZQzeJUfd2u9R15x77W3nlTszC1utLXhqn3f+/fll45AACYEKcAYO6j/IpQ7X0bqUZxajRBtbfaVzr3tb0D0XuTU7fEqTpM1YHKqh8AAKeJUwAwVsenvUA1O4NqdbXvOyan2omp7Xq0zjeKU1uQeitfA5UpKgAAThOnAOCr7Wypv8vXMDVb69uLU7PVvtm5U/V9bXVyarTS1waqUZyqw9R2vYWpj/I5UjmLCgCAQ8QpAPhsW+Mb3Y5MT+2t9vUC1Wh6qjTX93qnvjZItXGqF6bqKFV//PHvtTU/AACWiVMA8Mt7+RWh/q/0p6aOBKpbz50q5Wugas3W+vbC1GiV72f5PCHVO2+qjVL1zZofAADLxCkA+EcdoOowNVvt2wtU7QHp9z53qg1T23XvIPQ2Uu2dM/Vevk5PtZNTe9NeAhUAALvEKQD4Jx7VQWoUp2ZhqheoenGqDVVH3rGv5+zkVO+8qdWD0OuVvtFa4va8/a0BAMCUPxgBeHZbmDoSp1YPSG/j1Mpq39nJqe1+NDm1utI3mqCanTO195z9vQEAwJA/FgF4Zm2Mau+Pnj/VXq/GqdXJqSMHoo/eqa+dmJrFqSPnTO2dkeVvDgAAuvyhCMCz2kJSPTXVi1NHz5/ae/e+NlIdfce+Vnve1CxMrZ43dcs5U+1zrT/2dwcAAF/4IxGAZzRb5dubolo5f2rl3fuOrPaV0g9Ut7xT3xahenFqFKZWz5lqn2895eVvDwAAPvEHIgDP5r18XdmbTU8dOX9qL1CtrPbVkaqUr5NTH+XrSt92PwpT23XvEPQ2TvUOQD9zzlTP9vy8ix8AAP8hTgHwTN7L5xjVm5665/lTe3Fqu95b7SvlfpNTbZCqr+swdfScqZH6OdUfv07+GwAAnog4BcCz+CjjyadbV/xWz59679z31vrqSFU696WM36lvb6WvNy1VP492re/IOVOjVb7Sefy/Jl8DAMATEacAeBZ7q3n3WPFbWe+75dypnt7U1Oysqdm78/XC1JFzpo48v5dSyl9FoAIAeHriFADPoF2/a0PU2RW/M4Gqt9rXrvW1EaiUfgAarfWtrvTtHYS+stLXMwtS7fP0twgAwJPzByEAV9cegL4XmVZX/FbOn+oFqtXVvr0JpTZMlfIrSh1Z6WvX+1ZW+krnvn5OPbNzsRyQDgDwxMQpAK5sds7UrSt+o8jVnj/VBqr35nplta+U45NTR+PU0WmpWyenXv99XepIBQDAExKnALiyNg6NppxGa363rPi1UWq73otTe2dOfZTPIac3OdWu9b2V+XlTR86ZKmUcylaCVG/18O/yz/lTAAA8IXEKgKvaItDqlNTq9NTe5NQsUO2dPzVa6yvl2OTUkfOmVs6ZKp3r9jn0zM7C+ru5354bAABPRpwC4Kp6k1KrsWrvLKqVFb/eet8oTs0ORC+d+00vTu1NT/XCVH3e1OyMqaOrfO2kVLvG91q+BirrfQAAT0acAuCKRit1R0LVkRW/2RTVbHpq5byp1bW+vcmpn+VznKrDVG+d7x6rfLM1vjZKbdf+NgEAeDL+AATgaj5K/yDyWZBqH++FpltW/EaHov+OA9G3d8PbPq5/1uwg9NK5bn9u7/msrvFt9z+b67fJ9wcA4ILEKQCupp1O6gWqW9f8Vlf8Rqt9bZzqrfatnDtVH4Y+W+nbok8dp36Wrweh771DX+vWNb42TG03f58AADwRf/wBcCXb1FQbg9oodWbNbzQttfIufrOD0NsJqtmh6NvvOFvra0NQPTFVx6m3yc86erbU6hpfG6N6Yape9QMA4AmIUwBcyejg8d4E0yxM9db8elGqt+I3Ohi9F6dmq32jMFVbWesbxaleEBsdxN4zi1OzNb5ZmPpZPSZOAQA8CXEKgKtoD/SeharRat+ZNb/ZOVSjVcJZnFpZ56uNAtUWibZ1vm2175aftzc1Va/xjc6XmoWp+jm2E2IAAFyUOAXAVRwNUnvnUB15N79emJpNTvXesW/lzKfRoeTt2VNbINq+d+8w9DNnWx1d4zsaptqbv1MAAJ6AP/oAuIqV2HE2UPXW/PYOSB+dbzULU721utU4td1vt3qNr41TKwef96aWjqzxjULUapjapqhMTwEAXJw4BcAVHF3n2wtWt6z4jc6uatf6ehNMe3FqpDc59VJ+rcZt3/t18LPOHIA+W+NbOVNqdqtfm7eF3x8AgAcmTgFwBaOwMQpWo4jVBqrVNb/etNQoUK0cgH7LmVPb9Rai2kh16zvzvXbu2xi1cqbU7NZ+PQAAFyZOAfDoVg5C34tVKyt/szDVi1m9QDV7p7xZlNqbnppNTW2Rqp6aKuV8mDr7Lnyrt95r5J37AAAuTJwC4NG10elIiDoaqHqxavbY381/uxqmZmdN9czesa+emqojz0qYar9nPS1162Hne+FQnAIAeBLiFACPbhSmzkxN7QWqvRg1emw0EbQXpvYC1Uv1uTpE9W7b162+M9923Tv0fBSijp4ntffvtF0DAHBh4hQAj6xd6Vs5x2jlTKqVaLX3Dn9tmGp/9mxqqnTuR79/KV8npEZxavTOdz+rz/fC1EsZr/HVt7cyf53b13pv6s30FADAExCnAHhkswByZN1vJaTMpqpGn5v9/JWzplbV/90oUJXSj1MvzXW9wre3xte7bQeer05NrQYqcQoA4KLEKQAe2ZHJqdkE1ZHJqZUotbfOtzc1detr0vNexpNT5d/P/Sxfz5eq49Q9zpOa/dt8TK4BALgocQqAR7YSNlZi1S1rfitRphdh6kBVyv0DzPYz2mmo987X1l/zs3yOUe/VY/Xa3srvfmZ9bxbyZnENAIAHJU4B8KjqaHFroDoaq45MZfUmpNrHvlP9/bf1uDpQvZRfU1WjQNVGqlumo45OTdWPiVMAABckTgHwqNqpmr2wsRKtViZ/9iarRj93FKp+h16gqkNZKftxajtLaiXIzQLVmX+r7d65UwAAFyROAfCo2nDRTiXdOkm1Gqraz80mpNrHfqeP5npveqr3O7arfbdMTM0i1OjfEgCACxKnAHhUvcPER6FjFEFWo9XeKt9KgKkDy9l34+s5surW/vz6dXspn1/LOkad+d1XItTs36v372u1DwDggsQpAB7RXojam1q6V7TqhZi9yakjYWrla7evORup2ufbHp7+s8wD1S0RajUstpNeAABciDgFwKMaTdaciR6jx2YTUCtf0wap1Th1ZoVtNVK1632j6amV6DR7HVano2ZTUianAACegINFAXhER2LGPSLWe/N1oxizd1v93W5xJH6tvi5Hw96Z6LR6AwDgYsQpAB7RkbhyNEStBqre47OgshJX7hVfVgPVd4epewar1d8LAIAHI04B8IhG0eI7JnV6UetoSElEldWf2Qto7WO/aypq5d8VAICLEacAeFR7QaX3uTNBay/EvJev4eTM1M/vDi97z/newe6Wf5P6cwAAXIw4BcAjaiNG+9jsc2ejydHo0nu+e7/Pva18395zPvKarHxd72t6n2sfG30OAIALEacAeFSjmLLd3xpAjkSovWDzKPZeh/bjewan2de31wAAXIg4BcAjmsWLo4/NJoZGn1/9Pn+6IxNde6/JLcHpyGOP8toCALBInALg0fVixdHHVgLUanD506w8t9nEVxuEVkPS3nO45TEAAC5EnALgUX1nBLl3ELlKYDkSoUQqAACWiFMAAAAAxIhTADyqlzs/duTzR937+6W8DK5nj+19/l7/ZgAAPChxCoBHd6/g8bLz2CzM/MnhZOW51b9f7370u++9ZrPnIEoBAFBKEacAeExHw8YtQaV9rI02jxxURs9zNeTV1/d6bO85PMprCwDAInEKgEc2m/JZiSGjr997bBauZj//T9ebCJv97re8nu3PXP16AAAuRpwC4BGtrqH1ppxWPze6zT5fP79S1qPKd8WXIyt9Z1+Pla8vBz/XPtZ+DgCACxGnAHhUKyFjJY7c69Z7TqN49aeYTUrd+nrN/tsy+Zr2c6X5HAAAFyNOAfCIbokfe7fXxc+397fGqHvHl73vN4tD9eMrv+fs9TkSsnrPp/c1AABciDgFwCM6M61zNlLNIstemDoaq+4VX1a/z9nX4nXyuXvcZs8NAICLEacAeETfFaBm96+dj49EmVLd7/1ut1j9Gauvy95rcuR1vEekAgDgYsQpAB7VrbFp5b+Z3Xpf03suZ6Z/zkaYI//dXpgavQaz1+aW13o1agEAcDE/0k8AAE6455TULEjNpqZGX/tR3Xph5ePA77j69feIUr3X6+jvPgpV95qmAgDggsQpAB7V0ZW81Qi1MjXV3t5KKe//3trAU0eqUt0fjVT3cCRG9X7Hlc/tvZYr/zazewAALkacAuBRrU5H7T12JMi8lVJ+dq7r20dze62e8xavNquB6h5Gq3ujj+vfeRamVm+r/x6z6SoAAC5InALgUd0zQu0FqVGgqSemtlsbp9r1vtd/v27zOwLV2Ump0e88e12Ohqy9QGVqCgDg4sQpAB5VG1n2wtQsyIyC1CjYbCGqnqJqI1U9RVVrp6dK52vuaTRdNntt3ia3UYA6Gqr2/p16jwEAcEHiFACP6tZzovYC1SzS9KJUfb3Fpl6cKtXn3juP3Ut9xlVv7XH0uvVi1MprshL3jt7a5woAwAWJUwA8sluno1bDS+/WW+cbrfZt6jOo2umq9utuMTv0fBSktvsfB1+HvVB1r6kqAAAuSpwC4JHdOh11Nr78KP1zpmZhatOeN1V//FLOR6r23QBXDop/K/3XYhSofkw+dzRWHf23AwDgosQpAB5Z76ykI9NReyFrFqbaNb7RYegz7frf9vEWqdpYNXsd2vuVian69xyFpx/N/d501cpreXSqzUofAMCFiVMAPLqVaanVaNKu7Y2mpbbrXoxaDVMv5dfh6O33KtX1S/Vx73u010fX+eowVQeqXoxaDVRHg9VeUBSnAAAuTJwC4NGNVvWOhKjZdFT78Xb9o5ybmNqC0bbO91G+BqpSvn6flTi1cgh6+5q0YaoXqPY+f+a2GhHfuq8iAACXIU4B8Oheyu0xaiVM1dNSo3OmShlHpJfyz7v8tVNN2/fYrnsTVKVzvX3f9n40OVVHqu31GoWpOkD9VcbBqrcOeDZYjf79TE0BAFycOAXAFYxi1C3nSfXCVDs1VcepUvpTU+1EUxuo2jBVT1TNglf9/dv7emKqjVO933l0+2vyudma30qg2vv3sNIHAPAkxCkAruBohGrPkxqt7vWC1Owd+fbC1BZb6kD1Wj5PZNWHoPdiV30O1ejnzFb62t95L1Ct3o4EqtWD6a30AQA8AXEKgCvoTQTNbqPDzo+u8c3OmBqFqe36Z/kVwLYg9brwvet39Bv9rDZMzV6fUaD6q8wnp2Zrfreu+FnpAwB4IuIUAFexGjxWDzufTUuVsr9qNzoHqp4Mei+fI9UoTK2u9c1+1t5rUgeqv8rXSLUXq3rf59ZABQDAExCnALiK+rDvW8NUHaTaM6ZWwtFoxa69bT/7bKDqnTc1WuVr73tTTm2cWpmeOjJVdWSi6rXz+wIAcEHiFABXsoWeI2Gqt743C1KzVb5SPgepv8s4TNVRals1rAPV6rsAtj+zfWe+WZwarfX91bnvhapRuBqt+x05kwoAgCchTgFwJa/la4QaTUjN3oVvduj5yOp6XT01td3PwlTv0PXeeVN7P+9MnOpNT81W/EYrfaMoNQtUAAA8CXEKgKuZTUytrPHNDj3vxaLeal0biv4ua3Fq78D11XcDnE1r9db6Zut591jxG01VjQIVAABPRJwC4Grqd6abHXY+W+ObxanZz+1NL/1d+oGqt843ClRl8LNnQWx27tTe5NSZFb8za369QGWlDwDgyYhTAFzRXphaWeNbWefbm5ZqJ6d+NrfVlb6ja30rk1O9OLQap3orfmfW/HqfAwDgyYhTAFxRb3rqzBrf5ugqX29yqg1TdZz62TyP0WHovbW+3s9fPRC9DVRtLBodgKFZrPQAAAuoSURBVH6vFb82TJmaAgB4QuIUAFe1RY9e9Dmyxndkla8XpWZhqjc9NToM/Za1vtV36zty/tTeit/RNT9TUwAAT0qcAuDKzqzx3bLKt7rON4tTt5451Ttv6si5U0fPnxqt+B1d86tXFQEAeCLiFABX9lLOrfGVzmMrq3y9d+jrhal2pe/omVN7z2MUqEZxanYO1GgS6syK3+jrrPMBADwxcQqAq9veEe8ea3ylnDtnavv4rYwPQ/+OyanV1b5bz59aWfGbnTkFAMATE6cAeAZ1mFqNUqOpqVEQGp0zVYeq98790Xfrax15t74j01Nnzp+arfiNopV1PgCAJydOAfAM6vW+veCzuspXB5/eOVO9UDWLU0ffrW/03F7LPFDN4tSZ86fOrvgJUwAAlFLEKQCex2uZT1DNHD1nqnfe1Hs5t9J35t366jDVW+2bxalbz59aXfGzzgcAQClFnALgubyV+63yzc6Z6h2EfnSl7716TntrffVz25ucWpmeWl3tO7vi5+8PAAD+wx+HADyb3v/7Vtbl6us2Sm0ft2dMrcap0Urf6J36es919R37enFqu64PRd+bnDq74mdiCgCAT8QpAJ7Rtt43c+Scqb0wNZuYaiPVaOVw9HzbeNZOTvUC1ShOzc6dOnP+VG/FzzlTAAB8Ik4B8Ky2/weeWeVbPWfqSJxaOW9q9bD2Xph6KZ/PnJrFqVmYOnP+lAPQAQAYEqcAeFbbO/ht1+39kXOm9sJUHaVWzps6eyB6KWuTU+301Oj8qdXJqZVQJUwBANAlTgHwzOpAtX283e9FqXZyaotQ28dvZTw1tV1/lGOTU6PfofecXzv3Z+PUkUDVi1PCFAAAQ+IUAM/upXyd7FmJUrMwVQepW1b6bjkQvX3u9fVb53rvXftWD0hvD0EXpgAAmBKnAOAfW0jZu9VRqj0IvQ5Vq3Fq9k59pXwNVK2z79i3GqduOX8KAAB2+cMRAH55+/f+yDlTo/Om2jjVC1SzMDVa69uuZ5Neq4FqZbXvzPlT2+sIAAC7xCkA+OytrJ8zNTsIvRenZmt9s8mp+r525h37zsap1UD1On5pAQDgK3EKAL56Lf+sp+2dMzV7h773zv3Z86bOrvWdXe2rr+szp/ZW+5wvBQDAYeIUAPRt7+RXT0tt1+3h52fiVPtOfW2kKuXreVOzA9G3+yPv2LcSp1beuc8aHwAAp4lTADBXv7PdapgaRant4zZM/a7JqV6gat+5b7bi11vrMy0FAMBNxCkA2LdNUe2dM7UXp/ZW+vYmp7bH2iD0UvqR6rW5bs+easPUyvlT9QQVAADcTJwCgHXbWVRv5fME1Wv12Op5U0ffqW92IPp2fa9zp/YORzctBQDA3YhTAHDMS/k8cTR6d746StXXs5W+7bqUc2t92/1ocmr13KlRnBKlAAC4O3EKAM7ZVv22ialZnLrH5FR7vT2H9n5lcqqNVKP1PlEKAIBvJ04BwG22SPVRPoeqlZW+2UHoZw9Er6+PTE71pqdEKQAAvp04BQD3sa37vZV5nOqFqV6kKs11qR7rHYi+3Z95x746TtWRCwAAvp04BQD3V8eeOkbN3qlvNjlVSn96anQg+vbxaHKqF6gEKQAAIsQpAPg+dRhqp6Rufae++me096PJqfbcKUEKAIA4cQoAfo86DpWyPzF1y4Ho23UdocQoAAD+SOIUAGSMYtE9JqfqewAA+KOJUwDwZzHhBADAU3nd/xIAAAAA+B7iFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx4hQAAAAAMeIUAAAAADHiFAAAAAAx/9+OHRMAAMIADNuBfyf424mMHiQKetecAgAAACBjTgEAAACQMacAAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDGnAIAAAAgY04BAAAAkDGnAAAAAMiYUwAAAABkzCkAAAAAMuYUAAAAABlzCgAAAICMOQUAAABAxpwCAAAAIGNOAQAAAJAxpwAAAADImFMAAAAAZMwpAAAAADLmFAAAAAAZcwoAAACAjDkFAAAAQMacAgAAACBjTgEAAACQMacAAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDGnAIAAAAgY04BAAAAkDGnAAAAAMiYUwAAAABkzCkAAAAAMuYUAAAAABlzCgAAAICMOQUAAABAxpwCAAAAIGNOAQAAAJAxpwAAAADImFMAAAAAZMwpAAAAADLmFAAAAAAZcwoAAACAjDkFAAAAQMacAgAAACBjTgEAAACQMacAAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDGnAIAAAAgY04BAAAAkDGnAAAAAMiYUwAAAABkzCkAAAAAMuYUAAAAABlzCgAAAICMOQUAAABAxpwCAAAAIGNOAQAAAJAxpwAAAADImFMAAAAAZMwpAAAAADLmFAAAAAAZcwoAAACAjDkFAAAAQMacAgAAACBjTgEAAACQMacAAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDmzMytIwAAAAD40j4p0IW5XD+92gAAAABJRU5ErkJggg=="/> -<mask id="mask5"> - <g filter="url(#alpha)"> -<use xlink:href="#image32" transform="matrix(1,0,0,1,-415,-458)"/> - </g> -</mask> -<clipPath id="clip26"> - <rect x="0" y="0" width="159" height="160"/> -</clipPath> -<g id="surface31" clip-path="url(#clip26)"> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 70.269531 158.367188 L 88.882812 158.367188 L 87.8125 116.570312 C 87.8125 116.339844 87.804688 116.109375 87.792969 115.859375 C 87.789062 115.765625 87.78125 115.679688 87.773438 115.605469 C 87.761719 115.390625 87.753906 115.207031 87.734375 115.003906 L 87.589844 113.523438 C 87.566406 113.320312 87.546875 113.109375 87.519531 112.894531 L 87.359375 111.613281 C 87.34375 111.492188 87.328125 111.355469 87.3125 111.214844 L 87.136719 109.949219 C 86.917969 108.355469 86.667969 106.628906 86.402344 104.820312 C 86.074219 102.570312 85.726562 100.199219 85.410156 97.84375 L 85.195312 96.140625 L 85.195312 93.1875 L 87.980469 96.808594 C 89.441406 98.726562 90.894531 100.675781 92.273438 102.523438 C 93.339844 103.953125 94.355469 105.320312 95.296875 106.5625 L 96.855469 108.585938 C 96.9375 108.695312 97.023438 108.800781 97.105469 108.90625 L 97.566406 109.484375 C 97.652344 109.59375 97.734375 109.695312 97.816406 109.792969 C 97.957031 109.96875 98.09375 110.132812 98.226562 110.292969 L 98.460938 110.566406 C 98.589844 110.722656 98.710938 110.863281 98.832031 111 L 99.015625 111.207031 C 99.1875 111.394531 99.347656 111.566406 99.492188 111.714844 L 128.304688 142.039062 L 141.46875 128.875 L 111.15625 100.078125 C 110.992188 99.914062 110.824219 99.757812 110.636719 99.589844 L 110.433594 99.410156 C 110.289062 99.28125 110.148438 99.160156 109.996094 99.027344 L 108.914062 98.136719 C 108.722656 97.984375 108.535156 97.832031 108.335938 97.675781 L 107.035156 96.667969 C 106.738281 96.4375 106.433594 96.203125 106.121094 95.96875 L 106 95.875 C 104.742188 94.921875 103.351562 93.886719 101.898438 92.800781 C 99.617188 91.101562 97.253906 89.34375 94.949219 87.566406 L 92.617188 85.765625 L 95.695312 85.773438 L 97.21875 85.976562 C 99.648438 86.304688 102.058594 86.65625 104.339844 86.988281 C 106.117188 87.246094 107.816406 87.496094 109.367188 87.707031 L 113.386719 88.207031 C 113.664062 88.238281 113.871094 88.257812 114.070312 88.273438 L 114.433594 88.304688 C 114.640625 88.320312 114.828125 88.335938 115.011719 88.347656 L 115.28125 88.363281 C 115.535156 88.375 115.769531 88.382812 115.976562 88.382812 L 157.796875 89.453125 L 157.796875 70.839844 L 116.003906 71.910156 C 115.769531 71.910156 115.535156 71.917969 115.289062 71.929688 L 115.003906 71.949219 C 114.808594 71.960938 114.628906 71.972656 114.4375 71.988281 L 114.121094 72.015625 C 113.871094 72.039062 113.664062 72.058594 113.445312 72.082031 L 110.671875 72.410156 C 110.3125 72.457031 109.941406 72.507812 109.566406 72.558594 L 109.359375 72.585938 C 107.796875 72.800781 106.082031 73.050781 104.285156 73.3125 C 102.019531 73.644531 99.628906 73.996094 97.257812 74.3125 L 97.191406 74.320312 C 96.714844 74.386719 96.203125 74.453125 95.691406 74.519531 L 95.269531 73.132812 L 94.957031 72.726562 C 94.957031 72.726562 94.960938 72.722656 94.964844 72.722656 L 94.964844 72.71875 L 94.976562 72.710938 C 95.371094 72.40625 95.765625 72.101562 96.167969 71.796875 L 96.230469 71.75 C 98.09375 70.328125 100.007812 68.902344 101.828125 67.546875 C 103.304688 66.445312 104.714844 65.394531 105.988281 64.425781 L 107.332031 63.398438 C 107.5625 63.222656 107.789062 63.046875 108.003906 62.875 L 108.335938 62.617188 C 108.542969 62.457031 108.726562 62.308594 108.910156 62.160156 C 109.019531 62.074219 109.121094 61.988281 109.21875 61.910156 L 109.996094 61.265625 C 110.148438 61.136719 110.289062 61.015625 110.421875 60.894531 L 110.632812 60.707031 C 110.824219 60.535156 110.992188 60.378906 111.136719 60.234375 L 141.46875 31.417969 L 128.304688 18.253906 L 99.507812 48.566406 C 99.347656 48.726562 99.1875 48.898438 99.015625 49.085938 L 98.828125 49.300781 C 98.710938 49.433594 98.589844 49.570312 98.460938 49.726562 L 98.21875 50.015625 C 98.089844 50.164062 97.957031 50.324219 97.820312 50.492188 L 96.761719 51.824219 C 96.617188 52.007812 96.472656 52.199219 96.320312 52.394531 L 95.328125 53.6875 C 94.371094 54.957031 93.355469 56.3125 92.296875 57.734375 C 90.96875 59.519531 89.566406 61.402344 88.152344 63.257812 L 88.152344 63.378906 L 87.878906 63.613281 C 87.585938 64.003906 87.289062 64.390625 86.992188 64.773438 L 84.734375 67.691406 L 85.203125 64.03125 C 85.265625 63.539062 85.332031 63.042969 85.398438 62.550781 L 85.40625 62.472656 C 85.730469 60.097656 86.078125 57.703125 86.410156 55.4375 C 86.671875 53.636719 86.921875 51.917969 87.136719 50.351562 L 87.351562 48.734375 C 87.398438 48.386719 87.433594 48.101562 87.46875 47.824219 C 87.488281 47.683594 87.503906 47.550781 87.515625 47.421875 L 87.734375 45.285156 C 87.753906 45.085938 87.761719 44.902344 87.773438 44.71875 C 87.78125 44.613281 87.789062 44.53125 87.792969 44.457031 C 87.804688 44.1875 87.8125 43.953125 87.8125 43.746094 L 88.882812 1.925781 L 70.269531 1.925781 L 71.339844 43.722656 C 71.339844 43.953125 71.347656 44.1875 71.359375 44.441406 L 71.378906 44.71875 C 71.386719 44.898438 71.402344 45.085938 71.417969 45.289062 L 71.449219 45.648438 C 71.46875 45.851562 71.484375 46.058594 71.507812 46.273438 L 71.554688 46.691406 C 71.578125 46.925781 71.605469 47.160156 71.632812 47.40625 L 71.839844 49.046875 C 71.890625 49.425781 71.9375 49.808594 71.992188 50.203125 L 72.015625 50.351562 C 72.226562 51.890625 72.472656 53.589844 72.734375 55.371094 C 73.148438 58.203125 73.574219 61.128906 73.949219 64.03125 L 72.117188 64.71875 C 71.847656 64.367188 71.539062 63.964844 71.230469 63.5625 L 71.179688 63.496094 C 69.738281 61.605469 68.296875 59.671875 66.929688 57.835938 C 65.84375 56.382812 64.808594 54.988281 63.851562 53.730469 L 60.699219 49.730469 C 60.5625 49.574219 60.4375 49.429688 60.316406 49.289062 L 60.136719 49.089844 C 59.964844 48.898438 59.804688 48.730469 59.660156 48.585938 L 30.847656 18.253906 L 17.683594 31.417969 L 47.996094 60.214844 C 48.160156 60.378906 48.332031 60.539062 48.515625 60.707031 L 48.722656 60.886719 C 48.859375 61.011719 49.003906 61.136719 49.160156 61.269531 L 49.433594 61.5 C 49.59375 61.632812 49.753906 61.765625 49.925781 61.902344 L 51.148438 62.878906 C 51.363281 63.046875 51.585938 63.21875 51.8125 63.394531 L 53.164062 64.429688 C 54.421875 65.382812 55.8125 66.417969 57.265625 67.5 C 59.105469 68.871094 61.046875 70.316406 62.953125 71.769531 L 63.007812 71.816406 C 63.410156 72.121094 63.804688 72.425781 64.203125 72.730469 L 66.539062 74.527344 L 63.457031 74.519531 L 61.933594 74.320312 C 59.511719 73.992188 57.113281 73.644531 54.84375 73.3125 C 53.054688 73.050781 51.347656 72.800781 49.785156 72.585938 L 49.585938 72.558594 C 49.210938 72.507812 48.839844 72.457031 48.480469 72.410156 L 46.839844 72.207031 C 46.589844 72.175781 46.351562 72.148438 46.121094 72.125 L 45.703125 72.082031 C 45.488281 72.058594 45.28125 72.039062 45.078125 72.019531 L 44.71875 71.988281 C 44.511719 71.972656 44.328125 71.960938 44.140625 71.949219 L 43.863281 71.929688 C 43.617188 71.917969 43.382812 71.910156 43.175781 71.910156 L 1.355469 70.839844 L 1.355469 89.453125 L 43.152344 88.382812 C 43.382812 88.382812 43.617188 88.375 43.867188 88.363281 L 44.140625 88.347656 C 44.335938 88.332031 44.515625 88.324219 44.714844 88.304688 L 47.214844 88.042969 L 48.480469 87.882812 C 48.851562 87.835938 49.238281 87.785156 49.632812 87.726562 L 49.707031 87.71875 C 51.324219 87.496094 53.03125 87.246094 54.820312 86.988281 C 57.644531 86.574219 60.566406 86.148438 63.460938 85.773438 L 63.886719 87.164062 L 64.195312 87.566406 C 63.796875 87.875 63.394531 88.183594 62.988281 88.496094 L 62.933594 88.535156 C 61.023438 89.992188 59.078125 91.445312 57.230469 92.820312 C 55.789062 93.894531 54.410156 94.921875 53.160156 95.867188 L 53.007812 95.984375 C 52.710938 96.210938 52.410156 96.4375 52.121094 96.660156 L 50.808594 97.683594 C 50.613281 97.835938 50.425781 97.984375 50.246094 98.132812 L 49.917969 98.394531 C 49.753906 98.53125 49.59375 98.660156 49.441406 98.792969 L 49.15625 99.027344 C 49.015625 99.144531 48.890625 99.257812 48.769531 99.367188 L 48.507812 99.59375 C 48.332031 99.753906 48.160156 99.914062 48.015625 100.0625 L 17.683594 128.875 L 30.847656 142.039062 L 59.644531 111.726562 C 59.808594 111.5625 59.964844 111.394531 60.132812 111.207031 L 60.335938 110.976562 C 60.453125 110.847656 60.570312 110.710938 60.699219 110.566406 L 60.933594 110.28125 C 61.058594 110.132812 61.191406 109.972656 61.332031 109.800781 L 62.300781 108.582031 C 62.476562 108.363281 62.648438 108.140625 62.824219 107.910156 L 63.046875 107.617188 C 63.28125 107.316406 63.5 107.027344 63.722656 106.734375 L 63.835938 106.585938 C 64.808594 105.304688 65.832031 103.933594 66.898438 102.5 C 68.277344 100.652344 69.730469 98.699219 71.191406 96.78125 C 71.515625 96.359375 71.808594 95.976562 72.097656 95.601562 L 72.128906 95.542969 L 74.324219 93.34375 L 73.949219 96.261719 C 73.578125 99.148438 73.152344 102.0625 72.742188 104.878906 C 72.480469 106.667969 72.230469 108.375 72.015625 109.9375 L 71.988281 110.132812 C 71.933594 110.515625 71.886719 110.882812 71.839844 111.242188 L 71.511719 114.007812 C 71.484375 114.238281 71.46875 114.441406 71.449219 114.644531 L 71.417969 115.007812 C 71.402344 115.210938 71.386719 115.398438 71.378906 115.585938 L 71.359375 115.867188 C 71.347656 116.105469 71.339844 116.339844 71.339844 116.546875 Z M 70.269531 158.367188 "/> -</g> -<image id="image42" width="1191" height="842" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABKcAAANKCAYAAABf/S2vAAAABmJLR0QA/wD/AP+gvaeTAAAgAElEQVR4nOzdW3LbSLil0ZRdNY+eSM9/Fj2RU7b6oYzjdDpvACXvIrhWBAMwqeJND6X44s/EWynl/xUAAAAA+PP+71sp5T39LgAAAAB4Sf/nS/odAAAAAPC6xCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGLEKQAAAABixCkAAAAAYsQpAAAAAGL+Sr8BAOB/vS+OI2/V8a25DwAA/tPEKQDIeO/cvnceq3++p41Rb53bl/JruAIAgP8McQoA/ow6QPWOvVsp40D11jnvhak2ULVHAACIEqcA4PPUQaq9tWGqPi+d85Ezk1P1sb0JVQAARIhTAPDx6gj1rfSj1NnJqV6gaqen2j2n6uV8szAlVAEAECNOAcDHqKPTt9IPU7uTU73pqZnVsr42TPUi1dfOUaQCAODTiVMA8JgjLH2rbqswtTs5dXZZX3t+ZnLqa3P+tbkPAAA+hTgFANf0otQoTq2W9o0iVSm/hqk2Uu3sN3VmSV89NfW1+fdfxSQVAACfQJwCgPN6QWoUp0YTVL1AdWVyqpT+1FRvcqoXqdqJqTZOfa3e5/FvkQoAgA8jTgHAviM2/VP24tTu0r6dyalRoFpNTa32mhrtNzW7/fXjCAAADxOnAGDtvfwepXYD1Wpp3+6+U6U5b6/UdxxXk1NnlvSNwtT38jNQ2Y8KAICHiFMAMPe9/Buijhi1G6jOLO2bBapS5sv6Hpmc2lnSNwpTdaDy9wQAAJf5YxIAxtoo1QaqR+NUL0wd56WsA9VqQ/QvnePVJX1HkDr2oKoD1XuxYToAABeJUwDwu/fyM0bVt16YGgWq3r5T9Xm7Ifru5NR7+T0C1UGqlN+X882W9e3GqSNMHVGqDlPH+/+7WOYHAMBJ4hQA/KpexjcLVKvlfW2curK0r3SOrdmyvtnkVLukbxanesv56rjWTlD5+wIAgG3+eASAn+rw9D9lHKV295+6urRvZ0P0Un4PU8f57uTUztRUL0z1Jqfa9/u1WOYHAMAGcQoA/vWt/Bqk2ji12n+qF6g+YmlfKeOpqVL2J6euxKl2n6l6v6nV1QXtQwUAwBZxCgB+D1KjMLXaf6o9v7K078wV+2Ybordhqj7fWdJXh6nZcr76M7Tv9738uw+VQAUAwJA4BcCr60WpnTg1mp7auXrfmUBVOsdSfg0+vampM0v62vNRmJot5yud84NABQDAkDgFwCtrg9QoTvVi1Wx5306cOs7bpX2rfadao2V9Z5b01VfkO26zjdBXt9I5ClQAAHSJUwC8qlWY2p2iGi3vGwWq3gTVKlCV0g9Us/2mVkv6Zpug17Gqt8/UzvvsTXkJVAAA/EacAuAVtVfleyROnQlUO0v76vhTyu+TU8f5bL+pUZiaTU3NrtLX7jM1Wta38lZskg4AQEOcAuDVHFfla6PUaoKqt8TvWxlHqt2lfW34OTM5VcrPGHWcfym/B6qzcWp3n6mze2Md/h58FgAAXpA4BcAr+V76k0+96alenBpNUe3uP/W9c+xtiF5HqtI5HmbL+trJqfYKfaM4dXafqdn7a99rffM3CAAApRR/GALwOt7LOEqN7vuM/admS/tW+zkdn6O9Ut9xbDdCb6endvabaveZGi09PDPVNbp9Hfx3AAC8EHEKgFcxi1K9ZX47S/yuBKrVVft6gaqUcwFodqW+M1NTq8mpkdGeUqP3CgDACxOnAHgFq6A0m6g6s8SvXd7XC1S7S/t2JpR6y/pGk1Orqakzy/lK59izmpx6K67gBwDw8sQpAO7uiECzqLS7QfrO/lOr6anvzfnu0r5Db1nf7uTUEaLaOHUEqXo5X71R+5k4tYpRvQ3b/T0CAPDC/DEIwJ319plqr7C3ilVXl/jtBKrdpX2l9CeUVpNTO0v66jDVRqnR9FT7fmaTT6s4Vb9XAABekDgFwJ21S+vOLu8bharVEr/V8r6zS/tK2Y9TZ5f01WFqFqVG76F9P7uTU19+fCfH0fI+AIAXJU4BcFftcr4rMWq1zG+1xK+NUsf5bGnfzj5Ppfwapo7jaNncaEnfaDnfmX2mzgSp1dUE/V0CAPCC/BEIwF2NwtCVKarZMr8z+08d5704tbvHU2sWhOrbt/JrnJot5+u9j9l7WL2n0bRUfaxvAAC8EHEKgDuq93ca7QHVW4K32kdqZ/+p/xk897fObbQher20r5RfA1Fttqyvt7TvCFTHa62W881eu/e6o0mpf5r30oapb+XXSAUAwAsRpwC4m/cy3/OpF45m942W+Z3ZJH2259QsUJ1d1jda0ldPTh3HOkztbnxev/YojM2W8dUBqhem6ukuAABehDgFwN3US+ZGS/t2l/mNYtMqUo0C1WpD9NHSvlLOTS/1rtT35cfzHsd6WuqRpXyrKwbOlvG1Yeq4iVMAAC9EnALgTo6pqfbWRqkr+07t7kXVPn72an1nJ6eO89WSvlGc6l0dcBanHtn0vA1RvTBlegoA4MWIUwDcSTs1NQpUVzdL7y3xm22S3nvdM/tO7UwvrfadaqPUsedU/Rqr16xDWO+1VtNSvRA1ClOmpwAAXow4BcCdzCLQmUB1drP02fTUKlCNpphK2dv7qT32AlVvI/R2CWH7ej2zqanVpudnwpTpKQCAFyJOAXAXqzC1E6zOLvmbTU/1nm8Up3YmmGZ7Th3H9nZMTLVL+dogdjz/bPPzRzc9PxOmTE8BALwQcQqAu+iFlzOBarXMrw1Wvf2lRlNTvUC1s+9T6Rxbszj1Vj13L1DtTkzVr3V1Gd+ZMFW/v3ZJIQAANyNOAXAHo43Qzwar1TK/K7de/OpFolWcas9LWS/rO+LUcZzdenanpXbC1LfBfb3vo37M3yoAADfnDz4A7qANHFeX+K2W+Y1iVW8pXy9SjYJZu6yvlHGgaq0C1SpOzZzZ9HwVpo59pHaCYf0YAAA3J04BcAdt5Li6vK8XqHamqHohq/czozB1dUnfYTU59aXz/Fc2P9+ZltrdT2oWpNr77D0FAHBj4hQAz64OGqModXUfqvbfsymqWaD6VvrvcRSmzsSpt+rxOiSV8jMmHYHqeK6zE1OPTEvtxKjZ70qcAgC4OXEKgGc3ugrdahrn7B5UvSmq2XRV+zNnpqZ2J6bqn6mX8NVX66tjVR2pRnbDVP25d8PU7u+j9z3ZGB0A4KbEKQCe3W6YGgWpWdxaRaudQNV7H++d45XlfLV6b6nZ8r6ZUZjqLePbCVNnpqNmN3EKAODGxCkAntkRd2a3M6GqF1bOTFX1nqO3pG82MXUmSPW+j/q8jUzfJ//t24/PNApT7X5SbZhaBb4zv5PezdI+AICbEqcAeGa9JXKPhKrVxNROvJptft6+z4+KUq3ecx1L/HrqIPWtjMNUHaPaK+/tLOPbCVLt93OcAwBwU+IUAM9sFKVGgWMVrnanqs7srdSGqM8OU7VRpGpdiVO7+0rtRrqdiGdpHwDADYlTADyrOva0x53g8Uio2p0WmkWqzw5Thzrs1FfsOyJVu3H6EajqIFVPST0SpHa+m9F94hQAwE2JUwA8s16Y6gWq3Sh1NlTtBpf61t73pxyv/aU6tpGqvdJf/XnrSPUZQWr2e6vfOwAANyNOAfCsZhFjJ3RcDVW7Aav3Wn9yYmqkvhJgG81601Pt1NQoUJ2Zipr93noh7zgCAHBD4hQAz2p0pbtV4FhFrCthahSgesHlbJja+dnd5W7t1fzqqane99YGqatTUbNJqd1QJU4BANyUOAXAszoTOXYD1U5UmYWsVZD66CjV/uxOpHrvHI+pqeNYL/lbfS878enK72R0s+8UAMDN2LsBgGe1Ozn16CTVKkpdCSw7n+2KMwHs7Hc0i1SPxKbVezA9BQBwc+IUAM+oDhVnY9QjEWsWbVbPV6rj6rM9avUcO9/f6nN/xPHKDQCAmxGnAHhWvfAzi0JXg9QoppwJLaU6rj7Tn/TenF+Jex8ZnnZ+bwAA3Iw4BcAzGsWL3n1XItVOwFpFqDb87H6mj7J6vtV3NPqZj4hPo+de/V4BALghcQqAZzUKQqP7rkaqM5NR/7W4svuao6D20bfe87avv3MfAAA3Ik4B8Ix6U0mPxI6d+87GrdH7XX2eP20W89p/73z+0jl/9L72HACAGxGnAHhWo3AxCxtXo9ZqQmrnPT6DUVRbffarj49eZ+dxAABuQpwC4JmtJmuuBpBRDGl/fncSKGknns1i3Ojx3nP0XvMzHgcA4EbEKQCe0ZUotfP46mdHP3Nns+9nNyztPPeVxwEAuAFxCoBXciZ27ESZq899B1c/ryAFAMAvxCkAXsnbxZ9t/7szz3NXn/Ud+G4BAF6MOAXAs1vFjDOPPxJGXiGqjCLd6DvsfSdnvu9X+E4BAF6eOAXAM9qNHqtosvN4e//osd7j/wU776P3uXa+g9HrnPmuV//97D4AAG5AnALgmc3Cxyo27T4+izXtY73391+OKmenynr/nn037fNc/Z3svl8AAJ6QOAXAM5pFpPq+9vHez40Cy+40Ufvzo4mq1edJmX2O0f2PfoePBCwAAG5GnALgWe0EqlEMeTRK7YSW2eN/yu5rjt7j2TB1Jkid/R2c+TwAADwRcQqAZzQLF2ciyuyxL4t/j+67GlL+dHg5832MPufo/t1b7/VK57H6PgAAbkacAuBZPRKezsSVUaRqj7vhZeczfYSzr7f6/Luf+0q4mr1++xgAADcjTgHwjB6Z1rkSYHaPOwFm57M96szrPPr9XPkuroSs3c8FAMCTEacAeFZ/Kjh9KfMIUz+2E1x2P9sVu6+xE6Z2Pnf7c58R9a58fwAAPBFxCoBndTZqXJkIaoPL6PHe7dHpn7Mx5uzz7n4fs+9idf9HhKn65wEAuKG/0m8AAC76iGmoVYg5bl8Xj8+mp96b+w7vJz5n7795dLpqNOV1JsDNvoedQHUlXgEAcDPiFADP6swU1GoaaufWC1RfSynfmvvem1sdqL6UUr5Xn2E3UNWf+RHt97XzXXwt+3FuN1rtxqv2CADADYlTADyr1dTPLIRcDVJHqPnW3Pe9ur3/ONaTU19+vOfj/trZQHXVztK5+t+jKPW1uc3i3e501e6UFQAANyROAfCszi7T241UoyDVhpgjRvVC1RGlSnPsTU/VP/NZzi7ZawNU7zuYTVbNQtXu76b3fgEAuCFxCoBndiZ6rALUKrjsTE69N+f1sr56eupPBaqd/aXa72kWomaTU7PvbfRdr4KhySkAgBcgTgHwzD5i6d5qAmh0+9651XGq1oanUaDq/ewVb9VxtC9XLxjNPu/Obec73bn13iMAADclTgHwzM6EqLPTUbPbX6Ufpt6b8+PWUy/36/3slUjVRqlSfp+Umk2THZ+td97795lYdXb5XxuqAAC4KXEKgGe2ilO7gWT3Vkep0Uboqyh1GMWp4/ZW1rHqrXPem5paRanj+FdZR6izgWoWAXdDlSV9AAA3Jk4B8Ow+O0i1Yao3OdWLS6X0I9URjL43P7MKVLNAMwtTO8v5/iq/h6n6+FfnZ1aRavVdnwlV4hQAwI2JUwA8u7eyN4VzJaDMwtRx3AlT3ybvffQc7dX+2vOPmJqqw9QsUK3u632vo4mp3ve9ClUAANyYOAXAszvCy2o6ahZJvnV+vp2Qas9X+0vVE09tSKqnp2aBqn6e+nnbSaLRxNToqnztUr5VoJpNTvUC1e5UVe931d5vagoA4ObEKQDuYBU5VtNR9WTUmWmp1R5TvTD1rfwajurle7uBqn2N9tibmqojVW8aahahdgPWmUC187uypA8A4AWIUwDcwc7SsdltFKZW01JnNkCvI1EdqNo41duLqnZmcqoXp0bL9Nr49PfgfBSoekHqIzZPBwDg5sQpAO7iSgQZTUjVYaqelqoj1U6QOtSB6Dj/Vr1WHae+lP70VGnOR3tOzfabGi2/mwWqUZjaiVWPBKojUgEAcHPiFAB3ceUqfL0w1ZucaoPUbAlea7T/Uxuozuw7NXuN47uoX+9rGX8/nxGm2mV+Z/egMjUFAPBCxCkA7qJ3RbpVlJrtKdUed2LUbGPy0dLDNlDVr1c6x93X7F0Bb/Z9HMe/O8f2trM31WrJ384NAIAXIE4BcCdt3Gj3kjqzjK+3x9TuUrudMPXlx3MfxzpMza4C2Kpfc7THVG+j8V486u019cj01COBCgCAFyFOAXAnb6Ufoc4s41tten5lcqpdZlfHqXp6ajSxVQav13utWQxr41RvKV5vI/TR1NTuZum9KavZvlSW9AEAvBBxCoC7OROmesv4zl6F71Dv+TTbmPyYlqonpr6W36enVntPvXWOowjWLu1rJ6dGk1DtEr/eMr+rm6XPAlV7NUIAAG5MnALgburpqdHSvdkyvtW0VBurdpby/VN+boD+rYz3m+pNTpXB6+68dnulvtUV+1Zx6myQmi33mwUqAABeiDgFwB3Vk0FXNj1f7TNVGwWif8qvcegIU/+Un2FqZ0nflWV9sw3YZ/tNjZb3tWFqNDm1u1n6bJkfAAAvRpwC4I7eyjxMzfaYOrOU73it49gLRPXU1HH+vfyMVTtL+naX9c2W9q32m1qFpNH01O4Sv53N0u01BQDwgsQpAO7qSxkv4zuz6flqamp3OV99Xsep72W8tG81ObWzx9Vov6mzgerKEr+zm6UDAPCCxCkA7qyemvqoIFXKPAr1lvO1e0597xyvXK1vZ7+r0bK+3r5Po+V4oyv4tZNTV/emsgk6AMALE6cAuLNjc/TdaamenaV0s+V8bZjqRane9NRHX61vZ1P01dK+Xqha7Tu1E6ZMTQEAvDBxCoC7+1p+xqgjUp29Il9rtQl5HaRmYWq0rO+zr9a3umrfzvK+0fTUmSh1nAMA8ML8QQjAK6inp84EqdGk0mqPqdFyvp0lfaOJro+4Wt8sTo32gdqNU+00Ve/Wi1SW8wEAvDhxCoBXcFy972yQOv7b9nh1Od/VJX1nrtbX2xB9N06NJqeOf4+W8F3da8rV+QAAEKcAeBnH1fuuXJHvOO4u59sJU7tX6ruyIfrVpX1nlvfN9p/ajVQAAOAPQwBeyrH/1M6+UoedAFRfoW8Upq4u6Tt75cDVpuhnp6eu7j81W+Ln7w8AAP6XPw4BeDXt//t2pqaO894SvjoC9TZAv7Kkrz4vnePqvT26tO8j9p+yzxQAAFvEKQBe0bG8b2RnKd+ZfaZ24tTsCoJl8H7b91lKf3Kqt7RvFad2l/bN4lTvSn7CFAAAvxCnAHhVx/8DdzYZr8/P7jN1ZUnfcV7K+WV9O5NTvempY0+uj9p/qjc9ZQN0AAB+I04B8KqOK/iNHjuOo/2b/umcHyHqn/JvzDm7pG82OVXK75FqNeF1Zmlfff5X53y2vG9niZ8wBQBAlzgFwCurA9XOtNRoOV8bpo5/14Hq6pK+2bK+3vvuhak2UPWW9rXL/GaBqv13u7dUG6eEKQAAhsQpAF7dW/k3ouzGqdlyvnaC6ntz3gtU753jLEwd56sN0c8u7evtQdUGqt09qCzlAwBgmzgFAP+qJ6hWUaqdnOqFqdk+Ux81OTXaI2s1ObUbp0b7Tu3uP2XzcwAAlsQpAPjpCCqrKLUTpnbj1GxyqpTf951qraa8dpf2za7eNwpTvUj194/7hSkAALaIUwDwqyOs7O4ztQpTjyzpm22I3lvWV59/Kb8Httnyvp3pqZ3Jqa/TbxcAABriFAD87kv5uQ/VbJ+pUZg6s6SvjlKjyan62Hrkin27S/t295+yvxQAAKeJUwDQd2yUPttnahSmzi7pu3qlvuN9HsfjvJ6aqsNUL1LtxqnV8j7L+AAAuEScAoC5em+mnX2menFqtqRvNDlVynxZXynzDdF3J6d2950aBSrL+AAAeIg4BQBr9RTVTpjaXc73yJX66vd2HD97aV8bqExLAQDwMHEKAPbVU1S7YWoWqHph6vuP1xrFqffyexTqbYi+c8W+q3HK3lIAAHwYcQoAznkrPyeHrsapdknfaHKqdI7te6nPdyenjvM6to3i1PFvG54DAPApxCkAuOaIVEe8+YwlfWeX9R3Hs5NTO3EKAAA+hTgFAI+pw04dp84s6esFqtKcl+a8nZo6jqvJqTNL+0xKAQDw6cQpAPgY9XK/VZxahak2UpUynp5qw1R9/qVz3i7ta5f32egcAIA/SpwCgI/1Vn69qt0Rqep9ps5MTtXH2Wsex6uTU6akAACIEKcA4PPUoWq0Cfpscqp0ju3zt8f6fDQ51T4GAAAx4hQA/BltqGpvdaQqnfPZ87bH0eSUGAUAwH+OOAUAf14vEo2i1GpJ3/F89fO2wQoAAP6zxCkA+G8QlAAAeEk2PwUAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAO/IczQAAAuUSURBVAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAgAAACBGnAIAAAAgRpwCAAAAIEacAv5/O3ZMAAAIAzBsB/6d4G8nMnqQKOhdAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDGnAIAAAAgY04BAAAAkDGnAAAAAMiYUwAAAABkzCkAAAAAMuYUAAAAABlzCgAAAICMOQUAAABAxpwCAAAAIGNOAQAAAJAxpwAAAADImFMAAAAAZMwpAAAAADLmFAAAAAAZcwoAAACAjDkFAAAAQMacAgAAACBjTgEAAACQMacAAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDGnAIAAAAgY04BAAAAkDGnAAAAAMiYUwAAAABkzCkAAAAAMuYUAAAAABlzCgAAAICMOQUAAABAxpwCAAAAIGNOAQAAAJAxpwAAAADImFMAAAAAZMwpAAAAADLmFAAAAAAZcwoAAACAjDkFAAAAQMacAgAAACBjTgEAAACQMacAAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDGnAIAAAAgY04BAAAAkDGnAAAAAMiYUwAAAABkzCkAAAAAMuYUAAAAABlzCgAAAICMOQUAAABAxpwCAAAAIGNOAQAAAJAxpwAAAADImFMAAAAAZMwpAAAAADLmFAAAAAAZcwoAAACAjDkFAAAAQMacAgAAACBjTgEAAACQMacAAAAAyJhTAAAAAGTMKQAAAAAy5hQAAAAAGXMKAAAAgIw5BQAAAEDGnAIAAAAgc2bm1hEAAAAAfGkfLEhWxwjQKQ8AAAAASUVORK5CYII="/> -<mask id="mask6"> - <g filter="url(#alpha)"> -<use xlink:href="#image42" transform="matrix(1,0,0,1,-415,-208)"/> - </g> -</mask> -<clipPath id="clip27"> - <rect x="0" y="0" width="159" height="159"/> -</clipPath> -<g id="surface41" clip-path="url(#clip27)"> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 70.40625 157.523438 L 88.953125 157.523438 L 87.886719 115.878906 C 87.886719 115.648438 87.878906 115.417969 87.867188 115.167969 C 87.863281 115.074219 87.855469 114.992188 87.851562 114.917969 C 87.839844 114.703125 87.828125 114.519531 87.8125 114.316406 L 87.667969 112.84375 C 87.644531 112.636719 87.621094 112.429688 87.59375 112.214844 L 87.4375 110.9375 C 87.421875 110.820312 87.40625 110.683594 87.386719 110.542969 L 87.214844 109.28125 C 86.996094 107.691406 86.746094 105.96875 86.484375 104.167969 C 86.15625 101.925781 85.808594 99.566406 85.496094 97.21875 L 85.28125 95.523438 L 85.28125 92.578125 L 88.054688 96.1875 C 89.511719 98.097656 90.960938 100.042969 92.332031 101.882812 C 93.394531 103.308594 94.40625 104.671875 95.34375 105.90625 L 96.894531 107.921875 C 96.980469 108.03125 97.0625 108.136719 97.144531 108.242188 L 97.605469 108.820312 C 97.691406 108.925781 97.773438 109.027344 97.855469 109.125 C 97.996094 109.300781 98.132812 109.464844 98.265625 109.621094 L 98.496094 109.898438 C 98.625 110.050781 98.746094 110.191406 98.871094 110.328125 L 99.050781 110.53125 C 99.21875 110.722656 99.382812 110.890625 99.527344 111.039062 L 128.234375 141.253906 L 141.351562 128.140625 L 111.148438 99.445312 C 110.984375 99.28125 110.816406 99.125 110.632812 98.960938 L 110.429688 98.78125 C 110.28125 98.652344 110.144531 98.53125 109.992188 98.398438 L 108.914062 97.511719 C 108.722656 97.359375 108.535156 97.207031 108.339844 97.050781 L 107.042969 96.046875 C 106.746094 95.816406 106.441406 95.585938 106.132812 95.347656 L 106.011719 95.257812 C 104.757812 94.304688 103.371094 93.273438 101.921875 92.195312 C 99.648438 90.5 97.296875 88.75 95 86.976562 L 92.675781 85.183594 L 95.742188 85.195312 L 97.261719 85.394531 C 99.683594 85.71875 102.082031 86.070312 104.355469 86.402344 C 106.125 86.660156 107.816406 86.90625 109.363281 87.121094 L 113.367188 87.617188 C 113.648438 87.648438 113.851562 87.664062 114.050781 87.683594 L 114.410156 87.714844 C 114.617188 87.734375 114.804688 87.742188 114.988281 87.753906 L 115.257812 87.773438 C 115.511719 87.785156 115.742188 87.792969 115.949219 87.792969 L 157.621094 88.859375 L 157.621094 70.3125 L 115.976562 71.378906 C 115.742188 71.378906 115.511719 71.386719 115.265625 71.398438 L 114.980469 71.417969 C 114.785156 71.429688 114.605469 71.441406 114.414062 71.457031 L 114.101562 71.484375 C 113.851562 71.507812 113.644531 71.527344 113.425781 71.546875 L 110.664062 71.878906 C 110.304688 71.925781 109.9375 71.972656 109.5625 72.027344 L 109.359375 72.050781 C 107.800781 72.265625 106.089844 72.515625 104.300781 72.777344 C 102.042969 73.109375 99.660156 73.457031 97.296875 73.773438 L 97.230469 73.78125 C 96.757812 73.847656 96.246094 73.914062 95.738281 73.980469 L 95.316406 72.597656 L 95.003906 72.191406 C 95.007812 72.191406 95.011719 72.1875 95.011719 72.1875 L 95.023438 72.175781 C 95.417969 71.875 95.8125 71.570312 96.210938 71.269531 L 96.273438 71.21875 C 98.132812 69.804688 100.039062 68.378906 101.851562 67.03125 C 103.324219 65.933594 104.726562 64.886719 106 63.921875 L 107.335938 62.898438 C 107.566406 62.722656 107.792969 62.550781 108.007812 62.378906 L 108.335938 62.117188 C 108.539062 61.960938 108.726562 61.808594 108.90625 61.664062 C 109.015625 61.578125 109.121094 61.496094 109.214844 61.414062 L 109.992188 60.773438 C 110.144531 60.644531 110.28125 60.523438 110.414062 60.402344 L 110.625 60.21875 C 110.816406 60.046875 110.984375 59.886719 111.128906 59.742188 L 141.351562 31.03125 L 128.234375 17.917969 L 99.539062 48.121094 C 99.382812 48.28125 99.222656 48.453125 99.050781 48.640625 L 98.863281 48.851562 C 98.746094 48.984375 98.625 49.121094 98.496094 49.273438 L 98.253906 49.5625 C 98.128906 49.714844 97.996094 49.871094 97.859375 50.039062 L 96.804688 51.363281 C 96.664062 51.546875 96.515625 51.738281 96.367188 51.933594 L 95.378906 53.222656 C 94.421875 54.484375 93.414062 55.839844 92.355469 57.257812 C 91.03125 59.035156 89.636719 60.90625 88.226562 62.757812 L 88.226562 62.878906 L 87.957031 63.113281 C 87.660156 63.5 87.367188 63.882812 87.070312 64.265625 L 84.820312 67.175781 L 85.289062 63.527344 C 85.351562 63.039062 85.414062 62.546875 85.484375 62.050781 L 85.492188 61.972656 C 85.8125 59.609375 86.160156 57.222656 86.488281 54.964844 C 86.75 53.171875 87 51.460938 87.214844 49.898438 L 87.429688 48.285156 C 87.476562 47.941406 87.511719 47.65625 87.542969 47.378906 C 87.5625 47.238281 87.578125 47.109375 87.59375 46.976562 L 87.8125 44.851562 C 87.828125 44.652344 87.839844 44.46875 87.847656 44.285156 C 87.855469 44.179688 87.863281 44.097656 87.867188 44.027344 C 87.878906 43.753906 87.886719 43.523438 87.886719 43.316406 L 88.953125 1.648438 L 70.40625 1.648438 L 71.472656 43.292969 C 71.472656 43.523438 71.480469 43.757812 71.492188 44.007812 L 71.511719 44.285156 C 71.523438 44.464844 71.535156 44.652344 71.550781 44.855469 L 71.582031 45.210938 C 71.601562 45.414062 71.621094 45.621094 71.644531 45.835938 L 71.6875 46.25 C 71.710938 46.484375 71.738281 46.71875 71.769531 46.964844 L 71.972656 48.597656 C 72.023438 48.976562 72.070312 49.359375 72.125 49.75 L 72.144531 49.898438 C 72.355469 51.429688 72.605469 53.125 72.863281 54.902344 C 73.273438 57.722656 73.699219 60.636719 74.074219 63.53125 L 72.25 64.210938 C 71.980469 63.863281 71.671875 63.460938 71.363281 63.058594 L 71.316406 62.996094 C 69.878906 61.113281 68.441406 59.183594 67.082031 57.355469 C 66 55.90625 64.96875 54.519531 64.015625 53.265625 L 60.871094 49.277344 C 60.738281 49.125 60.613281 48.980469 60.488281 48.839844 L 60.3125 48.640625 C 60.140625 48.453125 59.984375 48.285156 59.839844 48.136719 L 31.128906 17.917969 L 18.015625 31.03125 L 48.214844 59.726562 C 48.378906 59.890625 48.550781 60.050781 48.734375 60.21875 L 48.9375 60.394531 C 49.078125 60.519531 49.21875 60.644531 49.375 60.777344 L 49.648438 61.003906 C 49.804688 61.136719 49.964844 61.269531 50.136719 61.410156 L 51.355469 62.378906 C 51.570312 62.546875 51.792969 62.71875 52.019531 62.894531 L 53.367188 63.925781 C 54.617188 64.875 56.003906 65.90625 57.449219 66.984375 C 59.285156 68.351562 61.214844 69.792969 63.117188 71.242188 L 63.171875 71.285156 C 63.570312 71.589844 63.96875 71.890625 64.363281 72.195312 L 66.691406 73.988281 L 63.621094 73.980469 L 62.101562 73.78125 C 59.691406 73.453125 57.300781 73.105469 55.039062 72.773438 C 53.257812 72.515625 51.554688 72.265625 50 72.050781 L 49.800781 72.027344 C 49.425781 71.972656 49.058594 71.925781 48.699219 71.878906 L 47.0625 71.671875 C 46.816406 71.644531 46.578125 71.617188 46.347656 71.59375 L 45.933594 71.546875 C 45.714844 71.527344 45.511719 71.507812 45.308594 71.488281 L 44.949219 71.457031 C 44.742188 71.441406 44.558594 71.429688 44.375 71.417969 L 44.097656 71.398438 C 43.851562 71.386719 43.617188 71.378906 43.414062 71.378906 L 1.742188 70.3125 L 1.742188 88.859375 L 43.386719 87.792969 C 43.617188 87.792969 43.851562 87.785156 44.101562 87.773438 L 44.371094 87.753906 C 44.570312 87.742188 44.75 87.734375 44.945312 87.714844 L 47.4375 87.453125 L 48.699219 87.296875 C 49.070312 87.246094 49.453125 87.195312 49.847656 87.140625 L 49.917969 87.128906 C 51.53125 86.910156 53.234375 86.660156 55.011719 86.402344 C 57.828125 85.988281 60.738281 85.566406 63.625 85.195312 L 64.046875 86.578125 L 64.355469 86.980469 C 63.960938 87.285156 63.558594 87.59375 63.152344 87.902344 L 63.097656 87.945312 C 61.195312 89.394531 59.253906 90.84375 57.417969 92.210938 C 55.980469 93.28125 54.605469 94.308594 53.359375 95.25 L 53.207031 95.367188 C 52.910156 95.59375 52.613281 95.820312 52.328125 96.039062 L 51.019531 97.058594 C 50.824219 97.210938 50.636719 97.359375 50.453125 97.507812 L 50.132812 97.769531 C 49.96875 97.902344 49.808594 98.03125 49.652344 98.164062 L 49.371094 98.398438 C 49.230469 98.515625 49.105469 98.628906 48.984375 98.734375 L 48.726562 98.964844 C 48.550781 99.125 48.378906 99.28125 48.234375 99.429688 L 18.015625 128.140625 L 31.128906 141.253906 L 59.820312 111.050781 C 59.984375 110.890625 60.144531 110.71875 60.308594 110.535156 L 60.511719 110.304688 C 60.625 110.175781 60.746094 110.042969 60.871094 109.894531 L 61.105469 109.613281 C 61.230469 109.460938 61.363281 109.300781 61.5 109.132812 L 62.46875 107.917969 C 62.640625 107.699219 62.8125 107.476562 62.988281 107.25 L 63.210938 106.957031 C 63.445312 106.65625 63.664062 106.371094 63.886719 106.078125 L 64 105.929688 C 64.96875 104.652344 65.984375 103.285156 67.050781 101.855469 C 68.421875 100.015625 69.871094 98.070312 71.328125 96.160156 C 71.648438 95.742188 71.941406 95.359375 72.230469 94.984375 L 72.257812 94.925781 L 74.449219 92.734375 L 74.074219 95.644531 C 73.703125 98.519531 73.277344 101.421875 72.871094 104.226562 C 72.609375 106.007812 72.363281 107.710938 72.148438 109.265625 L 72.121094 109.460938 C 72.066406 109.84375 72.019531 110.210938 71.972656 110.566406 L 71.644531 113.324219 C 71.621094 113.554688 71.601562 113.757812 71.582031 113.960938 L 71.550781 114.320312 C 71.535156 114.519531 71.523438 114.710938 71.511719 114.894531 L 71.492188 115.175781 C 71.480469 115.414062 71.472656 115.648438 71.472656 115.855469 Z M 70.40625 157.523438 "/> -</g> -</defs> -<g id="surface1"> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(95.68634%,95.608521%,95.628357%);fill-opacity:1;" d="M 355.816406 675.621094 L 1153.34375 675.621094 L 1153.34375 153.214844 L 355.816406 153.214844 Z M 355.816406 675.621094 "/> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph0-1" x="36.836377" y="819.007442"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph0-2" x="50.461469" y="819.007442"/> - <use xlink:href="#glyph0-3" x="63.373893" y="819.007442"/> - <use xlink:href="#glyph0-2" x="78.109424" y="819.007442"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph0-4" x="90.241376" y="819.007442"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph0-5" x="100.473379" y="819.007442"/> - <use xlink:href="#glyph0-3" x="112.180442" y="819.007442"/> - <use xlink:href="#glyph0-6" x="126.915973" y="819.007442"/> - <use xlink:href="#glyph0-7" x="131.647013" y="819.007442"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph0-8" x="140.648041" y="819.007442"/> - <use xlink:href="#glyph0-9" x="152.249635" y="819.007442"/> - <use xlink:href="#glyph0-1" x="164.438842" y="819.007442"/> - <use xlink:href="#glyph0-10" x="177.878611" y="819.007442"/> - <use xlink:href="#glyph0-2" x="183.46847" y="819.007442"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-1" x="1134.56776" y="819.007342"/> - <use xlink:href="#glyph1-2" x="1139.740805" y="819.007342"/> - <use xlink:href="#glyph1-3" x="1142.007948" y="819.007342"/> - <use xlink:href="#glyph1-2" x="1145.903475" y="819.007342"/> - <use xlink:href="#glyph1-4" x="1148.170618" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-5" x="674.795543" y="819.007342"/> - <use xlink:href="#glyph1-6" x="681.596972" y="819.007342"/> - <use xlink:href="#glyph1-7" x="686.904966" y="819.007342"/> - <use xlink:href="#glyph1-8" x="691.916072" y="819.007342"/> - <use xlink:href="#glyph1-9" x="694.965919" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-10" x="697.817841" y="819.007342"/> - <use xlink:href="#glyph1-2" x="703.107842" y="819.007342"/> - <use xlink:href="#glyph1-11" x="705.374985" y="819.007342"/> - <use xlink:href="#glyph1-8" x="711.051839" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-7" x="714.020717" y="819.007342"/> - <use xlink:href="#glyph1-12" x="719.031823" y="819.007342"/> - <use xlink:href="#glyph1-6" x="724.681687" y="819.007342"/> - <use xlink:href="#glyph1-13" x="729.989681" y="819.007342"/> - <use xlink:href="#glyph1-14" x="732.211841" y="819.007342"/> - <use xlink:href="#glyph1-15" x="737.861706" y="819.007342"/> - <use xlink:href="#glyph1-10" x="743.124716" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-16" x="515.305727" y="819.007342"/> - <use xlink:href="#glyph1-17" x="521.738295" y="819.007342"/> - <use xlink:href="#glyph1-18" x="524.248346" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-2" x="529.439384" y="819.007342"/> - <use xlink:href="#glyph1-19" x="531.706527" y="819.007342"/> - <use xlink:href="#glyph1-2" x="537.230439" y="819.007342"/> - <use xlink:href="#glyph1-20" x="539.497583" y="819.007342"/> - <use xlink:href="#glyph1-17" x="547.49556" y="819.007342"/> - <use xlink:href="#glyph1-18" x="550.005611" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-21" x="925.212291" y="819.007342"/> - <use xlink:href="#glyph1-1" x="930.385336" y="819.007342"/> - <use xlink:href="#glyph1-2" x="935.558381" y="819.007342"/> - <use xlink:href="#glyph1-22" x="937.825524" y="819.007342"/> - <use xlink:href="#glyph1-23" x="943.322446" y="819.007342"/> - <use xlink:href="#glyph1-9" x="948.57646" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-22" x="951.428382" y="819.007342"/> - <use xlink:href="#glyph1-24" x="956.925305" y="819.007342"/> - <use xlink:href="#glyph1-8" x="962.593162" y="819.007342"/> -</g> -<g style="fill:rgb(30.984497%,29.728699%,30.039978%);fill-opacity:1;"> - <use xlink:href="#glyph1-10" x="965.526054" y="819.007342"/> - <use xlink:href="#glyph1-2" x="970.816054" y="819.007342"/> - <use xlink:href="#glyph1-1" x="973.083197" y="819.007342"/> - <use xlink:href="#glyph1-21" x="978.256242" y="819.007342"/> - <use xlink:href="#glyph1-1" x="983.429287" y="819.007342"/> - <use xlink:href="#glyph1-25" x="988.602331" y="819.007342"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph2-1" x="37.057393" y="60.044512"/> - <use xlink:href="#glyph2-2" x="55.63736" y="60.044512"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph2-3" x="74.217328" y="60.044512"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph2-4" x="115.545637" y="60.044512"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph2-5" x="132.513186" y="60.044512"/> - <use xlink:href="#glyph2-6" x="152.256421" y="60.044512"/> - <use xlink:href="#glyph2-5" x="172.645916" y="60.044512"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph3-1" x="34.372708" y="128.136675"/> - <use xlink:href="#glyph3-2" x="42.994449" y="128.136675"/> - <use xlink:href="#glyph3-1" x="46.518117" y="128.136675"/> - <use xlink:href="#glyph3-2" x="55.139858" y="128.136675"/> - <use xlink:href="#glyph3-3" x="58.663527" y="128.136675"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph3-3" x="62.442098" y="128.136675"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph3-4" x="116.181786" y="128.136675"/> - <use xlink:href="#glyph3-5" x="127.517501" y="128.136675"/> - <use xlink:href="#glyph3-6" x="136.679039" y="128.136675"/> - <use xlink:href="#glyph3-7" x="145.525695" y="128.136675"/> - <use xlink:href="#glyph3-8" x="153.067844" y="128.136675"/> - <use xlink:href="#glyph3-9" x="157.896019" y="128.136675"/> - <use xlink:href="#glyph3-10" x="162.979098" y="128.136675"/> - <use xlink:href="#glyph3-11" x="171.750782" y="128.136675"/> - <use xlink:href="#glyph3-8" x="180.507472" y="128.136675"/> - <use xlink:href="#glyph3-12" x="185.335647" y="128.136675"/> - <use xlink:href="#glyph3-5" x="189.039248" y="128.136675"/> - <use xlink:href="#glyph3-6" x="198.200785" y="128.136675"/> - <use xlink:href="#glyph3-3" x="207.047441" y="128.136675"/> - <use xlink:href="#glyph3-13" x="210.826013" y="128.136675"/> - <use xlink:href="#glyph3-10" x="220.257448" y="128.136675"/> - <use xlink:href="#glyph3-3" x="229.029132" y="128.136675"/> - <use xlink:href="#glyph3-14" x="232.807704" y="128.136675"/> - <use xlink:href="#glyph3-5" x="236.631259" y="128.136675"/> - <use xlink:href="#glyph3-15" x="245.792796" y="128.136675"/> - <use xlink:href="#glyph3-5" x="255.25422" y="128.136675"/> -</g> -<use xlink:href="#surface5" transform="matrix(1,0,0,1,35,139)" mask="url(#mask0)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000191571 0.00146384 L -415.258653 0.00146384 " transform="matrix(0.999622,0,0,-0.999622,800.468941,428.03662)"/> -<use xlink:href="#surface8" transform="matrix(1,0,0,1,571,459)" mask="url(#mask1)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000336657 -0.0000350542 L -0.000336657 218.082364 " transform="matrix(0.999622,0,0,-0.999622,572.789399,646.035121)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00157854 -0.0000350542 L 0.00157854 218.082364 " transform="matrix(0.999622,0,0,-0.999622,768.904672,646.035121)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000692308 0.00000988409 L 0.000692308 -156.734991 " transform="matrix(0.999622,0,0,-0.999622,706.272745,459.710947)"/> -<use xlink:href="#surface11" transform="matrix(1,0,0,1,415,459)" mask="url(#mask2)"/> -<use xlink:href="#surface14" transform="matrix(1,0,0,1,571,459)" mask="url(#mask3)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00186012 -0.000116848 L 0.00186012 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,429.052828,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000673954 -0.000116848 L 0.000673954 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,479.011045,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000612209 -0.000116848 L -0.000612209 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,528.969362,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000561645 -0.000116848 L 0.000561645 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,441.542407,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000624518 -0.000116848 L -0.000624518 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,491.500624,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00181068 -0.000116848 L -0.00181068 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,541.458841,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000736828 -0.000116848 L -0.000736828 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,454.031987,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00182299 -0.00164389 L -0.00182299 -218.373215 " transform="matrix(0.999622,0,0,-0.999622,503.990104,428.037419)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000798573 -0.000116848 L 0.000798573 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,553.94842,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.0019353 -0.000116848 L -0.0019353 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,466.521466,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000786264 -0.000116848 L 0.000786264 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,516.479683,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000399899 -0.000116848 L -0.000399899 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,566.4379,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000792982 -0.000116848 L -0.000792982 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,435.297668,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00050549 -0.000116848 L 0.00050549 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,422.808088,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00192858 -0.00164389 L 0.00192858 -218.373215 " transform="matrix(0.999622,0,0,-0.999622,485.255885,428.037419)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000842418 -0.000116848 L 0.000842418 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,535.214002,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00191627 -0.000116848 L 0.00191627 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,447.787147,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000730109 -0.000116848 L 0.000730109 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,497.745364,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000456054 -0.000116848 L -0.000456054 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,547.703581,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.0006178 -0.000116848 L 0.0006178 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,460.276726,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000568363 -0.000116848 L -0.000568363 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,510.234943,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00175453 -0.000116848 L -0.00175453 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,560.19316,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000580673 -0.000116848 L -0.000580673 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,472.766205,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00186684 -0.000116848 L -0.00186684 -155.937942 " transform="matrix(0.999622,0,0,-0.999622,522.724522,460.128789)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00164016 -0.000180632 L -156.389481 -0.000180632 " transform="matrix(0.999622,0,0,-0.999622,572.775704,472.699038)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 -0.000693137 L -156.553481 -0.000693137 " transform="matrix(0.999622,0,0,-0.999622,572.939642,522.503213)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 -0.00110564 L -156.553481 -0.00110564 " transform="matrix(0.999622,0,0,-0.999622,572.939642,572.307489)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00164016 0.00169511 L -156.389481 0.00169511 " transform="matrix(0.999622,0,0,-0.999622,572.775704,485.150132)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 0.0011826 L -156.553481 0.0011826 " transform="matrix(0.999622,0,0,-0.999622,572.939642,534.954307)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000353443 0.000670096 L -156.242981 0.000670096 " transform="matrix(0.999622,0,0,-0.999622,572.62926,584.758482)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000234881 -0.000436884 L -156.946253 -0.000436884 " transform="matrix(0.999622,0,0,-0.999622,572.992422,497.601126)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00168032 -0.000849389 L -383.885553 -0.000849389 " transform="matrix(0.999622,0,0,-0.999622,769.107695,547.405401)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000353443 -0.00136189 L -156.242981 -0.00136189 " transform="matrix(0.999622,0,0,-0.999622,572.62926,597.209576)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 0.00143885 L -156.553481 0.00143885 " transform="matrix(0.999622,0,0,-0.999622,572.939642,510.05222)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000191571 0.000926348 L -384.168781 0.000926348 " transform="matrix(0.999622,0,0,-0.999622,800.468941,559.856395)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000353443 0.000413843 L -156.242981 0.000413843 " transform="matrix(0.999622,0,0,-0.999622,572.62926,609.66057)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00164016 -0.00119663 L -156.389481 -0.00119663 " transform="matrix(0.999622,0,0,-0.999622,572.775704,478.924585)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00164016 0.000835363 L -156.389481 0.000835363 " transform="matrix(0.999622,0,0,-0.999622,572.775704,466.473491)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000365551 -0.00170913 L -414.715653 -0.00170913 " transform="matrix(0.999622,0,0,-0.999622,799.926147,528.72876)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000338434 0.00178609 L -156.539953 0.00178609 " transform="matrix(0.999622,0,0,-0.999622,572.586276,578.533035)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00164016 0.00067911 L -156.389481 0.00067911 " transform="matrix(0.999622,0,0,-0.999622,572.775704,491.375679)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 0.000166606 L -156.553481 0.000166606 " transform="matrix(0.999622,0,0,-0.999622,572.939642,541.179854)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000353443 -0.000345899 L -156.242981 -0.000345899 " transform="matrix(0.999622,0,0,-0.999622,572.62926,590.984029)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 -0.00145288 L -156.553481 -0.00145288 " transform="matrix(0.999622,0,0,-0.999622,572.939642,503.826673)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 0.00194234 L -156.553481 0.00194234 " transform="matrix(0.999622,0,0,-0.999622,572.939642,553.630848)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000353443 0.00152984 L -156.242981 0.00152984 " transform="matrix(0.999622,0,0,-0.999622,572.62926,603.435123)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 0.000422858 L -156.553481 0.000422858 " transform="matrix(0.999622,0,0,-0.999622,572.939642,516.277766)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00176467 -0.0000896466 L -156.553481 -0.0000896466 " transform="matrix(0.999622,0,0,-0.999622,572.939642,566.081942)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00118044 -0.000214539 L 0.00118044 218.375264 " transform="matrix(0.999622,0,0,-0.999622,416.424601,646.327911)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000306196 -0.000214539 L -0.000306196 218.375264 " transform="matrix(0.999622,0,0,-0.999622,572.754212,646.327911)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000293348 -0.000214539 L -0.000293348 218.375264 " transform="matrix(0.999622,0,0,-0.999622,800.265918,646.327911)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000191571 0.00100127 L -415.258653 0.00100127 " transform="matrix(0.999622,0,0,-0.999622,800.468941,459.817407)"/> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-1" x="35.647826" y="160.366393"/> - <use xlink:href="#glyph4-2" x="41.970436" y="160.366393"/> - <use xlink:href="#glyph4-1" x="44.554459" y="160.366393"/> - <use xlink:href="#glyph4-2" x="50.87707" y="160.366393"/> - <use xlink:href="#glyph4-1" x="53.461093" y="160.366393"/> - <use xlink:href="#glyph4-2" x="59.783703" y="160.366393"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-3" x="62.367726" y="160.366393"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-4" x="116.181386" y="160.366393"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-5" x="121.954204" y="160.366393"/> - <use xlink:href="#glyph4-6" x="128.672665" y="160.366393"/> - <use xlink:href="#glyph4-5" x="135.611042" y="160.366393"/> -</g> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000191571 0.00101064 L -415.258653 0.00101064 " transform="matrix(0.999622,0,0,-0.999622,800.468941,616.184604)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000365551 0.00122498 L -414.715653 0.00122498 " transform="matrix(0.999622,0,0,-0.999622,799.926147,646.208256)"/> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000850913 -0.000214539 L -0.000850913 218.375264 " transform="matrix(0.999622,0,0,-0.999622,416.360226,646.327911)"/> -<g clip-path="url(#clip8)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000520332 0.000406637 L -81.113199 81.113085 " transform="matrix(0.999622,0,0,-0.999622,623.910676,680.848063)"/> -</g> -<g clip-path="url(#clip9)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00171283 -0.000556063 L -94.514467 94.515624 " transform="matrix(0.999622,0,0,-0.999622,650.5686,680.979913)"/> -</g> -<g clip-path="url(#clip10)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000520332 -0.000207786 L -81.113199 -81.112886 " transform="matrix(0.999622,0,0,-0.999622,623.910676,395.765417)"/> -</g> -<g clip-path="url(#clip11)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00171283 0.000854914 L -94.514467 -94.515325 " transform="matrix(0.999622,0,0,-0.999622,650.5686,395.633667)"/> -</g> -<g clip-path="url(#clip12)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000754451 0.000902605 L 81.111924 -81.115683 " transform="matrix(0.999622,0,0,-0.999622,364.94216,395.457934)"/> -</g> -<g clip-path="url(#clip13)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00168138 0.000559068 L 80.602993 -80.604115 " transform="matrix(0.999622,0,0,-0.999622,352.189181,409.231028)"/> -</g> -<g clip-path="url(#clip14)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000754451 -0.00139798 L 80.82666 80.599368 " transform="matrix(0.999622,0,0,-0.999622,364.94216,680.494696)"/> -</g> -<g clip-path="url(#clip15)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.405;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00168138 0.000654347 L 80.602993 80.605328 " transform="matrix(0.999622,0,0,-0.999622,352.189181,667.477217)"/> -</g> -<path style="fill:none;stroke-width:0.255;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 385.637895 195.55064 L 603.759371 195.55064 L 603.759371 413.668209 L 385.637895 413.668209 Z M 385.637895 195.55064 " transform="matrix(0.999622,0,0,-0.999622,0,841.675974)"/> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-1" x="35.647826" y="435.696224"/> - <use xlink:href="#glyph4-2" x="41.970436" y="435.696224"/> - <use xlink:href="#glyph4-1" x="44.554459" y="435.696224"/> - <use xlink:href="#glyph4-2" x="50.87707" y="435.696224"/> - <use xlink:href="#glyph4-7" x="53.461093" y="435.696224"/> - <use xlink:href="#glyph4-2" x="59.783703" y="435.696224"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-3" x="62.367726" y="435.696224"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-4" x="116.181386" y="435.696224"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph4-5" x="121.954204" y="435.696224"/> - <use xlink:href="#glyph4-6" x="128.672665" y="435.696224"/> - <use xlink:href="#glyph4-5" x="135.611042" y="435.696224"/> - <use xlink:href="#glyph4-3" x="142.329503" y="435.696224"/> - <use xlink:href="#glyph4-8" x="145.100455" y="435.696224"/> - <use xlink:href="#glyph4-3" x="151.423066" y="435.696224"/> - <use xlink:href="#glyph4-9" x="154.194018" y="435.696224"/> - <use xlink:href="#glyph4-10" x="161.231358" y="435.696224"/> - <use xlink:href="#glyph4-11" x="167.356043" y="435.696224"/> - <use xlink:href="#glyph4-12" x="172.886953" y="435.696224"/> - <use xlink:href="#glyph4-13" x="179.352509" y="435.696224"/> - <use xlink:href="#glyph4-14" x="182.156449" y="435.696224"/> - <use xlink:href="#glyph4-15" x="184.872423" y="435.696224"/> - <use xlink:href="#glyph4-12" x="191.35997" y="435.696224"/> -</g> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(30.984497%,29.728699%,30.039978%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000833728 0.00116153 L 532.942718 0.00116153 " transform="matrix(0.999622,0,0,-0.999622,385.782083,177.630067)"/> -<use xlink:href="#surface17" transform="matrix(1,0,0,1,571,177)" mask="url(#mask4)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00013232 -0.00127413 L 0.00013232 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,429.394399,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00020926 -0.00127413 L -0.00020926 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,479.172084,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000550841 -0.00127413 L -0.000550841 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,528.949769,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000948857 -0.00127413 L 0.000948857 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,441.838895,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000707276 -0.00127413 L 0.000707276 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,491.61648,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000365695 -0.00127413 L 0.000365695 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,541.394166,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00186539 -0.00127413 L 0.00186539 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,454.283292,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00162381 -0.00127413 L 0.00162381 -156.193101 " transform="matrix(0.999622,0,0,-0.999622,504.060877,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00128223 -0.00127413 L 0.00128223 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,553.838562,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.0011258 -0.00127413 L -0.0011258 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,466.727688,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00146738 -0.00127413 L -0.00146738 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,516.505373,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00170896 -0.00127413 L -0.00170896 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,566.282958,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000540588 -0.00127413 L 0.000540588 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,435.616647,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000375948 -0.00127413 L -0.000375948 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,423.172251,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000199008 -0.00127413 L 0.000199008 -155.735897 " transform="matrix(0.999622,0,0,-0.999622,485.394332,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.0000425728 -0.00127413 L -0.0000425728 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,535.171918,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00145712 -0.00127413 L 0.00145712 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,448.061043,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00111554 -0.00127413 L 0.00111554 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,497.838729,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000773964 -0.00127413 L 0.000773964 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,547.616414,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00153407 -0.00127413 L -0.00153407 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,460.50544,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00187565 -0.00127413 L -0.00187565 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,510.283125,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.0016905 -0.00127413 L 0.0016905 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,560.06081,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000717529 -0.00127413 L -0.000717529 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,472.949936,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00105911 -0.00127413 L -0.00105911 -155.372479 " transform="matrix(0.999622,0,0,-0.999622,522.727621,209.850289)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000415255 -0.000245193 L -155.824916 -0.000245193 " transform="matrix(0.999622,0,0,-0.999622,572.598071,222.374755)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00110228 L -155.988916 -0.00110228 " transform="matrix(0.999622,0,0,-0.999622,572.762009,271.998898)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 0.00194836 L -155.988916 0.00194836 " transform="matrix(0.999622,0,0,-0.999622,572.762009,321.623041)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000415255 -0.000484465 L -155.824916 -0.000484465 " transform="matrix(0.999622,0,0,-0.999622,572.598071,234.780766)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00124155 L -155.988916 -0.00124155 " transform="matrix(0.999622,0,0,-0.999622,572.762009,284.405009)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00149887 0.00180909 L -155.678416 0.00180909 " transform="matrix(0.999622,0,0,-0.999622,572.451627,334.029152)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000482166 -0.000723737 L -501.971394 -0.000723737 " transform="matrix(0.999622,0,0,-0.999622,918.523919,247.186777)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00148082 L -155.988916 -0.00148082 " transform="matrix(0.999622,0,0,-0.999622,572.762009,296.81102)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00149887 0.00166982 L -155.678416 0.00166982 " transform="matrix(0.999622,0,0,-0.999622,572.451627,346.435263)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.000863009 L -155.988916 -0.000863009 " transform="matrix(0.999622,0,0,-0.999622,572.762009,259.592887)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.0017201 L -155.988916 -0.0017201 " transform="matrix(0.999622,0,0,-0.999622,572.762009,309.217031)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00149887 0.00143054 L -155.678416 0.00143054 " transform="matrix(0.999622,0,0,-0.999622,572.451627,358.841274)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000415255 -0.000414829 L -155.824916 -0.000414829 " transform="matrix(0.999622,0,0,-0.999622,572.598071,228.57771)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000415255 -0.000175558 L -155.824916 -0.000175558 " transform="matrix(0.999622,0,0,-0.999622,572.598071,216.1717)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00117192 L -155.988916 -0.00117192 " transform="matrix(0.999622,0,0,-0.999622,572.762009,278.201954)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000482166 0.00187872 L -501.971394 0.00187872 " transform="matrix(0.999622,0,0,-0.999622,918.523919,327.826097)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000415255 -0.000654101 L -155.824916 -0.000654101 " transform="matrix(0.999622,0,0,-0.999622,572.598071,240.983721)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00141119 L -155.988916 -0.00141119 " transform="matrix(0.999622,0,0,-0.999622,572.762009,290.607964)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00149887 0.00173945 L -155.678416 0.00173945 " transform="matrix(0.999622,0,0,-0.999622,572.451627,340.232208)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.000793373 L -155.988916 -0.000793373 " transform="matrix(0.999622,0,0,-0.999622,572.762009,253.389832)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00155046 L -155.988916 -0.00155046 " transform="matrix(0.999622,0,0,-0.999622,572.762009,303.014075)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00149887 0.00150018 L -155.678416 0.00150018 " transform="matrix(0.999622,0,0,-0.999622,572.451627,352.638218)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00103264 L -155.988916 -0.00103264 " transform="matrix(0.999622,0,0,-0.999622,572.762009,265.795843)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000290743 -0.00178973 L -155.988916 -0.00178973 " transform="matrix(0.999622,0,0,-0.999622,572.762009,315.420086)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00134536 -0.00121507 L 0.00134536 218.374263 " transform="matrix(0.999622,0,0,-0.999622,416.811155,395.717535)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00152387 -0.00121507 L 0.00152387 218.374263 " transform="matrix(0.999622,0,0,-0.999622,603.455508,395.717535)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000758684 -0.00121507 L -0.000758684 218.374263 " transform="matrix(0.999622,0,0,-0.999622,385.567165,395.717535)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00134637 -0.000636916 L 0.00134637 218.234163 " transform="matrix(0.999622,0,0,-0.999622,752.912717,395.577488)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000769882 -0.000636916 L 0.000769882 218.234163 " transform="matrix(0.999622,0,0,-0.999622,797.819543,395.577488)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000583942 -0.000636916 L -0.000583942 218.234163 " transform="matrix(0.999622,0,0,-0.999622,918.320896,395.577488)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00182479 -0.000636916 L -0.00182479 218.234163 " transform="matrix(0.999622,0,0,-0.999622,734.22448,395.577488)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00150644 -0.000636916 L 0.00150644 218.234163 " transform="matrix(0.999622,0,0,-0.999622,779.131307,395.577488)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000610719 -0.000636916 L -0.000610719 218.234163 " transform="matrix(0.999622,0,0,-0.999622,887.113892,395.577488)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00143617 0.0005801 L 30.966261 0.0005801 " transform="matrix(0.999622,0,0,-0.999622,572.318877,296.883392)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00143617 0.000758101 L 30.966261 0.000758101 " transform="matrix(0.999622,0,0,-0.999622,572.318877,278.28982)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00030188 0.00108888 L 0.109114 -31.717927 " transform="matrix(0.999622,0,0,-0.999622,485.293271,178.02062)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.0000238787 0.00108888 L 0.109392 -31.717927 " transform="matrix(0.999622,0,0,-0.999622,503.886743,178.02062)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000192529 0.000792658 L 0.109224 30.242688 " transform="matrix(0.999622,0,0,-0.999622,503.953317,395.715636)"/> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00030188 0.000792658 L 0.109114 30.242688 " transform="matrix(0.999622,0,0,-0.999622,485.293271,395.715636)"/> -<path style="fill:none;stroke-width:0.28;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000610719 0.00154831 L -501.936353 0.00154831 " transform="matrix(0.999622,0,0,-0.999622,887.113892,297.001548)"/> -<path style="fill:none;stroke-width:0.28;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000610719 0.00185766 L -501.936353 0.00185766 " transform="matrix(0.999622,0,0,-0.999622,887.113892,278.341701)"/> -<path style="fill:none;stroke-width:0.28;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00119345 -0.000619598 L -501.532053 -0.000619598 " transform="matrix(0.999622,0,0,-0.999622,886.709744,209.772818)"/> -<path style="fill:none;stroke-width:0.28;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00139106 0.0012046 L -501.531855 0.0012046 " transform="matrix(0.999622,0,0,-0.999622,887.315016,365.325423)"/> -<path style="fill:none;stroke-width:0.28;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000561207 -0.000636916 L -484.132701 -0.000636916 " transform="matrix(0.999622,0,0,-0.999622,918.281811,395.577488)"/> -<g clip-path="url(#clip18)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00136367 0.00135113 L 80.969456 80.969444 " transform="matrix(0.999622,0,0,-0.999622,365.252543,430.368538)"/> -</g> -<g clip-path="url(#clip19)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.00138333 0.000607459 L 80.462612 80.460695 " transform="matrix(0.999622,0,0,-0.999622,352.845133,416.891232)"/> -</g> -<g clip-path="url(#clip20)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.0013396 0.000635823 L 80.969432 -80.752532 " transform="matrix(0.999622,0,0,-0.999622,365.139286,145.387354)"/> -</g> -<g clip-path="url(#clip21)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.000500772 0.00158261 L 80.515297 -80.567922 " transform="matrix(0.999622,0,0,-0.999622,352.784656,158.509395)"/> -</g> -<g clip-path="url(#clip22)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000269532 0.000635823 L -80.808145 -80.803332 " transform="matrix(0.999622,0,0,-0.999622,624.441676,145.387354)"/> -</g> -<g clip-path="url(#clip23)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00158566 -0.00128691 L -93.608002 -93.661675 " transform="matrix(0.999622,0,0,-0.999622,650.099977,145.34637)"/> -</g> -<g clip-path="url(#clip24)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0.00159734 -0.000493893 L -80.591353 80.803474 " transform="matrix(0.999622,0,0,-0.999622,624.170278,430.237788)"/> -</g> -<g clip-path="url(#clip25)" clip-rule="nonzero"> -<path style="fill:none;stroke-width:0.404;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M -0.000211679 -0.000556593 L -93.96931 93.855218 " transform="matrix(0.999622,0,0,-0.999622,650.406462,430.370537)"/> -</g> -<path style="fill:none;stroke-width:0.255;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(18.043518%,16.551208%,16.921997%);stroke-opacity:1;stroke-miterlimit:4;" d="M 385.637895 446.25474 L 603.759371 446.25474 L 603.759371 664.372308 L 385.637895 664.372308 Z M 385.637895 446.25474 " transform="matrix(0.999622,0,0,-0.999622,0,841.675974)"/> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 732.226562 547.273438 L 732.226562 524.78125 C 734.417969 527.421875 738.996094 529.496094 745.101562 529.496094 C 758.355469 529.496094 768.671875 519.222656 768.671875 504.894531 C 768.671875 490.847656 759.40625 480.480469 745.671875 480.480469 C 738.902344 480.480469 733.75 483.402344 731.75 486.417969 L 731.75 481.609375 L 716.761719 481.609375 L 716.761719 547.273438 Z M 753.492188 504.988281 C 753.492188 512.8125 748.4375 516.675781 742.714844 516.675781 C 737.089844 516.675781 732.035156 512.71875 732.035156 504.988281 C 732.035156 497.164062 737.089844 493.394531 742.714844 493.394531 C 748.4375 493.394531 753.492188 497.164062 753.492188 504.988281 "/> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 618.996094 528.472656 L 618.996094 501.464844 C 618.996094 497.21875 621.761719 493.34375 626.816406 493.34375 C 632.0625 493.34375 634.445312 496.839844 634.445312 501.277344 L 634.445312 528.472656 L 649.71875 528.472656 L 649.71875 501.371094 C 649.71875 497.21875 652.066406 493.34375 657.300781 493.34375 C 662.449219 493.34375 665.167969 496.839844 665.167969 501.277344 L 665.167969 528.472656 L 680.15625 528.472656 L 680.15625 498.066406 C 680.15625 485.320312 669.636719 480.125 660.671875 480.125 C 654.28125 480.125 649.703125 482.203125 645.984375 487.492188 C 643.601562 482.863281 638.738281 480.125 631.964844 480.125 C 626.816406 480.125 620.710938 482.769531 618.328125 486.925781 L 618.328125 481.542969 L 603.53125 481.542969 L 603.53125 528.472656 Z M 618.996094 528.472656 "/> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 706.3125 481.542969 L 690.847656 481.542969 L 690.847656 528.472656 L 706.3125 528.472656 Z M 706.3125 481.542969 "/> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 690.175781 467.171875 C 690.175781 471.800781 693.953125 475.574219 698.488281 475.574219 C 703.207031 475.574219 706.984375 471.800781 706.984375 467.171875 C 706.984375 462.542969 703.207031 458.769531 698.488281 458.769531 C 693.953125 458.769531 690.175781 462.542969 690.175781 467.171875 "/> -<use xlink:href="#surface31" transform="matrix(1,0,0,1,415,458)" mask="url(#mask5)"/> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-1" x="601.840219" y="573.701562"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-2" x="618.275298" y="573.701562"/> - <use xlink:href="#glyph5-3" x="629.511169" y="573.701562"/> - <use xlink:href="#glyph5-4" x="641.636838" y="573.701562"/> - <use xlink:href="#glyph5-5" x="646.661575" y="573.701562"/> - <use xlink:href="#glyph5-6" x="657.653189" y="573.701562"/> - <use xlink:href="#glyph5-7" x="669.778858" y="573.701562"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-8" x="601.840219" y="594.847369"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-9" x="607.187726" y="594.847369"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-10" x="618.327639" y="594.847369"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-11" x="626.117727" y="594.847369"/> - <use xlink:href="#glyph5-12" x="637.737432" y="594.847369"/> - <use xlink:href="#glyph5-13" x="645.065174" y="594.847369"/> - <use xlink:href="#glyph5-6" x="662.267922" y="594.847369"/> - <use xlink:href="#glyph5-14" x="674.393591" y="594.847369"/> - <use xlink:href="#glyph5-4" x="682.349425" y="594.847369"/> - <use xlink:href="#glyph5-5" x="687.374163" y="594.847369"/> - <use xlink:href="#glyph5-15" x="698.365776" y="594.847369"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-16" x="601.840219" y="615.993176"/> - <use xlink:href="#glyph5-7" x="613.128431" y="615.993176"/> - <use xlink:href="#glyph5-6" x="618.153169" y="615.993176"/> - <use xlink:href="#glyph5-14" x="630.278837" y="615.993176"/> - <use xlink:href="#glyph5-10" x="638.234672" y="615.993176"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph5-11" x="646.02476" y="615.993176"/> - <use xlink:href="#glyph5-12" x="657.644465" y="615.993176"/> - <use xlink:href="#glyph5-13" x="664.972208" y="615.993176"/> -</g> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 824.367188 365.464844 L 824.367188 321.269531 C 828.121094 325.785156 835.953125 329.335938 846.398438 329.335938 C 869.082031 329.335938 886.734375 311.75 886.734375 287.230469 C 886.734375 263.195312 870.878906 245.453125 847.378906 245.453125 C 835.789062 245.453125 826.976562 250.453125 823.550781 255.613281 L 823.550781 247.386719 L 797.898438 247.386719 L 797.898438 365.464844 Z M 860.757812 287.394531 C 860.757812 300.78125 852.109375 307.394531 842.316406 307.394531 C 832.6875 307.394531 824.039062 300.621094 824.039062 287.394531 C 824.039062 274.003906 832.6875 267.550781 842.316406 267.550781 C 852.109375 267.550781 860.757812 274.003906 860.757812 287.394531 "/> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 629.910156 327.695312 L 629.910156 281.480469 C 629.910156 274.207031 634.644531 267.582031 643.292969 267.582031 C 652.269531 267.582031 656.347656 273.5625 656.347656 281.15625 L 656.347656 327.695312 L 682.488281 327.695312 L 682.488281 281.316406 C 682.488281 274.207031 686.5 267.582031 695.460938 267.582031 C 704.273438 267.582031 708.925781 273.5625 708.925781 281.15625 L 708.925781 327.695312 L 734.574219 327.695312 L 734.574219 275.660156 C 734.574219 253.847656 716.570312 244.957031 701.226562 244.957031 C 690.292969 244.957031 682.460938 248.511719 676.09375 257.5625 C 672.015625 249.644531 663.691406 244.957031 652.105469 244.957031 C 643.292969 244.957031 632.847656 249.484375 628.765625 256.59375 L 628.765625 247.382812 L 603.445312 247.382812 L 603.445312 327.695312 Z M 629.910156 327.695312 "/> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 779.117188 247.382812 L 752.652344 247.382812 L 752.652344 327.695312 L 779.117188 327.695312 Z M 779.117188 247.382812 "/> -<use xlink:href="#surface41" transform="matrix(1,0,0,1,415,208)" mask="url(#mask6)"/> -<path style=" stroke:none;fill-rule:nonzero;fill:rgb(18.043518%,16.551208%,16.921997%);fill-opacity:1;" d="M 751.503906 221.867188 C 751.503906 229.785156 757.964844 236.25 765.722656 236.25 C 773.804688 236.25 780.269531 229.785156 780.269531 221.867188 C 780.269531 213.949219 773.804688 207.484375 765.722656 207.484375 C 757.964844 207.484375 751.503906 213.949219 751.503906 221.867188 "/> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-1" x="582.749234" y="541.69446"/> - <use xlink:href="#glyph6-2" x="588.271085" y="541.69446"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-3" x="776.650844" y="497.926403"/> - <use xlink:href="#glyph6-3" x="781.289198" y="497.926403"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-2" x="786.811049" y="497.926403"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-4" x="779.165394" y="590.74442"/> - <use xlink:href="#glyph6-5" x="783.803748" y="590.74442"/> - <use xlink:href="#glyph6-2" x="785.58841" y="590.74442"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-6" x="393.341826" y="290.240903"/> - <use xlink:href="#glyph6-7" x="398.863676" y="290.240903"/> - <use xlink:href="#glyph6-2" x="404.385527" y="290.240903"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-1" x="582.749234" y="196.352191"/> - <use xlink:href="#glyph6-2" x="588.271085" y="196.352191"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-1" x="582.749234" y="290.240903"/> - <use xlink:href="#glyph6-2" x="588.271085" y="290.240903"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-1" x="738.269052" y="196.352191"/> - <use xlink:href="#glyph6-2" x="743.790902" y="196.352191"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-1" x="783.331119" y="196.352191"/> - <use xlink:href="#glyph6-2" x="788.85297" y="196.352191"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-3" x="894.783893" y="290.286286"/> - <use xlink:href="#glyph6-1" x="899.422247" y="290.286286"/> -</g> -<g style="fill:rgb(13.729858%,12.159729%,12.548828%);fill-opacity:1;"> - <use xlink:href="#glyph6-2" x="904.944097" y="290.286286"/> -</g> -</g> -</svg> diff --git a/src/assets/mip-logo-short.png b/src/assets/mip-logo-short.png index debf0b0cc..aaec23003 100644 Binary files a/src/assets/mip-logo-short.png and b/src/assets/mip-logo-short.png differ diff --git a/src/assets/mip-logo-whole.png b/src/assets/mip-logo-whole.png deleted file mode 100644 index 49121d274..000000000 Binary files a/src/assets/mip-logo-whole.png and /dev/null differ diff --git a/src/assets/notebook-ai-icon.svg b/src/assets/notebook-ai-icon.svg new file mode 100644 index 000000000..967b2d7ae --- /dev/null +++ b/src/assets/notebook-ai-icon.svg @@ -0,0 +1,6 @@ +<svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 28 28" fill="none" aria-hidden="true"> + <rect x="6.5" y="4.5" width="15" height="19" rx="2.5" stroke="#2B33E9" stroke-width="1.75"/> + <path d="M10 4.5V23.5" stroke="#2B33E9" stroke-width="1.75" stroke-linecap="round"/> + <path d="M10 8.5H8.5M10 12H8.5M10 15.5H8.5M10 19H8.5" stroke="#2B33E9" stroke-width="1.5" stroke-linecap="round"/> + <path d="M15.2 11.8L16.1 13.6L18.1 13.9L16.7 15.3L17 17.3L15.2 16.3L13.4 17.3L13.7 15.3L12.3 13.9L14.3 13.6L15.2 11.8Z" fill="#2B33E9"/> +</svg> diff --git a/src/favicon.ico b/src/favicon.ico deleted file mode 100644 index 74b5c218e..000000000 Binary files a/src/favicon.ico and /dev/null differ diff --git a/src/polyfills.ts b/src/polyfills.ts deleted file mode 100644 index 97048d942..000000000 --- a/src/polyfills.ts +++ /dev/null @@ -1,6 +0,0 @@ -import * as $ from 'jquery'; -import 'jquery-ui-dist/jquery-ui'; -import 'jQuery-QueryBuilder/dist/js/query-builder'; - -(window as any).$ = $; -(window as any).jQuery = $; diff --git a/src/styles.css b/src/styles.css index e5aa83941..fafeafdf9 100644 --- a/src/styles.css +++ b/src/styles.css @@ -19,131 +19,102 @@ /* Softer brand-tinted border */ --studio-card-bg: #ffffff; --studio-card-border: rgba(43, 51, 233, 0.1); - --studio-card-shadow: 0 8px 32px rgba(15, 23, 42, 0.06); + --studio-card-shadow: 0 1px 2px rgba(43, 51, 233, 0.06); --studio-card-header-bg: #f8fbff; --studio-card-header-border: rgba(43, 51, 233, 0.08); /* Low Profile Semantic Colors */ - --covariate-color: #ccb692; - /* Desaturated Sand */ - --filter-color: #94a3b8; - /* Muted Slate */ - --variable-color: #2b33e9; - /* Brand Blue */ + --covariate-color: #bba66f; + /* Color-blind default tan/beige */ + --filter-color: #483300; + /* Color-blind default dark brown */ + --variable-color: #ffba08; + /* Color-blind default golden yellow */ + + /* Variables missing from a selected dataset (see variable-availability.utils). */ + --unavailable-text: #94a3b8; + --unavailable-border: #cbd5e1; + --unavailable-fill: #f1f5f9; + --unavailable-opacity: 0.55; --chart-label-color: #334155; /* Darker for better visibility */ - /* Arctic Precision (Glassmorphism) */ - --glass-bg: rgba(255, 255, 255, 0.7); - --glass-border: rgba(255, 255, 255, 0.2); - --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.07); - --glass-blur: blur(12px); - - /* Typography - Alaska Aesthetic */ - --line-height-config: 1.6; + /* Glass tokens alias solid studio surfaces (no frost). */ + --glass-bg: var(--card-bg); + --glass-border: var(--border-color); + --glass-shadow: var(--studio-card-shadow); + --glass-blur: none; + + /* Typography - Alaska Aesthetic. + Alaska is the licensed brand face and stays first in the slot: it resolves when the + user has it locally, and will win outright the moment a @font-face ships. + Inter is the face we actually load (see index.html) and therefore the app's real + default; system-ui is the honest fallback, not an accident. */ + --font-sans: 'Alaska Regular', 'Alaska', 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; --letter-spacing-config: 0.015em; /* Spacing & Borders - Charter standard */ --radius-sm: 4px; --radius-md: 8px; --radius-lg: 12px; - --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05); - --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1); - --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1); + --shadow-sm: 0 1px 2px rgba(43, 51, 233, 0.05); + --shadow-md: 0 1px 2px rgba(43, 51, 233, 0.06); + --shadow-lg: 0 2px 8px rgba(43, 51, 233, 0.08); --header-height: 64px; -} - -@media (prefers-color-scheme: dark) { - body:not(.theme-light) { - --bg-color: #0b0f1a; - --card-bg: #151b2b; - --text-main: #f1f5f9; - --text-muted: #94a3b8; - --border-color: rgba(148, 163, 184, 0.15); - --studio-card-bg: #151b2b; - --studio-card-border: rgba(148, 163, 184, 0.2); - --studio-card-shadow: 0 12px 32px rgba(0, 0, 0, 0.4); - --studio-card-header-bg: #1c253d; - --studio-card-header-border: rgba(148, 163, 184, 0.2); - --glass-bg: rgba(21, 27, 43, 0.8); - --glass-border: rgba(148, 163, 184, 0.1); - - /* MIP Brand colors for dark mode */ - --primary-color: #7f9ce8; - --primary-dark: #5a73c9; - --primary-light: #2b33e9; - --accent-color: #ffba08; - } -} - -/* Manual dark mode via class */ -body.theme-dark { - --bg-color: #0b0f1a; - --card-bg: #151b2b; - --text-main: #f1f5f9; - --text-muted: #94a3b8; - --border-color: rgba(148, 163, 184, 0.15); - --studio-card-bg: #151b2b; - --studio-card-border: rgba(148, 163, 184, 0.2); - --studio-card-shadow: 0 12px 32px rgba(0, 0, 0, 0.4); - --studio-card-header-bg: #1c253d; - --studio-card-header-border: rgba(148, 163, 184, 0.2); - --glass-bg: rgba(21, 27, 43, 0.8); - --glass-border: rgba(148, 163, 184, 0.1); - - /* MIP Brand colors for dark mode */ - --primary-color: #7f9ce8; - --primary-dark: #5a73c9; - --primary-light: #2b33e9; - /* Low Profile Semantic Colors - Dark mode adjustments */ - --covariate-color: #a48f68; - --filter-color: #64748b; - --variable-color: #7f9ce8; - --accent-color: var(--covariate-color); - --chart-label-color: #cbd5e1; -} - -/* Colorblind Mode Overrides (matches portal-frontend colors) */ -body.colorblind-mode { - --variable-color: #ffba08; - /* MIP golden yellow */ - --covariate-color: #bba66f; - /* MIP tan/beige */ - --filter-color: #483300; - /* MIP dark brown */ -} - -body.colorblind-mode.theme-dark { - --variable-color: #ffba08; - --covariate-color: #bba66f; - --filter-color: #5a4420; - /* Slightly lighter for dark mode visibility */ -} - -/* Global Transition for Theme Switching */ -body { - transition: background-color 0.3s ease, color 0.3s ease; -} - -/* Scrollbar Theming */ -body.theme-dark ::-webkit-scrollbar { - width: 8px; - height: 8px; -} - -body.theme-dark ::-webkit-scrollbar-track { - background: #0b0f1a; -} -body.theme-dark ::-webkit-scrollbar-thumb { - background: #1c253d; - border-radius: 10px; - border: 2px solid #0b0f1a; + /* Top chrome: one surface recipe shared by the header and the studio sub-header. */ + --header-surface: var(--card-bg); + --header-border: color-mix(in srgb, var(--primary-color) 14%, transparent); + --header-shadow: 0 1px 2px rgba(43, 51, 233, 0.06), 0 4px 12px -6px rgba(43, 51, 233, 0.14); + + /* Studio content column: the measure of .studio-inner's panels. */ + --studio-page-gutter: 16px; + --studio-inner-max: 1320px; + + /* Chrome inset — the floor, not the measure. One flat value on every route: the header + and the studio sub-header are two rows of the same bar, so they never disagree about + where the bar starts and no route can indent its own bar. It matches the dashboard + gutter, the one surface the bar sits above most often. A row that sits above a + centred column caps its own content instead of changing this value — see + .sub-header-inner, which shares .studio-inner's measure on the studio route. */ + --chrome-inline-padding: 24px; + + /* Mono slot. Numbers, IDs and measurements are set in mono across the product, but + four files each carried their own stack (JetBrains Mono / Roboto Mono / bare + ui-monospace), so the same kind of cell rendered in three faces. One token. */ + --font-mono: ui-monospace, SFMono-Regular, Menlo, monospace; + + /* Tint ladder. Every light brand tint in the app is a percentage of a brand colour + over one base, so a component asks for a step and never writes the recipe. The + steps below are the ones already shipped in the chrome; a new step is a design + decision, not a free value. */ + --tint-base: #ffffff; + --tint-nav-hover: color-mix(in srgb, var(--primary-light) 20%, var(--tint-base)); + + /* Avatar ring and placeholder fill, previously inline on the header control. */ + --ring-avatar: color-mix(in srgb, var(--primary-light) 40%, var(--tint-base)); + --avatar-bg: color-mix(in srgb, var(--text-main) 30%, var(--tint-base)); + + /* Focus ring. It was --primary-light, which is 2.68:1 on white — under the 3:1 a + focus indicator needs, and the one place the brand's own "never a foreground on + paper" rule was broken. --primary-color is 7.63:1 on the card. */ + --color-focus: var(--primary-color); + + /* Motion: one enter curve, a shorter exit, applied to background/border/shadow/ + transform only. Exit is decisive because the user already chose to dismiss. */ + --motion-enter: 200ms; + --motion-exit: 140ms; + --ease-standard: cubic-bezier(0.23, 1, 0.32, 1); } -body.theme-dark ::-webkit-scrollbar-thumb:hover { - background: #2b33e9; +/* The header's height IS this token at every breakpoint. Consumers (sub-header, warning + banner, page padding, scroll anchors, guide spotlight) all derive from it, so changing + the token here — never a component's height — keeps the whole chrome stack aligned. */ +@media (max-width: 768px) { + :root { + --header-height: 56px; + } } html, @@ -153,7 +124,7 @@ body { body { margin: 0; - font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; + font-family: var(--font-sans); background-color: var(--bg-color); color: var(--text-main); line-height: 1.6; @@ -167,60 +138,457 @@ body { /* Removed radial pattern for cleaner workspace as requested */ } +* { + scrollbar-width: thin; + scrollbar-color: rgba(43, 51, 233, 0.22) transparent; +} + +::-webkit-scrollbar { + width: 6px; + height: 6px; +} + +::-webkit-scrollbar-track { + background: transparent; +} + +::-webkit-scrollbar-thumb { + background: rgba(43, 51, 233, 0.22); + border-radius: 999px; +} + +::-webkit-scrollbar-thumb:hover { + background: rgba(43, 51, 233, 0.34); +} + +button:focus-visible, +a:focus-visible, +input:focus-visible, +select:focus-visible, +textarea:focus-visible, +[role='button']:focus-visible, +[tabindex='0']:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} + /* Style all required fields */ input:required, textarea:required, select:required { - border-color: #ccc; - /* Neutral border color for required fields */ - background-color: #fafafa; - /* Light background color */ + border-color: var(--border-color); + background-color: var(--card-bg); box-shadow: none; - /* Remove any default shadow */ } -/* Style for invalid fields (unfilled required fields) */ input:required:invalid, textarea:required:invalid, select:required:invalid { - border-color: #d32f2f; - /* Red border for invalid fields */ - background-color: #ffe6e6; - /* Light red background for visibility */ + border-color: var(--danger); + background-color: color-mix(in srgb, var(--danger) 10%, white); } +@keyframes studioSoftReveal { + from { + opacity: 0; + transform: translateY(10px); + } + + to { + opacity: 1; + transform: translateY(0); + } +} -@keyframes glacialPulse { +@keyframes studioBadgePop { 0% { - box-shadow: 0 0 0 0 rgba(43, 51, 233, 0.2); + transform: scale(0.96); } 70% { - box-shadow: 0 0 0 10px rgba(43, 51, 233, 0); + transform: scale(1.025); } 100% { - box-shadow: 0 0 0 0 rgba(43, 51, 233, 0); + transform: scale(1); } } -/* Glassmorphism Utility */ -.glass-panel { - background: var(--glass-bg); - backdrop-filter: var(--glass-blur); - -webkit-backdrop-filter: var(--glass-blur); - border: 1px solid var(--glass-border); - box-shadow: var(--glass-shadow); +@keyframes studioSkeletonShimmer { + 0% { + background-position: 180% 0; + } + + 100% { + background-position: -180% 0; + } } -/* Style for valid fields (when required fields are filled) */ input:required:valid, textarea:required:valid, select:required:valid { - border-color: #4caf50; - /* Green border for valid fields */ - background-color: #e8f5e9; - /* Light green background */ + border-color: var(--success); + background-color: color-mix(in srgb, var(--success) 10%, white); +} + +/* The one count recipe — compare buttons, folder chips, row menus, canvas headers and the + studio variable pool. Tabular so a count does not jitter as it changes. */ +.count-badge { + background: var(--primary-color); + color: #ffffff; + font-size: 0.6rem; + font-weight: 800; + min-width: 15px; + height: 15px; + padding: 0 4px; + border-radius: 999px; + display: inline-flex; + align-items: center; + justify-content: center; + font-variant-numeric: tabular-nums; + flex-shrink: 0; +} + +@media (prefers-reduced-motion: reduce) { + .experiment-studio-page .config-field, + .experiment-studio-page .workflow-section-body.open > *, + .experiment-studio-page .summary-workspace, + .experiment-studio-page .result-section, + .experiment-studio-page .empty-state-block i, + .experiment-studio-page .statistics-variable-btn.active, + .experiment-studio-page .preprocessing-state-chip, + .experiment-studio-page .studio-skeleton-line, + .experiment-studio-page .studio-skeleton-bar, + .experiment-studio-page .studio-skeleton-cell { + animation: none; + } +} + +@media (max-width: 768px) { + +} + +.experiment-studio-page input:not([type='checkbox']):not([type='radio']), +.experiment-studio-page select, +.experiment-studio-page textarea { + appearance: none; + -webkit-appearance: none; + width: 100%; + min-height: 36px; + border: 1px solid rgba(43, 51, 233, 0.16); + border-radius: var(--radius-md, 8px); + background-color: rgba(255, 255, 255, 0.72); + color: var(--text-main); + font: inherit; + font-size: 0.82rem; + line-height: 1.35; + padding: 8px 11px; + box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8); + transition: + background-color 0.18s ease, + border-color 0.18s ease, + box-shadow 0.18s ease, + transform 0.12s ease; +} + +.experiment-studio-page select:not([multiple]) { + padding-right: 34px; + background-image: + linear-gradient(45deg, transparent 50%, var(--primary-color) 50%), + linear-gradient(135deg, var(--primary-color) 50%, transparent 50%); + background-position: + calc(100% - 17px) 50%, + calc(100% - 12px) 50%; + background-size: 5px 5px, 5px 5px; + background-repeat: no-repeat; +} + +.experiment-studio-page select[multiple] { + min-height: 112px; + padding-right: 11px; +} + +.experiment-studio-page input:not([type='checkbox']):not([type='radio']):hover, +.experiment-studio-page select:hover, +.experiment-studio-page textarea:hover { + border-color: rgba(43, 51, 233, 0.28); + background-color: rgba(255, 255, 255, 0.9); +} + +.experiment-studio-page input:not([type='checkbox']):not([type='radio']):focus, +.experiment-studio-page select:focus, +.experiment-studio-page textarea:focus { + outline: none; + border-color: var(--primary-color); + background-color: #ffffff; + box-shadow: + 0 0 0 3px rgba(127, 156, 232, 0.24), + 0 6px 14px rgba(43, 51, 233, 0.06); +} + +.experiment-studio-page input[type='number'] { + font-variant-numeric: tabular-nums; +} + +.experiment-studio-page input[type='number']::-webkit-outer-spin-button, +.experiment-studio-page input[type='number']::-webkit-inner-spin-button { + margin: 0; + -webkit-appearance: none; +} + +.experiment-studio-page input[type='checkbox'], +.experiment-studio-page input[type='radio'] { + appearance: none; + -webkit-appearance: none; + display: inline-grid; + place-content: center; + width: 18px; + height: 18px; + margin: 0; + border: 1.5px solid rgba(43, 51, 233, 0.32); + background: rgba(255, 255, 255, 0.88); + color: var(--primary-color); + cursor: pointer; + flex: 0 0 auto; +} + +.experiment-studio-page input[type='checkbox'] { + border-radius: 5px; +} + +.experiment-studio-page input[type='radio'] { + border-radius: 50%; +} + +.experiment-studio-page input[type='checkbox']::before, +.experiment-studio-page input[type='radio']::before { + content: ''; + transform: scale(0); + transition: transform 0.12s ease-in-out; +} + +.experiment-studio-page input[type='checkbox']::before { + width: 10px; + height: 10px; + border-radius: 3px; + background: var(--primary-color); +} + +.experiment-studio-page input[type='radio']::before { + width: 8px; + height: 8px; + border-radius: 50%; + background: var(--primary-color); +} + +.experiment-studio-page input[type='checkbox']:checked::before, +.experiment-studio-page input[type='radio']:checked::before { + transform: scale(1); +} + +.experiment-studio-page input:disabled, +.experiment-studio-page select:disabled, +.experiment-studio-page textarea:disabled { + cursor: not-allowed; + opacity: 0.58; + background-color: rgba(241, 245, 249, 0.82); +} + +.experiment-studio-page .config-field, +.experiment-studio-page .workflow-section-body.open > *, +.experiment-studio-page .summary-workspace, +.experiment-studio-page .result-section { + animation: studioSoftReveal 0.28s ease both; +} + +.experiment-studio-page .statistics-variable-btn.active, +.experiment-studio-page .preprocessing-state-chip { + animation: studioBadgePop 0.22s ease both; +} + +/* Selectable chip pickers (pathology, datasets). A chip is either a button or a + label wrapping a hidden native input that carries selection/keyboard state. */ +.experiment-studio-page .studio-chip-group { + display: inline-flex; + flex-direction: column; + align-items: flex-start; + gap: 8px; + min-width: 0; + font-family: inherit; +} + +.experiment-studio-page .studio-chip-label { + font-size: 0.72rem; + font-weight: 750; + letter-spacing: 0.05em; + text-transform: uppercase; + color: #475569; + line-height: 1; +} + +.experiment-studio-page .studio-chip-row { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 6px; + width: 100%; + min-width: 0; +} + +.experiment-studio-page .studio-chip { + position: relative; + display: inline-flex; + align-items: center; + max-width: 100%; + padding: 5px 12px; + border: 1px solid rgba(43, 51, 233, 0.12); + border-radius: 999px; + background: #ffffff; + color: var(--text-main, #0f172a); + font-family: inherit; + font-size: 0.8rem; + font-weight: 600; + line-height: 1.2; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + cursor: pointer; + box-shadow: none; + transition: border-color 0.15s ease, background-color 0.15s ease, + box-shadow 0.15s ease, color 0.15s ease; +} + +.experiment-studio-page .studio-chip:hover { + border-color: rgba(43, 51, 233, 0.35); + background: #f8fbff; +} + +.experiment-studio-page .studio-chip.is-selected { + border-color: var(--primary-color, #2b33e9); + background: var(--primary-color, #2b33e9); + color: #ffffff; +} + +.experiment-studio-page .studio-chip:focus-visible, +.experiment-studio-page .studio-chip:has(input:focus-visible) { + outline: none; + border-color: var(--primary-color, #2b33e9); + box-shadow: 0 0 0 3px rgba(43, 51, 233, 0.18); +} + +@media (max-width: 960px) { + .experiment-studio-page .studio-chip { + font-size: 0.78rem; + } +} + +.experiment-studio-page .panel-secondary-btn { + display: inline-flex; + align-items: center; + justify-content: center; + gap: 8px; + padding: 10px 18px; + border-radius: 999px; + border: 1px solid rgba(127, 156, 232, 0.38); + background: rgba(255, 255, 255, 0.96); + color: #334155; + font: inherit; + font-size: 0.82rem; + font-weight: 600; + line-height: 1.2; + cursor: pointer; + transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, + transform 0.12s ease, box-shadow 0.15s ease; +} + +.experiment-studio-page .panel-secondary-btn i { + font-size: 0.78rem; + color: var(--primary-color, #2b33e9); +} + +.experiment-studio-page .panel-secondary-btn:hover { + background: rgba(43, 51, 233, 0.06); + border-color: rgba(43, 51, 233, 0.45); + color: var(--primary-color, #2b33e9); + transform: translateY(-1px); + box-shadow: 0 8px 18px rgba(43, 51, 233, 0.08); +} + +.experiment-studio-page .panel-secondary-btn:focus-visible { + outline: 2px solid var(--primary-color, #2b33e9); + outline-offset: 2px; +} + +.experiment-studio-page .studio-skeleton-card { + display: flex; + flex-direction: column; + gap: 16px; + width: min(100%, 860px); + margin: 18px auto; + padding: 22px; + border: 1px solid rgba(127, 156, 232, 0.18); + border-radius: var(--radius-lg, 12px); + background: rgba(255, 255, 255, 0.66); + box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.72), 0 18px 42px rgba(43, 51, 233, 0.05); +} + +.experiment-studio-page .studio-skeleton-line, +.experiment-studio-page .studio-skeleton-bar, +.experiment-studio-page .studio-skeleton-cell { + overflow: hidden; + border-radius: 999px; + background: linear-gradient( + 90deg, + rgba(127, 156, 232, 0.12) 0%, + rgba(255, 186, 8, 0.12) 38%, + rgba(127, 156, 232, 0.18) 52%, + rgba(127, 156, 232, 0.12) 100% + ); + background-size: 360% 100%; + animation: studioSkeletonShimmer 1.7s ease-in-out infinite; +} + +.experiment-studio-page .studio-skeleton-line { + height: 12px; +} + +.experiment-studio-page .studio-skeleton-line.short { width: 28%; } +.experiment-studio-page .studio-skeleton-line.medium { width: 52%; } +.experiment-studio-page .studio-skeleton-line.long { width: 74%; } + +.experiment-studio-page .studio-skeleton-table { + display: grid; + grid-template-columns: 1.25fr repeat(3, minmax(72px, 0.6fr)); + gap: 12px; + width: 100%; +} + +.experiment-studio-page .studio-skeleton-cell { + height: 14px; +} + +.experiment-studio-page .studio-skeleton-cell.head { + height: 10px; + opacity: 0.7; +} + +.experiment-studio-page .studio-skeleton-chart { + display: flex; + align-items: end; + justify-content: center; + gap: 12px; + min-height: 170px; + padding: 18px 10px 0; + border-bottom: 1px solid rgba(43, 51, 233, 0.12); +} + +.experiment-studio-page .studio-skeleton-bar { + width: clamp(18px, 8vw, 52px); + border-radius: 8px 8px 2px 2px; +} + +.experiment-studio-page .empty-state-block { + position: relative; } .btn-cancel, @@ -256,8 +624,7 @@ select:required:valid { .btn-create:hover { background-color: var(--primary-dark); - transform: translateY(-1.5px); - box-shadow: 0 6px 12px rgba(43, 51, 233, 0.25); + box-shadow: var(--shadow-md); } /* Primary Action Buttons (Harsh Fixes) */ @@ -272,14 +639,14 @@ select:required:valid { border-radius: var(--radius-md); font-weight: 700; cursor: pointer; - transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); - box-shadow: 0 4px 12px rgba(43, 51, 233, 0.2); + transition: background-color 0.15s ease, box-shadow 0.15s ease; + box-shadow: var(--shadow-sm); + text-decoration: none; } .primary-action-btn:hover:not(:disabled) { background: var(--primary-dark); - transform: translateY(-2px); - box-shadow: 0 10px 20px rgba(43, 51, 233, 0.3); + box-shadow: var(--shadow-md); } .primary-action-btn:disabled { @@ -290,27 +657,15 @@ select:required:valid { box-shadow: none; } -.custom-validation-tooltip { - position: absolute; - background-color: #d32f2f; - color: white; - padding: 5px 10px; - border-radius: 4px; - font-size: 0.85em; - white-space: nowrap; - box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); - z-index: 1000; -} - .tooltip { position: fixed; pointer-events: none; - background: rgba(255, 255, 255, 0.92); - border: 1px solid rgba(0, 0, 0, 0.25); - border-radius: 6px; - box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15); - color: #111; - font-family: system-ui, sans-serif; + background: var(--card-bg); + border: 1px solid var(--border-color); + border-radius: var(--radius-md); + box-shadow: var(--shadow-md); + color: var(--text-main); + font-family: inherit; font-size: 12.5px; line-height: 1.45; padding: 8px 10px; @@ -319,7 +674,6 @@ select:required:valid { transform: translateY(4px); transition: opacity 0.15s ease, transform 0.15s ease; z-index: 200000; - backdrop-filter: blur(3px); } .tooltip.visible { @@ -339,305 +693,832 @@ select:required:valid { line-height: 1.4; } -/* GLOBAL MOBILE HEADER OVERRIDES */ -@media (max-width: 768px) { +/* Removed: "GLOBAL MOBILE HEADER OVERRIDES" (max-width 768). It hid .topbar/.brand/ + .header-logo/.login-icon — classes no template uses — plus the header's account slot, + which was display:none on phones, kept alive only by a competing !important in the + component. The header owns its own responsive rules. */ - header.topbar, - .topbar, - .topbar-inner, - .brand, - .account-pill, - .header-logo, - .header-account, - .user-icon, - .login-icon { - display: none !important; - } +.summary-workspace { + display: grid; + gap: 12px; +} + +.chart-search { + max-width: none; + margin: 10px 0 12px; } -/* Query Builder Theming - Global */ +.chart-note { + margin-bottom: 10px; +} + +.chart-container--workspace { + width: 100%; + max-width: none; + min-height: 420px; + box-sizing: border-box; +} -/* AND / OR row layout */ -.q-switch-row { +/* One card, two panes. The summary sub-steps used to render the shared class as + two separate bordered boxes 24px apart, which is what made them read as + unrelated surfaces. The preprocessing stations already had this shell. */ +.statistics-workspace { + display: grid; + grid-template-columns: minmax(240px, 300px) minmax(0, 1fr); + gap: 0; + align-items: stretch; + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-lg); + overflow: hidden; + background: var(--studio-card-bg); + box-shadow: var(--studio-card-shadow); + /* The cap belongs on the card, not the rail. Capping the rail left its tinted + background and divider stopping short of the card's bottom edge. */ + max-height: min(72vh, 760px); +} + +.statistics-browser { display: flex; - align-items: center; - gap: 10px; + flex-direction: column; + padding: 14px 16px; + min-height: 0; + border-right: 1px solid var(--studio-card-border); + background: var(--studio-card-header-bg); + overflow-y: auto; } -/* AND / OR switch labels */ -.q-switch-group .q-switch-label { - display: inline-flex; - align-items: center; - justify-content: center; - min-width: 52px; - padding: 4px 14px; - margin: 0 3px; - border-radius: 999px; - border: 1px solid var(--primary-color) !important; - background: linear-gradient(135deg, rgba(43, 51, 233, 0), rgba(43, 51, 233, 0.24)) !important; - font-size: 12px; - font-weight: 500; +/* Rail foot. The rail stretches to the table's height, so a short list left a + large blank inside the card's own border. Rather than pad it, this shows the + one number the per-variable table cannot: coverage re-pooled over the whole + selection. It sits directly under the list, not pinned to the card's bottom + edge — pinned, a five-variable list put ~320px of blank between the list and + the rule, which reads as a broken column. Trailing blank below a list reads + as a short list. */ +.statistics-browser-foot { + margin-top: 16px; + padding-top: 12px; + border-top: 1px solid var(--studio-card-border); +} + +.statistics-browser-foot-label { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); text-transform: uppercase; - letter-spacing: 0.04em; - cursor: pointer; - box-shadow: none !important; - color: var(--primary-color) !important; - transition: background 0.15s ease-in-out, color 0.15s ease-in-out, - border-color 0.15s ease-in-out, transform 0.08s ease-in-out; } -/* Active state (when radio is checked) */ -input.q-switch-radio:checked+.q-switch-label { - background: linear-gradient(135deg, #2b33e9, #1b21a3) !important; - color: white !important; - box-shadow: 0 0 6px rgba(43, 51, 233, 0.45); - border-color: var(--primary-dark) !important; +.statistics-browser-foot-value { + display: flex; + align-items: baseline; + gap: 8px; + margin-top: 3px; + font-variant-numeric: tabular-nums; } -.q-switch-group:hover .q-switch-label:hover { - background: var(--primary-color) !important; - color: #ffffff !important; - border-color: var(--primary-dark) !important; +.statistics-browser-foot-value strong { + font-size: 1.1rem; + font-weight: 700; } +.statistics-browser-foot-value small { + color: var(--text-muted); + font-size: 0.72rem; + line-height: 1.35; + text-wrap: pretty; +} -/* "+ Rule" / "+ Ruleset" buttons */ -.q-button-group button, -.q-button-group .q-button, -.q-button-group .btn { - background: #ffffff !important; - color: var(--primary-color) !important; - font-weight: 500; - border-radius: 999px; - padding: 4px 12px; - font-size: 12px; - border: 1px solid var(--primary-color) !important; +.statistics-panel { + min-width: 0; + min-height: 0; + padding: 20px; + overflow-y: auto; + background: transparent; + border: 0; box-shadow: none; - text-transform: none; - letter-spacing: 0.02em; - cursor: pointer; - transition: background 0.15s ease-in-out, color 0.15s ease-in-out, - transform 0.08s ease-in-out, border-color 0.15s ease-in-out; } -.q-button-group button:hover, -.q-button-group .q-button:hover, -.q-button-group .btn:hover { - background: var(--primary-color) !important; - color: #ffffff !important; - border-color: var(--primary-dark) !important; + + +.statistics-browser-header, +.statistics-panel-header, +.statistics-variable-group-title { + justify-content: space-between; +} + +.statistics-browser-header span, +.statistics-browser-header small, +.statistics-panel-kicker, +.statistics-variable-group-title { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; } -/* Rule row layout */ -.rule-container { +.statistics-variable-btn > span { + color: var(--text-main); + font-size: 0.86rem; + font-weight: 600; +} + +.statistics-variable-group { + display: grid; + gap: 4px; + margin-top: 16px; +} + +/* Rail row = name, then the one number the step is decided on, right-aligned. + A bordered card per row made the rail a stack of boxes instead of a list you + can scan; selection is a tonal fill plus a leading accent edge. */ +.statistics-variable-btn { display: flex; + flex-wrap: wrap; align-items: center; - gap: 10px; + gap: 8px; + row-gap: 5px; width: 100%; - justify-content: flex-start; - margin: 6px 0; + padding: 7px 10px; + border: 0; + border-left: 2px solid transparent; + border-radius: 0 var(--radius-md) var(--radius-md) 0; + background: transparent; + color: var(--text-main); + cursor: pointer; + text-align: left; + transition: background 0.14s var(--ease-standard), + border-color 0.14s var(--ease-standard); } -/* Field inputs */ -.rule-container select, -.rule-container input[type="text"], -.rule-container input[type="number"], -.rule-container input[type="date"], -.rule-container input[type="email"] { - background: #ffffff; - border: 1px solid #cbd5e1; - border-radius: 8px; - padding: 6px 10px; - font-size: 13px; - color: #111827; - min-width: 150px; - max-width: 240px; - box-shadow: none; +/* Missing-share bar: one full-width row under the variable name, so the rail + ranks coverage without adding a second number beside it. */ +.statistics-variable-bar { + flex: 1 0 100%; + height: 3px; + border-radius: 999px; + overflow: hidden; + background: rgba(43, 51, 233, 0.08); } -/* Rule actions container */ -.rule-actions { - margin-left: auto; +.statistics-variable-bar > span { + display: block; + height: 100%; + background: color-mix(in srgb, var(--primary-light) 70%, var(--text-muted)); +} + +.statistics-variable-bar[data-severity='warning'] > span { + background: var(--accent-color); +} + +.statistics-variable-bar[data-severity='high'] > span { + background: color-mix(in srgb, var(--danger) 75%, var(--text-main)); +} + +.statistics-variable-btn > span:first-of-type { + min-width: 0; + line-height: 1.35; + text-wrap: pretty; +} + +.statistics-variable-btn small { + color: var(--text-muted); + font-size: 0.68rem; + overflow-wrap: anywhere; +} + +.statistics-variable-meta { display: flex; align-items: center; - gap: 4px; + flex-wrap: wrap; + gap: 5px; + color: var(--text-muted); + font-size: 0.68rem; + line-height: 1.25; + margin-left: auto; + font-variant-numeric: tabular-nums; } -/* Delete Button as Icon */ -.rule-actions .btn-danger { - background: transparent !important; - border: none !important; - color: #dc2626 !important; - font-size: 16px; - cursor: pointer; - padding: 3px; +.statistics-variable-meta small { + color: inherit; + font: inherit; +} + +.statistics-variable-btn:hover { + border-left-color: color-mix(in srgb, var(--primary-color) 35%, transparent); + background: color-mix(in srgb, var(--primary-color) 5%, transparent); +} + +.statistics-variable-btn:hover > span { + color: color-mix(in srgb, var(--primary-color) 72%, var(--text-main)); +} + +.statistics-variable-btn.active { + border-left-color: var(--primary-color); + background: color-mix(in srgb, var(--primary-color) 9%, transparent); +} + +/* Selection must not rely on hue alone (1.4.1). The active row keeps --text-main + and carries the state in the edge and the fill. */ +.statistics-variable-btn.active > span:first-of-type { + font-weight: 700; +} + +.statistics-panel-header { + margin-bottom: 18px; +} + +.statistics-panel-header h4 { + margin: 2px 0 0; + color: var(--text-main); + font-size: 1rem; +} + +.statistics-panel-header small { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; +} + +.statistics-browser-header, +.statistics-panel-header, +.statistics-variable-group-title { display: flex; align-items: center; - justify-content: center; - transition: color 0.12s ease-in-out, transform 0.08s ease-in-out; + gap: 10px; +} + +/* The share a variable is judged on, right-aligned in the rail. Severity is + colour-only here by design: the rail is the one surface where the values are + compared against each other. In the table the row is labelled "Missing", so + the same red would fire on nearly every numeric variable and say nothing. */ +.statistics-variable-coverage, +.preprocessing-variable-coverage { + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 700; + white-space: nowrap; +} + +.statistics-variable-coverage[data-severity='warning'], +.statistics-browser-foot-value strong[data-severity='warning'] { + /* --accent-color is 1.9:1 as text, so it is deepened toward --text-main the + same way the state dots are. */ + color: color-mix(in srgb, var(--accent-color) 45%, var(--text-main)); +} + +.statistics-variable-coverage[data-severity='high'], +.preprocessing-variable-coverage[data-severity='high'], +.statistics-browser-foot-value strong[data-severity='high'] { + color: color-mix(in srgb, var(--danger) 58%, var(--text-main)); +} + +/* Share of Total, next to the Missing count it belongs to. */ +.statistics-cell-note { + margin-left: 6px; + color: var(--text-muted); + font-size: 0.72rem; + font-weight: 600; +} + +.statistics-table-wrapper { + padding: 0; + /* Vertical scrolling belongs to the panel; the wrapper only scrolls sideways + for the 620px minimum. Two nested vertical scrollbars in one card is worse + than either alone. */ + overflow-x: auto; + border: 1px solid var(--studio-card-border); + border-radius: var(--radius-md); + background: var(--card-bg); +} + +.statistics-table { + min-width: 620px; + border: 0; + border-radius: 0; } -.rule-actions .btn-danger:hover { - color: #b91c1c !important; - transform: scale(1.08); +.statistics-table th:first-child, +.statistics-table td:first-child { + position: sticky; + left: 0; + z-index: 1; + background: #ffffff; +} + +.statistics-table th:first-child { + z-index: 4; + background: var(--studio-card-header-bg); } -.rule-actions .btn-danger i { - font-size: 16px; +.statistics-table th:not(:first-child), +.statistics-table td:not(:first-child) { + text-align: right; } +.statistics-table td:not(:first-child) { + color: var(--text-main); + font-weight: 600; +} -/* Delete Button Redesign */ -.query-builder .btn-danger { - background: #ffe8e8 !important; - border-radius: 50% !important; - border: 1px solid #ffb7b7 !important; - width: 26px; - height: 26px; - padding: 0 !important; +.statistics-table td:first-child, +.statistics-table .metric-cell { + color: var(--text-muted); + font-weight: 500; +} + +/* The pooled column is a rollup over the datasets beside it, not a fourth + dataset. Without a divider it reads as one more of the same. */ +.statistics-table th.statistics-col-rollup, +.statistics-table td.statistics-col-rollup { + border-left: 1px solid var(--studio-card-border); +} + +.statistics-table th.statistics-col-rollup { + background: color-mix(in srgb, var(--primary-color) 6%, var(--card-bg)); +} + +.statistics-table td.statistics-col-rollup { + font-weight: 700; +} + +/* Per-dataset n under the header label, read from the Total row, so the + Missing % above the fold is readable without scrolling to the count. */ +.statistics-table th small { + display: block; + font-size: 0.68rem; + font-weight: 500; + text-transform: none; + letter-spacing: 0; +} +/* Metric groups: the three counts answer "how much data do I have", the rest + answer "what does it look like". One flat list of ten identical bold rows + answers neither. */ +.statistics-table tr.statistics-metric-group td { + position: static; + padding: 6px 12px 4px; + height: auto; + border: 0; + border-top: 1px solid var(--studio-card-border); + background: var(--studio-card-header-bg); + color: var(--text-muted); + font-size: 0.68rem; + font-weight: 700; + letter-spacing: var(--letter-spacing-config); + text-transform: uppercase; +} + +@media (max-width: 768px) { + .statistics-workspace { + grid-template-columns: 1fr; + max-height: none; + } + + .statistics-browser { + max-height: 320px; + border-right: 0; + border-bottom: 1px solid var(--studio-card-border); + } + + .statistics-panel { + padding: 16px; + } + + .statistics-panel-header { + align-items: stretch; + flex-direction: column; + } +} + +/* Preprocessing shell — unified step layout */ +.preprocessing-shell { display: flex; + flex-direction: column; + gap: 12px; +} + +/* Inside a station card the card is the only frame: the workspace drops its own + border/radius/shadow and the body's 14px padding, so the variable rail and the + evidence pane reach the card edge instead of nesting a third 12px box. */ +.station-card:has(.preprocessing-workspace) .station-card-body { + padding: 0; +} + +.station-card .preprocessing-workspace { + border: none; + border-radius: 0; + box-shadow: none; + background: transparent; + margin: 0; +} + +/* The preprocessing documentation rows wear the algorithm configuration panel's + disclosure, and they own their inset: the workspace card body has no padding of its + own (see .station-card:has(.preprocessing-workspace)), so the component styles carry + the 8px rather than a global override competing with them at equal specificity. */ + +/* Station status chip — shared by station-card headers, preprocessing detail headers and variable rows. */ +.preprocessing-state-mini, +.preprocessing-state-chip { + display: inline-flex; align-items: center; justify-content: center; - font-size: 14px !important; - color: #c62828 !important; - transition: transform 0.15s ease, background 0.2s ease; + border: 0; + border-radius: 999px; + background: rgba(248, 250, 252, 0.88); + color: #475569; + font-weight: 600; + line-height: 1; + letter-spacing: var(--letter-spacing-config); + text-transform: none; } -.query-builder .btn-danger:hover { - background: #ffb7b7 !important; - transform: scale(1.15); +.preprocessing-state-mini { + padding: 3px 7px; + font-size: 0.68rem; + flex-shrink: 0; } -/* Make the X icon aligned */ -.query-builder .btn-danger i { - font-size: 14px; - line-height: 1; +.preprocessing-state-chip { + padding: 2px 9px; + font-size: 0.72rem; + flex-shrink: 0; +} + +.preprocessing-state-mini.state-pending, +.preprocessing-state-chip.state-pending { + background: color-mix(in srgb, var(--accent-color) 16%, white); + color: #78350f; } +.preprocessing-state-mini.state-applied, +.preprocessing-state-chip.state-applied { + background: color-mix(in srgb, var(--success) 12%, white); + color: #065f46; +} -/* Card around rules */ -.query-builder .q-row.q-connector { - border: 1px solid #d6e7e7 !important; - background: #f8fcfc !important; - border-radius: 10px !important; - padding: 8px 12px !important; - margin: 8px 0 !important; - box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04); +.preprocessing-state-mini.state-default, +.preprocessing-state-chip.state-default { + background: color-mix(in srgb, var(--primary-color) 8%, white); + color: var(--primary-color, #2B33E9); } -/* Wrapper of X (align right) */ -.query-builder .q-rule-actions { - margin-left: auto; +.preprocessing-workspace { + margin-top: 0; + /* Only the column width is local — the shared .statistics-workspace rule now + owns the single-card shell, the tonal rail pane and the transparent panel. */ + grid-template-columns: minmax(224px, 288px) minmax(0, 1fr); +} + +.preprocessing-workspace .statistics-panel, +.preprocessing-workspace .preprocessing-detail-panel, +.preprocessing-workspace .preprocessing-detail-column { + padding: 16px; +} + +.preprocessing-workspace .statistics-panel-header { + margin-bottom: 12px; + padding-bottom: 12px; + border-bottom: 1px solid var(--studio-card-border); +} + +.preprocessing-workspace .statistics-panel-header .preprocessing-state-chip { + display: none; +} + +.outlier-preprocessing-workspace .preprocessing-detail-column { display: flex; + flex-direction: column; + gap: 0; + min-width: 0; + padding: 0; +} + +.outlier-preprocessing-workspace .preprocessing-detail-panel { + padding: 16px; +} + +@media (max-width: 768px) { + .preprocessing-workspace { + grid-template-columns: 1fr; + } + + .preprocessing-workspace .statistics-panel-header .preprocessing-state-chip { + display: inline-flex; + } +} + +.documentation-panel { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 8px; + margin: 0 0 14px; + border: 0; + background: transparent; + box-shadow: none; + overflow: visible; +} + +.documentation-panel summary { + list-style: none; +} + +.documentation-panel summary::-webkit-details-marker { + display: none; +} + +.documentation-summary { + display: inline-flex; + width: fit-content; + max-width: 100%; + padding: 0; + cursor: pointer; + user-select: none; +} + +.documentation-label { + font-family: inherit; + font-size: 0.78rem; + font-weight: 600; + letter-spacing: 0.02em; + color: #334155; + white-space: nowrap; +} + +.documentation-chevron { + font-size: 0.62rem; + color: #64748b; + transition: transform 0.24s var(--ease-standard); +} + +.documentation-panel[open] .documentation-chevron { + transform: rotate(180deg); + color: var(--primary-color); +} + +.documentation-panel[open] .documentation-label { + color: color-mix(in srgb, var(--primary-color, #2b33e9) 78%, #1e293b); +} + +.documentation-content { + width: 100%; + box-sizing: border-box; + padding: 14px 16px 16px; + border: 0; + border-radius: 12px; + background: linear-gradient(180deg, rgba(248, 251, 255, 0.98) 0%, rgba(255, 255, 255, 0.96) 100%); + box-shadow: none; + color: #334155; + font-size: 0.875rem; + line-height: 1.65; + letter-spacing: 0.004em; + white-space: pre-wrap; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +.experiment-panel-workspace { + display: grid; + grid-template-columns: minmax(0, 1fr); + gap: 20px; + padding: 14px; + align-items: start; +} + +.experiment-panel-main { + min-width: 0; +} + +.experiment-panel-workspace--result { + grid-template-columns: minmax(0, 1fr) minmax(260px, 320px); + align-items: start; +} + +.experiment-panel-workspace--result .result-section { + margin: 0; + min-width: 0; +} + +.result-section-title { + margin: 0; + font-size: 1.02rem; + font-weight: 700; + color: var(--text-main); + line-height: 1.3; + min-width: 0; +} + +.result-section-body { + min-width: 0; +} + +.result-section-body app-algorithm-result { + display: block; +} + +.algorithm-requirement-action { + display: inline-flex; align-items: center; gap: 6px; + margin: 0; + padding: 5px 10px; + border: 1px solid rgba(146, 64, 14, 0.35); + border-radius: 999px; + background: rgba(255, 255, 255, 0.88); + font: inherit; + font-size: 0.76rem; + font-weight: 700; + color: inherit; + text-decoration: none; + cursor: pointer; + white-space: nowrap; + transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease; +} + +.algorithm-requirement-action i { + font-size: 0.68rem; +} + +.algorithm-requirement-action:hover { + background: #fff; + border-color: rgba(146, 64, 14, 0.55); + transform: translateY(-1px); +} + +.algorithm-requirement-action:focus-visible { + outline: 2px solid currentColor; + outline-offset: 2px; } -/* X (delete button) */ -.query-builder .q-row.q-connector .q-rule-actions>button.q-button.q-remove-button { - background: #ffe8e8 !important; - border-radius: 50% !important; - border: 1px solid #ffb7b7 !important; - width: 26px; - height: 26px; - padding: 0 !important; - display: flex !important; +/* STUDIO UNIFIED EXPORT CONTROLS */ +.studio-export-btn { + appearance: none; + -webkit-appearance: none; + display: inline-flex; align-items: center; justify-content: center; - font-size: 14px !important; - color: #c62828 !important; - box-shadow: none !important; + gap: 6px; + min-height: 26px; + padding: 4px 10px; + border: 1px solid rgba(127, 156, 232, 0.35); + border-radius: 6px; + background: rgba(43, 51, 233, 0.06); + box-shadow: none; + color: var(--primary-color, #2b33e9); cursor: pointer; - transition: - transform 0.15s ease, - background 0.2s ease, - color 0.2s ease; + font: inherit; + font-size: 0.72rem; + font-weight: 600; + letter-spacing: 0.02em; + line-height: 1; + white-space: nowrap; + transition: color 0.16s ease, background-color 0.16s ease, border-color 0.16s ease, transform 0.12s ease; } -.query-builder .q-row.q-connector .q-rule-actions>button.q-button.q-remove-button:hover { - background: #ffb7b7 !important; - color: #7f1d1d !important; - transform: scale(1.12); +.studio-export-btn i { + font-size: 0.75rem; + color: inherit; } -/* Icon X in the button */ -.query-builder .q-row.q-connector .q-rule-actions>button.q-button.q-remove-button i.q-remove-icon { - font-size: 14px; - line-height: 1; +.studio-export-btn:hover:not(:disabled), +.studio-export-btn[aria-expanded="true"]:not(:disabled) { + background: rgba(43, 51, 233, 0.1); + border-color: rgba(43, 51, 233, 0.45); + color: var(--primary-color, #2b33e9); + outline: none; + box-shadow: none; } -button.q-button.q-remove-button { - background: #ffe8e8 !important; - border-radius: 50% !important; - border: 1px solid #ffb7b7 !important; - width: 26px !important; - height: 26px !important; - padding: 0 !important; - display: flex !important; - align-items: center; - justify-content: center; - color: #c62828 !important; - font-size: 14px !important; - box-shadow: none !important; - transition: transform 0.15s ease, background 0.2s ease, color 0.2s ease !important; +.studio-export-btn:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 1px; } -button.q-button.q-remove-button:hover { - background: #ffb7b7 !important; - color: #7f1d1d !important; - transform: scale(1.12) !important; +.studio-export-btn:active:not(:disabled) { + transform: scale(0.98); } -button.q-button.q-remove-button i.q-remove-icon { - font-size: 14px !important; - line-height: 1 !important; +.studio-export-btn:disabled { + opacity: 0.4; + cursor: not-allowed; + transform: none; } -/* Rule row compact card */ +.studio-export-btn:disabled i { + color: inherit; +} -li.q-row.q-connector { - padding: 6px 10px !important; - margin: 6px 0 !important; - border-radius: 10px !important; - border: 1px solid #d6e7e7 !important; - background: #f8fcfc !important; - box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04); +.studio-export-menu { + position: absolute; + top: calc(100% + 4px); + right: 0; + z-index: 4; + min-width: 100%; + display: flex; + flex-direction: column; + padding: 3px; + border: 1px solid rgba(127, 156, 232, 0.35); + border-radius: 6px; + background: #ffffff; + box-shadow: 0 4px 12px rgba(43, 51, 233, 0.12); } -/* rule flex & centered */ -li.q-row.q-connector>div.q-rule { +.studio-export-menu-item { + appearance: none; + -webkit-appearance: none; display: flex; align-items: center; + gap: 6px; width: 100%; - background: none; + min-height: 24px; + padding: 0 8px; + border: none; + border-radius: 4px; + background: transparent; + color: #475569; + cursor: pointer; + font: inherit; + font-size: 0.72rem; + font-weight: 600; + letter-spacing: 0.02em; + line-height: 1; + white-space: nowrap; + transition: color 0.16s ease, background-color 0.16s ease; } -/* Left side: field / operator / value */ -li.q-row.q-connector .q-rule-content { - display: flex; - align-items: center; - gap: 8px; - flex: 1; +.studio-export-menu-item i { + font-size: 0.72rem; + color: inherit; } -/* Remove margins around controls */ -li.q-row.q-connector .q-rule-content .q-control-size { - margin: 0 !important; +.studio-export-menu-item:hover, +.studio-export-menu-item:focus-visible { + background: rgba(43, 51, 233, 0.08); + color: var(--primary-color, #2b33e9); + outline: none; } -/* X align */ -li.q-row.q-connector .q-rule-actions { - margin-left: 8px; - display: flex; + +@media (max-width: 1100px) { + .experiment-panel-workspace { + grid-template-columns: minmax(0, 1fr); + } + + .experiment-panel-main { + order: 1; + } +} + +/* The segmented tray recipe shared by the Data Handling overlays and the + Algorithm catalog filter: one surface, one active white pill. */ +.modal-tabs { + display: inline-flex; align-items: center; + gap: 2px; + padding: 3px; + background: rgba(148, 163, 184, 0.12); + border: 0; + box-shadow: inset 0 0 0 1px var(--border-color); + border-radius: 999px; + margin: 0; } -li.q-row.q-connector select, -li.q-row.q-connector input[type="text"], -li.q-row.q-connector input[type="number"], -li.q-row.q-connector input[type="date"], -li.q-row.q-connector input[type="email"] { - height: 30px; - padding: 4px 8px; - font-size: 13px; -} \ No newline at end of file +.modal-tabs button { + display: inline-flex; + align-items: center; + justify-content: center; + gap: 6px; + border: 0; + border-radius: 999px; + min-height: 28px; + padding: 0 12px; + background: transparent; + color: var(--text-muted); + font-family: inherit; + font-weight: 600; + font-size: 0.78rem; + cursor: pointer; + text-transform: none; + letter-spacing: 0.01em; + transition: + background 0.2s ease, + color 0.2s ease, + box-shadow 0.2s ease; +} + +.modal-tabs button:hover { + background: rgba(148, 163, 184, 0.1); + color: var(--primary-color); +} + +.modal-tabs button.active { + background: var(--card-bg); + color: var(--primary-color); + box-shadow: var(--shadow-sm); +} + +.modal-tabs button:focus-visible { + outline: 2px solid var(--color-focus); + outline-offset: 2px; +} diff --git a/src/test-setup.ts b/src/test-setup.ts new file mode 100644 index 000000000..f2a23a60d --- /dev/null +++ b/src/test-setup.ts @@ -0,0 +1,28 @@ +const noisyMessagePatterns = [ + /^No algorithm selected\.?$/, + /^✘ mock_algo:/, + /^✘ mock_notblank_algo:/, +]; + +function isNoisyMessage(args: unknown[]): boolean { + if (!args.length) return false; + const first = args[0]; + if (typeof first !== 'string') return false; + return noisyMessagePatterns.some((pattern) => pattern.test(first)); +} + +const originalWarn = console.warn.bind(console); +const originalError = console.error.bind(console); + +console.warn = (...args: unknown[]) => { + if (isNoisyMessage(args)) return; + originalWarn(...args); +}; + +console.error = (...args: unknown[]) => { + if (isNoisyMessage(args)) return; + originalError(...args); +}; + +// Restore vi.spyOn spies after each test, so one spec's stubs never leak into the next. +afterEach(() => vi.restoreAllMocks()); diff --git a/tsconfig.app.json b/tsconfig.app.json index 5ed8ead73..49ca737f7 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -4,7 +4,6 @@ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "./out-tsc/app", - "types": ["jquery"], }, "files": [ "src/main.ts" diff --git a/tsconfig.json b/tsconfig.json index dda3d01bf..618169ed9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,8 @@ "noPropertyAccessFromIndexSignature": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, + "noUnusedLocals": true, + "noUnusedParameters": true, "skipLibCheck": true, "isolatedModules": true, "esModuleInterop": true, diff --git a/tsconfig.spec.json b/tsconfig.spec.json index 5fb748d92..7b43a9faa 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -5,11 +5,12 @@ "compilerOptions": { "outDir": "./out-tsc/spec", "types": [ - "jasmine" + "vitest/globals" ] }, "include": [ "src/**/*.spec.ts", + "src/test-setup.ts", "src/**/*.d.ts" ] }