def test_ibd_segments_both_parents(self):
        '''Test computing GERMLINE IBD segments.'''
        h_mat = ig._HapMatrix(self.problem, self.sibs)
        m = self.ibd_computer.ibd_segments(h_mat)
        assert_segments_almost_equal(m,
                                     [((0   , 344), (16484792, 21449028, 4.964, 0), ((2, 0), (4, 0))),
                                      ((0   , 344), (16484792, 21449028, 4.964, 0), ((1, 0), (4, 0))),
                                      ((0   , 780), (16484792, 26920270, 10.435, 0), ((4, 1), (2, 1))),
                                      ((2145, 2996), (39608193, 48759228, 9.151, 0), ((1, 1), (2, 1))),
                                      ((2145, 2996), (39608193, 48759228, 9.151, 0), ((4, 1), (2, 1))),
                                      ((885 , 3218), (27425790, 51156933, 23.731, 0), ((4, 1), (1, 1))),
                                      ((0   , 3218), (16484792, 51156933, 34.672, 0), ((2, 0), (1, 0)))],
                                     full_data=True, decimal=3, err_msg='Wrong IBD segments, raw')
        # A transitive-logic test test to see that we don't miss any intervals with GERMLINE
        m.group_to_disjoint(False) 
        assert_segments_almost_equal(m,
                                     [((0   , 344), (16484792, 21449028, 4.964, 0), ((2, 0), (1, 0), (4, 0))),
                                      ((0   , 344), (16484792, 21449028, 4.964, 0), ((4, 1), (2, 1))),
                                      ((344 , 780), (21449028, 26920270, 5.471, 0), ((2, 0), (1, 0))),
                                      ((344 , 780), (21449028, 26920270, 5.471, 0), ((4, 1), (2, 1))),
                                      ((780 , 885), (26920270, 27425790, 0.506, 0), ((2, 0), (1, 0))),
                                      ((885 , 2145), (27425790, 39608193, 12.182, 0), ((2, 0), (1, 0))),
                                      ((885 , 2145), (27425790, 39608193, 12.182, 0), ((4, 1), (1, 1))),
                                      ((2145, 2996), (39608193, 48759228, 9.151, 0), ((4, 1), (1, 1), (2, 1))),
                                      ((2145, 2996), (39608193, 48759228, 9.151, 0), ((2, 0), (1, 0))),
                                      ((2996, 3218), (48759228, 51156933, 2.398, 0), ((2, 0), (1, 0))),
                                      ((2996, 3218), (48759228, 51156933, 2.398, 0), ((4, 1), (1, 1)))],
                                     full_data=True, decimal=3, err_msg='Wrong IBD segments, grouped')

        stats = np.array([(len(s.samples), s.length) for s in m])
        best_segment = np.lexsort((-stats[:, 1], -stats[:, 0]))[0]
        assert_equal(best_segment, 7, 'Wrong best segment (IBD set size + length)') 
        assert_almost_equal(m[best_segment].length, 9.15, decimal=2, err_msg='Wrong best segment (IBD set size + length)')
        assert_equal(m[best_segment].samples, set([(4, 1), (1, 1), (2, 1)]), err_msg='Wrong best segment (IBD set size + length)')
Esempio n. 2
0
 def test_masked_binary_operations2(self):
     # Tests domained_masked_binary_operation
     (x, mx) = self.data
     xmx = masked_array(mx.data.__array__(), mask=mx.mask)
     self.assertTrue(isinstance(divide(mx, mx), mmatrix))
     self.assertTrue(isinstance(divide(mx, x), mmatrix))
     assert_equal(divide(mx, mx), divide(xmx, xmx))
Esempio n. 3
0
 def test_fancy_dtype_alt(self):
     "Check that a nested dtype isn't MIA"
     data = StringIO('1,2,3.0\n4,5,6.0\n')
     fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
     test = np.mafromtxt(data, dtype=fancydtype, delimiter=',')
     control = ma.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
     assert_equal(test, control)
Esempio n. 4
0
 def test_with_masked_column_uniform(self):
     "Test masked column"
     data = StringIO('1 2 3\n4 5 6\n')
     test = np.genfromtxt(data, dtype=None,
                          missing_values='2,5', usemask=True)
     control = ma.array([[1, 2, 3], [4, 5, 6]], mask=[[0, 1, 0], [0, 1, 0]])
     assert_equal(test, control)
Esempio n. 5
0
 def test_out(self):
     x = masked_array(np.arange(30).reshape(10, 3))
     x[:3] = x[-3:] = masked
     out = masked_array(np.ones(10))
     r = median(x, axis=1, out=out)
     assert_equal(r, out)
     assert_(type(r) == MaskedArray)
Esempio n. 6
0
 def test_2D(self):
     x = ma.array([[1, 1, 1],
                   [1, 1, 1],
                   [4, 4, 3],
                   [1, 1, 1],
                   [1, 1, 1]])
     assert_equal(mstats.scoreatpercentile(x,50), [1,1,1])
