Skip to content

Commit 9f6d5af

Browse files
authored
Merge pull request sympy#16513 from oscargus/pyglet-fixes
Pyglet fixes for 1.4
2 parents ce6176f + 0e69459 commit 9f6d5af

19 files changed

+211
-197
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ matrix:
4949
env:
5050
- TEST_ASCII="true"
5151
# space separated list of optional dependencies(conda packages) to install and test
52-
- TEST_OPT_DEPENDENCY="numpy scipy gmpy2 matplotlib>=2.2 theano llvmlite autowrap cython wurlitzer python-symengine=0.3.* tensorflow numexpr ipython antlr-python-runtime>=4.7,<4.8 antlr>=4.7,<4.8 cloudpickle python=2.7"
52+
- TEST_OPT_DEPENDENCY="numpy scipy gmpy2 matplotlib>=2.2 theano llvmlite autowrap cython wurlitzer python-symengine=0.3.* tensorflow numexpr ipython antlr-python-runtime>=4.7,<4.8 antlr>=4.7,<4.8 cloudpickle python=2.7 pyglet"
5353
addons:
5454
apt:
5555
packages:
@@ -64,7 +64,7 @@ matrix:
6464
# and we aren't actually using the Travis Python anyway.
6565
env:
6666
- TEST_ASCII="true"
67-
- TEST_OPT_DEPENDENCY="numpy scipy gmpy2 matplotlib theano llvmlite autowrap cython wurlitzer python-symengine=0.3.* tensorflow numexpr ipython antlr-python-runtime>=4.7,<4.8 antlr>=4.7,<4.8 cloudpickle"
67+
- TEST_OPT_DEPENDENCY="numpy scipy gmpy2 matplotlib theano llvmlite autowrap cython wurlitzer python-symengine=0.3.* tensorflow numexpr ipython antlr-python-runtime>=4.7,<4.8 antlr>=4.7,<4.8 cloudpickle pyglet"
6868
- TEST_SAGE="true"
6969
addons:
7070
apt:

examples/advanced/pyglet_plotting.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
"""
44
Plotting Examples
55
6-
Suggested Usage: python -i plotting.py
6+
Suggested Usage: python -i pyglet_plotting.py
77
"""
88

99

10-
from sympy import symbols
11-
from sympy.plotting.pygletplot import PygletPlot
12-
from sympy import sin, cos, pi, sqrt, exp
10+
from sympy import symbols, sin, cos, pi, sqrt
1311
from sympy.core.compatibility import range
12+
from sympy.plotting.pygletplot import PygletPlot
1413

1514
from time import sleep, clock
1615

sympy/plotting/pygletplot/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
@doctest_depends_on(modules=('pyglet',))
88
def PygletPlot(*args, **kwargs):
99
"""
10+
1011
Plot Examples
1112
=============
1213
1314
See examples/advanced/pyglet_plotting.py for many more examples.
1415
15-
1616
>>> from sympy.plotting.pygletplot import PygletPlot as Plot
1717
>>> from sympy import symbols
1818
>>> from sympy.abc import x, y, z
@@ -76,7 +76,7 @@ def PygletPlot(*args, **kwargs):
7676
1: parametric, cartesian, polar
7777
2: parametric, cartesian, cylindrical = polar, spherical
7878
79-
>>> Plot(1, mode='spherical') # doctest: +SKIP
79+
>>> Plot(1, mode='spherical')
8080
8181
8282
Calculator-like Interface
@@ -86,8 +86,8 @@ def PygletPlot(*args, **kwargs):
8686
>>> f = x**2
8787
>>> p[1] = f
8888
>>> p[2] = f.diff(x)
89-
>>> p[3] = f.diff(x).diff(x) # doctest: +SKIP
90-
>>> p # doctest: +SKIP
89+
>>> p[3] = f.diff(x).diff(x)
90+
>>> p
9191
[1]: x**2, 'mode=cartesian'
9292
[2]: 2*x, 'mode=cartesian'
9393
[3]: 2, 'mode=cartesian'
@@ -136,8 +136,8 @@ def PygletPlot(*args, **kwargs):
136136
=============================
137137
"""
138138

