Skip to content

Commit

Permalink
cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Maguire, Andrew committed Nov 21, 2019
1 parent d983907 commit 2d460c2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
Empty file added am4894plots/dists/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions am4894plots/dists/bokeh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pandas as pd
import numpy as np
from bokeh.io import output_file, curdoc, output_notebook
from bokeh.layouts import gridplot
from bokeh.plotting import figure, show


def plot_hists(df: pd.DataFrame, cols: list = None, n_bins: int = 50, n_cols: int = 3, pw: int = 400, ph: int = 400,
out_path: str = None, return_p: bool = False, notebook: bool = False, p_theme: str = 'light_minimal',
show_p: bool = True, density: bool = False):
"""Plot histograms"""
# get cols to plot
if not cols:
cols = df._get_numeric_data().columns
plots = []
for i, col in enumerate(cols):
hist, edges = np.histogram(df[col], density=density, bins=n_bins)
p = figure(title=col, tools='', background_fill_color="#fafafa")
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="navy", line_color="white", alpha=0.5)
plots.append(p)
if out_path:
output_file(out_path)
curdoc().theme = p_theme
if notebook:
output_notebook()
p = gridplot(plots, ncols=n_cols, plot_width=pw, plot_height=ph, toolbar_location=None)
if show_p:
show(p)
if return_p:
return p
37 changes: 37 additions & 0 deletions am4894plots/dists/plotly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import numpy as np
import pandas as pd
import plotly
import math
from plotly.subplots import make_subplots
import plotly.graph_objects as go


def plot_boxes(df: pd.DataFrame, cols: list = None, out_path: str = None, show_p: bool = True, return_p: bool = False,
h: int = None, w: int = None, spacing: float = 0.05, theme: str = 'simple_white',
renderer: str = 'browser', n_cols: int = 3, shared_yaxes: bool = True):
"""plot box plots"""
# get cols to plot
if not cols:
cols = df._get_numeric_data().columns
n_rows = math.ceil(len(cols) / n_cols)
p = make_subplots(rows=n_rows, cols=n_cols, shared_yaxes=shared_yaxes, vertical_spacing=spacing, horizontal_spacing=spacing)
# figure out what to plot where on the subplot
axes_dict = dict()
i = 0
for index, x in np.ndenumerate(np.zeros((n_cols, n_rows))):
axes_dict[i] = index
i += 1
# make each plot
for i, col in enumerate(cols):
p.add_trace(go.Box(name=col, y=df[col]), row=axes_dict[i][1]+1, col=axes_dict[i][0]+1)
if h:
p.update_layout(height=h)
if w:
p.update_layout(width=w)
p.update_layout(template=theme)
if out_path:
plotly.offline.plot(p, filename=out_path, auto_open=False)
if show_p:
p.show(renderer=renderer)
if return_p:
return p
9 changes: 9 additions & 0 deletions tests/dists/test_bokeh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from am4894pd.utils import df_dummy_ts
from am4894plots.dists.bokeh import plot_hists


def test_plot_hists():
df = df_dummy_ts(n_cols=2, freq='1min')
p = plot_hists(df, out_path=None, return_p=True, show_p=False)
assert str(type(p)) == "<class 'bokeh.models.layouts.GridBox'>"
assert len(p.children) == 2
13 changes: 13 additions & 0 deletions tests/dists/test_plotly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from am4894pd.utils import df_dummy_ts
from am4894plots.dists.plotly import plot_boxes

# make test data
df = df_dummy_ts(n_cols=2, freq='1min')


def test_plot_lines():
p = plot_boxes(df, out_path=None, return_p=True, show_p=False)
assert str(type(p)) == "<class 'plotly.graph_objs._figure.Figure'>"
assert len(p.data) == 2
assert [p.data[c].name for c in range(len(p.data))] == ['col0', 'col1']
assert [len(p.data[c].y) for c in range(len(p.data))] == [1342, 1342]
5 changes: 5 additions & 0 deletions tmp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## tmp

Just a tmp folder for spitting out html plots and things to when working locally.

Nothing of interest in here.

0 comments on commit 2d460c2

Please sign in to comment.