Skip to content

API update for OpenDX and MRC#164

Open
spyke7 wants to merge 4 commits intoMDAnalysis:masterfrom
spyke7:api_update
Open

API update for OpenDX and MRC#164
spyke7 wants to merge 4 commits intoMDAnalysis:masterfrom
spyke7:api_update

Conversation

@spyke7
Copy link

@spyke7 spyke7 commented Mar 11, 2026

This is regarding #161

Implemented from_grid() a static method and native (property) inside OpenDX.py and mrc.py

  • Just simply added those two functions inside files
  • register converters
  • change export
  • write tests

@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 81.53846% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.60%. Comparing base (b29c1f4) to head (304abaf).

Files with missing lines Patch % Lines
gridData/OpenDX.py 81.81% 20 Missing and 10 partials ⚠️
gridData/core.py 57.14% 3 Missing ⚠️
gridData/mrc.py 86.95% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
+ Coverage   88.20%   88.60%   +0.40%     
==========================================
  Files           5        5              
  Lines         814      834      +20     
  Branches      107      108       +1     
==========================================
+ Hits          718      739      +21     
- Misses         56       57       +1     
+ Partials       40       38       -2     

☔ View full report in Codecov by Sentry.
📢 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.

@spyke7
Copy link
Author

spyke7 commented Mar 16, 2026

@orbeckst pls check this kindly

Copy link
Member

@orbeckst orbeckst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already looking pretty good. Please also add doc strings and update CHANGELOG. Will have a closer look once this is done. Thank you!

@spyke7 spyke7 changed the title API update from OpenDX and MRC API update for OpenDX and MRC Mar 19, 2026
@spyke7 spyke7 requested a review from orbeckst March 19, 2026 15:09

assert isinstance(dx_field, gridData.OpenDX.field)

assert any('test_density' and 'test_user' in str(c) for c in dx_field.comments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a logic error. 'test_density' and 'test_user' in str(c) evaluates as 'test_user' in str(c) due to Python's and short-circuit semantics — 'test_density' is truthy (not None / empty string), so it's ignored. Should be:

assert any('test_density' in str(c) for c in dx_field.comments)                                                                 
assert any('test_user' in str(c) for c in dx_field.comments)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually to get what you are doing there, you might need

assert any(('test_density' in str(c) and 'test_user' in str(c)) for c in dx_field.comments)

If you need them both to be true for a single comment.


converter = {
'MRC': mrc.MRC.from_grid,
'VDB': None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VDB is going to be implemented but it should be removed for now. Currently the self.converter[]() has potential to try and call None() so best to just not include until the VDB support is added (or add a guard for such in the later usage).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can comment out this line.

grid, edges = g.histogramdd()
self._load(grid=grid, edges=edges, metadata=self.metadata)

def convert_to(self, format_specifier, tolerance=None, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tolerance is currently not used at all. Should be removed and left for the PR that introduces VDB.

if filename is not None:
self.read(filename, assume_volumetric=assume_volumetric)

@staticmethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be classmethods instead of staticmethods?

Copy link
Author

@spyke7 spyke7 Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the issue it was staticmethod and it's fine for basic usage as well. But classmethod is more good, if somebody subclasses mrc/dx
Should I change it?

Copy link
Member

@BradyAJohnston BradyAJohnston Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have thought it makes more sense to make it classmethod, but @orbeckst was specific about it being static method in the issue. @orbeckst I'm interested why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did't know any better ;-). After some reading I also recommend going with @classmethod — if we need to work with inheritance or use it from another classmethod then classmethod will work better.

Copy link
Member

@orbeckst orbeckst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looking good but there are some necessary changes. In particular, the MRC.native should return a MrcFile.

Could you please also add a section to the gridData.core docs about using from_grid() and .native? Have a look at the issue text for #161 and you can probably copy and paste from there. We just want to document somewhere for users that this API exists.

* 1.1.1

Fixes
Changes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhancements (not changes)

??/??/???? orbeckst
??/??/???? orbeckst, spyke7

* 1.1.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to 1.2.0 (under semantic versioning, we must increase minor version for new features)

Returns
-------
field
OpenDX field wrapper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a .. versionadded:: 1.2.0 (with two blank lines above)

Comment on lines +568 to +570
grid : Grid
type : str, optional
typequote : str, optional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add minimal explanations (can copy from other docs)


@property
def native(self):
"""Return native object"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Add a sentence "The "native" object is the :class:gridData.OpenDX.field itself."
  • add a .. versionadded:: 1.2.0 (with two blank lines above)

assert isinstance(dx_field, gridData.OpenDX.field)

assert any('test_density' and 'test_user' in str(c) for c in dx_field.comments)
# assert any('test_user' in str(c) for c in dx_field.comments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove commented out code

Comment on lines +107 to +108
assert_equal(dx_field.components['data'].array, data)
assert_equal(dx_field.components['positions'].origin, g.origin)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These may be floats so use assert_allclose.

g = Grid(data, origin=[0, 0, 0], delta=[1, 1, 1])

dx_field = gridData.OpenDX.field.from_grid(g)
assert dx_field.native is dx_field No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks that it's the same object. Add another assertion that checks the type explicitly

assert isinstance(dx_field.native, OpenDX.field)


mrc_obj = mrc.MRC.from_grid(g)

assert mrc_obj.native is mrc_obj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to change as the native object should be the mrcfile.MrcFile.

  • check type
  • check some values


assert isinstance(mrc_obj, mrc.MRC)

assert_equal(mrc_obj.array, data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the data are integers, assert_equal is technically correct but for consistency and robustness, also use assert_allclose, please.

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.

3 participants