Source code for medpy.io.header

# Copyright (C) 2013 Oskar Maier
# 
# This program 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.
# 
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
# author Oskar Maier
# version r0.2.3
# since 2012-06-01
# status Release

# build-in modules
import warnings

# third-party modules
import numpy as np

# own modules
from ..core import Logger

# code
def get_voxel_spacing(hdr):
    r"""
    Extracts the voxel spacing from an image header.

    Notes
    -----
    It is recommended to call `hdr.get_voxel_spacing()` instead
    of this function.
    
    Parameters
    ----------
    hdr : medpy.io.Header
        An image header as returned by `load`.
    
    Returns
    -------
    pixel_spacing : tuple of floats
        The image's pixel spacing.
    """
    return hdr.get_voxel_spacing()

[docs]def get_pixel_spacing(hdr): r"""Depreciated synonym of `~medpy.io.header.get_voxel_spacing`.""" warnings.warn('get_pixel_spacing() is depreciated, use set_voxel_spacing() instead', category=DeprecationWarning) return get_voxel_spacing(hdr)
[docs]def get_offset(hdr): r""" Extracts the image offset (akak origin) from an image header. Notes ----- It is recommended to call `hdr.get_offset()` instead of this function. It can be assumed that the offset is measured from the center point of the first pixel, which SimpleITK promises independent of the file format. Some formats do not specify a header field for the offset, thus zeros are returned. Parameters ---------- hdr : medpy.io.Header An image header as returned by `load`. Returns ------- offset : tuple of floats The image's offset. """ return hdr.get_offset()
def set_voxel_spacing(hdr, spacing): r""" Sets the voxel spacing in an image header. Notes ----- It is recommended to call `hdr.set_voxel_spacing()` instead of this function. Parameters ---------- hdr : medpy.io.Header An image header as returned by `load`. pixel_spacing : tuple of floats The desired pixel spacing. """ hdr.set_voxel_spacing(spacing)
[docs]def set_pixel_spacing(hdr, spacing): r"""Depreciated synonym of `~medpy.io.header.set_voxel_spacing`.""" warnings.warn('get_pixel_spacing() is depreciated, use set_voxel_spacing() instead', category=DeprecationWarning) set_voxel_spacing(hdr, spacing)
[docs]def set_offset(hdr, offset): r""" Sets the offset (aka origin) in the image header. Notes ----- It is recommended to call `hdr.set_offset()` instead of this function. The offset is based on the center of the first voxel. See also `get_offset` for more details. Parameters ---------- hdr : medpy.io.Header An image header as returned by `load`. offset : tuple of floats The desired offset. """ hdr.set_offset(offset)
[docs]def copy_meta_data(hdr_to, hdr_from): r""" Copy image meta data (voxel spacing and offset) from one header to another. Parameters ---------- hdr_to : object An image header as returned by `load`. hdr_from : object An image header as returned by `load`. """ warnings.warn('copy_meta_data() is depreciated and may be removed in future versions', category=DeprecationWarning) logger = Logger.getInstance() try: set_pixel_spacing(hdr_to, get_pixel_spacing(hdr_from)) except AttributeError as e: logger.warning('The voxel spacing could not be set correctly. Signaled error: {}'.format(e)) try: set_offset(hdr_to, get_offset(hdr_from)) except AttributeError as e: logger.warning('The image offset could not be set correctly. Signaled error: {}'.format(e))