swh:1:snp:c50c8da9941e1ff7b642631e081af81fc0c3fc53
Raw File
Tip revision: ff88f1a29e80d1daf16dfceb7b99f894e063ff13 authored by Jonathon Penix on 18 May 2018, 00:29:10 UTC
deleting now uneeded file for popup wave thingy
Tip revision: ff88f1a
JP_Cluster.ipf
#pragma rtGlobals=3		// Use modern global access method and strict wave access.

macro JP_Cluster ()
	buildClusterPanelLUL()
endmacro

function buildClusterPanelLUL()
	string panel_waves_df = "root:CIW"
	if (DataFolderExists(panel_waves_df))
		KillDataFolder $panel_waves_df
	endif
	NewDataFolder $panel_waves_df

	// general variable/dimenson setting
	string panelName = "cluster"

	variable /G xPos = 20
	variable /G yPos = 40
	variable buttonWidth = 100, buttonHeight = 20
	variable listBoxWidth = 150, listBoxHeight = 150

	// creates the panel and sets color
	NewPanel /K=1/W=(50, 50, 1125, 750)/N=$panelName
	modifypanel cbRGB=(50000,50000,50000)

	// makes the display area
	Variable left = 250, top = 35, right = 1045, bottom = 514
	Display /W=(left, top, right, bottom) /HOST=cluster
	RenameWindow #, clusterDisplay
	SetActiveSubwindow ##

	// makes the two tables
	variable /G first_cluster_run2 = 1
	variable /G num_COP_params2 = 0
	string /G cluster_results_tn2 = "COP_results2"
	string /G COP_results_wn2 = panel_waves_df + ":COP_results_wave2"

	edit /N=$cluster_results_tn2 /HIDE=(0) /K=1 /HOST=cluster /W=(250, 520, 1045, 600)
	ModifyTable /W=cluster#COP_results2 showParts=122
	SetActiveSubWindow cluster#clusterDisplay

	// table to track settings cluster was run with
	string /G cluster_settings_tn2 = "COP_settings2"
	string /G COP_settings_wn2 = panel_waves_df + ":COP_settings_wave2"
	variable /G num_COP_settings2 = 0
	edit /N=$cluster_settings_tn2 /HIDE=(0) /K=1 /HOST=cluster /W=(250, 600, 1045, 680)
	ModifyTable /W=cluster#COP_settings2 showParts=122
	SetActiveSubWindow cluster#clusterDisplay

	// button for pulling settings from table
	Button useTableSettings2 pos={xPos + buttonWidth + 5, yPos + 600}, size={buttonWidth, buttonHeight}, title="Use tab sett", proc=useTableSettingsProc2
	
	variable /G cluster_ran2 = 0
	// controls for cluster
	variable /G updnEnabled2 = 0
	variable /G mscoreEnabled2 = 0
	variable /G updnPresent2 = 0
	variable /G mscorePresent2 = 0
	variable/G zeroterminate2 = 0

