Author: tlinnet Date: Fri Nov 28 22:10:19 2014 New Revision: 26811 URL: http://svn.gna.org/viewcvs/relax?rev=26811&view=rev Log: Expanded the histogram plotting to add initial gauss distribution. This shows that the gauss distribution should be fitted, instead of just calculating the gauss distribution from the Full width at half maximum (FWHM). 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=26811&r1=26810&r2=26811&view=diff ============================================================================== --- branches/nmrglue/lib/software/nmrglue.py (original) +++ branches/nmrglue/lib/software/nmrglue.py Fri Nov 28 22:10:19 2014 @@ -23,7 +23,7 @@ """Module for the wrapper functions around the nmrglue module.""" # Python module imports. -from numpy import arange +from numpy import arange, argmax, exp, log, pi, sqrt import matplotlib.pyplot as plt import matplotlib.cm @@ -109,11 +109,13 @@ return ax -def hist_plot(ndarray=None, show=False): +def hist_plot(ndarray=None, hist_kwargs=None, show=False): """Flatten the 2D numpy array, and plot as histogram. @keyword ndarray: The numpy array to flatten, and plot as histogram. @type ndarray: numpy array + @keyword hist_kwargs: The dictionary of keyword arguments to be send to matplotlib.pyplot.hist() plot function. If None, standard values will be used. + @type hist_kwargs: None or dic @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. @@ -128,17 +130,68 @@ fig = plt.figure() ax = fig.add_subplot(111) - #kwargs = {'bins': 3000, 'spam': 'ham'} - - # Make the plot. - n, bins, patches = ax.hist(data, bins=1000, range=None, normed=False, facecolor='green', alpha=0.75) + if hist_kwargs == None: + hist_kwargs = {'bins': 1000, 'range': None, 'normed': False, 'facecolor':'green', 'alpha':0.75} + + # Make the plot, and unpack the dictionary keywords. + #n : array or list of arrays. The values of the histogram bins. + #bins : array. The edges of the bins. + #patches : list or list of lists. Silent list of individual patches used to create the histogram. + n, bins, patches = ax.hist(data, **hist_kwargs) # Calculate the bin centers. bincenters = 0.5*(bins[1:]+bins[:-1]) + # Find index for maximum number in a bin. + i = argmax(n) + + # Get the position for the maximum. + x0 = bincenters[i] + + # Get the amplitude for the maximum. + amp = n[i] + + # Try find Full width at half maximum (FWHM). FWHM = 2 * sqrt(2 ln(2 )) * sigma ~ 2.355 * sigma. + # Half maximum + hm = 0.5 * amp + + # Find the first instances of left and right bin, where is lower than hm. + for j in range(1, len(bins)): + # Find the center values of the bins. + left_bin_x = bincenters[i-j] + right_bin_x = bincenters[i+j] + + # Find the values of the bins. + left_bin_y = n[i-j] + right_bin_y = n[i+j] + + if left_bin_y < hm and right_bin_y < hm: + fwhm = right_bin_x - left_bin_x + fwhm_std = fwhm / (2 * sqrt(2 * log(2))) + break + + # Annotate the center. + ax.annotate("%3.2f"%x0, xy=(x0, 0.0), xycoords='data', xytext=(x0, 0.25*amp), textcoords='data', size=8, horizontalalignment="center", arrowprops=dict(arrowstyle="->", connectionstyle="arc3, rad=0")) + + # Annotate the Full width at half maximum. + ax.annotate("", xy=(left_bin_x, hm), xycoords='data', xytext=(right_bin_x, hm), textcoords='data', arrowprops=dict(arrowstyle="<->", connectionstyle="arc3, rad=0")) + #ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data", va="center", ha="center", bbox=dict(boxstyle="round", fc="w")) + ax.annotate("FWHM=%3.2f std=%3.2f"%(fwhm, fwhm_std), xy=(x0, hm), xycoords="data", size=8, va="center", horizontalalignment="center", bbox=dict(boxstyle="round", facecolor="w")) + + # Plot the gaussian function. + a = amp + sigma = fwhm_std + x = bincenters + + # Calculate the gauss values. + gauss = a*exp(-(x-x0)**2/(2*sigma**2)) + + # Plot the values. + ax.plot(bincenters, gauss, 'r-', label='gauss') + # Set limits. - ax.set_ylim(0, 10000) - ax.set_xlim(-30000, 50000) + ax.set_xlim(x0-5*sigma, x0+5*sigma) + ax.set_ylim(0, amp) # If show. if show: Modified: branches/nmrglue/pipe_control/nmrglue.py URL: http://svn.gna.org/viewcvs/relax/branches/nmrglue/pipe_control/nmrglue.py?rev=26811&r1=26810&r2=26811&view=diff ============================================================================== --- branches/nmrglue/pipe_control/nmrglue.py (original) +++ branches/nmrglue/pipe_control/nmrglue.py Fri Nov 28 22:10:19 2014 @@ -100,11 +100,13 @@ return ax -def plot_hist(ndarray=None, show=False): +def plot_hist(ndarray=None, hist_kwargs=None, show=False): """Flatten the 2D numpy array, and plot as histogram. @keyword ndarray: The numpy array to flatten, and plot as histogram. @type ndarray: numpy array + @keyword hist_kwargs: The dictionary of keyword arguments to be send to matplotlib.pyplot.hist() plot function. If None, standard values will be used. + @type hist_kwargs: None or dic @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. @@ -112,7 +114,7 @@ """ # Call the contour plot. - ax = hist_plot(ndarray=ndarray, show=show) + ax = hist_plot(ndarray=ndarray, hist_kwargs=hist_kwargs, show=show) # Return the axis instance, for possibility for additional decoration. 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=26811&r1=26810&r2=26811&view=diff ============================================================================== --- branches/nmrglue/test_suite/system_tests/nmrglue.py (original) +++ branches/nmrglue/test_suite/system_tests/nmrglue.py Fri Nov 28 22:10:19 2014 @@ -165,4 +165,5 @@ data = cdp.ngdata[sp_id].data # Plot the histogram. - plot_hist(ndarray=data, show=True) + kwargs = {'bins': 3000, 'range': None, 'normed': False, 'facecolor':'green', 'alpha':0.75} + plot_hist(ndarray=data, hist_kwargs=kwargs, show=True)