I would like to summarize the input parameters for a python calculation using text strings. I can summarize the input parameters using print() but I don't know how to also save these as text strings.
Sample Code
First, define the scan of input parameters:
import numpy as np
gapsize = np.arange(0.5,0.8,0.1)
ringwidth = np.arange(1.25,5,0.75)
outputangle = [28.43]
Now define a function that prints a parameter-specific summary of these values
def printSCANvalues(parameter):
## Do cases for text descriptions
if parameter is ringwidth:
paramTEXT = "RW"
elif parameter is gapsize:
paramTEXT = "Gap"
elif parameter is outputangle:
paramTEXT = "Facet Angle"
if parameter is ringwidth or parameter is gapsize:
unit = 'um'
elif parameter is outputangle:
unit = 'deg.'
## only calculate the gradient for arrays with more than one value
if len(parameter) > 1:
step = round(1000*np.gradient(parameter)[0])/1000
elif len(parameter) == 1:
step = 0
stopVAL = round(1000*parameter[-1])/1000
print("")
print(paramTEXT, "Scan: qty =", len(parameter),", start = ", parameter[0],
unit, ", stop = ", stopVAL, unit, ", step = ", step, unit)
print(paramTEXT, "values: ", parameter, unit)
Now use function to generate the text that summarizes the input parameter scan:
print("")
print("====== Input Parameter Scan Values ======")
print("")
printSCANvalues(ringwidth)
printSCANvalues(gapsize)
printSCANvalues(outputangle)
Screenshot of output:

The problem
I would also like to save this output as a text string so that it can be written to a text file. Is there a way to save the associated string for each parameter type before print() is executed so that I can use it later?
CodePudding user response:
As a very general rule, in a situation like this, you shouldn't have the function do the printing. Have the function return a string, and let the caller decide what to DO with the string. Like this. You can see how to take the results and copy them to a file later.
def SCANvalues(parameter):
## Do cases for text descriptions
if parameter is ringwidth:
paramTEXT = "RW"
elif parameter is gapsize:
paramTEXT = "Gap"
elif parameter is outputangle:
paramTEXT = "Facet Angle"
if parameter is ringwidth or parameter is gapsize:
unit = 'um'
elif parameter is outputangle:
unit = 'deg.'
## only calculate the gradient for arrays with more than one value
step = 0
if len(parameter) > 1:
step = round(1000*np.gradient(parameter)[0])/1000
stopVAL = round(1000*parameter[-1])/1000
s = f"{paramTEXT} Scan: qty = {len(parameter)}, start = {parameter[0]}, stop = {stopVal} {unit}, step = {step} {unit}\n"
s = f"{paramTEXT} Values: {parameter} {unit}"
return s
print("")
print("====== Input Parameter Scan Values ======")
print("")
rw = SCANvalues(ringwidth)
gs = SCANvalues(gapsize)
oa = SCANvalues(outputangle)
print(rw)
print(gs)
print(oa)
CodePudding user response:
I think I figured it out using the tip from @martineau and the modification from @Tim Roberts.
def SCANvalues(parameter):
## Do cases for text descriptions
if parameter is ringwidth:
paramTEXT = "RW"
elif parameter is gapsize:
paramTEXT = "Gap"
elif parameter is outputangle:
paramTEXT = "Facet Angle"
if parameter is ringwidth or parameter is gapsize:
unit = 'um'
elif parameter is outputangle:
unit = 'deg.'
## only calculate the gradient for arrays with more than one value
step = 0
if len(parameter) > 1:
step = round(1000*np.gradient(parameter)[0])/1000
stopVAL = round(1000*parameter[-1])/1000
s = f"{paramTEXT} Scan: qty = {len(parameter)}, start = {parameter[0]}, stop = {stopVAL} {unit}, step = {step} {unit}\n"
s = f"{paramTEXT} Values: {parameter} {unit}\n"
return s
Code for printing to a file
I didn't see if there was an option to print to a file as well as the console so I just included two instances
log = open('log.txt', 'w')
geos = [ringwidth, gapsize, outputangle]
def geoPRINT(geoPARAMS):
print("\n ====== Input Parameter Scan Values ======\n")
print("\n ====== Input Parameter Scan Values ======\n",file = log)
for i in range(len(geoPARAMS)):
print(SCANvalues(geoPARAMS[i]))
print(SCANvalues(geoPARAMS[i]),file = log)
geoPRINT(geos)
log.close()