//	20170219 added listbox to allow choosing a wave for cluster
	string /G man_wave_name2
	SetVariable PopupWaveSelectorSV3,pos={xPos,yPos + 15},size={200,15},title="Select a wave:"
	MakeSetVarIntoWSPopupButton("cluster", "PopupWaveSelectorSV3", "testNotificationFunction2", "root:man_wave_name2")
	
	CheckBox mscoreEnabledBox pos={xpos, yPos + 35}, proc=mscoreEnabledBoxProc2, title="Show MScore", disable=(0)
	CheckBox updnEnabledBox pos={xpos + buttonwidth, yPos + 35}, proc=updnEnabledBoxProc2, title="Show Up/Down", disable=(0)
	checkbox cbZeroTerminate pos={xpos, yPos + 55}, proc=cbZeroTerminateProc, title="Zero terminate", disable=(0), variable = ZeroTerminate2
	variable /G autoscale_mscore2 = 0
	CheckBox autoscale_mscoreBox pos={xpos + buttonWidth, yPos + 55}, proc=autoscale_mscoreBoxProc2, title="Autoscale MScore", variable = autoscale_mscore2
	
	variable /G g_npntsUP2 = 2
	variable /G g_npntsDN2 = 2
	variable /G g_TscoreUP2 = 2.0
	variable /G g_TscoreDN2 = 2.0
	variable /G g_minPeak2 = 0.0
	variable /G g_halflife2 = 0.0
	variable /G g_outlierTscore2 = 4.0
	variable /G g_minNadir2 = -1
	
	SetVariable numPointsPeak pos={xPos, yPos + 75}, size={200,20}, value=g_npntsUP2, title="# Points for Peak", disable=(0), limits={1,inf,1}
	SetVariable numPointsNadir pos={xPos, yPos + 95}, size={200,20}, value=g_npntsDN2, title="# Points for Nadir", disable=(0), limits={1,inf,1}
	SetVariable tscoreIncrease pos={xPos, yPos + 115}, size={200,20}, value=g_TscoreUP2, title="T-Score for Increase", disable=(0), limits={0,inf,0.1}
	SetVariable tscoreDecrease pos={xPos, yPos + 135}, size={200,20}, value=g_TscoreDN2, title="T-Score for Decrease", disable=(0), limits={0,inf,0.1}
	SetVariable minPeakSize pos={xPos, yPos + 155}, size={200,20}, value=g_minPeak2, title="Minimum Peak Size", disable=(0), limits={0,inf,0.1}
	SetVariable minNadir pos={xPos, yPos+175}, size={200,20}, value=g_minNadir2, title="Minimum Nadir", disable=(0), limits={-1, inf, .1}
	SetVariable halfLife pos={xPos, yPos + 195}, size={200,20}, value=g_HalfLife2, title="Half-Life", disable=(0), limits={0,inf,0.1}
	SetVariable outlierTscore pos={xPos, yPos + 215}, size={200,20}, value=g_outlierTscore2, title="Outlier T-Score", disable=(0), limits={0,inf,0.1}

	string udata = ""
	Button saveParamDef pos={xPos, yPos + 235}, size={buttonWidth + 90, 20}, title="Save Parameters as Default", disable=(0), proc=cluster_buStoreParams, userdata=udata
	udata += "target:"
	udata += "cluster#clusterDisplay;"
	udata += "hw:;"
	Button calculate pos={xPos, yPos + 255}, size={buttonWidth, 20}, title="Calculate", disable=(0),proc=jcluster_buCalculate2, userdata=udata
	udata = ""
	Button viewResults pos={xPos, yPos +275}, size={buttonWidth - 5,20}, title="View Results", disable=(0),proc = cluster_buViewResults, userdata=udata
	Button printResults pos={xPos + buttonWidth + 5, yPos + 275}, size={buttonWidth - 8,20}, title= "Print Results", disable=(0), proc = cluster_buPrintResults, userdata=udata

	valdisplay nPeaks pos = {xPos, yPos + 300}, size={100, 20}, title="n Peaks", disable=(0)
	valdisplay nNadirs, pos={xPos, yPos + 317}, size={100,20}, title="n Nadirs", disable=(0)

	Button storeResults, pos={xPos,yPos + 335}, size={buttonWidth, buttonHeight}, title="Store Results", disable=(0), proc=cluster_buStoreResults, userdata=udata
	Button recallResults, pos={xPos + buttonWidth + 5,yPos + 335}, size={buttonWidth - 8, buttonHeight}, title="Recall Results", disable=(0), proc=cluster_buRecallResults, userdata=udata

	variable /G gRadioVal3 = 1, gZero = 0.01, gFixedValue2 = 0.1
	CheckBox cbGlobalSD, pos={xPos, yPos + 360}, size={78,15}, title="Global: SD", value=1, mode=1, disable=(0),proc=clustercheckProc
	CheckBox cbGlobalSE, pos={xPos + 105, yPos + 360}, size={78,15}, title="SE", value=0,mode=1, disable=(0),proc=clustercheckProc
	CheckBox cbLocalSD, pos={xPos, yPos + 380}, size={78,15}, title="Local: SD", value=0, mode=1, disable=(0),proc=clustercheckProc
	CheckBox cbLocalSE, pos={xPos + 105, yPos + 380}, size={78,15}, title="SE", value=0, mode=1, disable=(0),proc=clustercheckProc
	CheckBox cbSQRT, pos={xPos, yPos + 400}, size={78,15}, title="SQRT", value=0, mode=1, disable=(0),proc=clustercheckProc
	SetVariable svZero, pos={xPos + 105, yPos + 400}, size={90,20},title="Zero:", value=gZero, limits={0,inf,0.1}, disable=(0)
	CheckBox cbFixed,pos={xPos, yPos + 420},size={78,15},title="Fixed:",value=0,mode=1, disable=(0),proc=clustercheckProc
	SetVariable svFixedValue, pos={xPos + 105, yPos + 420}, size={90,20}, title="Value:", value=gFixedValue2,limits={0,inf,0.1}, disable=(0)
	CheckBox cbErrWave,pos={xPos, yPos + 440}, size={78,15},title="Wave:", value=0, mode=1, disable=(0),proc=clustercheckProc

	String quote = "\""
	string errwl=""
	errwl = quote + "NONE;" + wavelist("*",";", "") + quote // list of all current waves in local igor data folder
	errwl =  quote + "NONE;" + quote
	PopupMenu puErrWaveName pos={xPos, yPos + 460}, size={100, 20}, title="Wave:", mode=2, disable=(0),userdata=udata
	PopupMenu puErrWaveName value = #errwl
	udata = ""
	udata = "puErrWaveName"
	Button buUpdateErrWLPU, pos={xPos + buttonWidth + 5,yPos + 460},size={buttonWidth - 10, buttonHeight}, title="Update Wave", disable=(0),proc=cluster_buUpdateWL, userdata=udata

	// MISC
	udata = "target:cluster#clusterDisplay;"
	Button resetZoom pos={xPos, yPos + 600}, size={buttonWidth, buttonHeight}, title="Reset zoom", proc=resetZoomProc2, userdata = udata

	// placing this here to make sure is defined for man wave
	string /G histoAxis = "histo"
	string /G upsAxis = "lower1"
	string /G downAxis = "lower2"