139-
import plot
140-
return plot.PygletPlot(*args, **kwargs)
139+
from sympy.plotting.pygletplot.plot import PygletPlot
140+
return PygletPlot(*args, **kwargs)
141141

142142
except Exception as e:
143143
def PygletPlot(*args, **kwargs):

sympy/plotting/pygletplot/color_scheme.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import print_function, division
22

33
from sympy import Basic, Symbol, symbols, lambdify
4-
from sympy.utilities.iterables import sift
5-
from util import interpolate, rinterpolate, create_bounds, update_bounds
64
from sympy.core.compatibility import range, string_types
5+
from .util import interpolate, rinterpolate, create_bounds, update_bounds
6+
from sympy.utilities.iterables import sift
77

88

99
class ColorGradient(object):
@@ -61,13 +61,13 @@ def __init__(self, *args, **kwargs):
6161
else:
6262
self.f = lambdify('x,y,z,u,v', args[0])
6363
else:
64-
self.f, self.gradient = self._interpret_args(args, kwargs)
64+
self.f, self.gradient = self._interpret_args(args)
6565
self._test_color_function()
6666
if not isinstance(self.gradient, ColorGradient):
6767
raise ValueError("Color gradient not properly initialized. "
6868
"(Not a ColorGradient instance.)")
6969

70-
def _interpret_args(self, args, kwargs):
70+
def _interpret_args(self, args):
7171
f, gradient = None, self.gradient
7272
atoms, lists = self._sort_args(args)
7373
s = self._pop_symbol_list(lists)
@@ -162,6 +162,7 @@ def _pop_symbol_list(self, lists):
162162

163163
def _fill_in_vars(self, args):
164164
defaults = symbols('x,y,z,u,v')
165+
v_error = ValueError("Could not find what to plot.")
165166
if len(args) == 0:
166167
return defaults
167168
if not isinstance(args, (tuple, list)):
@@ -205,6 +206,7 @@ def _fill_in_vars(self, args):
205206
def _sort_args(self, args):
206207
lists, atoms = sift(args,
207208
lambda a: isinstance(a, (tuple, list)), binary=True)
209+
return atoms, lists
208210

209211
def _test_color_function(self):
210212
if not callable(self.f):
@@ -213,18 +215,18 @@ def _test_color_function(self):
213215
result = self.f(0, 0, 0, 0, 0)
214216
if len(result) != 3:
215217
raise ValueError("length should be equal to 3")
216-
except TypeError as te:
218+
except TypeError:
217219
raise ValueError("Color function needs to accept x,y,z,u,v, "
218220
"as arguments even if it doesn't use all of them.")
219-
except AssertionError as ae:
221+
except AssertionError:
220222
raise ValueError("Color function needs to return 3-tuple r,g,b.")
221-
except Exception as ie:
223+
except Exception:
222224
pass # color function probably not valid at 0,0,0,0,0
223225

224226
def __call__(self, x, y, z, u, v):
225227
try:
226228
return self.f(x, y, z, u, v)
227-
except Exception as e:
229+
except Exception:
228230
return None
229231

230232
def apply_to_curve(self, verts, u_set, set_len=None, inc_pos=None):

sympy/plotting/pygletplot/managed_window.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import print_function, division
22

3-
from pyglet.gl import *
43
from pyglet.window import Window
54
from pyglet.clock import Clock
65

sympy/plotting/pygletplot/plot.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
from __future__ import print_function, division
22

3-
from sympy import Integer
4-
from sympy.core.compatibility import is_sequence, SYMPY_INTS
5-
63
from threading import RLock
74

85
# it is sufficient to import "pyglet" here once
96
try:
10-
from pyglet.gl import *
7+
import pyglet.gl as pgl
118
except ImportError:
129
raise ImportError("pyglet is required for plotting.\n "
1310
"visit http://www.pyglet.org/")
1411

