libinxulab / xulab_software

software repository for lab use
1 stars 3 forks source link

add plotting functions to `dhrmasslynxapi` for chromatograms and mass spectra #7

Open dylanhross opened 2 years ago

dylanhross commented 2 years ago

It would be nice to be able to produce some standard plots for extracted chromatograms and mass spectra. The functions could go in a new module dhrmasslynxapi/plotting.py. They should be configurable so that the plot can be viewed interactively (via pyplot.show()) and/or saved to a file (via pyplot.savefig(...)).


The basic signature for plotting a chromatogram should be something like:

plot_chromatogram(t, i, xlab, show=True, savefig=None, figsize=(4, 1))

Parameters t : array-like array of drift times or retention times i : array-like array of intensities xlab : str x axis label (e.g. "retention time (min)") show : bool, default=True whether to view the plot interactively using pyplot.show() savefig : None or str, default=None if a filename is provided, save the image to file, if None do not save the image figsize : tuple(float, float), default=(4, 1) figure width, height in inches


The basic signature for plotting a mass spectrum should be something like:

plot_spectrum(mz, i, mz_min=None, mz_max=None, show=True, savefig=None, figsize=(4, 1))

Parameters mz : array-like array of m/z values i : array-like array of intensities mz_min : None or float, default=None minimum m/z to display, None to use the minimum value from mz array mz_max : None or float, default=None maximum m/z to display, None to use the maximum value from mz array show : bool, default=True whether to view the plot interactively using pyplot.show() savefig : None or str, default=None if a filename is provided, save the image to file, if None do not save the image figsize : tuple(float, float), default=(4, 1) figure width, height in inches


A note on implementation: For greater extensibility, it would be good to start with some "private" functions that generate the basic plots then return a matplotlib.Axes instance which can either be used by the "public" functions to just show/save the plot OR more traces/annotations/etc. can be added using that matplotlib.Axes instance. If the basic functions are implemented first with this in mind, such extended features could easily be added down the line. The "private" functions could also house some of the more granular control over things like plotting color/line width or style. The following example shows how this might be implemented for the basic plotting:

from matplotlib import pyplot as plt

def _plot_chromatogram(t, i, figsize, ...):
    """ this is the "private" function"""
    fig = plt.figure(figsize=figsize)
    ax = fig.add_subplot(111)
    # do the basic plotting and configuration ...
    return ax

def plot_chromatogram(t, i, xlab, show=True, savefig=None, figsize=(4, 1)):
    """ this is the "public" function """
    # use the "private" function to do the actual plotting, then do any further customization that is needed
    ax = _plot_chromatogram(t, i, figsize, ...)
    ax.set_xlabel(xlab)
    if show:
        plt.show()
    if savefig is not None:
        plt.savefig(savefig, dpi=350, bbox_inches='tight')
    plt.close()