end

// function that handles the calc button/actually does the cluster analysis stuff
function jcluster_buCalculate2(s) : ButtonControl
	Struct WMButtonAction &s

	// 20170111 added /z to prevent debugger // and moved into event code if statement (no ref needed if no click)
	if (s.eventcode == 2)
		NVAR cluster_ran2, autoscale_mscore2
		cluster_ran2 = 1

		NVAR/Z gRadioVal3 = root:gRadioVal3
		NVAR/Z gZero = root:gZero
		NVAR/Z gFixedValue2 = root:gFixedValue2
		NVAR/Z gZeroTerminate2 = root:ZeroTerminate2
		SVAR man_wave_name2 = root:man_wave_name2
		SVAR cluster_wavepath
		NVAR binsize, updnEnabled2, mscoreEnabled2, updnPresent2, mscorePresent2
		
		string mscorewn = "", wn_ups="", wn_dns="",	 thisAxis = "", thiswn = ""

		//removes old graphs
		string target = stringbykey("target", s.userdata)
		string cluster_waves
		variable item = 0
		variable nitems
		string rwn

		setactivesubwindow $target
		string cluster_waves_df = "root:CW"
		if (DataFolderExists(cluster_waves_df))
			SetDataFolder $cluster_waves_df
			cluster_waves = WaveList("Mscore*", ";", "")
			cluster_waves += WaveList("ups*", ";", "")
			cluster_waves += WaveList("downs*", ";", "")
			nitems = itemsinlist(cluster_waves)
			if (nitems > 0)
				do
					rwn = stringfromlist(item, cluster_waves)
					WAVE /Z rw = $rwn
					removefromgraph /Z $rwn
					item += 1
				while (item < nitems)
			endif
		endif
		NewDataFolder /O /S $cluster_waves_df
		
		// make copy of the histogram for use
		string wn = man_wave_name2
		WAVE /Z test = $cluster_wavepath
		Duplicate /O test root:CW:$wn
		
		variable nPeaks, nNadir, tScoreUp, tScoreDN, minPeak, minNadir, halfLife, outScore 
		string temp_errwn = "", errwn = ""
		
		controlinfo numPointsPeak
		nPeaks = v_value
		controlinfo numPointsNadir
		nNadir = v_value
		controlinfo tscoreIncrease
		tScoreUp = v_value
		controlinfo tscoreDecrease
		tScoreDn = v_value
		controlinfo minPeakSize
		minPeak = v_value
		controlinfo minNadir
		minNadir = v_value
		controlinfo puErrWaveName
    	temp_errwn = S_Value // 20180425 update to make access to user selection of error wavename
		controlinfo svZero
		variable zero = V_value

		outScore = 4

		string wn_results = ""

		// evaluate error handling; need errorType and errorValue
		string errorType = ""
		variable errorValue = 0
		switch(gRadioVal3)
			case 1:
				//Global SD
				errorType = "Global SD"
				break
			case 2:
				//Global SE
				errorType = "Global SE"
				break
			case 3:
				//local SD
				errorType = "Local SD"
				break
			case 4:
				//local se
				errorType = "Local SE"
				break
			case 5:
				//sqrt
				errorType = "SQRT"
				//errorValue = gSQRTOValue2 20180427 not correct!
				break
			case 6:
				//Global SD
				errorType = "Fixed"
				errorValue = gFixedValue2
				break
			case 7:
				// user provided error wave // \\ // not implemented yet
				errorType = "Error Wave"
