Author: tlinnet Date: Thu Nov 27 17:57:00 2014 New Revision: 26790 URL: http://svn.gna.org/viewcvs/relax?rev=26790&view=rev Log: Added systemtest Nmrglue.xtest_plot_contour to test the functionality of contour plotting. Task #7873 (https://gna.org/task/index.php?7873): Write wrapper function to nmrglue, to read .ft2 files and process them. Homepage: http://www.nmrglue.com/ Link to nmrglue discussion: https://groups.google.com/forum/#!forum/nmrglue-discuss The code is develop at Github: https://github.com/jjhelmus/nmrglue/ Google code: https://code.google.com/p/nmrglue/ Documentation: http://nmrglue.readthedocs.org/en/latest/index.html Modified: branches/nmrglue/lib/software/nmrglue.py branches/nmrglue/pipe_control/nmrglue.py branches/nmrglue/test_suite/system_tests/nmrglue.py Modified: branches/nmrglue/lib/software/nmrglue.py URL: http://svn.gna.org/viewcvs/relax/branches/nmrglue/lib/software/nmrglue.py?rev=26790&r1=26789&r2=26790&view=diff ============================================================================== --- branches/nmrglue/lib/software/nmrglue.py (original) +++ branches/nmrglue/lib/software/nmrglue.py Thu Nov 27 17:57:00 2014 @@ -23,13 +23,90 @@ """Module for the wrapper functions around the nmrglue module.""" # Python module imports. -from re import search, split +from numpy import arange +import matplotlib.pyplot as plt +import matplotlib.cm # relax module imports. from extern import nmrglue from lib.errors import RelaxError from lib.io import get_file_path from lib.spectrum.objects import Nmrglue_data + + +def contour_plot(spectrum_id=None, contour_start=30000., contour_num=20, contour_factor=1.20, ppm=True, show=False): + """Plot the spectrum as contour plot. + + @keyword spectrum_id: The spectrum identification string. + @type spectrum_id: str or list of str + @keyword contour_start: Contour level start value + @type contour_start: float + @keyword contour_num: Number of contour levels + @type contour_num: int + @keyword contour_factor: Scaling factor between contour levels + @type contour_factor: float + @keyword ppm: A flag which if True will make the plot in ppm scale. Else it is in points. + @type ppm: bool + @keyword show: A flag which if True will make a call to matplotlib.pyplot.show(). + @type show: bool + @return: The matplotlib.axes.AxesSubplot class, which can be manipulated to add additional text to the axis. + @rtype: matplotlib.axes.AxesSubplot + """ + + # Extract the data. + dic = cdp.ngdata[spectrum_id].dic + udic = cdp.ngdata[spectrum_id].udic + data = cdp.ngdata[spectrum_id].data + + # Setup plot parameters + # contour map (colors to use for contours) + cmap = matplotlib.cm.Blues_r + + # Calculate contour levels + cl = contour_start * contour_factor ** arange(contour_num) + + # Create the figure + fig = plt.figure() + ax = fig.add_subplot(111) + + # Plot the contours + + # Plot in ppm scale + if ppm: + # make ppm scales + uc_dim1 = nmrglue.pipe.make_uc(dic, data, dim=1) + ppm_dim1 = uc_dim1.ppm_scale() + ppm_dim1_0, ppm_dim1_1 = uc_dim1.ppm_limits() + uc_dim0 = nmrglue.pipe.make_uc(dic, data, dim=0) + ppm_dim0 = uc_dim0.ppm_scale() + ppm_dim0_0, ppm_dim0_1 = uc_dim0.ppm_limits() + + ax.contour(data, cl, cmap=cmap, extent=(ppm_dim1_0, ppm_dim1_1, ppm_dim0_0, ppm_dim0_1)) + + # Decorate + ax.set_ylabel("%s (ppm)"%udic[0]['label']) + ax.set_xlabel("%s (ppm)"%udic[1]['label']) + ax.set_title("Spectrum") + lim_dim1 = [ppm_dim1_0, ppm_dim1_1] + lim_dim0 = [ppm_dim0_0, ppm_dim0_1] + ax.set_xlim(max(lim_dim1), min(lim_dim1)) + ax.set_ylim(max(lim_dim0), min(lim_dim0)) + + else: + # Plot in points. + ax.contour(data, cl, cmap=cmap, extent=(0, data.shape[1] - 1, 0, data.shape[0] - 1)) + + # Decorate + ax.set_ylabel("%s (points)"%udic[0]['label']) + ax.set_xlabel("%s (points)"%udic[1]['label']) + ax.set_title("Spectrum") + + # If show. + if show: + plt.show() + + # Return ax + return ax def read_spectrum(file=None, dir=None): Modified: branches/nmrglue/pipe_control/nmrglue.py URL: http://svn.gna.org/viewcvs/relax/branches/nmrglue/pipe_control/nmrglue.py?rev=26790&r1=26789&r2=26790&view=diff ============================================================================== --- branches/nmrglue/pipe_control/nmrglue.py (original) +++ branches/nmrglue/pipe_control/nmrglue.py Thu Nov 27 17:57:00 2014 @@ -24,7 +24,7 @@ # relax module imports. from lib.errors import RelaxError -from lib.software.nmrglue import read_spectrum +from lib.software.nmrglue import contour_plot, read_spectrum from pipe_control.pipes import check_pipe from pipe_control.spectrum import add_spectrum_id, check_spectrum_id, delete @@ -72,3 +72,29 @@ # Store the data. add_nmrglue_data(spectrum_id=spectrum_id, nmrglue_data=nmrglue_data) + + +def plot_contour(spectrum_id=None, contour_start=30000., contour_num=20, contour_factor=1.20, ppm=True, show=False): + """Plot the spectrum as contour plot. + + @keyword spectrum_id: The spectrum identification string. + @type spectrum_id: str or list of str + @keyword contour_start: Contour level start value + @type contour_start: float + @keyword contour_num: Number of contour levels + @type contour_num: int + @keyword contour_factor: Scaling factor between contour levels + @type contour_factor: float + @keyword ppm: A flag which if True will make the plot in ppm scale. Else it is in points. + @type ppm: bool + @keyword show: A flag which if True will make a call to matplotlib.pyplot.show(). + @type show: bool + @return: The matplotlib.axes.AxesSubplot class, which can be manipulated to add additional text to the axis. + @rtype: matplotlib.axes.AxesSubplot + """ + + # Call the contour plot. + ax = contour_plot(spectrum_id=spectrum_id, contour_start=contour_start, contour_num=contour_num, contour_factor=contour_factor, ppm=ppm, show=show) + + return ax + Modified: branches/nmrglue/test_suite/system_tests/nmrglue.py URL: http://svn.gna.org/viewcvs/relax/branches/nmrglue/test_suite/system_tests/nmrglue.py?rev=26790&r1=26789&r2=26790&view=diff ============================================================================== --- branches/nmrglue/test_suite/system_tests/nmrglue.py (original) +++ branches/nmrglue/test_suite/system_tests/nmrglue.py Thu Nov 27 17:57:00 2014 @@ -26,6 +26,7 @@ # relax module imports. from data_store import Relax_data_store; ds = Relax_data_store() +from pipe_control.nmrglue import plot_contour from status import Status; status = Status() from test_suite.system_tests.base_classes import SystemTestCase from extern import nmrglue @@ -82,3 +83,27 @@ # Assert the version to be 0.4. self.assertEqual(ng_vers, '0.4') + + + def xtest_plot_contour(self): + """Test the plot_contour function in pipe_control. + This is from the U{tutorial<http://jjhelmus.github.io/nmrglue/current/examples/plot_2d_spectrum.html>}.""" + + # Read the spectrum. + fname = 'freq_real.ft2' + sp_id = 'test' + self.interpreter.spectrum.nmrglue_read(file=fname, dir=ds.ng_test, spectrum_id=sp_id) + + # Call the pipe_control function and get the return axis. + ax = plot_contour(spectrum_id=sp_id, ppm=True, show=False) + + # Set new limits. + ax.set_xlim(30, 0) + ax.set_ylim(15, -20) + + # add some labels + ax.text(25.0, 0.0, "Test", size=8, color='r') + + # Now show + import matplotlib.pyplot as plt + plt.show()