I have written a function that gets a function as input (acquired from numpy.poly1d
). The function has access to the GUI variables self.batch
and self.inputFile
, these variables specify if the function is called in batch mode or not and what the current inputFile (for transformation is).
My main concerns are:
- Would you understand the function based on my documentation?
- Is vectorizing only the values to be transformed correct?
- Is the function in general correct (meaning pythonic?)
The function:
def transformFile(self, f):
""" This function gets a single function as input, reads the
raw data file and transforms the read m/z values in the raw data
file with the given function. The transformed m/z values are
stored in a calibrated_<inputfile>.xy file (if program is running
in batchProcess mode) or asks the user for a filepath to save
the calibrated file (if program is running in single spectrum mode).
INPUT: a numpy polynomial (poly1d) function
OUTPUT: a calibrated data file
"""
# Prepare variables required for transformation
outputBatch = []
mzList = []
intList = []
with open (self.inputFile,'r') as fr:
if self.batch == False:
output = tkFileDialog.asksaveasfilename()
if output:
pass
else:
tkMessageBox.showinfo("File Error","No output file selected")
return
else:
parts = os.path.split(str(self.inputFile))
output = "calibrated_"+str(parts[-1])
if self.log == True:
with open('MassyTools.log','a') as fw:
fw.write(str(datetime.now())+"\tWriting output file: "+output+"\n")
for line in fr:
line = line.rstrip('\n')
values = line.split()
mzList.append(float(values[0]))
intList.append(int(values[1]))
# Transform python list into numpy array
mzArray = numpy.array(mzList)
newArray = f(mzArray)
# Prepare the output as a list
for index, i in enumerate(newArray):
outputBatch.append(str(i)+" "+str(intList[index])+"\n")
# Join the output list elements
joinedOutput = ''.join(outputBatch)
with open(output,'w') as fw:
fw.write(joinedOutput)
if self.log == True:
with open('MassyTools.log','a') as fw:
fw.write(str(datetime.now())+"\tFinished writing output file: "+output+"\n")