Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const DEFAULT_CASCADE_PARAMS = {
export const ALL = '*';
export const URL_REGISTRY_DOC_LINK = 'https://docs.openconceptlab.org/en/latest/oclapi/apireference/urlregistry.html'
export const MATCHING_ALGORITHMS_DOC_LINK = 'https://docs.openconceptlab.org/en/latest/oclmap/matching-algorithms.html'
export const MAPPER_DOCS_LINK = 'https://docs.openconceptlab.org/en/latest/oclmap/overview.html'
export const MAPPER_ANNOUNCEMENT_LETTER_URL = 'https://openconceptlab.org/blog/letter-to-the-ocl-community?utm_source=oclmap&utm_medium=banner&utm_campaign=subscription_dates'
export const MAPPER_ANNOUNCEMENT_PRICING_URL = 'https://openconceptlab.org/pricing?utm_source=oclmap&utm_medium=banner&utm_campaign=subscription_dates'
export const TERMS_OF_USE_URL = 'https://openconceptlab.org/terms-of-use'
Expand Down
13 changes: 13 additions & 0 deletions src/components/map-projects/ColumnMapTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import Autocomplete from '@mui/material/Autocomplete'
import TextField from '@mui/material/TextField'
import Switch from '@mui/material/Switch'
import ListItemText from '@mui/material/ListItemText'
import Typography from '@mui/material/Typography'
import ValidIcon from '@mui/icons-material/CheckCircleOutlined';
import InvalidIcon from '@mui/icons-material/CancelOutlined';
import UnmappedIcon from '@mui/icons-material/RemoveCircleOutlineOutlined';
import { isCodeLikeHeader } from './columnSuggestions'
import find from 'lodash/find';
import map from 'lodash/map';
import omit from 'lodash/omit';
Expand Down Expand Up @@ -130,6 +133,8 @@ const ColumnMapTable = ({
{
map(columns, (column, position) => {
const isValidColumn = isValid(column.label)
// Left unmapped on purpose (cleared, or a code-like header): no error.
const isDeliberatelyUnmapped = !isValidColumn && (column.unmapped || isCodeLikeHeader(column.original))
const isHidden = columnVisibilityModel[column.dataKey] === false
const sendToAIAssistant = AIAssistantColumns[column.dataKey] === false
return (
Expand All @@ -139,6 +144,8 @@ const ColumnMapTable = ({
{
isValidColumn ?
<ValidIcon sx={{marginRight: '8px'}} fontSize='small' color='success' /> :
isDeliberatelyUnmapped ?
<UnmappedIcon sx={{marginRight: '8px'}} fontSize='small' color='disabled' /> :
<InvalidIcon sx={{marginRight: '8px'}} fontSize='small' color='error' />
}
{column.original}
Expand All @@ -153,6 +160,12 @@ const ColumnMapTable = ({
isValid={isValidColumn}
onChange={(event, value) => onValChange(position, value?.label || value)}
/>
{
column.suggested &&
<Typography variant='caption' color='text.secondary' sx={{display: 'block'}}>
{t('map_project.column_suggested')}
</Typography>
}
{
'Mapping: List' === column.label &&
<div>
Expand Down
9 changes: 7 additions & 2 deletions src/components/map-projects/ConfigurationForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import omit from 'lodash/omit'
import filter from 'lodash/filter'

import { formatDate, getCurrentUser, toV3URL } from '../../common/utils'
import { MATCHING_ALGORITHMS_DOC_LINK } from '../../common/constants'
import { MATCHING_ALGORITHMS_DOC_LINK, MAPPER_DOCS_LINK } from '../../common/constants'
import { COLORS } from '../../common/colors'
import { getProjectConfigErrors } from './algorithms'
import NamespaceDropdown from '../common/NamespaceDropdown'
Expand Down Expand Up @@ -490,7 +490,12 @@ const ConfigurationForm = ({ project, handleFileUpload, file, owner, setOwner, n
{t('map_project.field_configuration')}
</Typography>
<FormHelperText sx={{marginTop: 0}}>
{t('map_project.field_configuration_description')}
<Trans
i18nKey='map_project.field_configuration_description'
components={[
<a key='learn-more' className='link' href={MAPPER_DOCS_LINK} target='_blank' rel='noopener noreferrer' />
]}
/>
</FormHelperText>
<ColumnMapTable
sx={{marginTop: '12px'}}
Expand Down
29 changes: 22 additions & 7 deletions src/components/map-projects/MapProject.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import { buildPanelPayload } from './openConceptPanel'
import LoaderDialog from '../common/LoaderDialog'
import Error403 from '../errors/Error403'
import { HEADERS, SEMANTIC_SEARCH_HEADERS, ROW_STATES, VIEWS, DECISION_TABS, ROW_STAGES, PROMPTS_KEY_DEFAULT, PROMPTS_ACTION_TYPE_DEFAULT, SCORES_COLOR } from './constants'
import { updateColumnState, resuggestColumns } from './columnSuggestions'
import MapProjectDeleteConfirmDialog from './MapProjectDeleteConfirmDialog';
import ConfigurationForm from './ConfigurationForm'
import Controls from './Controls'
Expand Down Expand Up @@ -719,6 +720,13 @@ const MapProject = () => {
return {id: `property__${_filter.code}`, label: `Property: ${_filter.code}`, description: _filter.description || ''}
}))]

// The target repo can load after the file (and so its properties), so
// re-suggest columns the user has not edited yet (ocl_issues#2907).
const headersKey = map(headers, 'label').join('|')
React.useEffect(() => {
setColumns(prev => resuggestColumns(prev, headers))
}, [headersKey])

React.useEffect(() => {
if(!isEmpty(decisions)) {
window.addEventListener("beforeunload", alertUser);
Expand Down Expand Up @@ -857,7 +865,8 @@ const MapProject = () => {
setProjectFromData(data, response.data)
setAlgosSelected(response?.data?.algorithms || [])
if (response?.data.columns?.length > 0) {
const _columns = response.data.columns.map(col => ({...omit(col, ['hidden'])}))
// Saved mappings are the user's, so they are never re-suggested.
const _columns = response.data.columns.map(col => ({...omit(col, ['hidden', 'auto', 'suggested'])}))
setColumns(_columns)
setTargetSourcesFromRows(getTargetSourcesFromRows(_columns, data))
let AIAssistantColVisibility = {}
Expand Down Expand Up @@ -1311,11 +1320,16 @@ const MapProject = () => {
}

const updateColumn = (position, newValue, key) => {
let cols = {...columns}
cols[position][key || 'label'] = newValue || ''
if(key === 'targetSource') {
// Each source picker sets its value on mount, often several in one render,
// so merge into the latest state; this render's copy would keep only the last.
setColumns(prev => updateColumnState(prev, position, {...prev[position]?.targetSource, ...newValue}, key))
return
}
// A manual choice takes its field back from any suggestion holding it.
const cols = resuggestColumns(updateColumnState(columns, position, newValue, key), headers)
setColumns(cols)
if(key !== 'targetSource')
setTargetSourcesFromRows(getTargetSourcesFromRows(cols))
setTargetSourcesFromRows(getTargetSourcesFromRows(cols))
}

const resetState = () => {
Expand Down Expand Up @@ -1493,8 +1507,9 @@ const MapProject = () => {
return prev
})

let cols = getColumns(omit(_data[0], ['__index']))
setColumns(cols)
// Uploaded columns start as suggestions (`auto`) until the user edits them.
const cols = map(getColumns(omit(_data[0], ['__index'])), col => ({...col, auto: true}))
setColumns(resuggestColumns(cols, headers))
}

const getReferencesForImport = (collection, scope, cascadeMethod, transformReferences) => {
Expand Down
160 changes: 160 additions & 0 deletions src/components/map-projects/__tests__/columnSuggestions.test.js
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', () => {
Comment thread
snyaggarwal marked this conversation as resolved.
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')
})
Comment thread
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'])
})
98 changes: 98 additions & 0 deletions src/components/map-projects/columnSuggestions.js
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)
}
Loading
Loading