Beispiel #1
0
 def test_against_numpy_nanmean(self):
     """ Test results against numpy.mean"""
     source = [np.random.random((16, 12, 5)) for _ in range(10)]
     for arr in source:
         arr[randint(0, 15), randint(0, 11), randint(0, 4)] = np.nan
     stack = np.stack(source, axis=-1)
     for axis in (0, 1, 2, None):
         with self.subTest("axis = {}".format(axis)):
             from_numpy = np.nanmean(stack, axis=axis)
             out = last(imean(source, axis=axis, ignore_nan=True))
             self.assertSequenceEqual(from_numpy.shape, out.shape)
             self.assertTrue(np.allclose(out, from_numpy))
Beispiel #2
0
    def test_against_scipy_no_nans(self):
        """ Test that isem outputs the same as scipy.stats.sem """
        source = [np.random.random((16, 12, 5)) for _ in range(10)]
        stack = np.stack(source, axis=-1)

        for axis in (0, 1, 2, None):
            for ddof in range(4):
                with self.subTest("axis = {}, ddof = {}".format(axis, ddof)):
                    from_scipy = scipy_sem(stack, axis=axis, ddof=ddof)
                    from_isem = last(isem(source, axis=axis, ddof=ddof))
                    self.assertSequenceEqual(from_scipy.shape, from_isem.shape)
                    self.assertTrue(np.allclose(from_isem, from_scipy))
Beispiel #3
0
    def test_against_numpy_std(self):
        stream = [np.random.random((16, 7, 3)) for _ in range(10)]
        stack = np.stack(stream, axis=-1)

        with catch_warnings():
            simplefilter("ignore")
            for axis in (0, 1, 2, None):
                for ddof in range(4):
                    with self.subTest("axis = {}, ddof = {}".format(axis, ddof)):
                        from_numpy = np.std(stack, axis=axis, ddof=ddof)
                        from_ivar = last(istd(stream, axis=axis, ddof=ddof))
                        self.assertSequenceEqual(from_numpy.shape, from_ivar.shape)
                        self.assertTrue(np.allclose(from_ivar, from_numpy))
Beispiel #4
0
def combine_masks(*masks):
    """ 
    Combine multiple pixel masks into one. This assumes that pixel masks evaluate
    to ``True`` on invalid pixels.

    Returns
    -------
    combined : `~numpy.ndarray`, dtype bool 
    """
    # By multiplying boolean arrays, values of False propagate
    # Hence, much easier to do if invalue pixels are False instead of True
    valids = map(np.logical_not, masks)
    combined_valid = last(iprod(valids, dtype=np.bool))
    return np.logical_not(combined_valid)
Beispiel #5
0
    def test_file_list(self):
        """ Test that pload works on iterable of filenames """
        with self.subTest("processes = 1"):
            files = [
                "tests\\data\\test_data1.npy",
                "tests\\data\\test_data2.npy",
                "tests\\data\\test_data3.npy",
            ]
            stream = pload(files, load_func=np.load)
            s = last(isum(stream)).astype(
                np.float)  # Cast to float for np.allclose
            self.assertTrue(np.allclose(s, np.zeros_like(s)))

        with self.subTest("processes = 2"):
            files = [
                "tests\\data\\test_data1.npy",
                "tests\\data\\test_data2.npy",
                "tests\\data\\test_data3.npy",
            ]
            stream = pload(files, load_func=np.load, processes=2)
            s = last(isum(stream)).astype(
                np.float)  # Cast to float for np.allclose
            self.assertTrue(np.allclose(s, np.zeros_like(s)))
Beispiel #6
0
    def test_against_numpy_nanstd(self):
        source = [np.random.random((16, 12, 5)) for _ in range(10)]
        for arr in source:
            arr[randint(0, 15), randint(0, 11), randint(0, 4)] = np.nan
        stack = np.stack(source, axis=-1)

        for axis in (0, 1, 2, None):
            for ddof in range(4):
                with self.subTest("axis = {}, ddof = {}".format(axis, ddof)):
                    from_numpy = np.nanstd(stack, axis=axis, ddof=ddof)
                    from_ivar = last(
                        istd(source, axis=axis, ddof=ddof, ignore_nan=True)
                    )
                    self.assertSequenceEqual(from_numpy.shape, from_ivar.shape)
                    self.assertTrue(np.allclose(from_ivar, from_numpy))
