OpenFreeEnergy / gufe

grand unified free energy by OpenFE
https://gufe.readthedocs.io
MIT License
29 stars 8 forks source link

Non-primitive types in GufeTokenizable #79

Open richardjgowers opened 1 year ago

richardjgowers commented 1 year ago

Originally here: https://github.com/OpenFreeEnergy/gufe/pull/68#issuecomment-1284134284

It would be nice if this worked out of the box:

class Thing(gufe.tokenization.GufeTokenizable):
    def __init__(self, v):
        self.vals = v

    def _defaults(self):
        return {}

    def _to_dict(self):
        return {'vals': self.vals}

    @classmethod
    def _from_dict(cls, dct):
        return cls(**dct)

def test_thing():
    t = Thing(np.arange(5))

    dd = t.to_dict()

    t2 = Thing.from_dict(dd)

    assert t == t2

And also with pathlib and openff-units objects too. Then we also need to document properly how to extend this list of allowed types.

Currently most of the special casing in gufe tokenize is good at special casing gufe objects, but we also need to be able to handle foreign objects just as well. #68 probably adds a lot of the code we need for this (turns foreign objects into primitive)

dwhswenson commented 1 year ago

This was discussed in call today. Plan is to do a significant modification of normalize; we'll dump to json using sort_keys=True and using out custom serialization class, rather than making the string representation in tokenize. If an object can't be handled, it'll raise an error in json.dumps.