示例#1
0
文件: misc_io.py 项目: amh28/NIF
def _image3_animated_gif(tag, ims):
    check_module('PIL')
    import PIL
    from PIL.GifImagePlugin import Image as GIF

    # x=numpy.random.randint(0,256,[10,10,10],numpy.uint8)
    ims = [np.asarray((ims[i, :, :]).astype(np.uint8))
           for i in range(ims.shape[0])]
    ims = [GIF.fromarray(im) for im in ims]
    s = b''
    for b in PIL.GifImagePlugin.getheader(ims[0])[0]:
        s += b
    s += b'\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50' \
         b'\x45\x32\x2E\x30\x03\x01\x00\x00\x00'
    for i in ims:
        for b in PIL.GifImagePlugin.getdata(i):
            s += b
    s += b'\x3B'
    if IS_PYTHON2:
        s = str(s)
    summary_image_str = summary_pb2.Summary.Image(
        height=10, width=10, colorspace=1, encoded_image_string=s)
    image_summary = summary_pb2.Summary.Value(
        tag=tag, image=summary_image_str)
    return [summary_pb2.Summary(value=[image_summary]).SerializeToString()]
示例#2
0
def add_network_args(parser):
    parser.add_argument(
        "--name",
        help="Choose a net from NiftyNet/niftynet/network/ or from "
        "user specified module string",
        metavar='')

    import niftynet.layer.activation
    parser.add_argument("--activation_function",
                        help="Specify activation function types",
                        choices=list(niftynet.layer.activation.SUPPORTED_OP),
                        metavar='TYPE_STR',
                        default='relu')

    parser.add_argument("--batch_size",
                        metavar='',
                        help="Set batch size of the net",
                        type=int,
                        default=2)

    parser.add_argument("--decay",
                        help="[Training only] Set weight decay",
                        type=float,
                        default=0)

    parser.add_argument("--reg_type",
                        metavar='TYPE_STR',
                        help="[Training only] Specify regulariser type_str",
                        type=str,
                        default='L2')

    parser.add_argument(
        "--volume_padding_size",
        metavar='',
        help="Set padding size of each volume (in all dimensions)",
        type=spatialnumarray,
        default=(0, 0, 0))

    parser.add_argument("--window_sampling",
                        metavar='TYPE_STR',
                        help="How to sample patches from each loaded image:"
                        " 'uniform': fixed size uniformly distributed,"
                        " 'resize': resize image to the patch size.",
                        choices=['uniform', 'resize'],
                        default='uniform')

    parser.add_argument("--queue_length",
                        help="Set size of preprocessing buffer queue",
                        metavar='',
                        type=int,
                        default=5)

    import niftynet.layer.binary_masking
    parser.add_argument(
        "--multimod_foreground_type",
        choices=list(
            niftynet.layer.binary_masking.SUPPORTED_MULTIMOD_MASK_TYPES),
        help="Way of combining the foreground masks from different "
        "modalities. 'and' is the intersection, 'or' is the union "
        "and 'multi' permits each modality to use its own mask.",
        default='and')

    parser.add_argument(
        "--histogram_ref_file",
        metavar='',
        type=str,
        help="A reference file of histogram for intensity normalisation",
        default='')

    # TODO add choices of normalisation types
    import niftynet.utilities.histogram_standardisation as hist_std_module
    parser.add_argument("--norm_type",
                        help="Type of normalisation to perform",
                        type=str,
                        default='percentile',
                        choices=list(hist_std_module.SUPPORTED_CUTPOINTS))

    parser.add_argument("--cutoff",
                        help="Cutoff values for the normalisation process",
                        type=float_array,
                        default=(0.01, 0.99))

    import niftynet.layer.binary_masking
    parser.add_argument(
        "--foreground_type",
        choices=list(niftynet.layer.binary_masking.SUPPORTED_MASK_TYPES),
        help="type_str of foreground masking strategy used",
        default='otsu_plus')

    parser.add_argument(
        "--normalisation",
        help="Indicates if the normalisation must be performed",
        type=str2boolean,
        default=False)

    parser.add_argument(
        "--whitening",
        help="Indicates if the whitening of the data should be applied",
        type=str2boolean,
        default=False)

    parser.add_argument(
        "--normalise_foreground_only",
        help="Indicates whether a foreground mask should be applied when"
        " normalising volumes",
        type=str2boolean,
        default=False)

    parser.add_argument("--weight_initializer",
                        help="Set the initializer for the weight parameters",
                        type=str,
                        default='he_normal')

    parser.add_argument("--bias_initializer",
                        help="Set the initializer for the bias parameters",
                        type=str,
                        default='zeros')

    try:
        from niftynet.utilities.util_import import check_module
        check_module('yaml')
        import yaml
        parser.add_argument(
            "--weight_initializer_args",
            help="Pass arguments to the initializer for the weight parameters",
            type=yaml.load,
            default={})
        parser.add_argument(
            "--bias_initializer_args",
            help="Pass arguments to the initializer for the bias parameters",
            type=yaml.load,
            default={})
    except ImportError:
        # "PyYAML module not found")
        pass

    return parser