//				print "in JP cluster: need code to move selected wave to error wave!"
				//print "buCluster_calculate: error wavename:", errwn
				errwn = temp_errwn

				//abort
				break
			default:
				print "switch ClusterMain, unaccounted for errortype code: ", gRadioVal3
				errortype = ""
				errorvalue = 1e-6
		endswitch

		//20170111 if error value is lost, warn the user!
		if( numtype( errorvalue ) != 0 )
			print "jp cluster handler: errorvalue = nan: ", errorvalue
			errorvalue = 1e-6
			print " jp cluster handler: reset to:", errorvalue
		endif

		string outlist = ""

		//print "error wave:", errwn

		//scroll over to see errwn handling with optional param
//		outlist = envClusterMain(wn, nPeaks, nNadir, tScoreUp, tScoreDn, minPeak, halfLife, outScore, errorType, errorValue, zero = zero, zeroTerminate = gZeroTerminate2, errwn = errwn )
		outlist = ClusterMain(wn, nPeaks, nNadir, tScoreUp, tScoreDn, minPeak, halfLife, outScore, errorType, errorValue, zero, gZeroTerminate2, errwn, minNadir )



		wn_results = stringfromlist( 0, outlist )

//print "insert cop here", ts_cop( wn_results, wn )

		wn_ups = stringfromlist( 1, outlist )
		wn_dns = stringfromlist( 2, outlist )

		
		string clusterOutput2 = ts_COP(wn_results, wn, deltat=binsize)

		WAVE /Z w_results = $wn_results
		WAVE /Z w_ups = $wn_ups
		WAVE /Z w_dns = $wn_dns
		
		target = stringbykey("target", s.userdata)
		setactivesubwindow $target

		string oldtraces=tracenamelist("",";",1)

		// graphs the stuff
		if(strsearch( oldtraces, wn_results, 0) < 0)
			AppendToGraph/R w_results //20170110 this labels the pulses from cluster analysis
			string pulseAxis = "Right"
			ModifyGraph mode($wn_results)=5,rgb($wn_results)=(65535,65535,0)
			ModifyGraph hbFill($wn_results)=2
		// ** 20170109 SET THIS TO HALF THE BINSIZE
			ModifyGraph offset($wn_results)={0,0} // using bar graphs, no realignment necessary
			ModifyGraph axRGB($pulseAxis)=(65535,65535,65535),tlblRGB($pulseAxis)=(65535,65535,65535);DelayUpdate
			ModifyGraph alblRGB($pulseAxis)=(0,65535,0)
			ModifyGraph axisEnab($pulseAxis) = {0, 1}
		endif
		string upAxis = "lower1"
		if(strsearch( oldtraces, wn_ups, 1) < 0)
			if (updnEnabled2)
				AppendToGraph/R=$upAxis w_ups
				modifygraph rgb($wn_ups)=(0,65535,0), mode($wn_ups)=5, hbfill($wn_ups)=2
				Label $upAxis "\\K(0,0,0) <UP"
				ModifyGraph axRGB($upAxis)=(65535,65535,65535),tlblRGB($upAxis)=(65535,65535,65535)
				ModifyGraph alblRGB($upAxis)=(65535,65535,65535)
				ModifyGraph freePos($upAxis)=20
				updnPresent2 = 1
			endif
		endif
		string dnAxis = "lower2"
		if(strsearch( oldtraces, wn_dns, 1) < 0)
			if (updnEnabled2)
				AppendToGraph/R=$dnAxis w_dns
				modifygraph rgb($wn_dns)=(65535,0,0), mode($wn_dns)=5, hbfill($wn_dns)=2
				Label $dnAxis "\\K(0,0,0) DN>"
				ModifyGraph axRGB($dnAxis)=(65535,65535,65535),tlblRGB($dnAxis)=(65535,65535,65535)
				ModifyGraph alblRGB($dnAxis)=(65535,65535,65535)
				ModifyGraph freePos($dnAxis)=20
			endif
		endif
		mscorewn = "Mscore_ups_" + wn
		thiswn = ""
		thisAxis = ""
		thisAxis = "Mscore"
		if(strsearch( oldtraces, mscorewn, 1) < 0)
			thiswn = mscorewn
			WAVE /Z thisW = $thiswn
			if (mscoreEnabled2)
				AppendToGraph/R=$thisAxis thisw  // thisw contains the reference to Mscore
				if (!autoscale_mscore2)
					setaxis $thisaxis, -tscoredn, tscoreup
				else
					SetAxis /A $thisAxis
				endif
				modifygraph rgb($thiswn)=(0,0,65535), mode($thiswn)=5, hbfill($thiswn)=2
				ModifyGraph zero($thisAxis)=1
				ModifyGraph freePos($thisAxis)=0
				Label $thisAxis "Mscore"
				ModifyGraph lblPos($thisAxis)=80
				Label $thisAxis "\\K(0,0,0) Mscore"
				ModifyGraph freePos($thisAxis)=0
				mscorePresent2 = 1
			endif
		endif

		oldtraces = tracenamelist("",";",1)
		string firsttrace = stringfromlist(0,oldtraces)
		if(!stringmatch( firsttrace, wn_results))
			reordertraces $firsttrace, {$wn_results}
		endif
		
		SetDataFolder root:

		update_cluster_tables2(clusterOutput2)

		if (mscoreEnabled2)
			string /G mscoreAxis = thisAxis
		endif
		if (updnEnabled2)
			string /G downAxis = dnAxis
			string /G upsAxis = upAxis
		endif
		variable /G modifyHisto2 = 1
		cluster_resizeWindows()
		modifyHisto2 = 0	
		
		cluster_resizeWindows()
	endif
