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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 65 additions & 33 deletions src/did/+did2/+convert/+migrators_j/element.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
% -- now to an entity.
%
% 1 -> N:
% - entity the element itself (id preserved; name/reference ->
% local_identifier '<name> | <reference>').
% - entity the element itself (id preserved; name -> `name`,
% exactly; name/reference -> local_identifier
% '<name> | <reference>').
% - term_assertion(s) its kind, nothing dropped: `type` -> variable
% "element type"; `ndi_element_class` -> variable "ndi
% element class" (the MATLAB reconstruction handle; kept
Expand Down Expand Up @@ -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,
% '<name> | <reference>' (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)
% '<name> | <reference>' (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
Expand Down Expand Up @@ -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):
% '<name> | <reference>', 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 '<name> | <reference>')
% [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 ('<name> | <reference>', [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)
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions src/did/+did2/+convert/+migrators_j/private/jBaseName.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.<class>)
% 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.<class>) 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);
Expand Down
34 changes: 25 additions & 9 deletions src/did/+did2/+convert/+roundtrip/element.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
% ('<name> | <reference>'; 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
% ('<name> | <reference>', 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,
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions src/did/+did2/+convert/+roundtrip/private/manifestProbemap.m
Original file line number Diff line number Diff line change
Expand Up @@ -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, ':',
Expand Down Expand Up @@ -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
Expand Down
106 changes: 106 additions & 0 deletions src/did/+did2/+convert/elementHandle.m
Original file line number Diff line number Diff line change
@@ -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 ' | ' <reference> 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 ' | ' <reference> any other number: num2str(R, 17), which
% str2double reads back to the same double
% NAME ' | ' <text> 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 ' | <ref>'
% (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
11 changes: 10 additions & 1 deletion src/did/+did2/+convert/elementLocalIdentifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/did/+did2/+convert/elementNameReference.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' | <integer>'.
%
% 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
Expand Down
Loading
Loading