source: sasview/src/sas/sasgui/plottools/plottables.py @ b9d74f3

Last change on this file since b9d74f3 was b9d74f3, checked in by andyfaff, 8 years ago

MAINT: use raise Exception() not raise Exception

  • Property mode set to 100644
File size: 39.8 KB
Line 
1"""
2Prototype plottable object support.
3
4The main point of this prototype is to provide a clean separation between
5the style (plotter details: color, grids, widgets, etc.) and substance
6(application details: which information to plot).  Programmers should not be
7dictating line colours and plotting symbols.
8
9Unlike the problem of style in CSS or Word, where most paragraphs look
10the same, each line on a graph has to be distinguishable from its neighbours.
11Our solution is to provide parametric styles, in which a number of
12different classes of object (e.g., reflectometry data, reflectometry
13theory) representing multiple graph primitives cycle through a colour
14palette provided by the underlying plotter.
15
16A full treatment would provide perceptual dimensions of prominence and
17distinctiveness rather than a simple colour number.
18
19"""
20
21# Design question: who owns the color?
22# Is it a property of the plottable?
23# Or of the plottable as it exists on the graph?
24# Or if the graph?
25# If a plottable can appear on multiple graphs, in some case the
26# color should be the same on each graph in which it appears, and
27# in other cases (where multiple plottables from different graphs
28# coexist), the color should be assigned by the graph.  In any case
29# once a plottable is placed on the graph its color should not
30# depend on the other plottables on the graph.  Furthermore, if
31# a plottable is added and removed from a graph and added again,
32# it may be nice, but not necessary, to have the color persist.
33#
34# The safest approach seems to be to give ownership of color
35# to the graph, which will allocate the colors along with the
36# plottable.  The plottable will need to return the number of
37# colors that are needed.
38#
39# The situation is less clear for symbols.  It is less clear
40# how much the application requires that symbols be unique across
41# all plots on the graph.
42
43# Support for ancient python versions
44import copy
45import numpy as np
46import sys
47import logging
48
49logger = logging.getLogger(__name__)
50
51if 'any' not in dir(__builtins__):
52    def any(L):
53        for cond in L:
54            if cond:
55                return True
56        return False
57
58    def all(L):
59        for cond in L:
60            if not cond:
61                return False
62        return True
63
64
65class Graph(object):
66    """
67    Generic plottables graph structure.
68
69    Plot styles are based on color/symbol lists.  The user gets to select
70    the list of colors/symbols/sizes to choose from, not the application
71    developer.  The programmer only gets to add/remove lines from the
72    plot and move to the next symbol/color.
73
74    Another dimension is prominence, which refers to line sizes/point sizes.
75
76    Axis transformations allow the user to select the coordinate view
77    which provides clarity to the data.  There is no way we can provide
78    every possible transformation for every application generically, so
79    the plottable objects themselves will need to provide the transformations.
80    Here are some examples from reflectometry: ::
81
82       independent: x -> f(x)
83          monitor scaling: y -> M*y
84          log:  y -> log(y if y > min else min)
85          cos:  y -> cos(y*pi/180)
86       dependent:   x -> f(x,y)
87          Q4:      y -> y*x^4
88          fresnel: y -> y*fresnel(x)
89       coordinated: x,y = f(x,y)
90          Q:    x -> 2*pi/L (cos(x*pi/180) - cos(y*pi/180))
91                y -> 2*pi/L (sin(x*pi/180) + sin(y*pi/180))
92       reducing: x,y = f(x1,x2,y1,y2)
93          spin asymmetry: x -> x1, y -> (y1 - y2)/(y1 + y2)
94          vector net: x -> x1, y -> y1*cos(y2*pi/180)
95
96    Multiple transformations are possible, such as Q4 spin asymmetry
97
98    Axes have further complications in that the units of what are being
99    plotted should correspond to the units on the axes.  Plotting multiple
100    types on the same graph should be handled gracefully, e.g., by creating
101    a separate tab for each available axis type, breaking into subplots,
102    showing multiple axes on the same plot, or generating inset plots.
103    Ultimately the decision should be left to the user.
104
105    Graph properties such as grids/crosshairs should be under user control,
106    as should the sizes of items such as axis fonts, etc.  No direct
107    access will be provided to the application.
108
109    Axis limits are mostly under user control.  If the user has zoomed or
110    panned then those limits are preserved even if new data is plotted.
111    The exception is when, e.g., scanning through a set of related lines
112    in which the user may want to fix the limits so that user can compare
113    the values directly.  Another exception is when creating multiple
114    graphs sharing the same limits, though this case may be important
115    enough that it is handled by the graph widget itself.  Axis limits
116    will of course have to understand the effects of axis transformations.
117
118    High level plottable objects may be composed of low level primitives.
119    Operations such as legend/hide/show copy/paste, etc. need to operate
120    on these primitives as a group.  E.g., allowing the user to have a
121    working canvas where they can drag lines they want to save and annotate
122    them.
123
124    Graphs need to be printable.  A page layout program for entire plots
125    would be nice.
126
127    """
128    def _xaxis_transformed(self, name, units):
129        """
130        Change the property of the x axis
131        according to an axis transformation
132        (as opposed to changing the basic properties)
133        """
134        if units != "":
135            name = "%s (%s)" % (name, units)
136        self.prop["xlabel"] = name
137        self.prop["xunit"] = units
138
139    def _yaxis_transformed(self, name, units):
140        """
141        Change the property of the y axis
142        according to an axis transformation
143        (as opposed to changing the basic properties)
144        """
145        if units != "":
146            name = "%s (%s)" % (name, units)
147        self.prop["ylabel"] = name
148        self.prop["yunit"] = units
149
150    def xaxis(self, name, units):
151        """
152        Properties of the x axis.
153        """
154        if units != "":
155            name = "%s (%s)" % (name, units)
156        self.prop["xlabel"] = name
157        self.prop["xunit"] = units
158        self.prop["xlabel_base"] = name
159        self.prop["xunit_base"] = units
160
161    def yaxis(self, name, units):
162        """
163        Properties of the y axis.
164        """
165        if units != "":
166            name = "%s (%s)" % (name, units)
167        self.prop["ylabel"] = name
168        self.prop["yunit"] = units
169        self.prop["ylabel_base"] = name
170        self.prop["yunit_base"] = units
171
172    def title(self, name):
173        """
174        Graph title
175        """
176        self.prop["title"] = name
177
178    def get(self, key):
179        """
180        Get the graph properties
181        """
182        if key == "color":
183            return self.color
184        elif key == "symbol":
185            return self.symbol
186        else:
187            return self.prop[key]
188
189    def set(self, **kw):
190        """
191        Set the graph properties
192        """
193        for key in kw:
194            if key == "color":
195                self.color = kw[key] % len(self.colorlist)
196            elif key == "symbol":
197                self.symbol = kw[key] % len(self.symbollist)
198            else:
199                self.prop[key] = kw[key]
200
201    def isPlotted(self, plottable):
202        """Return True is the plottable is already on the graph"""
203        if plottable in self.plottables:
204            return True
205        return False
206
207    def add(self, plottable, color=None):
208        """Add a new plottable to the graph"""
209        # record the colour associated with the plottable
210        if not plottable in self.plottables:
211            if color is not None:
212                self.plottables[plottable] = color
213            else:
214                self.color += plottable.colors()
215                self.plottables[plottable] = self.color
216                plottable.custom_color = self.color
217
218    def changed(self):
219        """Detect if any graphed plottables have changed"""
220        return any([p.changed() for p in self.plottables])
221
222    def get_range(self):
223        """
224        Return the range of all displayed plottables
225        """
226        min_value = None
227        max_value = None
228        for p in self.plottables:
229            if p.hidden == True:
230                continue
231            if p.x is not None:
232                for x_i in p.x:
233                    if min_value is None or x_i < min_value:
234                        min_value = x_i
235                    if max_value is None or x_i > max_value:
236                        max_value = x_i
237        return min_value, max_value
238
239    def replace(self, plottable):
240        """Replace an existing plottable from the graph"""
241        selected_color = None
242        selected_plottable = None
243        for p in self.plottables.keys():
244            if plottable.id == p.id:
245                selected_plottable = p
246                selected_color = self.plottables[p]
247                break
248        if  selected_plottable is not None and selected_color is not None:
249            del self.plottables[selected_plottable]
250            self.plottables[plottable] = selected_color
251
252    def delete(self, plottable):
253        """Remove an existing plottable from the graph"""
254        if plottable in self.plottables:
255            del self.plottables[plottable]
256            self.color = len(self.plottables)
257
258    def reset_scale(self):
259        """
260        Resets the scale transformation data to the underlying data
261        """
262        for p in self.plottables:
263            p.reset_view()
264
265    def reset(self):
266        """Reset the graph."""
267        self.color = -1
268        self.symbol = 0
269        self.prop = {"xlabel": "", "xunit": None,
270                     "ylabel": "", "yunit": None,
271                     "title": ""}
272        self.plottables = {}
273
274    def _make_labels(self):
275        """
276        """
277        # Find groups of related plottables
278        sets = {}
279        for p in self.plottables:
280            if p.__class__ in sets:
281                sets[p.__class__].append(p)
282            else:
283                sets[p.__class__] = [p]
284        # Ask each plottable class for a set of unique labels
285        labels = {}
286        for c in sets:
287            labels.update(c.labels(sets[c]))
288        return labels
289
290    def get_plottable(self, name):
291        """
292        Return the plottable with the given
293        name if it exists. Otherwise return None
294        """
295        for item in self.plottables:
296            if item.name == name:
297                return item
298        return None
299
300    def returnPlottable(self):
301        """
302        This method returns a dictionary of plottables contained in graph
303        It is just by Plotpanel to interact with the complete list of plottables
304        inside the graph.
305        """
306        return self.plottables
307
308    def render(self, plot):
309        """Redraw the graph"""
310        plot.connect.clearall()
311        plot.clear()
312        plot.properties(self.prop)
313        labels = self._make_labels()
314        for p in self.plottables:
315            if p.custom_color is not None:
316                p.render(plot, color=p.custom_color, symbol=0,
317                         markersize=p.markersize, label=labels[p])
318            else:
319                p.render(plot, color=self.plottables[p], symbol=0,
320                         markersize=p.markersize, label=labels[p])
321        plot.render()
322
323    def __init__(self, **kw):
324        self.reset()
325        self.set(**kw)
326        # Name of selected plottable, if any
327        self.selected_plottable = None
328
329
330# Transform interface definition
331# No need to inherit from this class, just need to provide
332# the same methods.
333class Transform(object):
334    """
335    Define a transform plugin to the plottable architecture.
336
337    Transforms operate on axes.  The plottable defines the
338    set of transforms available for it, and the axes on which
339    they operate.  These transforms can operate on the x axis
340    only, the y axis only or on the x and y axes together.
341
342    This infrastructure is not able to support transformations
343    such as log and polar plots as these require full control
344    over the drawing of axes and grids.
345
346    A transform has a number of attributes.
347
348    name
349      user visible name for the transform.  This will
350      appear in the context menu for the axis and the transform
351      menu for the graph.
352
353    type
354      operational axis.  This determines whether the
355      transform should appear on x,y or z axis context
356      menus, or if it should appear in the context menu for
357      the graph.
358
359    inventory
360      (not implemented)
361      a dictionary of user settable parameter names and
362      their associated types.  These should appear as keyword
363      arguments to the transform call.  For example, Fresnel
364      reflectivity requires the substrate density:
365      ``{ 'rho': type.Value(10e-6/units.angstrom**2) }``
366      Supply reasonable defaults in the callback so that
367      limited plotting clients work even though they cannot
368      set the inventory.
369
370    """
371    def __call__(self, plottable, **kwargs):
372        """
373        Transform the data.  Whenever a plottable is added
374        to the axes, the infrastructure will apply all required
375        transforms.  When the user selects a different representation
376        for the axes (via menu, script, or context menu), all
377        plottables on the axes will be transformed.  The
378        plottable should store the underlying data but set
379        the standard x,dx,y,dy,z,dz attributes appropriately.
380
381        If the call raises a NotImplemented error the dataline
382        will not be plotted.  The associated string will usually
383        be 'Not a valid transform', though other strings are possible.
384        The application may or may not display the message to the
385        user, along with an indication of which plottable was at fault.
386
387        """
388        raise NotImplemented("Not a valid transform")
389
390    # Related issues
391    # ==============
392    #
393    # log scale:
394    #    All axes have implicit log/linear scaling options.
395    #
396    # normalization:
397    #    Want to display raw counts vs detector efficiency correction
398    #    Want to normalize by time/monitor/proton current/intensity.
399    #    Want to display by eg. counts per 3 sec or counts per 10000 monitor.
400    #    Want to divide by footprint (ab initio, fitted or measured).
401    #    Want to scale by attenuator values.
402    #
403    # compare/contrast:
404    #    Want to average all visible lines with the same tag, and
405    #    display difference from one particular line.  Not a transform
406    #    issue?
407    #
408    # multiline graph:
409    #    How do we show/hide data parts.  E.g., data or theory, or
410    #    different polarization cross sections?  One way is with
411    #    tags: each plottable has a set of tags and the tags are
412    #    listed as check boxes above the plotting area.  Click a
413    #    tag and all plottables with that tag are hidden on the
414    #    plot and on the legend.
415    #
416    # nonconformant y-axes:
417    #    What do we do with temperature vs. Q and reflectivity vs. Q
418    #    on the same graph?
419    #
420    # 2D -> 1D:
421    #    Want various slices through the data.  Do transforms apply
422    #    to the sliced data as well?
423
424
425class Plottable(object):
426    """
427    """
428    # Short ascii name to refer to the plottable in a menu
429    short_name = None
430    # Fancy name
431    name = None
432    # Data
433    x = None
434    y = None
435    dx = None
436    dy = None
437    # Parameter to allow a plot to be part of the list without being displayed
438    hidden = False
439    # Flag to set whether a plottable has an interactor or not
440    interactive = True
441    custom_color = None
442    markersize = 5  # default marker size is 'size 5'
443
444    def __init__(self):
445        self.view = View()
446        self._xaxis = ""
447        self._xunit = ""
448        self._yaxis = ""
449        self._yunit = ""
450
451    def __setattr__(self, name, value):
452        """
453        Take care of changes in View when data is changed.
454        This method is provided for backward compatibility.
455        """
456        object.__setattr__(self, name, value)
457        if name in ['x', 'y', 'dx', 'dy']:
458            self.reset_view()
459            # print "self.%s has been called" % name
460
461    def set_data(self, x, y, dx=None, dy=None):
462        """
463        """
464        self.x = x
465        self.y = y
466        self.dy = dy
467        self.dx = dx
468        self.transformView()
469
470    def xaxis(self, name, units):
471        """
472        Set the name and unit of x_axis
473
474        :param name: the name of x-axis
475        :param units: the units of x_axis
476
477        """
478        self._xaxis = name
479        self._xunit = units
480
481    def yaxis(self, name, units):
482        """
483        Set the name and unit of y_axis
484
485        :param name: the name of y-axis
486        :param units: the units of y_axis
487
488        """
489        self._yaxis = name
490        self._yunit = units
491
492    def get_xaxis(self):
493        """Return the units and name of x-axis"""
494        return self._xaxis, self._xunit
495
496    def get_yaxis(self):
497        """ Return the units and name of y- axis"""
498        return self._yaxis, self._yunit
499
500    @classmethod
501    def labels(cls, collection):
502        """
503        Construct a set of unique labels for a collection of plottables of
504        the same type.
505
506        Returns a map from plottable to name.
507
508        """
509        n = len(collection)
510        label_dict = {}
511        if n > 0:
512            basename = str(cls).split('.')[-1]
513            if n == 1:
514                label_dict[collection[0]] = basename
515            else:
516                for i in xrange(len(collection)):
517                    label_dict[collection[i]] = "%s %d" % (basename, i)
518        return label_dict
519
520    # #Use the following if @classmethod doesn't work
521    # labels = classmethod(labels)
522    def setLabel(self, labelx, labely):
523        """
524        It takes a label of the x and y transformation and set View parameters
525
526        :param transx: The label of x transformation is sent by Properties Dialog
527        :param transy: The label of y transformation is sent Properties Dialog
528
529        """
530        self.view.xLabel = labelx
531        self.view.yLabel = labely
532
533    def set_View(self, x, y):
534        """Load View"""
535        self.x = x
536        self.y = y
537        self.reset_view()
538
539    def reset_view(self):
540        """Reload view with new value to plot"""
541        self.view = View(self.x, self.y, self.dx, self.dy)
542        self.view.Xreel = self.view.x
543        self.view.Yreel = self.view.y
544        self.view.DXreel = self.view.dx
545        self.view.DYreel = self.view.dy
546
547    def render(self, plot):
548        """
549        The base class makes sure the correct units are being used for
550        subsequent plottable.
551
552        For now it is assumed that the graphs are commensurate, and if you
553        put a Qx object on a Temperature graph then you had better hope
554        that it makes sense.
555
556        """
557        plot.xaxis(self._xaxis, self._xunit)
558        plot.yaxis(self._yaxis, self._yunit)
559
560    def is_empty(self):
561        """
562        Returns True if there is no data stored in the plottable
563        """
564        if (self.x is not None and len(self.x) == 0
565            and self.y is not None and len(self.y) == 0):
566            return True
567        return False
568
569    def colors(self):
570        """Return the number of colors need to render the object"""
571        return 1
572
573    def transformView(self):
574        """
575        It transforms x, y before displaying
576        """
577        self.view.transform(self.x, self.y, self.dx, self.dy)
578
579    def returnValuesOfView(self):
580        """
581        Return View parameters and it is used by Fit Dialog
582        """
583        return self.view.returnXview()
584
585    def check_data_PlottableX(self):
586        """
587        Since no transformation is made for log10(x), check that
588        no negative values is plot in log scale
589        """
590        self.view.check_data_logX()
591
592    def check_data_PlottableY(self):
593        """
594        Since no transformation is made for log10(y), check that
595        no negative values is plot in log scale
596        """
597        self.view.check_data_logY()
598
599    def transformX(self, transx, transdx):
600        """
601        Receive pointers to function that transform x and dx
602        and set corresponding View pointers
603
604        :param transx: pointer to function that transforms x
605        :param transdx: pointer to function that transforms dx
606
607        """
608        self.view.setTransformX(transx, transdx)
609
610    def transformY(self, transy, transdy):
611        """
612        Receive pointers to function that transform y and dy
613        and set corresponding View pointers
614
615        :param transy: pointer to function that transforms y
616        :param transdy: pointer to function that transforms dy
617
618        """
619        self.view.setTransformY(transy, transdy)
620
621    def onReset(self):
622        """
623        Reset x, y, dx, dy view with its parameters
624        """
625        self.view.onResetView()
626
627    def onFitRange(self, xmin=None, xmax=None):
628        """
629        It limits View data range to plot from min to max
630
631        :param xmin: the minimum value of x to plot.
632        :param xmax: the maximum value of x to plot
633
634        """
635        self.view.onFitRangeView(xmin, xmax)
636
637
638class View(object):
639    """
640    Representation of the data that might include a transformation
641    """
642    x = None
643    y = None
644    dx = None
645    dy = None
646
647    def __init__(self, x=None, y=None, dx=None, dy=None):
648        """
649        """
650        self.x = x
651        self.y = y
652        self.dx = dx
653        self.dy = dy
654        # To change x range to the reel range
655        self.Xreel = self.x
656        self.Yreel = self.y
657        self.DXreel = self.dx
658        self.DYreel = self.dy
659        # Labels of x and y received from Properties Dialog
660        self.xLabel = ""
661        self.yLabel = ""
662        # Function to transform x, y, dx and dy
663        self.funcx = None
664        self.funcy = None
665        self.funcdx = None
666        self.funcdy = None
667
668    def transform(self, x=None, y=None, dx=None, dy=None):
669        """
670        Transforms the x,y,dx and dy vectors and stores
671         the output in View parameters
672
673        :param x: array of x values
674        :param y: array of y values
675        :param dx: array of  errors values on x
676        :param dy: array of error values on y
677
678        """
679        # Sanity check
680        # Do the transofrmation only when x and y are empty
681        has_err_x = not (dx is None or len(dx) == 0)
682        has_err_y = not (dy is None or len(dy) == 0)
683
684        if(x is not None) and (y is not None):
685            if dx is not None and not len(dx) == 0 and not len(x) == len(dx):
686                msg = "Plottable.View: Given x and dx are not"
687                msg += " of the same length"
688                raise ValueError(msg)
689            # Check length of y array
690            if not len(y) == len(x):
691                msg = "Plottable.View: Given y "
692                msg += "and x are not of the same length"
693                raise ValueError(msg)
694
695            if dy is not None and not len(dy) == 0 and not len(y) == len(dy):
696                msg = "Plottable.View: Given y and dy are not of the same "
697                msg += "length: len(y)=%s, len(dy)=%s" % (len(y), len(dy))
698                raise ValueError(msg)
699            self.x = []
700            self.y = []
701            if has_err_x:
702                self.dx = []
703            else:
704                self.dx = None
705            if has_err_y:
706                self.dy = []
707            else:
708                self.dy = None
709            if not has_err_x:
710                dx = np.zeros(len(x))
711            if not has_err_y:
712                dy = np.zeros(len(y))
713            for i in range(len(x)):
714                try:
715                    tempx = self.funcx(x[i], y[i])
716                    tempy = self.funcy(y[i], x[i])
717                    if has_err_x:
718                        tempdx = self.funcdx(x[i], y[i], dx[i], dy[i])
719                    if has_err_y:
720                        tempdy = self.funcdy(y[i], x[i], dy[i], dx[i])
721                    self.x.append(tempx)
722                    self.y.append(tempy)
723                    if has_err_x:
724                        self.dx.append(tempdx)
725                    if has_err_y:
726                        self.dy.append(tempdy)
727                except Exception:
728                    pass
729            # Sanity check
730            if not len(self.x) == len(self.y):
731                msg = "Plottable.View: transformed x "
732                msg += "and y are not of the same length"
733                raise ValueError(msg)
734            if has_err_x and not (len(self.x) == len(self.dx)):
735                msg = "Plottable.View: transformed x and dx"
736                msg += " are not of the same length"
737                raise ValueError(msg)
738            if has_err_y and not (len(self.y) == len(self.dy)):
739                msg = "Plottable.View: transformed y"
740                msg += " and dy are not of the same length"
741                raise ValueError(msg)
742            # Check that negative values are not plot on x and y axis for
743            # log10 transformation
744            self.check_data_logX()
745            self.check_data_logY()
746            # Store x ,y dx,and dy in their full range for reset
747            self.Xreel = self.x
748            self.Yreel = self.y
749            self.DXreel = self.dx
750            self.DYreel = self.dy
751
752    def onResetView(self):
753        """
754        Reset x,y,dx and y in their full range  and in the initial scale
755        in case their previous range has changed
756        """
757        self.x = self.Xreel
758        self.y = self.Yreel
759        self.dx = self.DXreel
760        self.dy = self.DYreel
761
762    def setTransformX(self, funcx, funcdx):
763        """
764        Receive pointers to function that transform x and dx
765        and set corresponding View pointers
766
767        :param transx: pointer to function that transforms x
768        :param transdx: pointer to function that transforms dx
769        """
770        self.funcx = funcx
771        self.funcdx = funcdx
772
773    def setTransformY(self, funcy, funcdy):
774        """
775        Receive pointers to function that transform y and dy
776        and set corresponding View pointers
777
778        :param transx: pointer to function that transforms y
779        :param transdx: pointer to function that transforms dy
780        """
781        self.funcy = funcy
782        self.funcdy = funcdy
783
784    def returnXview(self):
785        """
786        Return View  x,y,dx,dy
787        """
788        return self.x, self.y, self.dx, self.dy
789
790    def check_data_logX(self):
791        """
792        Remove negative value in x vector to avoid plotting negative
793        value of Log10
794        """
795        tempx = []
796        tempdx = []
797        tempy = []
798        tempdy = []
799        if self.dx is None:
800            self.dx = np.zeros(len(self.x))
801        if self.dy is None:
802            self.dy = np.zeros(len(self.y))
803        if self.xLabel == "log10(x)":
804            for i in range(len(self.x)):
805                try:
806                    if self.x[i] > 0:
807                        tempx.append(self.x[i])
808                        tempdx.append(self.dx[i])
809                        tempy.append(self.y[i])
810                        tempdy.append(self.dy[i])
811                except:
812                    logger.error("check_data_logX: skipping point x %g", self.x[i])
813                    logger.error(sys.exc_value)
814            self.x = tempx
815            self.y = tempy
816            self.dx = tempdx
817            self.dy = tempdy
818
819    def check_data_logY(self):
820        """
821        Remove negative value in y vector
822        to avoid plotting negative value of Log10
823
824        """
825        tempx = []
826        tempdx = []
827        tempy = []
828        tempdy = []
829        if self.dx is None:
830            self.dx = np.zeros(len(self.x))
831        if self.dy is None:
832            self.dy = np.zeros(len(self.y))
833        if self.yLabel == "log10(y)":
834            for i in range(len(self.x)):
835                try:
836                    if self.y[i] > 0:
837                        tempx.append(self.x[i])
838                        tempdx.append(self.dx[i])
839                        tempy.append(self.y[i])
840                        tempdy.append(self.dy[i])
841                except:
842                    logger.error("check_data_logY: skipping point %g", self.y[i])
843                    logger.error(sys.exc_value)
844
845            self.x = tempx
846            self.y = tempy
847            self.dx = tempdx
848            self.dy = tempdy
849
850    def onFitRangeView(self, xmin=None, xmax=None):
851        """
852        It limits View data range to plot from min to max
853
854        :param xmin: the minimum value of x to plot.
855        :param xmax: the maximum value of x to plot
856
857        """
858        tempx = []
859        tempdx = []
860        tempy = []
861        tempdy = []
862        if self.dx is None:
863            self.dx = np.zeros(len(self.x))
864        if self.dy is None:
865            self.dy = np.zeros(len(self.y))
866        if xmin is not None and xmax is not None:
867            for i in range(len(self.x)):
868                if self.x[i] >= xmin and self.x[i] <= xmax:
869                    tempx.append(self.x[i])
870                    tempdx.append(self.dx[i])
871                    tempy.append(self.y[i])
872                    tempdy.append(self.dy[i])
873            self.x = tempx
874            self.y = tempy
875            self.dx = tempdx
876            self.dy = tempdy
877
878
879class Data2D(Plottable):
880    """
881    2D data class for image plotting
882    """
883    def __init__(self, image=None, qx_data=None, qy_data=None,
884                 err_image=None, xmin=None, xmax=None, ymin=None,
885                 ymax=None, zmin=None, zmax=None):
886        """
887        Draw image
888        """
889        Plottable.__init__(self)
890        self.name = "Data2D"
891        self.label = None
892        self.data = image
893        self.qx_data = qx_data
894        self.qy_data = qx_data
895        self.err_data = err_image
896        self.source = None
897        self.detector = []
898
899        # # Units for Q-values
900        self.xy_unit = 'A^{-1}'
901        # # Units for I(Q) values
902        self.z_unit = 'cm^{-1}'
903        self._zaxis = ''
904        # x-axis unit and label
905        self._xaxis = '\\rm{Q_{x}}'
906        self._xunit = 'A^{-1}'
907        # y-axis unit and label
908        self._yaxis = '\\rm{Q_{y}}'
909        self._yunit = 'A^{-1}'
910
911        # ## might remove that later
912        # # Vector of Q-values at the center of each bin in x
913        self.x_bins = []
914        # # Vector of Q-values at the center of each bin in y
915        self.y_bins = []
916
917        # x and y boundaries
918        self.xmin = xmin
919        self.xmax = xmax
920        self.ymin = ymin
921        self.ymax = ymax
922
923        self.zmin = zmin
924        self.zmax = zmax
925        self.id = None
926
927    def xaxis(self, label, unit):
928        """
929        set x-axis
930
931        :param label: x-axis label
932        :param unit: x-axis unit
933
934        """
935        self._xaxis = label
936        self._xunit = unit
937
938    def yaxis(self, label, unit):
939        """
940        set y-axis
941
942        :param label: y-axis label
943        :param unit: y-axis unit
944
945        """
946        self._yaxis = label
947        self._yunit = unit
948
949    def zaxis(self, label, unit):
950        """
951        set z-axis
952
953        :param label: z-axis label
954        :param unit: z-axis unit
955
956        """
957        self._zaxis = label
958        self._zunit = unit
959
960    def setValues(self, datainfo=None):
961        """
962        Use datainfo object to initialize data2D
963
964        :param datainfo: object
965
966        """
967        self.image = copy.deepcopy(datainfo.data)
968        self.qx_data = copy.deepcopy(datainfo.qx_data)
969        self.qy_data = copy.deepcopy(datainfo.qy_data)
970        self.err_image = copy.deepcopy(datainfo.err_data)
971
972        self.xy_unit = datainfo.Q_unit
973        self.z_unit = datainfo.I_unit
974        self._zaxis = datainfo._zaxis
975
976        self.xaxis(datainfo._xunit, datainfo._xaxis)
977        self.yaxis(datainfo._yunit, datainfo._yaxis)
978        # x and y boundaries
979        self.xmin = datainfo.xmin
980        self.xmax = datainfo.xmax
981        self.ymin = datainfo.ymin
982        self.ymax = datainfo.ymax
983        # # Vector of Q-values at the center of each bin in x
984        self.x_bins = datainfo.x_bins
985        # # Vector of Q-values at the center of each bin in y
986        self.y_bins = datainfo.y_bins
987
988    def set_zrange(self, zmin=None, zmax=None):
989        """
990        """
991        if zmin < zmax:
992            self.zmin = zmin
993            self.zmax = zmax
994        else:
995            raise "zmin is greater or equal to zmax "
996
997    def render(self, plot, **kw):
998        """
999        Renders the plottable on the graph
1000
1001        """
1002        plot.image(self.data, self.qx_data, self.qy_data,
1003                   self.xmin, self.xmax, self.ymin,
1004                   self.ymax, self.zmin, self.zmax, **kw)
1005
1006    def changed(self):
1007        """
1008        """
1009        return False
1010
1011    @classmethod
1012    def labels(cls, collection):
1013        """Build a label mostly unique within a collection"""
1014        label_dict = {}
1015        for item in collection:
1016            if item.label == "Data2D":
1017                item.label = item.name
1018            label_dict[item] = item.label
1019        return label_dict
1020
1021
1022class Data1D(Plottable):
1023    """
1024    Data plottable: scatter plot of x,y with errors in x and y.
1025    """
1026
1027    def __init__(self, x, y, dx=None, dy=None, lam=None, dlam=None):
1028        """
1029        Draw points specified by x[i],y[i] in the current color/symbol.
1030        Uncertainty in x is given by dx[i], or by (xlo[i],xhi[i]) if the
1031        uncertainty is asymmetric.  Similarly for y uncertainty.
1032
1033        The title appears on the legend.
1034        The label, if it is different, appears on the status bar.
1035        """
1036        Plottable.__init__(self)
1037        self.name = "data"
1038        self.label = "data"
1039        self.x = x
1040        self.y = y
1041        self.lam = lam
1042        self.dx = dx
1043        self.dy = dy
1044        self.dlam = dlam
1045        self.source = None
1046        self.detector = None
1047        self.xaxis('', '')
1048        self.yaxis('', '')
1049        self.view = View(self.x, self.y, self.dx, self.dy)
1050        self.symbol = 0
1051        self.custom_color = None
1052        self.markersize = 5
1053        self.id = None
1054        self.zorder = 1
1055        self.hide_error = False
1056
1057    def render(self, plot, **kw):
1058        """
1059        Renders the plottable on the graph
1060        """
1061        if self.interactive == True:
1062            kw['symbol'] = self.symbol
1063            kw['id'] = self.id
1064            kw['hide_error'] = self.hide_error
1065            kw['markersize'] = self.markersize
1066            plot.interactive_points(self.view.x, self.view.y,
1067                                    dx=self.view.dx, dy=self.view.dy,
1068                                    name=self.name, zorder=self.zorder, **kw)
1069        else:
1070            kw['id'] = self.id
1071            kw['hide_error'] = self.hide_error
1072            kw['symbol'] = self.symbol
1073            kw['color'] = self.custom_color
1074            kw['markersize'] = self.markersize
1075            plot.points(self.view.x, self.view.y, dx=self.view.dx,
1076                        dy=self.view.dy, zorder=self.zorder,
1077                        marker=self.symbollist[self.symbol], **kw)
1078
1079    def changed(self):
1080        return False
1081
1082    @classmethod
1083    def labels(cls, collection):
1084        """Build a label mostly unique within a collection"""
1085        label_dict = {}
1086        for item in collection:
1087            if item.label == "data":
1088                item.label = item.name
1089            label_dict[item] = item.label
1090        return label_dict
1091
1092
1093class Theory1D(Plottable):
1094    """
1095    Theory plottable: line plot of x,y with confidence interval y.
1096    """
1097    def __init__(self, x, y, dy=None):
1098        """
1099        Draw lines specified in x[i],y[i] in the current color/symbol.
1100        Confidence intervals in x are given by dx[i] or by (xlo[i],xhi[i])
1101        if the limits are asymmetric.
1102
1103        The title is the name that will show up on the legend.
1104        """
1105        Plottable.__init__(self)
1106        msg = "Theory1D is no longer supported, please use Data1D and change symbol.\n"
1107        raise DeprecationWarning(msg)
1108
1109class Fit1D(Plottable):
1110    """
1111    Fit plottable: composed of a data line plus a theory line.  This
1112    is treated like a single object from the perspective of the graph,
1113    except that it will have two legend entries, one for the data and
1114    one for the theory.
1115
1116    The color of the data and theory will be shared.
1117
1118    """
1119    def __init__(self, data=None, theory=None):
1120        """
1121        """
1122        Plottable.__init__(self)
1123        self.data = data
1124        self.theory = theory
1125
1126    def render(self, plot, **kw):
1127        """
1128        """
1129        self.data.render(plot, **kw)
1130        self.theory.render(plot, **kw)
1131
1132    def changed(self):
1133        """
1134        """
1135        return self.data.changed() or self.theory.changed()
1136
1137
1138# ---------------------------------------------------------------
1139class Text(Plottable):
1140    """
1141    """
1142    def __init__(self, text=None, xpos=0.5, ypos=0.9, name='text'):
1143        """
1144        Draw the user-defined text in plotter
1145        We can specify the position of text
1146        """
1147        Plottable.__init__(self)
1148        self.name = name
1149        self.text = text
1150        self.xpos = xpos
1151        self.ypos = ypos
1152
1153    def render(self, plot, **kw):
1154        """
1155        """
1156        from matplotlib import transforms
1157
1158        xcoords = transforms.blended_transform_factory(plot.subplot.transAxes,
1159                                                       plot.subplot.transAxes)
1160        plot.subplot.text(self.xpos,
1161                          self.ypos,
1162                          self.text,
1163                          label=self.name,
1164                          transform=xcoords)
1165
1166    def setText(self, text):
1167        """Set the text string."""
1168        self.text = text
1169
1170    def getText(self, text):
1171        """Get the text string."""
1172        return self.text
1173
1174    def set_x(self, x):
1175        """
1176        Set the x position of the text
1177        ACCEPTS: float
1178        """
1179        self.xpos = x
1180
1181    def set_y(self, y):
1182        """
1183        Set the y position of the text
1184        ACCEPTS: float
1185        """
1186        self.ypos = y
1187
1188
1189# ---------------------------------------------------------------
1190class Chisq(Plottable):
1191    """
1192    Chisq plottable plots the chisq
1193    """
1194    def __init__(self, chisq=None):
1195        """
1196        Draw the chisq in plotter
1197        We can specify the position of chisq
1198        """
1199        Plottable.__init__(self)
1200        self.name = "chisq"
1201        self._chisq = chisq
1202        self.xpos = 0.5
1203        self.ypos = 0.9
1204
1205    def render(self, plot, **kw):
1206        """
1207        """
1208        if  self._chisq is None:
1209            chisqTxt = r'$\chi^2=$'
1210        else:
1211            chisqTxt = r'$\chi^2=%g$' % (float(self._chisq))
1212
1213        from matplotlib import transforms
1214
1215        xcoords = transforms.blended_transform_factory(plot.subplot.transAxes,
1216                                                      plot.subplot.transAxes)
1217        plot.subplot.text(self.xpos,
1218                          self.ypos,
1219                          chisqTxt, label='chisq',
1220                          transform=xcoords)
1221
1222    def setChisq(self, chisq):
1223        """
1224        Set the chisq value.
1225        """
1226        self._chisq = chisq
1227
1228
1229######################################################
1230
1231def sample_graph():
1232    import numpy as np
1233
1234    # Construct a simple graph
1235    if False:
1236        x = np.array([1, 2, 3, 4, 5, 6], 'd')
1237        y = np.array([4, 5, 6, 5, 4, 5], 'd')
1238        dy = np.array([0.2, 0.3, 0.1, 0.2, 0.9, 0.3])
1239    else:
1240        x = np.linspace(0, 1., 10000)
1241        y = np.sin(2 * np.pi * x * 2.8)
1242        dy = np.sqrt(100 * np.abs(y)) / 100
1243    data = Data1D(x, y, dy=dy)
1244    data.xaxis('distance', 'm')
1245    data.yaxis('time', 's')
1246    graph = Graph()
1247    graph.title('Walking Results')
1248    graph.add(data)
1249    graph.add(Theory1D(x, y, dy=dy))
1250    return graph
1251
1252
1253def demo_plotter(graph):
1254    import wx
1255    from pylab_plottables import Plotter
1256    # from mplplotter import Plotter
1257
1258    # Make a frame to show it
1259    app = wx.PySimpleApp()
1260    frame = wx.Frame(None, -1, 'Plottables')
1261    plotter = Plotter(frame)
1262    frame.Show()
1263
1264    # render the graph to the pylab plotter
1265    graph.render(plotter)
1266
1267    class GraphUpdate(object):
1268        callnum = 0
1269
1270        def __init__(self, graph, plotter):
1271            self.graph, self.plotter = graph, plotter
1272
1273        def __call__(self):
1274            if self.graph.changed():
1275                self.graph.render(self.plotter)
1276                return True
1277            return False
1278
1279        def onIdle(self, event):
1280            self.callnum = self.callnum + 1
1281            if self.__call__():
1282                pass  # event.RequestMore()
1283    update = GraphUpdate(graph, plotter)
1284    frame.Bind(wx.EVT_IDLE, update.onIdle)
1285    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.