pathpy / pathpyG

GPU-accelerated Next-Generation Network Analytics and Graph Learning for Time Series Data on Complex Networks.
https://www.pathpy.net
GNU Affero General Public License v3.0
33 stars 3 forks source link

Bug in plot function for temporal networks #224

Closed FranziskHeeg closed 1 week ago

FranziskHeeg commented 1 week ago

Plot function for temporal networks is not working, when network is created by pp.TemporalGraph.from_edge_list() with timestamps and nodes as integers.

This works:

Screenshot 2024-11-14 at 11 55 10

This does not work:

Screenshot 2024-11-14 at 11 53 49

This works again:

Screenshot 2024-11-14 at 11 54 20
hackl commented 1 week ago

Seems json cannot encode numpy values. See https://stackoverflow.com/questions/50916422/python-typeerror-object-of-type-int64-is-not-json-serializable

Solution:

import json
import numpy as np

class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        if isinstance(obj, np.floating):
            return float(obj)
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return super(NpEncoder, self).default(obj)

# Your codes .... 
json.dumps(data, cls=NpEncoder)