Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 48 additions & 33 deletions docs/tutorial/activities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.simplefilter('ignore')\n",
"\n",
"from numpy.random import seed\n",
"from scipy import stats\n",
"from matplotlib import pyplot\n",
"import seaborn\n",
"\n",
Expand Down Expand Up @@ -127,7 +128,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fitting distribution\n",
"### Fitting distributions\n",
"Fitting distributions to data follows a similar pattern."
]
},
Expand All @@ -143,6 +144,25 @@
"paramnormal.activity.fit('beta', data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Equivalent command to perform the same fits in raw scipy is shown below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# constrained loc and scale\n",
"stats.beta.fit(data, floc=0, fscale=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -165,8 +185,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting\n",
"There is very limited plotting functionality built into paramnormal. For the time being, we just plot the probability distribution function (PDF), and offload everything else to the seaborn module if it's available."
"And again in raw scipy:"
]
},
{
Expand All @@ -177,15 +196,17 @@
},
"outputs": [],
"source": [
"ax = paramnormal.activity.plot('beta', α=3, β=2)\n",
"ax.figure"
"# constrained beta and scale, unconstrained loc\n",
"stats.beta.fit(data, f1=2, fscale=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can plot on an existing figure through the `ax` argument and control the line style through `line_opts`."
"### Plotting\n",
"There is very limited plotting functionality built into paramnormal.\n",
"The probability distribution function (PDF) is plotted by default, but any other method of the distributions can be plotted by specifying the ``which`` parameters."
]
},
{
Expand All @@ -196,21 +217,16 @@
},
"outputs": [],
"source": [
"seaborn.set(style='ticks', rc=clean_bkgd)\n",
"fig, (ax1, ax2) = pyplot.subplots(nrows=2, sharex=True, sharey=True)\n",
"ax1 = paramnormal.activity.plot('beta', α=6, β=2, ax=ax1, line_opts=dict(color='firebrick', lw=3))\n",
"ax2 = paramnormal.activity.plot('beta', α=2, β=6, ax=ax2, line_opts=dict(color='forestgreen', lw=1.25))\n",
"ax1.set_ylabel('α=6, β=2')\n",
"ax2.set_ylabel('α=2, β=6')\n",
"seaborn.despine(fig)\n",
"fig"
"ax = paramnormal.activity.plot('beta', α=3, β=2)\n",
"paramnormal.activity.plot('beta', α=3, β=2, ax=ax, which='CDF')\n",
"ax.legend()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Of course, you can create a fully-specified distribtion and omit the distribution parameters."
"You can plot on an existing figure through the `ax` argument and control the line style through `line_opts`."
]
},
{
Expand All @@ -221,16 +237,19 @@
},
"outputs": [],
"source": [
"beta = paramnormal.beta(α=3, β=2)\n",
"ax = paramnormal.activity.plot(beta)\n",
"ax.figure"
"fig, (ax, ax2) = pyplot.subplots(nrows=2, sharex=True, sharey=True)\n",
"paramnormal.activity.plot('beta', α=6, β=2, ax=ax, line_opts=dict(color='firebrick', lw=3))\n",
"paramnormal.activity.plot('beta', α=2, β=6, ax=ax2, line_opts=dict(color='forestgreen', lw=1.25))\n",
"ax.set_ylabel('α=6, β=2')\n",
"ax2.set_ylabel('α=2, β=6')\n",
"seaborn.despine(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing an array of sample data will fit the distribution to that data and allow you to use `seaborn.distplot`."
"Of course, you can create a fully-specified distribtion and omit the distribution parameters."
]
},
{
Expand All @@ -241,18 +260,15 @@
},
"outputs": [],
"source": [
"data = paramnormal.activity.random('beta', α=3, β=2, shape=125)\n",
"ax = paramnormal.activity.plot('beta', data, distplot=True)\n",
"seaborn.despine()\n",
"ax.legend()\n",
"ax.figure"
"beta = paramnormal.beta(α=3, β=2)\n",
"ax = paramnormal.activity.plot(beta)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And you can control `seaborn.distplot` through `displot_opts`."
"And finally, you can pass an array of data and an unfrozen distribution, and a new distribution will be fit to your data."
]
},
{
Expand All @@ -263,12 +279,11 @@
},
"outputs": [],
"source": [
"data = paramnormal.activity.random('normal', μ=0.75, σ=1.25, shape=125)\n",
"ax = paramnormal.activity.plot('normal', data, distplot=True,\n",
" distplot_opts=dict(hist=True, rug=True, kde=False, norm_hist=True))\n",
"seaborn.despine()\n",
"ax.legend()\n",
"ax.figure"
"data = paramnormal.activity.random('beta', α=2, β=6, shape=37) + \\\n",
" paramnormal.activity.random('normal', μ=5, σ=1, shape=37)\n",
"ax = paramnormal.activity.plot('normal', data=data, line_opts=dict(label='Emperical Fit'))\n",
"ax = paramnormal.activity.plot('normal', μ=5, σ=1, line_opts=dict(label='Theoretical'))\n",
"ax.legend()"
]
}
],
Expand All @@ -293,4 +308,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
43 changes: 21 additions & 22 deletions paramnormal/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def _check_distro(distro, **params):

