-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatlabExport.s2s
497 lines (416 loc) · 16.1 KB
/
MatlabExport.s2s
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
'exportchan-ChronRig.s2s
'Created by DPK 09/18/08 from sort16tet_chronrig.s2s
#include "include/chron_include.s2s"
View(LogHandle()).EditSelectAll(); 'clear the log window
View(LogHandle()).EditClear(); 'clear the log window
'Startup log file
PrintLog("\n\n\n********************\n********************\n********************\n\n");
PL(Print$("%s.s2s started\n",View(App(3)).FileName$(3)+View(App(3)).FileName$(4)));
const expPathRoot$ := "D:\\experiments\\";
FilePathSet(expPathRoot$);
var startView%:=0;
startView%:=View();
'Startup log file
PrintLog("\n\n\n********************\n********************\n********************\n\n");
PL(Print$("%s.s2s started\n",View(App(3)).FileName$(3)+View(App(3)).FileName$(4)));
const HUGE% := 16384; '2^14
const LARGE% := 1024;
const SMALL% := 64;
const NUMMAXIMUMTICKS% := 2147483647;
const stimchan% := 17;
const textmarkchan% := 30;
const keyboardchan% := 31;
const digchan% := 32;
const HANDLERID% := 0;
const CANCELCATID% := 1;
const DOCONCATID% := 3;
const SETUPCONCATID% := 5;
var numExpts% := 0;
var catPath$[LARGE%],birdcatpath$[LARGE%],birdrawpath$[LARGE%],bird$[LARGE%],birdanalyzsispath$[LARGE%];
var arrangeHandle%[LARGE%];
var numFilesToExport%[LARGE%];
ArrConst(numFilesToExport%,0);
var filesToExport$[LARGE%][LARGE%];
var fileTimes[LARGE%][LARGE%];
var fileSizes%[LARGE%][LARGE%];
var fileChanNums%[LARGE%][LARGE%];
var catsizes[LARGE%],cattimes[LARGE%];
var exportName$[LARGE%];
var bOutChans%[LARGE%][LARGE%];
var nChans%,numChans%[LARGE%];
var allChans%[LARGE%];
var outChans%[LARGE%];
var sitepath$;
var site$[LARGE%],pen$[LARGE%];
var totsize,tottime;
SetupToolbar();
Toolbar("Toolbar Running, setting up export files.",1+2+4+8+16+32+64+128+256+512,0);
halt;
'''''''''''''''''''''''''''''''
'''' Done Making Expt Files ''''
'''''''''''''''''''''''''''''''
'
''''
'Functions
''''
'
proc SetupToolbar()
ToolbarSet(HANDLERID%,"",Handler%);
ToolbarSet(CANCELCATID%,"Cancel Export Setup",CancelExpt%);
ToolbarSet(SETUPCONCATID%,"Setup a export file",SetupExport%);
ToolbarSet(DOCONCATID%,"Done Setting Up, Do the Exports",DoExports%);
ToolbarEnable(DOCONCATID%,0); 'set to 1, once at least one export has been set up
end
'
func Handler%()
'toolbar stuff in here
return 1;
end
'
func SetupExport%()
numExpts% += 1;
var numepochs%,epochNames$[LARGE%],currepoch%;
var currFile%,numAllFiles%:=0,allFileNames$[LARGE%],allFileSizes[LARGE%],allFileTimes[LARGE%],numTmpFiles%,tmpFileNames$[LARGE%],wfchans%[LARGE%];
var chanfilehandle%,tmphandle%,numwfChans%;
'Set Site
FilePathSet("",0,"Pick Site Folder");
sitepath$ := FilePath$();
PL(Print$("Site path is set to %s\n", sitepath$));
numepochs%:=FileList(epochNames$[],-2,"Epc*");
PL(Print$("found %d subfolders in %s\n", numepochs%, sitepath$));
site$[numExpts%]:=Left$(sitepath$,Len(sitepath$)-1);
while InStr(site$[numExpts%],"\\") do
site$[numExpts%] := DelStr$(site$[numExpts%],1,1);
wend
var sitestart% := InStr(sitepath$,site$[numExpts%]);
pen$[numExpts%]:=Left$(sitepath$,sitestart%-2);
pen$[numExpts%]:=Right$(pen$[numExpts%],Len(pen$[numExpts%]) - InStr(pen$[numExpts%],"Pen") + 1);
var tmpind1%,tmpind2%,tmpind3%;
tmpind1% := InStr(LCase$(sitepath$),"experiments\\raw\\");
bird$[numExpts%] := Right$(sitepath$,Len(sitepath$)-tmpind1%-15);
tmpind2% := InStr(bird$[numExpts%],"\\data\\");
bird$[numExpts%] := Left$(bird$[numExpts%],tmpind2%-1);
var trash$[LARGE%];
var numMultExpt% := 0,dummy%:=0;
numMultExpt% := FileList(trash$[],0,"cat*");
tmpind2% := InStr(sitepath$,"\\data\\");
birdrawpath$[numExpts%] := Left$(sitepath$,tmpind2%);
tmpind3% := InStr(birdrawpath$[numExpts%],"\\raw\\");
birdanalyzsispath$[numExpts%] := Left$(birdrawpath$[numExpts%],tmpind3%) + "analysis\\" + bird$[numExpts%];
PrintLog("birdanalyzsispath$ : %s\n",birdanalyzsispath$[numExpts%]);
FilePathSet(birdanalyzsispath$[numExpts%],0,1); 'this allows us to make the "birdanalysispath" dir if it doesn't exist
birdcatpath$[numExpts%] := birdanalyzsispath$[numExpts%] + "\\matfiles\\";
PrintLog("birdcatpath$ : %s\n",birdcatpath$[numExpts%]);
FilePathSet(birdcatpath$[numExpts%],0,1); 'this allows us to make the "birdcatpath" dir if it doesn't exist
'sitecatpath$[numExpts%] := birdcatpath$[numExpts%] + pen$[numExpts%] + "__" + site$[numExpts%] + "\\"
'PrintLog("sitecatpath$ : %s\n",sitecatpath$[numExpts%]);
'FilePathSet(sitecatpath$[numExpts%],0,1); 'this allows us to make the "sitecatpath" dir if it doesn't exist
exportName$[numExpts%] := Print$("%s_cat_P%s_S%s_%d",bird$[numExpts%],Right$(Left$(Pen$[numExpts%],5),2),Right$(Left$(Site$[numExpts%],6),2),numMultExpt%+numExpts%);
exportName$[numExpts%]:=Input$("Enter final export name, or use provided autoname",exportName$[numExpts%]);
catPath$[numExpts%] := birdcatpath$[numExpts%] + pen$[numExpts%] + "__" + site$[numExpts%] + "__" + exportName$[numExpts%] + "\\";
PrintLog("catPath$ : %s\n",catPath$[numExpts%]);
FilePathSet(catPath$[numExpts%],0,1); 'this allows us to make the "catpath" dir if it doesn't exist
'
arrangeHandle%[numExpts%]:=FileNew(1,1);
for currepoch% :=0 to numepochs%-1 do
epochNames$[currepoch%] := sitepath$ + epochNames$[currepoch%];
FilePathSet(epochNames$[currepoch%]);
numTmpFiles%:=FileList(tmpFileNames$[],0);
for currFile% := 0 to numTmpFiles%-1 do
allFileNames$[numAllFiles%]:= epochNames$[currepoch%] + "\\" + tmpFileNames$[currFile%];
tmphandle% := FileOpen(allFileNames$[numAllFiles%],9,0); 'open as binary to get file size info
if tmphandle% < 0 then
Message("%s error: %d, %s",allFileNames$[numAllFiles%],tmphandle%,Error$(tmphandle%));
endif
View(tmphandle%);
allFileSizes[numAllFiles%] := BSeek(0,2); 'seek to end of file and return number of bytes
BSeek(0,0); 'return to beginning of file, for cleanliness sake
FileClose(0);
tmphandle% := FileOpen(allFileNames$[numAllFiles%],0,0); 'open as spike2 data file to get time info
if tmphandle% < 0 then
Message("%s error: %d, %s",allFileNames$[numAllFiles%],tmphandle%,Error$(tmphandle%));
endif
View(tmphandle%);
numwfChans% := ChanList(wfchans%[],1); 'get a list of all waveform channels
if numwfChans% < 1 then
allFileTimes[numAllFiles%] := -99 'means there are no waveform channels
else
allFileTimes[numAllFiles%] := MaxTime(wfchans%[1]); 'get time of the first waveform channel (WARNING, other channels might have slightly longer times..)
endif
FileClose(0);
View(arrangeHandle%[numExpts%]);
if Len(allFileNames$[numAllFiles%]) > 133 then
Print("\"%s\",\t%6.2f, (sec),\t%4.0f, (MB),%d\n",allFileNames$[numAllFiles%],allFileTimes[numAllFiles%],allFileSizes[numAllFiles%]/1000000.0,numwfChans%);
else
Print("\"%s\",\t\t%6.2f, (sec),\t%4.0f, (MB),%d\n",allFileNames$[numAllFiles%],allFileTimes[numAllFiles%],allFileSizes[numAllFiles%]/1000000.0,numwfChans%);
endif
next
'
next
'tell user to arrange desired files for export in order then press button
if Interact("Arrange the files that you would like to export - in desired order, 1 per line.",1023,0,"Done","Cancel") <> 1 then
numExpts%-=1;
return 0; 'will want to change to something other than halt
endif
'
View(arrangeHandle%[numExpts%]);
WindowVisible(0);
MoveTo(0,0);
var line$,timesec,sizemb%,lbl1$,lbl2$,nnwfchans%;
ReadSetup(","," \t","\"(","\")");
while Read(line$,timesec,lbl1$,sizemb%,lbl2$,nnwfchans%) >= 0 do ' read while not EOF or error
filesToExport$[numExpts%][numFilesToExport%[numExpts%]]:=line$;
fileTimes[numExpts%][numFilesToExport%[numExpts%]]:=timesec;
fileSizes%[numExpts%][numFilesToExport%[numExpts%]]:=sizemb%;
fileChanNums%[numExpts%][numFilesToExport%[numExpts%]]:=nnwfchans%;
numFilesToExport%[numExpts%]+=1;
wend;
ReadSetup();
'
chanfilehandle% := FileOpen(filesToExport$[numExpts%][0],0);
View(chanfilehandle%);
numChans%[numExpts%] := ChanList(allChans%[],2097152); 'excludes virtual chans (in case they got left there after soft referencing during recording)
nChans%:=numChans%[numExpts%];
cattimes[numExpts%]:=tottime;
catsizes[numExpts%]:=totsize;
ArrConst(bOutChans%[numExpts%][],outChans%[]); 'copy outchans to boutchans (batch out chans)
ArrConst(allChans%[],0);
view(chanfilehandle%);
FileClose(0,-1);
FilePathSet(expPathRoot$);
ToolbarEnable(DOCONCATID%,1);
return 1;
end;
'
func DoExports%()
var catNum%;
var datafilehandle%;
var catMax;
var catLogName$;
var fileNum%;
var retCS%;
var retFSA%;
var savename$;
var matSRAS%;
'
for catNum% := 1 to numExpts% do
PrintLog("catNum% %d\n",catNum%);
PL(Print$("starting export file #%d of %d",catNum%,numExpts%));
FilePathSet(catPath$[catNum%]);
var DMindex%,newdigmarkchan%:=0,newerdigmarkchan%:=0;
for fileNum% := 1 to numFilesToExport%[catNum%] do
PL(Print$("starting file #%d of %d\n",fileNum%,numFilesToExport%[catNum%]));
ToolbarText(Print$("export file #%d of %d | file #%d of %d",catNum%,numExpts%,fileNum%,numFilesToExport%[catNum%]));
Draw();
datafilehandle% := FileOpen(filesToExport$[catNum%][fileNum%-1],0,0);
savename$ := filesToExport$[catNum%][fileNum%-1];
savename$ := DelStr$(savename$,0,InStr(savename$,"\\Sub"));
savename$ := DelStr$(savename$,0,InStr(savename$,"\\AutoSv"));
savename$ := DelStr$(savename$,InStr(savename$,".smr"),Len(savename$));
PrintLog("saving %s to export files\n", filesToExport$[catNum%][fileNum%-1]);
PrintLog("saving as %s\n", savename$);
View(datafilehandle%);
ExportChanList();
ExportChanList(0,View(datafilehandle%).MaxTime(),-1);
matSRAS%:=FileSaveAs(catPath$[catNum%]+savename$+".mat",100,1,"",0,"UseSName=0|UseCName=1|WaveTimes=1|WaveData=0|WaveAll=1|WaveSameN=1|Compat=4");
if matSRAS% <> 0 then
PL(Print$("Error exporting to Matlab using FileSaveAs. Error: %s",Error$(matSRAS%)));
else
PL(Print$("\nData exported to Matlab successfully as: %s\n\n",FileName$(0)));
endif
FileClose();
next;
'save arrange handles
View(arrangeHandle%[catNum%]);
catLogName$ := exportName$[catNum%]+".catLog";
FileSaveAs(catLogName$);
FileClose(arrangeHandle%[catNum%],-1);
View(LogHandle());
var catLogLogName$ := exportName$[catNum%]+".catLogLog";
FileSaveAs(catLogLogName$);
FilePathSet(expPathRoot$);
next;
end;
'
func CancelExpt%()
return -1;
end
'
func SelectAllSSC%()
var iSA%;
for iSA% :=1 to nChans% do
DlgValue(iSA%,1);
next;
return 1;
end;
'
func ClearAllSSC%()
var iCA%;
for iCA% :=1 to nChans% do
DlgValue(iCA%,0);
next;
return 1;
end;
'
func CheckSome%()
var iCS%;
for iCS% :=1 to nChans% do
if allChans%[iCS%] = 30 or allChans%[iCS%] = 31 or allChans%[iCS%] = 32 or InStr(ChanTitle$(allChans%[iCS%]),"Aud. Stim") <> 0 then
DlgValue(iCS%,1);
endif
next
return 1;
end;
'
func CheckandResampleDigMarkChan%(datahandle%,digmarkNEWID%,desuspertick)
View(datahandle%);
'
var markchan%[1000],keyboardID%,digmarkID%,allchan%[1000];
ChanList(markchan%[],8); 'picks marker channels - should just be keyboard and digmark
if markchan%[0] > 2 then
Message("Found more than 2 marker channels (expecting just keyboard and digmark). Halting.");
halt;
else
keyboardID%:=31; 'assumptions here, but I don't know how else to distinguish between them...
digmarkID%:=32; 'assumptions here, but I don't know how else to distinguish between them...
endif
'
if digmarkNEWID% = 0 then
ChanList(allchan%[],8192+16384+2097152);
digmarkNEWID% := allchan%[Max(allchan%[1:])+1]+1; 'This will attempt to save the digmark to the next highest non virtual or memory channel number
endif
var memchanID%,memindex%:=0,retmindex%;
var memchanID2%;
var numM1%,numM2%;
memchanID% := MemChan(5);
numM1%:=DownsampleMarkerChan%(digmarkID%,memchanID%,desuspertick);
'
memchanID2% := MemChan(5);
numM2%:=DownsampleMarkerChan%(memchanID%,memchanID2%,desuspertick);
if numM2% <> 0 then
Message("M1-M2: %d events detected\nOne pass through the data did not fix issues - further coding is required\nhave fun!\nFile returned to previous state",numM2%);
ChanDelete(memchanID%);
ChanDelete(memchanID2%);
else
PrintLog("One pass through the data cleared up all digmark issues\n\n");
var MSret%;
MSret% := MemSave(memchanID%,digmarkNEWID%,5,0);
if MSret% < 0 then
Message("MemSave did not work, error is: %d: %s",MSret%,Error$(MSret%));
endif
ChanTitle$(digmarkNEWID%,"DigMark*");
ChanShow(digmarkNEWID%);
ChanDelete(memchanID%);
ChanDelete(memchanID2%);
'below saves the new marker chan as the digmark chan (32) and renames it "DigMark*"
' MemSave(memchanID%,digmarkID%,5,1);
' ChanTitle$(digmarkID%,"DigMark*");
' ChanShow(digmarkID%);
' ChanDelete(memchanID%);
' ChanDelete(memchanID2%);
endif
return digmarkNEWID%;
end
'
func DownsampleMarkerChan%(chantodownsample%,outputmemchan%,desireduspertick)
var oldtime := -0.5,currtime := -0.5,newtime;
var currcode%[4],lastcode%;
var numeventsmoved%:=0;
var code1%[100000];
var nt[100000];
var markerbinsizeus := BinSize(chantodownsample%)*1000000;
var retCS%;
var numshorts% := 0,mostallowednumshorts% := 5;
retCS%:=ChanSave(chantodownsample%,outputmemchan%);
if retCS% < 0 then
ErrDlg(retCS%);
endif
var numItems% := MemGetItem(outputmemchan%);
var currItem% := 1;
while currItem% < numItems% do
oldtime := currtime;
currtime := MemGetItem(outputmemchan%,currItem%,currcode%[]);
if currtime - oldtime < desireduspertick/1000000 then
numshorts% := numshorts% +1;
if numshorts% > mostallowednumshorts% then
Message("There are more than %d short intervals in a row around time: %f in file: %s",numshorts%,currtime,FileName$());
endif
numeventsmoved%+=1;
newtime := oldtime + (Ceil(desireduspertick*1.5+markerbinsizeus)/1000000);
code1%[numeventsmoved%]:=currcode%[0];
nt[numeventsmoved%]:=newtime;
MemSetItem(outputmemchan%,currItem%,newtime,currcode%[]);
'Message(Print$("currinterval: %f\nlastcode: %d\ncurrcode: %d\n",currtime - oldtime,lastcode%,currcode%[0]));
PrintLog("currItem: %d oldtime: %f currinterval: %f\tlastcode: %d\tcurrcode: %d\n",currItem%,oldtime,currtime - oldtime,lastcode%,currcode%[0]);
currtime := oldtime;
else
numshorts% := 0;
currItem% := currItem% + 1;
lastcode% := currcode%[0];
endif
wend;
'
return numeventsmoved%;
end
'
func LoadTrialTimes%(dfh%,tStart[],tEnd[])
var oldtime := -0.5,currtime := -0.5,newtime;
var currcode%[4],lastcode%;
var numeventsmoved%:=0;
var code1%[100000];
var nt[100000];
var retCS%;
var haveend%:=0;
var currSeekTime := 0;
var currTrialNum%:=0;
var cdcCodes%[4],cdcReturn%:=0;
'
while currSeekTime > -0.5 do
'
currSeekTime := View(dfh%).NextTime(digchan%,currSeekTime,cdcCodes%[]);
'
if cdcCodes%[0] = 60 then
currTrialNum% := currTrialNum% +1;
tStart[currTrialNum%] := currSeekTime;
haveend% := 0;
while haveend% = 0 do
currSeekTime := View(dfh%).NextTime(digchan%,currSeekTime,cdcCodes%[]);
if cdcCodes%[0] = 41 then
tEnd[currTrialNum%] := currSeekTime;
haveend% := 1;
endif
wend
endif;
wend;
'
return currTrialNum%;
end
'
func Idle%()
var fnum%;
tottime := 0;
totsize := 0;
var iCS%,totselectedchans%:=0;
for iCS% :=1 to nChans%-4 do 'WARNING: this -4 doesn't count the TOD, digMark, Stim, and keyboard chans if these change or waveforms have higher chan numbers than these, then will have a problem in estimating file size
totselectedchans%+=DlgValue(iCS%);
next
'
for fnum% := 0 to numFilesToExport%[numExpts%]-1 do
tottime += fileTimes[numExpts%][fnum%];
totsize += 15+totselectedchans%*(fileSizes%[numExpts%][fnum%]/(fileChanNums%[numExpts%][fnum%])); 'gives a really rough number - digmark, keyboard, markers utterly negligable... 10 MB headroom found experimentally/file, upped to 15 for safety
next;
DlgValue(allChans%[0]+1, tottime);
DlgValue(allChans%[0]+2, totsize);
return 1;
end
'
func CopyChan%(chan%)
var mc%;
mc% := MemChan(0, chan%); 'Create waveform channel
if mc%>0 then 'Created OK?
ChanComment$(mc%, "Copy of channel "+Str$(chan%));
MemImport(mc%, chan%, 0, MaxTime()); 'Copy data
ChanShow(mc%); 'display new channel
endif;
return mc%; 'Return the new memory channel number
end;