Skip to content

Commit

Permalink
Apply some automatic (pep8) whitespace formating changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoereth committed Sep 10, 2017
1 parent 5f3fd04 commit 6be654c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
9 changes: 1 addition & 8 deletions MPLAnimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, name=None, setup_cb=None):
if name == None:
self.tmpdir = tempfile.TemporaryDirectory()
self.dir = self.tmpdir.name
self.name = 'animator_'+self.dir
self.name = 'animator_' + self.dir
else:
self.dir = '.prerendered/' + name
if not os.path.exists(self.dir):
Expand All @@ -47,7 +47,6 @@ def __init__(self, name=None, setup_cb=None):
if setup_cb:
setup_cb()


def initUI(self):
"""Initialize the UI."""

Expand Down Expand Up @@ -78,7 +77,6 @@ def initUI(self):
self.prerender_checkbox = QtWidgets.QCheckBox('Pre-rendered')
self.layout.addWidget(self.prerender_checkbox)


def setFrameCallback(self, frame_cb, max_frame):
"""Set frame-callback relevant attributes.
Expand All @@ -92,7 +90,6 @@ def setFrameCallback(self, frame_cb, max_frame):
self.max_frame = max_frame
self.slider.setMaximum(max_frame - 1)


def setClickCallback(self, click_cb):
"""Set click-callback relevant attributes.
Expand All @@ -102,7 +99,6 @@ def setClickCallback(self, click_cb):
"""
self.click_cb = click_cb


def prerender(self):
"""Prerender the animation."""
if len(os.listdir(self.dir)) == 0:
Expand All @@ -112,13 +108,11 @@ def prerender(self):
self.frame_cb(i)
plt.savefig('{}/{}.png'.format(self.dir, i))


def handleCanvasClick(self, event: matplotlib.backend_bases.MouseEvent):
"""Unpack canvas click event to click callback function."""
self.click_cb(**(event.__dict__))
self.visualize()


def visualize(self, i=None):
"""Update visualization for set frame.
Expand All @@ -142,7 +136,6 @@ def visualize(self, i=None):
self.frame_cb(i)
self.canvas.draw()


def clear(self):
"""Clear pre-rendered images."""

Expand Down
14 changes: 9 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@
import matplotlib.pyplot as plt
from MPLAnimator import Animator


def naive_estimator(x, data, h):
n = sum(1 for d in data if x-h/2 < d <= x+h/2)
n = sum(1 for d in data if x - h / 2 < d <= x + h / 2)
N = len(data)
return n/(N*h)
return n / (N * h)


data = [0.5, 0.7, 0.8, 1.9, 2.4, 6.1, 6.2, 7.3]
xs = np.arange(0, 8, 0.01)
h = 2
hist = [naive_estimator(x, data, h) for x in xs]


def setup():
plt.gcf()
plt.suptitle("Naive Estimator for h = {}".format(h))


def frame(i):
plt.cla()

# plot original data
plt.plot(xs, hist)
plt.plot(data, [0]*len(data), 'xk')
plt.plot(data, [0] * len(data), 'xk')
plt.axhline(0, color='k', linewidth=0.5)

# calculate current interval
x = i / 10
x1, x2 = x-h/2, x+h/2
x1, x2 = x - h / 2, x + h / 2

# calculate relative width for visualization
axis_to_data = plt.gca().transAxes + plt.gca().transData.inverted()
Expand All @@ -45,7 +49,7 @@ def frame(i):

# highlight data in interval
highlight_data = [d for d in data if x1 < d <= x2]
plt.plot(highlight_data, [0]*len(highlight_data), 'oC3')
plt.plot(highlight_data, [0] * len(highlight_data), 'oC3')

plt.xlim(-0.5, 8.5)

Expand Down

0 comments on commit 6be654c

Please sign in to comment.