Esempio n. 7
0
 def test_different_field_order(self):
     # gh-8940
     a = np.zeros(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u1')])
     b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')])
     # this should not give a FutureWarning:
     j = join_by(['c', 'b'], a, b, jointype='inner', usemask=False)
     assert_equal(j.dtype.names, ['b', 'c', 'a1', 'a2'])
Esempio n. 8
0
 def test_intersect1d(self):
     # Test intersect1d
     x = array([1, 3, 3, 3], mask=[0, 0, 0, 1])
     y = array([3, 1, 1, 1], mask=[0, 0, 0, 1])
     test = intersect1d(x, y)
     control = array([1, 3, -1], mask=[0, 0, 1])
     assert_equal(test, control)
Esempio n. 9
0
    def test_unnamed_and_named_fields(self):
        # Test combination of arrays w/ & w/o named fields
        (_, x, _, z) = self.data

        test = stack_arrays((x, z))
        control = ma.array([(1, -1, -1), (2, -1, -1),
                            (-1, 'A', 1), (-1, 'B', 2)],
                           mask=[(0, 1, 1), (0, 1, 1),
                                 (1, 0, 0), (1, 0, 0)],
                           dtype=[('f0', int), ('A', '|S3'), ('B', float)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, x))
        control = ma.array([('A', 1, -1), ('B', 2, -1),
                            (-1, -1, 1), (-1, -1, 2), ],
                           mask=[(0, 0, 1), (0, 0, 1),
                                 (1, 1, 0), (1, 1, 0)],
                           dtype=[('A', '|S3'), ('B', float), ('f2', int)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, z, x))
        control = ma.array([('A', 1, -1), ('B', 2, -1),
                            ('A', 1, -1), ('B', 2, -1),
                            (-1, -1, 1), (-1, -1, 2), ],
                           mask=[(0, 0, 1), (0, 0, 1),
                                 (0, 0, 1), (0, 0, 1),
                                 (1, 1, 0), (1, 1, 0)],
                           dtype=[('A', '|S3'), ('B', float), ('f2', int)])
        assert_equal(test, control)
Esempio n. 10
0
    def test_outer_join(self):
        a, b = self.a, self.b

        test = join_by(('a', 'b'), a, b, 'outer')
        control = ma.array([(0, 50, 100, -1), (1, 51, 101, -1),
                            (2, 52, 102, -1), (3, 53, 103, -1),
                            (4, 54, 104, -1), (5, 55, 105, -1),
                            (5, 65, -1, 100), (6, 56, 106, -1),
                            (6, 66, -1, 101), (7, 57, 107, -1),
                            (7, 67, -1, 102), (8, 58, 108, -1),
                            (8, 68, -1, 103), (9, 59, 109, -1),
                            (9, 69, -1, 104), (10, 70, -1, 105),
                            (11, 71, -1, 106), (12, 72, -1, 107),
                            (13, 73, -1, 108), (14, 74, -1, 109)],
                           mask=[(0, 0, 0, 1), (0, 0, 0, 1),
                                 (0, 0, 0, 1), (0, 0, 0, 1),
                                 (0, 0, 0, 1), (0, 0, 0, 1),
                                 (0, 0, 1, 0), (0, 0, 0, 1),
                                 (0, 0, 1, 0), (0, 0, 0, 1),
                                 (0, 0, 1, 0), (0, 0, 0, 1),
                                 (0, 0, 1, 0), (0, 0, 0, 1),
                                 (0, 0, 1, 0), (0, 0, 1, 0),
                                 (0, 0, 1, 0), (0, 0, 1, 0),
                                 (0, 0, 1, 0), (0, 0, 1, 0)],
                           dtype=[('a', int), ('b', int),
                                  ('c', int), ('d', int)])
        assert_equal(test, control)
Esempio n. 11
0
 def test_addfield(self):
     # Tests addfield
     (mrec, nrec, ddtype) = self.data
     (d, m) = ([100, 200, 300], [1, 0, 0])
     mrec = addfield(mrec, ma.array(d, mask=m))
     assert_equal(mrec.f3, d)
     assert_equal(mrec.f3._mask, m)
 def test_pickling_subbaseclass(self):
     # Test pickling w/ a subclass of ndarray
     a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
     a_pickled = pickle.loads(a.dumps())
     assert_equal(a_pickled._mask, a._mask)
     assert_equal(a_pickled, a)
     assert_(isinstance(a_pickled._data, np.matrix))
Esempio n. 13
0
 def test_view_simple_dtype(self):
     (mrec, a, b, arr) = self.data
     ntype = (np.float, 2)
     test = mrec.view(ntype)
     self.assertTrue(isinstance(test, ma.MaskedArray))
     assert_equal(test, np.array(list(zip(a, b)), dtype=np.float))
     self.assertTrue(test[3, 1] is ma.masked)
Esempio n. 14
0
    def test_fromrecords(self):
        # Test construction from records.
        (mrec, nrec, ddtype) = self.data
        #......
        palist = [(1, 'abc', 3.7000002861022949, 0),
                  (2, 'xy', 6.6999998092651367, 1),
                  (0, ' ', 0.40000000596046448, 0)]
        pa = recfromrecords(palist, names='c1, c2, c3, c4')
        mpa = fromrecords(palist, names='c1, c2, c3, c4')
        assert_equal_records(pa, mpa)
        #.....
        _mrec = fromrecords(nrec)
        assert_equal(_mrec.dtype, mrec.dtype)
        for field in _mrec.dtype.names:
            assert_equal(getattr(_mrec, field), getattr(mrec._data, field))

        _mrec = fromrecords(nrec.tolist(), names='c1,c2,c3')
        assert_equal(_mrec.dtype, [('c1', int), ('c2', float), ('c3', '|S5')])
        for (f, n) in zip(('c1', 'c2', 'c3'), ('a', 'b', 'c')):
            assert_equal(getattr(_mrec, f), getattr(mrec._data, n))

        _mrec = fromrecords(mrec)
        assert_equal(_mrec.dtype, mrec.dtype)
        assert_equal_records(_mrec._data, mrec.filled())
        assert_equal_records(_mrec._mask, mrec._mask)
Esempio n. 15
0
    def test_two_keys_two_vars(self):
        a = np.array(
            list(zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2), np.arange(50, 60), np.arange(10, 20))),
            dtype=[("k", int), ("a", int), ("b", int), ("c", int)],
        )

        b = np.array(
            list(zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2), np.arange(65, 75), np.arange(0, 10))),
            dtype=[("k", int), ("a", int), ("b", int), ("c", int)],
        )

        control = np.array(
            [
                (10, 0, 50, 65, 10, 0),
                (11, 0, 51, 66, 11, 1),
                (10, 1, 52, 67, 12, 2),
                (11, 1, 53, 68, 13, 3),
                (10, 2, 54, 69, 14, 4),
                (11, 2, 55, 70, 15, 5),
                (10, 3, 56, 71, 16, 6),
                (11, 3, 57, 72, 17, 7),
                (10, 4, 58, 73, 18, 8),
                (11, 4, 59, 74, 19, 9),
            ],
            dtype=[("k", int), ("a", int), ("b1", int), ("b2", int), ("c1", int), ("c2", int)],
        )
        test = join_by(["a", "k"], a, b, r1postfix="1", r2postfix="2", jointype="inner")
        assert_equal(test.dtype, control.dtype)
        assert_equal(test, control)
Esempio n. 16
0
 def test_skewtest_2D_notmasked(self):
     # a normal ndarray is passed to the masked function
     x = np.random.random((20,2))*20.
     r = stats.skewtest(x)
     rm = stats.mstats.skewtest(x)
     assert_equal(r[0][0],rm[0][0])
     assert_equal(r[0][1],rm[0][1])
Esempio n. 17
0
    def test_unnamed_and_named_fields(self):
        # Test combination of arrays w/ & w/o named fields
        (_, x, _, z) = self.data

        test = stack_arrays((x, z))
        control = ma.array(
            [(1, -1, -1), (2, -1, -1), (-1, "A", 1), (-1, "B", 2)],
            mask=[(0, 1, 1), (0, 1, 1), (1, 0, 0), (1, 0, 0)],
            dtype=[("f0", int), ("A", "|S3"), ("B", float)],
        )
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, x))
        control = ma.array(
            [("A", 1, -1), ("B", 2, -1), (-1, -1, 1), (-1, -1, 2)],
            mask=[(0, 0, 1), (0, 0, 1), (1, 1, 0), (1, 1, 0)],
            dtype=[("A", "|S3"), ("B", float), ("f2", int)],
        )
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, z, x))
        control = ma.array(
            [("A", 1, -1), ("B", 2, -1), ("A", 1, -1), ("B", 2, -1), (-1, -1, 1), (-1, -1, 2)],
            mask=[(0, 0, 1), (0, 0, 1), (0, 0, 1), (0, 0, 1), (1, 1, 0), (1, 1, 0)],
            dtype=[("A", "|S3"), ("B", float), ("f2", int)],
        )
        assert_equal(test, control)
Esempio n. 18
0
    def test_leftouter_join(self):
        a, b = self.a, self.b

        test = join_by(("a", "b"), a, b, "leftouter")
        control = ma.array(
            [
                (0, 50, 100, -1),
                (1, 51, 101, -1),
                (2, 52, 102, -1),
                (3, 53, 103, -1),
                (4, 54, 104, -1),
                (5, 55, 105, -1),
                (6, 56, 106, -1),
                (7, 57, 107, -1),
                (8, 58, 108, -1),
                (9, 59, 109, -1),
            ],
            mask=[
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
                (0, 0, 0, 1),
            ],
            dtype=[("a", int), ("b", int), ("c", int), ("d", int)],
        )
        assert_equal(test, control)
