Skip to content

Commit

Permalink
BICAS: SRM: Rm. "s", keys as non-cell arrays
Browse files Browse the repository at this point in the history
Note: "keys" in constructor argument, return method "keys", array of
constants.
  • Loading branch information
ErikPGJ committed Sep 6, 2024
1 parent dc7dc2f commit 495e141
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
%
% PROPOSAL: Store ASID constants in dictionary.
% PROPOSAL: get_derived_ASR_constants() should return dictionary.
% PROPOSAL: Store mapping string-->ASID objects separately from collections
% of string constants and object constants (ALL_ASID_NAMES_CA,
% ALL_ASID_CA).
% PROPOSAL: Use dictionary. Obtain sets of strings/objects via methods.
%
% PROPOSAL: Private ASID/SSID/SDID constructors. Only instantiate objects for
% unique ASID/SSID/SDID ONCE.
Expand All @@ -55,18 +51,14 @@
% (2) Makes it possible to access constants through a variable copy of
% this constant rather than using the long qualifiers.
C = bicas.proc.L1L2.AntennaSignalId.init_const()

% Array of all ASIDs in "C".
ALL_ARRAY = bicas.proc.L1L2.AntennaSignalId.init_const_array()
end



properties(SetAccess=immutable, GetAccess=public)
% String constant that represents unique object. The name is deliberately
% short since objects themselves are IDs and should preferably be used
% except when one has to convert to string. The only reason this exists
% is to be used as a replacement for objects as keys in containers.Map
% objects. containers.Map does not accept objects as keys.
s

% String constant that represents the type of signal (single/diff, DC/AC).
category

Expand All @@ -86,9 +78,7 @@


% Constructor
function obj = AntennaSignalId(name, category, antennas)
assert(isstring(name))

function obj = AntennaSignalId(category, antennas)
% ASSERTIONS: antennas
assert(isnumeric(antennas))
% NOTE: Assertion permits empty value, []. Assert vector length
Expand Down Expand Up @@ -131,7 +121,6 @@
end

% Assign object.
obj.s = name;
obj.antennas = antennas;
obj.category = category;
end
Expand Down Expand Up @@ -166,9 +155,9 @@
ASID = bicas.proc.L1L2.AntennaSignalId.C;
C = struct();

for asidNameCa = ASID.ALL_ASID_NAMES_CA'
asidName = asidNameCa{1};
C.(asidName) = fh(ASID.(asidName));
for fnCa = fieldnames(ASID)'
fn = fnCa{1};
C.(fn) = fh(ASID.(fn));
end
end

Expand All @@ -185,9 +174,11 @@
function C = init_const()
C = struct();

function add(name, asidCategory, asidAntennas)
C.(name) = bicas.proc.L1L2.AntennaSignalId(...
name, asidCategory, asidAntennas);
function add(fn, asidCategory, asidAntennas)
Asid = bicas.proc.L1L2.AntennaSignalId(...
asidCategory, asidAntennas);

C.(fn) = Asid;
end

% =====================================
Expand All @@ -204,18 +195,13 @@ function add(name, asidCategory, asidAntennas)
add("AC_V12", 'AC_DIFF', [1, 2]);
add("AC_V13", 'AC_DIFF', [1, 3]);
add("AC_V23", 'AC_DIFF', [2, 3]);
end

% =======================================
% Create lists of all unique ASID objects
% =======================================
ALL_ASID_NAMES_CA = {}; % All ASID names.
ALL_ASID_CA = {}; % All ASID objects.
for fnCa = fieldnames(C)'
ALL_ASID_NAMES_CA{end+1, 1} = string(fnCa{1});
ALL_ASID_CA{ end+1, 1} = C.(fnCa{1});
end
C.ALL_ASID_NAMES_CA = ALL_ASID_NAMES_CA;
C.ALL_ASID_CA = ALL_ASID_CA;


function AsidArray = init_const_array()
ca = struct2cell(bicas.proc.L1L2.AntennaSignalId.C);
AsidArray = [ca{:}]';
end


Expand Down
2 changes: 1 addition & 1 deletion mission/solar_orbiter/bicas/src/+bicas/+proc/+L1L2/dc.m
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
AsrSamplesAVoltSrm = bicas.utils.SameRowsMap(...
"bicas.proc.L1L2.AntennaSignalId", nRecords, 'CONSTANT', ...
nan(nRecords, nSamplesPerRecordChannel), ...
bicas.proc.L1L2.AntennaSignalId.C.ALL_ASID_CA);
bicas.proc.L1L2.AntennaSignalId.ALL_ARRAY);