15-
from plot_object import PlotObject
16-
from plot_axes import PlotAxes
17-
from plot_window import PlotWindow
18-
from plot_mode import PlotMode
12+
from sympy.core.compatibility import is_sequence, SYMPY_INTS
13+
from sympy.core.numbers import Integer
14+
from sympy.geometry.entity import GeometryEntity
15+
from sympy.plotting.pygletplot.plot_axes import PlotAxes
16+
from sympy.plotting.pygletplot.plot_mode import PlotMode
17+
from sympy.plotting.pygletplot.plot_object import PlotObject
18+
from sympy.plotting.pygletplot.plot_window import PlotWindow
19+
from sympy.plotting.pygletplot.util import parse_option_string
20+
from sympy.utilities.decorator import doctest_depends_on
1921

2022
from time import sleep
2123
from os import getcwd, listdir
22-
from util import parse_option_string
2324

24-
from sympy.geometry.entity import GeometryEntity
25-
26-
from sympy.utilities.decorator import doctest_depends_on
25+
import ctypes
2726

2827
@doctest_depends_on(modules=('pyglet',))
2928
class PygletPlot(object):
@@ -33,7 +32,6 @@ class PygletPlot(object):
3332
3433
See examples/advanced/pyglet_plotting.py for many more examples.
3534
36-
3735
>>> from sympy.plotting.pygletplot import PygletPlot as Plot
3836
>>> from sympy.abc import x, y, z
3937
@@ -95,7 +93,7 @@ class PygletPlot(object):
9593
1: parametric, cartesian, polar
9694
2: parametric, cartesian, cylindrical = polar, spherical
9795
98-
>>> Plot(1, mode='spherical') # doctest: +SKIP
96+
>>> Plot(1, mode='spherical')
9997
10098
10199
Calculator-like Interface
@@ -105,8 +103,8 @@ class PygletPlot(object):
105103
>>> f = x**2
106104
>>> p[1] = f
107105
>>> p[2] = f.diff(x)
108-
>>> p[3] = f.diff(x).diff(x) # doctest: +SKIP
109-
>>> p # doctest: +SKIP
106+
>>> p[3] = f.diff(x).diff(x)
107+
>>> p
110108
[1]: x**2, 'mode=cartesian'
111109
[2]: 2*x, 'mode=cartesian'
112110
[3]: 2, 'mode=cartesian'
@@ -226,6 +224,9 @@ def __init__(self, *fargs, **win_args):
226224
True OR False
227225
228226
"""
227+
# Register the plot modes
228+
from . import plot_modes
229+
229230
self._win_args = win_args
230231
self._window = None
231232

@@ -256,8 +257,8 @@ def show(self):
256257
self._win_args['visible'] = True
257258
self.axes.reset_resources()
258259

259-
if hasattr(self, '_doctest_depends_on'):
260-
self._win_args['runfromdoctester'] = True
260+
#if hasattr(self, '_doctest_depends_on'):
261+
# self._win_args['runfromdoctester'] = True
261262

262263
self._window = PlotWindow(self, **self._win_args)
263264

@@ -411,9 +412,7 @@ def __init__(self, plot):
411412
self.flag = 0
412413

413414
def __nonzero__(self):
414-
if self.screenshot_requested:
415-
return 1
416-
return 0
415+
return self.screenshot_requested
417416

418417
__bool__ = __nonzero__
419418

@@ -423,9 +422,9 @@ def _execute_saving(self):
423422
return
424423

425424
size_x, size_y = self._plot._window.get_size()
426-
size = size_x*size_y*4*sizeof(c_ubyte)
427-
image = create_string_buffer(size)
428-
glReadPixels(0, 0, size_x, size_y, GL_RGBA, GL_UNSIGNED_BYTE, image)
425+
size = size_x*size_y*4*ctypes.sizeof(ctypes.c_ubyte)
426+
image = ctypes.create_string_buffer(size)
427+
pgl.glReadPixels(0, 0, size_x, size_y, pgl.GL_RGBA, pgl.GL_UNSIGNED_BYTE, image)
429428
from PIL import Image
430429
im = Image.frombuffer('RGBA', (size_x, size_y),
431430
image.raw, 'raw', 'RGBA', 0, 1)

sympy/plotting/pygletplot/plot_axes.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from __future__ import print_function, division
22

3-
from pyglet.gl import *
3+
import pyglet.gl as pgl
44
from pyglet import font
55

6-
from plot_object import PlotObject
7-
from util import strided_range, billboard_matrix
8-
from util import get_direction_vectors
9-
from util import dot_product, vec_sub, vec_mag
106
from sympy.core import S
11-
from sympy.core.compatibility import is_sequence, range
7+
from sympy.core.compatibility import is_sequence
8+
from sympy.plotting.pygletplot.plot_object import PlotObject
9+
from sympy.plotting.pygletplot.util import billboard_matrix, dot_product, \
10+
get_direction_vectors, strided_range, vec_mag, vec_sub
1211

1312

1413
class PlotAxes(PlotObject):
@@ -89,20 +88,21 @@ def reset_bounding_box(self):
8988

9089
def draw(self):
9190
if self._render_object:
92-
glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT | GL_DEPTH_BUFFER_BIT)
91+
pgl.glPushAttrib(pgl.GL_ENABLE_BIT | pgl.GL_POLYGON_BIT | pgl.GL_DEPTH_BUFFER_BIT)
9392
if self._overlay:
94-
glDisable(GL_DEPTH_TEST)
93+
pgl.glDisable(pgl.GL_DEPTH_TEST)
9594
self._render_object.draw()
96-
glPopAttrib()
95+
pgl.glPopAttrib()
9796

9897
def adjust_bounds(self, child_bounds):
9998
b = self._bounding_box
10099
c = child_bounds
101100
for i in [0, 1, 2]:
102101
if abs(c[i][0]) is S.Infinity or abs(c[i][1]) is S.Infinity:
103102
continue
104-
b[i][0] = [min([b[i][0], c[i][0]]), c[i][0]][b[i][0] is None]
105-
b[i][1] = [max([b[i][1], c[i][1]]), c[i][1]][b[i][1] is None]
103+
b[i][0] = c[i][0] if b[i][0] is None else min([b[i][0], c[i][0]])
104+
b[i][1] = c[i][1] if b[i][1] is None else max([b[i][1], c[i][1]])
105+
self._bounding_box = b
106106
self._recalculate_axis_ticks(i)
107107

108108
def _recalculate_axis_ticks(self, axis):
@@ -153,22 +153,22 @@ def draw_text(self, text, position, color, scale=1.0):
153153
valign=font.Text.BASELINE,
154154
halign=font.Text.CENTER)
155155

156-
glPushMatrix()
157-
glTranslatef(*position)
156+
pgl.glPushMatrix()
157+
pgl.glTranslatef(*position)
158158
billboard_matrix()
159159
scale_factor = 0.005 * scale
160-
glScalef(scale_factor, scale_factor, scale_factor)
161-
glColor4f(0, 0, 0, 0)
160+
pgl.glScalef(scale_factor, scale_factor, scale_factor)
161+
pgl.glColor4f(0, 0, 0, 0)
162162
label.draw()
163-
glPopMatrix()
163+
pgl.glPopMatrix()
164164

165165
def draw_line(self, v, color):
166166
o = self._p._origin
167-
glBegin(GL_LINES)
168-
glColor3f(*color)
169-
glVertex3f(v[0][0] + o[0], v[0][1] + o[1], v[0][2] + o[2])
170-
glVertex3f(v[1][0] + o[0], v[1][1] + o[1], v[1][2] + o[2])
171-
glEnd()
167+
pgl.glBegin(pgl.GL_LINES)
168+
pgl.glColor3f(*color)
169+
pgl.glVertex3f(v[0][0] + o[0], v[0][1] + o[1], v[0][2] + o[2])
170+
pgl.glVertex3f(v[1][0] + o[0], v[1][1] + o[1], v[1][2] + o[2])
171+
pgl.glEnd()
172172

173173

174174
class PlotAxesOrdinate(PlotAxesBase):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy