myFig.py 5.71 KB
Newer Older
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
1
2
3
4
5
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
6
#   myplotlib v1.0.1,
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
#   @file      myFig.py
#   @author    Yori 'AGy' Fournier
#   @licence   CC-BY-SA
#
#   MyFig class: Overlay of matplotlib Figure class
#   It is done such that the user can concentrate on
#   the important aspect of the figure and not the
#   technical part.
#
#   @Class MyFig
#
#   @Constructor
#
#                fignum:      number of the Figure.
22
#                inputarg:    identifier of the raw data in G_RAWDATA.
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
23
24
25
26
27
28
29
30
#                reformat:    flag for reComputing plotted data
#                             from rawData.
#                debug:       flag for debugging, more verbatile.
#                (kw)args:    user defined arguments and keywords.
#
#   @section Functions
#
#            - printDebug:    print some debug information.
31
#
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
32
33
34
35
36
37
38
#            - plot:          overlay of Figure.plot() function,
#                             should be overwrited by the user.
#                             Consists of
#                             - creating axes,
#                             - formating the data,
#                             - adding the axis to the figure
#                             - plotting the axis
39
40
41
#
#            - update:        update the parameters in the
#                             keywords dictionary.
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
42
43
44
#
#   @section History
#
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#   v 0.1.1 - MyFig class for the myplotlib module.
#
#   v 1.0.0 - Changed rawdata with inputarg to allows any type
#             of rawdata, can be an array.
#
#             replace the attribute format (reserved word) with
#             formatted.
#
#   v 1.0.1 - Suppress the explicit definitions of the keywords.
#             by the introduction of the keywords dictionary.
#             All parameters in this dictionary are automatically
#             updated.
#
#             suppress the default value of fignum and hardcode it
#             to be -1.
#
#             add the attribute boundedToWin required for the
#             interactive mode of myplotlib.
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
63
64
65
66
67
#
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
68
69
70
71
from . import D_FIGSIZE, D_INPUTARG, D_DEBUG, D_REFORMAT, D_FORMATTED
from . import DBUG, SEVR, INFO, SPCE, WARN
from . import G_RAWDATAS
from . import Figure, MyAxes
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
72
73


74
# Class MyFig Overwriting Matplotlib.figure.Figure
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
75
76
77
78
79
80
81
class MyFig(Figure):
  
    # Set the size of the Figure in inch
    # (private variable can not be updated)
    FIGSIZE = D_FIGSIZE
  
    # CONSTRUCTOR --------------------------------------------------------
82
83
    def __init__(self, *args, **kwargs):
        
84
        self.keywords = {'fignum': -1,  # need to be hard coded for interactive mode
85
86
87
                         'inputarg': D_INPUTARG,
                         'reformat': D_REFORMAT,
                         'debug': D_DEBUG,
88
                         'formatted': D_FORMATTED}
89
90
91
        
        # check keywords
        for keyword in self.keywords.keys():
92
            # if already in kwargs
93
94
95
96
97
98
            if keyword in kwargs.keys():
                # overwrite default value
                self.keywords[keyword] = kwargs[keyword]
                # suppress keyword from kwargs for Figure.__init__
                del kwargs[keyword]
            
99
        # add figsize in the kwargs for Figure.__init__
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
100
        kwargs['figsize'] = self.FIGSIZE
101
        
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
102
103
        # parent constructor
        Figure.__init__(self, *args, **kwargs)
104
        
105
106
107
        # This is required for the interactive mode
        # it ensures that a figure can not be bounded
        # to several windows and prevent the loss of canvas.
108
109
        self.boundedToWin = False
    
110
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
111
112
113
114
    # DEBUG --------------------------------------------------------------
    def printDebug(self):
        className = str(self.__class__.__name__)
        print('\n' + DBUG + "  {0} PARAMETERS: ".format(className))
115
116
        print(SPCE + "          Raw data: " + str(self.keywords['inputarg']))
        print(SPCE + "     ID the figure: " + str(self.keywords['fignum']))
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
117
        print(SPCE + "Size of the figure: " + str(self.FIGSIZE) + ' [inch] \n')
118
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
119
120
121
122
    # PLOT ---------------------------------------------------------------
    def plot(self):
        
        try:
123
            self.rawdata = G_RAWDATAS[self.keywords['inputarg']]
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
124
        except KeyError:
125
            print(SEVR + "The dataset {dataName} does not exist. --> FALSE".format(dataName=self.keywords['inputarg']))
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
126
127
128
129
130
131
132
133
134
            return(False)
        
        ratio = 6. / 8.  # height/width of the axes (in inch)
        frame = [0.0, 0.0, 1.0, 1.0]  # part of the fig that is available
        
        # create an axes MyAxes with a given rect
        ax = MyAxes(self, ratio, frame)
        
        # reformat the data if needed (default True)
135
        if((self.keywords['reformat']) or (not self.keywords['formatted'])):
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
136
137
            try:
                status = ax.formatRawData(self.rawdata)
138
                self.keywords['formatted'] = True
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
            except (TypeError, KeyError):
                print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
                return(False)
        
        if(not status): return(False)
        
        # add the axis to the figure
        self.add_axes(ax)
        
        # try to plot the Axes
        status = ax.plotting()
        if(not status): return(False)
        
        return(True)
   
    # UPDATE -------------------------------------------------------------
155
    def update(self, **kwargs):
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
156
     
157
158
159
160
161
        # check keywords
        for keyword in self.keywords.keys():
            # if already in kwargs overwrite default value
            if keyword in kwargs.keys():
                self.keywords[keyword] = kwargs[keyword]