Esempio n. 19
0
 def test_simple_flexible(self):
     # Test recursive_fill_fields on flexible-array
     a = np.array([(1, 10.0), (2, 20.0)], dtype=[("A", int), ("B", float)])
     b = np.zeros((3,), dtype=a.dtype)
     test = recursive_fill_fields(a, b)
     control = np.array([(1, 10.0), (2, 20.0), (0, 0.0)], dtype=[("A", int), ("B", float)])
     assert_equal(test, control)
Esempio n. 20
0
 def test_w_singlefield(self):
     # Test single field
     test = merge_arrays((np.array([1, 2]).view([("a", int)]), np.array([10.0, 20.0, 30.0])))
     control = ma.array(
         [(1, 10.0), (2, 20.0), (-1, 30.0)], mask=[(0, 0), (0, 0), (1, 0)], dtype=[("a", int), ("f1", float)]
     )
     assert_equal(test, control)
Esempio n. 21
0
 def test_tabulate1D(self):
     "Tests the transformation from a 1D MaskedArray to..."
     # Test 1D
     data1D = self.data['data1D']
     _data1D = tabulate(data1D)
     assert_equal(_data1D['_data'], data1D.filled())
     assert_equal(_data1D['_mask'], data1D.mask)
Esempio n. 22
0
 def test_clump_unmasked(self):
     # Test clump_unmasked
     a = masked_array(np.arange(10))
     a[[0, 1, 2, 6, 8, 9]] = masked
     test = clump_unmasked(a)
     control = [slice(3, 6), slice(7, 8), ]
     assert_equal(test, control)
Esempio n. 23
0
 def _test_complete_genotype_partial(self, h, h_expected):
     """Test completing a haplotype with one known entry vs. a partially-called genotype. Comprehensive
     checks for a single h and all possible genotypes g."""
     g = np.array(list(it.product(xrange(3), xrange(3))))
     h_temp = np.tile(h, (9, 1)).copy()
     gt.complete_haplotype_partial(h_temp, g)
     assert_equal(h_temp, h_expected, "Unexpected haplotype completion")
Esempio n. 24
0
 def test_convert_to_annual(self):
     "Test convert_to_annual"
     base = dict(D=1, H=24, T=24 * 60, S=24 * 3600)
     #for fq in ('D', 'H', 'T', 'S'):
     # Don't test for minuTe and Second frequency, too time consuming.
     for fq in ('D', 'H'):
         dates = date_array(start_date=Date(fq, '2001-01-01 00:00:00'),
                            end_date=Date(fq, '2004-12-31 23:59:59'))
         bq = base[fq]
         series = time_series(range(365 * bq) * 3 + range(366 * bq),
                              dates=dates)
         control = ma.masked_all((4, 366 * bq), dtype=series.dtype)
         control[0, :58 * bq] = range(58 * bq)
         control[0, 59 * bq:] = range(58 * bq, 365 * bq)
         control[[1, 2]] = control[0]
         control[3] = range(366 * bq)
         test = convert_to_annual(series)
         assert_equal(test, control)
     #
     series = time_series(range(59, 365) + range(366) + range(365),
                          start_date=Date('D', '2003-03-01'))
     test = convert_to_annual(series)
     assert_equal(test[:, 59:62],
                  ma.masked_values([[-1, 59, 60], [59, 60, 61], [-1, 59, 60]],
                                   - 1))
Esempio n. 25
0
    def test_guessfreq(self):
        "Test the guessing of frequency."

        dates = [
            (_c.FR_MIN,
             ['2001-01-01 00:00', '2001-01-01 00:30', '2001-01-01 01:00']),
            (_c.FR_MIN,
             ['2001-01-01 00:00', '2001-01-01 01:30', '2001-01-01 03:00']),
            (_c.FR_HR,
             ['2001-01-01 00:00', '2001-01-01 01:00', '2001-01-01 03:00']),
            (_c.FR_HR,
             ['2001-01-01 00:00', '2001-01-01 01:00', '2002-01-01 03:00']),
            (_c.FR_DAY,
             ['1959-01-01', '2009-07-31', '2009-08-01', '2009-08-02']),
            (_c.FR_BUS,
             ['2001-07-31', '2009-08-03', '2009-08-04']),
            (_c.FR_MTH,
             ['2001-01-01', '2001-02-01', '2002-03-01']),
            (_c.FR_MTH,
             ['2001-01-01', '2001-04-01', '2002-03-01']),
            (_c.FR_QTR,
             ['2004-01-01', '2004-04-01', '2005-01-01']),
            (_c.FR_WKTHU,
             ['2004-01-01', '2004-01-22', '2004-12-30']),
        ]

        for fr, dt in dates:
            assert_equal(guess_freq(dt), fr)
Esempio n. 26
0
 def test_ibs_segments(self):
     '''Test computing GERMLINE IBD segments.'''
     m = ig.ibd_germline(self.problem, self.sibs)
     assert_equal(m.length, 24, 'Wrong # IBD grouped segments')
     assert_segments_almost_equal(m,
                                  [((1   , 434), (17065079, 23614026, 6.549, 0), ((0, 1), (3, 1), (4, 1), (1, 1))),
                                   ((1   , 434), (17065079, 23614026, 6.549, 0), ((5, 1), (0, 0), (4, 0))),
                                   ((1   , 434), (17065079, 23614026, 6.549, 0), ((3, 0), (1, 0))),
                                   ((434 , 537), (23614026, 24732045, 1.118, 0), ((0, 1), (3, 1), (4, 1))),
                                   ((434 , 537), (23614026, 24732045, 1.118, 0), ((5, 1), (0, 0), (4, 0))),
                                   ((537 , 848), (24732045, 27321172, 2.589, 0), ((3, 0), (5, 1), (0, 0), (4, 0))),
                                   ((537 , 848), (24732045, 27321172, 2.589, 0), ((0, 1), (3, 1), (4, 1))),
                                   ((537 , 848), (24732045, 27321172, 2.589, 0), ((1, 1), (5, 0))),
                                   ((848 , 948), (27321172, 27671082, 0.350, 0), ((0, 1), (3, 1), (4, 1))),
                                   ((848 , 948), (27321172, 27671082, 0.350, 0), ((3, 0), (5, 1), (0, 0))),
                                   ((848 , 948), (27321172, 27671082, 0.350, 0), ((1, 1), (5, 0))),
                                   ((948 , 2481), (27671082, 44544028, 16.873, 0), ((0, 1), (3, 1), (4, 1))),
                                   ((948 , 2481), (27671082, 44544028, 16.873, 0), ((3, 0), (5, 1), (0, 0))),
                                   ((948 , 2481), (27671082, 44544028, 16.873, 0), ((1, 1), (5, 0))),
                                   ((948 , 2481), (27671082, 44544028, 16.873, 0), ((1, 0), (4, 0))),
                                   ((2481, 2583), (44544028, 45330235, 0.786, 0), ((0, 1), (3, 1), (4, 1))),
                                   ((2481, 2583), (44544028, 45330235, 0.786, 0), ((3, 0), (5, 1), (0, 0))),
                                   ((2481, 2583), (44544028, 45330235, 0.786, 0), ((1, 0), (4, 0))),
                                   ((2583, 3107), (45330235, 49669743, 4.340, 0), ((0, 1), (3, 1), (4, 1), (5, 0))),
                                   ((2583, 3107), (45330235, 49669743, 4.340, 0), ((3, 0), (5, 1), (0, 0))),
                                   ((2583, 3107), (45330235, 49669743, 4.340, 0), ((1, 0), (4, 0))),
                                   ((3107, 3218), (49669743, 51156933, 1.487, 0), ((0, 1), (3, 1), (4, 1), (1, 1), (5, 0))),
                                   ((3107, 3218), (49669743, 51156933, 1.487, 0), ((3, 0), (5, 1), (0, 0))),
                                   ((3107, 3218), (49669743, 51156933, 1.487, 0), ((1, 0), (4, 0)))],
                         full_data=True, decimal=3, err_msg='Wrong IBD segments')
     stats = np.array([(len(s.samples), s.length) for s in m])
     best_segment = np.lexsort((-stats[:, 1], -stats[:, 0]))[0]
     assert_equal(best_segment, 21, 'Wrong best segment (IBD set size + length)') 
