Repository navigation
OpenConceptLab/ocl_issues#2907 | suggesting field mapping for column headers #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10e5fa0
OpenConceptLab/ocl_issues#2907 | suggesting field mapping for column …
snyaggarwal 74777ce
OpenConceptLab/ocl_issues#2907 | keep Field Configuration columns an …
snyaggarwal 14e174c
OpenConceptLab/ocl_issues#2907 | reserve fields before suggesting, so…
snyaggarwal d55a640
Apply batched suggestions from code review
snyaggarwal fb150ab
OpenConceptLab/ocl_issues#2907 | review feedback
snyaggarwal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/components/map-projects/__tests__/columnSuggestions.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /** | ||
| * Suggested mapped fields for uploaded column headers (ocl_issues#2907). | ||
| * | ||
| * Run with: npm test | ||
| */ | ||
|
|
||
| import test from 'node:test' | ||
| import assert from 'node:assert/strict' | ||
|
|
||
| import { suggestColumnLabels, isCodeLikeHeader, updateColumnState, resuggestColumns } from '../columnSuggestions.js' | ||
|
|
||
| const BASE = [ | ||
| {label: 'ID'}, | ||
| {label: 'Name'}, | ||
| {label: 'Synonyms'}, | ||
| {label: 'Description'}, | ||
| {label: 'Mapping: Code'}, | ||
| {label: 'Mapping: List'}, | ||
| {label: 'Property: Class'}, | ||
| {label: 'Property: Datatype'}, | ||
| ] | ||
| const LOINC = [...BASE, {label: 'Property: SYSTEM'}] | ||
|
|
||
| test('suggestColumnLabels: term, test and description headers suggest Name, once', () => { | ||
| assert.deepEqual( | ||
| suggestColumnLabels(['Test Name', 'Term', 'Lab test', 'Test description'], BASE), | ||
| ['Name', null, null, null] | ||
| ) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: Name goes to the strongest header, not the first', () => { | ||
| assert.deepEqual( | ||
| suggestColumnLabels(['Test Date', 'Test Description', 'Test Name'], BASE), | ||
| [null, null, 'Name'] | ||
| ) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: exact matches are left alone', () => { | ||
| assert.deepEqual(suggestColumnLabels(['Name', 'Description'], BASE), [null, null]) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: synonym headers suggest Synonyms', () => { | ||
| assert.deepEqual(suggestColumnLabels(['Alternate synonym'], BASE), ['Synonyms']) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: specimen, system and sample suggest Property: SYSTEM when the target has it', () => { | ||
| assert.deepEqual( | ||
| suggestColumnLabels(['Specimen', 'System', 'Sample type'], LOINC), | ||
| ['Property: SYSTEM', null, null] | ||
| ) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: no Property: SYSTEM suggestion when the target lacks it', () => { | ||
| assert.deepEqual(suggestColumnLabels(['Specimen'], BASE), [null]) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: class headers are left alone, since they are already recognised', () => { | ||
| assert.deepEqual(suggestColumnLabels(['Class', 'Lab class'], BASE), [null, null]) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: code-like headers stay unmapped', () => { | ||
| assert.deepEqual( | ||
| suggestColumnLabels(['Local Code', 'Sample ID', 'external_id', 'UUID'], LOINC), | ||
| [null, null, null, null] | ||
| ) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: unrelated headers get no suggestion', () => { | ||
| assert.deepEqual(suggestColumnLabels(['Notes', 'Units'], LOINC), [null, null]) | ||
| }) | ||
|
|
||
| test('isCodeLikeHeader: matches code-like words as whole tokens', () => { | ||
| assert.equal(isCodeLikeHeader('Local Code'), true) | ||
| assert.equal(isCodeLikeHeader('ID'), true) | ||
| assert.equal(isCodeLikeHeader('Identity'), false) | ||
| assert.equal(isCodeLikeHeader('Test Name'), false) | ||
| }) | ||
|
|
||
| // Mirrors the upload path: every column starts as an auto suggestion. | ||
| const upload = (headers, validColumns = BASE) => resuggestColumns( | ||
| headers.map(original => ({label: original, original, dataKey: original, auto: true})), | ||
| validColumns | ||
| ) | ||
|
|
||
| test('suggestColumnLabels: an exact match keeps its field, even when a pattern column comes first', () => { | ||
| assert.deepEqual(suggestColumnLabels(['Test Name', 'Name'], BASE), [null, null]) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: exact matches reserve their fields in mixed headers', () => { | ||
| assert.deepEqual( | ||
| suggestColumnLabels(['Name', 'Test Name', 'Synonyms', 'Alternate synonym'], BASE), | ||
| [null, null, null, null] | ||
| ) | ||
| }) | ||
|
|
||
| test('suggestColumnLabels: labels the user already chose are reserved', () => { | ||
| assert.deepEqual(suggestColumnLabels(['Test Name'], BASE, ['Name']), [null]) | ||
| }) | ||
|
|
||
| test('upload: an exact Name column and a Test Name column do not both map to Name', () => { | ||
| const labels = upload(['Test Name', 'Name', 'Specimen'], LOINC).map(col => col.label) | ||
| assert.deepEqual(labels, ['Test Name', 'Name', 'Property: SYSTEM']) | ||
| }) | ||
|
|
||
| test('upload: a suggestion still applies when nothing collides', () => { | ||
| const labels = upload(['Test Name', 'Synonym'], BASE).map(col => col.label) | ||
| assert.deepEqual(labels, ['Name', 'Synonyms']) | ||
| }) | ||
|
|
||
| test('regression: a manual Name choice is not duplicated when the target reloads', () => { | ||
| let cols = upload(['Notes', 'Test Name'], LOINC) | ||
| cols = updateColumnState(cols, 0, 'Name') | ||
| const afterLoad = resuggestColumns(cols, LOINC) | ||
| assert.deepEqual(afterLoad.map(col => col.label), ['Name', 'Test Name']) | ||
| assert.equal(afterLoad[1].suggested, false) | ||
| }) | ||
|
|
||
| test('updateColumnState: keeps columns an array after a user edit', () => { | ||
| const cols = updateColumnState(upload(['Code', 'Name']), 0, 'Name') | ||
| assert.ok(Array.isArray(cols)) | ||
| assert.equal(cols[0].label, 'Name') | ||
| assert.equal(cols[0].auto, false) | ||
| }) | ||
|
|
||
| test('regression: editing a column, then the target loads, does not throw and keeps the edit', () => { | ||
| let cols = upload(['Specimen', 'Code']) | ||
| cols = updateColumnState(cols, 1, '') | ||
| assert.equal(cols[1].unmapped, true) | ||
| const afterLoad = resuggestColumns(cols, LOINC) | ||
| assert.ok(Array.isArray(afterLoad)) | ||
| assert.equal(afterLoad[0].label, 'Property: SYSTEM') | ||
| assert.equal(afterLoad[0].suggested, true) | ||
| assert.equal(afterLoad[1].label, '') | ||
| assert.equal(afterLoad[1].unmapped, true) | ||
| }) | ||
|
|
||
| test('resuggestColumns: a manual choice is not overwritten when the target changes', () => { | ||
| let cols = upload(['Specimen']) | ||
| cols = updateColumnState(cols, 0, 'Name') | ||
| assert.equal(resuggestColumns(cols, LOINC)[0].label, 'Name') | ||
| }) | ||
|
|
||
| test('resuggestColumns: suggests Property: SYSTEM once the target loads it', () => { | ||
| const cols = upload(['Specimen']) | ||
| assert.equal(resuggestColumns(cols, BASE)[0].label, 'Specimen') | ||
| assert.equal(resuggestColumns(cols, LOINC)[0].label, 'Property: SYSTEM') | ||
| }) | ||
|
snyaggarwal marked this conversation as resolved.
|
||
|
|
||
| test('resuggestColumns: a cleared column does not hold the field it would have been suggested', () => { | ||
| let cols = upload(['Specimen', 'Sample']) | ||
| cols = updateColumnState(cols, 0, '') | ||
| assert.deepEqual(resuggestColumns(cols, LOINC).map(col => col.label), ['', 'Property: SYSTEM']) | ||
| }) | ||
|
|
||
| test('resuggestColumns: a manual choice takes its field back from a suggestion', () => { | ||
| let cols = upload(['Notes', 'Test Name']) | ||
| assert.equal(cols[1].label, 'Name') | ||
| cols = resuggestColumns(updateColumnState(cols, 0, 'Name'), BASE) | ||
| assert.deepEqual(cols.map(col => col.label), ['Name', 'Test Name']) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * Suggested mapped fields for the columns of an uploaded spreadsheet | ||
| * (ocl_issues#2907). | ||
| * | ||
| * A column is only recognised when its header is exactly a mapped-field | ||
| * label, so a new user has to learn that vocabulary before the first run. | ||
| * These suggestions map common header words to a mapped field when the | ||
| * header is not already an exact match. They are only a starting point: they | ||
| * are shown as suggestions and can be changed, and nothing is saved until the | ||
| * project is. | ||
| * | ||
| * Code-like headers (for example "Local Code") stay unmapped on purpose. | ||
| * Headers containing "class" are already recognised as Property: Class by | ||
| * MapProject's isValidColumnValue, so they get no suggestion here. | ||
| */ | ||
|
|
||
| const CODE_TOKENS = ['code', 'codes', 'id', 'ids', 'uuid', 'identifier'] | ||
|
|
||
| const SUGGESTION_RULES = [ | ||
| {label: 'Name', tokens: ['name', 'term', 'test', 'description']}, | ||
| {label: 'Synonyms', tokens: ['synonym', 'synonyms']}, | ||
| {label: 'Property: SYSTEM', tokens: ['specimen', 'system', 'sample']}, | ||
| ] | ||
|
|
||
| const tokenize = header => String(header || '').toLowerCase().split(/[\s_-]+/).filter(Boolean) | ||
|
|
||
| export const isCodeLikeHeader = header => tokenize(header).some(token => CODE_TOKENS.includes(token)) | ||
|
|
||
| /** | ||
| * Returns one suggested label per header, aligned with `headers`, or null | ||
| * where there is no suggestion. `validColumns` are the mapped fields the | ||
| * target supports (`{label}` objects, as in MapProject's `headers`). A rule | ||
| * only suggests a field the target has, and each field is suggested at most | ||
| * once, for the first matching column. | ||
| */ | ||
| export const suggestColumnLabels = (headers, validColumns, reserved = []) => { | ||
| const findValid = label => validColumns.find(column => column.label.toLowerCase() === label.toLowerCase()) | ||
| const isExact = header => Boolean(findValid(String(header || ''))) | ||
|
|
||
| // Exact matches and labels the user already chose hold their field first, so | ||
| // no suggestion can duplicate it, wherever the column sits in the file. | ||
| const taken = new Set(reserved) | ||
| headers.forEach(header => { | ||
| const exactMatch = findValid(String(header || '')) | ||
| if(exactMatch) | ||
| taken.add(exactMatch.label) | ||
| }) | ||
|
|
||
| const matches = headers.map((header, idx) => { | ||
| const alreadyRecognised = String(header || '').toLowerCase().includes('class') | ||
| if(isExact(header) || alreadyRecognised || isCodeLikeHeader(header)) | ||
| return null | ||
|
|
||
| const tokens = tokenize(header) | ||
| const rule = SUGGESTION_RULES.find(rule => tokens.some(token => rule.tokens.includes(token))) | ||
| return rule ? {idx, rule, rank: rule.tokens.findIndex(token => tokens.includes(token))} : null | ||
| }) | ||
|
|
||
| // A field goes to the column with its strongest word ("name" before "test"), | ||
| // then the first such column, so "Test Name" wins over an earlier "Test Date". | ||
| const suggestions = headers.map(() => null) | ||
| matches.filter(Boolean).sort((a, b) => a.rank - b.rank || a.idx - b.idx).forEach(({idx, rule}) => { | ||
| const target = findValid(rule.label) | ||
| if(target && !taken.has(target.label)) { | ||
| taken.add(target.label) | ||
| suggestions[idx] = target.label | ||
| } | ||
| }) | ||
| return suggestions | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new columns array with one column's mapping changed. A change | ||
| * without a key is a mapping choice: it ends any suggestion, and clearing it | ||
| * marks the column as unmapped on purpose. | ||
| */ | ||
| export const updateColumnState = (columns, position, newValue, key) => { | ||
| const cols = [...columns] | ||
| cols[position] = {...cols[position], [key || 'label']: newValue || ''} | ||
| if(!key) { | ||
| cols[position].suggested = false | ||
| cols[position].unmapped = !newValue | ||
| cols[position].auto = false | ||
| } | ||
| return cols | ||
| } | ||
|
|
||
| /** | ||
| * Re-runs suggestions for columns still driven by suggestions (`auto`) after | ||
| * the target's mapped fields change. Columns the user has edited are kept. | ||
| */ | ||
| export const resuggestColumns = (columns, validColumns) => { | ||
| if(!columns.some(col => col.auto)) | ||
| return columns | ||
| const reserved = columns.filter(col => !col.auto && col.label).map(col => col.label) | ||
| const suggestedLabels = suggestColumnLabels(columns.map(col => col.auto ? col.original : null), validColumns, reserved) | ||
| return columns.map((col, idx) => col.auto ? {...col, label: suggestedLabels[idx] || col.original, suggested: Boolean(suggestedLabels[idx])} : col) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.