Skip to content

Commit e85aa19

Browse files
authored
Merge pull request #680 from Mathics3/ByteArray-box
Make ByteArray more WMA compatible...
2 parents 15e2f93 + 2e42f1d commit e85aa19

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

mathics/builtin/binary/bytearray.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class ByteArray(Builtin):
2525
</dl>
2626
2727
>> A=ByteArray[{1, 25, 3}]
28-
= ByteArray["ARkD"]
28+
= ByteArray[<3>]
2929
>> A[[2]]
3030
= 25
3131
>> Normal[A]
3232
= {1, 25, 3}
3333
>> ToString[A]
34-
= ByteArray["ARkD"]
34+
= ByteArray[<3>]
3535
>> ByteArray["ARkD"]
36-
= ByteArray["ARkD"]
36+
= ByteArray[<3>]
3737
>> B=ByteArray["asy"]
3838
: The first argument in Bytearray[asy] should be a B64 enconded string or a vector of integers.
3939
= $Failed
@@ -57,7 +57,7 @@ def eval_str(self, string, evaluation):
5757

5858
def eval_to_str(self, baa, evaluation):
5959
"ToString[ByteArray[baa_ByteArrayAtom]]"
60-
return String('ByteArray["' + baa.__str__() + '"]')
60+
return String(f"ByteArray[<{len(baa.value)}>]")
6161

6262
def eval_normal(self, baa, evaluation):
6363
"System`Normal[ByteArray[baa_ByteArrayAtom]]"

