def test_csv_sniffer(self): # GH-2785 reader = CSVReader(test_filename('test_asn_data_working.csv')) data = reader.read() self.assertEqual(len(data), 8) self.assertEqual( len(data.domain.variables) + len(data.domain.metas), 15)
def test_header_call(self, writer): CSVReader.write_headers(writer, Table("iris"), False) self.assertEqual(len(writer.call_args_list), 1) writer.reset_mock() CSVReader.write_headers(writer, Table("iris"), True) self.assertEqual(len(writer.call_args_list), 3)
def test_csv_format_missing_values(self): f = CSVReader() data = Table("../../tests/test9.tab") self.assertTrue(any([x is "" for instance in data.metas for x in instance])) f.write_file(self.CSV_FILE, data) new_data = f.read_file(self.CSV_FILE) for new_instance, old_instance in zip(new_data, data): self.assertEqual(new_instance, old_instance)
def test_noncontinous_marked_continuous(self): file = NamedTemporaryFile("wt", delete=False) file.write(noncont_marked_cont) file.close() with self.assertRaises(ValueError) as cm: table = CSVReader(file.name).read() self.assertIn('line 5, column 2', cm.exception.args[0])
def test_readwrite_timevariable(self): output_csv = StringIO() input_csv = StringIO("""\ Date,Feature time,continuous , 1920-12-12,1.0 1920-12-13,3.0 1920-12-14,5.5 """) for stream in (output_csv, input_csv): stream.close = lambda: None # HACK: Prevent closing of streams table = CSVReader(input_csv).read() self.assertIsInstance(table.domain['Date'], TimeVariable) self.assertEqual(table[0, 'Date'], '1920-12-12') # Dates before 1970 are negative self.assertTrue(all(inst['Date'] < 0 for inst in table)) CSVReader.write_file(output_csv, table) self.assertEqual(input_csv.getvalue().splitlines(), output_csv.getvalue().splitlines())
def test_empty_columns(self): """Can't read files with more columns then headers. GH-1417""" samplefile = """\ a, b 1, 0, 1, 2, """ c = io.StringIO(samplefile) with self.assertWarns(UserWarning) as cm: table = CSVReader(c).read() self.assertEqual(len(table.domain.attributes), 2) self.assertEqual(cm.warning.args[0], "Columns with no headers were removed.")
def test_pr1734(self): ContinuousVariable('foo') file = NamedTemporaryFile("wt", delete=False) filename = file.name try: file.write('''\ foo time 123123123 ''') file.close() CSVReader(filename).read() finally: os.remove(filename)
def read_easy(self, s, name): file = NamedTemporaryFile("wt", delete=False) filename = file.name try: file.write(s) file.close() table = CSVReader(filename).read() f1, f2, f3 = table.domain self.assertIsInstance(f1, DiscreteVariable) self.assertEqual(f1.name, name + "1") self.assertIsInstance(f2, ContinuousVariable) self.assertEqual(f2.name, name + "2") self.assertIsInstance(f3, ContinuousVariable) self.assertEqual(f3.name, name + "3") finally: os.remove(filename)
def test_csv_sniffer(self): # GH-2785 reader = CSVReader(test_filename('test_asn_data_working.csv')) data = reader.read() self.assertEqual(len(data), 8) self.assertEqual(len(data.domain.variables) + len(data.domain.metas), 15)
def test_read_csv_with_na(self): c = io.StringIO(csv_file_missing) table = CSVReader(c).read() f1, f2 = table.domain.variables self.assertIsInstance(f1, ContinuousVariable) self.assertIsInstance(f2, DiscreteVariable)