Skip to content

Commit 8c49882

Browse files
committed
Formatting
1 parent 77885a4 commit 8c49882

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

mathics/builtin/drawing/plot.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from mathics.core.evaluation import Evaluation
2727
from mathics.core.expression import Expression
2828
from mathics.core.list import ListExpression
29-
from mathics.eval.nevaluator import eval_N
3029
from mathics.core.symbols import Symbol, SymbolList
3130
from mathics.core.systemsymbols import (
3231
SymbolAll,
@@ -55,6 +54,7 @@
5554
get_plot_range,
5655
get_plot_range_option,
5756
)
57+
from mathics.eval.nevaluator import eval_N
5858

5959
# The vectorized plot function generates GraphicsComplex using NumericArray,
6060
# which no consumer will currently understand. So lets make it opt-in for now.
@@ -64,9 +64,19 @@
6464
# TODO: work out exactly how to deploy.
6565
use_vectorized_plot = os.getenv("MATHICS3_USE_VECTORIZED_PLOT", False)
6666
if use_vectorized_plot:
67-
from mathics.eval.drawing.plot3d_vectorized import eval_DensityPlot, eval_Plot3D, eval_ComplexPlot, eval_ComplexPlot3D
67+
from mathics.eval.drawing.plot3d_vectorized import (
68+
eval_ComplexPlot,
69+
eval_ComplexPlot3D,
70+
eval_DensityPlot,
71+
eval_Plot3D,
72+
)
6873
else:
69-
from mathics.eval.drawing.plot3d import eval_DensityPlot, eval_Plot3D, eval_ComplexPlot, eval_ComplexPlot3D
74+
from mathics.eval.drawing.plot3d import (
75+
eval_ComplexPlot,
76+
eval_ComplexPlot3D,
77+
eval_DensityPlot,
78+
eval_Plot3D,
79+
)
7080

7181
# This tells documentation how to sort this module
7282
# Here we are also hiding "drawing" since this erroneously appears at the top level.
@@ -467,12 +477,14 @@ def __init__(self, expr, range_exprs, options, evaluation):
467477
range = [range_expr.elements[0]]
468478
for limit_expr in range_expr.elements[1:3]:
469479
limit = eval_N(limit_expr, evaluation).to_python()
470-
if not isinstance(limit, (int,float,complex)):
480+
if not isinstance(limit, (int, float, complex)):
471481
self.error(expr, "plln", limit_expr, range_expr)
472482
range.append(limit)
473-
if isinstance(limit, (int,float)) and range[2] <= range[1]:
483+
if isinstance(limit, (int, float)) and range[2] <= range[1]:
474484
self.error(expr, "invrange", range_expr)
475-
if isinstance(limit, complex) and (range[2].real <= range[1].real or range[2].imag <= range[1].imag):
485+
if isinstance(limit, complex) and (
486+
range[2].real <= range[1].real or range[2].imag <= range[1].imag
487+
):
476488
self.error(expr, "invrange", range_expr)
477489
self.ranges.append(range)
478490

