Skip to content

Commit

Permalink
some progress made
Browse files Browse the repository at this point in the history
  • Loading branch information
ctb committed Sep 28, 2023
1 parent f19af8c commit 9ae12fb
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 86 deletions.
127 changes: 63 additions & 64 deletions doc/plotting-compare.ipynb

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions src/sourmash/cli/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ def subparser(subparsers):
help='output PDF; default is PNG'
)
subparser.add_argument(
'--labels', action='store_true',
'--labels', action='store_true', default=False,
help='show sample labels on dendrogram/matrix'
)
subparser.add_argument(

Check warning on line 16 in src/sourmash/cli/plot.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/cli/plot.py#L16

Added line #L16 was not covered by tests
'--no-labels', action='store_false', dest='labels',
help='do not show sample labels'
)
subparser.add_argument(
'--labeltext',
help='filename containing list of labels (overrides signature names)'
help='filename containing list of labels (overrides signature names); implies --labels'
)
subparser.add_argument(

Check warning on line 24 in src/sourmash/cli/plot.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/cli/plot.py#L24

Added line #L24 was not covered by tests
'--indices', action='store_true', default=True,
help='show sample indices but not labels; overridden by --labels'
)
subparser.add_argument(
'--indices', action='store_false',
help='show sample indices but not labels'
'--no-indices', action='store_false', dest='indices',
help='do not show sample indices'
)
subparser.add_argument(
'--vmin', default=0.0, type=float,
Expand Down
32 changes: 23 additions & 9 deletions src/sourmash/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,33 @@ def plot(args):

# load files
D_filename = args.distances
labelfilename = D_filename + '.labels.txt'

notify(f'loading comparison matrix from {D_filename}...')
D = numpy.load(open(D_filename, 'rb'))
# not sure how to change this to use f-strings
notify('...got {} x {} matrix.', *D.shape)

if args.labeltext:
labelfilename = args.labeltext
notify(f'loading labels from {labelfilename}')
labeltext = [ x.strip() for x in open(labelfilename) ]
if len(labeltext) != D.shape[0]:
error('{} labels != matrix size, exiting', len(labeltext))
sys.exit(-1)
if args.labels or args.labeltext:
args.labels = True

Check warning on line 256 in src/sourmash/commands.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/commands.py#L256

Added line #L256 was not covered by tests
if args.labeltext:
labelfilename = args.labeltext

Check warning on line 258 in src/sourmash/commands.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/commands.py#L258

Added line #L258 was not covered by tests
else:
labelfilename = D_filename + '.labels.txt'

Check warning on line 260 in src/sourmash/commands.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/commands.py#L260

Added line #L260 was not covered by tests

notify(f'loading labels from {labelfilename}')

Check warning on line 262 in src/sourmash/commands.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/commands.py#L262

Added line #L262 was not covered by tests
labeltext = [ x.strip() for x in open(labelfilename) ]

if len(labeltext) != D.shape[0]:
error('{} labels != matrix size, exiting', len(labeltext))
sys.exit(-1)

Check warning on line 267 in src/sourmash/commands.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/commands.py#L266-L267

Added lines #L266 - L267 were not covered by tests
elif args.indices:
# construct integer labels
labeltext = [str(i) for i in range(D.shape[0])]
args.labels = True

Check warning on line 271 in src/sourmash/commands.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/commands.py#L271

Added line #L271 was not covered by tests
else:
assert not args.labels
assert not args.indices
assert not args.labeltext

Check warning on line 275 in src/sourmash/commands.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/commands.py#L273-L275

Added lines #L273 - L275 were not covered by tests

# build filenames, decide on PDF/PNG output
dendrogram_out = os.path.basename(D_filename) + '.dendro'
Expand Down Expand Up @@ -300,6 +313,8 @@ def plot(args):
ax1.set_xticks([])
ax1.set_yticks([])



# subsample?
if args.subsample:
numpy.random.seed(args.subsample_seed)
Expand All @@ -321,7 +336,6 @@ def plot(args):
### make the dendrogram+matrix:
(fig, rlabels, rmat) = sourmash_fig.plot_composite_matrix(D, labeltext,
show_labels=args.labels,
show_indices=args.indices,
vmin=args.vmin,
vmax=args.vmax,
force=args.force)
Expand Down
18 changes: 9 additions & 9 deletions src/sourmash/fig.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env python
"""
Make plots using the distance matrix+labels output by ``sourmash compare``.
Make plots using the distance matrix+labels output by `sourmash compare`.
"""
from .logging import error, notify
try:
Expand All @@ -20,11 +20,15 @@ def load_matrix_and_labels(basefile):
return (D, labeltext)


def plot_composite_matrix(D, labeltext, show_labels=True, show_indices=True,
def plot_composite_matrix(D, labeltext, show_labels=True,

Check warning on line 23 in src/sourmash/fig.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/fig.py#L23

Added line #L23 was not covered by tests
vmax=1.0, vmin=0.0, force=False):
"""Build a composite plot showing dendrogram + distance matrix/heatmap.
Returns a matplotlib figure."""
Returns a matplotlib figure.
If show_labels is True, display labels. Otherwise, no labels are
shown on the plot.
"""
if D.max() > 1.0 or D.min() < 0.0:
error('This matrix doesn\'t look like a distance matrix - min value {}, max value {}', D.min(), D.max())
if not force:
Expand All @@ -43,12 +47,8 @@ def plot_composite_matrix(D, labeltext, show_labels=True, show_indices=True,
# plot dendrogram
Y = sch.linkage(D, method='single') # centroid

dendrolabels = labeltext
if not show_labels:
dendrolabels = [str(i) for i in range(len(labeltext))]

Z1 = sch.dendrogram(Y, orientation='left', labels=dendrolabels,
no_labels=not show_indices, get_leaves=True)
Z1 = sch.dendrogram(Y, orientation='left', labels=labeltext,

Check warning on line 50 in src/sourmash/fig.py

View check run for this annotation

Codecov / codecov/patch

src/sourmash/fig.py#L50

Added line #L50 was not covered by tests
no_labels=not show_labels, get_leaves=True)
ax1.set_xticks([])

xstart = 0.45
Expand Down

0 comments on commit 9ae12fb

Please sign in to comment.