end

function cbZeroTerminateProc( s ) :  CheckBoxControl
STRUCT WMCheckBoxAction &s

end

function mscoreEnabledBoxProc2 (ctrlName, checked) : CheckBoxControl
	string ctrlName
	variable checked

	NVAR mscoreEnabled2
	mscoreEnabled2 = checked
end

function updnEnabledBoxProc2 (ctrlName, checked) : CheckBoxControl
	string ctrlName
	variable checked

	NVAR updnEnabled2
	updnEnabled2 = checked
end

// check proc for radio buttons in cluster
function clustercheckProc(name, value)
	String name
	Variable value

	NVAR gRadioVal3 = root:gRadioVal3

	strswitch (name)
		case "cbGlobalSD":
			gRadioVal3= 1
			break
		case "cbGlobalSE":
			gRadioVal3= 2
			break
		case "cbLocalSD":
			gRadioVal3= 3
			break
		case "cbLocalSE":
			gRadioVal3= 4
			break
		case "cbSQRT":
			gRadioVal3= 5
			break
		case "cbFixed":
			gRadioVal3= 6
			break
		case "cbErrWave":
			gRadioVal3= 7
			break
	endswitch
	CheckBox cbGlobalSD,value= gRadioVal3==1
	CheckBox cbGlobalSE,value= gRadioVal3==2
	CheckBox cbLocalSD,value= gRadioVal3==3
	CheckBox cbLocalSE,value= gRadioVal3==4
	CheckBox cbSQRT,value= gRadioVal3==5
	CheckBox cbFixed,value= gRadioVal3==6
	CheckBox cbErrWave,value= gRadioVal3==7
end

