Skip to content

Commit 15e2f93

Browse files
authored
Merge pull request #678 from Mathics3/complete_docs_for_bytearray_changed_modules
doc for required modules
2 parents fb98f3c + 68eeaa7 commit 15e2f93

File tree

5 files changed

+341
-152
lines changed

5 files changed

+341
-152
lines changed

mathics/builtin/binary/bytearray.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,24 @@
44
"""
55

66
from mathics.builtin.base import Builtin
7-
from mathics.core.atoms import (
8-
ByteArrayAtom,
9-
Integer,
10-
String,
11-
)
7+
from mathics.core.atoms import ByteArrayAtom, Integer, String
128
from mathics.core.convert.expression import to_mathics_list
139
from mathics.core.expression import Expression
14-
from mathics.core.systemsymbols import (
15-
SymbolByteArray,
16-
SymbolFailed,
17-
)
10+
from mathics.core.systemsymbols import SymbolByteArray, SymbolFailed
1811

1912

2013
class ByteArray(Builtin):
2114
r"""
15+
<url>:WMA link:
16+
https://reference.wolfram.com/language/ref/ByteArray.html</url>
17+
2218
<dl>
2319
<dt>'ByteArray[{$b_1$, $b_2$, ...}]'
24-
<dd> Represents a sequence of Bytes $b_1$, $b_2$, ...
20+
<dd> Represents a sequence of Bytes $b_1$, $b_2$, ...
2521
2622
<dt>'ByteArray["string"]'
27-
<dd> Constructs a byte array where bytes comes from decode a b64 encoded String
23+
<dd> Constructs a byte array where bytes comes from decode a b64-encoded \
24+
String
2825
</dl>
2926
3027
>> A=ByteArray[{1, 25, 3}]
@@ -49,7 +46,7 @@ class ByteArray(Builtin):
4946
}
5047
summary_text = "array of bytes"
5148

52-
def apply_str(self, string, evaluation):
49+
def eval_str(self, string, evaluation):
5350
"ByteArray[string_String]"
5451
try:
5552
atom = ByteArrayAtom(string.value)
@@ -58,15 +55,15 @@ def apply_str(self, string, evaluation):
5855
return SymbolFailed
5956
return Expression(SymbolByteArray, atom)
6057

61-
def apply_to_str(self, baa, evaluation):
58+
def eval_to_str(self, baa, evaluation):
6259
"ToString[ByteArray[baa_ByteArrayAtom]]"
6360
return String('ByteArray["' + baa.__str__() + '"]')
6461

65-
def apply_normal(self, baa, evaluation):
62+
def eval_normal(self, baa, evaluation):
6663
"System`Normal[ByteArray[baa_ByteArrayAtom]]"
6764
return to_mathics_list(*baa.value, elements_conversion_fn=Integer)
6865

69-
def apply_list(self, values, evaluation):
66+
def eval_list(self, values, evaluation):
7067
"ByteArray[values_List]"
7168
if not values.has_form("List", None):
7269
return

mathics/builtin/colors/color_operations.py

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,12 @@
88

99
import itertools
1010
from math import floor
11+
1112
from mathics.builtin.base import Builtin
12-
from mathics.builtin.colors.color_directives import (
13-
_ColorObject,
14-
ColorError,
15-
RGBColor,
16-
)
13+
from mathics.builtin.colors.color_directives import ColorError, RGBColor, _ColorObject
1714
from mathics.builtin.colors.color_internals import convert_color
18-
from mathics.builtin.drawing.image import _ImageBuiltin, Image
19-
20-
from mathics.core.atoms import (
21-
Integer,
22-
MachineReal,
23-
Rational,
24-
Real,
25-
)
15+
from mathics.builtin.drawing.image import Image, _ImageBuiltin
16+
from mathics.core.atoms import Integer, MachineReal, Rational, Real
2617
from mathics.core.convert.expression import to_expression, to_mathics_list
2718
from mathics.core.expression import Expression
2819
from mathics.core.list import ListExpression
@@ -32,8 +23,8 @@
3223
_image_requires = ("numpy", "PIL")
3324

3425
try:
35-
import PIL.ImageOps
3626
import numpy
27+
import PIL.ImageOps
3728

3829
_enabled = True
3930
except ImportError:
@@ -42,15 +33,19 @@
4233

