-
Notifications
You must be signed in to change notification settings - Fork 22
Description
For some formats (MRC, VDB – see PR #148) GridDataFormats uses external packages to load data into a native data structure. At the moment, GDF is focused on file-level interoperability by reading files in one format and writing it in another. API level interoperability would allow directly working with objects instead of files. This approach is more efficient in workflows and can make internal code cleaner.
MDAnalysis has the converter modules and in #148 (comment) we raised the idea to do something similar in GDF.
Converting a Grid object to a native object: Grid.convert_to()
Add a method Grid.convert_to(format_specifier) -> native object that generates an instance of the native object. For example, directly create a mrcfile.mrcfile.MrcFile or a OpenVDB Grid
g = gdf.Grid("density.dx") # -> gdf.Grid
mrc = g.convert_to("mrc") # -> mrcfile.mrcfile.MrcFile
v = g.convert_to("vdb") # -> openvdb.GridBase (eg a FloatGrid)Once we have this functionality, Grid.export() functions can be simplified to conversion and using the native write method.
Power-users can directly use the native instance.
Creating a Grid from a native object: overloading Grid.__init__()
We also want to directly work with native objects. We can overload Grid.__init__(grid: str or ndarray or object = None) to also accept e.g. mrcfile.mrcfile.MrcFile or an OpenVDB Grid such as openvdb.FloatGrid.
For example
mrc = mrcfile.mrcfile.MrcFile("density.ccp4")
g = Grid(mrc)Alternatively, we could create a static method Grid.from(obj, **kwargs), which would look like
g = Grid.from(mrc)TODO
The two parts of the API interoperability are independent and can be tackled separately and we may just open separate issues for them:
- producing a native object from a Grid:
Grid.convert_to() - loading a Grid from a native object:
Grid.__init__()orGrid.from()