mathics/builtin/drawing/image.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@
2424
colorspaces as known_colorspaces,
2525
convert_color,
2626
)
27-
from mathics.builtin.drawing.image_internals import (
28-
convolve,
29-
matrix_to_numpy,
30-
numpy_flip,
31-
numpy_to_matrix,
32-
pixels_as_float,
33-
pixels_as_ubyte,
34-
pixels_as_uint,
35-
)
3627
from mathics.core.atoms import (
3728
Atom,
3829
Integer,
@@ -48,6 +39,15 @@
4839
from mathics.core.list import ListExpression
4940
from mathics.core.symbols import Symbol, SymbolDivide, SymbolNull, SymbolTrue
5041
from mathics.core.systemsymbols import SymbolRule, SymbolSimplify
42+
from mathics.eval.image import (
43+
convolve,
44+
matrix_to_numpy,
45+
numpy_flip,
46+
numpy_to_matrix,
47+
pixels_as_float,
48+
pixels_as_ubyte,
49+
pixels_as_uint,
50+
)
5151

5252
SymbolColorQuantize = Symbol("ColorQuantize")
5353
SymbolImage = Symbol("Image")
@@ -1582,9 +1582,9 @@ def _linearize(a):
15821582
u = numpy.unique(a)
15831583
n = len(u)
15841584

1585-
lower = numpy.ndarray(a.shape, dtype=numpy.int)
1585+
lower = numpy.ndarray(a.shape, dtype=int)
15861586
lower.fill(0)
1587-
upper = numpy.ndarray(a.shape, dtype=numpy.int)
1587+
upper = numpy.ndarray(a.shape, dtype=int)
15881588
upper.fill(n - 1)
15891589

15901590
h = numpy.sort(u)
@@ -1713,7 +1713,7 @@ def apply(self, image, stype, evaluation):
17131713
elif stype == "Bit16":
17141714
pixels = pixels_as_uint(pixels)
17151715
elif stype == "Bit":
1716-
pixels = pixels.astype(numpy.int)
1716+
pixels = pixels.astype(int)
17171717
else:
17181718
return evaluation.message("ImageData", "pixelfmt", stype)
17191719
return from_python(numpy_to_matrix(pixels))

mathics/builtin/list/eol.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ def eval(self, list, i, evaluation):
986986
return
987987
indices = i.get_sequence()
988988
# How to deal with ByteArrays
989-
if list.get_head_name() == "System`ByteArray":
989+
if list.get_head() is SymbolByteArray:
990990
list = list.evaluate(evaluation)
991991
if len(indices) > 1:
992992
print(
@@ -995,8 +995,8 @@ def eval(self, list, i, evaluation):
995995
)
996996
return
997997
idx = indices[0]
998-
if idx.get_head_name() == "System`Integer":
999-
idx = idx.get_int_value()
998+
if isinstance(idx, Integer):
999+
idx = idx.value
10001000
if idx == 0:
10011001
return SymbolByteArray
10021002
data = list.elements[0].value

mathics/core/atoms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,14 @@ def __hash__(self):
561561
def __str__(self) -> str:
562562
return base64.b64encode(self.value).decode("utf8")
563563

564+
# FIXME: the below does not use the "f" parameter to
565+
# change behavior between FullForm and OutputForm
566+
# Below we have the OutputForm behavior.
567+
# A refactoring should be done so that this routine
568+
# is removed and the form makes decisions, rather than
569+
# have this routine know everything about all forms.
564570
def atom_to_boxes(self, f, evaluation) -> "String":
565-
res = String('""' + self.__str__() + '""')
571+
res = String(f"<{len(self.value)}>")
566572
return res
567573

568574
def do_copy(self) -> "ByteArrayAtom":

mathics/builtin/drawing/image_internals.py renamed to mathics/eval/image.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# -*- coding: utf-8 -*-
22

3-
"""helper functions for images
43
"""
5-
6-
# Signals to Mathics doc processing not to include this module in its documentation.
7-
no_doc = True
8-
4+
helper functions for images
5+
"""
96

107
import numpy
118

@@ -63,7 +60,7 @@ def pixels_as_float(pixels):
6360
return pixels.astype(numpy.float32) / 255.0
6461
elif dtype == numpy.uint16:
6562
return pixels.astype(numpy.float32) / 65535.0
66-
elif dtype == bool:
63+
elif dtype is numpy.dtype(bool):
6764
return pixels.astype(numpy.float32)
6865
else:
6966
raise NotImplementedError
@@ -78,7 +75,7 @@ def pixels_as_ubyte(pixels):
7875
return pixels
7976
elif dtype == numpy.uint16:
8077
return (pixels / 256).astype(numpy.uint8)
81-
elif dtype == numpy.bool:
78+
elif dtype is numpy.dtype(bool):
8279
return pixels.astype(numpy.uint8) * 255
8380
else:
8481
raise NotImplementedError
@@ -93,7 +90,7 @@ def pixels_as_uint(pixels):
9390
return pixels.astype(numpy.uint16) * 256
9491
elif dtype == numpy.uint16:
9592
return pixels
96-
elif dtype == numpy.bool:
93+
elif dtype is numpy.dtype(bool):
9794
return pixels.astype(numpy.uint8) * 65535
9895
else:
9996
raise NotImplementedError

setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@
4747
elif sys.version_info[:2] == (3, 6):
4848
INSTALL_REQUIRES += [
4949
"recordclass",
50-
"numpy<1.24",
50+
"numpy",
5151
"llvmlite<0.37",
5252
"sympy>=1.8,<1.9",
5353
]
5454
if is_PyPy:
5555
print("Mathics does not support PyPy Python 3.6" % sys.version_info[:2])
5656
sys.exit(-1)
57-
elif sys.version_info[:2] == (3, 7):
58-
INSTALL_REQUIRES += ["numpy<1.22", "llvmlite", "sympy>=1.8, < 1.11"]
5957
else:
60-
INSTALL_REQUIRES += ["numpy<1.24", "llvmlite", "sympy>=1.8, < 1.11"]
58+
INSTALL_REQUIRES += ["numpy<=1.24", "llvmlite", "sympy>=1.8, < 1.11"]
6159

6260
if not is_PyPy:
6361
INSTALL_REQUIRES += ["recordclass"]

0 commit comments

Comments
 (0)