Set of custom python packages for various odds and ends.
- mcol defines a set of colours as ANSI escape strings.
- module provides the module() function that calls lmod to persistently load softare on EUREKA.
- mout provides a set of functions for formatted console output.
- mplot provides a variety of plotting functions using matplotlib.
Provides ANSI escape strings for colourised terminal output that fits into a colour scheme. I.e. green success messages, red errors, orange warnings, yellow filenames etc...
For example to output an error message:
import mcol
print(mcol.error + "Error: Something went wrong!" + mcol.clear)
Please note that for most cases the functions in mout will achieve the same functionality with less code:
import mout
mout.errorOut("Something went wrong!")
Colour strings in the scheme:
varNameVariable names, turqoisevarTypeVariable types/units, pinkfuncFunction/script name, underlined turqoisefileFilename/path, yellowerrorFor error messages, bold redsuccessFor success messages, bold greenwarningFor warning messages, bold greenresultFor results/variable values, blueargFor function arguments, lime
General colour strings:
-
clear -
bold -
inverse -
underline -
red -
green -
yellow
Access the unix modulecmd from within python. Load and unload packages, which will persist so that subsequest exec() or os.system() commands will see them. Comma delimited arguments instead of spaces:
import module
module.module('load','lammps/2018/mpi+intel-xe_2017_3')
Write formatted output to the console.
General console message
mout.out(string,printScript=False,end="\n")
stringThe message to be displayedprintScriptPrepend the output with the name of the script?endEnd the output with this string.
Output a variable and its value
def varOut(name, value, unit="",valCol="",precision=8,printScript=False,end="\n")
nameThe name of the variablevalueValue of the variableunitUnit string for the variablevalColCustom colour string for the valueprecisionSignificant figures of the value outputprintScriptPrepend the output with the name of the script?endEnd the output with this string.
Warning message
def warningOut(string,printScript=False,code=None,end="\n")
stringThe message to be displayedprintScriptPrepend the output with the name of the script?codeThe warning codeendEnd the output with this string.
Error message
def errorOut(string,printScript=False,fatal=False,code=None,end="\n"):
stringThe message to be displayedprintScriptPrepend the output with the name of the script?fatalExit the script?codeThe warning codeendEnd the output with this string.
Functions for plotting data using matplotlib.
mplot requires matplotlib
Plotting 2D data:
mplot.graph2D(xdata,ydata,ytitles=None,filename=None,show=True,xmin=None,xmax=None,ymin=None,ymax=None,xlab='x',ylab='y',title=None)
xdataList of x-axis valuesydataList of y-axis values or list of list of y-axis values.ytitlesList of titles for the different y-axis data-sets.filenameOutput filename.showOpen a GUI window with the plot?xminLower bound of x-axis.xmaxUpper bound of x-axis.yminLower bound of y-axis.ymaxUpper bound of y-axis.xlabx-axis label.xlaby-axis label.titleTitle of plot.verbosityVerbosity level. Every nested function call will passverbosity=verbosity-1.
Example:
import mplot
xdata = [1,2,3,4]
ydata1 = [1,2,3,4]
ydata2 = [3,4,5,6]
mplot.graph2D(xdata,ydata1,filename="test1.png",title="Test 1")
mplot.graph2D(xdata,[ydata1,ydata2],ytitles=["ydata1","ydata2"],filename="test2.png",title="Test 2")
Open figures in GUI windows
To show all the figures created in the interactive session:
mplot.show(verbosity=1)