myFig.py 5.06 KB
Newer Older
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
#   myplotlib v0.0.0,
#
#   @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.
#                rawdata:     identifier of the raw data in G_RAWDATA.
#                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.
# 
#            - 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
#                             
#            - update:        This is done to update the figure
#                             in case of redraw to make sure that
#                             the attributes are up-to-date.
#                             Important for interactive work.
#
#   @section History
#
47
#   v 0.0.2 - MyFig class for the myplotlib module.
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
48
49
50
51
52
#
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
53
from myplotlib import D_FIGSIZE, D_INPUTARG, D_DEBUG, D_REFORMAT, D_FIGNUM, D_FORMATTED
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from myplotlib import DBUG, SEVR, INFO, SPCE, WARN

from myplotlib import G_RAWDATAS
from myplotlib import Figure, MyAxes


# Class MyFig Overwriting Matplotlib.fure.Figure
class MyFig(Figure):
  
    # Set the size of the Figure in inch
    # (private variable can not be updated)
    FIGSIZE = D_FIGSIZE
  
    # CONSTRUCTOR --------------------------------------------------------
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    def __init__(self, *args, **kwargs):
        
        self.keywords = {'fignum': D_FIGNUM,
                         'inputarg': D_INPUTARG,
                         'reformat': D_REFORMAT,
                         'debug': D_DEBUG,
                         'reformatted': D_FORMATTED}
        
        # check keywords
        for keyword in self.keywords.keys():
            # if already in kwargs 
            if keyword in kwargs.keys():
                # overwrite default value
                self.keywords[keyword] = kwargs[keyword]
                # suppress keyword from kwargs for Figure.__init__
                del kwargs[keyword]
            
        # inputarg, reformat, debug, fignum
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
86
        kwargs['figsize'] = self.FIGSIZE
87
        
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
88
89
        # parent constructor
        Figure.__init__(self, *args, **kwargs)
90
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
91
92
93
94
    # DEBUG --------------------------------------------------------------
    def printDebug(self):
        className = str(self.__class__.__name__)
        print('\n' + DBUG + "  {0} PARAMETERS: ".format(className))
95
96
        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
97
        print(SPCE + "Size of the figure: " + str(self.FIGSIZE) + ' [inch] \n')
98
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
99
100
101
102
    # PLOT ---------------------------------------------------------------
    def plot(self):
        
        try:
103
            self.rawdata = G_RAWDATAS[self.keywords['inputarg']]
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
104
        except KeyError:
105
            print(SEVR + "The dataset {dataName} does not exist. --> FALSE".format(dataName=self.keywords['inputarg']))
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
106
107
108
109
110
111
112
113
114
            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)
115
        if((self.keywords['reformat']) or (not self.keywords['formatted'])):
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
116
117
            try:
                status = ax.formatRawData(self.rawdata)
118
                self.keywords['formatted'] = True
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
            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 -------------------------------------------------------------
135
    def update(self, **kwargs):
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
136
     
137
138
139
140
141
        # 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]