-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,58 @@ | ||
function fullPath = getFile(obj, datasetURL) | ||
%GETFILE Recovers the full filepath of a file on the repository, given the datasetURL | ||
% This function is SUPER inefficient because it has to load all | ||
% filerecords from the database, and then search for the specific | ||
% filerecord whose parent dataset matches the one supplied as input. | ||
function [fullPath, exists] = getFile(obj, eid, type) | ||
%GETFILE Returns the full filepath for a given eid. | ||
% Returns the full path of associated data files given a dataset or file | ||
% record URL or eid (experiment ID). Also returns a logical array | ||
% indicating whether files exist for each element in the `eid` array. | ||
% | ||
% datasetURL: URL of a dataset on Alyx | ||
% Inputs: | ||
% `eid`: a string or char array containing the full URL(s) or eid(s) of | ||
% dataset(s) / file record(s) on Alyx. | ||
% `type`: a string indicating type of eid(s) ('dataset' (default) or | ||
% 'file') | ||
% | ||
% See also ALYX, GETDATA | ||
% Outputs: | ||
% `fullPath`: a cellstring containing the full data file path(s) for | ||
% each element in the `eid` array | ||
% `exists`: a logical array indicating whether files exist for each eid | ||
% in the `eid` array | ||
% | ||
% See also ALYX, GETDATA, GETSESSIONS | ||
% | ||
% Part of Alyx | ||
% TODO: Create endpoint so this function is no longer inefficient? | ||
% 2017 PZH created | ||
% 2019 MW Rewrote | ||
|
||
if nargin < 3; type = 'dataset'; end | ||
|
||
% Get all file records | ||
filerecords = obj.getData('files'); | ||
|
||
% Extract the datasets which are parent to the filerecords | ||
datasets = cellfun(@(fr) fr.dataset, filerecords, 'uni', 0); | ||
|
||
% Find whichever filerecord has input datasetURL as its parent | ||
idx = contains(datasets, datasetURL); | ||
|
||
if any(idx) | ||
fr = filerecords{idx}; | ||
relPath = fr.relative_path; % Get relative path of file | ||
|
||
repo = obj.getData('data-repository'); % Get absolute path of repository | ||
fullPath = [repo{1}.path relPath]; % Recover the full path of the file on the repository | ||
% Convert array of strings to cell string | ||
if isstring(eid) && ~isscalar(eid) | ||
eid = cellstr(eid); | ||
else | ||
error('No filerecords with inputted datasetURL as its parent'); | ||
eid = ensureCell(eid); | ||
end | ||
|
||
% Validate URL (UUIDs have 36 characters) | ||
assert(all(cellfun(@(str)ischar(str) && length(str) >= 36, eid)), 'Invalid eid') | ||
eid = mapToCell(@(str)str(end-35:end), eid); | ||
|
||
% Get all file records | ||
switch lower(type) | ||
case 'file' | ||
filerecords = mapToCell(@(url)obj.getData(['files/', url]), eid); | ||
repos = obj.getData('data-repository'); | ||
repos = containers.Map({repos.name}, {repos.data_url}); | ||
fullPath = mapToCell(@(s)[repos(s.data_repository) s.relative_path], filerecords); | ||
exists = cellfun(@(s)s.exists, filerecords); | ||
% Return as char if user expects one output | ||
if numel(eid) == 1; fullPath = fullPath{1}; end | ||
case 'dataset' | ||
filerecords = catStructs(mapToCell(@(url)getOr(obj.getData(['datasets/', url]), 'file_records'), eid)); | ||
exists = [filerecords.exists]; | ||
fullPath = {filerecords.data_url}; | ||
% Remove records with empty url field | ||
exists = exists(emptyElems(fullPath)); | ||
fullPath = rmEmpty(fullPath); | ||
otherwise | ||
error('Alyx:GetFile:InvalidType', 'Invalid eid type: must be ''dataset'' or ''file''') | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.