mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-29 18:27:16 +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
226 lines
6.8 KiB
Python
226 lines
6.8 KiB
Python
import warnings
|
|
|
|
import numpy as np
|
|
|
|
from ..qt import qt_api
|
|
|
|
try:
|
|
import matplotlib as mpl
|
|
from matplotlib.figure import Figure
|
|
from matplotlib import _pylab_helpers
|
|
from matplotlib.colors import LinearSegmentedColormap
|
|
if not qt_api is None:
|
|
from matplotlib.backends.backend_qt4 import FigureManagerQT
|
|
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
|
|
if 'agg' not in mpl.get_backend().lower():
|
|
print("Recommended matplotlib backend is `Agg` for full "
|
|
"skimage.viewer functionality.")
|
|
except ImportError:
|
|
FigureCanvasQTAgg = object # hack to prevent nosetest and autodoc errors
|
|
LinearSegmentedColormap = object
|
|
|
|
from ..qt import QtGui
|
|
|
|
|
|
__all__ = ['init_qtapp', 'start_qtapp', 'RequiredAttr', 'figimage',
|
|
'LinearColormap', 'ClearColormap', 'FigureCanvas', 'new_plot',
|
|
'update_axes_image']
|
|
|
|
|
|
QApp = None
|
|
|
|
|
|
def init_qtapp():
|
|
"""Initialize QAppliction.
|
|
|
|
The QApplication needs to be initialized before creating any QWidgets
|
|
"""
|
|
global QApp
|
|
QApp = QtGui.QApplication.instance()
|
|
if QApp is None:
|
|
QApp = QtGui.QApplication([])
|
|
return QApp
|
|
|
|
|
|
def is_event_loop_running(app=None):
|
|
"""Return True if event loop is running."""
|
|
if app is None:
|
|
app = init_qtapp()
|
|
if hasattr(app, '_in_event_loop'):
|
|
return app._in_event_loop
|
|
else:
|
|
return False
|
|
|
|
|
|
def start_qtapp(app=None):
|
|
"""Start Qt mainloop"""
|
|
if app is None:
|
|
app = init_qtapp()
|
|
if not is_event_loop_running(app):
|
|
app._in_event_loop = True
|
|
app.exec_()
|
|
app._in_event_loop = False
|
|
else:
|
|
app._in_event_loop = True
|
|
|
|
|
|
class RequiredAttr(object):
|
|
"""A class attribute that must be set before use."""
|
|
|
|
instances = dict()
|
|
|
|
def __init__(self, msg='Required attribute not set', init_val=None):
|
|
self.instances[self, None] = init_val
|
|
self.msg = msg
|
|
|
|
def __get__(self, obj, objtype):
|
|
value = self.instances[self, obj]
|
|
if value is None:
|
|
# Should raise an error but that causes issues with the buildbot.
|
|
warnings.warn(self.msg)
|
|
self.__set__(obj, self.init_val)
|
|
return value
|
|
|
|
def __set__(self, obj, value):
|
|
self.instances[self, obj] = value
|
|
|
|
|
|
class LinearColormap(LinearSegmentedColormap):
|
|
"""LinearSegmentedColormap in which color varies smoothly.
|
|
|
|
This class is a simplification of LinearSegmentedColormap, which doesn't
|
|
support jumps in color intensities.
|
|
|
|
Parameters
|
|
----------
|
|
name : str
|
|
Name of colormap.
|
|
|
|
segmented_data : dict
|
|
Dictionary of 'red', 'green', 'blue', and (optionally) 'alpha' values.
|
|
Each color key contains a list of `x`, `y` tuples. `x` must increase
|
|
monotonically from 0 to 1 and corresponds to input values for a
|
|
mappable object (e.g. an image). `y` corresponds to the color
|
|
intensity.
|
|
|
|
"""
|
|
def __init__(self, name, segmented_data, **kwargs):
|
|
segmented_data = dict((key, [(x, y, y) for x, y in value])
|
|
for key, value in segmented_data.items())
|
|
LinearSegmentedColormap.__init__(self, name, segmented_data, **kwargs)
|
|
|
|
|
|
class ClearColormap(LinearColormap):
|
|
"""Color map that varies linearly from alpha = 0 to 1
|
|
"""
|
|
def __init__(self, rgb, max_alpha=1, name='clear_color'):
|
|
r, g, b = rgb
|
|
cg_speq = {'blue': [(0.0, b), (1.0, b)],
|
|
'green': [(0.0, g), (1.0, g)],
|
|
'red': [(0.0, r), (1.0, r)],
|
|
'alpha': [(0.0, 0.0), (1.0, max_alpha)]}
|
|
LinearColormap.__init__(self, name, cg_speq)
|
|
|
|
|
|
class FigureCanvas(FigureCanvasQTAgg):
|
|
"""Canvas for displaying images."""
|
|
def __init__(self, figure, **kwargs):
|
|
self.fig = figure
|
|
FigureCanvasQTAgg.__init__(self, self.fig)
|
|
FigureCanvasQTAgg.setSizePolicy(self,
|
|
QtGui.QSizePolicy.Expanding,
|
|
QtGui.QSizePolicy.Expanding)
|
|
FigureCanvasQTAgg.updateGeometry(self)
|
|
|
|
def resizeEvent(self, event):
|
|
FigureCanvasQTAgg.resizeEvent(self, event)
|
|
# Call to `resize_event` missing in FigureManagerQT.
|
|
# See https://github.com/matplotlib/matplotlib/pull/1585
|
|
self.resize_event()
|
|
|
|
|
|
def new_canvas(*args, **kwargs):
|
|
"""Return a new figure canvas."""
|
|
allnums = _pylab_helpers.Gcf.figs.keys()
|
|
num = max(allnums) + 1 if allnums else 1
|
|
|
|
FigureClass = kwargs.pop('FigureClass', Figure)
|
|
figure = FigureClass(*args, **kwargs)
|
|
canvas = FigureCanvas(figure)
|
|
fig_manager = FigureManagerQT(canvas, num)
|
|
return fig_manager.canvas
|
|
|
|
|
|
def new_plot(parent=None, subplot_kw=None, **fig_kw):
|
|
"""Return new figure and axes.
|
|
|
|
Parameters
|
|
----------
|
|
parent : QtWidget
|
|
Qt widget that displays the plot objects. If None, you must manually
|
|
call ``canvas.setParent`` and pass the parent widget.
|
|
subplot_kw : dict
|
|
Keyword arguments passed ``matplotlib.figure.Figure.add_subplot``.
|
|
fig_kw : dict
|
|
Keyword arguments passed ``matplotlib.figure.Figure``.
|
|
"""
|
|
if subplot_kw is None:
|
|
subplot_kw = {}
|
|
canvas = new_canvas(**fig_kw)
|
|
canvas.setParent(parent)
|
|
|
|
fig = canvas.figure
|
|
ax = fig.add_subplot(1, 1, 1, **subplot_kw)
|
|
return fig, ax
|
|
|
|
|
|
def figimage(image, scale=1, dpi=None, **kwargs):
|
|
"""Return figure and axes with figure tightly surrounding image.
|
|
|
|
Unlike pyplot.figimage, this actually plots onto an axes object, which
|
|
fills the figure. Plotting the image onto an axes allows for subsequent
|
|
overlays of axes artists.
|
|
|
|
Parameters
|
|
----------
|
|
image : array
|
|
image to plot
|
|
scale : float
|
|
If scale is 1, the figure and axes have the same dimension as the
|
|
image. Smaller values of `scale` will shrink the figure.
|
|
dpi : int
|
|
Dots per inch for figure. If None, use the default rcParam.
|
|
"""
|
|
dpi = dpi if dpi is not None else mpl.rcParams['figure.dpi']
|
|
kwargs.setdefault('interpolation', 'nearest')
|
|
kwargs.setdefault('cmap', 'gray')
|
|
|
|
h, w, d = np.atleast_3d(image).shape
|
|
figsize = np.array((w, h), dtype=float) / dpi * scale
|
|
|
|
fig, ax = new_plot(figsize=figsize, dpi=dpi)
|
|
fig.subplots_adjust(left=0, bottom=0, right=1, top=1)
|
|
|
|
ax.set_axis_off()
|
|
ax.imshow(image, **kwargs)
|
|
return fig, ax
|
|
|
|
|
|
def update_axes_image(image_axes, image):
|
|
"""Update the image displayed by an image plot.
|
|
|
|
This sets the image plot's array and updates its shape appropriately
|
|
|
|
Parameters
|
|
----------
|
|
image_axes : `matplotlib.image.AxesImage`
|
|
Image axes to update.
|
|
image : array
|
|
Image array.
|
|
"""
|
|
image_axes.set_array(image)
|
|
|
|
# Adjust size if new image shape doesn't match the original
|
|
h, w = image.shape[:2]
|
|
image_axes.set_extent((0, w, h, 0))
|