irreducible-representations / irrep

GNU General Public License v3.0
62 stars 31 forks source link

JSON output error: Object of type complex is not JSON serializable #33

Closed bfocassio closed 3 years ago

bfocassio commented 3 years ago

Dear all,

I'm using irrep 1.6.0 from the cli. Everything runs fine as it should up o the end when the code tries to dumps the output as a .json file. The code throws the following error:

Traceback (most recent call last):
  File "/home/bruno.focassio/.local/lib/python3.7/site-packages/monty/json.py", line 178, in default
    d = o.as_dict()
AttributeError: 'complex' object has no attribute 'as_dict'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/bruno.focassio/anaconda3/bin/irrep", line 8, in <module>
    sys.exit(cli())
  File "/home/bruno.focassio/anaconda3/lib/python3.7/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/site-packages/irrep/cli.py", line 58, in invoke
    return super(LoadContextFromConfig, self).invoke(ctx)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/site-packages/irrep/cli.py", line 436, in cli
    dumpfn(json_data,"irrep-output.json",indent=4)
  File "/home/bruno.focassio/.local/lib/python3.7/site-packages/monty/serialization.py", line 123, in dumpfn
    fp.write("%s" % json.dumps(obj, *args, **kwargs))
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 325, in _iterencode_list
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 325, in _iterencode_list
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 439, in _iterencode
    yield from _iterencode(o, _current_indent_level)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 325, in _iterencode_list
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 325, in _iterencode_list
    yield from chunks
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/home/bruno.focassio/.local/lib/python3.7/site-packages/monty/json.py", line 192, in default
    return json.JSONEncoder.default(self, o)
  File "/home/bruno.focassio/anaconda3/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type complex is not JSON serializable

By inspecting the characters and characters_refUC keys inside the json_data dict, complex numbers need to be encoded.

mkhorton commented 3 years ago

Hi @bfocassio,

I would be happy to take a look at this issue. The error suggests a problem serializing complex(number) datatypes, which is handled by the monty package. This package does support complex datatypes as of v3.0.4, so it should be working. I suspect there will be a straight forward fix.

Could you provide an example (eg input files and the command you're running)? And also verify which version of monty you have installed?

bfocassio commented 3 years ago

@mkhorton as you suggested, everything works just fine with version v2021.8.17 of monty. I tested the Bi-hoti example.

Thank you very much.

mkhorton commented 3 years ago

Glad it worked out!