-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcatenate.s2s
822 lines (715 loc) · 28.6 KB
/
Concatenate.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
'concatchan-ChronRig.s2s
'Created by DPK 09/18/08 from sort16tet_chronrig.s2s
' THIS IS THE CORRECT CONCAT FILE BUT BIG WARNING
'WARNING!!!
'This script sometimes drops the date timestamp marker for one of the subfiles - this is absolutely unacceptable and needs to be fixed
'quickest fix: add a check for an equal number of date textmarks (F3 code)
'better fix: figure out why they're being dropped and fix it
'best fix: save an absolute timestamp at each trial instead of a relative one - requires redoing the read S2mat mfile
#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;
'var todChan% := 33; 'WARNING: careful when hardcoding channel numbers
'var todChan% := 50;
const HANDLERID% := 0;
const CANCELCATID% := 1;
const DOCONCATID% := 3;
const SETUPCONCATID% := 5;
var numCats% := 0;
var catPath$[LARGE%],birdcatpath$[LARGE%],birdrawpath$[LARGE%],bird$[LARGE%],birdanalyzsispath$[LARGE%];
var arrangeHandle%[LARGE%];
var numFilesToConcat%[LARGE%];
ArrConst(numFilesToConcat%,0);
var filesToConcat$[LARGE%][LARGE%];
var fileTimes[LARGE%][LARGE%];
var fileSizes%[LARGE%][LARGE%];
var fileChanNums%[LARGE%][LARGE%];
var catsizes[LARGE%],cattimes[LARGE%];
var catFname$[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 concat files.",1+2+4+8+16+32+64+128+256+512,0);
halt;
'''''''''''''''''''''''''''''''
'''' Done Making Cat Files ''''
'''''''''''''''''''''''''''''''
'
''''
'Functions
''''
'
proc SetupToolbar()
ToolbarSet(HANDLERID%,"",Handler%);
ToolbarSet(CANCELCATID%,"Cancel Concat Setup",CancelCat%);
ToolbarSet(SETUPCONCATID%,"Setup a concat file",SetupConcat%);
ToolbarSet(DOCONCATID%,"Done Setting Up, Do the Concats",DoConcats%);
ToolbarEnable(DOCONCATID%,0); 'set to 1, once at least one concat has been set up
end
'
func Handler%()
'toolbar stuff in here
return 1;
end
'
func SetupConcat%()
numCats% += 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$[numCats%]:=Left$(sitepath$,Len(sitepath$)-1);
while InStr(site$[numCats%],"\\") do
site$[numCats%] := DelStr$(site$[numCats%],1,1);
wend
var sitestart% := InStr(sitepath$,site$[numCats%]);
pen$[numCats%]:=Left$(sitepath$,sitestart%-2);
pen$[numCats%]:=Right$(pen$[numCats%],Len(pen$[numCats%]) - InStr(pen$[numCats%],"Pen") + 1);
var tmpind1%,tmpind2%,tmpind3%;
tmpind1% := InStr(sitepath$,"experiments\\raw\\");
bird$[numCats%] := Right$(sitepath$,Len(sitepath$)-tmpind1%-15);
tmpind2% := InStr(bird$[numCats%],"\\data\\");
bird$[numCats%] := Left$(bird$[numCats%],tmpind2%-1);
tmpind2% := InStr(sitepath$,"\\data\\");
birdrawpath$[numCats%] := Left$(sitepath$,tmpind2%);
tmpind3% := InStr(birdrawpath$[numCats%],"\\raw\\");
birdanalyzsispath$[numCats%] := Left$(birdrawpath$[numCats%],tmpind3%) + "analysis\\" + bird$[numCats%];
birdcatpath$[numCats%] := birdanalyzsispath$[numCats%] + "\\catfiles\\";
catPath$[numCats%] := birdcatpath$[numCats%] + pen$[numCats%] + "__" + site$[numCats%] + "\\";
'
arrangeHandle%[numCats%]:=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("error: %d, %s",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
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%[numCats%]);
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 concatenation in order then press button
if Interact("Arrange the files that you would like to concatenate - in desired order, 1 per line.",1023,0,"Done","Cancel") <> 1 then
numCats%-=1;
return 0; 'will want to change to something other than halt
endif
'
View(arrangeHandle%[numCats%]);
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
filesToConcat$[numCats%][numFilesToConcat%[numCats%]]:=line$;
fileTimes[numCats%][numFilesToConcat%[numCats%]]:=timesec;
fileSizes%[numCats%][numFilesToConcat%[numCats%]]:=sizemb%;
fileChanNums%[numCats%][numFilesToConcat%[numCats%]]:=nnwfchans%;
numFilesToConcat%[numCats%]+=1;
wend;
ReadSetup();
'
'PrintLog("opening %s\n", filesToConcat$[numCats%]);
chanfilehandle% := FileOpen(filesToConcat$[numCats%][0],0);
View(chanfilehandle%);
'var memtodChan%:=AddTODChan%();
'MemSave(memtodChan%,todChan%,8); 'saves the mem chan as a textmark into channel todChan%
'ChanDelete(memtodChan%);
'ChanShow(todChan%);
numChans%[numCats%] := ChanList(allChans%[],2097152); 'excludes virtual chans (in case they got left there after soft referencing during recording)
nChans%:=numChans%[numCats%];
if SetOutChans%() = -1 then 'User canceled current Concat
site$[numCats%] := "";
pen$[numCats%] := "";
arrangeHandle%[numCats%] := -1;
ArrConst(filesToConcat$[numCats%][:],"");
ArrConst(fileTimes[numCats%][:],0);
ArrConst(fileSizes%[numCats%][:],0);
ArrConst(fileChanNums%[numCats%][:],0);
numFilesToConcat%[numCats%]:=0;
catPath$[numCats%] := "";
numChans%[numCats%] := 0;
numCats%-=1;
'ChanDelete(todChan%); 'delete the TOD TM channel created in AddTODChan%() TOTALLY inneficient Hack
view(chanfilehandle%);
FileClose(0,-1);
ToolbarEnable(DOCONCATID%,1);
return 1;
endif
cattimes[numCats%]:=tottime;
catsizes[numCats%]:=totsize;
ArrConst(bOutChans%[numCats%][],outChans%[]); 'copy outchans to boutchans (batch out chans)
ArrConst(allChans%[],0);
view(chanfilehandle%);
'ChanDelete(todChan%); 'delete the TOD TM channel created in AddTODChan%() TOTALLY inneficient Hack
FileClose(0,-1);
FilePathSet(birdanalyzsispath$[numCats%],0,1); 'this allows us to make the "birdanalysispath" dir if it doesn't exist
FilePathSet(birdcatpath$[numCats%],0,1); 'this allows us to make the "birdcatpath" dir if it doesn't exist
FilePathSet(catPath$[numCats%],0,1); 'this allows us to make the "catpath" dir if it doesn't exist
var trash$[LARGE%];
var numMultCat% := 0,dummy%:=0;
numMultCat% := FileList(trash$[],0,"cat*");
catFname$[numCats%] := Print$("%s_cat_P%s_S%s_%d.smr",bird$[numCats%],Right$(Left$(Pen$[numCats%],5),2),Right$(Left$(Site$[numCats%],6),2),numMultCat%+numCats%);
catFname$[numCats%]:=Input$("Enter final concat file name, or use provided autoname",catFname$[numCats%]);
FilePathSet(expPathRoot$);
ToolbarEnable(DOCONCATID%,1);
return 1;
end;
'
func DoConcats%()
var catNum%;
var datafilehandle%;
var catMax;
var catLogName$;
var fileNum%;
var cathandle%;
var retCS%;
var retFSA%;
'
for catNum% := 1 to numCats% do
PrintLog("catNum% %d\n",catNum%);
PL(Print$("starting concatenation file #%d of %d",catNum%,numCats%));
var DMindex%,newdigmarkchan%:=0,newerdigmarkchan%:=0;
for fileNum% := 1 to numFilesToConcat%[catNum%] do
PL(Print$("starting file #%d of %d\n",fileNum%,numFilesToConcat%[catNum%]));
ToolbarText(Print$("concatenation file #%d of %d | file #%d of %d",catNum%,numCats%,fileNum%,numFilesToConcat%[catNum%]));
Draw();
datafilehandle% := FileOpen(filesToConcat$[catNum%][fileNum%-1],0,0);
PrintLog("saving %s to concat files\n", filesToConcat$[catNum%][fileNum%-1]);
if fileNum% = 1 then
'here, get the actual channel samplerate
var currchnum%,chbinsize:=-1,oldchbinsize:=99;',uspertimeunit;
'uspertimeunit := View(datafilehandle%).SampleTimePerAdc();
for currchnum% := 1 to bOutChans%[catNum%][0] do 'get the smallest binsize of all the waveform channels
if ChanKind(bOutChans%[catNum%][currchnum%]) = 1 then
chbinsize := Ceil(View(datafilehandle%).BinSize(bOutChans%[catNum%][currchnum%])*1e6);
if chbinsize <> oldchbinsize and currchnum% <> 1 then
Message("There are multiple binsizes for different channels here, proceeed with caution");
endif
if chbinsize > oldchbinsize then
chbinsize := oldchbinsize;
endif
oldchbinsize := chbinsize;
endif
next
if chbinsize = -1 then
chbinsize := View(datafilehandle%).BinSize();
endif
var ftb := FileTimeBase();
'cathandle%:=FileNew(7,1,chbinsize,uspertimeunit,1,400); 'Create new file
cathandle%:=FileNew(7,1,chbinsize,1,1,400,2); 'Create new file
var haveoutputtocatfile% := 0;
PrintLog("created new file with %.6f microsecond resolution\n", View(cathandle%).Binsize()*1e6);
endif;
'
for currchnum% := 1 to bOutChans%[catNum%][0] do 'do digmark channel binsize checks
View(datafilehandle%);
'Message("%d",bOutChans%[catNum%][currchnum%]);
var didreplacedigmark%:=0,didreplacedigmarkagain%:=0;
var retck%;
retck% := ChanKind(bOutChans%[catNum%][currchnum%]);
if ChanKind(bOutChans%[catNum%][currchnum%]) = 5 then 'marker channel
'Message("marker");
if bOutChans%[catNum%][currchnum%] = digchan% then 'DigMark
'Message("DM");
var chbinsize2;
chbinsize2 := View(datafilehandle%).BinSize(bOutChans%[catNum%][currchnum%]);
if chbinsize2 < chbinsize*1e-6 then 'this means that the digmark channel has a smaller uspertick than the waveform channels and might not be able to be represented in the output concat file
'need to look for smallest interval between data points and set the bin size to that (most conservative for digmark channel but requires interpolating the waveform channels)
'or alternatively - manually assign timepoints to each indiv digmark (if done right, should allow for longer files)
'the below code does the latter
'1. run resampledigmarkchan and get newdigmarkid
'2. find digmark channel in bOutChans% and replace with newdigmarkid
'3. do concat below and then delete newdigmarkchan
newdigmarkchan% := CheckandResampleDigMarkChan%(datafilehandle%,newdigmarkchan%,chbinsize);
if newdigmarkchan% > 0 then
var outchnum%;
for outchnum% := 1 to bOutChans%[catNum%][0]+1 do
if bOutChans%[catNum%][outchnum%] = digchan% then
bOutChans%[catNum%][outchnum%] := newdigmarkchan%;
didreplacedigmark% := 1;
DMindex%:=outchnum%;
endif
next
endif
endif
endif
endif
next
'var todmemChan%:=AddTODChan%();
var DoCompresstime% := 0;
if DoCompresstime% = 1 then
if didreplacedigmark% = 1 then
var startchunktime := 5.0;
var trialStart[HUGE%],trialEnd[HUGE%];
var trialPreSeconds:=2.0,btTrialpadding:=0.1;
var totalnumTrials%;
totalnumTrials% := LoadTrialTimes%(datafilehandle%,trialStart[],trialEnd[]);
var currStart[HUGE%],currEnd[HUGE%],trialNum%;
View(datafilehandle%);
currStart[0] := 0.00;
currEnd[0] := Min(startchunktime+chbinsize*1e-6,trialStart[1]-trialPreSeconds-chbinsize*1e-6);
var tmpmemchan%;
tmpmemchan% := CopyChan%(newdigmarkchan%);
var allchan%[LARGE%];
ChanList(allchan%[],8192+16384+2097152);
newerdigmarkchan% := allchan%[Max(allchan%[1:])+1]+1; 'This will attempt to save the digmark to the next highest non virtual or memory channel number
if newerdigmarkchan% > 0 then
for outchnum% := 1 to bOutChans%[catNum%][0]+1 do
if bOutChans%[catNum%][outchnum%] = newdigmarkchan% then
bOutChans%[catNum%][outchnum%] := newerdigmarkchan%;
didreplacedigmarkagain% := 1;
DMindex%:=outchnum%;
endif
next
endif
var digmarkstartcopycodes%[4],retMSI%;
ArrConst(digmarkstartcopycodes%[],0);
digmarkstartcopycodes%[0] := 42;
'get start and end times of chunks to save
for trialNum% := 1 to totalnumTrials% do
currStart[trialNum%] := Max(0.0,Max(currEnd[trialNum%-1]+chbinsize*1e-6,trialStart[trialNum%]-trialPreSeconds-chbinsize*1e-6));
retMSI% := MemSetItem(tmpmemchan%,0,currStart[trialNum%],digmarkstartcopycodes%[]);
'currEnd[trialNum%] := trialEnd[trialNum%]+trialPostSeconds;
if trialNum% <> totalnumTrials% then
currEnd[trialNum%] := trialEnd[trialNum%]+chbinsize*1e-6;
else
currEnd[trialNum%] := Min(View(datafilehandle%).MaxTime(),trialEnd[trialNum%]+chbinsize*1e-6);
endif
next;
retCS%:=ChanSave(tmpmemchan%,newerdigmarkchan%);
'here plop the new textmarks in for each trialtime relative to file start
var foundit%:=0;
var currcodes%[4];
var currtext$;
var currTMtime;
' for trialNum% := 1 to totalnumTrials% do
' foundit%:=0;
' ArrConst(currcodes%[],0);
'
' while foundit% = 0 do 'right now a superfluous loop, as I don't expect anything but stimulus textmarks
' currTMtime := NextTime(todmemChan%, currStart[trialNum%],currcodes%[],currtext$);
' foundit%:=1;
' wend;
' var newtmtext$ := Print$("%f",currTMtime);
' var newTMcodes%[4];
' ArrConst(newTMcodes%[],244);
' MemSetItem(todmemChan%,0,currTMtime+0.001,newTMcodes%[],newtmtext$);
'
' next;
'MemSave(todmemChan%,todChan%,8); 'saves the mem chan as a textmark into channel todChan%
'ChanDelete(todmemChan%);
'ChanShow(todChan%);
var startattrialnum% := 0;
if currEnd[0] < 0 or currStart[1] = 0.0 or currStart[1] < currEnd[0] then
startattrialnum% := 1;
endif
for trialNum% := startattrialnum% to totalnumTrials% do
View(cathandle%);
catMax:=MaxTime();
View(datafilehandle%);
if Ceil((catMax+chbinsize*1e-6+btTrialpadding)/(chbinsize*1e-6)) >= NUMMAXIMUMTICKS% then
Message("Concat file too large, hit tick limit!!\nHalting!");
halt;
else
if haveoutputtocatfile% <> 1 then
retCS% := ChanSave(bOutChans%[catNum%][],-1,cathandle%,currStart[trialNum%], currEnd[trialNum%], 0);
haveoutputtocatfile% := 1;
else
retCS% := ChanSave(bOutChans%[catNum%][],-1,cathandle%,currStart[trialNum%], currEnd[trialNum%], catMax+chbinsize*1e-6+btTrialpadding);
endif
PrintLog("Saved trial# %d\tstart: %4.4f\tend: %4.4f\tretCS: %d\tcurrnumticks: %d\n",trialNum%,currStart[trialNum%],currEnd[trialNum%],retCS%,Ceil((catMax+chbinsize*1e-6+btTrialpadding)/(chbinsize*1e-6)));
endif
next;
else
Message("Need to replace the digmark chan for compression to work properly, halting.");
halt;
endif
else
View(cathandle%);
catMax:=MaxTime();
View(datafilehandle%);
retCS% := ChanSave(bOutChans%[catNum%][],-1,cathandle%,0.00, MaxTime(), catMax+chbinsize*1e-6);
endif
PL(Print$("ChanSave returned %d",retCS%));
if didreplacedigmark% = 1 then
ChanDelete(newdigmarkchan%);
bOutChans%[catNum%][DMindex%] := digchan%;
if didreplacedigmarkagain% = 1 then
ChanDelete(newerdigmarkchan%);
endif
endif
View(datafilehandle%);
'ChanDelete(todChan%); 'delete the TOD TM channel created in AddTODChan%()
FileClose();
next;
'change the DigMark Channel number back to 32
View(cathandle%);
var retCS2%;
if didreplacedigmarkagain% = 1 then
retCS2% := ChanSave(newerdigmarkchan%,digchan%,cathandle%,0.0, MaxTime());
if retCS2% > 0 then
ChanDelete(newerdigmarkchan%);
else
Message("Error: %d; %s",retCS2%,Error$(retCS2%));
endif
else
if didreplacedigmark% = 1 then
retCS2% := ChanSave(newdigmarkchan%,digchan%,cathandle%,0.0, MaxTime());
if retCS2% > 0 then
ChanDelete(newdigmarkchan%);
else
Message("Error: %d; %s",retCS2%,Error$(retCS2%));
endif
endif
endif
'save log file for each concat file
FilePathSet(catPath$[catNum%]);
PrintLog("saving the file %s in %s\n", catFname$[catNum%], catPath$[catNum%]);
view(cathandle%);
ChanShow(-1);
retFSA%:=FileSaveAs(catFname$[catNum%]); 'save the new concatenated file
PrintLog("FileSaveAs returned %d",retFSA%);
FileClose();
'Add error control here
'save arrange handles
View(arrangeHandle%[catNum%]);
catLogName$ := Left$(catFname$[catNum%],(Len(catFname$[catNum%])-4))+".catLog";
FileSaveAs(catLogName$);
FileClose(arrangeHandle%[catNum%],-1);
View(LogHandle());
var catLogLogName$ := Left$(catFname$[catNum%],(Len(catFname$[catNum%])-4))+".catLogLog";
FileSaveAs(catLogLogName$);
FilePathSet(expPathRoot$);
next;
end;
'
func CancelCat%()
return -1;
end
'
func SetOutChans%()
var ssc%,ssc1%,ssc2%,ssc3%,chooseWFChans%[nChans%+2];
outChans%[0]:=0;
tottime:=0;
totsize:=0;
DlgCreate("Pick channels to concat",32.5,0.1,60,nChans%+5,-1,0);
DlgButton(1,"Save Checked Channels",0,24,(nChans%/2)+2);
DlgButton(0,"Save All Channels",0,26.5,(nChans%/2)+3);
DlgButton(5,"*Give up on this concat*",0,30,(nChans%)+5);
for ssc% :=1 to allChans%[0] do
DlgCheck(ssc%,Print$("%d_%s",allChans%[ssc%],ChanTitle$(allChans%[ssc%])),10,1+ssc%);
'CheckSome%(ssc%);
next;
DlgButton(2,"Select all",SelectAllSSC%,29.5,(nChans%/2)-3);
DlgButton(3,"Clear all",ClearAllSSC%,30,(nChans%/2)-1.5);
DlgButton(4,"Defaults",CheckSome%,30,(nChans%/2)-4.5);
DlgReal(allChans%[0]+1,"Concat Sec (4260max):",0,100000,30,(nChans%)+3);
DlgReal(allChans%[0]+2,"Concat aproxMB (2000max):",0,100000,30,(nChans%)+4);
DlgAllow(1,Idle%);
var retret%;
retret%:=DlgShow(chooseWFChans%[0:allChans%[0]],tottime,totsize);
if retret%=5 then
PL("User canceled this concat, rolling back current concat setup");
return -1;
endif
'
if retret%=0 then
PL("Saving all channels.");
View(arrangeHandle%[numCats%]).Print("Saving all channels.");
ArrConst(outChans%,0);
for ssc1%:=0 to allChans%[0] do
outChans%[ssc1%]:=allChans%[ssc1%];
next
else
PrintLog("\nUser selected the following channels to save\n");
View(arrangeHandle%[numCats%]).Print("\nUser selected the following channels to save\n");
for ssc2%:=1 to nChans% do
if chooseWFChans%[ssc2%-1] = 1 then
outChans%[0]+=1;
outChans%[outChans%[0]]:=allChans%[ssc2%];
PrintLog(", %d",outChans%[outChans%[0]]);
View(arrangeHandle%[numCats%]).Print(", %d",outChans%[outChans%[0]]);
endif
next
PrintLog("\n");
endif
'for ssc3%:=1 to outChans%[0] do 'have to do this because chans 30-32 are hardcoded for keyboard, textmark, and digmark
' if outChans%[ssc3%] > 29 then
' docase
' case outChans%[ssc3%] = 30 then
' outChans%[ssc3%] := 33;
' case outChans%[ssc3%] = 31 then
' outChans%[ssc3%] := 34;
' case outChans%[ssc3%] = 32 then
' outChans%[ssc3%] := 35;
' endcase;
' endif;
'next;
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 desuspertick := 40;
var memchanID2%;
var numM1%,numM2%;
memchanID% := MemChan(5);
numM1%:=DownsampleMarkerChan%(digmarkID%,memchanID%,desuspertick);
if numM1% <> 0 then
'Message("Digmark to MemChan1: %d events moved",numM1%);
endif
'
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 numFilesToConcat%[numCats%]-1 do
tottime += fileTimes[numCats%][fnum%];
totsize += 15+totselectedchans%*(fileSizes%[numCats%][fnum%]/(fileChanNums%[numCats%][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;
'
'func AddTODChan%()
''Need to preserve TOD info for chron rig
'var tmChans%[SMALL%],tmChan%,mc%,numTMchans%;
'' numTMchans%:=ChanList(tmChans%[],32);
'' if numTMchans% <> 1 then
'' Message("I found more (or less) than one textmark channel.\nI don't know what to do, exiting script.");
'' halt;
'' endif
''tmChan%:=tmChans%[1];
'tmChan%:=30; 'WARNING: careful about hardcoding channel numbers....
'mc% := MemChan(0,tmChan%);
'if mc%>0 then 'Created OK?
' ChanComment$(mc%, "Copy of channel "+Str$(tmChan%));
' MemImport(mc%, tmChan%, 0, MaxTime()); 'Copy data
' ChanTitle$(mc%,"stim&TOD");
' ChanShow(mc%); 'display new channel
'endif;
'var fTime$,fDate$,tmString$,tmCode%[4];
'fTime$ := FileTime$(1,7,0,"-");
'fDate$:=FileDate$(2,2,3,3,"-");
'tmString$:="date_"+fDate$+"+"+fTime$+"_filetime_0.010000";
'tmCode%[0]:=243;
'tmCode%[1]:=243;
'tmCode%[2]:=243;
'tmCode%[3]:=243;
'MemSetItem(mc%,0,0.1,tmCode%[],tmString$); 'add the file time info to the memory channel
''MemSave(mc%,todChan%,8); 'saves the mem chan as a textmark into channel todChan%
''ChanDelete(mc%);
''ChanShow(todChan%);
''return todChan%;
'return mc%
'end;