57
57
from numpy import arange, array, zeros, meshgrid, ndarray
59
from PyQt4.Qwt5 import QwtPlot
62
60
from guiqwt.config import _, CONF, make_title
61
from guiqwt.baseplot import BasePlot
63
62
from guiqwt.curve import CurveItem, ErrorBarCurveItem, GridItem
64
from guiqwt.histogram import HistogramItem
63
from guiqwt.histogram import HistogramItem, lut_range_threshold
65
64
from guiqwt.image import (ImageItem, QuadGridItem, TrImageItem, XYImageItem,
65
Histogram2DItem, RGBImageItem, MaskedImageItem)
67
66
from guiqwt.shapes import (XRangeSelection, RectangleShape, EllipseShape,
67
SegmentShape, VerticalCursor, HorizontalCursor)
69
68
from guiqwt.annotations import (AnnotatedRectangle, AnnotatedEllipse,
69
AnnotatedSegment, AnnotatedVCursor)
71
70
from guiqwt.styles import (update_style_attr, CurveParam, ErrorBarParam,
72
71
style_generator, LabelParam, LegendParam, ImageParam,
73
72
TrImageParam, HistogramParam, Histogram2DParam,
73
RGBImageParam, MaskedImageParam, XYImageParam,
74
74
ImageFilterParam, MARKERS, COLORS, GridParam,
75
75
LineStyleParam, AnnotationParam,
76
76
LabelParamWithContents)
77
77
from guiqwt.label import (LabelItem, LegendBoxItem, RangeComputation,
78
RangeComputation2d, DataInfoLabel,
78
RangeComputation2d, DataInfoLabel, CursorComputation,
79
79
SelectedLegendBoxItem)
80
80
from guiqwt.io import imagefile_to_array
81
81
import os.path as osp
523
514
hist = HistogramItem(curveparam, histparam)
524
515
hist.update_params()
525
516
hist.set_hist_data(data)
526
self.__set_axes(hist, xaxis, yaxis)
517
self.__set_curve_axes(hist, xaxis, yaxis)
529
def __set_image_param(self, param, title, background_color,
530
alpha_mask, alpha, colormap, **kwargs):
520
def __set_image_param(self, param, title, alpha_mask, alpha, interpolation,
532
523
param.label = title
534
525
global IMAGE_COUNT
536
527
param.label = make_title(_("Image"), IMAGE_COUNT)
537
if background_color is not None:
538
param.background = background_color
539
528
if alpha_mask is not None:
529
assert isinstance(alpha_mask, bool)
540
530
param.alpha_mask = alpha_mask
541
531
if alpha is not None:
532
assert (0.0 <= alpha <= 1.0)
542
533
param.alpha = alpha
543
if colormap is not None:
544
param.colormap = colormap
534
interp_methods = {'nearest': 0, 'linear': 1, 'antialiasing': 5}
535
param.interpolation = interp_methods[interpolation]
545
536
for key, val in kwargs.items():
546
setattr(param, key, val)
538
setattr(param, key, val)
548
def _get_image_data(self, data, filename, title, cmap):
540
def _get_image_data(self, data, filename, title, to_grayscale):
550
542
assert filename is not None
551
data = imagefile_to_array(filename)
543
data = imagefile_to_array(filename, to_grayscale=to_grayscale)
552
544
if title is None and filename is not None:
553
545
title = osp.basename(filename)
554
return data, filename, title, cmap
546
return data, filename, title
556
def image(self, data=None, filename=None, title=None, background_color=None,
557
alpha_mask=None, alpha=None, colormap=None,
558
xaxis="bottom", yaxis="left", zaxis="right"):
549
def compute_bounds(data, pixel_size):
550
"""Return image bounds from *pixel_size* (scalar or tuple)"""
551
if not isinstance(pixel_size, (tuple, list)):
552
pixel_size = [pixel_size, pixel_size]
555
xmax, ymax = data.shape[1]*dx, data.shape[0]*dy
556
return xmin, xmax, ymin, ymax
558
def image(self, data=None, filename=None, title=None, alpha_mask=None,
559
alpha=None, background_color=None, colormap=None,
560
xdata=[None, None], ydata=[None, None], pixel_size=None,
561
interpolation='linear', eliminate_outliers=None,
562
xformat='%.1f', yformat='%.1f', zformat='%.1f'):
560
564
Make an image `plot item` from data
561
(:py:class:`guiqwt.image.ImageItem` object)
565
(:py:class:`guiqwt.image.ImageItem` object or
566
:py:class:`guiqwt.image.RGBImageItem` object if data has 3 dimensions)
568
assert isinstance(xdata, (tuple, list)) and len(xdata) == 2
569
assert isinstance(ydata, (tuple, list)) and len(ydata) == 2
563
570
param = ImageParam(title=_("Image"), icon='image.png')
564
params = self._get_image_data(data, filename, title, colormap)
565
data, filename, title, colormap = params
566
self.__set_image_param(param, title, background_color,
567
alpha_mask, alpha, colormap)
571
data, filename, title = self._get_image_data(data, filename, title,
574
return self.rgbimage(data=data, filename=filename, title=title,
575
alpha_mask=alpha_mask, alpha=alpha)
576
assert data.ndim == 2, "Data must have 2 dimensions"
577
if pixel_size is None:
581
xmin, xmax, ymin, ymax = self.compute_bounds(data, pixel_size)
582
self.__set_image_param(param, title, alpha_mask, alpha, interpolation,
583
background=background_color,
585
xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
586
xformat=xformat, yformat=yformat,
568
588
image = ImageItem(data, param)
569
589
image.set_filename(filename)
590
if eliminate_outliers is not None:
591
image.set_lut_range(lut_range_threshold(image, 256,
595
def maskedimage(self, data=None, mask=None, filename=None, title=None,
596
alpha_mask=False, alpha=1.0,
597
xdata=[None, None], ydata=[None, None], pixel_size=None,
598
background_color=None, colormap=None,
599
show_mask=False, fill_value=None, interpolation='linear',
600
eliminate_outliers=None,
601
xformat='%.1f', yformat='%.1f', zformat='%.1f'):
603
Make a masked image `plot item` from data
604
(:py:class:`guiqwt.image.MaskedImageItem` object)
606
assert isinstance(xdata, (tuple, list)) and len(xdata) == 2
607
assert isinstance(ydata, (tuple, list)) and len(ydata) == 2
608
param = MaskedImageParam(title=_("Image"), icon='image.png')
609
data, filename, title = self._get_image_data(data, filename, title,
611
assert data.ndim == 2, "Data must have 2 dimensions"
612
if pixel_size is None:
616
xmin, xmax, ymin, ymax = self.compute_bounds(data, pixel_size)
617
self.__set_image_param(param, title, alpha_mask, alpha, interpolation,
618
background=background_color,
620
xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
621
show_mask=show_mask, fill_value=fill_value,
622
xformat=xformat, yformat=yformat,
624
image = MaskedImageItem(data, mask, param)
625
image.set_filename(filename)
626
if eliminate_outliers is not None:
627
image.set_lut_range(lut_range_threshold(image, 256,
631
def rgbimage(self, data=None, filename=None, title=None,
632
alpha_mask=False, alpha=1.0,
633
xdata=[None, None], ydata=[None, None], pixel_size=None,
634
interpolation='linear'):
636
Make a RGB image `plot item` from data
637
(:py:class:`guiqwt.image.RGBImageItem` object)
639
assert isinstance(xdata, (tuple, list)) and len(xdata) == 2
640
assert isinstance(ydata, (tuple, list)) and len(ydata) == 2
641
param = RGBImageParam(title=_("Image"), icon='image.png')
642
data, filename, title = self._get_image_data(data, filename, title,
644
assert data.ndim == 3, "RGB data must have 3 dimensions"
645
if pixel_size is None:
649
xmin, xmax, ymin, ymax = self.compute_bounds(data, pixel_size)
650
self.__set_image_param(param, title, alpha_mask, alpha, interpolation,
651
xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
652
image = RGBImageItem(data, param)
653
image.set_filename(filename)
572
def quadgrid(self, X, Y, Z, filename=None, title=None,
573
background_color=None, alpha_mask=None, alpha=None,
574
colormap=None, xaxis="bottom", yaxis="left", zaxis="right"):
656
def quadgrid(self, X, Y, Z, filename=None, title=None, alpha_mask=None,
657
alpha=None, background_color=None, colormap=None,
658
interpolation='linear'):
576
660
Make a pseudocolor `plot item` of a 2D array
577
661
(:py:class:`guiqwt.image.QuadGridItem` object)
579
663
param = ImageParam(title=_("Image"), icon='image.png')
580
self.__set_image_param(param, title, background_color,
581
alpha_mask, alpha, colormap)
664
self.__set_image_param(param, title, alpha_mask, alpha, interpolation,
665
background=background_color, colormap=colormap)
582
666
image = QuadGridItem(X, Y, Z, param)
620
704
* interpolation: 'nearest', 'linear' (default), 'antialiasing' (5x5)
622
706
param = TrImageParam(title=_("Image"), icon='image.png')
623
params = self._get_image_data(data, filename, title, colormap)
624
data, filename, title, colormap = params
625
self.__set_image_param(param, title, background_color,
626
alpha_mask, alpha, colormap,
627
x0=x0, y0=y0, angle=angle, dx=dx, dy=dy)
628
interp_methods = {'nearest': 0, 'linear': 1, 'antialiasing': 5}
629
param.interpolation = interp_methods[interpolation]
707
data, filename, title = self._get_image_data(data, filename, title,
709
self.__set_image_param(param, title, alpha_mask, alpha, interpolation,
710
background=background_color, colormap=colormap,
711
x0=x0, y0=y0, angle=angle, dx=dx, dy=dy,
712
xformat=xformat, yformat=yformat,
630
714
image = TrImageItem(data, param)
631
715
image.set_filename(filename)
716
if eliminate_outliers is not None:
717
image.set_lut_range(lut_range_threshold(image, 256,
634
def xyimage(self, x, y, data, title=None, background_color=None,
635
alpha_mask=None, alpha=None, colormap=None,
636
xaxis="bottom", yaxis="left", zaxis="right"):
721
def xyimage(self, x, y, data, title=None, alpha_mask=None, alpha=None,
722
background_color=None, colormap=None,
723
interpolation='linear', eliminate_outliers=None,
724
xformat='%.1f', yformat='%.1f', zformat='%.1f'):
638
726
Make an xyimage `plot item` (image with non-linear X/Y axes) from data
639
727
(:py:class:`guiqwt.image.XYImageItem` object)