3D line error in Sagemath 9.0

58 views
Skip to first unread message

Donald Munro

unread,
Jan 3, 2020, 7:52:03 AM1/3/20
to sage-devel
Running:
from sage.plot.plot3d.shapes2 import Line
X = Line([(-1, 0, 0), (0, 0, 0), (1, 0, 0)], color='red')
Y = Line([(0, -1, 0), (0, 0, 0), (0, -1,0)], color='green')
Z = Line([(0, 0, -1), (0, 0, 0), (0, 0, -1)], color='green')
show(X+Y+Z, figsize=8)

results in a
TypeError: Object of type Integer is not JSON serializable

The above works on 8.9 (tested using  docker run -p8888:8888 sagemath/sagemath:8.9 sage-jupyter).

Error occurred on Arch Linux distribution using SageMath 9.0 and Python 3.8.1.

The full error trace is:
TypeError                                 Traceback (most recent call last)
<ipython-input-1-f86e4a3173d9> in <module>
      3 Y = Line([(Integer(0), -Integer(1), Integer(0)), (Integer(0), Integer(0), Integer(0)), (Integer(0), -Integer(1),Integer(0))], color='green')
      4 Z = Line([(Integer(0), Integer(0), -Integer(1)), (Integer(0), Integer(0), Integer(0)), (Integer(0), Integer(0), -Integer(1))], color='green')
----> 5 show(X+Y+Z, figsize=Integer(8))

/usr/lib/python3.8/site-packages/sage/repl/rich_output/pretty_print.py in show(*args, **kwds)
    256         args[0].show()
    257         return
--> 258     pretty_print(*args, **kwds)

/usr/lib/python3.8/site-packages/sage/repl/rich_output/pretty_print.py in pretty_print(*args, **kwds)
    227             pass
    228         elif len(args) == 1:
--> 229             dm.display_immediately(*args, **kwds)
    230         else:
    231             SequencePrettyPrinter(*args, **kwds).pretty_print()

/usr/lib/python3.8/site-packages/sage/repl/rich_output/display_manager.py in display_immediately(self, obj, **rich_repr_kwds)
    835             1/2
    836         """
--> 837         plain_text, rich_output = self._rich_output_formatter(obj, rich_repr_kwds)
    838         self._backend.display_immediately(plain_text, rich_output)
    839 

/usr/lib/python3.8/site-packages/sage/repl/rich_output/display_manager.py in _rich_output_formatter(self, obj, rich_repr_kwds)
    623         has_rich_repr = isinstance(obj, SageObject) and hasattr(obj, '_rich_repr_')
    624         if has_rich_repr:
--> 625             rich_output = self._call_rich_repr(obj, rich_repr_kwds)
    626         if isinstance(rich_output, OutputPlainText):
    627             plain_text = rich_output

/usr/lib/python3.8/site-packages/sage/repl/rich_output/display_manager.py in _call_rich_repr(self, obj, rich_repr_kwds)
    581         if rich_repr_kwds:
    582             # do not ignore errors from invalid options
--> 583             return obj._rich_repr_(self, **rich_repr_kwds)
    584         try:
    585             return obj._rich_repr_(self)

/usr/lib/python3.8/site-packages/sage/plot/plot3d/base.pyx in sage.plot.plot3d.base.Graphics3d._rich_repr_ (build/cythonized/sage/plot/plot3d/base.c:5483)()
    149         ### Second, return the corresponding graphics file
    150         if viewer == 'threejs':
--> 151             return self._rich_repr_threejs(**opts)
    152         elif viewer == 'jmol':
    153             return self._rich_repr_jmol(**opts)

/usr/lib/python3.8/site-packages/sage/plot/plot3d/base.pyx in sage.plot.plot3d.base.Graphics3d._rich_repr_threejs (build/cythonized/sage/plot/plot3d/base.c:9653)()
    409                 thickness = p._extra_kwds.get('thickness', 1)
    410                 lines.append('{{"points":{}, "color":"{}", "opacity":{}, "linewidth":{}}}'.format(
--> 411                              json.dumps(p.points), color, opacity, thickness))
    412             if hasattr(p, '_trans'):
    413                 m = p.get_transformation().get_matrix()

/usr/lib/python3.8/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    229         cls is None and indent is None and separators is None and
    230         default is None and not sort_keys and not kw):
--> 231         return _default_encoder.encode(obj)
    232     if cls is None:
    233         cls = JSONEncoder

/usr/lib/python3.8/json/encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

/usr/lib/python3.8/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/usr/lib/python3.8/json/encoder.py in default(self, o)
    177 
    178         """
