Newer
Older
%% https://gitlab.rc.uab.edu/cbsmith2/transcription-save-script
Christophe Bradley Smith
committed
%% Main code
% Prepare work space
clc
Christophe Bradley Smith
committed
% Check for miscelaneous transcriptions in the work space
miscLS = who('-regexp', '^ls(([0-9]*$)|($))');
Christophe Bradley Smith
committed
newLS = CheckmiscLS(miscLS);
if ~isempty(newLS); ls = eval(newLS); end
% Search the workspace for ls and lss and check for the tracker
if ~ValidateWorkspace(ls, lss); return; end
if ~exist('remainingFiles', 'var'); remainingFiles = []; end
remainingFiles = FindRemainingFiles(ls, lss, remainingFiles);
% Transfer labels
lss = TransferLabels(ls, lss);
% Prompt user for Inits
reviewerInit = input('Please enter your initials: ', 's');
% Save latest transcription and progress
Christophe Bradley Smith
committed
uisave({'lss', 'remainingFiles', 'reviewerInit'}, '*.mat')
Christophe Bradley Smith
committed
function ls = CheckmiscLS(miscLS)
Christophe Bradley Smith
committed
if isempty(miscLS); error('No valid ls transcription file was found in the workspace. Please check that the file has been exported from the application.'); end
if length(miscLS) == 1
ls = string(miscLS);
fprintf('Variable %s will be used as the user transcription variable.\n', ls)
return
end
Christophe Bradley Smith
committed
fprintf(2, 'Warning: multiple potential ''ls'' files were found in the work space. \n\n')
nLS = length(miscLS);
Christophe Bradley Smith
committed
lsNames = string(miscLS);
Christophe Bradley Smith
committed
formatPat = '\t%i. %s\n';
Christophe Bradley Smith
committed
formattedstring = compose(formatPat, (1:nLS)', lsNames);
Christophe Bradley Smith
committed
listSet = join(formattedstring' , '');
validResp = 0:nLS;
while true
fprintf('Select the most up to date ls file by inputing a number from 0 to %i\n', nLS)
fprintf(listSet{1});
userResp = input('Resp:');
if any(ismember(validResp, userResp))
Christophe Bradley Smith
committed
fprintf('%s has been selected.\n', lsNames(userResp))
Christophe Bradley Smith
committed
break
else
Christophe Bradley Smith
committed
fprintf('%i is an invalid response.\n', userResp)
Christophe Bradley Smith
committed
end
end
Christophe Bradley Smith
committed
ls = lsNames(userResp);
Christophe Bradley Smith
committed
end
function extStatus = ValidateWorkspace(ls, lss)
if ~exist("ls", "var")
fprintf(2, 'No exported transcript found. Please export your transciription to the work space as ''ls''\n')
extStatus = false;
return
if ~exist("lss", 'var')
fprintf(2, 'The original transcription file is missing, please ensure the work you have done is saved to ''ls'' and load in the original transcription\n');
extStatus = false;
return
end
extStatus = true;
end
function remainingFiles = FindRemainingFiles(ls, lss, remainingFiles)
% Find identical labels
nFiles = lss.NumMembers;
isUntouched = false(nFiles, 1);
for i = 1:nFiles
Christophe Bradley Smith
committed
isUntouched(i) = isequal(ls.Labels.Words{i}.ROILimits, lss.Labels.Words{i}.ROILimits);
end
% Account for previosuly edited files
if ~isempty(remainingFiles)
wasPreviouslyUntouched = ~ismember(1:nFiles, remainingFiles.File_Pos);
isUntouched(wasPreviouslyUntouched) = false;
end
% Prepare output
File_Pos = find(isUntouched);
memberNames = ls.getMemberNames;
File_Name = memberNames(isUntouched);
remainingFiles = table(File_Pos, File_Name);
end
function lss = TransferLabels(ls, lss)
% Clear lss labels
resetLabelValues(lss)
Christophe Bradley Smith
committed
removeLabelDefinition(lss, "Words")
Christophe Bradley Smith
committed
Christophe Bradley Smith
committed
addLabelDefinitions(lss, getLabelDefinitions(ls))
% Get list of sublabels
lsSubLabelList = split(strtrim(extract(labelDefinitionsHierarchy(ls), regexpPattern('(?<=Sublabels:).*'))));
Christophe Bradley Smith
committed
% Move ls labels to lss
nTrials = height(ls.Labels);
for iTrial = 1:nTrials
Christophe Bradley Smith
committed
curROI = ls.Labels.Words{iTrial}.ROILimits;
curVal = ls.Labels.Words{iTrial}.Value;
if isempty(curROI)
continue
end
Christophe Bradley Smith
committed
setLabelValue(lss, iTrial, 'Words', curROI, curVal)
Christophe Bradley Smith
committed
curSpans = ls.Labels.Words{iTrial};
nSpans = height(curSpans);
for iSpan = 1:nSpans
for curLabel = lsSubLabelList'
labelValues = ls.Labels.Words{iTrial}.Sublabels.(curLabel{1});
if isa(labelValues, 'cell')
labelValue = labelValues{iSpan};
else
labelValue = labelValues(iSpan);
end
setLabelValue(lss, iTrial, ["Words", string(curLabel)], labelValue, "LabelRowIndex", iSpan)
end
Christophe Bradley Smith
committed
end
%% Git managment
function checkVersion()
% Check if the current directory is a Git repository
if ~isGitRepository()
warning('This is not a Git repository. The version you are using cannot be checked against the shared repository. Enable Git and make sure this is a git repository to validate versions.\n The most up to date code can be found at: https://gitlab.rc.uab.edu/cbsmith2/transcription-save-script');
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
end
% Fetch the latest changes from the remote repository
system('git fetch origin');
% Get the current branch name
[~, currentBranch] = system('git rev-parse --abbrev-ref HEAD');
currentBranch = strtrim(currentBranch);
% Get the local and remote commit hashes
localCommit = getGitCommitHash();
remoteCommit = getRemoteCommit_hash(currentBranch);
% Compare the local and remote commits
if strcmp(localCommit, remoteCommit)
disp('Your local branch is up-to-date with the remote branch.');
else
fprintf(2, 'WARNING: Your local branch is out of date with the remote.\n');
fprintf(['\tLocal commit: ' localCommit '\n']);
fprintf(['\tRemote commit: ' remoteCommit '\n']);
fprintf('\tPlease run "git pull" to update your local code.\n');
error('Invalid version.')
end
end
function isGit = isGitRepository()
% Check if the current directory is a Git repository by checking for the existence of a .git directory
isGit = exist('.git', 'dir') == 7;
end
function commitHash = getGitCommitHash()
% Get the commit hash of the current branch locally
[~, commitHash] = system('git rev-parse HEAD');
commitHash = strtrim(commitHash); % Remove any trailing newline or space
end
function remoteCommit = getRemoteCommit_hash(branch)
% Get the commit hash of the remote branch
[~, remoteCommit] = system(['git rev-parse origin/' branch]);
remoteCommit = strtrim(remoteCommit); % Remove any trailing newline or space