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

save_history fails with TypeError #76

Closed
smoia opened this issue Aug 22, 2024 · 2 comments · Fixed by #77 or #79
Closed

save_history fails with TypeError #76

smoia opened this issue Aug 22, 2024 · 2 comments · Fixed by #77 or #79
Assignees
Labels
Bug Something isn't working

Comments

@smoia
Copy link
Member

smoia commented Aug 22, 2024

Expected Behavior

Export the history of hte physio object into a json file

Actual Behavior

Fails with TypeError:

In [20]: pk.save_history('sub-001_ses-01_task-rest_recording-ppg_physio.json', p)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 pk.save_history('sub-001_ses-01_task-rest_recording-ppg_physio.json', p)

File ~/Scrivania/gitlab/peakdet/peakdet/io.py:220, in save_history(file, data)
    218 file += ".json" if not file.endswith(".json") else ""
    219 with open(file, "w") as dest:
--> 220     json.dump(data.history, dest, indent=4)
    221 logger.info(f"Saved {data} history in {file}")
    223 return file

File /usr/lib/python3.10/json/__init__.py:179, in dump(obj, fp, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    173     iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
    174         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    175         separators=separators,
    176         default=default, sort_keys=sort_keys, **kw).iterencode(obj)
    177 # could accelerate with writelines in some versions of Python, at
    178 # a debuggability cost
--> 179 for chunk in iterable:
    180     fp.write(chunk)

File /usr/lib/python3.10/json/encoder.py:429, in _make_iterencode.<locals>._iterencode(o, _current_indent_level)
    427     yield _floatstr(o)
    428 elif isinstance(o, (list, tuple)):
--> 429     yield from _iterencode_list(o, _current_indent_level)
    430 elif isinstance(o, dict):
    431     yield from _iterencode_dict(o, _current_indent_level)

File /usr/lib/python3.10/json/encoder.py:325, in _make_iterencode.<locals>._iterencode_list(lst, _current_indent_level)
    323         else:
    324             chunks = _iterencode(value, _current_indent_level)
--> 325         yield from chunks
    326 if newline_indent is not None:
    327     _current_indent_level -= 1

File /usr/lib/python3.10/json/encoder.py:325, in _make_iterencode.<locals>._iterencode_list(lst, _current_indent_level)
    323         else:
    324             chunks = _iterencode(value, _current_indent_level)
--> 325         yield from chunks
    326 if newline_indent is not None:
    327     _current_indent_level -= 1

File /usr/lib/python3.10/json/encoder.py:405, in _make_iterencode.<locals>._iterencode_dict(dct, _current_indent_level)
    403         else:
    404             chunks = _iterencode(value, _current_indent_level)
--> 405         yield from chunks
    406 if newline_indent is not None:
    407     _current_indent_level -= 1

File /usr/lib/python3.10/json/encoder.py:325, in _make_iterencode.<locals>._iterencode_list(lst, _current_indent_level)
    323         else:
    324             chunks = _iterencode(value, _current_indent_level)
--> 325         yield from chunks
    326 if newline_indent is not None:
    327     _current_indent_level -= 1

File /usr/lib/python3.10/json/encoder.py:438, in _make_iterencode.<locals>._iterencode(o, _current_indent_level)
    436         raise ValueError("Circular reference detected")
    437     markers[markerid] = o
--> 438 o = _default(o)
    439 yield from _iterencode(o, _current_indent_level)
    440 if markers is not None:

File /usr/lib/python3.10/json/encoder.py:179, in JSONEncoder.default(self, o)
    160 def default(self, o):
    161     """Implement this method in a subclass such that it returns
    162     a serializable object for ``o``, or calls the base implementation
    163     (to raise a ``TypeError``).
   (...)
    177 
    178     """
--> 179     raise TypeError(f'Object of type {o.__class__.__name__} '
    180                     f'is not JSON serializable')

TypeError: Object of type int64 is not JSON serializable

With history:

In [21]: p.history
Out[21]: 
[('interpolate_physio', {'kind': 'linear', 'target_fs': 40}),
 ('filter_physio', {'cutoffs': 5, 'method': 'lowpass'}),
 ('peakfind_physio', {'dist': 1}),
 ('delete_peaks', {'remove': [58, 146, 179, 212, 259, 364, 389]}),
 ('add_peaks', {'add': [183]})]

@rmarkello I know you retired from this world, but if you could help us understand where the issue arises, that'd be awesome!

@smoia
Copy link
Member Author

smoia commented Aug 22, 2024

So #77 did not actually solve the issue.
The issue clearly pops up when the "add_peak" function runs, as the indexes created by it are np.int64.
I'll revert everything done.

@me-pic
Copy link
Contributor

me-pic commented Aug 22, 2024

@smoia Can take a look at this today if you need. I might have run into a similar issue at some point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
2 participants