-
Notifications
You must be signed in to change notification settings - Fork 21
Add MRC file writing support #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
81e425b
1af054a
1bd6e09
469c30b
38b5f4b
18a2192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,7 +79,7 @@ class MRC(object): | |
| ----- | ||
| * Only volumetric (3D) densities are read. | ||
| * Only orthorhombic unitcells supported (other raise :exc:`ValueError`) | ||
| * Only reading is currently supported. | ||
| * Reading and writing are supported. | ||
|
|
||
|
|
||
| .. versionadded:: 0.7.0 | ||
|
|
@@ -148,5 +148,106 @@ def histogramdd(self): | |
| """Return array data as (edges,grid), i.e. a numpy nD histogram.""" | ||
| return (self.array, self.edges) | ||
|
|
||
|
|
||
|
|
||
| def write(self, filename): | ||
| """Write grid data to MRC/CCP4 file format. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| filename : str | ||
| Output filename for the MRC file | ||
|
|
||
| Notes | ||
| ----- | ||
| The data array should be in xyz order (axis 0=X, axis 1=Y, axis 2=Z). | ||
|
|
||
| If the MRC object was created by reading an existing file, the original | ||
| header information (including mapc, mapr, maps ordering) is preserved. | ||
| Otherwise, standard ordering (mapc=1, mapr=2, maps=3) is used. | ||
|
|
||
orbeckst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .. versionadded:: 1.1.0 | ||
| """ | ||
| if filename is not None: | ||
| self.filename = filename | ||
|
|
||
| # Preserve header if it exists, otherwise use defaults | ||
| if hasattr(self, 'header'): | ||
| # File was read - preserve original ordering | ||
| h = self.header | ||
|
Comment on lines
+174
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is it
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. core stores headers as
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i will provide a test mrc as you mention in some time. |
||
| axes_order = np.hstack([h.mapc, h.mapr, h.maps]) | ||
| mapc, mapr, maps = int(h.mapc), int(h.mapr), int(h.maps) | ||
| else: | ||
| # New file - use standard ordering | ||
| axes_order = np.array([1, 2, 3]) | ||
| mapc, mapr, maps = 1, 2, 3 | ||
| h = None | ||
|
|
||
| # Reverse the transformation done in read() | ||
| transpose_order = np.argsort(axes_order[::-1]) | ||
| inverse_transpose_order = np.argsort(transpose_order) | ||
|
|
||
| # Transform our xyz array back to the file's native ordering | ||
| data_for_file = np.transpose(self.array, axes=inverse_transpose_order) | ||
|
|
||
| # Ensure proper data type (float32 is standard for mode 2) | ||
orbeckst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data_for_file = data_for_file.astype(np.float32) | ||
|
|
||
| # Create new MRC file | ||
| with mrcfile.new(filename, overwrite=True) as mrc: | ||
| mrc.set_data(data_for_file) | ||
|
|
||
| # Set voxel size from delta (diagonal elements) | ||
| voxel_size = np.diag(self.delta).astype(np.float32) | ||
| mrc.voxel_size = tuple(voxel_size) | ||
|
|
||
| # Set map ordering | ||
| mrc.header.mapc = mapc | ||
| mrc.header.mapr = mapr | ||
| mrc.header.maps = maps | ||
|
|
||
| # Handle nstart and origin | ||
| if h is not None: | ||
| # Preserve original header values | ||
| nxstart = int(h.nxstart) | ||
| nystart = int(h.nystart) | ||
| nzstart = int(h.nzstart) | ||
| header_origin_xyz = np.array([h.origin.x, h.origin.y, h.origin.z], dtype=np.float32) | ||
|
|
||
| mrc.header.mx = int(h.mx) | ||
| mrc.header.my = int(h.my) | ||
| mrc.header.mz = int(h.mz) | ||
|
|
||
| # Preserve cell dimensions | ||
| if hasattr(h, 'cella'): | ||
| mrc.header.cella.x = float(h.cella.x) | ||
| mrc.header.cella.y = float(h.cella.y) | ||
| mrc.header.cella.z = float(h.cella.z) | ||
| if hasattr(h, 'cellb'): | ||
| mrc.header.cellb.alpha = float(h.cellb.alpha) | ||
| mrc.header.cellb.beta = float(h.cellb.beta) | ||
| mrc.header.cellb.gamma = float(h.cellb.gamma) | ||
| # Copy space group if available | ||
| if hasattr(h, 'ispg'): | ||
| mrc.header.ispg = int(h.ispg) | ||
| else: | ||
| # For new files, calculate nstart from origin | ||
| if np.any(voxel_size <= 0): | ||
| raise ValueError(f"Voxel size must be positive, got {voxel_size}") | ||
|
|
||
| # Set header.origin = 0 and encode everything in nstart | ||
| header_origin_xyz = np.zeros(3, dtype=np.float32) | ||
| nxstart = int(np.round(self.origin[0] / voxel_size[0])) | ||
| nystart = int(np.round(self.origin[1] / voxel_size[1])) | ||
| nzstart = int(np.round(self.origin[2] / voxel_size[2])) | ||
|
|
||
| # Set the start positions | ||
| mrc.header.nxstart = nxstart | ||
| mrc.header.nystart = nystart | ||
| mrc.header.nzstart = nzstart | ||
|
|
||
| # Set explicit origin | ||
| mrc.header.origin.x = float(header_origin_xyz[0]) | ||
| mrc.header.origin.y = float(header_origin_xyz[1]) | ||
| mrc.header.origin.z = float(header_origin_xyz[2]) | ||
|
|
||
| # Update statistics only | ||
| mrc.update_header_stats() | ||
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_export_mrc()method usesMRC.write()which properly uses mrcfile.new( filename)context manager is used -> in mrc.py line (194)