@platforma-sdk/model 1.51.5 → 1.51.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var version = "1.51.5";
3
+ var version = "1.51.9";
4
4
 
5
5
  exports.version = version;
6
6
  //# sourceMappingURL=package.json.cjs.map
@@ -1,4 +1,4 @@
1
- var version = "1.51.5";
1
+ var version = "1.51.9";
2
2
 
3
3
  export { version };
4
4
  //# sourceMappingURL=package.json.js.map
@@ -110,6 +110,30 @@ function deriveLabels(values, specExtractor, ops = {}) {
110
110
  }
111
111
  return result;
112
112
  };
113
+ const countUniqueLabels = (result) => result === undefined ? 0 : new Set(result.map((c) => c.label)).size;
114
+ // Post-processing: try removing types one by one (lowest importance first) to minimize the label set
115
+ // Accepts removal if it doesn't decrease the number of unique labels (cardinality)
116
+ const minimizeTypeSet = (typeSet) => {
117
+ const initialResult = calculate(typeSet);
118
+ if (initialResult === undefined) {
119
+ return typeSet;
120
+ }
121
+ const currentCardinality = countUniqueLabels(initialResult);
122
+ // Get types sorted by importance ascending (lowest first), excluding forced elements
123
+ const removableSorted = [...typeSet]
124
+ .filter((t) => !forceTraceElements?.has(t.split('@')[0])
125
+ && !(ops.includeNativeLabel && t === LabelTypeFull))
126
+ .sort((a, b) => (importances.get(a) ?? 0) - (importances.get(b) ?? 0));
127
+ for (const typeToRemove of removableSorted) {
128
+ const reducedSet = new Set(typeSet);
129
+ reducedSet.delete(typeToRemove);
130
+ const candidateResult = calculate(reducedSet);
131
+ if (candidateResult !== undefined && countUniqueLabels(candidateResult) >= currentCardinality) {
132
+ typeSet.delete(typeToRemove);
133
+ }
134
+ }
135
+ return typeSet;
136
+ };
113
137
  if (mainTypes.length === 0) {
114
138
  if (secondaryTypes.length !== 0)
115
139
  throw new Error('Non-empty secondary types list while main types list is empty.');
@@ -135,16 +159,20 @@ function deriveLabels(values, specExtractor, ops = {}) {
135
159
  if (additionalType >= 0)
136
160
  currentSet.add(mainTypes[additionalType]);
137
161
  const candidateResult = calculate(currentSet);
138
- // checking if labels uniquely separate our records
139
- if (candidateResult !== undefined && new Set(candidateResult.map((c) => c.label)).size === values.length)
140
- return candidateResult;
162
+ if (candidateResult !== undefined && countUniqueLabels(candidateResult) === values.length) {
163
+ minimizeTypeSet(currentSet);
164
+ return calculate(currentSet);
165
+ }
141
166
  additionalType++;
142
167
  if (additionalType >= mainTypes.length) {
143
168
  includedTypes++;
144
169
  additionalType = includedTypes;
145
170
  }
146
171
  }
147
- return calculate(new Set([...mainTypes, ...secondaryTypes]), true);
172
+ // Fallback: include all types, then try to minimize
173
+ const fallbackSet = new Set([...mainTypes, ...secondaryTypes]);
174
+ minimizeTypeSet(fallbackSet);
175
+ return calculate(fallbackSet, true);
148
176
  }
149
177
 
150
178
  exports.Trace = Trace;
