Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | 20x 20x 20x 20x 20x 20x 27x 27x 5x 5x 20x 5x 7x 7x 7x 20x 6x 6x 20x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 2x | import { Tables } from './database.js';
import { resolveMetadataImport } from './metadata/imports.js';
import { metadataOption } from './metadata/storage.js';
import {
ensureNamespacedMetadataId,
metadataOptionId,
namespacedMetadataId,
namespaceOfMetadataId,
parseMetadataOptionId,
} from './schemas/metadata.js';
import { ensureArray, entries, groupBy, nonnull, sum } from './utils.js';
/**
* @import * as DB from './database.js'
* @import { RuntimeValue } from './metadata/index.js'
* @import { DatabaseHandle } from './idb.svelte.js'
* @import { MetadataEnumVariant } from './database.js'
* @import { NamespacedMetadataID } from './schemas/common.js';
*/
/**
* Get all metadata the given MetadataValue cascades into
* Confidence scores are the sum of confidences of all metadata options that cascade into a metadata
*
* For example, if we have the following cascades:
*
* - species:40 -> genus:1
* - species:41 -> genus:1
* - species:42 -> genus:2
* - species:44 -> genus:3
*
* And make the following call
*
* ```js
* computeCascades({
* metadataId: "species",
* confidence: 0.4,
* value: 40,
* confidences: { "41": 0.3, "42": 0.2, "44": 0.1 }
* })
* ```
*
* We'll get the following cascades, ready for another round of `storeMetadataValue` calls:
*
* ```js
* // Only one object in the array
* // since we only have cascades for a single other metadata
* [
* {
* metadataId: "genus",
* value: "1",
* confidence: 0.7, // 0.4 + 0.3, from species:40 and species:41
* confidences: {
* "2": 0.2, // from species:42
* "3": 0.1 // from species:44
* }
* }
* ]
* ```
*
* @param {object} param0
* @param {import('./idb.svelte.js').DatabaseHandle} param0.db
* @param {string} param0.metadataId
* @param {number} param0.confidence
* @param {RuntimeValue} param0.value
* @param { DB.MetadataValue["confidences"] | Array<{ value: RuntimeValue, confidence: number }> } param0.confidences
*/
export async function computeCascades({
db,
metadataId,
confidence,
value,
confidences: _confidences,
}) {
const protocolId = namespaceOfMetadataId(metadataId);
Iif (!protocolId) {
throw new Error(`Metadata ${metadataId} is not namespaced, cannot cascade`);
}
const protocol = await db.get('Protocol', protocolId);
Iif (!protocol) {
throw new Error(`Metadata ${metadataId} is namespaced to unknown protocol ${protocolId}`);
}
const confidences = !Array.isArray(_confidences)
? Object.entries(_confidences).map(([value, confidence]) => ({
value: /** @type {RuntimeValue} */ (JSON.parse(value)),
confidence,
}))
: _confidences;
return await Promise.all(
// List of { value, confidence }, that includes the main value as well as the alternatives
[{ value, confidence }, ...confidences].map(async ({ confidence, value }) => {
// Get the cascades for the corresponding metadata option
const option = await db.get('MetadataOption', metadataOptionId(metadataId, value));
if (!option?.cascade) return undefined;
const { cascade } = option;
return { cascade, confidence };
})
).then((options) => {
// Combine all cascades that lead to the same metadata option, and sum their confidences
const groupedByOption = groupBy(
// Get a list of { option id, confidence } for every cascaded value,
// the confidence coming from the value that triggers it
options.filter(nonnull).flatMap(({ cascade, confidence }) => {
return entries(cascade).flatMap(([metadataId, values]) =>
ensureArray(values).map((value) => ({
optionId: metadataOptionId(metadataId, value),
confidence,
}))
);
}),
(c) => c.optionId,
(c) => c.confidence
);
// Combine all options of a same metadataId into alternatives.
// The confidence of every option is the sum of confidences of all
// cascades that lead to that option
const groupedByMetadata = groupBy(
groupedByOption.entries(),
([optionId]) =>
resolveMetadataImport(
protocol,
ensureNamespacedMetadataId(
parseMetadataOptionId(optionId).metadataId,
protocolId
)
),
([optionId, confidences]) =>
/** @type {const} */ ({
value: parseMetadataOptionId(optionId).key,
confidence: sum(confidences),
})
);
// Return a list of data ready for storeMetadataValue() for every cascaded metadata
return [...groupedByMetadata.entries()].map(([metadataId, options]) => {
const [{ value, confidence }, ...confidences] = options.toSorted(
({ confidence: a }, { confidence: b }) => b - a
);
return {
metadataId,
value,
confidence,
confidences,
};
});
});
}
/**
* @typedef {Record<string, Record<string, { value: string; metadata: string; depth: number, color?: string, icon?: string }>>} CascadeLabelsCache
*/
/**
* Resolve (NOT recursively anymore, see #1571) cascades for the given metadata value, return labels to display
*
* @param {object} param0
* @param {DB.MetadataEnumVariant | typeof DB.Schemas.MetadataEnumVariant['inferIn'] | undefined} param0.option the currently selected option
* @param {DatabaseHandle} param0.db
* @param {string | undefined} param0.protocolId
*/
export async function cascadeLabels({ protocolId, option, db }) {
Iif (!protocolId) return {};
Iif (!option) return {};
/** @type {Record<NamespacedMetadataID, DB.MetadataEnumVariant[]>} */
const labels = {};
for (const [metadataId, values] of Object.entries(option.cascade ?? {})) {
const id = namespacedMetadataId(protocolId, metadataId);
const options = await Promise.all(
ensureArray(values).map(async (key) => metadataOption(db, id, key))
);
labels[id] = options.filter(nonnull).map((option) => Tables.MetadataOption.assert(option));
}
return labels;
}
|