Beispiel #7
0
    def test_against_scipy_with_nans(self):
        """ Test that isem outputs the same as scipy.stats.sem when NaNs are ignored. """
        source = [np.random.random((16, 12, 5)) for _ in range(10)]
        for arr in source:
            arr[randint(0, 15), randint(0, 11), randint(0, 4)] = np.nan
        stack = np.stack(source, axis=-1)

        for axis in (0, 1, 2, None):
            for ddof in range(4):
                with self.subTest("axis = {}, ddof = {}".format(axis, ddof)):
                    from_scipy = scipy_sem(
                        stack, axis=axis, ddof=ddof, nan_policy="omit"
                    )
                    from_isem = last(
                        isem(source, axis=axis, ddof=ddof, ignore_nan=True)
                    )
                    self.assertSequenceEqual(from_scipy.shape, from_isem.shape)
                    self.assertTrue(np.allclose(from_isem, from_scipy))
Beispiel #8
0
 def test_trivial(self):
     """ Test a sum of zeros """
     source = [np.zeros((16,), dtype=np.float) for _ in range(10)]
     summed = last(isum(source))
     self.assertTrue(np.allclose(summed, np.zeros_like(summed)))
Beispiel #9
0
 def test_on_empty_iterable(self):
     """ Test that last() raises RuntimeError for empty iterable """
     with self.assertRaises(RuntimeError):
         last(list())
Beispiel #10
0
 def test_dtype(self):
     """ Test a sum of floating zeros with an int accumulator """
     source = [np.zeros((16,), dtype=np.float) for _ in range(10)]
     summed = last(isum(source, dtype=np.int))
     self.assertTrue(np.allclose(summed, np.zeros_like(summed)))
     self.assertEqual(summed.dtype, np.int)
Beispiel #11
0
 def test_ignore_nans(self):
     """ Test a sum of zeros with NaNs sprinkled """
     source = [np.zeros((16,), dtype=np.float) for _ in range(10)]
     source.append(np.full((16,), fill_value=np.nan))
     summed = last(isum(source, ignore_nan=True))
     self.assertTrue(np.allclose(summed, np.zeros_like(summed)))
Beispiel #12
0
 def test_dtype(self):
     """ Test that dtype argument is working """
     source = [np.ones((16,), dtype=np.float) for _ in range(10)]
     product = last(iprod(source, dtype=np.int))
     self.assertTrue(np.allclose(product, np.ones_like(product)))
     self.assertEqual(product.dtype, np.int)
Beispiel #13
0
 def test_single_array(self):
     """ Test ireduce_ufunc on a single array, not a sequence """
     source = np.ones((16, 16), dtype=np.int)
     out = last(ireduce_ufunc(source, np.add, axis=-1))
     self.assertTrue(np.allclose(source, out))
Beispiel #14
0
 def test_no_side_effects(self):
     """ Test that no arrays in the stream are modified """
     for arr in self.source:
         arr.setflags(write=False)
     out = last(ireduce_ufunc(self.source, np.add))
Beispiel #15
0
 def sufunc(arrays, ignore_nan=False):  # s for stream
     return last(ireduce_ufunc(arrays, ufunc, axis=1, ignore_nan=True))
Beispiel #16
0
 def sufunc(arrays, axis=-1):  # s for stream
     return last(ireduce_ufunc(arrays, ufunc, axis=axis))
Beispiel #17
0
 def test_trivial(self):
     """ Test last() on iterable of identical values """
     i = repeat(1, 10)
     self.assertEqual(last(i), 1)
Beispiel #18
0
 def test_trivial(self):
     """ Test a product of ones """
     source = [np.ones((16,), dtype=np.float) for _ in range(10)]
     product = last(iprod(source))
     self.assertTrue(np.allclose(product, np.ones_like(product)))
Beispiel #19
0
 def test_ignore_nans(self):
     """ Test that NaNs are ignored. """
     source = [np.ones((16,), dtype=np.float) for _ in range(10)]
     source.append(np.full_like(source[0], np.nan))
     product = last(iprod(source, ignore_nan=True))
     self.assertTrue(np.allclose(product, np.ones_like(product)))
Beispiel #20
0
def test_isnr_trivial():
    """ Test snr_from_collection on series of identical images """
    images = [np.ones((64, 64)) for _ in range(10)]
    snr = last(isnr(images))

    assert np.allclose(snr, np.zeros_like(snr))
Beispiel #21
0
 def test_glob(self):
     """ Test that iload works on glob-like patterns """
     stream = iload("tests\\data\\test_data*.npy", load_func=np.load)
     s = last(isum(stream)).astype(
         np.float)  # Cast to float for np.allclose
     self.assertTrue(np.allclose(s, np.zeros_like(s)))