@@ -1 +1 @@
1
- {"version":3,"file":"label.cjs","sources":["../../../src/render/util/label.ts"],"sourcesContent":["import { Annotation, parseJson, readAnnotation, type PObjectSpec } from '@milaboratories/pl-model-common';\nimport { z } from 'zod';\n\nexport type RecordsWithLabel<T> = {\n value: T;\n label: string;\n};\n\nexport type LabelDerivationOps = {\n /** Force inclusion of native column label */\n includeNativeLabel?: boolean;\n /** Separator to use between label parts (\" / \" by default) */\n separator?: string;\n /** If true, label will be added as suffix (at the end of the generated label). By default label added as a prefix. */\n addLabelAsSuffix?: boolean;\n /** Trace elements list that will be forced to be included in the label. */\n forceTraceElements?: string[];\n};\n\nexport const TraceEntry = z.object({\n type: z.string(),\n importance: z.number().optional(),\n id: z.string().optional(),\n label: z.string(),\n});\nexport type TraceEntry = z.infer<typeof TraceEntry>;\ntype FullTraceEntry = TraceEntry & { fullType: string; occurrenceIndex: number };\n\nexport const Trace = z.array(TraceEntry);\nexport type Trace = z.infer<typeof Trace>;\ntype FullTrace = FullTraceEntry[];\n\n// Define the possible return types for the specExtractor function\ntype SpecExtractorResult = PObjectSpec | {\n spec: PObjectSpec;\n prefixTrace?: TraceEntry[];\n suffixTrace?: TraceEntry[];\n};\n\nconst DistancePenalty = 0.001;\n\nconst LabelType = '__LABEL__';\nconst LabelTypeFull = '__LABEL__@1';\n\nexport function deriveLabels<T>(\n values: T[],\n specExtractor: (obj: T) => SpecExtractorResult,\n ops: LabelDerivationOps = {},\n): RecordsWithLabel<T>[] {\n const importances = new Map<string, number>();\n\n const forceTraceElements = (ops.forceTraceElements !== undefined && ops.forceTraceElements.length > 0)\n ? new Set(ops.forceTraceElements)\n : undefined;\n\n // number of times certain type occurred among all of the\n const numberOfRecordsWithType = new Map<string, number>();\n\n const enrichedRecords = values.map((value) => {\n const extractorResult = specExtractor(value);\n let spec: PObjectSpec;\n let prefixTrace: TraceEntry[] | undefined;\n let suffixTrace: TraceEntry[] | undefined;\n\n // Check if the result is the new structure or just PObjectSpec\n if ('spec' in extractorResult && typeof extractorResult.spec === 'object') {\n // It's the new structure { spec, prefixTrace?, suffixTrace? }\n spec = extractorResult.spec;\n prefixTrace = extractorResult.prefixTrace;\n suffixTrace = extractorResult.suffixTrace;\n } else {\n // It's just PObjectSpec\n spec = extractorResult as PObjectSpec;\n }\n\n const label = readAnnotation(spec, Annotation.Label);\n const traceStr = readAnnotation(spec, Annotation.Trace);\n const baseTrace = (traceStr ? Trace.safeParse(parseJson(traceStr)).data : undefined) ?? [];\n\n const trace = [\n ...(prefixTrace ?? []),\n ...baseTrace,\n ...(suffixTrace ?? []),\n ];\n\n if (label !== undefined) {\n const labelEntry = { label, type: LabelType, importance: -2 };\n if (ops.addLabelAsSuffix) trace.push(labelEntry);\n else trace.splice(0, 0, labelEntry);\n }\n\n const fullTrace: FullTrace = [];\n\n const occurrences = new Map<string, number>();\n for (let i = trace.length - 1; i >= 0; --i) {\n const { type: typeName } = trace[i];\n const importance = trace[i].importance ?? 0;\n const occurrenceIndex = (occurrences.get(typeName) ?? 0) + 1;\n occurrences.set(typeName, occurrenceIndex);\n const fullType = `${typeName}@${occurrenceIndex}`;\n numberOfRecordsWithType.set(fullType, (numberOfRecordsWithType.get(fullType) ?? 0) + 1);\n importances.set(\n fullType,\n Math.max(\n importances.get(fullType) ?? Number.NEGATIVE_INFINITY,\n importance - (trace.length - i) * DistancePenalty,\n ),\n );\n fullTrace.push({ ...trace[i], fullType, occurrenceIndex: occurrenceIndex });\n }\n fullTrace.reverse();\n return {\n value,\n spec,\n label,\n fullTrace,\n };\n });\n\n // excluding repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const mainTypes: string[] = [];\n // repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const secondaryTypes: string[] = [];\n\n const allTypeRecords = [...importances];\n // sorting: most important types go first\n allTypeRecords.sort(([, i1], [, i2]) => i2 - i1);\n\n for (const [typeName] of allTypeRecords) {\n if (typeName.endsWith('@1') || numberOfRecordsWithType.get(typeName) === values.length)\n mainTypes.push(typeName);\n else secondaryTypes.push(typeName);\n }\n\n const calculate = (includedTypes: Set<string>, force: boolean = false) => {\n const result: RecordsWithLabel<T>[] = [];\n for (let i = 0; i < enrichedRecords.length; i++) {\n const r = enrichedRecords[i];\n const includedTrace = r.fullTrace\n .filter((fm) => includedTypes.has(fm.fullType)\n || (forceTraceElements && forceTraceElements.has(fm.type)));\n if (includedTrace.length === 0) {\n if (force)\n result.push({\n label: 'Unlabeled',\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n else return undefined;\n }\n const labelSet = includedTrace\n .map((fm) => fm.label);\n const sep = ops.separator ?? ' / ';\n result.push({\n label: labelSet.join(sep),\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n }\n return result;\n };\n\n if (mainTypes.length === 0) {\n if (secondaryTypes.length !== 0) throw new Error('Non-empty secondary types list while main types list is empty.');\n return calculate(new Set(LabelTypeFull), true)!;\n }\n\n //\n // includedTypes = 2\n // * *\n // T0 T1 T2 T3 T4 T5\n // *\n // additionalType = 3\n //\n // Resulting set: T0, T1, T3\n //\n let includedTypes = 0;\n let additionalType = -1;\n while (includedTypes < mainTypes.length) {\n const currentSet = new Set<string>();\n if (ops.includeNativeLabel) currentSet.add(LabelTypeFull);\n for (let i = 0; i < includedTypes; ++i) currentSet.add(mainTypes[i]);\n if (additionalType >= 0)\n currentSet.add(mainTypes[additionalType]);\n\n const candidateResult = calculate(currentSet);\n\n // checking if labels uniquely separate our records\n if (candidateResult !== undefined && new Set(candidateResult.map((c) => c.label)).size === values.length) return candidateResult;\n\n additionalType++;\n if (additionalType >= mainTypes.length) {\n includedTypes++;\n additionalType = includedTypes;\n }\n }\n\n return calculate(new Set([...mainTypes, ...secondaryTypes]), true)!;\n}\n"],"names":["z","readAnnotation","Annotation","parseJson"],"mappings":";;;;;AAmBO,MAAM,UAAU,GAAGA,KAAC,CAAC,MAAM,CAAC;AACjC,IAAA,IAAI,EAAEA,KAAC,CAAC,MAAM,EAAE;AAChB,IAAA,UAAU,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACjC,IAAA,EAAE,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACzB,IAAA,KAAK,EAAEA,KAAC,CAAC,MAAM,EAAE;AAClB,CAAA;AAIM,MAAM,KAAK,GAAGA,KAAC,CAAC,KAAK,CAAC,UAAU;AAWvC,MAAM,eAAe,GAAG,KAAK;AAE7B,MAAM,SAAS,GAAG,WAAW;AAC7B,MAAM,aAAa,GAAG,aAAa;AAE7B,SAAU,YAAY,CAC1B,MAAW,EACX,aAA8C,EAC9C,MAA0B,EAAE,EAAA;AAE5B,IAAA,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB;AAE7C,IAAA,MAAM,kBAAkB,GAAG,CAAC,GAAG,CAAC,kBAAkB,KAAK,SAAS,IAAI,GAAG,CAAC,kBAAkB,CAAC,MAAM,GAAG,CAAC;AACnG,UAAE,IAAI,GAAG,CAAC,GAAG,CAAC,kBAAkB;UAC9B,SAAS;;AAGb,IAAA,MAAM,uBAAuB,GAAG,IAAI,GAAG,EAAkB;IAEzD,MAAM,e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SAAS,CAAC,MAAM,EAAE;AACtC,YAAA,aAAa,EAAE;YACf,cAAc,GAAG,aAAa;QAChC;IACF;AAEA,IAAA,OAAO,SAAS,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,SAAS,EAAE,GAAG,cAAc,CAAC,CAAC,EAAE,IAAI,CAAE;AACrE;;;;;;"}
1
+ {"version":3,"file":"label.cjs","sources":["../../../src/render/util/label.ts"],"sourcesContent":["import { Annotation, parseJson, readAnnotation, type PObjectSpec } from '@milaboratories/pl-model-common';\nimport { z } from 'zod';\n\nexport type RecordsWithLabel<T> = {\n value: T;\n label: string;\n};\n\nexport type LabelDerivationOps = {\n /** Force inclusion of native column label */\n includeNativeLabel?: boolean;\n /** Separator to use between label parts (\" / \" by default) */\n separator?: string;\n /** If true, label will be added as suffix (at the end of the generated label). By default label added as a prefix. */\n addLabelAsSuffix?: boolean;\n /** Trace elements list that will be forced to be included in the label. */\n forceTraceElements?: string[];\n};\n\nexport const TraceEntry = z.object({\n type: z.string(),\n importance: z.number().optional(),\n id: z.string().optional(),\n label: z.string(),\n});\nexport type TraceEntry = z.infer<typeof TraceEntry>;\ntype FullTraceEntry = TraceEntry & { fullType: string; occurrenceIndex: number };\n\nexport const Trace = z.array(TraceEntry);\nexport type Trace = z.infer<typeof Trace>;\ntype FullTrace = FullTraceEntry[];\n\n// Define the possible return types for the specExtractor function\ntype SpecExtractorResult = PObjectSpec | {\n spec: PObjectSpec;\n prefixTrace?: TraceEntry[];\n suffixTrace?: TraceEntry[];\n};\n\nconst DistancePenalty = 0.001;\n\nconst LabelType = '__LABEL__';\nconst LabelTypeFull = '__LABEL__@1';\n\nexport function deriveLabels<T>(\n values: T[],\n specExtractor: (obj: T) => SpecExtractorResult,\n ops: LabelDerivationOps = {},\n): RecordsWithLabel<T>[] {\n const importances = new Map<string, number>();\n\n const forceTraceElements = (ops.forceTraceElements !== undefined && ops.forceTraceElements.length > 0)\n ? new Set(ops.forceTraceElements)\n : undefined;\n\n // number of times certain type occurred among all of the\n const numberOfRecordsWithType = new Map<string, number>();\n\n const enrichedRecords = values.map((value) => {\n const extractorResult = specExtractor(value);\n let spec: PObjectSpec;\n let prefixTrace: TraceEntry[] | undefined;\n let suffixTrace: TraceEntry[] | undefined;\n\n // Check if the result is the new structure or just PObjectSpec\n if ('spec' in extractorResult && typeof extractorResult.spec === 'object') {\n // It's the new structure { spec, prefixTrace?, suffixTrace? }\n spec = extractorResult.spec;\n prefixTrace = extractorResult.prefixTrace;\n suffixTrace = extractorResult.suffixTrace;\n } else {\n // It's just PObjectSpec\n spec = extractorResult as PObjectSpec;\n }\n\n const label = readAnnotation(spec, Annotation.Label);\n const traceStr = readAnnotation(spec, Annotation.Trace);\n const baseTrace = (traceStr ? Trace.safeParse(parseJson(traceStr)).data : undefined) ?? [];\n\n const trace = [\n ...(prefixTrace ?? []),\n ...baseTrace,\n ...(suffixTrace ?? []),\n ];\n\n if (label !== undefined) {\n const labelEntry = { label, type: LabelType, importance: -2 };\n if (ops.addLabelAsSuffix) trace.push(labelEntry);\n else trace.splice(0, 0, labelEntry);\n }\n\n const fullTrace: FullTrace = [];\n\n const occurrences = new Map<string, number>();\n for (let i = trace.length - 1; i >= 0; --i) {\n const { type: typeName } = trace[i];\n const importance = trace[i].importance ?? 0;\n const occurrenceIndex = (occurrences.get(typeName) ?? 0) + 1;\n occurrences.set(typeName, occurrenceIndex);\n const fullType = `${typeName}@${occurrenceIndex}`;\n numberOfRecordsWithType.set(fullType, (numberOfRecordsWithType.get(fullType) ?? 0) + 1);\n importances.set(\n fullType,\n Math.max(\n importances.get(fullType) ?? Number.NEGATIVE_INFINITY,\n importance - (trace.length - i) * DistancePenalty,\n ),\n );\n fullTrace.push({ ...trace[i], fullType, occurrenceIndex: occurrenceIndex });\n }\n fullTrace.reverse();\n return {\n value,\n spec,\n label,\n fullTrace,\n };\n });\n\n // excluding repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const mainTypes: string[] = [];\n // repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const secondaryTypes: string[] = [];\n\n const allTypeRecords = [...importances];\n // sorting: most important types go first\n allTypeRecords.sort(([, i1], [, i2]) => i2 - i1);\n\n for (const [typeName] of allTypeRecords) {\n if (typeName.endsWith('@1') || numberOfRecordsWithType.get(typeName) === values.length)\n mainTypes.push(typeName);\n else secondaryTypes.push(typeName);\n }\n\n const calculate = (includedTypes: Set<string>, force: boolean = false) => {\n const result: RecordsWithLabel<T>[] = [];\n for (let i = 0; i < enrichedRecords.length; i++) {\n const r = enrichedRecords[i];\n const includedTrace = r.fullTrace\n .filter((fm) => includedTypes.has(fm.fullType)\n || (forceTraceElements && forceTraceElements.has(fm.type)));\n if (includedTrace.length === 0) {\n if (force)\n result.push({\n label: 'Unlabeled',\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n else return undefined;\n }\n const labelSet = includedTrace\n .map((fm) => fm.label);\n const sep = ops.separator ?? ' / ';\n result.push({\n label: labelSet.join(sep),\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n }\n return result;\n };\n\n const countUniqueLabels = (result: RecordsWithLabel<T>[] | undefined): number =>\n result === undefined ? 0 : new Set(result.map((c) => c.label)).size;\n\n // Post-processing: try removing types one by one (lowest importance first) to minimize the label set\n // Accepts removal if it doesn't decrease the number of unique labels (cardinality)\n const minimizeTypeSet = (typeSet: Set<string>): Set<string> => {\n const initialResult = calculate(typeSet);\n if (initialResult === undefined) {\n return typeSet;\n }\n const currentCardinality = countUniqueLabels(initialResult);\n\n // Get types sorted by importance ascending (lowest first), excluding forced elements\n const removableSorted = [...typeSet]\n .filter((t) =>\n !forceTraceElements?.has(t.split('@')[0])\n && !(ops.includeNativeLabel && t === LabelTypeFull))\n .sort((a, b) => (importances.get(a) ?? 0) - (importances.get(b) ?? 0));\n\n for (const typeToRemove of removableSorted) {\n const reducedSet = new Set(typeSet);\n reducedSet.delete(typeToRemove);\n const candidateResult = calculate(reducedSet);\n if (candidateResult !== undefined && countUniqueLabels(candidateResult) >= currentCardinality) {\n typeSet.delete(typeToRemove);\n }\n }\n return typeSet;\n };\n\n if (mainTypes.length === 0) {\n if (secondaryTypes.length !== 0) throw new Error('Non-empty secondary types list while main types list is empty.');\n return calculate(new Set(LabelTypeFull), true)!;\n }\n\n //\n // includedTypes = 2\n // * *\n // T0 T1 T2 T3 T4 T5\n // *\n // additionalType = 3\n //\n // Resulting set: T0, T1, T3\n //\n let includedTypes = 0;\n let additionalType = -1;\n while (includedTypes < mainTypes.length) {\n const currentSet = new Set<string>();\n if (ops.includeNativeLabel) currentSet.add(LabelTypeFull);\n for (let i = 0; i < includedTypes; ++i) currentSet.add(mainTypes[i]);\n if (additionalType >= 0)\n currentSet.add(mainTypes[additionalType]);\n\n const candidateResult = calculate(currentSet);\n\n if (candidateResult !== undefined && countUniqueLabels(candidateResult) === values.length) {\n minimizeTypeSet(currentSet);\n return calculate(currentSet)!;\n }\n\n additionalType++;\n if (additionalType >= mainTypes.length) {\n includedTypes++;\n additionalType = includedTypes;\n }\n }\n\n // Fallback: include all types, then try to minimize\n const fallbackSet = new Set([...mainTypes, ...secondaryTypes]);\n minimizeTypeSet(fallbackSet);\n return calculate(fallbackSet, true)!;\n}\n"],"names":["z","readAnnotation","Annotation","parseJson"],"mappings":";;;;;AAmBO,MAAM,UAAU,GAAGA,KAAC,CAAC,MAAM,CAAC;AACjC,IAAA,IAAI,EAAEA,KAAC,CAAC,MAAM,EAAE;AAChB,IAAA,UAAU,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACjC,IAAA,EAAE,EAAEA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACzB,IAAA,KAAK,EAAEA,KAAC,CAAC,MAAM,EAAE;AAClB,CAAA;AAIM,MAAM,KAAK,GAAGA,KAAC,CAAC,KAAK,CAAC,UAAU;AAWvC,MAAM,eAAe,GAAG,KAAK;AAE7B,MAAM,SAAS,GAAG,WAAW;AAC7B,MAAM,aAAa,GAAG,aAAa;AAE7B,S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AC;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAC;AAGpD,eAAO,MAAM,KAAK;;;;;;;;;;;;;;;WAAsB,CAAC;AACzC,MAAM,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,KAAK,CAAC,CAAC;AAI1C,KAAK,mBAAmB,GAAG,WAAW,GAAG;IACvC,IAAI,EAAE,WAAW,CAAC;IAClB,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;IAC3B,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;CAC5B,CAAC;AAOF,wBAAgB,YAAY,CAAC,CAAC,EAC5B,MAAM,EAAE,CAAC,EAAE,EACX,aAAa,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,mBAAmB,EAC9C,GAAG,GAAE,kBAAuB,GAC3B,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAuLvB"}
@@ -108,6 +108,30 @@ function deriveLabels(values, specExtractor, ops = {}) {
108
108
  }