def _check_ax(ax):
if ax is None:
fig, ax = pyplot.subplots()
else:
ax = pyplot.gca()
fig = ax.figure
else:
fig = ax.figure

return fig, ax

Expand Down Expand Up @@ -121,22 +121,8 @@ def fit(distro, data, as_params=True, **guesses):
return distro.from_params(params)


def _plot_pdf(distro, xlimits, ax=None, xscale='linear', **line_opts):
fig, ax = _check_ax(ax)
if xscale == 'log':
xlimits = numpy.log10(xlimits)
spacer = numpy.logspace
else:
spacer = numpy.linspace

x_hat = spacer(xlimits[0], xlimits[1], num=100)
y_hat = distro.pdf(x_hat)
ax.plot(x_hat, y_hat, **line_opts)
return ax


def plot(distro, data=None, fit_dist=True, ax=None, pad=0.1,
xscale='linear', line_opts=None, **guesses):
def plot(distro, which='PDF', data=None, fit_dist=True, ax=None,
pad=0.05, xscale='linear', line_opts=None, **guesses):
"""
Plot the PDF of a dataset and other representations of the
distribution (histogram, kernel density estimate, and rug plot).
Expand Down Expand Up @@ -253,18 +239,31 @@ def plot(distro, data=None, fit_dist=True, ax=None, pad=0.1,

"""

# validate the axes and distribution function (`which`)
fig, ax = _check_ax(ax)
if data is not None:
distro = fit(distro, data, as_params=False, **guesses)
else:
distro = _check_distro(distro, **guesses)
fxn = getattr(distro, which.lower())

fig, ax = _check_ax(ax)
# determine and set the xlimits of the plot
xlimits = distro.ppf([pad/100, 1 - pad/100])

# determine the x-values
if xscale == 'log':
#xlimits = numpy.log10(xlimits)
x_hat = numpy.logspace(*numpy.log10(xlimits), num=100)
else:
x_hat = numpy.linspace(*xlimits, num=100)

# compute y-values
y_hat = fxn(x_hat)

line_opts = dict() if line_opts is None else line_opts
line_opts['label'] = line_opts.pop('label', 'PDF')
xlimits = distro.ppf([pad/100, 1 - pad/100])
line_opts['label'] = line_opts.pop('label', which)

ax = _plot_pdf(distro, xlimits, ax=ax, xscale=xscale, **line_opts)
line, = ax.plot(x_hat, y_hat, **line_opts)
ax.set_xscale(xscale)

return ax
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
86 changes: 80 additions & 6 deletions paramnormal/tests/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ def test_normal(self):

