Skip to content

Conversation

@Pradyumn-cloud
Copy link

@Pradyumn-cloud Pradyumn-cloud commented Dec 9, 2025

Fix Issue - #108

Add MRC file writing support

  • Implement MRC.write() method
  • Add Grid.export() integration for MRC format
  • Added Test
  • Preserve header information and handle edge cases

@codecov
Copy link

codecov bot commented Dec 9, 2025

Codecov Report

❌ Patch coverage is 93.44262% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.13%. Comparing base (abd0feb) to head (18a2192).

Files with missing lines Patch % Lines
gridData/mrc.py 92.30% 0 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #147      +/-   ##
==========================================
+ Coverage   87.70%   88.13%   +0.43%     
==========================================
  Files           5        5              
  Lines         748      809      +61     
  Branches       97      105       +8     
==========================================
+ Hits          656      713      +57     
  Misses         56       56              
- Partials       36       40       +4     

☔ 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.

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.

Looks good at first glance but I am not an MRC expert. I'll see if I can get someone else to also have a look.

In the meantime, please also

  • update your branch with the latest changes on master (just did that)
  • add yourself to AUTHORS
  • add an entry to CHANGELOG
  • check your coverage and add tests where necessary

gridData/mrc.py Outdated
header information (including mapc, mapr, maps ordering) is preserved.
Otherwise, standard ordering (mapc=1, mapr=2, maps=3) is used.
.. versionadded:: 0.8.0
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.1.0 (next minor release)

@orbeckst
Copy link
Member

orbeckst commented Dec 9, 2025

@marinegor do you have some experience with MRC files and would you be able to have a very brief look at this PR?

@orbeckst orbeckst requested a review from marinegor December 9, 2025 16:34
@orbeckst orbeckst self-assigned this Dec 9, 2025
Copy link

@marinegor marinegor left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the PR!

I've added couple of comments regarding self.header vs self._mrc_header, and regarding using with open(...).

Also, perhaps it's a good idea to add a small test MRC (say, 4x4x4 with random numbers) file with/without a header that tests things? This way we can actually track regressions if something changes in mrcfile itself, while a roundtrip test won't check that.

Comment on lines +705 to +716
mrc_file = mrc.MRC()
mrc_file.array = self.grid
mrc_file.delta = numpy.diag(self.delta)
mrc_file.origin = self.origin
mrc_file.rank = 3

# Transfer header if it exists (preserves axis ordering and other metadata)
if hasattr(self, '_mrc_header'):
mrc_file.header = self._mrc_header

# Write to file
mrc_file.write(filename)

Choose a reason for hiding this comment

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

I feel like this should be replaced with with mrcfile.new(filename) as mrc: ... block, no? Generally writing to files without using context manager is bad practice.

Copy link
Author

Choose a reason for hiding this comment

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

_export_mrc() method uses MRC.write() which properly uses mrcfile.new( filename)
context manager is used -> in mrc.py line (194)

Comment on lines +174 to +175
# File was read - preserve original ordering
h = self.header

Choose a reason for hiding this comment

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

why is it self.header and not self._mrc_header as in core.py?

Copy link
Author

Choose a reason for hiding this comment

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

core stores headers as _mrc_header . When exporting, we assign mrc_file.header = self._mrc_header, giving the MRC instance a header attribute. MRC.write() then finds self.header and uses it. its like different class different name.

Copy link
Author

Choose a reason for hiding this comment

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

i will provide a test mrc as you mention in some time.

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.

Some minor fixes required and please address @marinegor 's comments. That should also help with test coverage.

CHANGELOG Outdated
Comment on lines 21 to 26
* Added MRC file writing support (Issue #108)
* Implemented MRC.write() method with proper coordinate transformations
* Added Grid.export() integration for MRC format
* Preserved header information (mapc/mapr/maps, nstart, cell dimensions)
* Added comprehensive tests

Copy link
Member

Choose a reason for hiding this comment

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

Too much information, focus on user-facing information, just

Suggested change
* Added MRC file writing support (Issue #108)
* Implemented MRC.write() method with proper coordinate transformations
* Added Grid.export() integration for MRC format
* Preserved header information (mapc/mapr/maps, nstart, cell dimensions)
* Added comprehensive tests
* Added MRC file writing support (Issue #108)

is sufficient.

CHANGELOG Outdated
Comment on lines 16 to 20
12/12/2025 Pradyumn-cloud
* 1.1.0

Changes

Copy link
Member

Choose a reason for hiding this comment

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

All of your changes should go into the existing 1.1.0 section below. Leave the date as ??/??/???? and just

gridData/core.py Outdated
information (including axis ordering) is preserved
* For new grids, standard ordering (mapc=1, mapr=2, maps=3) is used
.. versionadded:: 0.8.0
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.1.0

Did asked changes in changelog and core and added more test
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