def new_img_like(ref_niimg, data, affine=None, copy_header=False):
<EOL>if not (hasattr(ref_niimg, '<STR_LIT>')<EOL>and hasattr(ref_niimg,'<STR_LIT>')):<EOL><INDENT>if isinstance(ref_niimg, _basestring):<EOL><INDENT>ref_niimg = nib.load(ref_niimg)<EOL><DEDENT>elif operator.isSequenceType(ref_niimg):<EOL><INDENT>ref_niimg = nib.load(ref_niimg[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(('<STR_LIT>'<EOL>'<STR_LIT>') % ref_niimg )<EOL><DEDENT><DEDENT>if affine is None:<EOL><INDENT>affine = ref_niimg.get_affine()<EOL><DEDENT>if data.dtype == bool:<EOL><INDENT>default_dtype = np.int8<EOL>if (LooseVersion(nib.__version__) >= LooseVersion('<STR_LIT>') and<EOL>isinstance(ref_niimg, nib.freesurfer.mghformat.MGHImage)):<EOL><INDENT>default_dtype = np.uint8<EOL><DEDENT>data = as_ndarray(data, dtype=default_dtype)<EOL><DEDENT>header = None<EOL>if copy_header:<EOL><INDENT>header = copy.copy(ref_niimg.get_header())<EOL>header['<STR_LIT>'] = <NUM_LIT:0.><EOL>header['<STR_LIT>'] = <NUM_LIT:0.><EOL>header['<STR_LIT>'] = <NUM_LIT:0.><EOL>header['<STR_LIT>'] = np.max(data) if data.size > <NUM_LIT:0> else <NUM_LIT:0.><EOL>header['<STR_LIT>'] = np.min(data) if data.size > <NUM_LIT:0> else <NUM_LIT:0.><EOL><DEDENT>return ref_niimg.__class__(data, affine, header=header)<EOL>
Create a new image of the same class as the reference image

    Parameters
    ----------
    ref_niimg: image
        Reference image. The new image will be of the same type.

    data: numpy array
        Data to be stored in the image

    affine: 4x4 numpy array, optional
        Transformation matrix

    copy_header: boolean, optional
        Indicated if the header of the reference image should be used to
        create the new image

    Returns
    -------
    new_img: image
        A loaded image with the same type (and header) as the reference image.