iCalibLZv = Cal.get_BIAS_calibration_time_L(Dcip.Zv.Epoch);
iCalibHZv = Cal.get_BIAS_calibration_time_H(Dcip.Zv.Epoch);
Expand Down
7 changes: 3 additions & 4 deletions mission/solar_orbiter/bicas/src/+bicas/+proc/+L1L2/demuxer.m
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,14 @@ function complement_ASR(AsrSamplesAVoltSrm)
% Ex: bdm=1,2,3
%===================================================================

keysCa = AsSrm.keys;
keyArray = AsSrm.keys;

% IMPLEMENTATION NOTE: Can not use bicas.utils.SameRowsMap methods
% for deriving the entire size (samples per record), until possibly
% using a future bicas.utils.SameSizeTypeMap instead.
tempNaN = nan(size(AsrSamplesAVoltSrm(keysCa{1})));
tempNaN = nan(size(AsrSamplesAVoltSrm(keyArray)));

for asidCa = bicas.proc.L1L2.AntennaSignalId.C.ALL_ASID_CA'
Asid = asidCa{1};
for Asid = bicas.proc.L1L2.AntennaSignalId.ALL_ARRAY'
if ~AsSrm.isKey(Asid)
AsSrm.add(Asid, tempNaN);
end
Expand Down
4 changes: 2 additions & 2 deletions mission/solar_orbiter/bicas/src/+bicas/+proc/+L1L2/qual.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@
% ====================================
% NOTE: Should really use future bicas.utils.SameSizeTypeMap here
% which contains size on other dimensions.
keysCa = zvAsrSamplesAVoltSrm.keys;
nSpr = size(zvAsrSamplesAVoltSrm(keysCa{1}), 2);
keyArray = zvAsrSamplesAVoltSrm.keys;
nSpr = size(zvAsrSamplesAVoltSrm(keyArray), 2);

% IMPLEMENTATION NOTE: bicas.utils.SameRowsMap.set_rows() can not
% handle logical indexing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function test(zv_Epoch, zvBdm, isLfr, bdmRemoveArray, lfrBdmMarginSec, tdsBdmMar
assert(iscolumn(v))
AsrSamplesAVoltSrm = bicas.utils.SameRowsMap(...
"bicas.proc.L1L2.AntennaSignalId", size(v, 1), 'CONSTANT', v, ...
bicas.proc.L1L2.AntennaSignalId.C.ALL_ASID_CA(:));
bicas.proc.L1L2.AntennaSignalId.ALL_ARRAY);
end


Expand Down
34 changes: 18 additions & 16 deletions mission/solar_orbiter/bicas/src/+bicas/+utils/SameRowsMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
% initType == 'EMPTY': Zero length.
% initType == 'CONSTANT':
% varargin{1} = Array of values
% varargin{2} = Cell array of keys which should have this
% varargin{2} = Array of keys which should have the specified
% value.
%
% NOTE: To initialize with multiple keys with unique values, use both
Expand All @@ -225,12 +225,12 @@
% Same (constant) initial value for all keys.

assert(numel(varargin) == 2)
value = varargin{1};
keysCa = varargin{2};
value = varargin{1};
keysArray = varargin{2};

% NOTE: Implicitly checks for unique keys.
for keyCa = keysCa(:)'
obj.add(keyCa{1}, value);
for key = keysArray(:)'
obj.add(key, value);
end

otherwise
Expand All @@ -251,9 +251,11 @@



% Return array of key values.
%
% NOTE: Method name chosen to be identical with dictionary.keys().
function keysCa = keys(obj)
keysCa = num2cell(obj.Dict.keys);
function keysArray = keys(obj)
keysArray = obj.Dict.keys;
end


Expand Down Expand Up @@ -322,9 +324,9 @@ function set_rows(obj, Srm2, iRowsArray)

assert(bicas.utils.object_sets_isequaln(obj.keys, Srm2.keys))

keysCa = obj.keys();
for keyCa = keysCa(:)'
key = keyCa{1};
keyArray = obj.keys();
for i = 1:numel(keyArray)
key = keyArray(i);

hw1 = obj.Dict(key);
hw2 = Srm2.Dict(key);
Expand Down Expand Up @@ -401,9 +403,9 @@ function set_rows(obj, Srm2, iRowsArray)
return
end

