Skip to content

Commit 0ea4204

Browse files
committed
Fixed errors with imports and black
1 parent 8849090 commit 0ea4204

File tree

7 files changed

+59
-158
lines changed

7 files changed

+59
-158
lines changed

src/grid_strategy/_abc.py

+40-55
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10+
from .backends.mtpltlib import Matplotlib
11+
from .backends.bkh import Bokeh
12+
1013

1114
class GridStrategy(metaclass=ABCMeta):
1215
"""
@@ -15,8 +18,38 @@ class GridStrategy(metaclass=ABCMeta):
1518
nearly square (nearly equal in both dimensions).
1619
"""
1720

18-
def __init__(self, alignment="center"):
21+
def __init__(self, alignment="center", backend="matplotlib"):
1922
self.alignment = alignment
23+
self.supported_backends = ["matplotlib", "bokeh"]
24+
self.library = None
25+
26+
assert (
27+
backend in self.supported_backends
28+
), f"Library {backend} is not a supported backend."
29+
if backend == "matplotlib":
30+
try:
31+
import matplotlib
32+
except ImportError:
33+
print(
34+
"matplotlib not installed. Please install it to use it with grid_strategy."
35+
)
36+
self.library = Matplotlib(alignment=self.alignment)
37+
38+
elif backend == "bokeh":
39+
try:
40+
import bokeh
41+
except ImportError:
42+
print(
43+
"Bokeh is not installed. Please install it to use it with grid_strategy."
44+
)
45+
self.library = Bokeh(alignment=self.alignment)
46+
47+
# elif backend == "plotly":
48+
# try:
49+
# import plotly
50+
# except ImportError:
51+
# print("plotly not installed. Please install it to use it with grid_strategy.")
52+
# self.library = Plotly(alignment=self.alignment)
2053

2154
def get_grid(self, n):
2255
"""
@@ -30,70 +63,22 @@ def get_grid(self, n):
3063
where each x would be a subplot.
3164
"""
3265

66+
if n < 0:
67+
raise ValueError
3368
grid_arrangement = self.get_grid_arrangement(n)
34-
return self.get_gridspec(grid_arrangement)
69+
return self.get_figures(grid_arrangement)
3570

3671
@classmethod
3772
@abstractmethod
3873
def get_grid_arrangement(cls, n): # pragma: nocover
3974
pass
4075

41-
def get_gridspec(self, grid_arrangement):
76+
def get_figures(self, grid_arrangement):
4277
nrows = len(grid_arrangement)
4378
ncols = max(grid_arrangement)
4479

4580
# If it has justified alignment, will not be the same as the other alignments
4681
if self.alignment == "justified":
47-
return self._justified(nrows, grid_arrangement)
48-
else:
49-
return self._ragged(nrows, ncols, grid_arrangement)
50-
51-
def _justified(self, nrows, grid_arrangement):
52-
ax_specs = []
53-
num_small_cols = np.lcm.reduce(grid_arrangement)
54-
gs = gridspec.GridSpec(
55-
nrows, num_small_cols, figure=plt.figure(constrained_layout=True)
56-
)
57-
for r, row_cols in enumerate(grid_arrangement):
58-
skip = num_small_cols // row_cols
59-
for col in range(row_cols):
60-
s = col * skip
61-
e = s + skip
62-
63-
ax_specs.append(gs[r, s:e])
64-
return ax_specs
65-
66-
def _ragged(self, nrows, ncols, grid_arrangement):
67-
if len(set(grid_arrangement)) > 1:
68-
col_width = 2
82+
return self.library._justified(nrows, grid_arrangement)
6983
else:
70-
col_width = 1
71-
72-
gs = gridspec.GridSpec(
73-
nrows, ncols * col_width, figure=plt.figure(constrained_layout=True)
74-
)
75-
76-
ax_specs = []
77-
for r, row_cols in enumerate(grid_arrangement):
78-
# This is the number of missing columns in this row. If some rows
79-
# are a different width than others, the column width is 2 so every
80-
# column skipped at the beginning is also a missing slot at the end.
81-
if self.alignment == "left":
82-
# This is left-justified (or possibly full justification)
83-
# so no need to skip anything
84-
skip = 0
85-
elif self.alignment == "right":
86-
# Skip two slots for every missing plot - right justified.
87-
skip = (ncols - row_cols) * 2
88-
else:
89-
# Defaults to centered, as that is the default value for the class.
90-
# Skip one for each missing column - centered
91-
skip = ncols - row_cols
92-
93-
for col in range(row_cols):
94-
s = skip + col * col_width
95-
e = s + col_width
96-
97-
ax_specs.append(gs[r, s:e])
98-
99-
return ax_specs
84+
return self.library._ragged(nrows, ncols, grid_arrangement)