assert_dists_are_equivalent(dist, stats.norm(params.mu, params.sigma))

@image_comparison(baseline_images=['test_plot_basic'], extensions=['png'])
@image_comparison(baseline_images=['test_plot_pdf_basic'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_basic():
def test_plot_pdf_basic():
# first
fig, ax1 = pyplot.subplots()
norm_dist = dist.normal(μ=5.4, σ=2.5)
ax1 = activity.plot(norm_dist, ax=ax1)


@image_comparison(baseline_images=['test_plot_fit'], extensions=['png'])
@image_comparison(baseline_images=['test_plot_pdf_fit'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_fit():
def test_plot_pdf_fit():
# second
fig2, ax2 = pyplot.subplots()
norm_dist = dist.normal(μ=5.4, σ=2.5)
Expand All @@ -134,11 +134,85 @@ def test_plot_fit():
ax2.legend()


@image_comparison(baseline_images=['test_plot_xlog'], extensions=['png'])
@image_comparison(baseline_images=['test_plot_pdf_xlog'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_xlog():
def test_plot_pdf_xlog():
# first
fig, ax1 = pyplot.subplots()
loc_dist = dist.lognormal(μ=1.25, σ=0.75)
ax1 = activity.plot(loc_dist, ax=ax1, xscale='log')


@image_comparison(baseline_images=['test_plot_cdf_basic'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_cdf_basic():
# first
fig, ax1 = pyplot.subplots()
norm_dist = dist.normal(μ=5.4, σ=2.5)
ax1 = activity.plot(norm_dist, ax=ax1, which='cdf')


@image_comparison(baseline_images=['test_plot_cdf_fit'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_cdf_fit():
# second
fig2, ax2 = pyplot.subplots()
norm_dist = dist.normal(μ=5.4, σ=2.5)
data = activity.random('normal', μ=5.4, σ=2.5, shape=37)
ax2 = activity.plot(norm_dist, ax=ax2, line_opts=dict(label='Theoretical CDF'), which='cdf')
ax2 = activity.plot('normal', data=data, ax=ax2, line_opts=dict(label='Fit CDF'), which='cdf')
ax2.legend()


@image_comparison(baseline_images=['test_plot_cdf_xlog'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_cdf_xlog():
# first
fig, ax1 = pyplot.subplots()
loc_dist = dist.lognormal(μ=1.25, σ=0.75)
ax1 = activity.plot(loc_dist, ax=ax1, xscale='log', which='CDF')
ax1.legend()

@image_comparison(baseline_images=['test_plot_sf_basic'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_sf_basic():
# first
fig, ax1 = pyplot.subplots()
norm_dist = dist.normal(μ=5.4, σ=2.5)
ax1 = activity.plot(norm_dist, ax=ax1, which='sf')


@image_comparison(baseline_images=['test_plot_sf_fit'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_sf_fit():
# second
fig2, ax2 = pyplot.subplots()
norm_dist = dist.normal(μ=5.4, σ=2.5)
data = activity.random('normal', μ=5.4, σ=2.5, shape=37)
ax2 = activity.plot(norm_dist, ax=ax2, line_opts=dict(label='Theoretical sf'), which='sf')
ax2 = activity.plot('normal', data=data, ax=ax2, line_opts=dict(label='Fit sf'), which='sf')
ax2.legend()


@image_comparison(baseline_images=['test_plot_sf_xlog'], extensions=['png'])
@nptest.dec.skipif(sys.version_info.minor < 4)
@seed
def test_plot_sf_xlog():
# first
fig, ax1 = pyplot.subplots()
loc_dist = dist.lognormal(μ=1.25, σ=0.75)
ax1 = activity.plot(loc_dist, ax=ax1, xscale='log', which='sf')
ax1.legend()


@cleanup
@nt.raises(AttributeError)
def test_plot_bad_attribute():
loc_dist = dist.lognormal(μ=1.25, σ=0.75)
activity.plot(loc_dist, xscale='log', which='JUNK')