4334
class Blend(Builtin):
4435
"""
36+
<url>:WMA link:https://reference.wolfram.com/language/ref/Blend.html</url>
37+
4538
<dl>
46-
<dt>'Blend[{$c1$, $c2$}]'
47-
<dd>represents the color between $c1$ and $c2$.
48-
<dt>'Blend[{$c1$, $c2$}, $x$]'
49-
<dd>represents the color formed by blending $c1$ and $c2$ with
50-
factors 1 - $x$ and $x$ respectively.
51-
<dt>'Blend[{$c1$, $c2$, ..., $cn$}, $x$]'
52-
<dd>blends between the colors $c1$ to $cn$ according to the
53-
factor $x$.
39+
<dt>'Blend[{$c1$, $c2$}]'
40+
<dd>represents the color between $c1$ and $c2$.
41+
42+
<dt>'Blend[{$c1$, $c2$}, $x$]'
43+
<dd>represents the color formed by blending $c1$ and $c2$ with
44+
factors 1 - $x$ and $x$ respectively.
45+
46+
<dt>'Blend[{$c1$, $c2$, ..., $cn$}, $x$]'
47+
<dd>blends between the colors $c1$ to $cn$ according to the
48+
factor $x$.
5449
</dl>
5550
5651
>> Blend[{Red, Blue}]
@@ -109,7 +104,7 @@ def do_blend(self, colors, values):
109104
result = [r + p for r, p in zip(result, part)]
110105
return type(components=result)
111106

112-
def apply(self, colors, u, evaluation):
107+
def eval(self, colors, u, evaluation):
113108
"Blend[{colors___}, u_]"
114109

115110
colors_orig = colors
@@ -154,10 +149,13 @@ def apply(self, colors, u, evaluation):
154149

155150
class ColorConvert(Builtin):
156151
"""
152+
<url>:WMA link:
153+
https://reference.wolfram.com/language/ref/ColorConvert.html</url>
154+
157155
<dl>
158-
<dt>'ColorConvert[$c$, $colspace$]'
159-
<dd>returns the representation of $c$ in the color space $colspace$. $c$
160-
may be a color or an image.
156+
<dt>'ColorConvert[$c$, $colspace$]'
157+
<dd>returns the representation of $c$ in the color space $colspace$. $c$ \
158+
may be a color or an image.
161159
</dl>
162160
163161
Valid values for $colspace$ are:
@@ -178,15 +176,15 @@ class ColorConvert(Builtin):
178176
}
179177
summary_text = "convert between color models"
180178

181-
def apply(self, input, colorspace, evaluation):
179+
def eval(self, input, colorspace, evaluation):
182180
"ColorConvert[input_, colorspace_String]"
183181

184182
if isinstance(input, Image):
185183
return input.color_convert(colorspace.get_string_value())
186184
else:
187185
from mathics.builtin.colors.color_directives import (
188-
expression_to_color,
189186
color_to_expression,
187+
expression_to_color,
190188
)
191189

192190
py_color = expression_to_color(input)
@@ -208,6 +206,8 @@ def apply(self, input, colorspace, evaluation):
208206

209207
class ColorNegate(_ImageBuiltin):
210208
"""
209+
<url>:WMA link:https://reference.wolfram.com/language/ref/ColorNegate.html</url>
210+
211211
<dl>
212212
<dt>'ColorNegate[$image$]'
213213
<dd>returns the negative of $image$ in which colors have been negated.
@@ -223,7 +223,7 @@ class ColorNegate(_ImageBuiltin):
223223

224224
summary_text = "the negative color of a given color"
225225

226-
def apply_for_color(self, color, evaluation):
226+
def eval_for_color(self, color, evaluation):
227227
"ColorNegate[color_RGBColor]"
228228
# Get components
229229
r, g, b = [element.to_python() for element in color.elements]
@@ -232,13 +232,15 @@ def apply_for_color(self, color, evaluation):
232232
# Reconstitute
233233
return Expression(SymbolRGBColor, Real(r), Real(g), Real(b))
234234

235-
def apply_for_image(self, image, evaluation):
235+
def eval_for_image(self, image, evaluation):
236236
"ColorNegate[image_Image]"
237237
return image.filter(lambda im: PIL.ImageOps.invert(im))
238238

239239

240240
class Darker(Builtin):
241241
"""
242+
<url>:WMA link:https://reference.wolfram.com/language/ref/Darker.html</url>
243+
242244
<dl>
243245
<dt>'Darker[$c$, $f$]'
244246
<dd>is equivalent to 'Blend[{$c$, Black}, $f$]'.
@@ -262,23 +264,33 @@ class Darker(Builtin):
262264

