File: _api.py

package info (click to toggle)
python-rosettasciio 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,644 kB
  • sloc: python: 36,638; xml: 2,582; makefile: 20; ansic: 4
file content (257 lines) | stat: -rw-r--r-- 7,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# -*- coding: utf-8 -*-
# Copyright 2007-2023 The HyperSpy developers
#
# This file is part of RosettaSciIO.
#
# RosettaSciIO is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RosettaSciIO is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RosettaSciIO. If not, see <https://www.gnu.org/licenses/#GPL>.

import logging

import mrcz as _mrcz
from packaging.version import Version

from rsciio._docstrings import (
    ENDIANESS_DOC,
    FILENAME_DOC,
    LAZY_DOC,
    MMAP_DOC,
    RETURNS_DOC,
    SIGNAL_DOC,
)
from rsciio.utils.tools import DTBox

_logger = logging.getLogger(__name__)


_POP_FROM_HEADER = [
    "compressor",
    "MRCtype",
    "C3",
    "dimensions",
    "dtype",
    "extendedBytes",
    "gain",
    "maxImage",
    "minImage",
    "meanImage",
    "metaId",
    "packedBytes",
    "pixelsize",
    "pixelunits",
    "voltage",
]
# Hyperspy uses an unusual mixed Fortran- and C-ordering scheme
_READ_ORDER = [1, 2, 0]
_WRITE_ORDER = [0, 1, 2]


# API changes in mrcz 0.5
def _parse_metadata(metadata):
    if Version(_mrcz.__version__) < Version("0.5"):
        return metadata[0]
    else:
        return metadata


mapping = {
    "mrcz_header.voltage": ("Acquisition_instrument.TEM.beam_energy", _parse_metadata),
    "mrcz_header.gain": (
        "Signal.Noise_properties.Variance_linear_model.gain_factor",
        _parse_metadata,
    ),
    # There is no metadata field for spherical aberration
    #'mrcz_header.C3':
    # ("Acquisition_instrument.TEM.C3", lambda x: x),
}


def file_reader(filename, lazy=False, mmap_mode="c", endianess="<", **kwds):
    """
    File reader for the MRCZ format for tomographic data.

    Parameters
    ----------
    %s
    %s
    %s
    %s
    **kwds : dict, optional
        The keyword arguments are passed to :py:func:`mrcz.readMRC`.

    %s

    Examples
    --------
    >>> from rsciio.mrcz import file_reader
    >>> new_signal = file_reader('file.mrcz')
    """
    _logger.debug("Reading MRCZ file: %s" % filename)

    if mmap_mode != "c":
        # Note also that MRCZ does not support memory-mapping of compressed data.
        # Perhaps we could use the zarr package for that
        raise ValueError("MRCZ supports only C-ordering memory-maps")

    mrcz_endian = "le" if endianess == "<" else "be"
    data, mrcz_header = _mrcz.readMRC(
        filename, endian=mrcz_endian, useMemmap=lazy, pixelunits="nm", **kwds
    )

    # Create the axis objects for each axis
    names = ["y", "x", "z"]
    navigate = [False, False, True]
    axes = [
        {
            "size": data.shape[hsIndex],
            "index_in_array": hsIndex,
            "name": names[index],
            "scale": mrcz_header["pixelsize"][hsIndex],
            "offset": 0.0,
            "units": mrcz_header["pixelunits"],
            "navigate": nav,
        }
        for index, (hsIndex, nav) in enumerate(zip(_READ_ORDER, navigate))
    ]
    axes.insert(0, axes.pop(2))  # re-order the axes

    metadata = mrcz_header.copy()
    # Remove non-standard fields
    for popTarget in _POP_FROM_HEADER:
        metadata.pop(popTarget)

    dictionary = {
        "data": data,
        "axes": axes,
        "metadata": metadata,
        "original_metadata": {"mrcz_header": mrcz_header},
        "mapping": mapping,
    }

    return [
        dictionary,
    ]


file_reader.__doc__ %= (
    FILENAME_DOC,
    LAZY_DOC,
    MMAP_DOC.replace(
        "incompatible).",
        "incompatible). The MRCZ reader currently only supports C-ordering memory-maps.",
    ),
    ENDIANESS_DOC,
    RETURNS_DOC,
)


def file_writer(
    filename,
    signal,
    endianess="<",
    do_async=False,
    compressor=None,
    clevel=1,
    n_threads=None,
):
    """
    Write signal to MRCZ format.

    Parameters
    ----------
    %s
    %s
    %s
    do_async : bool, Default=False
        Currently supported within RosettaSciIO for writing only, this will
        save the file in a background thread and return immediately.
        Warning: there is no method currently implemented within RosettaSciIO
        to tell if an asychronous write has finished.
    compressor : {None, "zlib", "zstd", "lz4"}, Default=None
        The compression codec.
    clevel : int, Default=1
        The compression level, an ``int`` from 1 to 9.
    n_threads : int
        The number of threads to use for ``blosc`` compression. Defaults to
        the maximum number of virtual cores (including Intel Hyperthreading)
        on your system, which is recommended for best performance. If
        ``do_async = True`` you may wish to leave one thread free for the
        Python GIL.

    Notes
    -----
    The recommended compression codec is ``zstd`` (zStandard) with ``clevel=1`` for
    general use. If speed is critical, use ``lz4`` (LZ4) with ``clevel=9``. Integer data
    compresses more redably than floating-point data, and in general the histogram
    of values in the data reflects how compressible it is.

    To save files that are compatible with other programs that can use MRC such as
    GMS, IMOD, Relion, MotionCorr, etc. save with ``compressor=None``, extension ``.mrc``.
    JSON metadata will not be recognized by other MRC-supporting software but should
    not cause crashes.

    Examples
    --------
    >>> from rsciio.mrcz import file_writer
    >>> file_writer('file.mrcz', signal, do_async=True, compressor='zstd', clevel=1)
    """
    mrcz_endian = "le" if endianess == "<" else "be"

    md = DTBox(signal["metadata"], box_dots=True)

    # Get pixelsize and pixelunits from the axes
    pixelunits = signal["axes"][-1]["units"]
    pixelsize = [signal["axes"][I_]["scale"] for I_ in _WRITE_ORDER]

    # Strip out voltage from meta-data
    voltage = md.get("Acquisition_instrument.TEM.beam_energy")
    # There aren't hyperspy fields for spherical aberration or detector gain
    C3 = 0.0
    gain = md.get("Signal.Noise_properties.Variance_linear_model.gain_factor", 1.0)
    if do_async:
        _mrcz.asyncWriteMRC(
            signal["data"],
            filename,
            meta=md,
            endian=mrcz_endian,
            pixelsize=pixelsize,
            pixelunits=pixelunits,
            voltage=voltage,
            C3=C3,
            gain=gain,
            compressor=compressor,
            clevel=clevel,
            n_threads=n_threads,
        )
    else:
        _mrcz.writeMRC(
            signal["data"],
            filename,
            meta=md,
            endian=mrcz_endian,
            pixelsize=pixelsize,
            pixelunits=pixelunits,
            voltage=voltage,
            C3=C3,
            gain=gain,
            compressor=compressor,
            clevel=clevel,
            n_threads=n_threads,
        )


file_writer.__doc__ %= (
    FILENAME_DOC.replace("read", "write to"),
    SIGNAL_DOC,
    ENDIANESS_DOC,
)