Esempio n. 27
0
 def test_phase_family(self):
     '''Check phasing trivial cases in all genotyped trios.'''
     problem = io.read_plink(pedigree=itu.HUTT_PED, prefix=itu.GENOTYPE_SAMPLE, haplotype=None)
     itu.assert_size_equals(problem.genotype, 8, 1415)
     assert_equal(len(problem.trios()), 869, 'Unexpected # of genotyped trios')
     self.phaser.run(problem, PhaseParam(debug=False))
     itu.assert_problem_stats(problem, 22640, 20225, 144)
Esempio n. 28
0
    def test_phase_trivial_cases(self):
        '''Check phasing trivial cases in trios. The trio data is (0,1=parents, 2=child). The
        solution is kept in the trio test file as the fictitious individual 3.'''
        g = self.problem.genotype
        itu.assert_size_equals(self.problem.genotype, 1, 2)
        
        duo = (0, 1)
        solution = self.solution.genotype.data
        h = self.problem.haplotype
        assert_problem_stats(self.problem, 4 * g.num_snps, 0, 0)
        
        self.phaser.run(self.problem)
        
        for snp in self.problem.snp_range:
            expected_parent_genotype = solution[snp, duo[0], :]
            #expected_child_genotype  = solution[snp,trio[CHILD],:]
            expected_child_haplotype = solution[snp, duo[1], :]
            parent_genotype = g.data[snp, duo[0], :]
            #child_genotype  = g.data[snp,trio[CHILD]]
            child_haplotype = h.data[snp, duo[1]]
            '''
            print 'SNP', snp
            print 'Data', g.data[snp,trio,:]
            print 'Imputed parent', parent_genotype
            print 'Child hap', child_haplotype
            print 'Solution hap', solution[snp,trio,:]
            '''
            assert_equal(child_haplotype, expected_child_haplotype, 'Wrong child haplotype by trivial phaser at snp %d' % (snp,))
            assert_equal(parent_genotype, expected_parent_genotype, 'Wrong parent genotype imputation by trivial phaser at snp %d' % (snp,))
            #assert_equal(np.sort(child_genotype), np.sort(expected_child_genotype), 'Wrong child genotype imputation by trivial phaser at snp %d' % (snp,))

        assert_problem_stats(self.problem, 4 * g.num_snps, 4, 0)
 def test_find_phased_sibs(self):
     '''Test locating phased sibs of the unphased sample.'''
     relatives = self.__phased_sibs(self.s)
     assert_equal(relatives, [0, 1, 3, 4, 5], err_msg='Wrong phased sib set - IDs')
     for sample in relatives:
         self.assertTrue(self.problem.haplotype.fill_fraction(sample=sample) >= self.params.surrogate_parent_fill_threshold,
                         'Surrogate parent''s not phased enough')
Esempio n. 30
0
 def test_hash_rows(self):
     '''Test hashing the rows of a numpy matrix.'''
     a = np.array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [7, 8, 9]], dtype=int)
     d = dict(((Hashable(x), i) for (i, x) in enumerate(a)))
     assert_equal(len(d), 3, 'Wrong hash table size')
     # Hash value should be the last-encountered index of the corresponding row 
     assert_equal(d[Hashable(np.array([1, 2, 3]))], 2, 'Wrong hash table entry')
Esempio n. 31
0
 def test_trim(self):
     "Tests trimming"
     a = ma.arange(10)
     assert_equal(mstats.trim(a), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
     a = ma.arange(10)
     assert_equal(mstats.trim(a, (2, 8)),
                  [None, None, 2, 3, 4, 5, 6, 7, 8, None])
     a = ma.arange(10)
     assert_equal(mstats.trim(a, limits=(2, 8), inclusive=(False, False)),
                  [None, None, None, 3, 4, 5, 6, 7, None, None])
     a = ma.arange(10)
     assert_equal(mstats.trim(a, limits=(0.1, 0.2), relative=True),
                  [None, 1, 2, 3, 4, 5, 6, 7, None, None])
     #
     a = ma.arange(12)
     a[[0, -1]] = a[5] = masked
     assert_equal(mstats.trim(a, (2, 8)),
                  [None, None, 2, 3, 4, None, 6, 7, 8, None, None, None])
     #
     x = ma.arange(100).reshape(10, 10)
     trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=None)
     assert_equal(trimx._mask.ravel(), [1] * 10 + [0] * 70 + [1] * 20)
     trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=0)
     assert_equal(trimx._mask.ravel(), [1] * 10 + [0] * 70 + [1] * 20)
     trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=-1)
     assert_equal(trimx._mask.T.ravel(), [1] * 10 + [0] * 70 + [1] * 20)
     #
     x = ma.arange(110).reshape(11, 10)
     x[1] = masked
     trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=None)
     assert_equal(trimx._mask.ravel(), [1] * 20 + [0] * 70 + [1] * 20)
     trimx = mstats.trim(x, (0.1, 0.2), relative=True, axis=0)
     assert_equal(trimx._mask.ravel(), [1] * 20 + [0] * 70 + [1] * 20)
     trimx = mstats.trim(x.T, (0.1, 0.2), relative=True, axis=-1)
     assert_equal(trimx.T._mask.ravel(), [1] * 20 + [0] * 70 + [1] * 20)
Esempio n. 32
0
 def test_percentile(self):
     x = np.arange(8) * 0.5
     assert_equal(mstats.scoreatpercentile(x, 0), 0.)
     assert_equal(mstats.scoreatpercentile(x, 100), 3.5)
     assert_equal(mstats.scoreatpercentile(x, 50), 1.75)
Esempio n. 33
0
def check_named_results(res, attributes, ma=False):
    for i, attr in enumerate(attributes):
        if ma:
            ma_npt.assert_equal(res[i], getattr(res, attr))
        else:
            npt.assert_equal(res[i], getattr(res, attr))
