-
Notifications
You must be signed in to change notification settings - Fork 0
/
NTP_Functions.ipf
executable file
·4978 lines (3872 loc) · 129 KB
/
NTP_Functions.ipf
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma TextEncoding = "UTF-8"
#pragma rtGlobals=3 // Use modern global access method and strict wave access.
//Command functions that are built into NeuroTools
Function RunCmd(cmd)
//RUN COMMAND MASTER FUNCTION
String cmd
DFREF NPC = $CW
//Initialize data set info structure
STRUCT ds ds
//Make sure a timer is available
ResetAllTimers()
//Save data folder
DFREF saveDF = $GetDataFolder(1)
//Retrieve the data set information into the main data set Structure, called ds
//The definition of STRUCTURE ds lives in NTP_Structures.ipf
// Wave/T listWave ---listwave being used by the data set (Wave Match, Navigator, or a Data Set)
// Wave/T name ---holds data set names
// Wave/T paths ---string list of the waves in the wsn
// Wave/WAVE output ---holds the wave references for any output waves
// Wave/WAVE waves ---wave of wave references for the wsn
// Wave numWaveSets ---number of wave sets
// int16 wsi ---current wave set index
// int16 wsn ---current wave set number
// Wave numWaves ---number of waves in the current wsn for each data set
// int16 numDataSets ---number of datasets defined
Variable i,j,error = GetDataSetInfo(ds)
If(error == -1)
//reserved, doesn't break bc data sets aren't required necessarily
return 0
EndIf
//Start a timer to time the function execution
Variable ref = StartMSTimer
//Reset the output waveset wave
Redimension/N=0 ds.output
//WSN (Wave Set Number) loop
//Each data set is comprised of potentially multiple wave sets, each referenced by their wave set number
Do
//Get the waves in each wave set using the ds structure
Wave/WAVE ds.waves = GetWaveSetRefs(ds.listWave,ds.wsn,ds.name)
//Resize the text wave that holds the full paths to the waves
Redimension/N=(DimSize(ds.Waves,0),DimSize(ds.Waves,1)) ds.paths
//A function may reference multiple data sets.
//Wave sets and waves must be retrieved from each of them
For(i=0;i<ds.numDataSets;i+=1)
//Retrieve the full paths to the waves in each wave set
String fullPaths = GetWaveSetList(ds.listWave,ds.wsn,1,dsNum=i)
//Remove any potential empty positions that might be at the end of the list wave if the two data sets have different numbers of waves
fullPaths = RemoveEmptyItems(fullPaths,";")
//Put the full paths to the waves into the ds structure as ds.paths
ds.paths[][i] = StringFromList(p,fullPaths,";")
//Number of waves for each data set
ds.numWaves[i] = ItemsInList(fullPaths,";")
EndFor
//Make sure we start at the same point every call to the function in case of 'current data folder' wave references
SetDataFolder saveDF
//Execute the function with the resolved data set structure
Wave/WAVE out = ExecuteCommand(ds,cmd)
//Increment to the next WSN
ds.wsn += 1
//Reset the WSI
ds.wsi = 0
While(ds.wsn < ds.numWaveSets[0]) //This may be a bug for situations where each data sets have different numbers of wave sets.
//If output waves were assigned in the function, make a new data set
If(DimSize(ds.output,0))
//Change the listbox focus to data set before creating the data set output
changeFocus("DataSet",1)
//create output data set
CreateOutputDataSet(ds)
EndIf
//End the timer, print the result
print cmd + ":",StopMSTimer(ref)/(1e6),"s"
//Return to original data folder
SetDataFolder saveDF
//Check data set wave existence in case waves were deleted or renamed during execution
CheckDataSetWaves()
//Update the folders and wave list waves for the GUI
updateFolders()
updateFolderWaves()
//animate a notification that the function ran
String/G NPC:notificationEntry
SVAR notificationEntry = NPC:notificationEntry
notificationEntry = "Function: \f01" + cmd + "()"
SendNotification()
End
//Takes a Command string, executes the corresponding function
Function/WAVE ExecuteCommand(ds,cmd)
STRUCT ds &ds
String cmd
String extCmd = CurrentExtFunc()
If(cmpstr(extCmd,"Write Your Own"))
//Save the data set structure so the external function can retrieve it.
SaveStruct(ds)
EndIf
RunExternalFunction(extCmd)
return $""
strswitch(cmd)
case "Average":
// NT_Average(ds)
Wave/WAVE out = $""
break
case "Errors":
// NT_Error(ds)
Wave/WAVE out = $""
break
case "Measure":
// NT_Measure(ds)
Wave/WAVE out = $""
break
case "Set Wave Note":
// NT_SetWaveNote(ds)
Wave/WAVE out = $""
break
case "PSTH":
// NT_PSTH(ds)
Wave/WAVE out = $""
break
case "Subtract Mean":
// NT_SubtractMean(ds)
Wave/WAVE out = $""
break
case "Subtract Trend":
// NT_SubtractTrend(ds)
Wave/WAVE out = $""
break
case "Duplicate Rename":
// NT_DuplicateRename(ds)
Wave/WAVE out = $""
break
case "delSuffix":
delSuffix(ds)
Wave/WAVE out = $""
break
case "Move To Folder":
// NT_MoveToFolder(ds)
Wave/WAVE out = $""
break
case "Run Cmd Line":
// NT_RunCmdLine()
Wave/WAVE out = $""
break
case "Kill Waves":
// NT_KillWaves(ds)
Wave/WAVE out = $""
break
case "New Data Folder":
// NT_NewDataFolder()
Wave/WAVE out = $""
break
case "Kill Data Folder":
// NT_KillDataFolder()
Wave/WAVE out = $""
break
case "External Function":
extCmd = CurrentExtFunc()
If(cmpstr(extCmd,"Write Your Own"))
//Save the data set structure so the external function can retrieve it.
SaveStruct(ds)
EndIf
RunExternalFunction(extCmd)
break
//ScanImage Packages Functions
case "Max Project":
case "Vector Sum Map":
case "Get ROI":
case "dF Map":
case "Response Quality":
case "Align Images":
SaveStruct(ds)
Execute/Q/Z "RunCmd_ScanImagePackage(\"" + cmd + "\"" + ")"
break
endswitch
return out
End
//Performs various measurements on the data set and puts the result in an output wave
Function NT_Measure(DS_Waves,menu_Type,StartTime,EndTime,BaselineStart,BaselineEnd,Threshold,Width,cb_SubtractBaseline,cb_SaveToParentFolder,menu_SortOutput,OutputName,AngleWave,menu_ReturnType,menu_OSReturnType)
String DS_Waves,menu_Type
Variable StartTime,EndTime,BaselineStart,BaselineEnd,Threshold,Width,cb_SubtractBaseline,cb_SaveToParentFolder
String menu_SortOutput,OutputName,AngleWave,menu_ReturnType,menu_OSReturnType
String menu_Type_List = "Peak;Peak Location;Minimum;Area;Mean;Median;Std. Dev.;Std. Error;# Spikes;Orientation Vector Sum;Vector Sum;"
String menu_Type_Proc = "measureProc" //this identifies a trigger procedure based on the menu selection
String menu_ReturnType_List = "All;Angle;DSI;Resultant;"
String menu_OSReturnType_List = "Angle;OSI;Resultant;"
String menu_SortOutput_List = "Linear;Alternating;"
String Threshold_Assign = "root:Packages:NeuroToolsPlus:ControlWaves:threshold" //assigns the threshold variable to the Viewer Graph threshold bar
String StartTime_Assign = "root:Packages:NeuroToolsPlus:ControlWaves:rangeLeft"
String EndTime_Assign = "root:Packages:NeuroToolsPlus:ControlWaves:rangeRight"
//title designations
String menu_ReturnType_Title = "Return Type"
String menu_OSReturnType_Title = "OS Return Type"
String menu_SortOutput_Title = "Sort Output"
String StartTime_Title = "Start Time (s)"
String EndTime_Title = "End Time (s)"
String BaselineStart_Title = "Baseline Start (s)"
String BaselineEnd_Title = "Baseline End (s)"
String cb_SubtractBaseline_Title = "Subtract Baseline"
String AngleWave_Title = "Angle Wave"
String OutputName_Title = "Output Name"
String cb_SaveToParentFolder_Title = "Save To Parent"
STRUCT ds ds
GetStruct(ds)
// Note={
// Performs a variety of different measurements on the input waves.
// Output is a single wave per waveset.
//
// Most variables are self explanatory except for a few:
// \f01Width\f00 : Takes the average within that size window around the peak.
// \f01SortOutput\f00 : Sorts the output wave from if the data is alternating or linear (unsorted)
// -e.g. alternating directions (0°,180°,45°...) will be sorted 0° to 315°
// \f01AngleWave\f00 : Angles used for the vector sum. This can be an math expression
// to be evaluated at runtime, a path to a wave, or a list of angles.
// }
//Get input parameters
//no waves defined
If(DimSize(ds.waves,0) == 0)
return 0
EndIf
//Wave note
String theNote = ""
strswitch(menu_Type)
case "Peak":
String suffix = "_pk"
theNote = "Peak:\n"
break
case "Peak Location":
suffix = "_pkLoc"
theNote = "Peak Location:\n"
break
case "Minimum":
suffix = "_min"
theNote = "Minimum:\n"
break
case "Area":
suffix = "_area"
theNote = "Area:\n"
break
case "Mean":
suffix = "_mean"
theNote = "Average:\n"
break
case "Median":
suffix = "_med"
theNote = "Median:\n"
break
case "Std. Dev.":
suffix = "_sdev"
theNote = "Std. Deviation:\n"
break
case "Std. Error":
suffix = "_sem"
theNote = "Std. Error:\n"
break
case "# Spikes":
suffix = "_spkct"
theNote = "Spike Count:\n"
break
case "Vector Sum":
theNote = "Vector Sum:\n"
strswitch(menu_ReturnType)
case "Angle":
suffix = "_vAng"
String returnItem = "angle"
break
case "Resultant":
suffix = "_vRes"
returnItem = "resultant"
break
case "DSI":
suffix = "_vDSI"
returnItem = "DSI"
break
case "All":
suffix = "_All"
returnItem = "All"
break
endswitch
break
case "Orientation Vector Sum":
theNote = "OS Vector Sum:\n"
strswitch(menu_OSReturnType)
case "Angle":
suffix = "_vAng"
returnItem = "angle"
break
case "Resultant":
suffix = "_vRes"
returnItem = "resultant"
break
case "OSI":
suffix = "_vOSI"
returnItem = "OSI"
break
endswitch
break
endswitch
//Saves original value of the EndTime in case it needs adjusting to put into valid range
Variable origEndTm = EndTime
//Make the output wave
If(!strlen(OutputName))
OutputName = NameOfWave(ds.waves[0][%Waves]) + suffix
EndIf
If(cb_SaveToParentFolder)
SetDataFolder $ParseFilePath(1,GetWavesDataFolder(ds.waves[0][%Waves],1),":",1,0)
Else
SetDataFolder GetWavesDataFolderDFR(ds.waves[0][%Waves])
EndIf
Make/O/N=(ds.numWaves[0]) $OutputName /Wave = outWave
//Make the measurement
ds.wsi = 0
Do
Wave theWave = ds.waves[ds.wsi]
//Ensure valid point range
If(EndTime == 0 || EndTime < StartTime)
StartTime = 0
EndTime = x2pnt(theWave,DimSize(theWave,0))
EndIf
WaveStats/Q/R=(StartTime,EndTime) theWave
strswitch(menu_Type)
case "Peak": //peak
If(Width > 0)
outWave[ds.wsi] = mean(theWave,V_maxloc - 0.5*Width,V_maxloc + 0.5*Width)
Else
outWave[ds.wsi] = V_max
EndIf
If(cb_subtractBaseline)
If(BaselineEnd == 0)
BaselineStart = pnt2x(theWave,0)
BaselineEnd = pnt2x(theWave,DimSize(theWave,0) - 1)
EndIf
If(BaselineEnd < BaselineStart)
DoAlert 0,"Baseline End must be after Baseline Start"
return 0
EndIf
Variable bgnd = median(theWave,BaselineStart,BaselineEnd)
outWave[ds.wsi] -= bgnd
EndIf
break
case "Peak Location": //peak x location
outWave[ds.wsi] = V_maxLoc
break
case "Minimum": //minimum value (negative peak)
If(Width > 0)
outWave[ds.wsi] = mean(theWave,V_minloc - 0.5*Width,V_minloc + 0.5*Width)
Else
outWave[ds.wsi] = V_min
EndIf
If(cb_subtractBaseline)
If(BaselineEnd == 0)
BaselineStart = pnt2x(theWave,0)
BaselineEnd = pnt2x(theWave,DimSize(theWave,0) - 1)
EndIf
If(BaselineEnd < BaselineStart)
DoAlert 0,"Baseline End must be after Baseline Start"
return 0
EndIf
WaveStats/M=1/Q/R=(BaselineStart,BaselineEnd) theWave
bgnd = median(theWave,BaselineStart,BaselineEnd)
outWave[ds.wsi] -= bgnd
EndIf
break
case "Area": //area
If(cb_subtractBaseline)
If(BaselineEnd == 0)
BaselineStart = pnt2x(theWave,0)
BaselineEnd = pnt2x(theWave,DimSize(theWave,0) - 1)
EndIf
If(BaselineEnd < BaselineStart)
DoAlert 0,"Baseline End must be after Baseline Start"
return 0
EndIf
bgnd = mean(theWave,BaselineStart,BaselineEnd)
Duplicate/FREE theWave,temp
temp -= bgnd
outWave[ds.wsi] = area(temp,StartTime,EndTime)
Else
outWave[ds.wsi] = area(theWave,StartTime,EndTime)
EndIf
break
case "Mean": //mean
outWave[ds.wsi] = V_avg
break
case "Median": //median
outWave[ds.wsi] = median(theWave,StartTime,EndTime)
break
case "Std. Dev.": //sdev
outWave[ds.wsi] = V_sdev
break
case "Std. Error": //sem
outWave[ds.wsi] = V_sem
break
case "# Spikes": //spike count
outWave[ds.wsi] = GetSpikeCount(theWave,StartTime,EndTime,Threshold)
break
case "Vector Sum": //vector sum
String angles = GetVectorSumAngles(ds,AngleWave)
If(!cmpstr(returnItem,"All"))
KillWaves/Z outWave
VectorSum(theWave,angles,returnItem)
Else
outWave[ds.wsi] = VectorSum(theWave,angles,returnItem)
EndIf
menu_SortOutput = "Linear"
break
case "Orientation Vector Sum": //vector sum
angles = GetVectorSumAngles(ds,AngleWave)
outWave[ds.wsi] = OSVectorSum(theWave,angles,returnItem)
menu_SortOutput = "Linear"
break
endswitch
//Add wave name to wave note
theNote += ds.paths[ds.wsi][0] + "\n"
//Reset the end point to its original value for the next wave
EndTime = origEndTm
ds.wsi += 1
While(ds.wsi < ds.numWaves[0])
//Optional sorting
strswitch(menu_SortOutput)
case "Linear":
break
case "Alternating":
Make/O/FREE/N=(DimSize(outWave,0)) sortKey
Variable halfway = floor(DimSize(outWave,0) / 2)
// sortKey = {0,4,1,5,2,6,3,7, etc...}
sortKey[0,*;2] = p/2
sortKey[1,*;2] = halfway + (p-1)/2
Sort sortKey,outWave
break
endswitch
If(WaveExists(outWave))
Note outWave,theNote
EndIf
End
//Averages the waves
//Function/WAVE NT_AverageFilter(DS_Waves,CDF_Filter,outFolder,cb_ReplaceSuffix,cb_isCircular,[free])
String DS_Waves,CDF_Filter //data set reference
String outFolder //output folder location
Variable cb_ReplaceSuffix //replace suffix checkbox
Variable cb_isCircular //is this circular data
Variable free
//loads up the data set info structure
STRUCT ds ds
GetStruct(ds)
//Reset wsi in case this function has been passed to
ds.wsi = 0
//Set the output data folder
DFREF cdf = GetDataFolderDFR()
//If it's a full path folder
free = (ParamIsDefault(free)) ? 0 : 1
Wave filter = ds.waves[0][1]
If(!free)
If(stringmatch(outFolder,"root*"))
Variable i = 0
String sub = ""
Do
sub += ParseFilePath(0,outFolder,":",0,i) + ":"
If(!DataFolderExists(sub))
NewDataFolder $RemoveEnding(sub,":")
EndIf
i += 1
While(i < ItemsInList(outFolder,":"))
SetDataFolder outFolder
Else
If(strlen(outFolder))
If(!DataFolderExists(GetWavesDataFolder(ds.waves[0],1) + outFolder))
NewDataFolder $(GetWavesDataFolder(ds.waves[0],1) + outFolder)
EndIf
EndIf
SetDataFolder GetWavesDataFolder(ds.waves[0],1) + outFolder
EndIf
EndIf
//Make output wave for each wave set
If(cb_ReplaceSuffix)
String outputName = ReplaceSuffix(NameOfWave(ds.waves[0]),"avg")
Else
outputName = NameOfWave(ds.waves[0]) + "_avg"
EndIf
//What is the wave type?
Variable type = WaveType(ds.waves[0])
//Real wave
switch(type)
case 72: //unsigned 8 bit integer
Make/O/N=(DimSize(ds.waves[0],0),DimSize(ds.waves[0],1),DimSize(ds.waves[0],2))/B/U $outputName
break
case 2: //single float 32 bit
Make/O/N=(DimSize(ds.waves[0],0),DimSize(ds.waves[0],1),DimSize(ds.waves[0],2))/S $outputName
break
case 8: //signed 8 bit integer
Make/O/N=(DimSize(ds.waves[0],0),DimSize(ds.waves[0],1),DimSize(ds.waves[0],2))/B $outputName
break
case 16: //unsigned 16 bit word
Make/O/N=(DimSize(ds.waves[0],0),DimSize(ds.waves[0],1),DimSize(ds.waves[0],2))/W $outputName
endswitch
Wave outWave = $outputName
//Reset outWave in case of overwrite
outWave = 0
//Set the scale of the output wave
SetScale/P x,DimOffset(ds.waves[0],0),DimDelta(ds.waves[0],0),outWave
SetScale/P y,DimOffset(ds.waves[0],1),DimDelta(ds.waves[0],1),outWave
SetScale/P z,DimOffset(ds.waves[0],2),DimDelta(ds.waves[0],2),outWave
// WSI loop
//Do the average calculation
String noteStr = "Average: " + num2str(ds.numWaves[0]) + " Waves\r"
If(cb_isCircular)
//Circular data
Wave theWave = ds.waves[0]
Make/FREE/N=(DimSize(theWave,0)) xTotal,yTotal
xTotal = 0
yTotal = 0
Do
If(filter[ds.wsi] == 0)
ds.wsi += 1
If(ds.wsi > ds.numWaves[0] - 1)
break
EndIf
continue
EndIf
Wave theWave = ds.waves[ds.wsi][0]
If(WaveMax(theWave) > 2*pi)
//probably degrees
MatrixOP/O/FREE xComp = cos(theWave * pi/180)
MatrixOP/O/FREE yComp = sin(theWave * pi/180)
xTotal += xComp
yTotal += yComp
Else
//probably radians
MatrixOP/O/FREE xComp = cos(theWave)
MatrixOP/O/FREE yComp = sin(theWave)
xTotal += xComp
yTotal += yComp
EndIf
noteStr += ds.paths[ds.wsi][0] + "\r"
ds.wsi += 1
While(ds.wsi < ds.numWaves[0])
MatrixOP/O/FREE vMean = atan2(yTotal,xTotal)
vMean = vMean * 180/pi
//atan2 outputs data from -pi to +pi. We want it from 0 to +2pi.
vMean = (vMean < 0) ? vMean + 360 : vMean
outWave = vMean
Else
//Linear data
Do
If(filter[ds.wsi] == 0)
ds.wsi += 1
If(ds.wsi > ds.numWaves[0] - 1)
break
EndIf
continue
EndIf
Wave theWave = ds.waves[ds.wsi][0]
Multithread outWave += theWave
noteStr += ds.paths[ds.wsi][0] + "\r"
ds.wsi += 1
While(ds.wsi < ds.numWaves[0])
If(WaveType(outWave) > 7) //any type of integer
Redimension/S outWave
EndIf
Multithread outWave /= ds.numWaves[0]
EndIf
//Set the wave note
Note/K outWave,noteStr
//Reset data folder
SetDataFolder cdf
//pass the wave to the calling function
Duplicate/FREE outWave,freeWave
If(free)
KillWaves/Z outWave
return freeWave
Else
return outWave
EndIf
End
Function NT_MedianWaves(DS_Waves)
//TITLE=Median Waves
String DS_Waves
// Note={
// Calculates the median for the input waves. Same as averaging
// waves together, but uses the median.
// }
STRUCT ds ds
GetStruct(ds)
ds.wsi = 0
DFREF cdf = GetDataFolderDFR()
SetDataFolder GetWavesDataFolder(ds.waves[0],1)
//What is the wave type?
Variable type = WaveType(ds.waves[0])
//Make the output wave of the same type
String outputName = NameOfWave(ds.waves[0]) + "_med"
Make/O/N=(DimSize(ds.waves[0],0),DimSize(ds.waves[0],1),DimSize(ds.waves[0],2))/Y=(type) $outputName
Wave outWave = $outputName
//Set the scale of the output wave
String xDim = WaveUnits(ds.waves[0],0)
String yDim = WaveUnits(ds.waves[0],1)
String zDim = WaveUnits(ds.waves[0],2)
SetScale/P x,DimOffset(ds.waves[0],0),DimDelta(ds.waves[0],0),xDim,outWave
SetScale/P y,DimOffset(ds.waves[0],1),DimDelta(ds.waves[0],1),yDim,outWave
SetScale/P z,DimOffset(ds.waves[0],2),DimDelta(ds.waves[0],2),zDim,outWave
String noteStr = "Median: " + num2str(ds.numWaves[0]) + " Waves\r"
Make/FREE/N=(DimSize(ds.waves[0],0),ds.numWaves[0]) master
Do
Wave theWave = ds.waves[ds.wsi]
Multithread master[][ds.wsi] = theWave[p][0]
ds.wsi += 1
While(ds.wsi < ds.numWaves[0])
ds.wsi = 0
Variable i
For(i=0;i<DimSize(master,0);i+=1)
MatrixOP/FREE theRow = row(master,i)^t
outWave[i] = median(theRow)
EndFor
SetDataFolder cdf
End
//Averages the waves
Function/WAVE NT_Average(DS_Waves,outFolder,cb_ReplaceSuffix,cb_isCircular,[free])
//TITLE=Average
String DS_Waves //data set reference
String outFolder //output folder location
Variable cb_ReplaceSuffix //replace suffix checkbox
Variable cb_isCircular //is this circular data
Variable free
//title designation for the control
String outFolder_Title = "Output Folder"
String cb_ReplaceSuffix_Title = "Replace Suffix"
String cb_isCircular_Title = "Circular Data?"
// Note={
// Averages the waves in each wave set
//
// \f01outFolder\f00 : Folder to put the averaged wave.
// \f01ReplaceSuffix\f00 : End of the wave name is replaced with '_avg'. Otherwise '_avg' is added to the end of the wave name.
// \f01isCircular\f00 : Check if the data is angular.
// }
//loads up the data set info structure
STRUCT ds ds
GetStruct(ds)
//Reset wsi in case this function has been passed to
ds.wsi = 0
//Set the output data folder
DFREF cdf = GetDataFolderDFR()
//If it's a full path folder
free = (ParamIsDefault(free)) ? 0 : 1
If(!free)
If(stringmatch(outFolder,"root*"))
Variable i = 0
String sub = ""
Do
sub += ParseFilePath(0,outFolder,":",0,i) + ":"
If(!DataFolderExists(sub))
NewDataFolder $RemoveEnding(sub,":")
EndIf
i += 1
While(i < ItemsInList(outFolder,":"))
SetDataFolder outFolder
Else
If(strlen(outFolder))
If(!DataFolderExists(GetWavesDataFolder(ds.waves[0],1) + outFolder))
NewDataFolder $(GetWavesDataFolder(ds.waves[0],1) + outFolder)
EndIf
EndIf
SetDataFolder GetWavesDataFolder(ds.waves[0],1) + outFolder
EndIf
EndIf
//Make output wave for each wave set
If(cb_ReplaceSuffix)
String outputName = ReplaceSuffix(NameOfWave(ds.waves[0]),"avg")
Else
outputName = NameOfWave(ds.waves[0]) + "_avg"
EndIf
//What is the wave type?
Variable type = WaveType(ds.waves[0])
//Make the output wave of the same type
If(!free)
Make/O/N=(DimSize(ds.waves[0],0),DimSize(ds.waves[0],1),DimSize(ds.waves[0],2))/Y=(type) $outputName
Wave outWave = $outputName
Else
Make/O/N=(DimSize(ds.waves[0],0),DimSize(ds.waves[0],1),DimSize(ds.waves[0],2))/Y=(type)/FREE outWave
EndIf
//Add outwave to the output data set
// If(!free)
// AddOutput(outWave,ds)
// EndIf
//How many dimensions in the wave
Variable nDims = WaveDims(ds.waves[0])
//Reset outWave in case of overwrite
If(nDims > 2)
Multithread outWave = 0
Else
outWave = 0
EndIf
//Set the scale of the output wave
String xDim = WaveUnits(ds.waves[0],0)
String yDim = WaveUnits(ds.waves[0],1)
String zDim = WaveUnits(ds.waves[0],2)
SetScale/P x,DimOffset(ds.waves[0],0),DimDelta(ds.waves[0],0),xDim,outWave
SetScale/P y,DimOffset(ds.waves[0],1),DimDelta(ds.waves[0],1),yDim,outWave
SetScale/P z,DimOffset(ds.waves[0],2),DimDelta(ds.waves[0],2),zDim,outWave
// WSI loop
//Do the average calculation
String noteStr = "Average: " + num2str(ds.numWaves[0]) + " Waves\r"
If(cb_isCircular)
//Circular data
Wave theWave = ds.waves[0]
Make/FREE/N=(DimSize(theWave,0)) xTotal,yTotal
xTotal = 0
yTotal = 0
Do
Wave theWave = ds.waves[ds.wsi]
If(WaveMax(theWave) > 2*pi)
//probably degrees
MatrixOP/O/FREE xComp = cos(theWave * pi/180)
MatrixOP/O/FREE yComp = sin(theWave * pi/180)
xTotal += xComp
yTotal += yComp
Else
//probably radians
MatrixOP/O/FREE xComp = cos(theWave)
MatrixOP/O/FREE yComp = sin(theWave)
xTotal += xComp
yTotal += yComp
EndIf
noteStr += ds.paths[ds.wsi][0] + "\r"
// updateProgress(ds)
ds.wsi += 1
While(ds.wsi < ds.numWaves[0])
MatrixOP/O/FREE vMean = atan2(yTotal,xTotal)
vMean = vMean * 180/pi
//atan2 outputs data from -pi to +pi. We want it from 0 to +2pi.
vMean = (vMean < 0) ? vMean + 360 : vMean
outWave = vMean
Else
//Linear data
//accounts for any nans in the data
Duplicate/FREE ds.waves[0],pnts
If(nDims > 2)
Multithread pnts = 0
Else
pnts = 0
EndIf
Do
Wave theWave = ds.waves[ds.wsi]
//keeps track of nans so division is done properly later on
If(DimSize(theWave,1) == 0)
Duplicate/FREE theWave,temp
pnts += 1
pnts = (numtype(temp[p]) == 2) ? pnts - 1 : pnts
//replace nans with 0
temp = (numtype(temp[p]) == 2) ? 0 : temp[p]
Multithread outWave += temp
Else
Duplicate/FREE theWave,temp
If(nDims > 2)
Multithread pnts += 1
Multithread pnts = (numtype(temp[p][q][r]) == 2) ? pnts - 1 : pnts
//replace nans with 0
Multithread temp = (numtype(temp[p][q][r]) == 2) ? 0 : temp[p][q][r]
Else
pnts += 1
pnts = (numtype(temp[p][q]) == 2) ? pnts - 1 : pnts
//replace nans with 0
temp = (numtype(temp[p][q]) == 2) ? 0 : temp[p][q]
EndIf
Multithread outWave += temp
EndIf
noteStr += ds.paths[ds.wsi][0] + "\r"
// updateProgress(ds)
ds.wsi += 1
While(ds.wsi < ds.numWaves[0])
//Make a float briefly to handle the averaging
If(WaveType(outWave) > 7) //any type of integer
Redimension/S outWave
EndIf
// Multithread outWave /= ds.numWaves[0]
Multithread outWave /= pnts
EndIf
// Redimension/Y=(type) outWave
//
//Set the wave note
Note/K outWave,noteStr
//Reset data folder
SetDataFolder cdf
//pass the wave to the calling function
Duplicate/FREE outWave,freeWave
If(free)
// KillWaves/Z outWave
return freeWave
Else
return outWave
EndIf
End
//Gets the error of the waves - SEM or SDEV
Function/WAVE NT_Error(DS_Waves,menu_errorType,outFolder,cb_ReplaceSuffix,cb_isCircular)
String DS_Waves,menu_errorType,outFolder
Variable cb_ReplaceSuffix,cb_isCircular
String menu_errorType_List = "sem;sdev;"
//title designation for the control
String menu_errorType_Title = "Type"
String outFolder_Title = "Output Folder"
String cb_ReplaceSuffix_Title = "Replace Suffix"
String cb_isCircular_Title = "Circular Data?"
// Note={
// Measures the error (sem or sdev) of the waves in each wave set
//