<commit_before>import os
import glob
from tempfile import NamedTemporaryFile

import numpy as np

#from numpy.testing import NumpyTest, NumpyTestCase
from neuroimaging.externals.scipy.testing import *
from neuroimaging.utils.test_decorators import slow

from neuroimaging.core.image import image

from neuroimaging.core.api import Image, load_image, save_image, fromarray
from neuroimaging.core.api import parcels, data_generator, write_data

from neuroimaging.core.reference.grid import SamplingGrid
from neuroimaging.core.reference.mapping import Affine
from neuroimaging.testing import anatfile, funcfile
from neuroimaging.data_io.api import Analyze


class TestImage(TestCase):

    def setUp(self):
        self.img = load_image(anatfile)
        self.func = load_image(funcfile)

    def tearDown(self):
        tmpfiles = glob.glob('tmp.*')
        for tmpfile in tmpfiles:
            os.remove(tmpfile)
            
    def test_init(self):
        new = Image(np.asarray(self.img[:]), self.img.grid)
        np.testing.assert_equal(np.asarray(self.img)[:], np.asarray(new)[:])

        new = Image(self.img._data, self.img.grid)
        np.testing.assert_equal(np.asarray(self.img)[:], np.asarray(new)[:])

        self.assertRaises(ValueError, Image, None, None)

    def test_badfile(self):
        filename = "bad_file.foo"
        self.assertRaises(RuntimeError, load_image, filename)

    def test_maxmin_values(self):
        y = np.asarray(self.img)
        self.assertEquals(y.shape, tuple(self.img.grid.shape))
        self.assertEquals(y.max(), 7902.0)
        self.assertEquals(y.min(), 1910.0)

    def test_slice_plane(self):
        x = self.img[3]
        self.assertEquals(x.shape, self.img.shape[1:])
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_block(self):
        x = self.img[3:5]
        self.assertEquals(x.shape, (2,) + tuple(self.img.grid.shape[1:]))
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_step(self):
        s = slice(0,20,2)
        x = self.img[s]
        self.assertEquals(x.shape, (10,) + tuple(self.img.grid.shape[1:]))
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_type(self):
        s = slice(0,self.img.grid.shape[0])
        x = self.img[s]
        self.assertEquals(x.shape, tuple((self.img.grid.shape)))
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_steps(self):
        zdim, ydim, xdim = self.img.shape<commit_msg>Update init and maxmin tests to use avg152T1.nii.gz image.<commit_after>import os
import glob
from tempfile import NamedTemporaryFile

import numpy as np

#from numpy.testing import NumpyTest, NumpyTestCase
from neuroimaging.externals.scipy.testing import *
from neuroimaging.utils.test_decorators import slow
from neuroimaging.utils.tests.data import repository

from neuroimaging.core.image import image

from neuroimaging.core.api import Image, load_image, save_image, fromarray
from neuroimaging.core.api import parcels, data_generator, write_data

from neuroimaging.core.reference.grid import SamplingGrid
from neuroimaging.core.reference.mapping import Affine
from neuroimaging.data_io.api import Analyze

class TestImage(TestCase):

    def setUp(self):
        self.img = load_image(str(repository._fullpath('avg152T1.nii.gz')))

    def tearDown(self):
        tmpfiles = glob.glob('tmp.*')
        for tmpfile in tmpfiles:
            os.remove(tmpfile)
            
    def test_init(self):
        new = Image(np.asarray(self.img), self.img.grid)
        np.testing.assert_equal(np.asarray(self.img)[:], np.asarray(new)[:])

        self.assertRaises(ValueError, Image, None, None)

    def test_badfile(self):
        filename = "bad_file.foo"
        self.assertRaises(RuntimeError, load_image, filename)

    def test_maxmin_values(self):
        y = np.asarray(self.img)
        self.assertEquals(y.shape, tuple(self.img.grid.shape))
        np.allclose(y.max(), 437336.36, rtol=1.0e-8)
        self.assertEquals(y.min(), 0.0)

    def test_slice_plane(self):
        x = self.img[3]
        self.assertEquals(x.shape, self.img.shape[1:])
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_block(self):
        x = self.img[3:5]
        self.assertEquals(x.shape, (2,) + tuple(self.img.grid.shape[1:]))
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_step(self):
        s = slice(0,20,2)
        x = self.img[s]
        self.assertEquals(x.shape, (10,) + tuple(self.img.grid.shape[1:]))
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_type(self):
        s = slice(0,self.img.grid.shape[0])
        x = self.img[s]
        self.assertEquals(x.shape, tuple((self.img.grid.shape)))
        self.assertEquals(x.shape, x.grid.shape)

    def test_slice_steps(self):
        zdim, ydim, xdim = self.img.shape
{'fraction_non_alphanumeric': 0.12425180598555212, 'fraction_numerical': 0.01259029927760578, 'mean_word_length': 3.163230240549828, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '966703', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1578, 'n_words': 304}