109
109
  return result;
110
110
  };
111
+ const countUniqueLabels = (result) => result === undefined ? 0 : new Set(result.map((c) => c.label)).size;
112
+ // Post-processing: try removing types one by one (lowest importance first) to minimize the label set
113
+ // Accepts removal if it doesn't decrease the number of unique labels (cardinality)
114
+ const minimizeTypeSet = (typeSet) => {
115
+ const initialResult = calculate(typeSet);
116
+ if (initialResult === undefined) {
117
+ return typeSet;
118
+ }
119
+ const currentCardinality = countUniqueLabels(initialResult);
120
+ // Get types sorted by importance ascending (lowest first), excluding forced elements
121
+ const removableSorted = [...typeSet]
122
+ .filter((t) => !forceTraceElements?.has(t.split('@')[0])
123
+ && !(ops.includeNativeLabel && t === LabelTypeFull))
124
+ .sort((a, b) => (importances.get(a) ?? 0) - (importances.get(b) ?? 0));
125
+ for (const typeToRemove of removableSorted) {
126
+ const reducedSet = new Set(typeSet);
127
+ reducedSet.delete(typeToRemove);
128
+ const candidateResult = calculate(reducedSet);
129
+ if (candidateResult !== undefined && countUniqueLabels(candidateResult) >= currentCardinality) {
130
+ typeSet.delete(typeToRemove);
131
+ }
132
+ }
133
+ return typeSet;
134
+ };
111
135
  if (mainTypes.length === 0) {
112
136
  if (secondaryTypes.length !== 0)
113
137
  throw new Error('Non-empty secondary types list while main types list is empty.');
@@ -133,16 +157,20 @@ function deriveLabels(values, specExtractor, ops = {}) {
133
157
  if (additionalType >= 0)
134
158
  currentSet.add(mainTypes[additionalType]);
135
159
  const candidateResult = calculate(currentSet);
136
- // checking if labels uniquely separate our records
137
- if (candidateResult !== undefined && new Set(candidateResult.map((c) => c.label)).size === values.length)
138
- return candidateResult;
160
+ if (candidateResult !== undefined && countUniqueLabels(candidateResult) === values.length) {
161
+ minimizeTypeSet(currentSet);
162
+ return calculate(currentSet);
163
+ }
139
164
  additionalType++;
140
165
  if (additionalType >= mainTypes.length) {
141
166
  includedTypes++;
142
167
  additionalType = includedTypes;
143
168
  }
144
169
  }
145
- return calculate(new Set([...mainTypes, ...secondaryTypes]), true);
170
+ // Fallback: include all types, then try to minimize
171
+ const fallbackSet = new Set([...mainTypes, ...secondaryTypes]);
172
+ minimizeTypeSet(fallbackSet);
173
+ return calculate(fallbackSet, true);
146
174
  }
147
175
 
148
176
  export { Trace, TraceEntry, deriveLabels };
@@ -1 +1 @@
1
- {"version":3,"file":"label.js","sources":["../../../src/render/util/label.ts"],"sourcesContent":["import { Annotation, parseJson, readAnnotation, type PObjectSpec } from '@milaboratories/pl-model-common';\nimport { z } from 'zod';\n\nexport type RecordsWithLabel<T> = {\n value: T;\n label: string;\n};\n\nexport type LabelDerivationOps = {\n /** Force inclusion of native column label */\n includeNativeLabel?: boolean;\n /** Separator to use between label parts (\" / \" by default) */\n separator?: string;\n /** If true, label will be added as suffix (at the end of the generated label). By default label added as a prefix. */\n addLabelAsSuffix?: boolean;\n /** Trace elements list that will be forced to be included in the label. */\n forceTraceElements?: string[];\n};\n\nexport const TraceEntry = z.object({\n type: z.string(),\n importance: z.number().optional(),\n id: z.string().optional(),\n label: z.string(),\n});\nexport type TraceEntry = z.infer<typeof TraceEntry>;\ntype FullTraceEntry = TraceEntry & { fullType: string; occurrenceIndex: number };\n\nexport const Trace = z.array(TraceEntry);\nexport type Trace = z.infer<typeof Trace>;\ntype FullTrace = FullTraceEntry[];\n\n// Define the possible return types for the specExtractor function\ntype SpecExtractorResult = PObjectSpec | {\n spec: PObjectSpec;\n prefixTrace?: TraceEntry[];\n suffixTrace?: TraceEntry[];\n};\n\nconst DistancePenalty = 0.001;\n\nconst LabelType = '__LABEL__';\nconst LabelTypeFull = '__LABEL__@1';\n\nexport function deriveLabels<T>(\n values: T[],\n specExtractor: (obj: T) => SpecExtractorResult,\n ops: LabelDerivationOps = {},\n): RecordsWithLabel<T>[] {\n const importances = new Map<string, number>();\n\n const forceTraceElements = (ops.forceTraceElements !== undefined && ops.forceTraceElements.length > 0)\n ? new Set(ops.forceTraceElements)\n : undefined;\n\n // number of times certain type occurred among all of the\n const numberOfRecordsWithType = new Map<string, number>();\n\n const enrichedRecords = values.map((value) => {\n const extractorResult = specExtractor(value);\n let spec: PObjectSpec;\n let prefixTrace: TraceEntry[] | undefined;\n let suffixTrace: TraceEntry[] | undefined;\n\n // Check if the result is the new structure or just PObjectSpec\n if ('spec' in extractorResult && typeof extractorResult.spec === 'object') {\n // It's the new structure { spec, prefixTrace?, suffixTrace? }\n spec = extractorResult.spec;\n prefixTrace = extractorResult.prefixTrace;\n suffixTrace = extractorResult.suffixTrace;\n } else {\n // It's just PObjectSpec\n spec = extractorResult as PObjectSpec;\n }\n\n const label = readAnnotation(spec, Annotation.Label);\n const traceStr = readAnnotation(spec, Annotation.Trace);\n const baseTrace = (traceStr ? Trace.safeParse(parseJson(traceStr)).data : undefined) ?? [];\n\n const trace = [\n ...(prefixTrace ?? []),\n ...baseTrace,\n ...(suffixTrace ?? []),\n ];\n\n if (label !== undefined) {\n const labelEntry = { label, type: LabelType, importance: -2 };\n if (ops.addLabelAsSuffix) trace.push(labelEntry);\n else trace.splice(0, 0, labelEntry);\n }\n\n const fullTrace: FullTrace = [];\n\n const occurrences = new Map<string, number>();\n for (let i = trace.length - 1; i >= 0; --i) {\n const { type: typeName } = trace[i];\n const importance = trace[i].importance ?? 0;\n const occurrenceIndex = (occurrences.get(typeName) ?? 0) + 1;\n occurrences.set(typeName, occurrenceIndex);\n const fullType = `${typeName}@${occurrenceIndex}`;\n numberOfRecordsWithType.set(fullType, (numberOfRecordsWithType.get(fullType) ?? 0) + 1);\n importances.set(\n fullType,\n Math.max(\n importances.get(fullType) ?? Number.NEGATIVE_INFINITY,\n importance - (trace.length - i) * DistancePenalty,\n ),\n );\n fullTrace.push({ ...trace[i], fullType, occurrenceIndex: occurrenceIndex });\n }\n fullTrace.reverse();\n return {\n value,\n spec,\n label,\n fullTrace,\n };\n });\n\n // excluding repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const mainTypes: string[] = [];\n // repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const secondaryTypes: string[] = [];\n\n const allTypeRecords = [...importances];\n // sorting: most important types go first\n allTypeRecords.sort(([, i1], [, i2]) => i2 - i1);\n\n for (const [typeName] of allTypeRecords) {\n if (typeName.endsWith('@1') || numberOfRecordsWithType.get(typeName) === values.length)\n mainTypes.push(typeName);\n else secondaryTypes.push(typeName);\n }\n\n const calculate = (includedTypes: Set<string>, force: boolean = false) => {\n const result: RecordsWithLabel<T>[] = [];\n for (let i = 0; i < enrichedRecords.length; i++) {\n const r = enrichedRecords[i];\n const includedTrace = r.fullTrace\n .filter((fm) => includedTypes.has(fm.fullType)\n || (forceTraceElements && forceTraceElements.has(fm.type)));\n if (includedTrace.length === 0) {\n if (force)\n result.push({\n label: 'Unlabeled',\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n else return undefined;\n }\n const labelSet = includedTrace\n .map((fm) => fm.label);\n const sep = ops.separator ?? ' / ';\n result.push({\n label: labelSet.join(sep),\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n }\n return result;\n };\n\n if (mainTypes.length === 0) {\n if (secondaryTypes.length !== 0) throw new Error('Non-empty secondary types list while main types list is empty.');\n return calculate(new Set(LabelTypeFull), true)!;\n }\n\n //\n // includedTypes = 2\n // * *\n // T0 T1 T2 T3 T4 T5\n // *\n // additionalType = 3\n //\n // Resulting set: T0, T1, T3\n //\n let includedTypes = 0;\n let additionalType = -1;\n while (includedTypes < mainTypes.length) {\n const currentSet = new Set<string>();\n if (ops.includeNativeLabel) currentSet.add(LabelTypeFull);\n for (let i = 0; i < includedTypes; ++i) currentSet.add(mainTypes[i]);\n if (additionalType >= 0)\n currentSet.add(mainTypes[additionalType]);\n\n const candidateResult = calculate(currentSet);\n\n // checking if labels uniquely separate our records\n if (candidateResult !== undefined && new Set(candidateResult.map((c) => c.label)).size === values.length) return candidateResult;\n\n additionalType++;\n if (additionalType >= mainTypes.length) {\n includedTypes++;\n additionalType = includedTypes;\n }\n }\n\n return calculate(new Set([...mainTypes, ...secondaryTypes]), true)!;\n}\n"],"names":[],"mappings":";;;AAmBO,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC;AACjC,IAAA,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;AAChB,IAAA,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACjC,IAAA,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACzB,IAAA,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;AAClB,CAAA;AAIM,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,UAAU;AAWvC,MAAM,eAAe,GAAG,KAAK;AAE7B,MAAM,SAAS,GAAG,WAAW;AAC7B,MAAM,aAAa,GAAG,aAAa;AAE7B,SAAU,YAAY,CAC1B,MAAW,EACX,aAA8C,EAC9C,MAA0B,EAAE,EAAA;AAE5B,IAAA,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB;AAE7C,IAAA,MAAM,kBAAkB,GAAG,CAAC,GA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,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,MAAM;AAAE,YAAA,OAAO,eAAe;AAEhI,QAAA,cAAc,EAAE;AAChB,QAAA,IAAI,cAAc,IAAI,SAAS,CAAC,MAAM,EAAE;AACtC,YAAA,aAAa,EAAE;YACf,cAAc,GAAG,aAAa;QAChC;IACF;AAEA,IAAA,OAAO,SAAS,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,SAAS,EAAE,GAAG,cAAc,CAAC,CAAC,EAAE,IAAI,CAAE;AACrE;;;;"}
1
+ {"version":3,"file":"label.js","sources":["../../../src/render/util/label.ts"],"sourcesContent":["import { Annotation, parseJson, readAnnotation, type PObjectSpec } from '@milaboratories/pl-model-common';\nimport { z } from 'zod';\n\nexport type RecordsWithLabel<T> = {\n value: T;\n label: string;\n};\n\nexport type LabelDerivationOps = {\n /** Force inclusion of native column label */\n includeNativeLabel?: boolean;\n /** Separator to use between label parts (\" / \" by default) */\n separator?: string;\n /** If true, label will be added as suffix (at the end of the generated label). By default label added as a prefix. */\n addLabelAsSuffix?: boolean;\n /** Trace elements list that will be forced to be included in the label. */\n forceTraceElements?: string[];\n};\n\nexport const TraceEntry = z.object({\n type: z.string(),\n importance: z.number().optional(),\n id: z.string().optional(),\n label: z.string(),\n});\nexport type TraceEntry = z.infer<typeof TraceEntry>;\ntype FullTraceEntry = TraceEntry & { fullType: string; occurrenceIndex: number };\n\nexport const Trace = z.array(TraceEntry);\nexport type Trace = z.infer<typeof Trace>;\ntype FullTrace = FullTraceEntry[];\n\n// Define the possible return types for the specExtractor function\ntype SpecExtractorResult = PObjectSpec | {\n spec: PObjectSpec;\n prefixTrace?: TraceEntry[];\n suffixTrace?: TraceEntry[];\n};\n\nconst DistancePenalty = 0.001;\n\nconst LabelType = '__LABEL__';\nconst LabelTypeFull = '__LABEL__@1';\n\nexport function deriveLabels<T>(\n values: T[],\n specExtractor: (obj: T) => SpecExtractorResult,\n ops: LabelDerivationOps = {},\n): RecordsWithLabel<T>[] {\n const importances = new Map<string, number>();\n\n const forceTraceElements = (ops.forceTraceElements !== undefined && ops.forceTraceElements.length > 0)\n ? new Set(ops.forceTraceElements)\n : undefined;\n\n // number of times certain type occurred among all of the\n const numberOfRecordsWithType = new Map<string, number>();\n\n const enrichedRecords = values.map((value) => {\n const extractorResult = specExtractor(value);\n let spec: PObjectSpec;\n let prefixTrace: TraceEntry[] | undefined;\n let suffixTrace: TraceEntry[] | undefined;\n\n // Check if the result is the new structure or just PObjectSpec\n if ('spec' in extractorResult && typeof extractorResult.spec === 'object') {\n // It's the new structure { spec, prefixTrace?, suffixTrace? }\n spec = extractorResult.spec;\n prefixTrace = extractorResult.prefixTrace;\n suffixTrace = extractorResult.suffixTrace;\n } else {\n // It's just PObjectSpec\n spec = extractorResult as PObjectSpec;\n }\n\n const label = readAnnotation(spec, Annotation.Label);\n const traceStr = readAnnotation(spec, Annotation.Trace);\n const baseTrace = (traceStr ? Trace.safeParse(parseJson(traceStr)).data : undefined) ?? [];\n\n const trace = [\n ...(prefixTrace ?? []),\n ...baseTrace,\n ...(suffixTrace ?? []),\n ];\n\n if (label !== undefined) {\n const labelEntry = { label, type: LabelType, importance: -2 };\n if (ops.addLabelAsSuffix) trace.push(labelEntry);\n else trace.splice(0, 0, labelEntry);\n }\n\n const fullTrace: FullTrace = [];\n\n const occurrences = new Map<string, number>();\n for (let i = trace.length - 1; i >= 0; --i) {\n const { type: typeName } = trace[i];\n const importance = trace[i].importance ?? 0;\n const occurrenceIndex = (occurrences.get(typeName) ?? 0) + 1;\n occurrences.set(typeName, occurrenceIndex);\n const fullType = `${typeName}@${occurrenceIndex}`;\n numberOfRecordsWithType.set(fullType, (numberOfRecordsWithType.get(fullType) ?? 0) + 1);\n importances.set(\n fullType,\n Math.max(\n importances.get(fullType) ?? Number.NEGATIVE_INFINITY,\n importance - (trace.length - i) * DistancePenalty,\n ),\n );\n fullTrace.push({ ...trace[i], fullType, occurrenceIndex: occurrenceIndex });\n }\n fullTrace.reverse();\n return {\n value,\n spec,\n label,\n fullTrace,\n };\n });\n\n // excluding repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const mainTypes: string[] = [];\n // repeated types (i.e. ..@2, ..@3, etc.) not found in some records\n const secondaryTypes: string[] = [];\n\n const allTypeRecords = [...importances];\n // sorting: most important types go first\n allTypeRecords.sort(([, i1], [, i2]) => i2 - i1);\n\n for (const [typeName] of allTypeRecords) {\n if (typeName.endsWith('@1') || numberOfRecordsWithType.get(typeName) === values.length)\n mainTypes.push(typeName);\n else secondaryTypes.push(typeName);\n }\n\n const calculate = (includedTypes: Set<string>, force: boolean = false) => {\n const result: RecordsWithLabel<T>[] = [];\n for (let i = 0; i < enrichedRecords.length; i++) {\n const r = enrichedRecords[i];\n const includedTrace = r.fullTrace\n .filter((fm) => includedTypes.has(fm.fullType)\n || (forceTraceElements && forceTraceElements.has(fm.type)));\n if (includedTrace.length === 0) {\n if (force)\n result.push({\n label: 'Unlabeled',\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n else return undefined;\n }\n const labelSet = includedTrace\n .map((fm) => fm.label);\n const sep = ops.separator ?? ' / ';\n result.push({\n label: labelSet.join(sep),\n value: r.value,\n } satisfies RecordsWithLabel<T>);\n }\n return result;\n };\n\n const countUniqueLabels = (result: RecordsWithLabel<T>[] | undefined): number =>\n result === undefined ? 0 : new Set(result.map((c) => c.label)).size;\n\n // Post-processing: try removing types one by one (lowest importance first) to minimize the label set\n // Accepts removal if it doesn't decrease the number of unique labels (cardinality)\n const minimizeTypeSet = (typeSet: Set<string>): Set<string> => {\n const initialResult = calculate(typeSet);\n if (initialResult === undefined) {\n return typeSet;\n }\n const currentCardinality = countUniqueLabels(initialResult);\n\n // Get types sorted by importance ascending (lowest first), excluding forced elements\n const removableSorted = [...typeSet]\n .filter((t) =>\n !forceTraceElements?.has(t.split('@')[0])\n && !(ops.includeNativeLabel && t === LabelTypeFull))\n .sort((a, b) => (importances.get(a) ?? 0) - (importances.get(b) ?? 0));\n\n for (const typeToRemove of removableSorted) {\n const reducedSet = new Set(typeSet);\n reducedSet.delete(typeToRemove);\n const candidateResult = calculate(reducedSet);\n if (candidateResult !== undefined && countUniqueLabels(candidateResult) >= currentCardinality) {\n typeSet.delete(typeToRemove);\n }\n }\n return typeSet;\n };\n\n if (mainTypes.length === 0) {\n if (secondaryTypes.length !== 0) throw new Error('Non-empty secondary types list while main types list is empty.');\n return calculate(new Set(LabelTypeFull), true)!;\n }\n\n //\n // includedTypes = 2\n // * *\n // T0 T1 T2 T3 T4 T5\n // *\n // additionalType = 3\n //\n // Resulting set: T0, T1, T3\n //\n let includedTypes = 0;\n let additionalType = -1;\n while (includedTypes < mainTypes.length) {\n const currentSet = new Set<string>();\n if (ops.includeNativeLabel) currentSet.add(LabelTypeFull);\n for (let i = 0; i < includedTypes; ++i) currentSet.add(mainTypes[i]);\n if (additionalType >= 0)\n currentSet.add(mainTypes[additionalType]);\n\n const candidateResult = calculate(currentSet);\n\n if (candidateResult !== undefined && countUniqueLabels(candidateResult) === values.length) {\n minimizeTypeSet(currentSet);\n return calculate(currentSet)!;\n }\n\n additionalType++;\n if (additionalType >= mainTypes.length) {\n includedTypes++;\n additionalType = includedTypes;\n }\n }\n\n // Fallback: include all types, then try to minimize\n const fallbackSet = new Set([...mainTypes, ...secondaryTypes]);\n minimizeTypeSet(fallbackSet);\n return calculate(fallbackSet, true)!;\n}\n"],"names":[],"mappings":";;;AAmBO,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC;AACjC,IAAA,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;AAChB,IAAA,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACjC,IAAA,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;AACzB,IAAA,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;AAClB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ACzD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,aAAa,EAAE,EAAE,CAAC;YAAE,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACpE,IAAI,cAAc,IAAI,CAAC;YACrB,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC;AAE3C,QAAA,MAAM,eAAe,GAAG,SAAS,CAAC,UAAU,CAAC;AAE7C,QAAA,IAAI,eAAe,KAAK,SAAS,IAAI,iBAAiB,CAAC,eAAe,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE;YACzF,eAAe,CAAC,UAAU,CAAC;AAC3B,YAAA,OAAO,SAAS,CAAC,UAAU,CAAE;QAC/B;AAEA,QAAA,cAAc,EAAE;AAChB,QAAA,IAAI,cAAc,IAAI,SAAS,CAAC,MAAM,EAAE;AACtC,YAAA,aAAa,EAAE;YACf,cAAc,GAAG,aAAa;QAChC;IACF;;AAGA,IAAA,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,SAAS,EAAE,GAAG,cAAc,CAAC,CAAC;IAC9D,eAAe,CAAC,WAAW,CAAC;AAC5B,IAAA,OAAO,SAAS,CAAC,WAAW,EAAE,IAAI,CAAE;AACtC;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platforma-sdk/model",
3
- "version": "1.51.5",
3
+ "version": "1.51.9",
4
4
  "description": "Platforma.bio SDK / Block Model",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
@@ -23,8 +23,8 @@
23
23
  "canonicalize": "~2.1.0",
24
24
  "es-toolkit": "^1.39.10",
25
25
  "zod": "~3.23.8",
26
- "@milaboratories/ptabler-expression-js": "1.1.9",
27
26
  "@milaboratories/pl-model-common": "1.23.0",
27
+ "@milaboratories/ptabler-expression-js": "1.1.9",
28
28
  "@milaboratories/pl-error-like": "1.12.5"
29
29
  },
