Skip to content
Snippets Groups Projects
SaveTranscriptions.m 4.39 KiB
Newer Older
checkVersion()

% Check for miscelaneous transcriptions in the work space
miscLS = who('-regexp', '^ls.*[0-9]');
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
% Update progress
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
uisave({'lss', 'remainingFiles'}, ['lss_' reviewerInit '.mat'])
function ls = CheckmiscLS(miscLS)
ls = [];
if isempty(miscLS); return; end

fprintf(2, 'Warning: multiple potential ''ls'' files were found in the work space. \n\n')

nLS = length(miscLS);
lsNames = string(['ls'; miscLS]);
formatPat = '\t%i. %s\n';
formattedstring = compose(formatPat, (0:nLS)', lsNames);
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))
        fprintf('%s has been selected.', lsNames(userResp + 1))
        break
    else
        fprintf('%i is an invalid response.', userResp)
    end
end

ls = lsNames(userResp + 1);

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
    isUntouched(i) = isequal(ls.Labels.Words{i}, lss.Labels.Words{i});
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)

% Move ls labels to lss
nTrials = height(ls.Labels);
for iTrial = 1:nTrials
    setLabelValue(lss, iTrial, 'Words', ls.Labels.Words{iTrial}.ROILimits, ls.Labels.Words{iTrial}.Value)
end
%% Git managment
function checkVersion()
    % Check if the current directory is a Git repository
    if ~isGitRepository()
        error('This is not a Git repository.');
    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
        disp('WARNING: Your local branch is out of date with the remote.');
        disp(['Local commit: ' localCommit]);
        disp(['Remote commit: ' remoteCommit]);
        disp('Please run "git pull" to update your local code.');
    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
end