keysCa = obj1.Dict.keys;
for i = 1:numel(keysCa)
key = keysCa(i);
keyArray = obj1.keys;
for i = 1:numel(keyArray)
key = keyArray(i);

value1 = obj1.Dict(key).v;
value2 = obj2.Dict(key).v;
Expand Down Expand Up @@ -431,11 +433,11 @@ function set_rows(obj, Srm2, iRowsArray)
% Unclear if works for non-numeric, non-char key values. Probably not.
%
function s = disp(obj)
keysCa = obj.Dict.keys();
keyArray = obj.keys;

sCa = {sprintf('%i row(s)', obj.nRows())};
for i = 1:numel(keysCa)
key = keysCa(i);
for i = 1:numel(keyArray)
key = keyArray(i);
value = subsref(obj, substruct('()', {key}));

if isnumeric(value) || ischar(value)
Expand Down
62 changes: 31 additions & 31 deletions mission/solar_orbiter/bicas/src/+bicas/+utils/SameRowsMap___UTEST.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ function test_basic(testCase)

Srm = bicas.utils.SameRowsMap('string', 0, 'EMPTY');

testCase.assertEqual(Srm.keys(), cell(0, 1))
testCase.assertEqual(Srm.keys(), string.empty(0, 1))
testCase.assertEqual(Srm.numEntries, 0)
testCase.assertEqual(Srm.nRows, 0)
testCase.test_keys_values(testCase, Srm, {}, {})

Srm.add("K1", V1)

testCase.assertEqual(Srm.keys, {"K1"})
testCase.assertEqual(Srm.keys, ["K1"])
testCase.assertEqual(Srm.numEntries, 1)
testCase.test_keys_values(testCase, Srm, {"K1"}, {V1})
testCase.test_keys_values(testCase, Srm, ["K1"], {V1})
testCase.assertFalse(Srm.isKey("K2"))

Srm.add("K2", V2)
Expand All @@ -48,8 +48,8 @@ function test_basic(testCase)

% Test different orders. ==> Effectively testing the helper
% function "test_keys_values()".
testCase.test_keys_values(testCase, Srm, {"K2", "K1"}, {V2, V1})
testCase.test_keys_values(testCase, Srm, {"K1", "K2"}, {V1, V2})
testCase.test_keys_values(testCase, Srm, ["K2", "K1"], {V2, V1})
testCase.test_keys_values(testCase, Srm, ["K1", "K2"], {V1, V2})

testCase.assertEqual(Srm("K1"), V1);
testCase.assertEqual(Srm("K2"), V2);
Expand All @@ -65,10 +65,10 @@ function test_basic(testCase)
% double keys, non-zero rows, 'CONSTANT'
% ======================================
V = [1;2;3];
Srm = bicas.utils.SameRowsMap('double', 3, 'CONSTANT', V, {9});
Srm = bicas.utils.SameRowsMap('double', 3, 'CONSTANT', V, [9]);
testCase.assertEqual(Srm.nRows, 3)
testCase.assertEqual(Srm(9), V)
testCase.test_keys_values(testCase, Srm, {9}, {V})
testCase.test_keys_values(testCase, Srm, [9], {V})

% =============================================
% Initial value has inconsistent number of rows
Expand Down Expand Up @@ -161,8 +161,8 @@ function test_set_rows(testCase)
412, 422, 432 ...
];

Srm1 = bicas.utils.SameRowsMap('string', 4, 'CONSTANT', V1, {"K"});
Srm2 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', V1(1:2, :, :), {"K"});
Srm1 = bicas.utils.SameRowsMap('string', 4, 'CONSTANT', V1, ["K"]);
Srm2 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', V1(1:2, :, :), ["K"]);
Srm1.set_rows(Srm2, [3;2]) % NOTE: Decrementing indices.

V2 = V1;
Expand All @@ -175,20 +175,20 @@ function test_set_rows(testCase)
% Illegal
% =======
% Incompatible types
Srm1 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', int8([1;2]), {"K"});
Srm2 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], {"K"});
Srm1 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', int8([1;2]), ["K"]);
Srm2 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], ["K"]);
testCase.assertError(...
@() (Srm1.set_rows(Srm2, [1])), ...
?MException)
% Incompatible array sizes
Srm1 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', [1 2;3 4], {"K"});
Srm2 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], {"K"});
Srm1 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', [1 2;3 4], ["K"]);
Srm2 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], ["K"]);
testCase.assertError(...
@() (Srm1.set_rows(Srm2, [1])), ...
?MException)
% Different sets of keys.
Srm1 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', [1;2], {"K1", "K2a"});
Srm2 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], {"K1", "K2b"});
Srm1 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', [1;2], ["K1"; "K2a"]);
Srm2 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], ["K1"; "K2b"]);
testCase.assertError(...
@() (Srm1.set_rows(Srm2, [1])), ...
?MException)
Expand All @@ -212,12 +212,12 @@ function test_equal(testCase)
testCase.assertTrue( Srm2a == Srm2b)
testCase.assertFalse(Srm1a == Srm3 )