Function testNotificationFunction2(event, wavepath, windowName, ctrlName)
	Variable event
	String wavepath
	String windowName
	String ctrlName

	// doing this to prevent errors when selecting man wave after already having done one
	NVAR mscoreEnabled2, mscorePresent2, updnEnabled2, updnPresent2
	variable prev_mscoreEnabled = mscoreEnabled2
	variable prev_updnEnabled = updnEnabled2
	variable prev_mscorePresent = mscorePresent2
	variable prev_updnPresent = updnPresent2
	mscoreEnabled2 = 0
	mscorePresent2 = 0
	updnEnabled2 = 0
	updnPresent2 = 0

	print "Selected wave:",wavepath, " using control", ctrlName

	// empty graph
	string target = "cluster#clusterDisplay"
	string wl = "", rwn2 = ""
	wl = tracenamelist(target,  ";" , 1 )
	variable item2=0, nitems2=itemsinlist(wl)

	if (nitems2>0)
		do
			rwn2 = stringfromlist( item2, wl )
			WAVE /Z rw2 = $rwn2
			removefromgraph /W=$target $rwn2 
			item2+=1
		while(item2<nitems2)
	endif

	// graph the wave given by user
	variable /G modifyHisto2 = 1
	SVAR histoAxis
	
	string /G cluster_wavepath = wavepath
	WAVE /Z wn_wave = $wavepath

	// parse out the path stuff from the wavepath (root:, etc.)
	variable c_index = strsearch(wavepath, ":", Inf, 1)
	string wn
	if (c_index != -1)	
		wn = wavepath[c_index + 1, Inf]
	endif
	// remove start and end quote if there are any
	wn = replacestring("'", wn, "")
		
	appendtograph /W=$target /L=$histoAxis wn_wave
	ModifyGraph /W=$target freePos($histoAxis)=0
	ModifyGraph /W=$target lsize($wn) = 1
	ModifyGraph /W=$target rgb($wn) = (0, 0, 0)
	ModifyGraph /W=$target mode($wn)=5,hbFill($wn)=2
	modifyHisto2 = 0
	string histoLabel2 = ""
	histoLabel2 += "Events per "
	histoLabel2 += num2str(deltax(wn_wave))
	histoLabel2 += " s"
	Label /W=$target histo histoLabel2
	ModifyGraph /W=$target lblPosMode(histo)=2

	mscoreEnabled2 = prev_mscoreEnabled
	mscorePresent2 = prev_mscorePresent
	updnEnabled2 = prev_updnEnabled
	updnPresent2 = prev_updnPresent
end

function cluster_resizeWindows()
	variable upBeginVSV = .06
	variable upEndVSV = .1
	variable dnBeginVSV = 0
	variable dnEndVSV = .04
	variable mscoreBeginVSV = .8
	variable mscoreEndVSV = 1
	variable binBeginVSV = 0
	variable binEndVSV = 1
	NVAR modifyHisto2, mscoreEnabled2, mscorePresent2, updnEnabled2, updnPresent2
	SVAR mscoreAxis, upsAxis, downAxis, histoAxis
	string target = "cluster#clusterDisplay"

	if (mscoreEnabled2 && mscorePresent2)
		ModifyGraph /W=$target axisEnab($mscoreAxis) = {mscoreBeginVSV, mscoreEndVSV}
		binEndVSV = .75
	endif
	if (updnEnabled2 && updnPresent2)
		ModifyGraph /W=$target axisEnab($upsAxis) = {upBeginVSV, upEndVSV}
		ModifyGraph /W=$target axisEnab($downAxis) = {dnBeginVSV, dnEndVSV}
		binBeginVSV = .15
	endif
	
	if (modifyHisto2)
		ModifyGraph /W=$target axisEnab($histoAxis) = {binBeginVSV, binEndVSV}
	endif
end

function useTableSettingsProc2(B_Struct) : ButtonControl
	STRUCT WMButtonAction &B_Struct
	if (B_Struct.eventcode == 2)
		// decide which row user wants to take for settings
		string info = TableInfo("cluster#COP_settings2", -2)
		string selection_info = StringByKey("SELECTION", info)
		variable fRow, fCol, lRow, lCol, tRow, tCol
		sscanf selection_info, "%d,%d,%d,%d,%d,%d", fRow, fCol, lRow, lCol, tRow, tCol

		// make update settings from wave
		SVAR COP_settings_wn2
		WAVE /Z /T settings_wave = $COP_settings_wn2
		assign_cluster_settings2(settings_wave, tRow)
	endif
end

