AIM-Harvard / pyradiomics

Open-source python package for the extraction of Radiomics features from 2D and 3D images and binary masks. Support: https://discourse.slicer.org/c/community/radiomics
http://pyradiomics.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.14k stars 492 forks source link

Save results json/csv #769

Closed lauracbell closed 2 years ago

lauracbell commented 2 years ago

Thank you for the easy to read documents with installation and jupyter notebook examples. A simple question is how can I save the results as a json/csv file in interactive use? (I see the flag for command line use.) I've tried json.dump but the ordered dictionary isn't all serializable. If I need to loop over each key, value pair and save, then I can do this, but I feel like I'm missing something simple.

lauracbell commented 2 years ago

I did the below in the end based on searching through the existing code. I'd suggest to adding this to the jupyter notebook example as it may be helpful to others (who are also relatively new to python).

import json import numpy as np

class NumpyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) with open('result.json', 'w') as f: json.dump(result, f, cls=NumpyEncoder, indent=2, sort_keys=True)