diff --git a/src/did/+did2/+convert/+migrators_j/element.m b/src/did/+did2/+convert/+migrators_j/element.m index 42e85d02..8c079f94 100644 --- a/src/did/+did2/+convert/+migrators_j/element.m +++ b/src/did/+did2/+convert/+migrators_j/element.m @@ -11,8 +11,9 @@ % -- now to an entity. % % 1 -> N: -% - entity the element itself (id preserved; name/reference -> -% local_identifier ' | '). +% - entity the element itself (id preserved; name -> `name`, +% exactly; name/reference -> local_identifier +% ' | '). % - term_assertion(s) its kind, nothing dropped: `type` -> variable % "element type"; `ndi_element_class` -> variable "ndi % element class" (the MATLAB reconstruction handle; kept @@ -90,17 +91,28 @@ % % base.id / session_id / datestamp -> the entity's id / session edge / % creation_timestamp -% base.name -> entity.name when not empty ('', what -% ndi.element/newdocument writes, writes -% nothing; jBaseName; Decided by Jess Haley, -% 2026-10-27 [v1 name is the entity name]) +% base.name -> nothing: '' is what ndi.element/newdocument +% writes, and the round trip rebuilds ''. A +% non-empty one is carried, malformed +% (carry-149; Decided by Jess Haley, +% 2026-10-30 [element name kept apart, +% base.name carried]): the entity's one +% `name` holds element.name +% element.name -> entity.name, exactly (Decided by Jess Haley, +% 2026-10-29 [element name and reference +% stored apart]); not written when empty % element.name, element.reference -> entity.local_identifier, -% ' | ' (NDI's display form; -% did2.convert.elementLocalIdentifier, the -% mirror of NDI ndi.vintage's). Not reversible -% (an empty name, a non-integer reference, a -% reference-less name that reads as a pair): -% carried (carry-048 / carry-055) +% ' | ' (did2.convert. +% elementHandle; int2str for an integer, as +% NDI's display form and +% did2.convert.elementLocalIdentifier write +% it). The round trip reads the reference as +% what follows the known name, so a name +% holding ' | ' or ' (ref ', and a +% non-integer reference, rebuild exactly. +% Carried only when both are empty +% (carry-048) or the reference is neither one +% number nor one text (carry-120) % element.type, element.ndi_element_class -> the entity type % (elementEntityType) and one term assertion % each; a decided device kind adds one @@ -205,27 +217,51 @@ underlyingId = depValue(preBody, 'underlying_element_id'); sortingRunId = depValue(preBody, 'jrclust_clusters_id'); -% [element local identifier is name and reference] (Jess Haley, 2026-10-26): -% ' | ', NDI's display form, read back by splitting at the last -% ' | '. [no invented values] / [generic stage is lossless]: an identifier -% that would not read back exactly is not written; the element is carried. -localId = ''; -if isempty(name) || ischar(name) || (isstring(name) && isscalar(name)) - localId = did2.convert.elementLocalIdentifier(char(name), reference); -end -if isempty(localId) - nameText = ''; - if ischar(name) || (isstring(name) && isscalar(name)); nameText = char(name); end - row = '[carry-055] '; - if isempty(nameText) && isEmptyRef(reference); row = '[carry-048] '; end - % carry-register: carry-048 (no name and no reference); carry-055 (not - % written reversibly as ' | ') +% [element name and reference stored apart] (Jess Haley, 2026-10-29): the v1 +% name, exactly, is the entity's `name`; `local_identifier` keeps the +% name-and-reference handle (' | ', [element local identifier +% is name and reference], 2026-10-26), and the round trip reads the reference +% as what follows the known name (did2.convert.elementHandle / +% elementNameReferenceApart). A name that itself reads as a pair, or a +% non-integer reference, now converts (no longer carried as unpackable). +nameText = ''; +if ischar(name) || (isstring(name) && isscalar(name)); nameText = char(name); end +[localId, problem] = did2.convert.elementHandle(name, reference); +if ~isempty(problem) + switch problem + case 'empty' + row = '[carry-048] needs_mapping: '; + otherwise + row = '[carry-120] malformed: '; + end + % carry-register: carry-048 (no name and no reference: the identifier + % would be invented); carry-120 (a name that is not text, or a reference + % that is neither one number nor one text: NDI-matlab main 441530d0 + % +ndi/element.m shows `reference` with int2str and searches it with + % 'exact_number', so its writer stores one number) did2.convert.reportNote('carried_as_v1', 'element', elementId, ... - sprintf('%sunplaced: element.name "%s" / element.reference "%s" (no reversible local_identifier)', ... + sprintf('%selement.name "%s" / element.reference "%s" (no local_identifier)', ... row, nameText, refText(reference))); v2Body = {preBody}; return; end +% The entity has ONE `name`, and [element name and reference stored apart] +% gives it the element's v1 name. A non-empty v1 base.name then has no field +% of its own. ndi.element/newdocument writes base.name '' (NDI-matlab main +% 441530d0, +ndi/element.m newdocument sets element.* only), so a non-empty +% one is a value no NDI writer produces: Decided by Jess Haley, 2026-10-30 +% [element name kept apart, base.name carried] (did-schema +% V_eta_go_forward_class_audit.md), the element is carried whole as v1, +% malformed. +baseName = jBaseName(preBody); +if ~isempty(baseName) + % carry-register: carry-149 (an element whose base.name is not empty) + did2.convert.reportNote('carried_as_v1', 'element', elementId, ... + sprintf(['[carry-149] malformed: base.name "%s" (the entity''s `name` holds ' ... + 'element.name "%s"; [element name kept apart, base.name carried])'], baseName, nameText)); + v2Body = {preBody}; + return; +end [entityType, why] = elementEntityType(type, ndiClass, elementId); if isempty(entityType) @@ -258,7 +294,7 @@ % (V_eta_entity_composition_plan.md sec. 4), read off the v1 `type` and % `ndi_element_class` by elementEntityType (decided, above). subjectDoc = jEntity(preBody, entityType, ... - struct('local_identifier', localId, 'name', jBaseName(preBody)), 'Id', elementId); + struct('local_identifier', localId, 'name', nameText), 'Id', elementId); bodies = {subjectDoc}; @@ -494,10 +530,6 @@ end end -function tf = isEmptyRef(r) -tf = isempty(r) || ((ischar(r) || isstring(r)) && isempty(strtrim(char(r)))); -end - function s = refText(r) if ischar(r) || (isstring(r) && isscalar(r)) s = char(r); diff --git a/src/did/+did2/+convert/+migrators_j/private/jBaseName.m b/src/did/+did2/+convert/+migrators_j/private/jBaseName.m index 1cf1b90c..79be0a4d 100644 --- a/src/did/+did2/+convert/+migrators_j/private/jBaseName.m +++ b/src/did/+did2/+convert/+migrators_j/private/jBaseName.m @@ -12,9 +12,12 @@ % trimming), so the round trip reads it back exactly. A name that is not % text writes nothing, and the round trip reports it lost. % -% Callers: did2.convert.migrators_j.subject, .subject_group, .element, -% .session, .dataset_remote; each inverse (did2.convert.roundtrip.) -% rebuilds base.name from entity.name, '' when the entity has none. +% Callers: did2.convert.migrators_j.subject, .subject_group, .session, +% .dataset_remote; each inverse (did2.convert.roundtrip.) rebuilds +% base.name from entity.name, '' when the entity has none. .element reads it +% only to carry an element whose base.name is not empty: since [element name +% and reference stored apart] (2026-10-29) an element's entity.name is its +% element.name. name = ''; b = did2.convert.v1read.readBase(preBody); diff --git a/src/did/+did2/+convert/+roundtrip/element.m b/src/did/+did2/+convert/+roundtrip/element.m index f64405ca..9ced18a5 100644 --- a/src/did/+did2/+convert/+roundtrip/element.m +++ b/src/did/+did2/+convert/+roundtrip/element.m @@ -8,13 +8,24 @@ % 3cb19a8, as ndi.element/newdocument writes it): % % base.id, session_id, datestamp the entity (rebuildBase) -% base.name entity.name ('' when the entity has -% none; [v1 name is the entity name], -% Jess Haley, 2026-10-27) -% element.name, element.reference entity.local_identifier, read back by -% did2.convert.elementNameReference -% (' | '; the name alone -% for an element with no reference) +% element.name entity.name, exactly (Decided by Jess +% Haley, 2026-10-29 [element name and +% reference stored apart]) +% element.reference what follows that name in +% entity.local_identifier +% (' | ', or the name +% alone for no reference; a number when +% it reads as one), by +% did2.convert.elementNameReferenceApart +% base.name '' (ndi.element/newdocument writes ''; +% the migrator carries an element whose +% base.name is not empty) +% A DOCUMENT MIGRATED BEFORE 2026-10-29 (its entity.name, if any, is the +% v1 base.name, [v1 name is the entity name], 2026-10-27, and does not +% lead its local_identifier): element.name and element.reference are +% read from local_identifier alone, did2.convert.elementNameReference +% (the split at the last ' | ', or the old ' (ref X)' form), and +% base.name is entity.name ('' when the entity has none). % element.type the 'element type' assertion's value % element.ndi_element_class the 'ndi element class' assertion's value % (a device's Equipment Type assertion states its device kind, @@ -66,7 +77,12 @@ return; end [base, entity] = rebuildBase(ent); -[name, reference] = did2.convert.elementNameReference(fieldText(entity, 'local_identifier')); +[name, reference, apart] = did2.convert.elementNameReferenceApart( ... + fieldText(entity, 'name'), fieldText(entity, 'local_identifier')); +baseName = ''; +if ~apart + baseName = fieldText(entity, 'name'); % migrated before 2026-10-29 +end type = ''; ndiClass = ''; @@ -150,7 +166,7 @@ end v1.depends_on = struct('name', names, 'value', values); v1.base = struct('id', base.id, 'session_id', base.session_id, ... - 'name', fieldText(entity, 'name'), 'datestamp', base.datestamp); + 'name', baseName, 'datestamp', base.datestamp); blk = struct('ndi_element_class', ndiClass, 'name', name, 'type', type); blk.reference = reference; if ~isempty(direct) diff --git a/src/did/+did2/+convert/+roundtrip/private/manifestProbemap.m b/src/did/+did2/+convert/+roundtrip/private/manifestProbemap.m index ef772197..44944cee 100644 --- a/src/did/+did2/+convert/+roundtrip/private/manifestProbemap.m +++ b/src/did/+did2/+convert/+roundtrip/private/manifestProbemap.m @@ -19,9 +19,12 @@ % each naming its rig and channels through an `acquisition_channels` % document. A row is read back from its statement: % -% name, reference the instrument_id entity's local_identifier -% (did2.convert.elementNameReference; the probe map -% writes the reference with int2str) +% name, reference the instrument_id entity's `name` and what follows it +% in its local_identifier +% (did2.convert.elementNameReferenceApart, [element name +% and reference stored apart]; the local_identifier alone +% for an entity migrated before it; the probe map writes +% the reference with int2str) % type the instrument's `element type` assertion (the v1 % element.type, kept by +migrators_j/element.m) % devicestring the acquisition_channels' acquisition_system name, ':', @@ -134,9 +137,13 @@ if isempty(inst) why{end+1} = sprintf('statement %s: its instrument is not in the batch', char(st.base.id)); else - [name, r] = did2.convert.elementNameReference(fieldText(getIn(inst, 'entity'), 'local_identifier')); - if isnumeric(r) && ~isempty(r) + instEntity = getIn(inst, 'entity'); + [name, r] = did2.convert.elementNameReferenceApart(fieldText(instEntity, 'name'), ... + fieldText(instEntity, 'local_identifier')); + if isnumeric(r) && isscalar(r) && isfinite(r) && r == round(r) ref = int2str(r); + elseif isnumeric(r) && isscalar(r) + ref = num2str(r, 17); elseif ischar(r) || isstring(r) ref = char(r); end diff --git a/src/did/+did2/+convert/elementHandle.m b/src/did/+did2/+convert/elementHandle.m new file mode 100644 index 00000000..74387fa7 --- /dev/null +++ b/src/did/+did2/+convert/elementHandle.m @@ -0,0 +1,106 @@ +function [localId, problem] = elementHandle(name, reference) +%ELEMENTHANDLE A migrated element's local_identifier, written beside its name. +% +% [LOCALID, PROBLEM] = did2.convert.elementHandle(NAME, REFERENCE) +% +% Decided by Jess Haley, 2026-10-29 [element name and reference stored +% apart] (did-schema V_eta_go_forward_class_audit.md): a migrated `element` +% writes its v1 name, exactly, to the entity's `name`, and keeps the +% name-and-reference handle in `local_identifier` as the dataset-unique +% handle; the reference is read back as what follows the KNOWN name +% (did2.convert.elementNameReferenceApart), so every name and reference +% rebuild, including a name that itself reads as a pair. It replaces +% the carry of a pair that could not be packed reversibly. +% +% The handle keeps today's form ([element local identifier is name and +% reference], 2026-10-26, NDI's display form): +% +% NAME no reference +% NAME ' | ' an integer reference: int2str, exactly +% what did2.convert.elementLocalIdentifier +% (the mirror of NDI's) writes, so the +% common element's identifier is unchanged +% and the epoch probe-map joins +% (epochMint, resolveEpochProbemap) still +% meet it +% NAME ' | ' any other number: num2str(R, 17), which +% str2double reads back to the same double +% NAME ' | ' a text reference, verbatim (read back as +% a number when it is one, as today) +% +% NAME is written as it is (no trimming). An empty NAME writes ' | ' +% (the entity then has no `name`; the reader reads the empty name from the +% handle's leading ' | '). +% +% PROBLEM is '' when LOCALID was written, else (LOCALID ''): +% 'empty' NAME and REFERENCE are both empty: the identifier would be +% invented (the caller carries; carry-048) +% 'reference' REFERENCE is neither one number nor one text (an array, a +% struct, ...): no NDI writer produces it (the caller +% carries; carry-120) +% 'name' NAME is not text +% +% See also did2.convert.elementNameReferenceApart, +% did2.convert.elementLocalIdentifier, did2.convert.migrators_j.element. + +arguments + name + reference = [] +end + +localId = ''; +problem = ''; +if isstring(name) && isscalar(name) + name = char(name); +end +if isempty(name) && ~ischar(name) + name = ''; +end +if ~ischar(name) || (~isempty(name) && ~isrow(name)) + problem = 'name'; + return; +end +[refText, ok] = referenceText(reference); +if ~ok + problem = 'reference'; + return; +end +if isempty(refText) + if isempty(name) + problem = 'empty'; + return; + end + localId = name; + return; +end +localId = [name ' | ' refText]; +end + +function [s, ok] = referenceText(r) +% the reference as the handle writes it; '' for no reference +s = ''; +ok = true; +if isempty(r) && ~isstruct(r) && ~iscell(r) + return; % [] or '' or "" +end +if isstring(r) && isscalar(r) + r = char(r); + if isempty(r) + return; % "" + end +end +if ischar(r) && isrow(r) + s = r; + return; +end +if (isnumeric(r) || islogical(r)) && isscalar(r) && isreal(r) + x = double(r); + if isfinite(x) && x == round(x) + s = int2str(x); + else + s = num2str(x, 17); + end + return; +end +ok = false; +end diff --git a/src/did/+did2/+convert/elementLocalIdentifier.m b/src/did/+did2/+convert/elementLocalIdentifier.m index cf856510..d37174e8 100644 --- a/src/did/+did2/+convert/elementLocalIdentifier.m +++ b/src/did/+did2/+convert/elementLocalIdentifier.m @@ -27,11 +27,20 @@ % migration's "carried as v1") rather than writing a form that reads back % as something else. % +% SINCE [element name and reference stored apart] (Jess Haley, 2026-10-29) +% DID-matlab's migrator no longer calls this: it writes the name to the +% entity's `name` and the identifier with did2.convert.elementHandle, which +% writes the same text for every pair this function accepts and also +% writes the pairs it refuses (read back by +% did2.convert.elementNameReferenceApart, from the known name). This +% function is kept as the mirror of NDI's. +% % A MIRROR of NDI-matlab's ndi.vintage.elementLocalIdentifier (branch % claude/phase3, 471cd5c66): the same rules, so NDI's writer and reader and % DID's migrator agree on every identifier. Change both together. % -% See also did2.convert.elementNameReference, did2.convert.migrators_j.element, +% See also did2.convert.elementNameReference, did2.convert.elementHandle, +% did2.convert.migrators_j.element, % NDI ndi.vintage.elementLocalIdentifier, ndi.element/elementstring. arguments diff --git a/src/did/+did2/+convert/elementNameReference.m b/src/did/+did2/+convert/elementNameReference.m index f04f9b84..88014100 100644 --- a/src/did/+did2/+convert/elementNameReference.m +++ b/src/did/+did2/+convert/elementNameReference.m @@ -22,10 +22,17 @@ % The display form is tried first and cannot be mistaken for the old one: % an old identifier ends in ')', never in ' | '. % +% SINCE [element name and reference stored apart] (Jess Haley, 2026-10-29) +% a migrated element's name is the entity's `name`; DID-matlab's readers +% call did2.convert.elementNameReferenceApart, which reads the reference +% after that name and falls back to this parse for an entity migrated +% before it. +% % A MIRROR of NDI-matlab's ndi.vintage.elementNameReference (branch % claude/phase3, 471cd5c66): the same rules. Change both together. % -% See also did2.convert.elementLocalIdentifier, did2.convert.roundtrip.element, +% See also did2.convert.elementLocalIdentifier, did2.convert.elementNameReferenceApart, +% did2.convert.roundtrip.element, % NDI ndi.vintage.elementNameReference. arguments diff --git a/src/did/+did2/+convert/elementNameReferenceApart.m b/src/did/+did2/+convert/elementNameReferenceApart.m new file mode 100644 index 00000000..19d1aa79 --- /dev/null +++ b/src/did/+did2/+convert/elementNameReferenceApart.m @@ -0,0 +1,85 @@ +function [name, reference, apart] = elementNameReferenceApart(entityName, localId) +%ELEMENTNAMEREFERENCEAPART A migrated element's name and reference, read from its `name` first. +% +% [NAME, REFERENCE, APART] = did2.convert.elementNameReferenceApart(ENTITYNAME, LOCALID) +% +% The inverse of did2.convert.elementHandle. Decided by Jess Haley, +% 2026-10-29 [element name and reference stored apart] (did-schema +% V_eta_go_forward_class_audit.md): the element's v1 name is the entity's +% `name`, and the reference is what follows that KNOWN name in +% `local_identifier`, so a name holding ' | ' or ' (ref ' reads back +% exactly. ENTITYNAME is the entity's `name` ('' or [] when it has none). +% +% APART is true when the pair was read this way: +% LOCALID equal to ENTITYNAME NAME, REFERENCE [] +% LOCALID = [ENTITYNAME ' | ' X] NAME, REFERENCE X +% LOCALID = [ENTITYNAME ' (ref ' X ')'] NAME, REFERENCE X (the form +% migrated documents carried +% before 2026-10-26) +% no ENTITYNAME and LOCALID = [' | ' X] NAME '', REFERENCE X (an +% element with an empty name) +% X is a number when str2double reads it as one (v1 `reference` is numeric +% on every NDI-written element; elementHandle writes a number so that it +% reads back exactly), else the text. +% +% APART is false otherwise: a document migrated before this decision, whose +% `name` (if any) is its v1 base.name ([v1 name is the entity name], +% 2026-10-27) and whose LOCALID alone holds the pair. NAME and REFERENCE +% are then today's parse, did2.convert.elementNameReference(LOCALID), and a +% caller rebuilds base.name from ENTITYNAME. +% +% See also did2.convert.elementHandle, did2.convert.elementNameReference, +% did2.convert.roundtrip.element. + +arguments + entityName + localId +end + +entityName = textOf(entityName); +localId = textOf(localId); +apart = true; +if ~isempty(entityName) + name = entityName; + n = numel(name); + if strcmp(localId, name) + reference = []; + return; + end + if startsWith(localId, [name ' | ']) && numel(localId) > n + 3 + reference = referenceValue(localId(n + 4:end)); + return; + end + if startsWith(localId, [name ' (ref ']) && endsWith(localId, ')') ... + && numel(localId) > n + 7 + reference = referenceValue(localId(n + 7:end - 1)); + return; + end +elseif startsWith(localId, ' | ') && numel(localId) > 3 + name = ''; + reference = referenceValue(localId(4:end)); + return; +end +apart = false; +[name, reference] = did2.convert.elementNameReference(localId); +end + +function v = referenceValue(s) +% a number when the text is one (exactly what elementHandle wrote for a +% numeric reference), else the text +d = str2double(s); +if (~isnan(d) || strcmpi(strtrim(s), 'nan')) && isreal(d) + v = d; +else + v = s; +end +end + +function s = textOf(v) +s = ''; +if isstring(v) && isscalar(v) + s = char(v); +elseif ischar(v) + s = v; +end +end diff --git a/src/did/+did2/+convert/epochMint.m b/src/did/+did2/+convert/epochMint.m index 9dde46fe..fe93681c 100644 --- a/src/did/+did2/+convert/epochMint.m +++ b/src/did/+did2/+convert/epochMint.m @@ -3534,11 +3534,12 @@ %PROBEMAPDEVICES {probe local_identifier, device name} per row of a v1 % epochprobemap (tab-separated, header `name reference type devicestring % subjectstring`). The local_identifier is +migrators_j/element.m's, -% did2.convert.elementLocalIdentifier: 'name | reference', or the name alone +% did2.convert.elementHandle: 'name | reference', or the name alone % ([element local identifier is name and reference], Jess Haley, -% 2026-10-26); a row that cannot be written reversibly matches no element -% (element.m carries such an element). The device is the devicestring before -% ':'. +% 2026-10-26; written beside the entity's `name` since [element name and +% reference stored apart], 2026-10-29); a row that cannot be written +% matches no element (element.m carries such an element). The device is the +% devicestring before ':'. rowsOut = {}; lines = regexp(char(pm), '\r?\n', 'split'); if isempty(lines); return; end @@ -3556,7 +3557,10 @@ if ~isempty(iRef) ref = strtrim(c{iRef}); end - lid = did2.convert.elementLocalIdentifier(nm, ref); + if ~isempty(regexp(ref, '^-?\d+$', 'once')) + ref = str2double(ref); % the int2str text, written back as element.m does + end + lid = did2.convert.elementHandle(nm, ref); if isempty(lid); continue; end dev = strtrim(c{iDev}); colon = find(dev == ':', 1); diff --git a/src/did/+did2/+convert/resolveEpochProbemap.m b/src/did/+did2/+convert/resolveEpochProbemap.m index 309f1a4e..cee3424c 100644 --- a/src/did/+did2/+convert/resolveEpochProbemap.m +++ b/src/did/+did2/+convert/resolveEpochProbemap.m @@ -1854,13 +1854,19 @@ function lid = probeLocalIdentifier(name, reference) %PROBELOCALIDENTIFIER The element-subject local_identifier a probe row maps to. -% +migrators_j/element.m's, did2.convert.elementLocalIdentifier: 'name | -% reference', or the name alone ([element local identifier is name and -% reference], Jess Haley, 2026-10-26). The probemap serialises `reference` -% with int2str; the identifier reads the integer from that text, so the join -% no longer depends on two number formats agreeing. '' when the pair cannot -% be written reversibly (element.m carries such an element; nothing matches). -lid = did2.convert.elementLocalIdentifier(char(name), char(reference)); +% +migrators_j/element.m's, did2.convert.elementHandle: 'name | reference', +% or the name alone ([element local identifier is name and reference], Jess +% Haley, 2026-10-26; written beside the entity's `name` since [element name +% and reference stored apart], 2026-10-29). The probemap serialises +% `reference` with int2str and elementHandle writes an integer reference +% with int2str, so the two meet. '' when the pair cannot be written (both +% empty, or a reference that is not one number or text: element.m carries +% such an element; nothing matches). +if (ischar(reference) || isstring(reference)) ... + && ~isempty(regexp(strtrim(char(reference)), '^-?\d+$', 'once')) + reference = str2double(strtrim(char(reference))); % the int2str text +end +lid = did2.convert.elementHandle(char(name), reference); end % ===================== the epoch files as bodies (2026-10-21) ========== diff --git a/tests/+did2/+unittest/testMigratorsJ.m b/tests/+did2/+unittest/testMigratorsJ.m index 3ecee6da..6b938a4e 100644 --- a/tests/+did2/+unittest/testMigratorsJ.m +++ b/tests/+did2/+unittest/testMigratorsJ.m @@ -2628,7 +2628,10 @@ function testDeferredBathIsAManipulationValuedByItsFormulation(testCase) 'superclasses', struct('class_name', 'base', 'class_version', '1.0.0')); elem.depends_on = struct('name', {'subject_id'}, 'value', {subjId}); elem.base = struct('id', stimId, 'session_id', 'aabb1122ccdd3344_9900aabbccddeeff', ... - 'name', 'stimulator', 'datestamp', '2024-06-01T12:00:00.000Z'); + 'name', '', 'datestamp', '2024-06-01T12:00:00.000Z'); +% base.name '': what ndi.element/newdocument writes (NDI-matlab main 441530d0, +% +ndi/element.m newdocument sets element.* only); a non-empty one is carried +% since [element name and reference stored apart] (+migrators_j/element.m) % a stimulator element is an ndi.probe.timeseries.stimulator (NDI % probetype2object.json): [element types have no default] makes a probe type a % device only on an ndi.probe.* class (Jess Haley, 2026-10-26) @@ -2729,7 +2732,10 @@ function testDeferredGenericBathHasNoLocationObservation(testCase) 'superclasses', struct('class_name', 'base', 'class_version', '1.0.0')); elem.depends_on = struct('name', {'subject_id'}, 'value', {subjId}); elem.base = struct('id', stimId, 'session_id', 'aabb1122ccdd3344_9900aabbccddeeff', ... - 'name', 'stimulator', 'datestamp', '2024-06-01T12:00:00.000Z'); + 'name', '', 'datestamp', '2024-06-01T12:00:00.000Z'); +% base.name '': what ndi.element/newdocument writes (NDI-matlab main 441530d0, +% +ndi/element.m newdocument sets element.* only); a non-empty one is carried +% since [element name and reference stored apart] (+migrators_j/element.m) % a stimulator element is an ndi.probe.timeseries.stimulator (NDI % probetype2object.json): [element types have no default] makes a probe type a % device only on an ndi.probe.* class (Jess Haley, 2026-10-26) @@ -2778,7 +2784,10 @@ function testDeferredBathKeepsItsStimulatorAndItsEpoch(testCase) 'superclasses', struct('class_name', 'base', 'class_version', '1.0.0')); elem.depends_on = struct('name', {'subject_id'}, 'value', {subjId}); elem.base = struct('id', stimId, 'session_id', sessId, ... - 'name', 'stimulator', 'datestamp', '2024-06-01T12:00:00.000Z'); + 'name', '', 'datestamp', '2024-06-01T12:00:00.000Z'); +% base.name '': what ndi.element/newdocument writes (NDI-matlab main 441530d0, +% +ndi/element.m newdocument sets element.* only); a non-empty one is carried +% since [element name and reference stored apart] (+migrators_j/element.m) elem.element = struct('ndi_element_class', 'ndi.probe.timeseries.stimulator', 'name', 'bath_y', ... 'reference', 1, 'type', 'stimulator', 'direct', 0); % the stimulator's element_epoch for that epoch: its clock (+ndi/element.m) diff --git a/tests/+did2/+unittest/testRawV1RoundTripElement.m b/tests/+did2/+unittest/testRawV1RoundTripElement.m index 789ec145..5285e32e 100644 --- a/tests/+did2/+unittest/testRawV1RoundTripElement.m +++ b/tests/+did2/+unittest/testRawV1RoundTripElement.m @@ -9,6 +9,16 @@ % the mirror of NDI-matlab's % ndi.vintage.elementLocalIdentifier on % claude/phase3 471cd5c66) +% [element name and reference stored apart] (2026-10-29) the v1 name, +% exactly, is entity.name; the +% reference is what follows it in the +% local_identifier +% (did2.convert.elementHandle / +% elementNameReferenceApart), so a name +% that reads as a pair converts +% (no longer carried as unpackable); an entity +% with no `name` (migrated before it) +% still reads by the old parse % [element types have no default], [electrode spellings and imaging types % are devices], [event lines and environment probes are devices] % a device only on an ndi.probe.* class; @@ -103,6 +113,7 @@ function testTheLocalIdentifierIsNameBarReference(testCase) out = runRaw(v1); ent = out.migrated{1}; verifyEqual(testCase, ent.get('entity.local_identifier'), 'leftcortex | 2'); +verifyEqual(testCase, char(ent.get('entity.name')), 'leftcortex'); [n, r] = did2.convert.elementNameReference('leftcortex | 2'); verifyEqual(testCase, n, 'leftcortex'); verifyEqual(testCase, r, 2); @@ -112,24 +123,106 @@ function testTheLocalIdentifierIsNameBarReference(testCase) verifyEmpty(testCase, r2.lost, describeLost(r2)); end -function testABaseNameIsTheEntityName(testCase) -% [v1 name is the entity name] (Jess Haley, 2026-10-27): a non-empty v1 -% base.name is entity.name, and is rebuilt from it; '' (what -% ndi.element/newdocument writes) writes nothing +function testNameAndReferenceStoredApartRoundTrip(testCase) +% [element name and reference stored apart] (Jess Haley, 2026-10-29): the v1 +% name, exactly, is entity.name, and local_identifier keeps +% ' | '; the reference is read back as what follows the +% known name. INVERTED from the unpackable-pair carry rows of +% testCarriedElementsCiteTheirRows ('name reads as a pair', 'non-integer +% reference'), which carried these elements whole: each now converts and +% rebuilds exactly. +cases = { ... + 'name holding (ref 3)', 'el_ref3', 'leftcortex (ref 3)', 2, 'leftcortex (ref 3) | 2'; ... + 'name that is an old (ref X) pair, no reference', 'el_ref3n', 'leftcortex (ref 3)', [], 'leftcortex (ref 3)'; ... + 'name reads as a pair, no reference', 'el_pair', 'leftcortex | 2', [], 'leftcortex | 2'; ... + 'name reads as a pair, with a reference', 'el_pair2', 'leftcortex | 2', 5, 'leftcortex | 2 | 5'; ... + 'non-integer reference', 'el_frac', 'leftcortex', 1.5, 'leftcortex | 1.5'; ... + 'text reference', 'el_text', 'leftcortex', 'tip', 'leftcortex | tip'; ... + 'empty name with a reference', 'el_noname', '', 2, ' | 2'}; +for k = 1:size(cases, 1) + [label, id, name, reference, localId] = cases{k, :}; + v1 = probeDoc(id, name, reference, 'n-trode', 'ndi.probe.timeseries.mfdaq'); + out = runRaw(v1); + verifyEqual(testCase, numel(out.passthrough(strcmp({out.passthrough.id}, id))), 0, ... + sprintf('%s: carried (%s)', label, carryNoteFor(out, id))); + docs = elementOutputs(out, id); + if isempty(docs); continue; end + ent = docs{1}; + verifyEqual(testCase, char(ent.entity.local_identifier), localId, label); + if isempty(name) + verifyFalse(testCase, isfield(ent.entity, 'name'), [label ': an empty name wrote entity.name']); + else + verifyEqual(testCase, char(ent.entity.name), name, [label ': entity.name']); + end + r = did2.convert.roundtrip.check(v1, docs); + verifyFalse(testCase, r.carried, label); + verifyEmpty(testCase, r.lost, sprintf('%s: %s', label, describeLost(r))); + verifyEmpty(testCase, r.invented, sprintf('%s: invented: %s', label, strjoin(r.invented, ', '))); + verifyTrue(testCase, any(strcmp('element.name', [r.matched, r.not_set])), ... + [label ': element.name not rebuilt']); + verifyTrue(testCase, any(strcmp('element.reference', [r.matched, r.not_set])), ... + [label ': element.reference not rebuilt']); +end +end + +function testAnEntityWithoutNameReadsByTheOldParse(testCase) +% an element entity migrated before [element name and reference stored apart] +% has no `name` (its v1 base.name was '') and holds the pair in +% local_identifier alone: ' | ' (2026-10-26), or the older +% ' (ref )'. Its round trip falls back to that parse +% (did2.convert.elementNameReference) and still rebuilds the v1 element. +v1 = probeDoc('el_old', 'leftcortex', 2, 'n-trode', 'ndi.probe.timeseries.mfdaq'); +docs = elementOutputs(runRaw(v1), 'el_old'); +for form = {'leftcortex | 2', 'leftcortex (ref 2)'} + old = docs; + old{1}.entity = rmfield(old{1}.entity, 'name'); + old{1}.entity.local_identifier = form{1}; + r = did2.convert.roundtrip.check(v1, old); + verifyEmpty(testCase, r.lost, sprintf('%s: %s', form{1}, describeLost(r))); + verifyEmpty(testCase, r.invented, sprintf('%s: invented: %s', form{1}, strjoin(r.invented, ', '))); +end +% and one whose `name` is its v1 base.name ([v1 name is the entity name], +% 2026-10-27), not leading its local_identifier: base.name is rebuilt from +% `name`, element.name and element.reference from local_identifier +v1n = v1; +v1n.base.name = 'left cortex probe'; +old = docs; +old{1}.entity.name = 'left cortex probe'; +r = did2.convert.roundtrip.check(v1n, old); +verifyEmpty(testCase, r.lost, describeLost(r)); +verifyEmpty(testCase, r.invented, strjoin(r.invented, ', ')); +end + +function testANonEmptyBaseNameIsCarried(testCase) +% [element name and reference stored apart] (2026-10-29) gives the entity's +% one `name` to element.name. A non-empty v1 base.name, which [v1 name is the +% entity name] (2026-10-27) wrote there, then has no field: writing either +% name loses the other. ndi.element/newdocument writes base.name '' (NDI-matlab +% main 441530d0, +ndi/element.m newdocument), so Decided by Jess Haley, +% 2026-10-30 [element name kept apart, base.name carried]: the element is +% carried whole as v1, malformed, under carry-149. REPLACES +% testABaseNameIsTheEntityName's element case, which wrote base.name to +% entity.name. v1 = probeDoc('el_named', 'leftcortex', 2, 'n-trode', 'ndi.probe.timeseries.mfdaq'); v1.base.name = 'left cortex probe'; out = runRaw(v1); -docs = elementOutputs(out, v1.base.id); -verifyEqual(testCase, char(out.migrated{1}.get('entity.name')), 'left cortex probe'); +verifyEqual(testCase, numel(out.passthrough(strcmp({out.passthrough.id}, 'el_named'))), 1, ... + 'a non-empty base.name was not carried'); +d = carryNoteFor(out, 'el_named'); +verifyTrue(testCase, startsWith(d, '[carry-149] malformed: base.name "left cortex probe"'), d); +docs = elementOutputs(out, 'el_named'); r = did2.convert.roundtrip.check(v1, docs); -verifyEmpty(testCase, r.lost, describeLost(r)); -verifyEmpty(testCase, r.invented, strjoin(r.invented, ', ')); -verifyTrue(testCase, any(strcmp('base.name', r.matched))); +verifyTrue(testCase, r.carried); +verifyTrue(testCase, r.ok, describeLost(r)); +% base.name '' (the writer's) converts, and the entity's name is element.name v0 = probeDoc('el_unnamed', 'leftcortex', 2, 'n-trode', 'ndi.probe.timeseries.mfdaq'); e0 = runRaw(v0).migrated{1}.toStruct(); -verifyFalse(testCase, isfield(e0.entity, 'name'), 'an empty base.name wrote entity.name'); +verifyEqual(testCase, char(e0.entity.name), 'leftcortex'); +r0 = did2.convert.roundtrip.check(v0, elementOutputs(runRaw(v0), 'el_unnamed')); +verifyEmpty(testCase, r0.lost, describeLost(r0)); +verifyTrue(testCase, any(strcmp('base.name', [r0.matched, r0.not_set])), 'base.name '''' not rebuilt'); % and the named device validates -outV = did2.convert.v1_to_v2(v1, 'Validate', true, 'TargetVersion', 'V_eta'); +outV = did2.convert.v1_to_v2(v0, 'Validate', true, 'TargetVersion', 'V_eta'); reasons = ''; for k = 1:numel(outV.quarantine) reasons = [reasons sprintf('%s: %s; ', outV.quarantine(k).class_name, ... @@ -138,82 +231,6 @@ function testABaseNameIsTheEntityName(testCase) verifyEmpty(testCase, outV.quarantine, reasons); end -function testADecidedDeviceKindIsAnEquipmentTypeAssertion(testCase) -% [element type is a device kind term] and the decisions naming each kind -% (Jess Haley, 2026-10-27): one Equipment Type (NCIT:C178920) assertion with -% the decided term; a type with no registered kind writes none ((iii): it -% keeps today's assertion only). The `element type` and `ndi element class` -% assertions are still written (NDI-matlab reads them; PR body). -cases = { ... - 'n-trode', 'ndi.probe.timeseries.mfdaq', 'wikidata:Q1952355'; ... - 'accelerometer', 'ndi.probe.timeseries.mfdaq', 'NCIT:C88166'; ... - 'thermometer', 'ndi.probe.timeseries.mfdaq', 'NCIT:C50059'; ... - 'ppg', 'ndi.probe.timeseries.mfdaq', 'NCIT:C224112'; ... - 'optogenetic-fiber', 'ndi.probe.timeseries.stimulator', 'NCIT:C50358'; ... - 'intraoral-cannula', 'ndi.probe.timeseries.stimulator', 'NCIT:C50343'; ... - 'ecg', 'ndi.probe.timeseries.mfdaq', 'OBI:0003806'; ... - 'eeg', 'ndi.probe.timeseries.mfdaq', 'wikidata:Q4531135'; ... - 'one-photon-imaging', 'ndi.probe.image', 'OBI:0400169'; ... - 'two-photon-imaging', 'ndi.probe.image', 'OBI:0400169'; ... - 'patch-Vm', 'ndi.probe.timeseries.mfdaq', ''; ... - 'sharp-Im', 'ndi.probe.timeseries.mfdaq', ''; ... - 'stimulator', 'ndi.probe.timeseries.stimulator', ''; ... - 'lick-spout', 'ndi.probe.timeseries.mfdaq', ''; ... - 'reward-well', 'ndi.probe.timeseries.mfdaq', ''; ... - 'event', 'ndi.probe.timeseries.stimulator', ''; ... - 'electrode-4', 'ndi.probe.timeseries.mfdaq', ''}; -for k = 1:size(cases, 1) - [ty, cls, node] = cases{k, :}; - id = sprintf('el_kind_%d', k); - out = runRaw(probeDoc(id, 'p', 1, ty, cls)); - kinds = assertionsAbout(out, id, 'Equipment Type'); - vars = cellfun(@(b) char(b.statement.variable.name), assertionsAbout(out, id, ''), ... - 'UniformOutput', false); - verifyTrue(testCase, any(strcmp(vars, 'element type')), [ty ': element type assertion']); - verifyTrue(testCase, any(strcmp(vars, 'ndi element class')), [ty ': ndi element class assertion']); - if isempty(node) - verifyEmpty(testCase, kinds, [ty ': a kind with no registered term was written']); - else - verifyNumElements(testCase, kinds, 1, ty); - verifyEqual(testCase, char(kinds{1}.statement.variable.node), 'NCIT:C178920', ty); - verifyEqual(testCase, char(kinds{1}.term.value.node), node, ty); - end -end -end - -function testGroupIsWrittenAsTheRegistryNamesIt(testCase) -% [migrator writes the registry spelling] (Jess Haley, 2026-10-26): `Group`, -% NCIT:C43359, not `group` -out = runRaw({unitDoc('el_unit_m', 'leftcortex_18', 1), neuronDoc('ne_2', 'el_unit_m', 'multi'), unitProbe()}); -ent = elementOutputs(out, 'el_unit_m'); -verifyEqual(testCase, char(ent{1}.entity.type.name), 'Group'); -verifyEqual(testCase, char(ent{1}.entity.type.node), 'NCIT:C43359'); -end - -function testAUnitThatDisagreesWithItsProbeIsCarried(testCase) -% [unit direct and subject rebuild from the probe] (Jess Haley, 2026-10-27): -% a unit whose `direct` is not 0, or whose subject is not its probe's, is -% carried whole as v1, malformed (carry-120) -bad = unitDoc('el_unit_d', 'leftcortex_23', 1); -bad.element.direct = true; -other = unitDoc('el_unit_o', 'leftcortex_24', 1); -other.depends_on(strcmp({other.depends_on.name}, 'subject_id')).value = 'subj_2'; -cases = {'direct 1', bad; 'another subject', other}; -for k = 1:size(cases, 1) - [label, v1] = cases{k, :}; - out = runRaw({v1, neuronDoc(['ne_' v1.base.id], v1.base.id, 'single'), unitProbe()}); - verifyEqual(testCase, numel(out.passthrough(strcmp({out.passthrough.id}, v1.base.id))), 1, ... - [label ': not carried']); - d = carryNoteFor(out, v1.base.id); - verifyTrue(testCase, startsWith(d, '[carry-120] malformed: '), sprintf('%s: %s', label, d)); - r = did2.convert.roundtrip.check(v1, elementOutputs(out, v1.base.id)); - verifyTrue(testCase, r.carried, label); - verifyTrue(testCase, r.ok, sprintf('%s: %s', label, describeLost(r))); -end -end - -% ===================== carried writer shapes ============================ - function testCarriedElementsCiteTheirRows(testCase) cases = { ... 'unlabelled unit', unitDoc('el_unit_u', 'leftcortex_19', 1), {}, '[carry-062] '; ... @@ -226,8 +243,7 @@ function testCarriedElementsCiteTheirRows(testCase) 'unlisted type', probeDoc('el_ul', 'x', 1, 'sharp-I', 'ndi.probe.timeseries.mfdaq'), {}, '[carry-063] '; ... 'wide-field imaging probe', probeDoc('el_wf', 'cam', 1, 'wide-field-imaging', 'ndi.probe.image'), {}, '[carry-121] '; ... 'brightfield imaging probe', probeDoc('el_bf', 'cam', 1, 'brightfield-imaging', 'ndi.probe.image'), {}, '[carry-121] '; ... - 'name reads as a pair', probeDoc('el_pair', 'leftcortex | 2', [], 'n-trode', 'ndi.probe.timeseries.mfdaq'), {}, '[carry-055] '; ... - 'non-integer reference', probeDoc('el_frac', 'leftcortex', 1.5, 'n-trode', 'ndi.probe.timeseries.mfdaq'), {}, '[carry-055] '; ... + 'reference not one number', probeDoc('el_arr', 'leftcortex', [1 2], 'n-trode', 'ndi.probe.timeseries.mfdaq'), {}, '[carry-120] '; ... 'no name, no reference', probeDoc('el_none', '', [], 'n-trode', 'ndi.probe.timeseries.mfdaq'), {}, '[carry-048] '}; for k = 1:size(cases, 1) [label, v1, others, row] = cases{k, :}; @@ -354,6 +370,10 @@ function testBothPathsConvertAnElementAlike(testCase) function d = carryNoteFor(out, id) d = ''; +% no carried_as_v1 field at all when nothing in the run was carried +if ~isfield(out.summary.migration_notes, 'carried_as_v1') + return; +end n = out.summary.migration_notes.carried_as_v1; ix = find(strcmp(n.ids, id), 1); if ~isempty(ix)