Skip to content

Commit

Permalink
ENH/BUG/DOC: spy verbose; use nbcmap bg colr; tut
Browse files Browse the repository at this point in the history
  • Loading branch information
fedarko committed Sep 15, 2023
1 parent 60f9f4b commit be05502
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 21 deletions.
228 changes: 213 additions & 15 deletions docs/Tutorial.ipynb

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions wotplot/_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import time


def get_logger(verbose):
t0 = time.time()

def _mlog(s):
if verbose:
print(f"{time.time() - t0:,.2f}s: {s}", flush=True)

return _mlog
8 changes: 2 additions & 6 deletions wotplot/_make.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from pydivsufsort import divsufsort
from ._scipy_sm_constructor_getter import get_sm_constructor
from ._logging import get_logger

DNA = "ACGT"
RCDNA = "TGCA"
Expand Down Expand Up @@ -280,11 +280,7 @@ def _make(s1, s2, k, yorder="BT", binary=True, verbose=False):
ss1 and ss2 are versions of s1 and s2, respectively, converted to
strings.
"""
t0 = time.time()

def _mlog(s):
if verbose:
print(f"{time.time() - t0:,.2f}s: {s}", flush=True)
_mlog = get_logger(verbose)

# First, verify that the SciPy version installed is good
smc = get_sm_constructor()
Expand Down
28 changes: 28 additions & 0 deletions wotplot/_viz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
from matplotlib import pyplot
from ._make import FWD, REV, BOTH
from ._logging import get_logger

# Colormaps based on Figure 6.20 in Chapter 6 of Bioinformatics Algorithms
# (Compeau & Pevzner), ed. 2
Expand Down Expand Up @@ -72,6 +73,7 @@ def viz_spy(
nbcmap=NBCMAP_HEX,
title=None,
ax=None,
verbose=False,
**kwargs,
):
"""Visualizes a DotPlotMatrix object using matplotlib's spy().
Expand Down Expand Up @@ -108,6 +110,10 @@ def viz_spy(
If this is not None, then we'll add the visualization within this
Axes object.
verbose: bool
If True, prints information about time taken. Useful for performance
benchmarking.
**kwargs
Will be passed to spy().
Expand All @@ -123,10 +129,27 @@ def viz_spy(
-----
TODO case on binary and use color accordingly like in imshow
"""
_mlog = get_logger(verbose)

fig, ax = _create_fig_and_ax_if_needed(ax)
if not m.binary:
# spy() doesn't, as far as i can tell, have an easy way to set the
# background color. We can use set_facecolor(), though -- see
# https://stackoverflow.com/a/23645437.
#
# To avoid wasting time, we only call set_facecolor() if the background
# color is different from the default. This might still be unnecessary
# (if e.g. the user redefined the bg color as "white" instead of
# "#ffffff" then we still shouldn't need to call set_facecolor()),
# but I think this approach should do the most performant thing in most
# situations.
if nbcmap[0] != NBCMAP_HEX[0]:
_mlog(f"Setting background color to {nbcmap[0]}...")
ax.set_facecolor(nbcmap[0])
_mlog("Done setting background color.")

for val in (FWD, REV, BOTH):
_mlog(f'Visualizing "{val}" cells...')
# Filter the matrix to just the cells of a certain match type.
# https://stackoverflow.com/a/22077616
# This is somewhat inefficient -- ideally we'd do the filtering in
Expand All @@ -138,9 +161,14 @@ def viz_spy(
color=nbcmap[val],
**kwargs,
)
_mlog(f'Done visualizing "{val}" cells.')
else:
_mlog("Visualizing all match cells...")
ax.spy(m.mat, markersize=markersize, color=color, **kwargs)
_mlog("Done visualizing all match cells.")
_mlog("Slightly restyling the visualization...")
style_viz_ax(ax, m, title)
_mlog("Done.")
if fig is not None:
return fig, ax

Expand Down

0 comments on commit be05502

Please sign in to comment.