Esempio n. 34
0
    def test_trim(self):
        a = ma.arange(10)
        assert_equal(mstats.trim(a), [0,1,2,3,4,5,6,7,8,9])
        a = ma.arange(10)
        assert_equal(mstats.trim(a,(2,8)), [None,None,2,3,4,5,6,7,8,None])
        a = ma.arange(10)
        assert_equal(mstats.trim(a,limits=(2,8),inclusive=(False,False)),
                     [None,None,None,3,4,5,6,7,None,None])
        a = ma.arange(10)
        assert_equal(mstats.trim(a,limits=(0.1,0.2),relative=True),
                     [None,1,2,3,4,5,6,7,None,None])

        a = ma.arange(12)
        a[[0,-1]] = a[5] = masked
        assert_equal(mstats.trim(a, (2,8)),
                     [None, None, 2, 3, 4, None, 6, 7, 8, None, None, None])

        x = ma.arange(100).reshape(10, 10)
        expected = [1]*10 + [0]*70 + [1]*20
        trimx = mstats.trim(x, (0.1,0.2), relative=True, axis=None)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x, (0.1,0.2), relative=True, axis=0)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x, (0.1,0.2), relative=True, axis=-1)
        assert_equal(trimx._mask.T.ravel(), expected)

        # same as above, but with an extra masked row inserted
        x = ma.arange(110).reshape(11, 10)
        x[1] = masked
        expected = [1]*20 + [0]*70 + [1]*20
        trimx = mstats.trim(x, (0.1,0.2), relative=True, axis=None)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x, (0.1,0.2), relative=True, axis=0)
        assert_equal(trimx._mask.ravel(), expected)
        trimx = mstats.trim(x.T, (0.1,0.2), relative=True, axis=-1)
        assert_equal(trimx.T._mask.ravel(), expected)
Esempio n. 35
0
 def test_ld_graph(self):
     '''Test retrieving SNPs by chromosome.'''
     chrom = 22
     g = self.ld_dao.ld_graph(chrom)
     assert_equal(g.number_of_nodes(), 2958,
                  'Wrong number of SNPs on chromosome %d' % (chrom, ))
Esempio n. 36
0
 def test_num_records(self):
     '''Test getting the total # of SNP records.'''
     assert_equal(self.ld_dao.num_ld_records(), 1394244,
                  'Wrong row count from SNP table')
     assert_equal(self.ld_dao.num_snp_records(), 271486,
                  'Wrong row count from SNP table')
Esempio n. 37
0
 def test_subclasspreservation(self):
     # Checks that masked_array(...,subok=True) preserves the class.
     x = np.arange(5)
     m = [0, 0, 1, 0, 0]
     xinfo = [(i, j) for (i, j) in zip(x, m)]
     xsub = MSubArray(x, mask=m, info={'xsub':xinfo})
     #
     mxsub = masked_array(xsub, subok=False)
     self.assertTrue(not isinstance(mxsub, MSubArray))
     self.assertTrue(isinstance(mxsub, MaskedArray))
     assert_equal(mxsub._mask, m)
     #
     mxsub = asarray(xsub)
     self.assertTrue(not isinstance(mxsub, MSubArray))
     self.assertTrue(isinstance(mxsub, MaskedArray))
     assert_equal(mxsub._mask, m)
     #
     mxsub = masked_array(xsub, subok=True)
     self.assertTrue(isinstance(mxsub, MSubArray))
     assert_equal(mxsub.info, xsub.info)
     assert_equal(mxsub._mask, xsub._mask)
     #
     mxsub = asanyarray(xsub)
     self.assertTrue(isinstance(mxsub, MSubArray))
     assert_equal(mxsub.info, xsub.info)
     assert_equal(mxsub._mask, m)
Esempio n. 38
0
 def test_masked_unary_operations(self):
     # Tests masked_unary_operation
     (x, mx) = self.data
     with np.errstate(divide='ignore'):
         self.assertTrue(isinstance(log(mx), mmatrix))
         assert_equal(log(x), np.log(x))
Esempio n. 39
0
 def test_ediff1d_tobegin(self):
     "Test ediff1d w/ to_begin"
     x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
     test = ediff1d(x, to_begin=masked)
     control = array([0, 1, 1, 1, 4], mask=[1, 1, 0, 0, 1])
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
     #
     test = ediff1d(x, to_begin=[1, 2, 3])
     control = array([1, 2, 3, 1, 1, 1, 4], mask=[0, 0, 0, 1, 0, 0, 1])
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
Esempio n. 40
0
def assert_startswith(a, b):
    # produces a better error message than assert_(a.startswith(b))
    assert_equal(a[:len(b)], b)
Esempio n. 41
0
 def test_unique_onmaskedarray(self):
     "Test unique on masked data w/use_mask=True"
     data = masked_array([1, 1, 1, 2, 2, 3], mask=[0, 0, 1, 0, 1, 0])
     test = unique(data, return_index=True, return_inverse=True)
     assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1]))
     assert_equal(test[1], [0, 3, 5, 2])
     assert_equal(test[2], [0, 0, 3, 1, 3, 2])
     #
     data.fill_value = 3
     data = masked_array([1, 1, 1, 2, 2, 3],
                         mask=[0, 0, 1, 0, 1, 0],
                         fill_value=3)
     test = unique(data, return_index=True, return_inverse=True)
     assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1]))
     assert_equal(test[1], [0, 3, 5, 2])
     assert_equal(test[2], [0, 0, 3, 1, 3, 2])