30
30
  "devDependencies": {
@@ -32,9 +32,9 @@
32
32
  "vitest": "^4.0.16",
33
33
  "@vitest/coverage-istanbul": "^4.0.16",
34
34
  "fast-json-patch": "^3.1.1",
35
- "@milaboratories/build-configs": "1.2.1",
35
+ "@milaboratories/build-configs": "1.2.2",
36
36
  "@milaboratories/eslint-config": "1.0.5",
37
- "@milaboratories/ts-builder": "1.2.1",
37
+ "@milaboratories/ts-builder": "1.2.2",
38
38
  "@milaboratories/helpers": "1.13.0",
39
39
  "@milaboratories/ts-configs": "1.2.0"
40
40
  },
@@ -119,6 +119,87 @@ test('test fallback to native labels in label derivation', () => {
119
119
  ]);
120
120
  });
121
121
 
122
+ test.each<{ name: string; traces: Trace[]; labels: string[] }>([
123
+ {
124
+ name: 'removes redundant low-importance type when high-importance alone suffices',
125
+ traces: [
126
+ [
127
+ { type: 't1', importance: 10, label: 'High1' },
128
+ { type: 't2', importance: 1, label: 'Low1' }
129
+ ],
130
+ [
131
+ { type: 't1', importance: 10, label: 'High2' },
132
+ { type: 't2', importance: 1, label: 'Low2' }
133
+ ]
134
+ ],
135
+ // Both t1 and t2 distinguish, but t2 (low importance) should be removed since t1 alone suffices
136
+ labels: ['High1', 'High2']
137
+ },
138
+ {
139
+ name: 'keeps both types when both are needed for uniqueness',
140
+ traces: [
141
+ [
142
+ { type: 't1', importance: 10, label: 'A' },
143
+ { type: 't2', importance: 1, label: 'X' }
144
+ ],
145
+ [
146
+ { type: 't1', importance: 10, label: 'A' },
147
+ { type: 't2', importance: 1, label: 'Y' }
148
+ ],
149
+ [
150
+ { type: 't1', importance: 10, label: 'B' },
151
+ { type: 't2', importance: 1, label: 'Y' }
152
+ ]
153
+ ],
154
+ // Neither t1 nor t2 alone can distinguish all three, need both
155
+ labels: ['A / X', 'A / Y', 'B / Y']
156
+ },
157
+ {
158
+ name: 'removes multiple redundant types greedily',
159
+ traces: [
160
+ [
161
+ { type: 't1', importance: 100, label: 'Unique1' },
162
+ { type: 't2', importance: 10, label: 'Same' },
163
+ { type: 't3', importance: 1, label: 'Same' }
164
+ ],
165
+ [
166
+ { type: 't1', importance: 100, label: 'Unique2' },
167
+ { type: 't2', importance: 10, label: 'Same' },
168
+ { type: 't3', importance: 1, label: 'Same' }
169
+ ]
170
+ ],
171
+ // t1 alone distinguishes; t2 and t3 are redundant and should be removed
172
+ labels: ['Unique1', 'Unique2']
173
+ },
174
+ {
175
+ name: 'fallback case: removes types that do not reduce cardinality',
176
+ traces: [
177
+ // Two columns with identical traces - cannot be distinguished
178
+ [
179
+ { type: 't1', importance: 100, label: 'A' },
180
+ { type: 't2', importance: 10, label: 'X' },
181
+ { type: 't3', importance: 1, label: 'Same' }
182
+ ],
183
+ [
184
+ { type: 't1', importance: 100, label: 'A' },
185
+ { type: 't2', importance: 10, label: 'X' },
186
+ { type: 't3', importance: 1, label: 'Same' }
187
+ ],
188
+ // Third column is different
189
+ [
190
+ { type: 't1', importance: 100, label: 'B' },
191
+ { type: 't2', importance: 10, label: 'Y' },
192
+ { type: 't3', importance: 1, label: 'Same' }
193
+ ]
194
+ ],
195
+ // Cannot achieve full uniqueness (2 columns are identical), but t3 (Same) can be removed
196
+ // since it doesn't help distinguish anything. t1 alone gives cardinality 2.
197
+ labels: ['A', 'A', 'B']
198
+ }
199
+ ])('test label minimization: $name', ({ traces, labels }) => {
200
+ expect(deriveLabels(tracesToSpecs(traces), (s) => s).map((r) => r.label)).toEqual(labels);
201
+ });
202
+
122
203
  test.each<{ name: string; traces: Trace[]; labels: string[]; forceTraceElements: string[] }>([
123
204
  {
124
205
  name: 'force one element',
@@ -158,6 +158,36 @@ export function deriveLabels<T>(
158
158
  return result;
159
159
  };
160
160
 
161
+ const countUniqueLabels = (result: RecordsWithLabel<T>[] | undefined): number =>
162
+ result === undefined ? 0 : new Set(result.map((c) => c.label)).size;
163
+
164
+ // Post-processing: try removing types one by one (lowest importance first) to minimize the label set
165
+ // Accepts removal if it doesn't decrease the number of unique labels (cardinality)
166
+ const minimizeTypeSet = (typeSet: Set<string>): Set<string> => {
167
+ const initialResult = calculate(typeSet);
168
+ if (initialResult === undefined) {
169
+ return typeSet;
170
+ }
171
+ const currentCardinality = countUniqueLabels(initialResult);
172
+
173
+ // Get types sorted by importance ascending (lowest first), excluding forced elements
174
+ const removableSorted = [...typeSet]
175
+ .filter((t) =>
176
+ !forceTraceElements?.has(t.split('@')[0])
177
+ && !(ops.includeNativeLabel && t === LabelTypeFull))
178
+ .sort((a, b) => (importances.get(a) ?? 0) - (importances.get(b) ?? 0));
179
+
180
+ for (const typeToRemove of removableSorted) {
181
+ const reducedSet = new Set(typeSet);
182
+ reducedSet.delete(typeToRemove);
183
+ const candidateResult = calculate(reducedSet);
184
+ if (candidateResult !== undefined && countUniqueLabels(candidateResult) >= currentCardinality) {
185
+ typeSet.delete(typeToRemove);
186
+ }
187
+ }
188
+ return typeSet;
189
+ };
190
+
161
191
  if (mainTypes.length === 0) {
162
192
  if (secondaryTypes.length !== 0) throw new Error('Non-empty secondary types list while main types list is empty.');
163
193
  return calculate(new Set(LabelTypeFull), true)!;
@@ -183,8 +213,10 @@ export function deriveLabels<T>(
183
213
 
184
214
  const candidateResult = calculate(currentSet);
185
215
 
186
- // checking if labels uniquely separate our records
187
- if (candidateResult !== undefined && new Set(candidateResult.map((c) => c.label)).size === values.length) return candidateResult;
216
+ if (candidateResult !== undefined && countUniqueLabels(candidateResult) === values.length) {
217
+ minimizeTypeSet(currentSet);
218
+ return calculate(currentSet)!;
219
+ }
188
220
 
189
221
  additionalType++;
190
222
  if (additionalType >= mainTypes.length) {
@@ -193,5 +225,8 @@ export function deriveLabels<T>(
193
225
  }
194
226
  }
195
227
 
196
- return calculate(new Set([...mainTypes, ...secondaryTypes]), true)!;
228
+ // Fallback: include all types, then try to minimize
229
+ const fallbackSet = new Set([...mainTypes, ...secondaryTypes]);
230
+ minimizeTypeSet(fallbackSet);
231
+ return calculate(fallbackSet, true)!;
197
232
  }