263265
class DominantColors(_ImageBuiltin):
264266
"""
267+
<url>:WMA link:https://reference.wolfram.com/language/ref/DominantColors.html</url>
268+
265269
<dl>
266-
<dt>'DominantColors[$image$]'
270+
<dt>'DominantColors[$image$]'
267271
<dd>gives a list of colors which are dominant in the given image.
268-
<dt>'DominantColors[$image$, $n$]'
272+
273+
<dt>'DominantColors[$image$, $n$]'
269274
<dd>returns at most $n$ colors.
270-
<dt>'DominantColors[$image$, $n$, $prop$]'
271-
<dd>returns the given property $prop$, which may be "Color" (return RGB colors), "LABColor" (return
272-
LAB colors), "Count" (return the number of pixels a dominant color covers), "Coverage" (return the
273-
fraction of the image a dominant color covers), or "CoverageImage" (return a black and white image
274-
indicating with white the parts that are covered by a dominant color).
275+
276+
<dt>'DominantColors[$image$, $n$, $prop$]'
277+
<dd>returns the given property $prop$, which may be:
278+
<ul>
279+
<li>"Color": return RGB colors,
280+
<li> "LABColor": return LAB colors,
281+
<li> "Count": return the number of pixels a dominant color covers,
282+
<li> "Coverage": return the fraction of the image a dominant color \
283+
covers, or
284+
<li> "CoverageImage": return a black and white image indicating with \
285+
white the parts that are covered by a dominant color.
286+
</ul>
275287
</dl>
276288
277-
The option "ColorCoverage" specifies the minimum amount of coverage needed to include a dominant color
278-
in the result.
289+
The option "ColorCoverage" specifies the minimum amount of coverage needed to \
290+
include a dominant color in the result.
279291
280-
The option "MinColorDistance" specifies the distance (in LAB color space) up to which colors are merged
281-
and thus regarded as belonging to the same dominant color.
292+
The option "MinColorDistance" specifies the distance (in LAB color space) up \
293+
to which colors are merged and thus regarded as belonging to the same dominant color.
282294
283295
>> img = Import["ExampleData/lena.tif"]
284296
= -Image-
@@ -316,7 +328,7 @@ class DominantColors(_ImageBuiltin):
316328
options = {"ColorCoverage": "Automatic", "MinColorDistance": "Automatic"}
317329
summary_text = "find a list of dominant colors"
318330

319-
def apply(self, image, n, prop, evaluation, options):
331+
def eval(self, image, n, prop, evaluation, options):
320332
"DominantColors[image_Image, n_Integer, prop_String, OptionsPattern[%(name)s]]"
321333

322334
py_prop = prop.get_string_value()
@@ -379,9 +391,9 @@ def apply(self, image, n, prop, evaluation, options):
379391
num_pixels = im.size[0] * im.size[1]
380392

381393
from mathics.algorithm.clusters import (
382-
agglomerate,
383-
PrecomputedDistances,
384394
FixedDistanceCriterion,
395+
PrecomputedDistances,
396+
agglomerate,
385397
)
386398

387399
norm = numpy.linalg.norm
@@ -416,7 +428,7 @@ def result():
416428
elif py_prop == "Coverage":
417429
yield Rational(int(count), num_pixels)
418430
elif py_prop == "CoverageImage":
419-
mask = numpy.ndarray(shape=pixels.shape, dtype=numpy.bool)
431+
mask = numpy.ndarray(shape=pixels.shape, dtype=bool)
420432
mask.fill(0)
421433
for i in members:
422434
mask = mask | (pixels == i)
@@ -433,6 +445,8 @@ def result():
433445

434446
class Lighter(Builtin):
435447
"""
448+
<url>:WMA link:https://reference.wolfram.com/language/ref/Lighter.html</url>
449+
436450
<dl>
437451
<dt>'Lighter[$c$, $f$]'
438452
<dd>is equivalent to 'Blend[{$c$, White}, $f$]'.

0 commit comments

Comments
 (0)