Skip to content

Commit

Permalink
Merge pull request #15 from ACCLAB/v0.1.3
Browse files Browse the repository at this point in the history
v0.1.3
  • Loading branch information
josesho committed Jun 14, 2018
2 parents 344e26d + 0c3f09f commit 82490f5
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 104 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The Clear BSD License

Copyright (c) 2016-2017 Joses W. Ho
Copyright (c) 2016-2018 Joses W. Ho
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# DABEST (Python)
[![Travis CI](https://travis-ci.org/ACCLAB/DABEST-python.svg?branch=master)](https://travis-ci.org/ACCLAB/DABEST-python)
[![PyPI](https://img.shields.io/pypi/v/dabest.svg)](https://pypi.python.org/pypi/dabest/0.1.1)
[![PyPI](https://img.shields.io/pypi/v/dabest.svg)](https://pypi.python.org/pypi/dabest/0.1.3)

## About

Expand All @@ -17,7 +17,7 @@ In addition, the following packages are also required:
- [scipy](https://www.scipy.org/) (1.0.x)
- [matplotlib](https://www.matplotlib.org/) (2.0.x)
- [seaborn](https://seaborn.pydata.org/) (0.8.x)
- [pandas](https://pandas.pydata.org/) (0.22.x).
- [pandas](https://pandas.pydata.org/) (0.23.x).

To obtain these package dependencies easily, it is highly recommended to download the [Anaconda distribution](https://www.continuum.io/downloads) of Python.

Expand All @@ -42,10 +42,19 @@ Then, navigate to the cloned repo in the command line and run
pip install .
```


## Usage

Please refer to the [documentation](https://acclab.github.io/DABEST-python-docs/index.html).


## Matlab version

There is also a [Matlab version](https://github.com/ACCLAB/DABEST-Matlab) of DABEST.


## R version

There isn't an implementation of DABEST in R, and there are no current plans to create one.

However, it is possible to use the R package `reticulate` to run Python code. Please take a look at this [tutorial](https://acclab.github.io/DABEST-python-docs/dabest-r.html) on how to use `reticulate` to analyse data in R.
2 changes: 1 addition & 1 deletion dabest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .api import plot
from .bootstrap_tools import bootstrap

__version__="0.1.2"
__version__="0.1.3"
9 changes: 9 additions & 0 deletions dabest/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#!/usr/bin/python
# -*-coding: utf-8 -*-
# Author: Joses Ho
# Email : joseshowh@gmail.com



def plot(data, idx,
Expand Down Expand Up @@ -732,5 +737,9 @@ def plot(data, idx,
value=np.nan).dropna(axis=1)
# Reset seaborn aesthetic parameters.
sns.set()

if swarm_label is not None:
fig.axes[0].set_ylabel(swarm_label)

# Return the figure and the results DataFrame.
return fig, bootlist_df
11 changes: 10 additions & 1 deletion dabest/bootstrap_tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/usr/bin/python
# -*-coding: utf-8 -*-
# Author: Joses Ho
# Email : joseshowh@gmail.com


from __future__ import division


Expand Down Expand Up @@ -160,7 +166,7 @@ def __init__(self, x1, x2=None,

else:
diff = True
tx = x2-x1
tx = x2 - x1
ttest_single = 'NIL'
ttest_2_ind = 'NIL'
ttest_2_paired = ttest_rel(x1,x2)[1]
Expand All @@ -183,6 +189,7 @@ def __init__(self, x1, x2=None,

elif x2 is not None and paired is False:
diff = True
x2 = pd.Series(x2).dropna()
# Generate statarrays for both arrays.
ref_statarray = sns.algorithms.bootstrap(x1, **sns_bootstrap_kwargs)
exp_statarray = sns.algorithms.bootstrap(x2, **sns_bootstrap_kwargs)
Expand Down Expand Up @@ -290,6 +297,8 @@ def bca(data, alphas, statarray, statfunction, ostat, reps):
Subroutine called to calculate the BCa statistics.
Borrowed heavily from scikits.bootstrap code.
'''
import warnings

import numpy as np
import pandas as pd
import seaborn as sns
Expand Down
5 changes: 5 additions & 0 deletions dabest/misc_tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#!/usr/bin/python
# -*-coding: utf-8 -*-
# Author: Joses Ho
# Email : joseshowh@gmail.com

# CONVENIENCE FUNCTIONS THAT DON'T DIRECTLY DEAL WITH PLOTTING OR
# BOOTSTRAP COMPUTATIONS ARE PLACED HERE.

Expand Down
99 changes: 10 additions & 89 deletions dabest/plot_tools.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""
A set of convenience functions used for producing plots in `dabest`.
#!/usr/bin/python
# -*-coding: utf-8 -*-
# Author: Joses Ho
# Email : joseshowh@gmail.com
# A set of convenience functions used for producing plots in `dabest`.

Author: Joses W. Ho
Email: joseshowh@gmail.com
License: MIT
"""


import matplotlib.pyplot as plt
Expand All @@ -14,6 +13,7 @@
from .misc_tools import merge_two_dicts



def halfviolin(v, half = 'right', color = 'k'):
for b in v['bodies']:
mVertical = np.mean(b.get_paths()[0].vertices[:, 0])
Expand All @@ -33,6 +33,8 @@ def halfviolin(v, half = 'right', color = 'k'):
mHorizontal, np.inf)
b.set_color(color)



def align_yaxis(ax1, v1, ax2, v2):
"""adjust ax2 ylimit so that v2 in ax2 is aligned to v1 in ax1"""
# Taken from
Expand All @@ -50,90 +52,7 @@ def rotate_ticks(axes, angle=45, alignment='right'):
tick.set_rotation(angle)
tick.set_horizontalalignment(alignment)

def tufte_summary_line(df, x, y, type='mean_sd',
offset=0.3, ax=None, **kwargs):
'''Convenience function to plot sumamry statistics (mean and standard
deviation, or median and 25th & 75th percentiles) for ach group in the `x`
column of `df`. This style is inspired by Edward Tufte.
Keywords
--------
data: pandas DataFrame.
This DataFrame should be in 'long' format.
x, y: string.
x and y columns to be plotted.
type: {'mean_sd', 'median_quartiles'}, default 'mean_sd'
Plots the summary statistics for each group. If 'mean_sd', then the
mean and standard deviation of each group is plotted as a notched
line beside each group. If 'median_quantile', then the
median and 25th and 75th percentiles of each group is plotted
instead.
offset: float, default 0.4
The x-offset of the summary line.
offset: matplotlib Axes, default None
If specified, the axes to plot on.
kwargs: dict, default None
Dictionary with kwargs passed to `matplotlib.patches.FancyArrow`.
See docs at
https://matplotlib.org/api/_as_gen/
matplotlib.patches.FancyArrow.html#matplotlib.patches.FancyArrow
'''
import matplotlib.patches as mpatches

if ax is None:
ax = plt.gca()

means = df.groupby(x)[y].mean()
sd = df.groupby(x)[y].std()
lower_sd = means - sd
upper_sd = means + sd

medians = df.groupby(x)[y].median()
quantiles = df.groupby(x)[y].quantile([0.25, 0.75]).unstack()
lower_quartiles = quantiles[0.25]
upper_quartiles = quantiles[0.75]

if type == 'mean_sd':
central_measures = means
low = lower_sd
high = upper_sd
elif type == 'median_quartiles':
central_measures = medians
low = lower_quartiles
high = upper_quartiles

total_width = 0.05 # the horizontal span of the line, aka `linewidth`.

for k, m in enumerate(central_measures):

kwargs['dx'] = 0
kwargs['width'] = total_width
kwargs['head_width'] = total_width
kwargs['length_includes_head'] = True

if type == 'mean_sd':
dy_low = dy_high = sd[k]
elif type == 'median_quartiles':
dy_low = m - low[k]
dy_high = high[k] - m

arrow = mpatches.FancyArrow(x=offset+k, y=low[k],
dy=dy_low,
head_length=0.3*dy_low,
**kwargs)
ax.add_patch(arrow)

arrow = mpatches.FancyArrow(x=offset+k, y=high[k],
dy=-dy_high,
head_length=0.3*dy_high,
**kwargs)
ax.add_patch(arrow)

def get_swarm_spans(coll):
"""
Expand All @@ -147,6 +66,8 @@ def get_swarm_spans(coll):
except ValueError:
return None



def gapped_lines(data, x, y,
type='mean_sd',
offset=0.3,
Expand Down
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ help:
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
Binary file added docs/source/_images/dabest-in-r-markdown.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_images/dabest-plot-CO2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_images/ggplot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added docs/source/_static/.Rhistory
Empty file.
45 changes: 45 additions & 0 deletions docs/source/_static/reticulate_tutorial.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
```{r}
library(dplyr)
library(ggplot2)
df = filter(CO2, conc == '1000')
ggplot(df, aes(x = Type,
y = uptake,
color = Treatment)) +
geom_point() +
ggtitle('CO2 conc = 1000 mL/L') +
xlab('City') + ylab('CO2 uptake')
```


```{r}
library(reticulate)
```


```{python}
import matplotlib.pyplot as plt
import dabest
f1, results = dabest.plot(data=r.df, fig_size=(5,7),
x='Type', y='uptake',
swarm_label='CO2 uptake',
color_col='Treatment',
idx=['Quebec', 'Mississippi']
)
f1.savefig('dabest-plot-CO2.png', bbox_inches='tight')
```

```{r}
py_results = py$results
mean_diff = py_results$stat_summary
ci_low = py_results$bca_ci_low
ci_high = py_results$bca_ci_high
# Print results, with all numerical values formatted to 2 decimal places.
sprintf("Mean Difference = %.2f [95CI %.2f, %.2f]",
mean_diff, ci_low, ci_high)
```

15 changes: 15 additions & 0 deletions docs/source/_templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "!layout.html" %}

{%- block extrahead %}
{{ super() }}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-120627927-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-120627927-1');
</script>

{% endblock %}
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
# The short X.Y version.
version = '0.1'
# The full version, including alpha/beta/rc tags.
release = '0.1.2'
release = '0.1.3'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down

0 comments on commit 82490f5

Please sign in to comment.