--> 179         raise TypeError(f'Object of type {o.__class__.__name__} '
    180                         f'is not JSON serializable')
    181 

TypeError: Object of type Integer is not JSON serializable

Dima Pasechnik

unread,
Jan 3, 2020, 8:21:01 AM1/3/20
to sage-devel
On Fri, Jan 3, 2020 at 8:52 PM Donald Munro <donal...@gmail.com> wrote:
>
> Running:
>>
>> from sage.plot.plot3d.shapes2 import Line
>> X = Line([(-1, 0, 0), (0, 0, 0), (1, 0, 0)], color='red')
>> Y = Line([(0, -1, 0), (0, 0, 0), (0, -1,0)], color='green')
>> Z = Line([(0, 0, -1), (0, 0, 0), (0, 0, -1)], color='green')
>> show(X+Y+Z, figsize=8)
>
>
> results in a
>>
>> TypeError: Object of type Integer is not JSON serializable

the problem is that Sage's type Integer cannot be used in Line()
You'd need to use Python types, e.g. int(), float(), etc
E.g.

Line([(int(-1), int(0), int(0)), (int(0), int(0), int(0)), (int(1),
int(0), int(0))], color='red')

This appears to be a bug, not sure about its origin :-(
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/4a7cb74f-0250-4dc8-9bb8-7af3d9d904b6%40googlegroups.com.

Dima Pasechnik

unread,
Jan 3, 2020, 9:24:01 AM1/3/20
to sage-devel
I've opened https://trac.sagemath.org/ticket/28949

to deal with this problem.

Donald Munro

unread,
Jan 3, 2020, 9:51:00 AM1/3/20
to sage-devel
Dima Pasechnik wrote:
On Fri, Jan 3, 2020 at 8:52 PM Donald Munro <donal...@gmail.com> wrote:
>
> Running:
>>
>> from sage.plot.plot3d.shapes2 import Line
>> X = Line([(-1, 0, 0), (0, 0, 0), (1, 0, 0)], color='red')
>> Y = Line([(0, -1, 0), (0, 0, 0), (0, -1,0)], color='green')
>> Z = Line([(0, 0, -1), (0, 0, 0), (0, 0, -1)], color='green')
>> show(X+Y+Z, figsize=8)
>
>
> results in a
>>
>> TypeError: Object of type Integer is not JSON serializable

the problem is that Sage's type Integer cannot be used in Line()
You'd need to use Python types, e.g. int(), float(), etc
E.g.

Line([(int(-1), int(0), int(0)), (int(0), int(0), int(0)), (int(1),

Thanks,  replacing the above code with
def to_float(v):
    vv = [None] * len(v)
    for i in range(0, len(v)):
        vv[i] = float(v[i])
    return vv   
from sage.plot.plot3d.shapes2 import Line
X = Line([to_float((-1, 0, 0)), to_float((0, 0, 0)), to_float((1, 0, 0))], color='red')
Y = Line([to_float((0, -1, 0)), to_float((0, 0, 0)), to_float((0, -1,0))], color='green')
Z = Line([to_float((0, 0, -1)), to_float((0, 0, 0)), to_float((0, 0, -1))], color='green')
show(X+Y+Z, figsize=8)
 
 does solve the problem  or at least provide a work-around.

Samuel Lelievre

unread,
Jan 3, 2020, 11:50:55 AM1/3/20
to sage-devel
A simpler workaround is to use `line3d` instead of `Line`:

sage: from sage.plot.plot3d.shapes2 import line3d
sage: X = line3d([(-1, 0, 0), (0, 0, 0), (1, 0, 0)], color='red')
sage: Y = line3d([(0, -1, 0), (0, 0, 0), (0, -1,0)], color='green')
sage: Z = line3d([(0, 0, -1), (0, 0, 0), (0, 0, -1)], color='green')
sage: G = X + Y + Z
sage: G.show(figsize=8) 
Reply all
Reply to author
Forward
0 new messages