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

Rascal json encoder #363

Open
agoscinski opened this issue Jun 15, 2021 · 4 comments · May be fixed by #366
Open

Rascal json encoder #363

agoscinski opened this issue Jun 15, 2021 · 4 comments · May be fixed by #366
Assignees
Labels
upstream The issue is caused by one of the project's dependencies

Comments

@agoscinski
Copy link
Contributor

agoscinski commented Jun 15, 2021

Because of

ASE changed it's JSON structure format in ~3.16. the format we use is so simplistic that we did not write a dedicated function to do it. The ASE reader on the other hand is backward compatible

one cannot simply use ase to transform the files to json
I used the ase json encoder as basis to make a rascal encoder
https://gitlab.com/ase/ase/blob/master/ase/io/jsonio.py#L25-27

I could put this a bit more cleaned to the python utils. Please give thumbs up if you think this is good or comment if you think we should solve the problem in a different way

import ase.io
import numpy as np
import json

json_frames = {}
frames = ase.io.read('/home/alexgo/datasets/methane.extxyz', ':2')
for frame in frames:
    frame.cell = [50, 50, 50]
    frame.center()

class RascalEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, 'todict'):
            d = obj.todict()

            if not isinstance(d, dict):
                raise RuntimeError('todict() of {} returned object of type {} '
                                   'but should have returned dict'
                                   .format(obj, type(d)))
            if hasattr(obj, 'ase_objtype'):
                d['__ase_objtype__'] = obj.ase_objtype

            return d
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        if isinstance(obj, np.integer):
            return int(obj)
        if isinstance(obj, np.bool_):
            return bool(obj)
        if isinstance(obj, datetime.datetime):
            return {'__datetime__': obj.isoformat()}
        if isinstance(obj, complex):
            return {'__complex__': (obj.real, obj.imag)}
        return json.JSONEncoder.default(self, obj)

for i, frame in enumerate(frames):
    json_frames[str(i)] = json.loads(json.dumps(frame, cls=RascalEncoder))

json_frames['ids'] = [i for i in range(len(frames))]
json_frames['nextid'] = len(frames)
with open('/home/alexgo/datasets/methane_test.json', 'w') as f:
    json.dump(json_frames, f, indent=2)
@agoscinski agoscinski added the upstream The issue is caused by one of the project's dependencies label Jun 15, 2021
@agoscinski agoscinski self-assigned this Jun 15, 2021
@ceriottm
Copy link
Contributor

ceriottm commented Jun 15, 2021 via email

@agoscinski
Copy link
Contributor Author

While we are doing this, I think there's a more transparent way to encode
nparrays, if I'm not mistaken. I find this tolist() a bit burdensome

I am not sure what issues appear with tolist() ?

@ceriottm
Copy link
Contributor

ceriottm commented Jun 18, 2021 via email

@agoscinski
Copy link
Contributor Author

The first 4 answers all use tolist()

        if isinstance(obj, np.ndarray):
            return obj.tolist()

@agoscinski agoscinski linked a pull request Jun 18, 2021 that will close this issue
14 tasks
@agoscinski agoscinski linked a pull request Jun 30, 2021 that will close this issue
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream The issue is caused by one of the project's dependencies
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants