From be57896e4c0fd1fa75f43b02bce9a1c8f4bed764 Mon Sep 17 00:00:00 2001 From: arokem Date: Tue, 21 May 2013 08:05:00 -0700 Subject: [PATCH 1/4] VIZ: some visualization functions. WIP. --- MRS/viz.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 MRS/viz.py diff --git a/MRS/viz.py b/MRS/viz.py new file mode 100644 index 0000000..452f1c0 --- /dev/null +++ b/MRS/viz.py @@ -0,0 +1,64 @@ +""" + +Visualization functions + +""" + +import numpy as np +import scipy.stats as stats +import matplotlib.pyplot as plt + +def stacked_spectra(f, spectra, fig=None): + + """ + Plot stacked spectra + + Parameters + ---------- + + """ + if fig is None: + fig, ax = plt.subplots(1) + else: + fig = fig + ax = fig.get_axes()[0] + + offset = 0 + for s in spectra: + ax.plot(f, s+offset, color='k') + offset += np.var(s) + ax.invert_xaxis() + return fig + +def plot_spectra(f, spectra, n_boots=1000, fig=None, pct=68): + """ + Plot average spectra with boot-strapped confidence intervals + + Parameters + ---------- + pct : how many percentiles to include in the CI (default to 95%) + + """ + if fig is None: + fig, ax = plt.subplots(1) + else: + fig = fig + ax = fig.get_axes()[0] + + boots = np.zeros((n_boots, spectra.shape[-1])) + for b in xrange(n_boots): + boots[b] = np.random.random_integers(0, + spectra.shape[-1]-1, + spectra.shape[-1]) + + alpha = (100 - pct)/2. + CI = np.zeros(spectra.shape[-1]) + for x in xrange(CI.shape[0]): + CI[x] = (stats.scoreatpercentile(boots[:, x], 100 - alpha) - + stats.scoreatpercentile(boots[:, x], alpha)) + + y = np.mean(spectra, 0) + ax.plot(f, y, color='k') + ax.fill_between(f, y-CI, y+CI, alpha=0.2, color='b') + ax.invert_xaxis() + return fig From 6ff9eccdc1b1cbd9837cc80f8a9612e533747d41 Mon Sep 17 00:00:00 2001 From: arokem Date: Wed, 9 Oct 2013 09:26:22 -0700 Subject: [PATCH 2/4] Offset by individual variance. --- MRS/viz.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MRS/viz.py b/MRS/viz.py index 452f1c0..9671c85 100644 --- a/MRS/viz.py +++ b/MRS/viz.py @@ -23,9 +23,8 @@ def stacked_spectra(f, spectra, fig=None): fig = fig ax = fig.get_axes()[0] - offset = 0 for s in spectra: - ax.plot(f, s+offset, color='k') + ax.plot(f, s+np.std(s)/2.0, color='k') offset += np.var(s) ax.invert_xaxis() return fig From 076b0c27099d363040fec9ba2cf38d581472215d Mon Sep 17 00:00:00 2001 From: arokem Date: Wed, 9 Oct 2013 09:36:01 -0700 Subject: [PATCH 3/4] One more viz function for the standard 3-plot of GABA + models. --- MRS/viz.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/MRS/viz.py b/MRS/viz.py index 9671c85..0a7422d 100644 --- a/MRS/viz.py +++ b/MRS/viz.py @@ -61,3 +61,27 @@ def plot_spectra(f, spectra, n_boots=1000, fig=None, pct=68): ax.fill_between(f, y-CI, y+CI, alpha=0.2, color='b') ax.invert_xaxis() return fig + + +def plot_gaba(G): + """ + This is the basic three-panel plot: echo1/echo2/diff with model fits + + """ + G.fit_gaba() + fig, ax = plt.subplots(3) + ax[0].plot(G.f_ppm, np.mean(G.echo1,0)) + ax[0].plot(G.f_ppm[G.cr_idx], stats.nanmean(G.creatine_model, 0), 'r') + Cr_text = 'Cr params \n freq0: %1.2f\n area: %1.4f \n hwhm: %1.2f \n phase: %1.2f \n offset: %1.2f \n drift %1.2f'%(tuple([w for w in np.mean(G.creatine_params, 0)])) + ax[0].text(0.9, 0.6, Cr_text, horizontalalignment='center', verticalalignment='center', transform = ax[0].transAxes) + ax[1].plot(G.f_ppm, np.mean(G.echo2,0)) + ax[2].plot(G.f_ppm, np.mean(G.diff_spectra, 0)) + ax[2].plot(G.f_ppm[G.gaba_idx], stats.nanmean(G.gaba_model, 0), 'r') + gaba_text = 'GABA params \n freq0: %1.2f\n sigma: %1.2f \n amp: %1.4f \n offset: %1.2f \n drift %1.2f'%(tuple([w for w in np.mean(G.gaba_params, 0)])) + ax[2].text(0.9, 0.5, gaba_text, horizontalalignment='center', verticalalignment='center', transform = ax[2].transAxes) + ax[2].set_xlabel('ppm') + fig.set_size_inches([10,8]) + for a in ax: + a.invert_xaxis() + + return fig From a998bd3c2b22eb533cd4c5f71172279fb36d8fa9 Mon Sep 17 00:00:00 2001 From: arokem Date: Wed, 9 Oct 2013 09:40:47 -0700 Subject: [PATCH 4/4] BF: Make this play nice. --- MRS/viz.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MRS/viz.py b/MRS/viz.py index 0a7422d..d76ca12 100644 --- a/MRS/viz.py +++ b/MRS/viz.py @@ -23,9 +23,10 @@ def stacked_spectra(f, spectra, fig=None): fig = fig ax = fig.get_axes()[0] + offset = 0 for s in spectra: - ax.plot(f, s+np.std(s)/2.0, color='k') - offset += np.var(s) + ax.plot(f, s.squeeze()+offset, color='k') + offset += np.std(s)/2.0 ax.invert_xaxis() return fig