function init_cluster_results_table2(string cluster_out)
	NVAR num_COP_params2
	num_COP_params2 = ItemsInList(cluster_out)

	// set up table wave
	SVAR COP_results_wn2
	make /O /T /N=(1, num_COP_params2) $COP_results_wn2
	AppendToTable /W=cluster#COP_results2 $COP_results_wn2

	// label columns with each of the keys in cluster out, label list wave for output
	variable i
	string /G COP_output_params2 = ""
	for (i = 0; i < num_COP_params2; i += 1) 
		string param = ithkey(i, cluster_out)
		ModifyTable /W=cluster#COP_results2 title[i + 1] = param
		COP_output_params2 += (param + ";")
	endfor
end

function init_cluster_settings_table2()
	string ts_nw = "root:CIW:temp_setting_nw"
	make /T /O /N=0 $ts_nw = {"Zero terminate", "Points for Peak", "Points for Nadir", "T-Score for Increase", "T-Score for Decrease", "Minimum Peak Size", "Minimum Nadir", "Half-Life", "Outlier T-Score", "Error Type", "Options"}
	WAVE /Z /T ts_nww = $ts_nw
	NVAR num_COP_settings2
	num_COP_settings2 = numpnts(ts_nww)

	// set up settings wave
	SVAR COP_settings_wn2
	make /O /T /N=(1, num_COP_settings2) $COP_settings_wn2
	AppendToTable /W=cluster#COP_settings2 $COP_settings_wn2

	// label columns with each of the keys
	variable i
	for (i = 0; i < num_COP_settings2; i += 1)
		ModifyTable /W=cluster#COP_settings2 title[i + 1] = ts_nww[i]
	endfor
end

// appends new rows to cluster tables to reflect results/settings of just run instance of cluster
function update_cluster_tables2(string cluster_out)
	// set up results table if first time cluster has been run
	NVAR first_cluster_run2
	if (first_cluster_run2)
		init_cluster_results_table2(cluster_out)
		init_cluster_settings_table2()
	endif

	// build wave from cluster results
	NVAR num_COP_params2
	string result_wn = "root:CIW:result"
	make /T /O /N=(num_COP_params2) $result_wn
	WAVE /Z /T result_w = $result_wn
	variable i
	for (i = 0; i < num_COP_params2; i += 1)
		result_w[i] = ithkeyed_str(i, cluster_out)
	endfor

	// pplaceholder
	// redimension table wave and append result
	SVAR COP_results_wn2
	WAVE /Z /T COP_results_w2 = $COP_results_wn2
	variable num_dims = dimsize(COP_results_w2, 0)
	if (!first_cluster_run2)
		Redimension /N=(num_dims + 1, num_COP_params2) COP_results_w2
		COP_results_w2[num_dims][] = result_w[q]
	else
		COP_results_w2[num_dims - 1][] = result_w[q]
	endif

	// build wave from settings
	NVAR num_COP_settings2
	string ts_n = "root:CIW:temp_settings"
	make /T /O /N=(num_COP_settings2) $ts_n
	WAVE /Z /T ts_w = $ts_n
	update_cluster_settings_val2(ts_w)

	// redimension settings wave and append result
	SVAR COP_settings_wn2
	WAVE /Z /T COP_settings_w = $COP_settings_wn2
	num_dims = dimsize(COP_settings_w, 0)
	if (!first_cluster_run2)
		Redimension /N=(num_dims + 1, num_COP_settings2) COP_settings_w
		COP_settings_w[num_dims][] = ts_w[q]
	else
		COP_settings_w[num_dims - 1][] = ts_w[q]
	endif

	if (first_cluster_run2)
		first_cluster_run2 = 0
	endif
end

