Skip to content

Create a Grid from a native mrcfile object - #186

Open
aviralgarg05 wants to merge 1 commit into
MDAnalysis:masterfrom
aviralgarg05:grid-from-mrcfile-object
Open

aviralgarg05 wants to merge 1 commit into
MDAnalysis:masterfrom
aviralgarg05:grid-from-mrcfile-object

Conversation

@aviralgarg05

Copy link
Copy Markdown

Fixes #172

Grid() now takes a native mrcfile object, not just a filename:

with mrcfile.open("density.mrc") as mrcobj:
    g = Grid(mrcobj)

The check is against mrcfile.mrcobject.MrcObject, so MrcFile and
MrcInterpreter both work, and so do subclasses like MrcMemmap and
GzipMrcFile.

Getting the data, header, origin and spacing out of an mrcfile object is
what reading a file already did, so I pulled that out of MRC.read()
into MRC._populate() and share it with a new classmethod
MRC.from_mrcfile(). That keeps the MRC-specific code in mrc.py and
leaves core.Grid with just the dispatch, along the lines of #162. Most
of the diff in mrc.py is that block moving one level out.

As discussed in the issue, the data are a view and are not copied. The
one caveat worth knowing is that a grid built from a memory-mapped
object loses its data when that object is closed; it is noted in the
docstring.

The interoperability docs still said loading from a native object was
not implemented, so I updated that section. It covers OpenVDB too, since
#170 landed that half already.

Tested with MrcFile, MrcMemmap, a gzipped file, MrcInterpreter,
and an object built in memory with no file behind it, plus the
non-volumetric, assume_volumetric and triclinic paths. A DX file
exported from a grid built this way is byte-identical to one exported
from the same file read from disk. Docs build clean.

Grid(grid=...) now also accepts the native objects of the mrcfile
library, i.e., anything derived from mrcfile.mrcobject.MrcObject, which
covers MrcFile, MrcInterpreter and subclasses such as MrcMemmap and
GzipMrcFile:

    with mrcfile.open("density.mrc") as mrcobj:
        g = Grid(mrcobj)

Pulling the data, header, origin and grid spacing out of an mrcfile
object is exactly what reading a file already did, so that part was
split out of MRC.read() into MRC._populate() and is now shared with the
new classmethod MRC.from_mrcfile(). The grid data are a view of the data
of the native object and are not copied, so a grid built this way needs
the native object to keep its data around.

The API interoperability docs still said that loading from a native
object was not implemented; they now describe it for both MRC and
OpenVDB.

Fixes MDAnalysis#172
@codecov

codecov Bot commented Nov 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.53%. Comparing base (313fde2) to head (bd44f45).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #186      +/-   ##
==========================================
+ Coverage   90.35%   90.53%   +0.18%     
==========================================
  Files           6        6              
  Lines         985     1004      +19     
  Branches      131      133       +2     
==========================================
+ Hits          890      909      +19     
  Misses         57       57              
  Partials       38       38              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 GridDataFormats | 🛠️ Build #35784811 | 📁 Comparing bd44f45 against latest (313fde2)

  🔍 Preview build  

7 files changed · ± 7 modified

± Modified

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a Grid from an mrcfile object

1 participant