Skip to content
Closed
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
122 changes: 122 additions & 0 deletions ui-react/src/lib/datasets/__tests__/upload-preview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { describe, expect, it } from 'vitest';
import type { GetModelTreeWithRegionsQuery, ModelConfigInfo } from '@/graphql/generated/modeling';
import { rankSuggestionsByUse, suggestionUseText } from '@/lib/datasets/upload-preview';

const SV = 'https://w3id.org/okn/i/mint/';

function input(id: string, variable: string, format: string | null) {
return {
configuration_id: 'c',
input_id: id,
is_optional: false,
input: {
id,
label: id,
has_format: format,
presentations: [
{
dataset_specification_id: id,
presentation_id: `${id}-p`,
presentation: {
id: `${id}-p`,
standard_variable: { id: SV + variable, label: variable },
},
},
],
},
};
}

function config(id: string, inputs: ReturnType<typeof input>[], flow = false): ModelConfigInfo {
return {
id,
label: id,
flow: flow ? { configuration_id: id, thread_bindings_json: null } : null,
regions: [],
inputs,
outputs: [],
child_configurations: [],
} as unknown as ModelConfigInfo;
}

const tree: GetModelTreeWithRegionsQuery = {
modelcatalog_software: [
{
id: 's',
label: 's',
versions: [
{
id: 'v',
label: 'v',
configurations: [
config(
'Dictionary flow',
[input('corpus', 'document_collection__text', 'directory')],
true,
),
config(
'Narrative flow',
[input('corpus2', 'document_collection__text', 'directory')],
true,
),
config('HAND', [input('dem', 'land_surface__elevation', 'tif')]),
config('Narratives model', [input('nlp', 'corpus_nlp', null)]),
],
},
],
},
],
};

// The semantic order the service returned for "RGV Transcripts — oral histories".
const hits = [
{ id: SV + 'corpus_nlp', label: 'corpus_nlp' },
{
id: SV + 'document_collection_narrative_frame__description',
label: 'document_collection_narrative_frame__description',
},
{ id: SV + 'land_surface__elevation', label: 'land_surface__elevation' },
{ id: SV + 'document_collection__text', label: 'document_collection__text' },
];

const labels = (ranked: { item: { label: string } }[]) => ranked.map((r) => r.item.label);

describe('rankSuggestionsByUse', () => {
it('puts the variable a flow input accepts these files under first', () => {
const ranked = rankSuggestionsByUse(tree, hits, [{ name: 'transcript.json' }]);
expect(labels(ranked)).toEqual([
'document_collection__text',
'corpus_nlp',
'land_surface__elevation',
'document_collection_narrative_frame__description',
]);
expect(ranked.map((r) => r.fit)).toEqual([0, 1, 2, 3]);
});

it('keeps the semantic order among variables that fit equally', () => {
const ranked = rankSuggestionsByUse(tree, hits, []);
// No files yet: anything an input takes ranks above what nothing takes.
expect(labels(ranked)).toEqual([
'corpus_nlp',
'land_surface__elevation',
'document_collection__text',
'document_collection_narrative_frame__description',
]);
});

it('ranks nothing without the model tree but keeps every suggestion', () => {
expect(labels(rankSuggestionsByUse(undefined, hits, []))).toEqual(hits.map((h) => h.label));
});
});

describe('suggestionUseText', () => {
it('says who takes the variable and when the format is wrong', () => {
const ranked = rankSuggestionsByUse(tree, hits, [{ name: 'transcript.json' }]);
expect(ranked.map(suggestionUseText)).toEqual([
'Input to 2 flows',
'Input to 1 model',
'Input to 1 model, but not in this file format',
'No registered flow or model takes this as an input',
]);
});
});
68 changes: 68 additions & 0 deletions ui-react/src/lib/datasets/upload-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,74 @@ export function consumersOfVariable(
);
}

/**
* How useful a suggested variable is for these files, best first:
* 0 an input that declares a format takes these files,
* 1 an input takes the variable but declares no format (or no files are chosen yet),
* 2 inputs take the variable but none takes these files,
* 3 no registered flow or model takes it.
*/
export type SuggestionFit = 0 | 1 | 2 | 3;

export interface RankedSuggestion<T> {
item: T;
fit: SuggestionFit;
/** Flows and models with an input that takes the variable, whatever the format. */
flows: number;
models: number;
}

function suggestionFit(consumers: UploadConsumer[]): SuggestionFit {
if (consumers.length === 0) return 3;
if (consumers.some((c) => c.formatFits === true && (c.format ?? '').trim())) return 0;
if (consumers.some((c) => c.formatFits !== false)) return 1;
return 2;
}

