Skip to content

Commit 45450de

Browse files
authored
Merge pull request #9 from boutproject/black
Black
2 parents 86db679 + e0b4e0e commit 45450de

37 files changed

+627
-443
lines changed

.github/workflows/black-fix.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: black
2+
3+
on: push
4+
5+
defaults:
6+
run:
7+
shell: bash
8+
9+
jobs:
10+
black:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
with:
16+
ref: ${{ github.head_ref }}
17+
18+
- name: Installing black
19+
run: |
20+
sudo apt update -y
21+
sudo apt -y install python3-pip python3-setuptools python3-wheel
22+
pip3 install black[jupyter]
23+
24+
- name: Version
25+
run: |
26+
python3 --version
27+
$HOME/.local/bin/black --version
28+
29+
- name: Run black
30+
run: |
31+
$HOME/.local/bin/black . $(grep '^#!.*/bin/.*python.*$' . -Ir|cut -d: -f1)
32+
33+
- uses: stefanzweifel/git-auto-commit-action@v4
34+
with:
35+
commit_message: "Apply black changes"

.github/workflows/python_publish.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Upload Python Package
2+
3+
on:
4+
# Only run when a release is created
5+
release:
6+
types: [created]
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v1
13+
- name: Set up Python
14+
uses: actions/setup-python@v1
15+
with:
16+
python-version: '3.x'
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install setuptools wheel twine
21+
- name: Build and publish
22+
env:
23+
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
24+
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
25+
run: |
26+
python setup.py sdist bdist_wheel
27+
twine upload dist/*

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ ENV/
9898

9999
# vscode reST doc preview
100100
_build
101+
/animatplot/_version.py
102+
103+
# temporary files
104+
*~
105+
\#*
106+
.\#*

animatplot/_version.py

-3
This file was deleted.

animatplot/animation.py

+77-48
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Animation:
2929
animation
3030
a matplotlib animation returned from FuncAnimation
3131
"""
32+
3233
def __init__(self, blocks, timeline=None, fig=None):
3334
self.fig = plt.gcf() if fig is None else fig
3435

@@ -45,8 +46,9 @@ def _animate(self, blocks, timeline):
4546
_len_time = len(self.timeline)
4647
for block in blocks:
4748
if len(block) != _len_time:
48-
raise ValueError("All blocks must animate for the same amount "
49-
"of time")
49+
raise ValueError(
50+
"All blocks must animate for the same amount " "of time"
51+
)
5052

5153
self.blocks = blocks
5254
self._has_slider = False
@@ -62,21 +64,25 @@ def update_all(i):
6264
self.timeline._update()
6365
return updates
6466

65-
return FuncAnimation(self.fig, update_all, frames=self.timeline._len,
66-
interval=1000 / self.timeline.fps)
67+
return FuncAnimation(
68+
self.fig,
69+
update_all,
70+
frames=self.timeline._len,
71+
interval=1000 / self.timeline.fps,
72+
)
6773

6874
@property
6975
def _controls_gridspec(self):
7076
if self._controls_gridspec_object is None:
7177
# make the bottom of the subplots grid lower to fit the controls in
72-
adjust_plot = {'bottom': 0.03}
78+
adjust_plot = {"bottom": 0.03}
7379
plt.subplots_adjust(**adjust_plot)
7480

7581
controls_height = 0.2
7682

7783
fig_gridspecs = self.fig._gridspecs
7884
if len(fig_gridspecs) > 1:
79-
raise ValueError('multiple gridspecs found in figure')
85+
raise ValueError("multiple gridspecs found in figure")
8086
gs = fig_gridspecs[0]
8187
nrows, ncols = gs.get_geometry()
8288
height_ratios = gs.get_height_ratios()
@@ -85,17 +91,17 @@ def _controls_gridspec(self):
8591
if height_ratios is None:
8692
# if height_ratios is None, all rows on the original gridspec
8793
# are the same height
88-
height_ratios = [(1.-controls_height)/nrows
89-
for i in range(nrows)]
94+
height_ratios = [(1.0 - controls_height) / nrows for i in range(nrows)]
9095
else:
91-
height_ratios = [r*(1.-controls_height) for r in height_ratios]
96+
height_ratios = [r * (1.0 - controls_height) for r in height_ratios]
9297
height_ratios.append(controls_height)
9398
gs._nrows += 1
9499
gs.set_height_ratios(height_ratios)
95100

96101
# make a sub-grid in the bottom row
97102
self._controls_gridspec_object = gs[-1, :].subgridspec(
98-
1,3, width_ratios=[.07, .65, .28], wspace=0., hspace=0.)
103+
1, 3, width_ratios=[0.07, 0.65, 0.28], wspace=0.0, hspace=0.0
104+
)
99105
gs.update()
100106

101107
return self._controls_gridspec_object
@@ -112,17 +118,24 @@ def toggle(self, ax=None):
112118
try:
113119
button_subplotspec = self._controls_gridspec[0, 2]
114120
button_gridspec = button_subplotspec.subgridspec(
115-
3, 3, width_ratios=[0.45, 0.45, 0.1],
116-
height_ratios=[.05, .5, .45], wspace=0., hspace=0.)
117-
self.button_ax = self.fig.add_subplot(button_gridspec[1,1])
121+
3,
122+
3,
123+
width_ratios=[0.45, 0.45, 0.1],
124+
height_ratios=[0.05, 0.5, 0.45],
125+
wspace=0.0,
126+
hspace=0.0,
127+
)
128+
self.button_ax = self.fig.add_subplot(button_gridspec[1, 1])
118129
except:
119130
# editing the gridspec did not work for some reason, fall back to
120131
# subplots_adjust
121-
print('warning: adding play/pause button to gridspec failed, '
122-
'adding in independent axes. tight_layout() will ignore '
123-
'the button.')
124-
adjust_plot = {'bottom': .2}
125-
left, bottom, width, height = (.78, .03, .1, .07)
132+
print(
133+
"warning: adding play/pause button to gridspec failed, "
134+
"adding in independent axes. tight_layout() will ignore "
135+
"the button."
136+
)
137+
adjust_plot = {"bottom": 0.2}
138+
left, bottom, width, height = (0.78, 0.03, 0.1, 0.07)
126139
rect = (left, bottom, width, height)
127140

128141
plt.subplots_adjust(**adjust_plot)
@@ -132,10 +145,12 @@ def toggle(self, ax=None):
132145

133146
self.button = Button(self.button_ax, "Pause")
134147
self.button.label2 = self.button_ax.text(
135-
x=0.5, y=0.5, s='Play',
136-
verticalalignment='center',
137-
horizontalalignment='center',
138-
transform=self.button_ax.transAxes
148+
x=0.5,
149+
y=0.5,
150+
s="Play",
151+
verticalalignment="center",
152+
horizontalalignment="center",
153+
transform=self.button_ax.transAxes,
139154
)
140155
self.button.label2.set_visible(False)
141156

@@ -150,9 +165,10 @@ def pause(event):
150165
self.button.label2.set_visible(True)
151166
self.fig.canvas.draw()
152167
self._pause ^= True
168+
153169
self.button.on_clicked(pause)
154170

155-
def timeline_slider(self, text='Time', ax=None, valfmt=None, color=None):
171+
def timeline_slider(self, text="Time", ax=None, valfmt=None, color=None):
156172
"""Creates a timeline slider.
157173
158174
Parameters
@@ -171,30 +187,33 @@ def timeline_slider(self, text='Time', ax=None, valfmt=None, color=None):
171187
try:
172188
slider_subplotspec = self._controls_gridspec[0, 1]
173189
slider_gridspec = slider_subplotspec.subgridspec(
174-
3, 1, height_ratios=[.2, .2, .6], wspace=0.,
175-
hspace=0.)
190+
3, 1, height_ratios=[0.2, 0.2, 0.6], wspace=0.0, hspace=0.0
191+
)
176192
self.slider_ax = self.fig.add_subplot(slider_gridspec[1, 0])
177193
except:
178194
# editing the gridspec did not work for some reason, fall back to
179195
# subplots_adjust
180-
print('warning: adding timeline slider to gridspec failed, '
181-
'adding in independent axes. tight_layout() will ignore '
182-
'the slider.')
183-
adjust_plot = {'bottom': .2}
184-
rect = [.18, .05, .5, .03]
196+
print(
197+
"warning: adding timeline slider to gridspec failed, "
198+
"adding in independent axes. tight_layout() will ignore "
199+
"the slider."
200+
)
201+
adjust_plot = {"bottom": 0.2}
202+
rect = [0.18, 0.05, 0.5, 0.03]
185203
plt.subplots_adjust(**adjust_plot)
186204
self.slider_ax = plt.axes(rect)
187205
else:
188206
self.slider_ax = ax
189207

190208
if valfmt is None:
191-
if (np.issubdtype(self.timeline.t.dtype, np.datetime64)
192-
or np.issubdtype(self.timeline.t.dtype, np.timedelta64)):
193-
valfmt = '%s'
209+
if np.issubdtype(self.timeline.t.dtype, np.datetime64) or np.issubdtype(
210+
self.timeline.t.dtype, np.timedelta64
211+
):
212+
valfmt = "%s"
194213
else:
195-
valfmt = '%1.2f'
214+
valfmt = "%1.2f"
196215
if self.timeline.log:
197-
valfmt = '$10^{%s}$' % valfmt
216+
valfmt = "$10^{%s}$" % valfmt
198217

199218
if ax is None:
200219
# Try to intelligently decide slider width to avoid overlap
@@ -208,16 +227,18 @@ def text_width(txt):
208227
extents = self.fig.transFigure.inverted().transform(bbox)
209228
return extents[1][0] - extents[0][0]
210229

211-
text_val_width = max(text_width(valfmt % (self.timeline[i]))
212-
for i in range(len(self.timeline)))
230+
text_val_width = max(
231+
text_width(valfmt % (self.timeline[i]))
232+
for i in range(len(self.timeline))
233+
)
213234
label_width = text_width(text)
214235

215236
# Calculate width of slider
216237
default_button_width = 0.1
217238
width = 0.73 - text_val_width - label_width - default_button_width
218239

219-
adjust_plot = {'bottom': .2}
220-
left, bottom, height = (.18, .05, .03)
240+
adjust_plot = {"bottom": 0.2}
241+
left, bottom, height = (0.18, 0.05, 0.03)
221242
rect = (left, bottom, width, height)
222243

223244
plt.subplots_adjust(**adjust_plot)
@@ -226,18 +247,23 @@ def text_width(txt):
226247
self.slider_ax = ax
227248

228249
self.slider = Slider(
229-
self.slider_ax, label=text, valmin=0, valmax=self.timeline._len-1,
250+
self.slider_ax,
251+
label=text,
252+
valmin=0,
253+
valmax=self.timeline._len - 1,
230254
valinit=0,
231-
valfmt=(valfmt+self.timeline.units),
232-
valstep=1, color=color
255+
valfmt=(valfmt + self.timeline.units),
256+
valstep=1,
257+
color=color,
233258
)
234259
self._has_slider = True
235260

236261
def set_time(new_slider_val):
237262
# Update slider value and text on each step
238263
self.timeline.index = int(new_slider_val)
239264
self.slider.valtext.set_text(
240-
self.slider.valfmt % (self.timeline[self.timeline.index]))
265+
self.slider.valfmt % (self.timeline[self.timeline.index])
266+
)
241267

242268
if self._pause:
243269
for block in self.blocks:
@@ -273,8 +299,9 @@ def save_gif(self, filename):
273299
the name of the file to be created without the file extension
274300
"""
275301
self.timeline.index -= 1 # required for proper starting point for save
276-
self.animation.save(filename+'.gif',
277-
writer=PillowWriter(fps=self.timeline.fps))
302+
self.animation.save(
303+
filename + ".gif", writer=PillowWriter(fps=self.timeline.fps)
304+
)
278305

279306
def save(self, *args, **kwargs):
280307
"""Saves an animation
@@ -312,9 +339,11 @@ def add(self, new):
312339

313340
for i, block in enumerate(new_blocks):
314341
if not isinstance(block, Block):
315-
raise TypeError(f"Block number {i} passed is of type "
316-
f"{type(block)}, not of type "
317-
f"animatplot.blocks.Block (or a subclass)")
342+
raise TypeError(
343+
f"Block number {i} passed is of type "
344+
f"{type(block)}, not of type "
345+
f"animatplot.blocks.Block (or a subclass)"
346+
)
318347

319348
self.blocks.append(block)
320349

animatplot/animations/vectors.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import numpy as np
55

66

7-
def vector_plot(X, Y, U, V, t, skip=5, *, t_axis=0, units='', fps=10,
8-
pcolor_kw={}, quiver_kw={}):
7+
def vector_plot(
8+
X, Y, U, V, t, skip=5, *, t_axis=0, units="", fps=10, pcolor_kw={}, quiver_kw={}
9+
):
910
"""produces an animation of vector fields
1011
1112
This takes 2D vector field, and plots the magnitude as a pcolomesh, and the
@@ -56,8 +57,9 @@ def vector_plot(X, Y, U, V, t, skip=5, *, t_axis=0, units='', fps=10,
5657
The timeline that was generated for the animation.
5758
"""
5859
# plot the magnitude of the vectors as a pcolormesh
59-
blocks = vector_comp(X, Y, U, V, skip, t_axis=t_axis,
60-
pcolor_kw=pcolor_kw, quiver_kw=quiver_kw)
60+
blocks = vector_comp(
61+
X, Y, U, V, skip, t_axis=t_axis, pcolor_kw=pcolor_kw, quiver_kw=quiver_kw
62+
)
6163

6264
# create the animation
6365
timeline = Timeline(t, units=units, fps=fps)

animatplot/blocks/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Block:
1717
ax : matplotlib.axes.Axes
1818
The matplotlib axes that the block is attached to.
1919
"""
20+
2021
def __init__(self, ax=None, t_axis=None):
2122
self.ax = ax if ax is not None else plt.gca()
2223
self.t_axis = t_axis
@@ -44,6 +45,6 @@ def _make_slice(self, i, dim):
4445
"""A helper function to slice arrays or lists"""
4546
if self._is_list:
4647
return i
47-
Slice = [slice(None)]*dim
48+
Slice = [slice(None)] * dim
4849
Slice[self.t_axis] = i
4950
return tuple(Slice)

0 commit comments

Comments
 (0)