Esempio n. 42
0
 def test_ediff1d_tobegin_toend(self):
     "Test ediff1d w/ to_begin and to_end"
     x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
     test = ediff1d(x, to_end=masked, to_begin=masked)
     control = array([0, 1, 1, 1, 4, 0], mask=[1, 1, 0, 0, 1, 1])
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
     #
     test = ediff1d(x, to_end=[1, 2, 3], to_begin=masked)
     control = array([0, 1, 1, 1, 4, 1, 2, 3],
                     mask=[1, 1, 0, 0, 1, 0, 0, 0])
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
Esempio n. 43
0
 def test_mask_rowcols(self):
     "Tests mask_rowcols."
     x = array(np.arange(9).reshape(3, 3),
               mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
     assert_equal(mask_rowcols(x).mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
     assert_equal(
         mask_rowcols(x, 0).mask, [[1, 1, 1], [0, 0, 0], [0, 0, 0]])
     assert_equal(
         mask_rowcols(x, 1).mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
     x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(mask_rowcols(x).mask, [[0, 1, 0], [1, 1, 1], [0, 1, 0]])
     assert_equal(
         mask_rowcols(x, 0).mask, [[0, 0, 0], [1, 1, 1], [0, 0, 0]])
     assert_equal(
         mask_rowcols(x, 1).mask, [[0, 1, 0], [0, 1, 0], [0, 1, 0]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(mask_rowcols(x).mask, [[1, 1, 1], [1, 1, 1], [1, 1, 0]])
     assert_equal(
         mask_rowcols(x, 0).mask, [[1, 1, 1], [1, 1, 1], [0, 0, 0]])
     assert_equal(
         mask_rowcols(
             x,
             1,
         ).mask, [[1, 1, 0], [1, 1, 0], [1, 1, 0]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
     self.assertTrue(mask_rowcols(x).all() is masked)
     self.assertTrue(mask_rowcols(x, 0).all() is masked)
     self.assertTrue(mask_rowcols(x, 1).all() is masked)
     self.assertTrue(mask_rowcols(x).mask.all())
     self.assertTrue(mask_rowcols(x, 0).mask.all())
     self.assertTrue(mask_rowcols(x, 1).mask.all())
Esempio n. 44
0
 def test_unique_allmasked(self):
     "Test all masked"
     data = masked_array([1, 1, 1], mask=True)
     test = unique(data, return_index=True, return_inverse=True)
     assert_equal(test[0], masked_array([
         1,
     ], mask=[True]))
     assert_equal(test[1], [0])
     assert_equal(test[2], [0, 0, 0])
     #
     "Test masked"
     data = masked
     test = unique(data, return_index=True, return_inverse=True)
     assert_equal(test[0], masked_array(masked))
     assert_equal(test[1], [0])
     assert_equal(test[2], [0])
Esempio n. 45
0
 def test_contiguous(self):
     "Tests notmasked_contiguous"
     a = masked_array(np.arange(24).reshape(3, 8),
                      mask=[
                          [0, 0, 0, 0, 1, 1, 1, 1],
                          [1, 1, 1, 1, 1, 1, 1, 1],
                          [0, 0, 0, 0, 0, 0, 1, 0],
                      ])
     tmp = notmasked_contiguous(a, None)
     assert_equal(tmp[-1], slice(23, 24, None))
     assert_equal(tmp[-2], slice(16, 22, None))
     assert_equal(tmp[-3], slice(0, 4, None))
     #
     tmp = notmasked_contiguous(a, 0)
     self.assertTrue(len(tmp[-1]) == 1)
     self.assertTrue(tmp[-2] is None)
     assert_equal(tmp[-3], tmp[-1])
     self.assertTrue(len(tmp[0]) == 2)
     #
     tmp = notmasked_contiguous(a, 1)
     assert_equal(tmp[0][-1], slice(0, 4, None))
     self.assertTrue(tmp[1] is None)
     assert_equal(tmp[2][-1], slice(7, 8, None))
     assert_equal(tmp[2][-2], slice(0, 6, None))
Esempio n. 46
0
 def test_dot(self):
     "Tests dot product"
     n = np.arange(1, 7)
     #
     m = [1, 0, 0, 0, 0, 0]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b, True)
     assert_equal(c.mask, [[1, 1], [1, 0]])
     c = dot(b, a, True)
     assert_equal(c.mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
     c = dot(a, b, False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     m = [0, 0, 0, 0, 0, 1]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b, True)
     assert_equal(c.mask, [[0, 1], [1, 1]])
     c = dot(b, a, True)
     assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [1, 1, 1]])
     c = dot(a, b, False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     assert_equal(c, dot(a, b))
     c = dot(b, a, False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     m = [0, 0, 0, 0, 0, 0]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b)
     assert_equal(c.mask, nomask)
     c = dot(b, a)
     assert_equal(c.mask, nomask)
     #
     a = masked_array(n, mask=[1, 0, 0, 0, 0, 0]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, True)
     assert_equal(c.mask, [[1, 1], [0, 0]])
     c = dot(a, b, False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, True)
     assert_equal(c.mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
     c = dot(b, a, False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, True)
     assert_equal(c.mask, [[0, 0], [1, 1]])
     c = dot(a, b)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, True)
     assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [0, 0, 1]])
     c = dot(b, a, False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 1, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, True)
     assert_equal(c.mask, [[1, 0], [1, 1]])
     c = dot(a, b, False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, True)
     assert_equal(c.mask, [[0, 0, 1], [1, 1, 1], [0, 0, 1]])
     c = dot(b, a, False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
Esempio n. 47
0
 def test_edges(self):
     "Tests unmasked_edges"
     data = masked_array(
         np.arange(25).reshape(5, 5),
         mask=[[0, 0, 1, 0, 0], [0, 0, 0, 1, 1], [1, 1, 0, 0, 0],
               [0, 0, 0, 0, 0], [1, 1, 1, 0, 0]],
     )
     test = notmasked_edges(data, None)
     assert_equal(test, [0, 24])
     test = notmasked_edges(data, 0)
     assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
     assert_equal(test[1], [(3, 3, 3, 4, 4), (0, 1, 2, 3, 4)])
     test = notmasked_edges(data, 1)
     assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 2, 0, 3)])
     assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 2, 4, 4, 4)])
     #
     test = notmasked_edges(data.data, None)
     assert_equal(test, [0, 24])
     test = notmasked_edges(data.data, 0)
     assert_equal(test[0], [(0, 0, 0, 0, 0), (0, 1, 2, 3, 4)])
     assert_equal(test[1], [(4, 4, 4, 4, 4), (0, 1, 2, 3, 4)])
     test = notmasked_edges(data.data, -1)
     assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 0, 0, 0)])
     assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 4, 4, 4, 4)])
     #
     data[-2] = masked
     test = notmasked_edges(data, 0)
     assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
     assert_equal(test[1], [(1, 1, 2, 4, 4), (0, 1, 2, 3, 4)])
     test = notmasked_edges(data, -1)
     assert_equal(test[0], [(0, 1, 2, 4), (0, 0, 2, 3)])
     assert_equal(test[1], [(0, 1, 2, 4), (4, 2, 4, 4)])
Esempio n. 48
0
 def test_compress2d(self):
     "Tests compress2d"
     x = array(np.arange(9).reshape(3, 3),
               mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[4, 5], [7, 8]])
     assert_equal(compress_rowcols(x, 0), [[3, 4, 5], [6, 7, 8]])
     assert_equal(compress_rowcols(x, 1), [[1, 2], [4, 5], [7, 8]])
     x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[0, 2], [6, 8]])
     assert_equal(compress_rowcols(x, 0), [[0, 1, 2], [6, 7, 8]])
     assert_equal(compress_rowcols(x, 1), [[0, 2], [3, 5], [6, 8]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[8]])
     assert_equal(compress_rowcols(x, 0), [[6, 7, 8]])
     assert_equal(compress_rowcols(
         x,
         1,
     ), [[2], [5], [8]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
     assert_equal(compress_rowcols(x).size, 0)
     assert_equal(compress_rowcols(x, 0).size, 0)
     assert_equal(compress_rowcols(x, 1).size, 0)
Esempio n. 49
0
 def test_testAverage3(self):
     "Yet more tests of average!"
     a = arange(6)
     b = arange(6) * 3
     r1, w1 = average([[a, b], [b, a]], axis=1, returned=1)
     assert_equal(shape(r1), shape(w1))
     assert_equal(r1.shape, w1.shape)
     r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1)
     assert_equal(shape(w2), shape(r2))
     r2, w2 = average(ones((2, 2, 3)), returned=1)
     assert_equal(shape(w2), shape(r2))
     r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1)
     assert_equal(shape(w2), shape(r2))
     a2d = array([[1, 2], [0, 4]], float)
     a2dm = masked_array(a2d, [[False, False], [True, False]])
     a2da = average(a2d, axis=0)
     assert_equal(a2da, [0.5, 3.0])
     a2dma = average(a2dm, axis=0)
     assert_equal(a2dma, [1.0, 3.0])
     a2dma = average(a2dm, axis=None)
     assert_equal(a2dma, 7. / 3.)
     a2dma = average(a2dm, axis=1)
     assert_equal(a2dma, [1.5, 4.0])
