Fityk (curve fitting software) file integration

This is a small script for saving an loading files for use with Fityk (https://fityk.nieto.pl/), which is a small but powerful fitting software. 1D waves are saved to Fityk's native '.fit' file format, which is basically just a text file containing a script. These files then can be directly opened in Fityk. Fit results can be exported as many file formats. Currently '.dat' for fit data and '.peaks' for individual peaks are supported, which are loaded together as set. The loading part might not be well suited to your workflow (e.g., exporting data in Fityk via scripts etc.). Suggestions for improvements are welcome. You can use the 'Fityk' menu at the top bar for saving 1D waves selected in the Data Browser or use the following functions for saving and loading:

ExportToFityk(inwave)   // save fit files
or
LoadDataFromFityk()   // load dat and peaks files

 

Menu "Fityk"
	"Export to Fityk [.fit] ...",/Q, CreateBrowser; ExportToFityk_wrapper()
	"Load from Fityk [.dat] ...",/Q, LoadDataFromFityk()
End

Function ExportToFityk_wrapper()
	Variable index=0
	do
		String name = GetBrowserSelection(index)
		if (strlen(name) <= 0)
			break
		endif
		Wave/Z work = $name
		if (WaveExists(work))
			ExportToFityk(work)
		endif
		index+=1
	while(1)
	return 0
End

//#################################

Function ExportToFityk(in)
	wave in
	if (WaveDims(in) > 1)
		Abort "This is meant for 1D waves!"
	endif
	
	Variable refNum
	String fileFilters = "Data Files (*.fit):.fit;All Files:.*;"
	Open/F=fileFilters/M="Save a fit file" refNum
	String outputPath = S_fileName
	if(strlen(outputPath) == 0)
		return -1	//canceled
	endif

	Variable i, size = numpnts(in)
	String Header = "", Data = ""
	Header += "set verbosity = -1\r\n"
	Header += "set autoplot = 0\r\n"
	Header += "reset\r\n\r\n"
	Header += "use @0\r\n"
	Header += "title = '"+ NameOfWave(in) + "'\r\n"
	Header += "M="+num2str(size)+"\r\n"
	
	if (DimDelta(in, 0) < 0)	// inverse waves
		Header += "X="+num2str(pnt2x(in,0))+"\r\n"
		FPrintF refnum, Header
		for (i = 0; i < size; i += 1)
			sprintf Data, "X[%d]=%f, Y[%d]=%.15f, S[%d]=1, A[%d]=1\r\n", i, pnt2x(in,size-1-i), i, numtype(in[size-1-i]) == 0 ? in[size-1-i] : 0, i, i
			FPrintF refnum, Data
		endfor
	else
		Header += "X="+num2str(pnt2x(in, (DimDelta(in, 0) < 1)*(size-1) ))+"\r\n"
		FPrintF refnum, Header
		for (i = 0; i < size; i += 1)
			sprintf Data, "X[%d]=%f, Y[%d]=%.15f, S[%d]=1, A[%d]=1\r\n", i, pnt2x(in,i), i, numtype(in[i]) == 0 ? in[i] : 0, i, i
			FPrintF refnum, Data
		endfor
	endif
	
	Header = "\r\nplot\r\n"
	Header += "use @0\r\n"
	Header += "set autoplot = 1\r\n"
	Header += "set verbosity = 0" 
	FPrintF refnum, Header
	close refnum
	return 0
End

//#################################

Function LoadDataFromFityk()
	Variable refNum
	String FileRef = "Fityk Files (*.dat):.dat;All Files:.*;"
	Open/D/R/MULT=1/F=FileRef/M="Select files to load" refNum
	if (strlen(S_fileName) == 0)
		return -1	//canceled
	endif
	
	Variable fileindex = 0, Datanum = 0
	String filePath = "", filename = "", fileList = S_FileName, Waves = ""
	DFREF saveDFR = GetDataFolderDFR()
	For (fileIndex = 0; fileIndex < ItemsInList(fileList,"\r"); fileIndex +=1)
		filePath = StringFromList (fileIndex, fileList,"\r")
		filename = ParseFilePath(3, filePath, ":", 0, 0)
		Print "Loading data from file '" + filename + "'."
		NewDataFolder/O/S $filename
			LoadWave/G/O/N=peak_/Q filePath
			Waves = S_waveNames
			LoadPeaksFromFityk(filePath)
			
			Wave xaxis = $StringFromList(0, Waves)
			For (Datanum = 1; Datanum < itemsinlist(Waves); Datanum += 1)
				SetScale/I x, xaxis[0], xaxis[numpnts(xaxis)-1], "", $StringFromList(Datanum, Waves)
			Endfor
			Wave total = $StringFromList(itemsinlist(Waves)-1, Waves)
			Duplicate/O total, $"summed"
			Killwaves xaxis, total
		SetDataFolder saveDFR
	Endfor
End

//#################################

Static Function LoadPeaksFromFityk(filePath)
	String filePath
	filePath = ReplaceString(".dat",filePath,".peaks")
	GetFileFolderInfo/Q/Z filePath
	if( V_Flag != 0)
		Print "Peak parameter file for this set not found."
		return -1
	endif
	LoadWave/J/M/K=2/O/U={1, 0, 1, 0}/V={"\t, ", " ", 0, 0}/N=parameters_/Q filePath
	Wave/T para = parameters_0	// get rid of empty row at end
	if(strlen(para[DimSize(para,0)-1]) == 0)
		DeletePoints/M=0 DimSize(para,0)-1, 1, para
	endif
	return 0
End

 

Fitky Integration_v1.ipf (3.83 KB)

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More