mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-10 16:31:50 +08:00
b6dcf3c336
Update Travis build to use Anaconda Travis updates and fixes More travis fixes Another travis attempt Revert changes Use PIL and Pillow Refactor travis into 4 different builds Fix activation error Remove explicit mpl in build_versions.py Make matplotlib an explicit requirement Rearrange travis Make pillow a hard requirement Try again to make Pillow optional Fix bash syntax error Fix bash syntax error Bump required cython version More rearrangments Remove mpl from build_versions, rearrange travis Fix version check Make matplotlib explicit again Conda install into test env Check for proper install Allow tests to skip if networkx is not available Allow tests to skip if networkx is not available Try swapping pillow for matplotlib Allow tests to pass when matplotlib is not present Remove matplotlib from build_versions Print PIL version Get pillow from PIP Allow tests to skip if matplotlib is not present. Allow tests to skip if networkx is not present. travis fix Remove unused mpl import that caused test error Use nose-cov and do not run doctests without optional libs Bump required numpy version and fix nose calls Make overlay test repeatable bump numpy version again Move low-end numpy to python 2.7 Play with minimum versions Add version requirements and use functions Add version requirements and use functions Allow require to skip a test More implementation of require decorator Update require decorator and clean up tests Only use requires decorator when needed Fix python3 error in version_requirements Fix build errors Fix handling of require with tests More fixes for require handler Use latest miniconda Fix more build errors Fix another dict comprehension and travis file. Fix missing imports Fix dictionary again Fix import warning Fix last failing test on 2.6 Skip doc examples on python2.6 Do not run doctests on python2.6 Fix typo in travis.yml Make numpy-1.6 compatibility changes Use numpy-1.6 in travis python2.6 Add tests for version requirements Fix line noise in PR Add additional io plugins Fix simpleitk test. Fix python 3 error in freeimage_plugin. Install imread in Travis. Put matplotlib settings in XDG recommended directory Fix formatting in travis yml Fix formatting in travis yml Make sure to close PIL file atexit Fix name of apt package xcftools Fix pil fp closing Fix matplotlibrc creation Only download SimpleITK on py2x, run coverage on py27 Fix travis yml syntax error Run coveralls on py2.7 Install SimpleITK on py3.3 and run coverage on py3.3 Make simpleitk install quiet Use standard nose and clean up incantation Fix travis yml syntax error Put in miniconda workout for libc error. Fix imread plugin. Fix travis syntax Remove unused import Remove miniconda libpng in favor of system png Fix imread install and move libm removal to after optional pkg install. Fix png header copy in travis yml Another attempt to use png headers Debug freeimage Add jpeg library for freeimage and debug imread. More debug for imread and freeimage More freeimage and imread debugging More debugging Use correct paths for test env Make sure imread is tied to libpng15 Add a TODO note for simpleitk test causing error. Fix typo in yml Cleanup and add more comments to travis yml Update comment Try and add 3.2 support. Docstring formatting Add more travis comments. Try numpy 1.6 on python 2.7 Fix travis syntax error Rename CONDA to ENV for clarity Alias python on python 3.2 Use python 3.2 as the system python Clean up libfreeimage install Fix order on py3.2 pre_install Move old numpy back to py26 Use the appropriate python calls. Debug 3.2 build. Update comment Fix syntax error Another fix for syntax error. Install scipy after downloading import tools More debugging for py32 Do not install conda on py3.2 (duh) Fix typo in travis yml Fix py32 qt install, separate pyfits and imread to find error Fix syntax error and front-load option lib check for debug pyfits is not supported in py3.2, try imread now imread is also not supported on py3.2 install imread before pyfits to show relationship with libs Make pip builds quiet Minor formatting to retrigger build Allow simpleitk to fail to download without breaking the build Use travis_retry for SimpleITK See what breaks when we keep libm in Now remove libm again
175 lines
5.3 KiB
Python
175 lines
5.3 KiB
Python
import numpy as np
|
|
try:
|
|
from matplotlib import lines
|
|
except ImportError:
|
|
pass
|
|
|
|
__all__ = ['CanvasToolBase', 'ToolHandles']
|
|
|
|
|
|
def _pass(*args):
|
|
pass
|
|
|
|
|
|
class CanvasToolBase(object):
|
|
"""Base canvas tool for matplotlib axes.
|
|
|
|
Parameters
|
|
----------
|
|
ax : :class:`matplotlib.axes.Axes`
|
|
Matplotlib axes where tool is displayed.
|
|
on_move : function
|
|
Function called whenever a control handle is moved.
|
|
This function must accept the end points of line as the only argument.
|
|
on_release : function
|
|
Function called whenever the control handle is released.
|
|
on_enter : function
|
|
Function called whenever the "enter" key is pressed.
|
|
useblit : bool
|
|
If True, update canvas by blitting, which is much faster than normal
|
|
redrawing (turn off for debugging purposes).
|
|
"""
|
|
def __init__(self, ax, on_move=None, on_enter=None, on_release=None,
|
|
useblit=True):
|
|
self.ax = ax
|
|
self.canvas = ax.figure.canvas
|
|
self.img_background = None
|
|
self.cids = []
|
|
self._artists = []
|
|
self.active = True
|
|
|
|
if useblit:
|
|
self.connect_event('draw_event', self._blit_on_draw_event)
|
|
self.useblit = useblit
|
|
|
|
self.callback_on_move = _pass if on_move is None else on_move
|
|
self.callback_on_enter = _pass if on_enter is None else on_enter
|
|
self.callback_on_release = _pass if on_release is None else on_release
|
|
|
|
self.connect_event('key_press_event', self._on_key_press)
|
|
|
|
def connect_event(self, event, callback):
|
|
"""Connect callback with an event.
|
|
|
|
This should be used in lieu of `figure.canvas.mpl_connect` since this
|
|
function stores call back ids for later clean up.
|
|
"""
|
|
cid = self.canvas.mpl_connect(event, callback)
|
|
self.cids.append(cid)
|
|
|
|
def disconnect_events(self):
|
|
"""Disconnect all events created by this widget."""
|
|
for c in self.cids:
|
|
self.canvas.mpl_disconnect(c)
|
|
|
|
def ignore(self, event):
|
|
"""Return True if event should be ignored.
|
|
|
|
This method (or a version of it) should be called at the beginning
|
|
of any event callback.
|
|
"""
|
|
return not self.active
|
|
|
|
def set_visible(self, val):
|
|
for artist in self._artists:
|
|
artist.set_visible(val)
|
|
|
|
def _blit_on_draw_event(self, event=None):
|
|
self.img_background = self.canvas.copy_from_bbox(self.ax.bbox)
|
|
self._draw_artists()
|
|
|
|
def _draw_artists(self):
|
|
for artist in self._artists:
|
|
self.ax.draw_artist(artist)
|
|
|
|
def remove(self):
|
|
"""Remove artists and events from axes.
|
|
|
|
Note that the naming here mimics the interface of Matplotlib artists.
|
|
"""
|
|
#TODO: For some reason, RectangleTool doesn't get properly removed
|
|
self.disconnect_events()
|
|
for a in self._artists:
|
|
a.remove()
|
|
|
|
def redraw(self):
|
|
"""Redraw image and canvas artists.
|
|
|
|
This method should be called by subclasses when artists are updated.
|
|
"""
|
|
if self.useblit and self.img_background is not None:
|
|
self.canvas.restore_region(self.img_background)
|
|
self._draw_artists()
|
|
self.canvas.blit(self.ax.bbox)
|
|
else:
|
|
self.canvas.draw_idle()
|
|
|
|
def _on_key_press(self, event):
|
|
if event.key == 'enter':
|
|
self.callback_on_enter(self.geometry)
|
|
self.set_visible(False)
|
|
self.redraw()
|
|
|
|
@property
|
|
def geometry(self):
|
|
"""Geometry information that gets passed to callback functions."""
|
|
return None
|
|
|
|
|
|
class ToolHandles(object):
|
|
"""Control handles for canvas tools.
|
|
|
|
Parameters
|
|
----------
|
|
ax : :class:`matplotlib.axes.Axes`
|
|
Matplotlib axes where tool handles are displayed.
|
|
x, y : 1D arrays
|
|
Coordinates of control handles.
|
|
marker : str
|
|
Shape of marker used to display handle. See `matplotlib.pyplot.plot`.
|
|
marker_props : dict
|
|
Additional marker properties. See :class:`matplotlib.lines.Line2D`.
|
|
"""
|
|
def __init__(self, ax, x, y, marker='o', marker_props=None):
|
|
self.ax = ax
|
|
|
|
props = dict(marker=marker, markersize=7, mfc='w', ls='none',
|
|
alpha=0.5, visible=False)
|
|
props.update(marker_props if marker_props is not None else {})
|
|
self._markers = lines.Line2D(x, y, animated=True, **props)
|
|
self.ax.add_line(self._markers)
|
|
self.artist = self._markers
|
|
|
|
@property
|
|
def x(self):
|
|
return self._markers.get_xdata()
|
|
|
|
@property
|
|
def y(self):
|
|
return self._markers.get_ydata()
|
|
|
|
def set_data(self, pts, y=None):
|
|
"""Set x and y positions of handles"""
|
|
if y is not None:
|
|
x = pts
|
|
pts = np.array([x, y])
|
|
self._markers.set_data(pts)
|
|
|
|
def set_visible(self, val):
|
|
self._markers.set_visible(val)
|
|
|
|
def set_animated(self, val):
|
|
self._markers.set_animated(val)
|
|
|
|
def draw(self):
|
|
self.ax.draw_artist(self._markers)
|
|
|
|
def closest(self, x, y):
|
|
"""Return index and pixel distance to closest index."""
|
|
pts = np.transpose((self.x, self.y))
|
|
# Transform data coordinates to pixel coordinates.
|
|
pts = self.ax.transData.transform(pts)
|
|
diff = pts - ((x, y))
|
|
dist = np.sqrt(np.sum(diff**2, axis=1))
|
|
return np.argmin(dist), np.min(dist)
|