Automating Tasks in Peak Spectroscopy Software

Peak can be used in workflows that require unattended automation of routine tasks. There are two ways to do this:

  • Batch Processing Sequences
  • Python Scripting

A difference between the Batch Processing Sequence and Script Examples is that a script can run a batch sequence, but a batch sequence cannot run a script. Scripting is much more powerful as it allows possibilties for custom processing, including all of the Python ecosystem. For more information, please see "C:\Program Files (x86)\peakSpectroscopy\peakScripting.html".

Another difference is that Batch Processing Sequences require no programming ability. A Sequence is simply a list of pre-programmed steps to be performed sequentially, in what is sometimes called a 'pipeline'. Sequences are created in a simple editor in Peak.

Command line arguments to peak.exe allow unattended execution of batch sequences or scripts.

Peak Command Line Options

Automation is accomplished using command line options along with a Python script (see peakScripting.html) or a Batch Sequence created within Peak's batch processing tool.

This table summarizes the command line options

Option Arguments Comment
-r "scriptname.py" or "batchSequenceFilename.batch" The named Python script or Peak Batch Sequence to run when Peak is launched. If the script is not located in the Peak 'scripts' document folder, a full path must be provided. Similary, if the batch sequence filename is not located in the Peak 'sequences' documents folder.
-h Hide Peak If present on the command line, and only if a script or batch sequence is specified with -r, Peak is hidden while it runs the script. This is useful when 'exitAfterRun' is specified in the script file (see below) or -x is on the command line, allowing Peak to be launched by other programs to perform some task through the script, and then exit upon completion. If '-h' were not included, the Peak UI will flash up on the screen and then disappear when the task is done.
-x Exit Peak If present on the command line, Peak will exit after executing any script or batch sequence specified with the '-r' option
-o "batchAnalysisReportFilename" with .csv or .json file extension. Works with the -r option. If specified, any results from analyses performed in a batch seuence will be written to the given filename. The extension of the file determines the file format, .csv or .json.
-w "batchSpectrumResultFilename" Works with the -r option. If specified, the result spectrum from any manipulations in the batch sequence will be written to this file. The filename extension determines the file format. The allowable extensions are: .spc (Galactic), .dx (JCamp), .csv (ASCII), .sp (Perkin Elmer), .xls (Excel), .mat (matlab), .0 (Bruker), .npy (Python numpy).

Batch Sequence Example

Batch Sequences can perform both spectral manipulations and analyses.

In this simple example, a spectrum is automatic baseline corrected and a peak pick is performed. The file 'batchExample.sequence' is installed with Peak. The spectral data file installed by Peak is "C:\Users\Public\Documents\data\scripts\polystyrene.spc" This command line will launch Peak, load the file, apply the batch sequence, write the baseline-corrected spectrum to "c:\temp\polystyrene_blc.spc" and write the peak picking report to "c:\temp\polystyrene_peaks.csv"

Note: "c:\temp" may not exist on your system. Change it accordingly.

"c:\Program Files (x86)\peakSpectroscopy\peak.exe" -r "batchExample.batch" -h -x -w "c:\temp\polystyrene_batch_blc.spc" -o "c:\temp\polystyrene_batch_peaks.csv" c:\Users\Public\Documents\peakSpectroscopy\data\scripts\polystyrene.spc"
Note: If batchExample.batch is not located in c:\Users\Public\Documents\peakSpectroscopy\sequences, the full path must be provided.

Python Script Example

The example script named 'batchScript.py' is installed with Peak. It does the same thing as the above 'Batch Sequence Example'.

This command line will launch Peak, load the .spc file, run the script. It writes the baseline-corrected spectrum to "c:/temp/polystyrene_blc.spc" and the peak picking report to "c:/temp/polystyrene_peaks.json"

Note: If batchExample.py file is not located in c:\Users\Public\Documents\peakSpectroscopy\scripts, the full path must be provided.

"c:\Program Files (x86)\peakSpectroscopy\peak.exe" -r "batchExample.py" -h -x -w "c:\temp\polystyrene_script_blc.spc" -o "c:\temp\polystyrene_script_peaks.json" c:\Users\Public\Documents\peakSpectroscopy\data\scripts\polystyrene.spc"

def run(listOfFiles = [], **kwargs):
    try:
        filename = eftir.commandLineFilenames()[0]
    except:
        raise Exception("No filename was supplied on the command line") 
    spectrum = eftir.loadSpectrum(filename)
    pipeline = eftir.buildBatchPipeline('C:/Users/Public/Documents/PeakSpectroscopy/sequences/batchExample.batch')
    result   = eftir.runBatchSequence(spectrum, pipeline)

    if not '-o' in sys.argv:
        # an analysis report filename may not have been specified on the command line
        report   = eftir.generateBatchResultsTable(spectrum, pipeline)
        eftir.exportBatchReport(spectrum, pipeline, 'c:/temp/polystyrene_script.json')
    if not '-w' in sys.argv:
        # a spectrum result  filename may not have been specified on the command line
        eftir.saveFile(result, 'c:/temp/polystyrene_script_blc.spc')

Note: "c:\temp" may not exist on your system. Change it accordingly.