-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathdb_set_session.m
More file actions
120 lines (108 loc) · 4.91 KB
/
Copy pathdb_set_session.m
File metadata and controls
120 lines (108 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
function [session,basename, basepath] = db_set_session(varargin)
% Loads a session from the database or defines paths for existing session struct
% INPUTS
% varargin described below
%
% OUTPUTS
% session : session struct containing the db session info
% basename : basename of the session
% basepath : basepath of the session
% By Peter Petersen
% petersen.peter@gmail.com
% Last edited: 07-11-2019
p = inputParser;
% Inputs
addParameter(p,'sessionId',[],@isnumeric); % DB numeric ID
addParameter(p,'sessionName',[],@isstr); % DB session name / basename
addParameter(p,'session',[],@isstruct); % session struct (can be used to generate the paths)
% Parameters
addParameter(p,'saveMat',true,@islogical); % Saves the session struct to a mat file
addParameter(p,'changeDir',true,@islogical); % change directory to basepath?
parse(p,varargin{:})
sessionId = p.Results.sessionId;
sessionName = p.Results.sessionName;
session = p.Results.session;
saveMat = p.Results.saveMat;
changeDir = p.Results.changeDir;
if ~isempty(sessionId)
sessions = db_load_sessions('sessionId',sessionId);
if isempty(sessions)
return
end
elseif ~isempty(sessionName)
sessions = db_load_sessions('sessionName',sessionName);
if isempty(sessions)
return
end
else
sessions{1} = session;
end
db_settings = db_load_settings;
defined_repositories = fieldnames(db_settings.repositories);
for i = 1:length(sessions)
session = sessions{i};
if ~isfield(session.general,'repositories')
warning('The repository has not been defined for the session.');
return
elseif ~contains(defined_repositories,{session.general.repositories{1}})
warning(['The repository has not been defined. Please specify the path for ' session.general.repositories{1},' in db_local_repositories.m']);
edit db_local_repositories.m
return
end
basename = session.general.name;
if strcmp(session.general.repositories{1},'NYUshare_Datasets')
Investigator_name = strsplit(session.general.investigator,' ');
path_Investigator = [Investigator_name{2},Investigator_name{1}(1)];
basepath = fullfile(db_settings.repositories.(session.general.repositories{1}), path_Investigator,session.animal.name, session.general.name);
elseif strcmp(session.general.repositories{1},'NYUshare_AllenInstitute')
basepath = fullfile(db_settings.repositories.(session.general.repositories{1}), session.general.name);
else
basepath = fullfile(db_settings.repositories.(session.general.repositories{1}), session.animal.name, session.general.name);
end
session.general.baseName = basename;
session.general.basePath = basepath;
if ~isfield(session.extracellular,'leastSignificantBit') || session.extracellular.leastSignificantBit==0
disp('''session.extracellular.leastSignificantBit'' set to default for intan system = 0.195 µV/bit')
session.extracellular.leastSignificantBit = 0.195; % Intan system = 0.195 µV/bit
end
% Checking format of spike groups and electrode groups (must be of type cell)
if isfield(session.extracellular,'spikeGroups') && isfield(session.extracellular.spikeGroups,'channels') && isnumeric(session.extracellular.spikeGroups.channels)
session.extracellular.spikeGroups.channels = num2cell(session.extracellular.spikeGroups.channels,2);
end
if isfield(session.extracellular,'electrodeGroups') && isfield(session.extracellular.electrodeGroups,'channels') && isnumeric(session.extracellular.electrodeGroups.channels)
session.extracellular.electrodeGroups.channels = num2cell(session.extracellular.electrodeGroups.channels,2)';
end
if changeDir
try
disp(['Changing Matlab directory to session basepath: ' basepath])
cd(basepath)
catch
error(['db_set_path: Unable to change directory to basepath: ', basepath])
end
end
if saveMat
[stat,mess]=fileattrib(fullfile(basepath, 'session.mat'));
if stat==0
try
disp(['Saving ',basename,'.session.mat file to basepath'])
save(fullfile(basepath, [basename,'.session.mat']),'session','-v7.3','-nocompression');
catch
warning(['Failed to save ',basename,'.session.mat. Location not available']);
end
elseif mess.UserWrite
save(fullfile(basepath, [basename,'.session.mat']),'session','-v7.3','-nocompression');
else
warning(['Unable to write to ',basename,'.session.mat. No writing permissions.']);
end
end
if length(sessions)>1
sessions{i} = session;
basepaths{i} = basepath;
basenames{i} = basename;
end
end
if length(sessions)>1
session = sessions;
basepath = basepaths;
basename = basenames;
end