示例#3
0
# -*- coding: utf-8 -*-
"""
Loading SimpleITK image as a Nibabel object.
"""
from niftynet.utilities.util_import import check_module

check_module('SimpleITK')

import SimpleITK as sitk
import nibabel
import numpy as np


class SimpleITKAsNibabel(nibabel.Nifti1Image):
    """
    Minimal interface to use a SimpleITK image as if it were
    a nibabel object. Currently only supports the subset of the
    interface used by NiftyNet and is read only
    """

    def __init__(self, filename):
        try:
            self._SimpleITKImage = sitk.ReadImage(filename)
        except RuntimeError as err:
            if 'Unable to determine ImageIO reader' in str(err):
                raise nibabel.filebasedimages.ImageFileError(str(err))
            else:
                raise
        # self._header = SimpleITKAsNibabelHeader(self._SimpleITKImage)
        affine = make_affine(self._SimpleITKImage)
        # super(SimpleITKAsNibabel, self).__init__(
示例#4
0
 def test_installed(self):
     check_module('tensorflow')
def add_network_args(parser):
    parser.add_argument(
        "--name",
        help="Choose a net from NiftyNet/niftynet/network/ or from "
             "user specified module string",
        metavar='')

    import niftynet.layer.activation
    parser.add_argument(
        "--activation_function",
        help="Specify activation function types",
        choices=list(niftynet.layer.activation.SUPPORTED_OP),
        metavar='TYPE_STR',
        default='relu')

    parser.add_argument(
        "--batch_size",
        metavar='',
        help="Set batch size of the net",
        type=int,
        default=2)

    parser.add_argument(
        "--decay",
        help="[Training only] Set weight decay",
        type=float,
        default=0)

    parser.add_argument(
        "--reg_type",
        metavar='TYPE_STR',
        help="[Training only] Specify regulariser type_str",
        type=str,
        default='L2')

    parser.add_argument(
        "--volume_padding_size",
        metavar='',
        help="Set padding size of each volume (in all dimensions)",
        type=spatialnumarray,
        default=(0, 0, 0))

    parser.add_argument(
        "--window_sampling",
        metavar='TYPE_STR',
        help="How to sample patches from each loaded image:"
             " 'uniform': fixed size uniformly distributed,"
             " 'resize': resize image to the patch size.",
        choices=['uniform', 'resize'],
        default='uniform')

    parser.add_argument(
        "--queue_length",
        help="Set size of preprocessing buffer queue",
        metavar='',
        type=int,
        default=5)

    import niftynet.layer.binary_masking
    parser.add_argument(
        "--multimod_foreground_type",
        choices=list(
            niftynet.layer.binary_masking.SUPPORTED_MULTIMOD_MASK_TYPES),
        help="Way of combining the foreground masks from different "
             "modalities. 'and' is the intersection, 'or' is the union "
             "and 'multi' permits each modality to use its own mask.",
        default='and')

    parser.add_argument(
        "--histogram_ref_file",
        metavar='',
        type=str,
        help="A reference file of histogram for intensity normalisation",
        default='')

    # TODO add choices of normalisation types
    import niftynet.utilities.histogram_standardisation as hist_std_module
    parser.add_argument(
        "--norm_type",
        help="Type of normalisation to perform",
        type=str,
        default='percentile',
        choices=list(hist_std_module.SUPPORTED_CUTPOINTS))

    parser.add_argument(
        "--cutoff",
        help="Cutoff values for the normalisation process",
        type=float_array,
        default=(0.01, 0.99))

    import niftynet.layer.binary_masking
    parser.add_argument(
        "--foreground_type",
        choices=list(
            niftynet.layer.binary_masking.SUPPORTED_MASK_TYPES),
        help="type_str of foreground masking strategy used",
        default='otsu_plus')

    parser.add_argument(
        "--normalisation",
        help="Indicates if the normalisation must be performed",
        type=str2boolean,
        default=False)

    parser.add_argument(
        "--whitening",
        help="Indicates if the whitening of the data should be applied",
        type=str2boolean,
        default=False)

    parser.add_argument(
        "--normalise_foreground_only",
        help="Indicates whether a foreground mask should be applied when"
             " normalising volumes",
        type=str2boolean,
        default=False)

    parser.add_argument(
        "--weight_initializer",
        help="Set the initializer for the weight parameters",
        type=str,
        default='he_normal')

    parser.add_argument(
        "--bias_initializer",
        help="Set the initializer for the bias parameters",
        type=str,
        default='zeros')

    try:
        from niftynet.utilities.util_import import check_module
        check_module('yaml')
        import yaml
        parser.add_argument(
            "--weight_initializer_args",
            help="Pass arguments to the initializer for the weight parameters",
            type=yaml.load,
            default={})
        parser.add_argument(
            "--bias_initializer_args",
            help="Pass arguments to the initializer for the bias parameters",
            type=yaml.load,
            default={})
    except ImportError:
        # "PyYAML module not found")
        pass

    return parser