Esempio n. 50
0
 def test_masked_all(self):
     "Tests masked_all"
     # Standard dtype
     test = masked_all((2, ), dtype=float)
     control = array([1, 1], mask=[1, 1], dtype=float)
     assert_equal(test, control)
     # Flexible dtype
     dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']})
     test = masked_all((2, ), dtype=dt)
     control = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt)
     assert_equal(test, control)
     test = masked_all((2, 2), dtype=dt)
     control = array([[(0, 0), (0, 0)], [(0, 0), (0, 0)]],
                     mask=[[(1, 1), (1, 1)], [(1, 1), (1, 1)]],
                     dtype=dt)
     assert_equal(test, control)
     # Nested dtype
     dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])])
     test = masked_all((2, ), dtype=dt)
     control = array([(1, (1, 1)), (1, (1, 1))],
                     mask=[(1, (1, 1)), (1, (1, 1))],
                     dtype=dt)
     assert_equal(test, control)
     test = masked_all((2, ), dtype=dt)
     control = array([(1, (1, 1)), (1, (1, 1))],
                     mask=[(1, (1, 1)), (1, (1, 1))],
                     dtype=dt)
     assert_equal(test, control)
     test = masked_all((1, 1), dtype=dt)
     control = array([[(1, (1, 1))]], mask=[[(1, (1, 1))]], dtype=dt)
     assert_equal(test, control)
Esempio n. 51
0
 def test_testAverage1(self):
     "Test of average."
     ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
     assert_equal(2.0, average(ott, axis=0))
     assert_equal(2.0, average(ott, weights=[1., 1., 2., 1.]))
     result, wts = average(ott, weights=[1., 1., 2., 1.], returned=1)
     assert_equal(2.0, result)
     self.assertTrue(wts == 4.0)
     ott[:] = masked
     assert_equal(average(ott, axis=0).mask, [True])
     ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
     ott = ott.reshape(2, 2)
     ott[:, 1] = masked
     assert_equal(average(ott, axis=0), [2.0, 0.0])
     assert_equal(average(ott, axis=1).mask[0], [True])
     assert_equal([2., 0.], average(ott, axis=0))
     result, wts = average(ott, axis=0, returned=1)
     assert_equal(wts, [1., 0.])
Esempio n. 52
0
 def test_onintegers_with_mask(self):
     "Test average on integers with mask"
     a = average(array([1, 2]))
     assert_equal(a, 1.5)
     a = average(array([1, 2, 3, 4], mask=[False, False, True, True]))
     assert_equal(a, 1.5)
Esempio n. 53
0
    def test_zip_descr(self):
        # Test zip_descr
        (w, x, y, z) = self.data

        # Std array
        test = zip_descr((x, x), flatten=True)
        assert_equal(test, np.dtype([('', int), ('', int)]))
        test = zip_descr((x, x), flatten=False)
        assert_equal(test, np.dtype([('', int), ('', int)]))

        # Std & flexible-dtype
        test = zip_descr((x, z), flatten=True)
        assert_equal(test, np.dtype([('', int), ('A', '|S3'), ('B', float)]))
        test = zip_descr((x, z), flatten=False)
        assert_equal(test,
                     np.dtype([('', int), ('', [('A', '|S3'), ('B', float)])]))

        # Standard & nested dtype
        test = zip_descr((x, w), flatten=True)
        assert_equal(
            test, np.dtype([('', int), ('a', int), ('ba', float),
                            ('bb', int)]))
        test = zip_descr((x, w), flatten=False)
        assert_equal(
            test,
            np.dtype([('', int),
                      ('', [('a', int), ('b', [('ba', float),
                                               ('bb', int)])])]))
Esempio n. 54
0
 def test_testAverage2(self):
     "More tests of average."
     w1 = [0, 1, 1, 1, 1, 0]
     w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
     x = arange(6, dtype=np.float_)
     assert_equal(average(x, axis=0), 2.5)
     assert_equal(average(x, axis=0, weights=w1), 2.5)
     y = array([arange(6, dtype=np.float_), 2.0 * arange(6)])
     assert_equal(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.)
     assert_equal(average(y, axis=0), np.arange(6) * 3. / 2.)
     assert_equal(
         average(y, axis=1),
         [average(x, axis=0), average(x, axis=0) * 2.0])
     assert_equal(average(y, None, weights=w2), 20. / 6.)
     assert_equal(average(y, axis=0, weights=w2), [0., 1., 2., 3., 4., 10.])
     assert_equal(
         average(y, axis=1),
         [average(x, axis=0), average(x, axis=0) * 2.0])
     m1 = zeros(6)
     m2 = [0, 0, 1, 1, 0, 0]
     m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
     m4 = ones(6)
     m5 = [0, 1, 1, 1, 1, 1]
     assert_equal(average(masked_array(x, m1), axis=0), 2.5)
     assert_equal(average(masked_array(x, m2), axis=0), 2.5)
     assert_equal(average(masked_array(x, m4), axis=0).mask, [True])
     assert_equal(average(masked_array(x, m5), axis=0), 0.0)
     assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
     z = masked_array(y, m3)
     assert_equal(average(z, None), 20. / 6.)
     assert_equal(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5])
     assert_equal(average(z, axis=1), [2.5, 5.0])
     assert_equal(average(z, axis=0, weights=w2),
                  [0., 1., 99., 99., 4.0, 10.0])