src/grid_strategy/backends.py

-89
This file was deleted.

src/grid_strategy/backends/__init__.py

Whitespace-only changes.

src/grid_strategy/backends/bkh.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
class JustifiedGrid:
66
def __init__(self, nrows, grid_arrangement):
7-
self.plots = [[]]
8-
self.grid_arrangement = grid_arrangement
9-
self.current_row = 0
10-
self.nrows = nrows
7+
self.plots = [[]]
8+
self.grid_arrangement = grid_arrangement
9+
self.current_row = 0
10+
self.nrows = nrows
1111

1212
def add_plot(self, plot):
1313
self.plots[self.current_row].append(plot)
1414
if len(self.plots[self.current_row]) == self.grid_arrangement[self.current_row]:
1515
self.plots.append([])
1616
self.current_row += 1
17-
assert self.current_row <= self.nrows, "Error: More graphs added to layout than previously specified."
17+
assert (
18+
self.current_row <= self.nrows
19+
), "Error: More graphs added to layout than previously specified."
1820

1921
def add_plots(self, plot_list):
2022
for plot in plot_list:
@@ -41,15 +43,17 @@ def add_plot(self, plot):
4143
if len(self.plots[self.current_row]) == self.grid_arrangement[self.current_row]:
4244
self.plots.append([])
4345
self.current_row += 1
44-
assert self.current_row <= self.nrows, "Error: More graphs added to the layout than previously specified."
46+
assert (
47+
self.current_row <= self.nrows
48+
), "Error: More graphs added to the layout than previously specified."
4549

4650
def add_plots(self, plots):
4751
for plot in plots:
4852
self.add_plot(plot)
4953

5054
def output_dest(self, file):
5155
output_file(file)
52-
56+
5357
def show_plot(self):
5458
for row in self.plots:
5559
if len(row) == max(self.grid_arrangement):
@@ -65,13 +69,13 @@ def show_plot(self):
6569
l = layout(self.plots, sizing_mode="scale_both")
6670
show(l)
6771

72+
6873
class Bokeh:
69-
7074
def __init__(self, alignment):
7175
self.alignment = alignment
7276

7377
def _justified(self, nrows, grid_arrangement):
7478
return JustifiedGrid(nrows, grid_arrangement)
7579

7680
def _ragged(self, nrows, ncols, grid_arrangement):
77-
return AlignedGrid(nrows, ncols, grid_arrangement, self.alignment)
81+
return AlignedGrid(nrows, ncols, grid_arrangement, self.alignment)

src/grid_strategy/backends/mtpltlib.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import matplotlib.pyplot as plt
33
import numpy as np
44

5-
class Matplotlib:
65

6+
class Matplotlib:
77
def __init__(self, alignment="center"):
88
self.alignment = alignment
99

@@ -58,8 +58,6 @@ def _ragged(self, nrows, ncols, grid_arrangement):
5858
return ax_specs
5959

6060

61-
62-
6361
# class Plotly:
6462
# from plotly.subplots import make_subplots
6563
# import numpy as np
@@ -89,4 +87,4 @@ def _ragged(self, nrows, ncols, grid_arrangement):
8987
# return fig, grid_arrangement
9088

9189
# def _ragged(self, nrows, ncols, grid_arrangement):
92-
# pass
90+
# pass

tests/test_grids.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from unittest import mock
33

44
from grid_strategy.strategies import SquareStrategy
5+
import grid_strategy.backends
56

67

78
class SpecValue:

tox.ini

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py36, py37, black-check
2+
envlist = py36, py37, py38, black-check
33
skip_missing_interpreters = true
44
isolated_build = true
55

@@ -10,6 +10,7 @@ deps =
1010
pytest
1111
pytest-cov >= 2.0.0
1212
coverage
13+
bokeh
1314
commands = pytest --cov-config="{toxinidir}/tox.ini" \
1415
--cov="{envsitepackagesdir}/grid_strategy" \
1516
--cov="{toxinidir}/tests" \
@@ -40,6 +41,7 @@ show_missing = True
4041
description = test if black works
4142
deps =
4243
pytest-black
44+
bokeh
4345
commands = pytest --black
4446

4547
[testenv:docs]

0 commit comments

Comments
 (0)