def test_write(self): for fp, obj in zip(self.valid_fps, self.ordination_results_objs): fh = StringIO() _ordination_results_to_ordres(obj, fh) obs = fh.getvalue() fh.close() with open(fp, 'U') as fh: exp = fh.read() npt.assert_equal(obs, exp)
def test_write(self): for fn, objs, strs in ((_dissimilarity_matrix_to_dm, self.dissim_objs, self.dissim_strs), (_distance_matrix_to_dm, self.dist_objs, self.dist_strs)): for obj, str_ in zip(objs, strs): fh = StringIO() fn(obj, fh) obs = fh.getvalue() fh.close() self.assertEqual(obs, str_) # Test writing CSV (TSV is written above). for fn, cls in ((_dissimilarity_matrix_to_dm, DissimilarityMatrix), (_distance_matrix_to_dm, DistanceMatrix)): obj = cls(self.dm_3x3_data, ['a', 'b', 'c']) fh = StringIO() fn(obj, fh, delimiter=',') obs = fh.getvalue() fh.close() self.assertEqual(obs, DM_3x3_CSV)
def test_to_file(self): """Should serialize a DissimilarityMatrix to file.""" for dm_f_line, dm in zip(self.dm_f_lines, self.dms): for file_type in ('file like', 'file name'): if file_type == 'file like': obs_f = StringIO() dm.to_file(obs_f) obs = obs_f.getvalue() obs_f.close() elif file_type == 'file name': with tempfile.NamedTemporaryFile('r+') as temp_file: dm.to_file(temp_file.name) temp_file.flush() temp_file.seek(0) obs = temp_file.read() self.assertEqual(obs, dm_f_line)
def test_to_file(self): for scores, test_path in zip(self.scores, self.test_paths): for file_type in ('file like', 'file name'): if file_type == 'file like': obs_f = StringIO() scores.to_file(obs_f) obs = obs_f.getvalue() obs_f.close() elif file_type == 'file name': with tempfile.NamedTemporaryFile('r+') as temp_file: scores.to_file(temp_file.name) temp_file.flush() temp_file.seek(0) obs = temp_file.read() with open(get_data_path(test_path), 'U') as f: exp = f.read() yield npt.assert_equal, obs, exp
def summary(self, delimiter='\t'): """Return a formatted summary of results as a string. The string is formatted as delimited text. Parameters ---------- delimiter : str, optional String to delimit fields by in formatted output. Default is tab (TSV). Returns ------- str Delimited-text summary of results. """ summary = StringIO() csv_writer = csv.writer(summary, delimiter=delimiter, lineterminator='\n') csv_writer.writerow(self._format_header()) csv_writer.writerow(self._format_data()) return summary.getvalue()