示例#6
0
 def test_no_version_info(self):
     check_module('importlib', 0)
示例#7
0
 def test_no_input(self):
     with self.assertRaisesRegexp(ImportError, ''):
         check_module([])
     with self.assertRaisesRegexp(ImportError, ''):
         check_module(None)
示例#8
0
 def test_self_version(self):
     check_module('importlib')
示例#9
0
 def test_wrong_version(self):
     with self.assertRaisesRegexp(AssertionError, ''):
         check_module('tensorflow', 100)
示例#10
0
 def test_no_package(self):
     with self.assertRaisesRegexp(ImportError, ''):
         check_module('foobar_wrong_case')
示例#11
0
 def test_installed_min_version(self):
     check_module('tensorflow', 1.0)
示例#12
0
# -*- coding: utf-8 -*-
"""
Loading SimpleITK image as a Nibabel object.
"""
from niftynet.utilities.util_import import check_module

check_module('SimpleITK')

import SimpleITK as sitk
import nibabel
import numpy as np


class SimpleITKAsNibabel(nibabel.Nifti1Image):
    """
    Minimal interface to use a SimpleITK image as if it were
    a nibabel object. Currently only supports the subset of the
    interface used by NiftyNet and is read only
    """
    def __init__(self, filename):
        try:
            self._SimpleITKImage = sitk.ReadImage(filename)
        except RuntimeError as err:
            if 'Unable to determine ImageIO reader' in str(err):
                raise nibabel.filebasedimages.ImageFileError(str(err))
            else:
                raise
        # self._header = SimpleITKAsNibabelHeader(self._SimpleITKImage)
        affine = make_affine(self._SimpleITKImage)
        # super(SimpleITKAsNibabel, self).__init__(
        #     sitk.GetArrayFromImage(self._SimpleITKImage).transpose(), affine)