Skip to content

Commit

Permalink
ENH: Address @wasade's comment to truncate outptut
Browse files Browse the repository at this point in the history
The output is truncated to 5 missing elements.
  • Loading branch information
ElDeveloper committed Oct 22, 2019
1 parent bd38e56 commit 9b4c0bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
17 changes: 14 additions & 3 deletions emperor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,22 @@ def _validate_metadata(self, metadata, matrix, ignore_missing_samples,
' to the same dataset.' % kind)

if difference and not ignore_missing_samples:
elements = ', '.join(sorted([str(i) for i in difference]))
# sort the elements so we have a deterministic output
difference = sorted([str(i) for i in difference])

# if there's more than 5 missing elements, truncate the list
if len(difference) > 5:
elements = ', '.join(difference[:5])
suffix = ("Showing only the first 5 %ss out of %d: %s ..." %
(kind, len(difference), elements))
else:
elements = ', '.join(difference)
suffix = ("Offending %ss: %s" % (kind, elements))

raise KeyError("There are %ss not included in the %s mapping "
"file. Override this error by using the "
"`ignore_missing_samples` argument. Offending "
"%ss: %s" % (kind, kind, kind, elements))
"`ignore_missing_samples` argument. %s" %
(kind, kind, suffix))
elif difference and ignore_missing_samples:
warnings.warn("%d out of %d %ss have no metadata and are being"
" included with a placeholder value." %
Expand Down
13 changes: 13 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ def test_one_dimensional(self):
"two dimensions are not supported"):
Emperor(self.ord_res, self.mf, remote=False)

def test_initial_unbalanced_more_than_five(self):
mf = self.mf.copy()
mf.drop(['PC.354', 'PC.355', 'PC.356', 'PC.481', 'PC.607', 'PC.636'],
inplace=True)
with self.assertRaisesRegexp(KeyError, "There are samples not "
"included in the sample mapping file. "
"Override this error by using the "
"`ignore_missing_samples` argument. "
"Showing only the first 5 samples out of "
"6: PC.354, PC.355, PC.356, PC.481, "
"PC.607 ..."):
Emperor(self.ord_res, mf, remote=self.url)

def test_initial_unbalanced(self):
mf = self.mf.copy()
mf.drop(['PC.354'], inplace=True)
Expand Down

0 comments on commit 9b4c0bb

Please sign in to comment.