def test_usecols(self): "Test the selection of columns" # Select 1 column control = np.array( [[1, 2], [3, 4]], float) data = StringIO.StringIO() np.savetxt(data, control) data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=(1,)) assert_equal(test, control[:, 1]) # control = np.array( [[1, 2, 3], [3, 4, 5]], float) data = StringIO.StringIO() np.savetxt(data, control) data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=(1, 2)) assert_equal(test, control[:, 1:]) # Testing with arrays instead of tuples. data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=np.array([1, 2])) assert_equal(test, control[:, 1:]) # Checking with dtypes defined converters. data = StringIO.StringIO("""JOE 70.1 25.3\nBOB 60.5 27.9""") names = ['stid', 'temp'] dtypes = ['S4', 'f8'] test = np.ndfromtxt(data, usecols=(0, 2), dtype=zip(names, dtypes)) assert_equal(test['stid'], ["JOE", "BOB"]) assert_equal(test['temp'], [25.3, 27.9])
def test_usecols(self): "Test the selection of columns" # Select 1 column control = np.array([[1, 2], [3, 4]], float) data = StringIO.StringIO() np.savetxt(data, control) data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=(1, )) assert_equal(test, control[:, 1]) # control = np.array([[1, 2, 3], [3, 4, 5]], float) data = StringIO.StringIO() np.savetxt(data, control) data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=(1, 2)) assert_equal(test, control[:, 1:]) # Testing with arrays instead of tuples. data.seek(0) test = np.ndfromtxt(data, dtype=float, usecols=np.array([1, 2])) assert_equal(test, control[:, 1:]) # Checking with dtypes defined converters. data = StringIO.StringIO("""JOE 70.1 25.3\nBOB 60.5 27.9""") names = ['stid', 'temp'] dtypes = ['S4', 'f8'] test = np.ndfromtxt(data, usecols=(0, 2), dtype=zip(names, dtypes)) assert_equal(test['stid'], ["JOE", "BOB"]) assert_equal(test['temp'], [25.3, 27.9])
def test_dtype_with_converters(self): dstr = "2009; 23; 46" test = np.ndfromtxt(StringIO(dstr), delimiter=";", dtype=float, converters={0: bytes}) control = np.array([("2009", 23.0, 46)], dtype=[("f0", "|S4"), ("f1", float), ("f2", float)]) assert_equal(test, control) test = np.ndfromtxt(StringIO(dstr), delimiter=";", dtype=float, converters={0: float}) control = np.array([2009.0, 23.0, 46]) assert_equal(test, control)
def test_unused_converter(self): "Test whether unused converters are forgotten" data = StringIO("1 21\n 3 42\n") test = np.ndfromtxt(data, usecols=(1,), converters={0: lambda s: int(s, 16)}) assert_equal(test, [21, 42]) # data.seek(0) test = np.ndfromtxt(data, usecols=(1,), converters={1: lambda s: int(s, 16)}) assert_equal(test, [33, 66])
def test_autostrip(self): "Test autostrip" data = "01/01/2003 , 1.3, abcde" kwargs = dict(delimiter=",", dtype=None) mtest = np.ndfromtxt(StringIO(data), **kwargs) ctrl = np.array([("01/01/2003 ", 1.3, " abcde")], dtype=[("f0", "|S12"), ("f1", float), ("f2", "|S8")]) assert_equal(mtest, ctrl) mtest = np.ndfromtxt(StringIO(data), autostrip=True, **kwargs) ctrl = np.array([("01/01/2003", 1.3, "abcde")], dtype=[("f0", "|S10"), ("f1", float), ("f2", "|S5")]) assert_equal(mtest, ctrl)
def test_comments(self): "Test the stripping of comments" control = np.array([1, 2, 3, 5], int) # Comment on its own line data = StringIO("# comment\n1,2,3,5\n") test = np.ndfromtxt(data, dtype=int, delimiter=asbytes(","), comments=asbytes("#")) assert_equal(test, control) # Comment at the end of a line data = StringIO("1,2,3,5# comment\n") test = np.ndfromtxt(data, dtype=int, delimiter=asbytes(","), comments=asbytes("#")) assert_equal(test, control)
def test_1D(self): "Test squeezing to 1D" control = np.array([1, 2, 3, 4], int) # data = StringIO.StringIO('1\n2\n3\n4\n') test = np.ndfromtxt(data, dtype=int) assert_array_equal(test, control) # data = StringIO.StringIO('1,2,3,4\n') test = np.ndfromtxt(data, dtype=int, delimiter=',') assert_array_equal(test, control)
def test_dtype_with_converters(self): dstr = "2009; 23; 46" test = np.ndfromtxt(StringIO(dstr,), delimiter=";", dtype=float, converters={0:bytes}) control = np.array([('2009', 23., 46)], dtype=[('f0', '|S4'), ('f1', float), ('f2', float)]) assert_equal(test, control) test = np.ndfromtxt(StringIO(dstr,), delimiter=";", dtype=float, converters={0:float}) control = np.array([2009., 23., 46],) assert_equal(test, control)
def test_dtype_with_converters(self): dstr = "2009; 23; 46" test = np.ndfromtxt(StringIO.StringIO(dstr,), delimiter=";", dtype=float, converters={0:str}) control = np.array([('2009', 23., 46)], dtype=[('f0','|S4'), ('f1', float), ('f2', float)]) assert_equal(test, control) test = np.ndfromtxt(StringIO.StringIO(dstr,), delimiter=";", dtype=float, converters={0:float}) control = np.array([2009., 23., 46],) assert_equal(test, control)
def test_comments(self): "Test the stripping of comments" control = np.array([1, 2, 3, 5], int) # Comment on its own line data = StringIO.StringIO('# comment\n1,2,3,5\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#') assert_equal(test, control) # Comment at the end of a line data = StringIO.StringIO('1,2,3,5# comment\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#') assert_equal(test, control)
def test_incomplete_names(self): "Test w/ incomplete names" data = "A,,C\n0,1,2\n3,4,5" kwargs = dict(delimiter=",", names=True) # w/ dtype=None ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, int) for _ in ("A", "f0", "C")]) test = np.ndfromtxt(StringIO(data), dtype=None, **kwargs) assert_equal(test, ctrl) # w/ default dtype ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, float) for _ in ("A", "f0", "C")]) test = np.ndfromtxt(StringIO(data), **kwargs)
def test_autostrip(self): "Test autostrip" data = "01/01/2003 , 1.3, abcde" kwargs = dict(delimiter=",", dtype=None) mtest = np.ndfromtxt(StringIO(data), **kwargs) ctrl = np.array([('01/01/2003 ', 1.3, ' abcde')], dtype=[('f0', '|S12'), ('f1', float), ('f2', '|S8')]) assert_equal(mtest, ctrl) mtest = np.ndfromtxt(StringIO(data), autostrip=True, **kwargs) ctrl = np.array([('01/01/2003', 1.3, 'abcde')], dtype=[('f0', '|S10'), ('f1', float), ('f2', '|S5')]) assert_equal(mtest, ctrl)
def test_fixed_width_names(self): "Test fix-width w/ names" data = " A B C\n 0 1 2.3\n 45 67 9." kwargs = dict(delimiter=(5, 5, 4), names=True, dtype=None) ctrl = np.array([(0, 1, 2.3), (45, 67, 9.0)], dtype=[("A", int), ("B", int), ("C", float)]) test = np.ndfromtxt(StringIO(data), **kwargs) assert_equal(test, ctrl) # kwargs = dict(delimiter=5, names=True, dtype=None) ctrl = np.array([(0, 1, 2.3), (45, 67, 9.0)], dtype=[("A", int), ("B", int), ("C", float)]) test = np.ndfromtxt(StringIO(data), **kwargs) assert_equal(test, ctrl)
def test_incomplete_names(self): "Test w/ incomplete names" data = "A,,C\n0,1,2\n3,4,5" kwargs = dict(delimiter=",", names=True) # w/ dtype=None ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, int) for _ in ('A', 'f0', 'C')]) test = np.ndfromtxt(StringIO(data), dtype=None, **kwargs) assert_equal(test, ctrl) # w/ default dtype ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, float) for _ in ('A', 'f0', 'C')]) test = np.ndfromtxt(StringIO(data), **kwargs)
def test_record(self): "Test w/ explicit dtype" data = StringIO(asbytes("1 2\n3 4")) # data.seek(0) test = np.ndfromtxt(data, dtype=[("x", np.int32), ("y", np.int32)]) control = np.array([(1, 2), (3, 4)], dtype=[("x", "i4"), ("y", "i4")]) assert_equal(test, control) # data = StringIO("M 64.0 75.0\nF 25.0 60.0") # data.seek(0) descriptor = {"names": ("gender", "age", "weight"), "formats": ("S1", "i4", "f4")} control = np.array([("M", 64.0, 75.0), ("F", 25.0, 60.0)], dtype=descriptor) test = np.ndfromtxt(data, dtype=descriptor) assert_equal(test, control)
def test_fixed_width_names(self): "Test fix-width w/ names" data = " A B C\n 0 1 2.3\n 45 67 9." kwargs = dict(delimiter=(5, 5, 4), names=True, dtype=None) ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)], dtype=[('A', int), ('B', int), ('C', float)]) test = np.ndfromtxt(StringIO(data), **kwargs) assert_equal(test, ctrl) # kwargs = dict(delimiter=5, names=True, dtype=None) ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)], dtype=[('A', int), ('B', int), ('C', float)]) test = np.ndfromtxt(StringIO(data), **kwargs) assert_equal(test, ctrl)
def test_single_dtype_wo_names(self): "Test single dtype w/o names" data = "0, 1, 2.3\n4, 5, 6.7" mtest = np.ndfromtxt(StringIO(data), delimiter=",", dtype=float, defaultfmt="f%02i") ctrl = np.array([[0., 1., 2.3], [4., 5., 6.7]], dtype=float) assert_equal(mtest, ctrl)
def test_record(self): "Test w/ explicit dtype" data = StringIO(asbytes('1 2\n3 4')) # data.seek(0) test = np.ndfromtxt(data, dtype=[('x', np.int32), ('y', np.int32)]) control = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) assert_equal(test, control) # data = StringIO('M 64.0 75.0\nF 25.0 60.0') # data.seek(0) descriptor = {'names': ('gender', 'age', 'weight'), 'formats': ('S1', 'i4', 'f4')} control = np.array([('M', 64.0, 75.0), ('F', 25.0, 60.0)], dtype=descriptor) test = np.ndfromtxt(data, dtype=descriptor) assert_equal(test, control)
def test_spacedelimiter(self): "Test space delimiter" data = StringIO.StringIO("1 2 3 4 5\n6 7 8 9 10") test = np.ndfromtxt(data) control = np.array([[ 1., 2., 3., 4., 5.], [ 6., 7., 8., 9.,10.]]) assert_equal(test, control)
def test_spacedelimiter(self): "Test space delimiter" data = StringIO("1 2 3 4 5\n6 7 8 9 10") test = np.ndfromtxt(data) control = np.array([[ 1., 2., 3., 4., 5.], [ 6., 7., 8., 9., 10.]]) assert_equal(test, control)
def from_txt(self, filename, nlines=None, dtype=float, **kwargs): """ Extract data from file to a single big ndarray. """ with open(filename, 'rb') as f: delimiter = kwargs.get('delimiter', ',') skiprows = kwargs.get('skiprows', 0) # Total number of columns. num_cols = len(f.readline().split(delimiter)) # Use columns defined by indexes. usecols = kwargs.get('usecols', None) if not usecols: # Skip columns from left side. skipcols = kwargs.get('skipcols', 0) usecols=range(skipcols, num_cols) # Ensure list. if isinstance(usecols, int): usecols = [usecols] # Number of examples. f.seek(0) if nlines > 0: f = itertools.islice(f, nlines + skiprows) data = np.ndfromtxt(f, dtype=dtype, skiprows=skiprows, delimiter=delimiter, usecols=usecols) return np.nan_to_num(data)
def get_data(name): data = {} os.chdir(name) # id is L, R, D, or T (Left, Right, Double, or Triple) id_list = ['L', 'R', 'D', 'T'] check_folders_in_directory(id_list) for id in id_list: for id_folder in os.listdir(): if id_folder[0] == id: data[id] = {} for position_folder in os.listdir(id_folder): for shot_file in os.listdir(id_folder + '/' + position_folder): position = get_radial_position(shot_file) data[id][position] = { shot_file: np.ndfromtxt(id_folder + '/' + position_folder + '/' + shot_file, delimiter='\t') } if data == {}: raise DataError("Data could not be read.") return data
def test_invalid_raise_with_usecols(self): "Test invalid_raise with usecols" data = ["1, 1, 1, 1, 1"] * 50 for i in range(5): data[10 * i] = "2, 2, 2, 2 2" data.insert(0, "a, b, c, d, e") mdata = StringIO("\n".join(data)) kwargs = dict(delimiter=",", dtype=None, names=True, invalid_raise=False) # XXX: is there a better way to get the return value of the callable in # assert_warns ? ret = {} def f(_ret={}): _ret['mtest'] = np.ndfromtxt(mdata, usecols=(0, 4), **kwargs) assert_warns(ConversionWarning, f, _ret=ret) mtest = ret['mtest'] assert_equal(len(mtest), 45) assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'ae'])) # mdata.seek(0) mtest = np.ndfromtxt(mdata, usecols=(0, 1), **kwargs) assert_equal(len(mtest), 50) control = np.ones(50, dtype=[(_, int) for _ in 'ab']) control[[10 * _ for _ in range(5)]] = (2, 2) assert_equal(mtest, control)
def test_shaped_dtype(self): c = StringIO("aaaa 1.0 8.0 1 2 3 4 5 6") dt = np.dtype([('name', 'S4'), ('x', float), ('y', float), ('block', int, (2, 3))]) x = np.ndfromtxt(c, dtype=dt) a = np.array([('aaaa', 1.0, 8.0, [[1, 2, 3], [4, 5, 6]])], dtype=dt) assert_array_equal(x, a)
def test_filling_values(self): "Test missing values" data = "1, 2, 3\n1, , 5\n0, 6, \n" kwargs = dict(delimiter=",", dtype=None, filling_values=-999) ctrl = np.array([[1, 2, 3], [1, -999, 5], [0, 6, -999]], dtype=int) test = np.ndfromtxt(StringIO(data), **kwargs) assert_equal(test, ctrl)
def test_record(self): "Test w/ explicit dtype" data = StringIO.StringIO('1 2\n3 4') # data.seek(0) test = np.ndfromtxt(data, dtype=[('x', np.int32), ('y', np.int32)]) control = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) assert_equal(test, control) # data = StringIO.StringIO('M 64.0 75.0\nF 25.0 60.0') # data.seek(0) descriptor = {'names': ('gender','age','weight'), 'formats': ('S1', 'i4', 'f4')} control = np.array([('M', 64.0, 75.0), ('F', 25.0, 60.0)], dtype=descriptor) test = np.ndfromtxt(data, dtype=descriptor) assert_equal(test, control)
def test_fancy_dtype(self): "Check that a nested dtype isn't MIA" data = StringIO.StringIO('1,2,3.0\n4,5,6.0\n') fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])]) test = np.ndfromtxt(data, dtype=fancydtype, delimiter=',') control = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype) assert_equal(test, control)
def test_converters_cornercases(self): "Test the conversion to datetime." converter = {"date": lambda s: strptime(s, "%Y-%m-%d %H:%M:%SZ")} data = StringIO("2009-02-03 12:00:00Z, 72214.0") test = np.ndfromtxt(data, delimiter=",", dtype=None, names=["date", "stid"], converters=converter) control = np.array((datetime(2009, 02, 03), 72214.0), dtype=[("date", np.object_), ("stid", float)]) assert_equal(test, control)
def test_fancy_dtype(self): "Check that a nested dtype isn't MIA" data = StringIO.StringIO('1,2,3.0\n4,5,6.0\n') fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])]) test = np.ndfromtxt(data, dtype=fancydtype, delimiter=',') control = np.array([(1,(2,3.0)),(4,(5,6.0))], dtype=fancydtype) assert_equal(test, control)
def test_invalid_raise_with_usecols(self): "Test invalid_raise with usecols" data = ["1, 1, 1, 1, 1"] * 50 for i in range(5): data[10 * i] = "2, 2, 2, 2 2" data.insert(0, "a, b, c, d, e") mdata = StringIO("\n".join(data)) kwargs = dict(delimiter=",", dtype=None, names=True, invalid_raise=False) # XXX: is there a better way to get the return value of the callable in # assert_warns ? ret = {} def f(_ret={}): _ret["mtest"] = np.ndfromtxt(mdata, usecols=(0, 4), **kwargs) assert_warns(ConversionWarning, f, _ret=ret) mtest = ret["mtest"] assert_equal(len(mtest), 45) assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in "ae"])) # mdata.seek(0) mtest = np.ndfromtxt(mdata, usecols=(0, 1), **kwargs) assert_equal(len(mtest), 50) control = np.ones(50, dtype=[(_, int) for _ in "ab"]) control[[10 * _ for _ in range(5)]] = (2, 2) assert_equal(mtest, control)
def test_fancy_dtype(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.ndfromtxt(data, dtype=fancydtype, delimiter=",") control = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype) assert_equal(test, control)
def get_data_dic(self): for folder in os.listdir(): self.data_dic.update({folder: []}) for shot in os.listdir(folder): self.data_dic[folder].append( [np.ndfromtxt(folder + '/' + shot, delimiter='\t')])
def test_single_dtype_w_implicit_names(self): "Test single dtype w implicit names" data = "a, b, c\n0, 1, 2.3\n4, 5, 6.7" mtest = np.ndfromtxt(StringIO(data), delimiter=",", dtype=float, names=True) ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)], dtype=[(_, float) for _ in "abc"]) assert_equal(mtest, ctrl)
def test_easy_structured_dtype(self): "Test easy structured dtype" data = "0, 1, 2.3\n4, 5, 6.7" mtest = np.ndfromtxt(StringIO(data), delimiter=",", dtype=(int, float, float), defaultfmt="f_%02i") ctrl = np.array([(0, 1., 2.3), (4, 5., 6.7)], dtype=[("f_00", int), ("f_01", float), ("f_02", float)]) assert_equal(mtest, ctrl)
def test_autonames_and_usecols(self): "Tests names and usecols" data = StringIO('A B C D\n aaaa 121 45 9.1') test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True, dtype=None) control = np.array(('aaaa', 45, 9.1), dtype=[('A', '|S4'), ('C', int), ('D', float)]) assert_equal(test, control)
def test_converters_with_usecols(self): "Test the combination user-defined converters and usecol" data = StringIO('1,2,3,,5\n6,7,8,9,10\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', converters={3:lambda s: int(s or - 999)}, usecols=(1, 3,)) control = np.array([[2, -999], [7, 9]], int) assert_equal(test, control)
def test_default_field_format(self): "Test default format" data = "0, 1, 2.3\n4, 5, 6.7" mtest = np.ndfromtxt(StringIO(data), delimiter=",", dtype=None, defaultfmt="f%02i") ctrl = np.array([(0, 1, 2.3), (4, 5, 6.7)], dtype=[("f00", int), ("f01", int), ("f02", float)]) assert_equal(mtest, ctrl)
def test_usecols_with_structured_dtype(self): "Test usecols with an explicit structured dtype" data = StringIO("""JOE 70.1 25.3\nBOB 60.5 27.9""") names = ["stid", "temp"] dtypes = ["S4", "f8"] test = np.ndfromtxt(data, usecols=(0, 2), dtype=zip(names, dtypes)) assert_equal(test["stid"], asbytes_nested(["JOE", "BOB"])) assert_equal(test["temp"], [25.3, 27.9])
def test_usecols_with_structured_dtype(self): "Test usecols with an explicit structured dtype" data = StringIO("""JOE 70.1 25.3\nBOB 60.5 27.9""") names = ['stid', 'temp'] dtypes = ['S4', 'f8'] test = np.ndfromtxt(data, usecols=(0, 2), dtype=zip(names, dtypes)) assert_equal(test['stid'], asbytes_nested(["JOE", "BOB"])) assert_equal(test['temp'], [25.3, 27.9])
def setUp(self): self.X = np.ndfromtxt('./tests/speech_data.csv', delimiter=',', dtype=np.float32) self.N = self.X.shape[0] self.D = self.X.shape[1] self.M = 5 self.init_num_clusters = 16
def test_converters_with_usecols_and_names(self): "Tests names and usecols" data = StringIO("A B C D\n aaaa 121 45 9.1") test = np.ndfromtxt( data, usecols=("A", "C", "D"), names=True, dtype=None, converters={"C": lambda s: 2 * int(s)} ) control = np.array(("aaaa", 90, 9.1), dtype=[("A", "|S4"), ("C", int), ("D", float)]) assert_equal(test, control)
def test_converters_with_usecols_and_names(self): "Tests names and usecols" data = StringIO('A B C D\n aaaa 121 45 9.1') test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True, dtype=None, converters={'C':lambda s: 2 * int(s)}) control = np.array(('aaaa', 90, 9.1), dtype=[('A', '|S4'), ('C', int), ('D', float)]) assert_equal(test, control)
def test_shaped_dtype(self): c = StringIO.StringIO("aaaa 1.0 8.0 1 2 3 4 5 6") dt = np.dtype([('name', 'S4'), ('x', float), ('y', float), ('block', int, (2, 3))]) x = np.ndfromtxt(c, dtype=dt) a = np.array([('aaaa', 1.0, 8.0, [[1, 2, 3], [4, 5, 6]])], dtype=dt) assert_array_equal(x, a)
def test_converters_with_usecols_and_names(self): "Tests names and usecols" data = StringIO.StringIO('A B C D\n aaaa 121 45 9.1') test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True, dtype=None, converters={'C':lambda s: 2 * int(s)}) control = np.array(('aaaa', 90, 9.1), dtype=[('A', '|S4'), ('C', int), ('D', float)]) assert_equal(test, control)
def test_converters_with_usecols(self): "Test the combination user-defined converters and usecol" data = StringIO.StringIO('1,2,3,,5\n6,7,8,9,10\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', converters={3:lambda s: int(s or -999)}, usecols=(1, 3, )) control = np.array([[2, -999], [7, 9]], int) assert_equal(test, control)
def test_autonames_and_usecols(self): "Tests names and usecols" data = StringIO.StringIO('A B C D\n aaaa 121 45 9.1') test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True, dtype=None) control = np.array(('aaaa', 45, 9.1), dtype=[('A', '|S4'), ('C', int), ('D', float)]) assert_equal(test, control)
def test_converters_cornercases(self): "Test the conversion to datetime." converter = {'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')} data = StringIO('2009-02-03 12:00:00Z, 72214.0') test = np.ndfromtxt(data, delimiter=',', dtype=None, names=['date', 'stid'], converters=converter) control = np.array((datetime(2009, 02, 03), 72214.), dtype=[('date', np.object_), ('stid', float)]) assert_equal(test, control)
def test_header(self): "Test retrieving a header" data = StringIO('gender age weight\nM 64.0 75.0\nF 25.0 60.0') test = np.ndfromtxt(data, dtype=None, names=True) control = {'gender': np.array(asbytes_nested(['M', 'F'])), 'age': np.array([64.0, 25.0]), 'weight': np.array([75.0, 60.0])} assert_equal(test['gender'], control['gender']) assert_equal(test['age'], control['age']) assert_equal(test['weight'], control['weight'])
def test_names_overwrite(self): "Test overwriting the names of the dtype" descriptor = {'names': ('g', 'a', 'w'), 'formats': ('S1', 'i4', 'f4')} data = StringIO.StringIO('M 64.0 75.0\nF 25.0 60.0') names = ('gender', 'age', 'weight') test = np.ndfromtxt(data, dtype=descriptor, names=names) descriptor['names'] = names control = np.array([('M', 64.0, 75.0), ('F', 25.0, 60.0)], dtype=descriptor) assert_equal(test, control)
def test_array(self): "Test outputing a standard ndarray" data = StringIO.StringIO('1 2\n3 4') control = np.array([[1, 2], [3, 4]], dtype=int) test = np.ndfromtxt(data, dtype=int) assert_array_equal(test, control) # data.seek(0) control = np.array([[1, 2], [3, 4]], dtype=float) test = np.loadtxt(data, dtype=float) assert_array_equal(test, control)
def test_skiprows(self): "Test row skipping" control = np.array([1, 2, 3, 5], int) # data = StringIO.StringIO('comment\n1,2,3,5\n') test = np.ndfromtxt(data, dtype=int, delimiter=',', skiprows=1) assert_equal(test, control) # data = StringIO.StringIO('# comment\n1,2,3,5\n') test = np.loadtxt(data, dtype=int, delimiter=',', skiprows=1) assert_equal(test, control)