Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Error about JSONEncode #93

Open
yuzl340822 opened this issue Mar 31, 2021 · 8 comments
Open

A Error about JSONEncode #93

yuzl340822 opened this issue Mar 31, 2021 · 8 comments
Labels
Needinfo More info required to solve this

Comments

@yuzl340822
Copy link

yuzl340822 commented Mar 31, 2021

I met a Error when running sopare/numpyjsonencoder. py. There are the code and the Error.
`
class numpyjsonencoder(json.JSONEncoder):

def default(self, obj):
    if isinstance(obj, numpy.ndarray):
        if obj.flags['C_CONTIGUOUS']:
            obj_data = obj.data
        else:
            cont_obj = numpy.ascontiguousarray(obj)
            if (cont_obj.flags['C_CONTIGUOUS']):
                obj_data = cont_obj.data
            else:
                raise Exception("numpyjsonencoder err: C_CONTIGUOUS not present in object!")
        data_base64 = base64.b64encode(obj_data)
        return dict(__ndarray__= data_base64, dtype = str(obj.dtype), shape = obj.shape)
    return json.JSONEncoder.default(self, obj)`

Traceback (most recent call last): File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap self.run() File "/home/pi/dev/sopare/sopare/worker.py", line 125, in run self.util.store_raw_dict_entry(self.cfg.getoption('cmdlopt', 'dict'), self.raw_character) File "/home/pi/dev/sopare/sopare/util.py", line 108, in store_raw_dict_entry json.dump(json_obj, json_file, cls=sopare.numpyjsonencoder.numpyjsonencoder) File "/usr/lib/python3.7/json/__init__.py", line 179, in dump for chunk in iterable: File "/usr/lib/python3.7/json/encoder.py", line 431, in _iterencode yield from _iterencode_dict(o, _current_indent_level) File "/usr/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict yield from chunks File "/usr/lib/python3.7/json/encoder.py", line 325, in _iterencode_list yield from chunks File "/usr/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict yield from chunks File "/usr/lib/python3.7/json/encoder.py", line 439, in _iterencode yield from _iterencode(o, _current_indent_level) File "/usr/lib/python3.7/json/encoder.py", line 431, in _iterencode yield from _iterencode_dict(o, _current_indent_level) File "/usr/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict yield from chunks File "/usr/lib/python3.7/json/encoder.py", line 438, in _iterencode o = _default(o) File "/home/pi/dev/sopare/sopare/numpyjsonencoder.py", line 38, in default return json.JSONEncoder.default(self, obj) File "/usr/lib/python3.7/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} 'TypeError: Object of type bytes is not JSON serializable
What's wrong with this code? I use python3 from raspberry Pi.

@bluesailor45
Copy link

Hi,
I get the same error when running sopare with python3.7. As I have seen in the project description sopare is only tested in python2.7 environment. Try running sopare unser python2.7.
I hope sopare will be updated to run also under python 3.
Maybe we should open a issue ?!

@bluesailor45
Copy link

Hi,
sopare should run under python3 - see issue: sopare in python3. After downloading/cloning new project see also new dependencies described in readme file in spare-testing directory.
However: I have also installed this new files and installed the dependencies, but still got errors and was not able to run sopare (I am still checking why) - In another raspberry environment I was able to run sopare under python3 however. I will check what are the differences. If you are successful, please comment.

@bishoph
Copy link
Owner

bishoph commented Dec 10, 2021

When you get errors you should post the error messages. I got several positive feedback that SOPARE runs with Python 3 incl. from myself :)

@bluesailor45
Copy link

bluesailor45 commented Dec 10, 2021 via email

@bluesailor45
Copy link

Hi,
when running sopare.py (1.5.6) under python3.7 I get the following:
sopare is recording wav file, but then it closes with error:
Process worker for filtered data:
Traceback (most recent call last):
File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/home/pi/sopare/sopare/worker.py", line 125, in run
self.util.store_raw_dict_entry(self.cfg.getoption('cmdlopt', 'dict'), self.raw_character)
File "/home/pi/sopare/sopare/util.py", line 108, in store_raw_dict_entry
json.dump(json_obj, json_file, cls=sopare.numpyjsonencoder.numpyjsonencoder)
File "/usr/lib/python3.7/json/init.py", line 179, in dump
for chunk in iterable:
File "/usr/lib/python3.7/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.7/json/encoder.py", line 325, in _iterencode_list
yield from chunks
File "/usr/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.7/json/encoder.py", line 439, in _iterencode
yield from _iterencode(o, _current_indent_level)
File "/usr/lib/python3.7/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.7/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/home/pi/sopare/sopare/numpyjsonencoder.py", line 38, in default
return json.JSONEncoder(self, obj)
TypeError: init() takes 1 positional argument but 3 were given
INFO:sopare.processing:stop append mode because of silence
INFO:sopare.buffering:stop buffering
INFO:sopare.buffering:terminating queue runner
INFO:sopare.recorder:Buffering not alive, stop recording

When running under python2.7 the following:

Capturing wav file runs OK, but when trying o create dictionary I get error: (see following)
........
.13777675301913467, 0.15378435347814873, 0.14845224802850057, 0.14580411949117664, 0.13977172468490975, 0.07205208052782061], 'token_peaks': []}
DEBUG:sopare.worker:meta = [{'token': 'stop'}]
INFO:sopare.recorder:Buffering not alive, stop recording
INFO:sopare.recorder:stop endless recording

pi@raspberrypi:~/sopare $ python2 sopare.py -c
sopare 1.5.6
recreating dictionary from raw input files...
Traceback (most recent call last):
File "sopare.py", line 187, in
main(sys.argv[1:])
File "sopare.py", line 100, in main
recreate_dict(debug, cfg)
File "sopare.py", line 130, in recreate_dict
utilities.recreate_dict_from_raw_files()
File "/home/pi/sopare/sopare/util.py", line 195, in recreate_dict_from_raw_files
self.writeDICT(self.getCompiledDict())
File "/home/pi/sopare/sopare/util.py", line 156, in getCompiledDict
json_obj = json.load(raw_json_file, object_hook=sopare.numpyjsonencoder.numpyjsonhook)
File "/usr/lib/python2.7/json/init.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/init.py", line 352, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I am at a loss!

Thank you for help.

@bishoph
Copy link
Owner

bishoph commented Dec 17, 2021

A user reported (the same?) issue and made some fixes:
cahein@5860115

Not sure if this solves the issue but it's worth a test.

To verify and fix this issue one need to be able to reproduce it. Please provide a step by step description and some background information about used OS/versions etc. (there are at least two Python versions in the comments).

@bishoph bishoph added the Needinfo More info required to solve this label Dec 17, 2021
@bluesailor45
Copy link

Hi Martin,
thank you very much for the tip. This fix is really working and made the code with python3.7 working.
Thanks a lot for this great product!

@bishoph
Copy link
Owner

bishoph commented Dec 19, 2021

You are welcome. Good to know that the fix is working. I need to install and setup a clean system to be able to verify and fix the issue without breaking Python 2 support ... maybe when and if there is some time between the holidays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needinfo More info required to solve this
Projects
None yet
Development

No branches or pull requests

3 participants