Skip to main content

checkParameterGuess.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% This function checks that the initial guess parameters file is in the
% correct order
%
% (struct) -> (struct)
%


function inputs = checkParameterGuess(inputs)
if isfield(inputs.initialGuess, 'parameter') || isfield(inputs,"synergyWeights")
if isfield(inputs.initialGuess, 'parameter')
inputs.synergyWeightsGuess = inputs.initialGuess.parameter;
elseif isfield(inputs,"synergyWeights")
inputs.synergyWeightsGuess = inputs.synergyWeights;
end

parameterIndex = zeros(length(inputs.synergyGroups), inputs.numMuscles);
for i = 1 : length(inputs.synergyGroups)
for j = 1 : inputs.numMuscles
for k = 1 : length(inputs.synergyGroups{i}.muscleNames)
if strcmpi(inputs.muscleNames(j), inputs.synergyGroups{i}.muscleNames(k))
if i <= 1
parameterIndex(i, k) = j;
else
parameterIndex(i, k + length(inputs.synergyGroups{i}.muscleNames)) = j;
end
end
end
end
end
synergyWeightsFlattened = [];
numSynergiesIndex = 0;
for j = 1 : length(inputs.synergyGroups)
synergyWeightsFlattened = cat(2, synergyWeightsFlattened, ...
reshape(inputs.synergyWeightsGuess(1 + numSynergiesIndex: ...
inputs.synergyGroups{j}.numSynergies + numSynergiesIndex, ...
nonzeros(parameterIndex(j, :)))', 1, []));
numSynergiesIndex = numSynergiesIndex + inputs.synergyGroups{j}.numSynergies;
end
inputs.synergyWeightsGuess = synergyWeightsFlattened;
end
if strcmp(inputs.controllerType, 'synergy_driven')
inputs = getMuscleSynergiesInitialGuess(inputs);
for i = 1 : length(inputs.coordinateNames)
for j = 1 : length(inputs.surrogateModelCoordinateNames)
if strcmp(inputs.coordinateNames(i), inputs.surrogateModelCoordinateNames(j))
inputs.surrogateModelIndex(j) = i;
end
end
end
inputs.dofsActuatedIndex = [];
for i = 1 : length(inputs.inverseDynamicMomentLabels)
for j = 1 : length(inputs.surrogateModelCoordinateNames)
if strcmp(inputs.inverseDynamicMomentLabels(i), ...
strcat(inputs.surrogateModelCoordinateNames(j), '_moment'))
inputs.dofsActuatedIndex(end+1) = j;
end
end
end
end
end
function inputs = getMuscleSynergiesInitialGuess(inputs)
if isfield(inputs.initialGuess,"parameter") || isfield(inputs,"synergyWeights")
synergyWeights = getSynergyWeightsFromGroups(inputs.synergyWeightsGuess, inputs);
inputs.synergyActivationsGuess = inputs.experimentalMuscleActivations / synergyWeights;
else
inputs.mtpActivationsColumnNames = inputs.muscleLabels;
inputs.mtpActivations = permute(inputs.experimentalMuscleActivations, [3 2 1]);
inputs.synergyWeightsGuess = prepareNonNegativeMatrixFactorizationInitialValues(inputs, inputs)';
synergyWeights = getSynergyWeightsFromGroups(inputs.synergyWeightsGuess, inputs);
inputs.synergyActivationsGuess = inputs.experimentalMuscleActivations / synergyWeights;
end
end