@@ -621,11 +633,12 @@ def eval(
621633
graphics = self.eval_function(plot_options, evaluation)
622634
if not graphics:
623635
return
624-
graphics_expr = graphics.generate(options_to_rules(options, self.graphics_class.options))
636+
graphics_expr = graphics.generate(
637+
options_to_rules(options, self.graphics_class.options)
638+
)
625639
return graphics_expr
626640

627641

628-
629642
class BarChart(_Chart):
630643
"""
631644
<url>:WMA link: https://reference.wolfram.com/language/ref/BarChart.html</url>
@@ -749,19 +762,16 @@ class ColorDataFunction(Builtin):
749762

750763

751764
class ComplexPlot3D(_Plot3D):
752-
753765
summary_text = "plots one or more complex functions as a surface"
754766
expected_args = 2
755-
options = _Plot3D.options3d | {
756-
"Mesh": "None"
757-
}
767+
options = _Plot3D.options3d | {"Mesh": "None"}
758768

759769
many_functions = True
760770
eval_function = staticmethod(eval_ComplexPlot3D)
761771
graphics_class = Graphics3D
762772

773+
763774
class ComplexPlot(_Plot3D):
764-
765775
summary_text = "plots a complex function showing amplitude and phase using colors"
766776
expected_args = 2
767777
options = _Plot3D.options2d

mathics/eval/drawing/plot3d_vectorized.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def compute_over_grid(nx, ny):
7171
if not is_complex:
7272
zs = function(**{str(names[0]): xs, str(names[1]): ys})
7373
else:
74-
cs = xs + ys * 1j # TODO: fast enough?
74+
cs = xs + ys * 1j # TODO: fast enough?
7575
zs = function(**{str(names[0]): cs})
7676

7777
# sometimes expr gets compiled into something that returns a complex
@@ -103,7 +103,6 @@ def compute_over_grid(nx, ny):
103103
# pass the xyzs and quads back to the caller to add colors and emit quads as appropriate
104104
emit(graphics, i, xyzs, quads)
105105

106-
107106
# If requested by the Mesh attribute create a mesh of lines covering the surfaces
108107
# For now only for Plot3D
109108
# TODO: mesh for DensityPlot?
@@ -131,7 +130,6 @@ def eval_Plot3D(
131130
evaluation: Evaluation,
132131
):
133132
def emit(graphics, i, xyzs, quads):
134-
135133
# color-blind friendly palette from https://davidmathlogic.com/colorblind
136134
palette = [
137135
(255, 176, 0), # orange
@@ -148,11 +146,10 @@ def emit(graphics, i, xyzs, quads):
148146
rgb = [c / 255.0 for c in rgb]
149147
# graphics.add_color(SymbolRGBColor, rgb)
150148
graphics.add_directives([SymbolRGBColor, *rgb])
151-
149+
152150
# add a GraphicsComplex displaying a surface for this function
153151
graphics.add_complex(xyzs, lines=None, polys=quads)
154152

155-
156153
return make_plot(plot_options, evaluation, dim=3, is_complex=False, emit=emit)
157154

158155

@@ -162,7 +159,6 @@ def eval_DensityPlot(
162159
evaluation: Evaluation,
163160
):
164161
def emit(graphics, i, xyzs, quads):
165-
166162
# Fixed palette for now
167163
# TODO: accept color options
168164
with Timer("compute colors"):
@@ -184,10 +180,9 @@ def emit(graphics, i, xyzs, quads):
184180

185181
@Timer("complex colors")
186182
def complex_colors(zs, s=None):
187-
188183
# hue depends on phase
189184
h = np.angle(zs, deg=True) / 360
190-
185+
191186
# saturation depends on magnitude
192187
if s is None:
193188
zabs = abs(zs)
@@ -196,10 +191,10 @@ def complex_colors(zs, s=None):
196191
s = (zabs - zmin) / (zmax - zmin)
197192
else:
198193
s = np.full(zs.shape, s)
199-
194+
200195
# brightness is constant
201196
b = np.full(zs.shape, 1.0)
202-
197+
203198
# convert to rgb
204199
hsb = np.array([h, s, b]).T
205200
rgb = convert_color(hsb, "HSB", "RGB", False)
@@ -213,23 +208,24 @@ def eval_ComplexPlot3D(
213208
evaluation: Evaluation,
214209
):
215210
def emit(graphics, i, xyzs, quads):
216-
zs = xyzs[:,2]
211+
zs = xyzs[:, 2]
217212
rgb = complex_colors(zs, s=0.8)
218-
xyzs[:,2] = abs(zs)
219-
graphics.add_complex(xyzs.astype(float), lines=None, polys=quads, colors=rgb)
213+
xyzs[:, 2] = abs(zs)
214+
graphics.add_complex(xyzs.astype(float), lines=None, polys=quads, colors=rgb)
220215

221216
return make_plot(plot_options, evaluation, dim=3, is_complex=True, emit=emit)
222217

223218

224-
225219
@Timer("eval_ComplexPlot")
226220
def eval_ComplexPlot(
227221
plot_options,
228222
evaluation: Evaluation,
229223
):
230224
def emit(graphics, i, xyzs, quads):
231225
# flatten the points and add the quads
232-
rgb = complex_colors(xyzs[:,2])
233-
graphics.add_complex(xyzs[:, 0:2].astype(float), lines=None, polys=quads, colors=rgb)
226+
rgb = complex_colors(xyzs[:, 2])
227+
graphics.add_complex(
228+
xyzs[:, 0:2].astype(float), lines=None, polys=quads, colors=rgb
229+
)
234230

235231
return make_plot(plot_options, evaluation, dim=2, is_complex=True, emit=emit)

0 commit comments

Comments
 (0)