// sets the cluster settings equal the settings found in settings wave
function assign_cluster_settings2(settings_wave, row_to_use)
	WAVE /Z /T settings_wave
	variable row_to_use

	// zero terminate
	NVAR ZeroTerminate2
	ZeroTerminate2 = str2num(settings_wave[row_to_use][0])
	// points for Peak
	NVAR g_npntsUP2
	g_npntsUP2 = str2num(settings_wave[row_to_use][1])
	// points for nadir
	NVAR g_npntsDN2
	g_npntsDN2 = str2num(settings_wave[row_to_use][2])
	// tscore for increase
	NVAR g_TscoreUP2
	g_TscoreUP2 = str2num(settings_wave[row_to_use][3])
	// tscore for Decrease
	NVAR g_TscoreDN2
	g_TscoreDN2 = str2num(settings_wave[row_to_use][4])
	// minimum peak size
	NVAR g_minPeak2
	g_minPeak2 = str2num(settings_wave[row_to_use][5])
	// minimum nadir
	NVAR g_minNadir2
	g_minNadir2 = str2num(settings_wave[row_to_use][6])
	// half life
	NVAR g_halflife2
	g_HalfLife2 = str2num(settings_wave[row_to_use][7])
	// outlier t score
	NVAR g_outlierTscore2
	g_outlierTscore2 = str2num(settings_wave[row_to_use][8])
	// error type
	NVAR gRadioVal3
	gRadioVal3 = str2num(settings_wave[row_to_use][9])

	// update radio buttons
	CheckBox cbGlobalSD,value= gRadioVal3==1
	CheckBox cbGlobalSE,value= gRadioVal3==2
	CheckBox cbLocalSD,value= gRadioVal3==3
	CheckBox cbLocalSE,value= gRadioVal3==4
	CheckBox cbSQRT,value= gRadioVal3==5
	CheckBox cbFixed,value= gRadioVal3==6
	CheckBox cbErrWave,value= gRadioVal3==7
	
	// if sqrt, fixed, or wave error types, update attached parameters
	switch (gRadioVal3)
		case 5:
			NVAR gSQRTOvalue2
			gSQRTOvalue2 = str2num(settings_wave[row_to_use][10])
			break
		case 6:
			NVAR gFixedValue2
			gFixedValue2 = str2num(settings_wave[row_to_use][10])
			break
		case 7:
			PopupMenu puErrWaveName value = #settings_wave[row_to_use][10]
			break
	endswitch
end

// records the settings used for cluster in settings_wave
function update_cluster_settings_val2(settings_wave)
	WAVE /Z /T settings_wave
	// zero terminate
	NVAR ZeroTerminate2
	settings_wave[0] = num2str(ZeroTerminate2)
	// points for Peak
	NVAR g_npntsUP2
	settings_wave[1] = num2str(g_npntsUP2)
	// points for nadir
	NVAR g_npntsDN2
	settings_wave[2] = num2str(g_npntsDN2)
	// tscore for increase
	NVAR g_TscoreUP2
	settings_wave[3] = num2str(g_TscoreUP2)
	// tscore for Decrease
	NVAR g_TscoreDN2
	settings_wave[4] = num2str(g_TscoreDN2)
	// minimum peak size
	NVAR g_minPeak2
	settings_wave[5] = num2str(g_minPeak2)
	// minimum nadir
	NVAR g_minNadir2
	settings_wave[6] = num2str(g_minNadir2)
	// half life
	NVAR g_HalfLife2
	settings_wave[7] = num2str(g_HalfLife2)
	// outlier t score
	NVAR g_outlierTscore2
	settings_wave[8] = num2str(g_outlierTscore2)
	// error type
	NVAR gRadioVal3
	settings_wave[9] = num2str(gRadioVal3)
		
	switch (gRadioVal3)
		case 5:
			NVAR gSQRTOvalue2
			settings_wave[10] = num2str(gSQRTOvalue2)
			break
		case 6:
			NVAR gFixedValue2
			settings_wave[10] = num2str(gFixedValue2)
			break
		case 7:
			ControlInfo puErrWaveName
			settings_wave[10] = s_value
			break
		default:
			settings_wave[10] = ""
	endswitch
end

function autoscale_mscoreBoxProc2(s) : CheckBoxControl
	STRUCT WMCheckBoxAction &s
	NVAR cluster_ran2, mscoreEnabled2
	SVAR mscoreAxis

	if (cluster_ran2 && mscoreEnabled2)
		NVAR autoscale_mscore2
		SVAR mscoreAxis
		if (autoscale_mscore2)
			SetAxis /W=cluster#clusterDisplay /A $mscoreAxis
		else
			NVAR g_TscoreUP2, g_TscoreDN2
			SetAxis /W=cluster#clusterDisplay $mscoreAxis, -g_TscoreDN2, g_TscoreUP2
		endif
	endif
end

function resetZoomProc2(B_Struct) : ButtonControl
	STRUCT WMButtonAction &B_Struct

	if (B_Struct.eventcode == 2)
		SVAR histoAxis
		string target = stringbykey("target", B_Struct.userdata)
		setactivesubwindow $target

		SetAxis /A
	endif
end
back to top