Esempio n. 55
0
    def test_structured_to_unstructured(self):
        a = np.zeros(4, dtype=[('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)])
        out = structured_to_unstructured(a)
        assert_equal(out, np.zeros((4, 5), dtype='f8'))

        b = np.array([(1, 2, 5), (4, 5, 7), (7, 8, 11), (10, 11, 12)],
                     dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')])
        out = np.mean(structured_to_unstructured(b[['x', 'z']]), axis=-1)
        assert_equal(out, np.array([3., 5.5, 9., 11.]))
        out = np.mean(structured_to_unstructured(b[['x']]), axis=-1)
        assert_equal(out, np.array([1., 4., 7., 10.]))

        c = np.arange(20).reshape((4, 5))
        out = unstructured_to_structured(c, a.dtype)
        want = np.array([(0, (1., 2), [3., 4.]), (5, (6., 7), [8., 9.]),
                         (10, (11., 12), [13., 14.]),
                         (15, (16., 17), [18., 19.])],
                        dtype=[('a', 'i4'), ('b', [('f0', 'f4'),
                                                   ('f1', 'u2')]),
                               ('c', 'f4', (2, ))])
        assert_equal(out, want)

        d = np.array([(1, 2, 5), (4, 5, 7), (7, 8, 11), (10, 11, 12)],
                     dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')])
        assert_equal(apply_along_fields(np.mean, d),
                     np.array([8.0 / 3, 16.0 / 3, 26.0 / 3, 11.]))
        assert_equal(apply_along_fields(np.mean, d[['x', 'z']]),
                     np.array([3., 5.5, 9., 11.]))

        # check that for uniform field dtypes we get a view, not a copy:
        d = np.array([(1, 2, 5), (4, 5, 7), (7, 8, 11), (10, 11, 12)],
                     dtype=[('x', 'i4'), ('y', 'i4'), ('z', 'i4')])
        dd = structured_to_unstructured(d)
        ddd = unstructured_to_structured(dd, d.dtype)
        assert_(dd.base is d)
        assert_(ddd.base is d)

        # including uniform fields with subarrays unpacked
        d = np.array([(1, [2, 3], [[4, 5], [6, 7]]),
                      (8, [9, 10], [[11, 12], [13, 14]])],
                     dtype=[('x0', 'i4'), ('x1', ('i4', 2)),
                            ('x2', ('i4', (2, 2)))])
        dd = structured_to_unstructured(d)
        ddd = unstructured_to_structured(dd, d.dtype)
        assert_(dd.base is d)
        assert_(ddd.base is d)

        # test that nested fields with identical names don't break anything
        point = np.dtype([('x', int), ('y', int)])
        triangle = np.dtype([('a', point), ('b', point), ('c', point)])
        arr = np.zeros(10, triangle)
        res = structured_to_unstructured(arr, dtype=int)
        assert_equal(res, np.zeros((10, 6), dtype=int))

        # test nested combinations of subarrays and structured arrays, gh-13333
        def subarray(dt, shape):
            return np.dtype((dt, shape))

        def structured(*dts):
            return np.dtype([('x{}'.format(i), dt)
                             for i, dt in enumerate(dts)])

        def inspect(dt, dtype=None):
            arr = np.zeros((), dt)
            ret = structured_to_unstructured(arr, dtype=dtype)
            backarr = unstructured_to_structured(ret, dt)
            return ret.shape, ret.dtype, backarr.dtype

        dt = structured(subarray(structured(np.int32, np.int32), 3))
        assert_equal(inspect(dt), ((6, ), np.int32, dt))

        dt = structured(subarray(subarray(np.int32, 2), 2))
        assert_equal(inspect(dt), ((4, ), np.int32, dt))

        dt = structured(np.int32)
        assert_equal(inspect(dt), ((1, ), np.int32, dt))

        dt = structured(np.int32, subarray(subarray(np.int32, 2), 2))
        assert_equal(inspect(dt), ((5, ), np.int32, dt))

        dt = structured()
        assert_raises(ValueError, structured_to_unstructured, np.zeros(3, dt))

        # these currently don't work, but we may make it work in the future
        assert_raises(NotImplementedError,
                      structured_to_unstructured,
                      np.zeros(3, dt),
                      dtype=np.int32)
        assert_raises(NotImplementedError, unstructured_to_structured,
                      np.zeros((3, 0), dtype=np.int32))
Esempio n. 56
0
 def test_3d(self):
     a = arange(12.).reshape(2, 2, 3)
     def myfunc(b):
         return b[1]
     xa = apply_along_axis(myfunc, 2, a)
     assert_equal(xa, [[1, 4], [7, 10]])
Esempio n. 57
0
    def test_mode(self):
        a1 = [0,0,0,1,1,1,2,3,3,3,3,4,5,6,7]
        a2 = np.reshape(a1, (3,5))
        a3 = np.array([1,2,3,4,5,6])
        a4 = np.reshape(a3, (3,2))
        ma1 = ma.masked_where(ma.array(a1) > 2, a1)
        ma2 = ma.masked_where(a2 > 2, a2)
        ma3 = ma.masked_where(a3 < 2, a3)
        ma4 = ma.masked_where(ma.array(a4) < 2, a4)
        assert_equal(mstats.mode(a1, axis=None), (3,4))
        assert_equal(mstats.mode(a1, axis=0), (3,4))
        assert_equal(mstats.mode(ma1, axis=None), (0,3))
        assert_equal(mstats.mode(a2, axis=None), (3,4))
        assert_equal(mstats.mode(ma2, axis=None), (0,3))
        assert_equal(mstats.mode(a3, axis=None), (1,1))
        assert_equal(mstats.mode(ma3, axis=None), (2,1))
        assert_equal(mstats.mode(a2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
        assert_equal(mstats.mode(ma2, axis=0), ([[0,0,0,1,1]], [[1,1,1,1,1]]))
        assert_equal(mstats.mode(a2, axis=-1), ([[0],[3],[3]], [[3],[3],[1]]))
        assert_equal(mstats.mode(ma2, axis=-1), ([[0],[1],[0]], [[3],[1],[0]]))
        assert_equal(mstats.mode(ma4, axis=0), ([[3,2]], [[1,1]]))
        assert_equal(mstats.mode(ma4, axis=-1), ([[2],[3],[5]], [[1],[1],[1]]))

        a1_res = mstats.mode(a1, axis=None)

        # test for namedtuple attributes
        attributes = ('mode', 'count')
        check_named_results(a1_res, attributes, ma=True)
Esempio n. 58
0
    def test_field_assignment_by_name(self):
        a = np.ones(2, dtype=[('a', 'i4'), ('b', 'f8'), ('c', 'u1')])
        newdt = [('b', 'f4'), ('c', 'u1')]

        assert_equal(require_fields(a, newdt), np.ones(2, newdt))

        b = np.array([(1, 2), (3, 4)], dtype=newdt)
        assign_fields_by_name(a, b, zero_unassigned=False)
        assert_equal(a, np.array([(1, 1, 2), (1, 3, 4)], dtype=a.dtype))
        assign_fields_by_name(a, b)
        assert_equal(a, np.array([(0, 1, 2), (0, 3, 4)], dtype=a.dtype))

        # test nested fields
        a = np.ones(2, dtype=[('a', [('b', 'f8'), ('c', 'u1')])])
        newdt = [('a', [('c', 'u1')])]
        assert_equal(require_fields(a, newdt), np.ones(2, newdt))
        b = np.array([((2, ), ), ((3, ), )], dtype=newdt)
        assign_fields_by_name(a, b, zero_unassigned=False)
        assert_equal(a, np.array([((1, 2), ), ((1, 3), )], dtype=a.dtype))
        assign_fields_by_name(a, b)
        assert_equal(a, np.array([((0, 2), ), ((0, 3), )], dtype=a.dtype))

        # test unstructured code path for 0d arrays
        a, b = np.array(3), np.array(0)
        assign_fields_by_name(b, a)
        assert_equal(b[()], 3)
Esempio n. 59
0
 def test_trim_old(self):
     x = ma.arange(100)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x,tail='r').count(), 80)
     x[50:70] = masked
     trimx = mstats.trimboth(x)
     assert_equal(trimx.count(), 48)
     assert_equal(trimx._mask, [1]*16 + [0]*34 + [1]*20 + [0]*14 + [1]*16)
     x._mask = nomask
     x.shape = (10,10)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x).count(), 80)
Esempio n. 60
0
    def test_find_duplicates(self):
        # Test find_duplicates
        a = ma.array([(2, (2., 'B')), (1, (2., 'B')), (2, (2., 'B')),
                      (1, (1., 'B')), (2, (2., 'B')), (2, (2., 'C'))],
                     mask=[(0, (0, 0)), (0, (0, 0)), (0, (0, 0)), (0, (0, 0)),
                           (1, (0, 0)), (0, (1, 0))],
                     dtype=[('A', int), ('B', [('BA', float), ('BB', '|S1')])])

        test = find_duplicates(a, ignoremask=False, return_index=True)
        control = [0, 2]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='A', return_index=True)
        control = [0, 1, 2, 3, 5]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='B', return_index=True)
        control = [0, 1, 2, 4]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='BA', return_index=True)
        control = [0, 1, 2, 4]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='BB', return_index=True)
        control = [0, 1, 2, 3, 4]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])