def common_ma_setup(): data2D = ma.array([np.random.rand(25).reshape(5,5), np.random.rand(25).reshape(5,5), np.random.rand(25).reshape(5,5), np.random.rand(25).reshape(5,5), np.random.rand(25).reshape(5,5),], mask=[np.random.rand(25).reshape(5,5)>.5, np.random.rand(25).reshape(5,5)>.5, np.random.rand(25).reshape(5,5)>.5, np.random.rand(25).reshape(5,5)>.5, np.random.rand(25).reshape(5,5)>.5,] ) data1D = ma.array(np.random.rand(25), mask=np.random.rand(25)>0.9, fill_value=-9999) dtype5R = [('a',float),('b',int),('c','|S3')] data5N = ma.array(zip(np.random.rand(5), np.arange(5), 'ABCDE'), dtype=dtype5R) data5R = mr.fromarrays([np.random.rand(5), np.arange(5), ('A','B','C','D','E')], dtype=dtype5R) data5R._mask['a'][0]=True data5R._mask['b'][2]=True data5R._mask['c'][-1]=True return dict(data1D=data1D, data2D=data2D, data5N=data5N, data5R=data5R)
def setUp(self): (a, b) = (np.arange(10), np.random.rand(10)) ndtype = [('a', np.float), ('b', np.float)] arr = np.array(list(zip(a, b)), dtype=ndtype) mrec = fromarrays([a, b], dtype=ndtype, fill_value=(-9., -99.)) mrec.mask[3] = (False, True) self.data = (mrec, a, b, arr)
def setup(self): "Generic setup" _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[0, 0, 1], dtype="|S8") ddtype = [("a", int), ("b", float), ("c", "|S8")] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(99999, 99999.0, "N/A")) nrec = recfromarrays((_a.data, _b.data, _c.data), dtype=ddtype) self.data = (mrec, nrec, ddtype)
def test_tolist(self): # Test tolist. _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[1, 0, 0], dtype="|S8") ddtype = [("a", int), ("b", float), ("c", "|S8")] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(99999, 99999.0, "N/A")) # assert_equal(mrec.tolist(), [(1, 1.1, None), (2, 2.2, asbytes("two")), (None, None, asbytes("three"))])
def setup(self): # Generic setup _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(list(map(asbytes, ["one", "two", "three"])), mask=[0, 0, 1], dtype="|S8") ddtype = [("a", int), ("b", float), ("c", "|S8")] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(asbytes("99999"), asbytes("99999."), asbytes("N/A"))) nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype) self.data = (mrec, nrec, ddtype)
def test_fromarrays(self): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8') (mrec, nrec, _) = self.data for (f, l) in zip(('a', 'b', 'c'), (_a, _b, _c)): assert_equal(getattr(mrec, f)._mask, l._mask) # One record only _x = ma.array([1, 1.1, 'one'], mask=[1, 0, 0],) assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[0])
def setup(self): "Generic setup" _a = ma.array([1,2,3],mask=[0,0,1],dtype=int) _b = ma.array([1.1,2.2,3.3],mask=[0,0,1],dtype=float) _c = ma.array(['one','two','three'],mask=[0,0,1],dtype='|S8') ddtype = [('a',int),('b',float),('c','|S8')] mrec = fromarrays([_a,_b,_c], dtype=ddtype, fill_value=(99999,99999.,'N/A')) nrec = recfromarrays((_a._data,_b._data,_c._data), dtype=ddtype) self.data = (mrec, nrec, ddtype)
def test_fromarrays(self): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[0, 0, 1], dtype="|S8") (mrec, nrec, _) = self.data for (f, l) in zip(("a", "b", "c"), (_a, _b, _c)): assert_equal(getattr(mrec, f)._mask, l._mask) # One record only _x = ma.array([1, 1.1, "one"], mask=[1, 0, 0]) assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[0])
def test_filled(self): # Test filling the array _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[0, 0, 1], dtype="|S8") ddtype = [("a", int), ("b", float), ("c", "|S8")] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(99999, 99999.0, "N/A")) mrecfilled = mrec.filled() assert_equal(mrecfilled["a"], np.array((1, 2, 99999), dtype=int)) assert_equal(mrecfilled["b"], np.array((1.1, 2.2, 99999.0), dtype=float)) assert_equal(mrecfilled["c"], np.array(("one", "two", "N/A"), dtype="|S8"))
def test_tolist(self): "Test tolist." _a = ma.array([1,2,3],mask=[0,0,1],dtype=int) _b = ma.array([1.1,2.2,3.3],mask=[0,0,1],dtype=float) _c = ma.array(['one','two','three'],mask=[1,0,0],dtype='|S8') ddtype = [('a',int),('b',float),('c','|S8')] mrec = fromarrays([_a,_b,_c], dtype=ddtype, fill_value=(99999,99999.,'N/A')) # assert_equal(mrec.tolist(), [(1,1.1,None),(2,2.2,'two'),(None,None,'three')])
def test_filled(self): "Test filling the array" _a = ma.array([1,2,3],mask=[0,0,1],dtype=int) _b = ma.array([1.1,2.2,3.3],mask=[0,0,1],dtype=float) _c = ma.array(['one','two','three'],mask=[0,0,1],dtype='|S8') ddtype = [('a',int),('b',float),('c','|S8')] mrec = fromarrays([_a,_b,_c], dtype=ddtype, fill_value=(99999,99999.,'N/A')) mrecfilled = mrec.filled() assert_equal(mrecfilled['a'], np.array((1,2,99999), dtype=int)) assert_equal(mrecfilled['b'], np.array((1.1,2.2,99999.), dtype=float)) assert_equal(mrecfilled['c'], np.array(('one','two','N/A'), dtype='|S8'))
def setup(self): # Generic setup _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(list(map(asbytes, ['one', 'two', 'three'])), mask=[0, 0, 1], dtype='|S8') ddtype = [('a', int), ('b', float), ('c', '|S8')] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(asbytes('99999'), asbytes('99999.'), asbytes('N/A'))) nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype) self.data = (mrec, nrec, ddtype)
def test_filled(self): # Test filling the array _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[0, 0, 1], dtype="|S8") ddtype = [("a", int), ("b", float), ("c", "|S8")] mrec = fromarrays( [_a, _b, _c], dtype=ddtype, fill_value=(99999, 99999.0, "N/A") ) mrecfilled = mrec.filled() assert_equal(mrecfilled["a"], np.array((1, 2, 99999), dtype=int)) assert_equal(mrecfilled["b"], np.array((1.1, 2.2, 99999.0), dtype=float)) assert_equal(mrecfilled["c"], np.array(("one", "two", "N/A"), dtype="|S8"))
def setup(self): "Generic setup" d = np.arange(5) m = ma.make_mask([1, 0, 0, 1, 1]) base_d = np.r_[d, d[::-1]].reshape(2, -1).T base_m = np.r_[[m, m[::-1]]].T base = ma.array(base_d, mask=base_m) mrec = mr.fromarrays(base.T, ) dlist = ['2007-%02i' % (i + 1) for i in d] dates = date_array(dlist) mts = time_series(mrec, dates) rts = time_records(mrec, dates) self.data = [d, m, mrec, dlist, dates, mts, rts]
def test_tolist(self): # Test tolist. _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[1, 0, 0], dtype="|S8") ddtype = [("a", int), ("b", float), ("c", "|S8")] mrec = fromarrays( [_a, _b, _c], dtype=ddtype, fill_value=(99999, 99999.0, "N/A") ) assert_equal( mrec.tolist(), [(1, 1.1, None), (2, 2.2, b"two"), (None, None, b"three")] )
def test_set_fields_mask(self): # Tests setting the mask of a field. base = self.base.copy() # This one has already a mask.... mbase = base.view(mrecarray) mbase["a"][-2] = masked assert_equal(mbase.a, [1, 2, 3, 4, 5]) assert_equal(mbase.a._mask, [0, 1, 0, 1, 1]) # This one has not yet mbase = fromarrays([np.arange(5), np.random.rand(5)], dtype=[("a", int), ("b", float)]) mbase["a"][-2] = masked assert_equal(mbase.a, [0, 1, 2, 3, 4]) assert_equal(mbase.a._mask, [0, 0, 0, 1, 0])
def setup(self): "Generic setup" d = np.arange(5) m = ma.make_mask([1, 0, 0, 1, 1]) base_d = np.r_[d, d[::-1]].reshape(2, -1).T base_m = np.r_[[m, m[::-1]]].T base = ma.array(base_d, mask=base_m) mrec = mr.fromarrays(base.T,) dlist = ['2007-%02i' % (i + 1) for i in d] dates = date_array(dlist) mts = time_series(mrec, dates) rts = time_records(mrec, dates) self.data = [d, m, mrec, dlist, dates, mts, rts]
def test_fromarrays(self): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8') (mrec, nrec, _) = self.data for (f, l) in zip(('a', 'b', 'c'), (_a, _b, _c)): assert_equal(getattr(mrec, f)._mask, l._mask) # One record only _x = ma.array( [1, 1.1, 'one'], mask=[1, 0, 0], ) assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[0])
def test_fromarrays(self): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[0, 0, 1], dtype="|S8") (mrec, nrec, _) = self.data for (f, l) in zip(("a", "b", "c"), (_a, _b, _c)): assert_equal(getattr(mrec, f)._mask, l._mask) # One record only _x = ma.array( [1, 1.1, "one"], mask=[1, 0, 0], ) assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[0])
def setup(self): "Generic setup" _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(list(map(asbytes, ['one', 'two', 'three'])), mask=[0, 0, 1], dtype='|S8') ddtype = [('a', int), ('b', float), ('c', '|S8')] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(asbytes('99999'), asbytes('99999.'), asbytes('N/A'))) nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype) self.data = (mrec, nrec, ddtype)
def test_set_fields_mask(self): "Tests setting the mask of a field." base = self.base.copy() # This one has already a mask.... mbase = base.view(mrecarray) mbase['a'][-2] = masked assert_equal(mbase.a, [1, 2, 3, 4, 5]) assert_equal(mbase.a._mask, [0, 1, 0, 1, 1]) # This one has not yet mbase = fromarrays([np.arange(5), np.random.rand(5)], dtype=[('a', int), ('b', float)]) mbase['a'][-2] = masked assert_equal(mbase.a, [0, 1, 2, 3, 4]) assert_equal(mbase.a._mask, [0, 0, 0, 1, 0])
def test_filled(self): # Test filling the array _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8') ddtype = [('a', int), ('b', float), ('c', '|S8')] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(99999, 99999., 'N/A')) mrecfilled = mrec.filled() assert_equal(mrecfilled['a'], np.array((1, 2, 99999), dtype=int)) assert_equal(mrecfilled['b'], np.array((1.1, 2.2, 99999.), dtype=float)) assert_equal(mrecfilled['c'], np.array(('one', 'two', 'N/A'), dtype='|S8'))
class TestMRecordsImport(object): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array([b"one", b"two", b"three"], mask=[0, 0, 1], dtype="|S8") ddtype = [("a", int), ("b", float), ("c", "|S8")] mrec = fromarrays( [_a, _b, _c], dtype=ddtype, fill_value=(b"99999", b"99999.", b"N/A") ) nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype) data = (mrec, nrec, ddtype) def test_fromarrays(self): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(["one", "two", "three"], mask=[0, 0, 1], dtype="|S8") (mrec, nrec, _) = self.data for (f, l) in zip(("a", "b", "c"), (_a, _b, _c)): assert_equal(getattr(mrec, f)._mask, l._mask) # One record only _x = ma.array([1, 1.1, "one"], mask=[1, 0, 0],) assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[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) def test_fromrecords_wmask(self): # Tests construction from records w/ mask. (mrec, nrec, ddtype) = self.data _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=[0, 1, 0,]) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), [(0, 0, 0), (1, 1, 1), (0, 0, 0)]) _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=True) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), [(1, 1, 1), (1, 1, 1), (1, 1, 1)]) _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=mrec._mask) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), mrec._mask.tolist()) _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=mrec._mask.tolist()) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), mrec._mask.tolist()) def test_fromtextfile(self): # Tests reading from a text file. fcontent = """# 'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)' 'strings',1,1.0,'mixed column',,1 'with embedded "double quotes"',2,2.0,1.0,,1 'strings',3,3.0E5,3,,1 'strings',4,-1e-10,,,1 """ with temppath() as path: with open(path, "w") as f: f.write(fcontent) mrectxt = fromtextfile(path, delimitor=",", varnames="ABCDEFG") assert_(isinstance(mrectxt, MaskedRecords)) assert_equal(mrectxt.F, [1, 1, 1, 1]) assert_equal(mrectxt.E._mask, [1, 1, 1, 1]) assert_equal(mrectxt.C, [1, 2, 3.0e5, -1e-10]) 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)
class TestMRecordsImport(object): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array([b'one', b'two', b'three'], mask=[0, 0, 1], dtype='|S8') ddtype = [('a', int), ('b', float), ('c', '|S8')] mrec = fromarrays([_a, _b, _c], dtype=ddtype, fill_value=(b'99999', b'99999.', b'N/A')) nrec = recfromarrays((_a._data, _b._data, _c._data), dtype=ddtype) data = (mrec, nrec, ddtype) def test_fromarrays(self): _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int) _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float) _c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8') (mrec, nrec, _) = self.data for (f, l) in zip(('a', 'b', 'c'), (_a, _b, _c)): assert_equal(getattr(mrec, f)._mask, l._mask) # One record only _x = ma.array( [1, 1.1, 'one'], mask=[1, 0, 0], ) assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[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) def test_fromrecords_wmask(self): # Tests construction from records w/ mask. (mrec, nrec, ddtype) = self.data _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=[ 0, 1, 0, ]) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), [(0, 0, 0), (1, 1, 1), (0, 0, 0)]) _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=True) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), [(1, 1, 1), (1, 1, 1), (1, 1, 1)]) _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=mrec._mask) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), mrec._mask.tolist()) _mrec = fromrecords(nrec.tolist(), dtype=ddtype, mask=mrec._mask.tolist()) assert_equal_records(_mrec._data, mrec._data) assert_equal(_mrec._mask.tolist(), mrec._mask.tolist()) def test_fromtextfile(self): # Tests reading from a text file. fcontent = ("""# 'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)' 'strings',1,1.0,'mixed column',,1 'with embedded "double quotes"',2,2.0,1.0,,1 'strings',3,3.0E5,3,,1 'strings',4,-1e-10,,,1 """) with temppath() as path: with open(path, 'w') as f: f.write(fcontent) mrectxt = fromtextfile(path, delimitor=',', varnames='ABCDEFG') assert_(isinstance(mrectxt, MaskedRecords)) assert_equal(mrectxt.F, [1, 1, 1, 1]) assert_equal(mrectxt.E._mask, [1, 1, 1, 1]) assert_equal(mrectxt.C, [1, 2, 3.e+5, -1e-10]) 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)