Srm1a = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [1], {"K1", "K2"});
Srm1b = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [1], {"K1", "K2"});
Srm2a = bicas.utils.SameRowsMap('double', 1, 'CONSTANT', [9], {1, 2});
Srm2b = bicas.utils.SameRowsMap('double', 1, 'CONSTANT', [9], {1, 2});
Srm3 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], {"K1", "K2"});
Srm4 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', [1;2], {"K1", "K2"});
Srm1a = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [1], ["K1"; "K2"]);
Srm1b = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [1], ["K1"; "K2"]);
Srm2a = bicas.utils.SameRowsMap('double', 1, 'CONSTANT', [9], [1; 2]);
Srm2b = bicas.utils.SameRowsMap('double', 1, 'CONSTANT', [9], [1; 2]);
Srm3 = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [9], ["K1"; "K2"]);
Srm4 = bicas.utils.SameRowsMap('string', 2, 'CONSTANT', [1;2], ["K1"; "K2"]);

testCase.assertTrue( Srm1a == Srm1b)
testCase.assertTrue( Srm2a == Srm2b)
Expand Down Expand Up @@ -249,18 +249,18 @@ function test_equal(testCase)


function test_display(testCase)
Srm = bicas.utils.SameRowsMap('string', 0, 'CONSTANT', uint8(ones(0,2)), {"K1"});
Srm = bicas.utils.SameRowsMap('string', 0, 'CONSTANT', uint8(ones(0,2)), ["K1"]);
disp(Srm)

Srm = bicas.utils.SameRowsMap('double', 1, 'CONSTANT', [9], {1});
Srm = bicas.utils.SameRowsMap('double', 1, 'CONSTANT', [9], [1]);
disp(Srm)

Srm = bicas.utils.SameRowsMap('string', 3, 'CONSTANT', [1,2;3,4;5,6], {"K1", "K2"});
Srm = bicas.utils.SameRowsMap('string', 3, 'CONSTANT', [1,2;3,4;5,6], ["K1"; "K2"]);
disp(Srm)

Asid1 = bicas.proc.L1L2.AntennaSignalId.C.DC_V1;
Asid2 = bicas.proc.L1L2.AntennaSignalId.C.DC_V12;
Srm = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [Asid1, Asid2], {"K1", "K2"});
Srm = bicas.utils.SameRowsMap('string', 1, 'CONSTANT', [Asid1, Asid2], ["K1"; "K2"]);
disp(Srm)
end

Expand All @@ -282,20 +282,20 @@ function test_display(testCase)
% Helper function. Test that SRM returns correct values for methods
% .keys() and .values(). Takes into account that keys/values may be in
% a different order than specified.
function test_keys_values(testCase, Srm, expKeysCa, expValuesCa)
function test_keys_values(testCase, Srm, expKeyArray, expValuesCa)
% NOTE: Implementation should permit both numbers and strings as
% keys, mixed.

assert(isa(Srm, 'bicas.utils.SameRowsMap'))

actKeysCa = Srm.keys();
actKeyArray = Srm.keys();
actValuesCa = Srm.values();

testCase.assertTrue(bicas.utils.object_sets_isequaln(actKeysCa, expKeysCa))
testCase.assertTrue(bicas.utils.object_sets_isequaln(actKeyArray, expKeyArray))

for iAct = 1:numel(actKeysCa)
for iExp = 1:numel(expKeysCa)
if isequal(actKeysCa{iAct}, expKeysCa{iExp})
for iAct = 1:numel(actKeyArray)
for iExp = 1:numel(expKeyArray)
if isequal(actKeyArray(iAct), expKeyArray(iExp))
actValue = actValuesCa{iAct};
expValue = expValuesCa{iExp};
testCase.assertEqual(actValue, expValue)
Expand Down

0 comments on commit 495e141

Please sign in to comment.