/**
* Order the semantic suggestions by whether a registered flow or model could use
* the files under that variable, keeping the semantic order within each group.
* Text similarity alone cannot tell "a corpus of transcripts" from "frames drawn
* from transcripts"; what the catalog's inputs accept can.
*/
export function rankSuggestionsByUse<T extends { id: string; label?: string | null }>(
tree: GetModelTreeWithRegionsQuery | undefined,
suggestions: T[],
files: Array<{ name: string }>,
): RankedSuggestion<T>[] {
return suggestions
.map((item, index) => {
const consumers = consumersOfVariable(
tree,
{ id: item.id, label: item.label?.trim() || item.id },
files,
);
return {
index,
ranked: {
item,
fit: suggestionFit(consumers),
flows: consumers.filter((c) => c.kind === 'flow').length,
models: consumers.filter((c) => c.kind === 'model').length,
},
};
})
.sort((a, b) => a.ranked.fit - b.ranked.fit || a.index - b.index)
.map(({ ranked }) => ranked);
}

/** "Input to 6 flows and 1 model", or why nothing can use it. */
export function suggestionUseText(ranked: RankedSuggestion<unknown>): string {
const count = (n: number, word: string) => `${n} ${word}${n === 1 ? '' : 's'}`;
const parts = [
ranked.flows ? count(ranked.flows, 'flow') : '',
ranked.models ? count(ranked.models, 'model') : '',
].filter(Boolean);
if (parts.length === 0) return 'No registered flow or model takes this as an input';
const users = parts.join(' and ');
return ranked.fit === 2 ? `Input to ${users}, but not in this file format` : `Input to ${users}`;
}

export interface CloseConsumer extends UploadConsumer {
/** The close variable whose input this matched on. */
via: string;
Expand Down
87 changes: 56 additions & 31 deletions ui-react/src/pages/datasets/DatasetUploadPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ import {
} from '@/lib/ckan-upload';
import { useDebouncedValue } from '@/hooks/useDebouncedValue';
import { useSemanticSearch, useSimilarVariables } from '@/hooks/useSemanticSearch';
import { closeConsumers, consumersOfVariable } from '@/lib/datasets/upload-preview';
import {
closeConsumers,
consumersOfVariable,
rankSuggestionsByUse,
suggestionUseText,
} from '@/lib/datasets/upload-preview';
import { unionBoundingBox, type BoundingBox } from '@/lib/geo/bbox';
import { standardVariableName } from '@/lib/standard-variable-display';
import { useAuth } from '@/lib/auth/useAuth';
Expand Down Expand Up @@ -177,12 +182,21 @@ export function DatasetUploadPage() {
// Standard variables suggested from what the user wrote. Never chosen for them.
const aboutText = useDebouncedValue(`${title}\n${description}`.trim(), SUGGEST_DELAY_MS);
const suggested = useSemanticSearch(aboutText, { target: 'svo', limit: 20, minLength: 4 });
// Ranked by whether a registered flow or model takes these files under the
// variable, then by how close it is to the text.
const suggestions = useMemo(
() => (suggested.results ?? []).filter((r) => r.label?.trim()),
[suggested.results],
() =>
rankSuggestionsByUse(
tree,
(suggested.results ?? []).filter((r) => r.label?.trim()),
files,
),
[tree, suggested.results, files],
);
const suggestionIds = useMemo(() => suggestions.map((r) => r.id), [suggestions]);
const shownSuggestions = suggestions.filter((r) => r.id !== variable?.id).slice(0, SUGGEST_SHOWN);
const suggestionIds = useMemo(() => suggestions.map((r) => r.item.id), [suggestions]);
const shownSuggestions = suggestions
.filter((r) => r.item.id !== variable?.id)
.slice(0, SUGGEST_SHOWN);

const similar = useSimilarVariables(variable?.id, { limit: 5 });
const closeMatches = useMemo(
Expand Down Expand Up @@ -532,32 +546,43 @@ export function DatasetUploadPage() {
<div data-testid="suggested-variables" className="rounded-md border p-2 text-sm">
<p className="font-medium">Suggested standard variables</p>
<ul className="mt-1 space-y-1">
{shownSuggestions.map((s) => (
<li key={s.id} className="flex flex-wrap items-start justify-between gap-2">
<span>
<span className="font-medium">{s.label}</span>
{s.description && (
<span className="block text-xs text-muted-foreground">{s.description}</span>
)}
</span>
<Button
type="button"
variant="outline"
size="sm"
disabled={busy}
aria-label={`Use ${s.label} as the standard variable`}
onClick={() =>
setVariable({
id: s.id,
label: s.label ?? '',
description: s.description ?? null,
})
}
>
Use this
</Button>
</li>
))}
{shownSuggestions.map((ranked) => {
const s = ranked.item;
return (
<li key={s.id} className="flex flex-wrap items-start justify-between gap-2">
<span>
<span className="font-medium">{s.label}</span>
{s.description && (
<span className="block text-xs text-muted-foreground">
{s.description}
</span>
)}
<span
className="block text-xs text-muted-foreground"
data-testid="suggestion-use"
>
{suggestionUseText(ranked)}
</span>
</span>
<Button
type="button"
variant="outline"
size="sm"
disabled={busy}
aria-label={`Use ${s.label} as the standard variable`}
onClick={() =>
setVariable({
id: s.id,
label: s.label ?? '',
description: s.description ?? null,
})
}
>
Use this
</Button>
</li>
);
})}
</ul>
</div>
)}
Expand Down
Loading