Skip to content

Commit

Permalink
Merge pull request #569 from yarikoptic/bf-scatter
Browse files Browse the repository at this point in the history
Making scatter more robust
  • Loading branch information
yarikoptic committed Mar 3, 2018
2 parents 3c38566 + a242da6 commit e432bca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
56 changes: 36 additions & 20 deletions mvpa2/misc/plot/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import nibabel as nb
import numpy as np

from mvpa2.base import verbose
from mvpa2.base import verbose, warning

__all__ = ['plot_scatter']

Expand Down Expand Up @@ -286,21 +286,22 @@ def jitter_me(x, w):

# for orientation we need to plot 1 slice... assume that the last dimension is z -- figure out a slice with max # of non-zeros
zdim_entries = ndindices_nz[:, -1]
zdim_counts, _ = np.histogram(zdim_entries, bins=np.arange(0, np.max(zdim_entries)+1))
zdim_max = np.argmax(zdim_counts)

if hint_opacity:
# now we need to plot that zdim_max slice taking into account our colormap
# create new axes
axslice = pl.axes([left, bottom+height * 0.72, width/4., height/5.],
axisbg='y')
axslice.axis('off')
sslice = np.zeros(dataXd.shape[1:3]) # XXX hardcoded assumption on dimcolor =1
sslice[:, : ] = np.arange(dimcolor_len)[None, :]
# if there is time dimension -- choose minimal value across all values
dataXd_mint = np.min(dataXd, axis=-1) if dataXd.ndim == 5 else dataXd
sslice[dataXd_mint[0, ..., zdim_max] == 0] = -1 # reset those not in the picture to be "under" range
axslice.imshow(sslice, alpha=hint_opacity, cmap=cm)
if np.size(zdim_entries):
zdim_counts, _ = np.histogram(zdim_entries, bins=np.arange(0, np.max(zdim_entries)+1))
zdim_max = np.argmax(zdim_counts)

if hint_opacity:
# now we need to plot that zdim_max slice taking into account our colormap
# create new axes
axslice = pl.axes([left, bottom+height * 0.72, width/4., height/5.],
axisbg='y')
axslice.axis('off')
sslice = np.zeros(dataXd.shape[1:3]) # XXX hardcoded assumption on dimcolor =1
sslice[:, : ] = np.arange(dimcolor_len)[None, :]
# if there is time dimension -- choose minimal value across all values
dataXd_mint = np.min(dataXd, axis=-1) if dataXd.ndim == 5 else dataXd
sslice[dataXd_mint[0, ..., zdim_max] == 0] = -1 # reset those not in the picture to be "under" range
axslice.imshow(sslice, alpha=hint_opacity, cmap=cm)
else:
# the scatter plot without colors to distinguish location
ax_scatter.scatter(x, y, **sc_kwargs)
Expand All @@ -322,8 +323,13 @@ def jitter_me(x, w):


# Axes
ax_scatter.plot((np.min(x), np.max(x)), (0, 0), 'r', alpha=0.5)
ax_scatter.plot((0,0), (np.min(y), np.max(y)), 'r', alpha=0.5)
if np.size(x):
ax_scatter.plot((np.min(x), np.max(x)), (0, 0), 'r', alpha=0.5)
else:
warning("There is nothing to plot, returning early")
return pl.gcf()

ax_scatter.plot((0, 0), (np.min(y), np.max(y)), 'r', alpha=0.5)

if (mask is not None and not masked_opacity and np.sum(mask)):
# if there is a non-degenerate mask which was not intended to be plotted,
Expand Down Expand Up @@ -395,8 +401,18 @@ def jitter_me(x, w):
binwidthx = (maxx - minx)/51.
binwidthy = (maxy - miny)/51.

binsx = np.arange(minx, maxx + binwidthx, binwidthx)
binsy = np.arange(miny, maxy + binwidthy, binwidthy)
try:
binsx = np.arange(minx, maxx + binwidthx, binwidthx)
binsy = np.arange(miny, maxy + binwidthy, binwidthy)
except Exception as exc:
warning(
"Received following exception while trying to get bins for "
"minx=%(minx)f maxx=%(maxx)f binwidthx=%(binwidthx)s "
"miny=%(miny)f maxy=%(maxy)f binwidthy=%(binwidthy)s: %(exc)s. "
"Returning early"
% locals()
)
return pl.gcf()

if xlim is not None:
ax_scatter.set_xlim( xlim )
Expand Down
5 changes: 5 additions & 0 deletions mvpa2/tests/test_misc_scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def test_plot_scatter():
fig = plot_scatter(data2d, mask=mask)
fig = plot_scatter(data2d, mask=mask, masked_opacity=0.42)

data2d_nan = np.empty(data2d.shape)
data2d_nan[:, :] = np.nan
# must not blow
fig = plot_scatter(data2d_nan, mask=mask)

# smoke test with threshold
fig = plot_scatter(data2d, thresholds=[0.2])
fig = plot_scatter(data2d, thresholds=[0.2, 0.4])
Expand Down

0 comments on commit e432bca

Please sign in to comment.