-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyzeExperiment.m
264 lines (234 loc) · 6.66 KB
/
AnalyzeExperiment.m
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
function varargout = AnalyzeExperiment(DirName)
tic
if(ispc)
Slash = '\';
AltSlash = '/';
else
Slash = '/';
AltSlash = '\';
end
DefaultDir = 'C:\Documents and Settings\Rachel\My Documents\spectra\';
LinuxDir = '/mnt/dwidget/';
%Add trailing Slash
if(DirName(end) ~= Slash)
DirName = [DirName, Slash];
end
[Records, IsMorrisLecar] = OpenParamFile(DirName);
DirStruct = dir(DirName);
FileList = [];
OrderInd = zeros(size(Records));
for n = 1:length(DirStruct)
%Check if the file in DirStruct(n).name is in the list of records
%we want to analyze:
RecordInd = IsInRecordsList(DirStruct(n).name, Records);
if(RecordInd > 0)
if(Records(RecordInd).Matched == true) %make sure that it can only
continue; %be matched once (in case
else %there is a .abf AND a .smr
Records(RecordInd).Matched = true;
end
FileStruct = DirStruct(n);
FileStruct.g_syn = Records(RecordInd).g_syn;
FileStruct.g_h = Records(RecordInd).g_h;
FileStruct.Condition = Records(RecordInd).Cat;
FileList = [FileList, FileStruct];
OrderInd(RecordInd) = length(FileList);
end
end
MissedInd = find(OrderInd == 0);
if(length(MissedInd) > 0)
fprintf(2, 'Missed file number(s) in %s:', DirName);
for Ind = MissedInd
fprintf(2, ' (%s %g)', Records(Ind).Cat, Records(Ind).FileNum)
end
fprintf(2, '%s', '\n');
error(['Error analyzing ', DirName])
end
FileList = FileList(OrderInd); %Analyze in order of records list
NumFiles = length(FileList);
if(NumFiles == 0)
error(['No files to analyze in ', DirName]);
end
OutFileName = [DirName, 'Analysis.mat'];
ContinueFileName = [DirName, 'Analysis_Continue.mat'];
[Analysis, Experiments, Start_n] = CheckContinue(ContinueFileName);
DoContinue = true;
n = Start_n;
%One would wish this worked, but it doesn't:
%CleanObj = onCleanup(@()SaveResults(OutFileName, Analysis, ...
% Experiments, n, DoContinue));
for n = Start_n:NumFiles
% CleanObj = onCleanup(@()SaveResults(OutFileName, Analysis, ...
% Experiments, n, DoContinue));
try
Experiments = [Experiments, GetExpNum(FileList(n).name)];
FileName = [DirName, FileList(n).name];
fprintf('Analyzing %s (%d of %d)\n', FileName, n, NumFiles)
if(IsMorrisLecar)
OutStruct = AnalyzeML(FileName);
else
OutStruct = RunAnalyze(FileName, 0);
end
if(strcmp(DirName, LinuxDir))
OutStruct.FileName = [DefaultDir, FileList(n).name];
else
OutStruct.FileName = FileName;
end
OutStruct.ExpNum = Experiments(end);
OutStruct.g_syn = FileList(n).g_syn;
OutStruct.g_h = FileList(n).g_h;
OutStruct.Condition = FileList(n).Condition;
Analysis = [Analysis, OutStruct];
catch
ErrStruct = lasterror;
if(n > Start_n)
SaveResults(OutFileName, ContinueFileName, DoContinue, ...
Analysis, Experiments, n);
end
rethrow(ErrStruct)
end
end
DoContinue = false;
%CleanObj = onCleanup(@()SaveResults(OutFileName, Analysis, ...
% Experiments, n, DoContinue));
ExpNum = Experiments(1);
if(sum(Experiments ~= ExpNum) > 0)
ExpNum = -1;
end
if(nargout <= 1)
varargout = {Analysis};
elseif(nargout == 2)
varargout = {Analysis, ExpNum};
end
SaveResults(OutFileName, ContinueFileName, DoContinue, ...
Analysis, Experiments, n);
Toc
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Records, IsMorrisLecar] = OpenParamFile(DirName)
FileList = dir(DirName);
FileList = GetTextFiles(FileList);
if(length(FileList) ~= 1)
error(['Error in OpenParamFile. Must specify directory with' ...
' exactly one .txt file.']);
end
FileName = [DirName, FileList.name];
fid = fopen(FileName);
if(fid < 0)
error(['Error opening ', FileName]);
end
dummy = fgets(fid); %contains the header description
Records = [];
IsMorrisLecar = 0;
while(1)
Temp = fscanf(fid, '%d', 3);
if(length(Temp) < 3)
break;
end
Cat = fscanf(fid, '%s', 1);
This.g_syn = Temp(1);
This.g_h = Temp(2);
This.Cat = Cat;
This.FileNum = Temp(3);
IsMorrisLecar = IsMorrisLecarCat(Cat, IsMorrisLecar);
This.AbfNamePart = sprintf('%s_%.4d.abf', Cat, Temp(3));
This.SmrNamePart = sprintf('%s_%.4d.smr', Cat, Temp(3));
This.DatNamePart = sprintf('%s_%.4d.dat', Cat, Temp(3));
This.MatNamePart = sprintf('%s_%.4d.mat', Cat, Temp(3));
This.Matched = false;
Records = [Records, This];
end
IsMorrisLecar = (IsMorrisLecar > 0);
fclose(fid);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function TextFileList = GetTextFiles(FileList)
TextFileList = [];
for n = 1:length(FileList)
if(strfind(FileList(n).name, '.txt'))
TextFileList = [TextFileList, FileList(n)];
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function RecordInd = IsInRecordsList(FileName, Records)
RecordInd = 0;
for n = 1:length(Records)
if(length(strfind(FileName, Records(n).AbfNamePart)) > 0)
RecordInd = n;
return
elseif(length(strfind(FileName, Records(n).SmrNamePart)) > 0)
RecordInd = n;
return
elseif(length(strfind(FileName, Records(n).DatNamePart)) > 0)
RecordInd = n;
return
elseif(length(strfind(FileName, Records(n).MatNamePart)) > 0)
RecordInd = n;
return
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ExpNum = GetExpNum(FileName)
Ind = strfind(FileName, '_');
if(length(Ind) >= 2);
Range = (Ind(1)+1):(Ind(2)-1);
ExpNum = str2double(FileName(Range));
else
ExpNum = -1;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Analysis, Experiments, Start_n] = CheckContinue(ContinueFileName)
DoContinue = false;
try
load(ContinueFileName);
catch
DoContinue = false;
end
if(DoContinue)
disp(sprintf('Resuming from file# %g', Start_n))
else
Analysis = [];
Experiments = [];
Start_n = 1;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function SaveResults(OutFileName, ContinueFileName, DoContinue, ...
Analysis, Experiments, n)
if(DoContinue)
if(n > 1) %Try to save any actual progress
Start_n = n;
Analysis = Analysis(1:(n - 1));
Experiments = Experiments(1:(n - 1));
DoContinue = true;
try
save(ContinueFileName, 'Analysis', 'Experiments', 'Start_n', ...
'DoContinue');
catch
disp(sprintf('Warning, could''t save %s', ContinueFileName))
end
end
else
ExpNum = Experiments(1);
if(sum(Experiments ~= ExpNum) > 0)
ExpNum = -1;
end
try
save(OutFileName, 'Analysis', 'ExpNum');
Saved = true;
catch
disp(sprintf('Warning, could''t save %s', OutFileName))
Saved = false;
end
if(Saved) %delete the continue file, if it exists
fid = fopen(ContinueFileName, 'r');
if(fid > 0)
fclose(fid);
delete(ContinueFileName);
end
end
end
return