Commit 45c165f8 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

I now separates between MyFig and MyFig_base. MyFig_base can be used by...

I now separates between MyFig and MyFig_base. MyFig_base can be used by overwritting the __init__ function and the plot function, very much like in v1.0.0, you just don't need update, remark the axes should be created in __init__ not in plot. The MyFig class however is smarter, and the user just need to overwrite defineKeywords and addAxes, yes, you don't need __init__! as their names suggests in defineKeywords you should update self.keywords, in addAxes you should add the objects MyAxes. Now the figure object reads a tuple of data object as argument (not optional anymore) each index for the axes. This behaviour can be overwritten in the function MyFig::formatRawdata, and MyFig::set_rawdata. Remark now the MyFig_base keywords became attributes: self.keywords['fignum'] -> self.fignum...
parent e5524e1d
......@@ -64,7 +64,7 @@ SPCE = " > -----: "
# myFig
D_FIGNUM = 0 # default figure number
D_FIGSIZE = (8., 6.) # default figure size
D_REFORMAT = True # default reformat value
D_REFORMAT = False # default reformat value
D_FORMATTED = False # default formatted value
D_RAWDATA = MyData() # default raw data
D_INPUTARG = 'current' # default Input argument
......@@ -101,7 +101,7 @@ rc('figure', dpi = 75.0)
from .myAxes import MyAxes
# MyFig: Overlay on matplotlib.Figure class
from .myFig import MyFig
from .myFig import MyFig_base, MyFig
# MyFig: Overlay on matplotlib.FigureManager class
if rcParams['backend'] == u'TkAgg':
......
......@@ -74,30 +74,34 @@ from . import Figure
# Class MyFig Overwriting Matplotlib.figure.Figure
class MyFig(Figure):
class MyFig_base(Figure):
# Set the size of the Figure in inch
# (private variable can not be updated)
FIGSIZE = D_FIGSIZE
# CONSTRUCTOR --------------------------------------------------------
def __init__(self, *args, **kwargs):
self.keywords = {'fignum': -1, # need to be hard coded for interactive mode
'inputarg': D_INPUTARG,
'reformat': D_REFORMAT,
'debug': D_DEBUG,
'formatted': D_FORMATTED}
# check keywords
for keyword in self.keywords.keys():
# if already in kwargs
if keyword in kwargs.keys():
def __init__(self, rawdata, *args, **kwargs):
attributes = {'fignum': -1, # need to be hard coded for interactive mode
'reformat': D_REFORMAT,
'debug': D_DEBUG,
'formatted': D_FORMATTED}
# check attributes in keywords
for attribute in attributes.keys():
# if in kwargs
if attribute in kwargs.keys():
# overwrite default value
self.keywords[keyword] = kwargs[keyword]
setattr(self, attribute, kwargs[attribute])
# suppress keyword from kwargs for Figure.__init__
del kwargs[keyword]
del kwargs[attribute]
else:
setattr(self, attribute, attributes[attribute])
self.keywords = {}
self.rawdata = rawdata
# add figsize in the kwargs for Figure.__init__
kwargs['figsize'] = self.FIGSIZE
......@@ -114,50 +118,36 @@ class MyFig(Figure):
def printDebug(self):
className = str(self.__class__.__name__)
print('\n' + DBUG + " {0} PARAMETERS: ".format(className))
print(SPCE + " Raw data: " + str(self.keywords['inputarg']))
print(SPCE + " ID the figure: " + str(self.keywords['fignum']))
print(SPCE + " Raw data: " + str(self.rawdata))
print(SPCE + " ID the figure: " + str(self.fignum))
print(SPCE + "Size of the figure: " + str(self.FIGSIZE) + ' [inch] \n')
# PLOT ---------------------------------------------------------------
def plot(self):
pass
# UPDATE -------------------------------------------------------------
def update(self, **kwargs):
try:
self.rawdata = G_RAWDATAS[self.keywords['inputarg']]
except KeyError:
print(SEVR + "The dataset {dataName} does not exist. --> FALSE".format(dataName=self.keywords['inputarg']))
return(False)
attributes = ('fignum', 'reformat', 'debug', 'formatted')
# For all axes in the figure reformat if needed and plot
for ax in self.get_axes():
# check attributes in keywords
for keyword in kwargs.keys():
# reformat the data if needed (default True)
if((self.keywords['reformat']) or (not self.keywords['formatted'])):
try:
status = ax.formatRawData(self.rawdata)
self.keywords['formatted'] = True
except (TypeError, KeyError):
print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
return(False)
# if it is an attribute
if keyword in attributes:
# update value
setattr(self, keyword, kwargs[keyword])
# clean
ax.cla()
# plot
status = ax.plotting()
if(not status): return(False)
return(True)
# UPDATE -------------------------------------------------------------
def update(self, **kwargs):
# check keywords
for keyword in self.keywords.keys():
# if already in kwargs overwrite default value
if keyword in kwargs.keys():
# if it is a user keyword
if keyword in self.keywords.keys():
# update
self.keywords[keyword] = kwargs[keyword]
# if it is the rawdata use the function
if keyword == 'rawdata':
self.set_rawdata(kwargs[keyword])
# PRINT 2 FILE -------------------------------------------------------
......@@ -186,7 +176,7 @@ class MyFig(Figure):
canvas = FigureCanvas(self) # the canvas of the figure changed
# Plot the figure
# Plot the figure
self.plot()
# draw the figure on the new canvas
......@@ -234,3 +224,94 @@ class MyFig(Figure):
self.canvas = winCanvas
return(True)
# CLASS MyFig ==========================================================
class MyFig(MyFig_base):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, rawdata, *args, **kwargs):
MyFig_base.__init__(self, rawdata, *args, **kwargs)
self.initialize(*args, **kwargs)
# INITIALIZE -------------------------------------------------------
def initialize(self, *args, **kwargs):
# sefine user keywords
self.defineKeywords()
# add the axes
self.addAxes(*args, **kwargs)
# set the rawdata through the function
self.set_rawdata(self.rawdata) # This function can be overwriten
# DEFINE KEYWORDS --------------------------------------------------
def defineKeywords(self):
pass
# ADD AXES ---------------------------------------------------------
def addAxes(self, *arg, **kwargs):
pass
# SET RAW DATA -----------------------------------------------------
def set_rawdata(self, rawdata):
if(type(rawdata) != tuple):
print(SEVR + "rawdata should be a tuple. i.e. (data1,)")
return(False)
if(len(self.get_axes()) == len(rawdata)):
self.rawdata = rawdata
self.formatRawData()
else:
self.rawdata = ()
print(SEVR + "rawdata should have the dimention of the number of axes: #axes = " + str(len(self.get_axes())) + "; dim of rawdata = " + str(len(rawdata)))
return(False)
return(True)
# FORMAT RAW DATA --------------------------------------------------
def formatRawData(self):
for ax, rawdata in zip(self.get_axes(), self.rawdata):
try:
ax.formatRawData(rawdata)
except:
print(SEVR + "The " + str(ax.__class__.__name__) + " with index " + str(self.get_axes().index(ax)) + " could not format the rawdata.")
return(False)
if self.formatted is False:
self.formatted = True
return(True)
# PLOT -------------------------------------------------------------
def plot(self):
status = True
if((self.reformat) or (not self.formatted)):
status = self.formatRawData()
# For all axes in the figure reformat if needed and plot
for ax in self.get_axes():
# clean the axes
ax.cla()
# plot
plottingStatus = ax.plotting()
status = status and plottingStatus
return(status)
# RESET ------------------------------------------------------------
def reset(self):
self.clf()
self.initialize()
self.plot()
from myIOs import readStupidData, readStupidData2
from axTest1 import AxTest1
from figTest1 import FigTest1
from figTest import FigTest1, FigTest2
from tests import myTest
......
......@@ -82,8 +82,7 @@ class AxTest1(MyAxes):
print(SEVR + 'The Raw Data could not be formatted --> EXIT')
return(False)
if(self.fig.keywords['debug']):
print(DBUG + 'I formatted the raw data!\n')
print(DBUG + 'formatted data:\n' + str(self.fig.keywords['formatted']))
if(self.fig.debug):
print(DBUG + 'I formatted the raw data!')
return(True)
......@@ -67,7 +67,7 @@
# IMPORT ---------------------------------------------------------------
from .. import os
from .. import DBUG, SEVR, INFO, SPCE, WARN
from .. import MyFig
from .. import MyFig, MyFig_base
from .axTest1 import AxTest1
......@@ -76,20 +76,42 @@ D_YRANGE = [-2, 2]
# Class MyFig Overwriting Matplotlib.figure.Figure
class FigTest1(MyFig):
class FigTest1(MyFig_base):
# Set the size of the Figure in inch
# (private variable can not be updated)
FIGSIZE = (8., 6.)
# CONSTRUCTOR --------------------------------------------------------
def __init__(self, *args, **kwargs):
def __init__(self, rawdata, *args, **kwargs):
# parent constructor
MyFig.__init__(self, *args, **kwargs)
MyFig.__init__(self, rawdata, *args, **kwargs)
self.keywords.update({'xRange': D_XRANGE,
'yRange': D_YRANGE})
self.keywords = {'xRange': D_XRANGE,
'yRange': D_YRANGE}
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
self.add_axes(AxTest1(self, ratio, frame, *args, **kwargs))
# Class MyFig Overwriting Matplotlib.figure.Figure
class FigTest2(MyFig):
# Set the size of the Figure in inch
# (private variable can not be updated)
FIGSIZE = (8., 6.)
def defineKeywords(self):
self.keywords = {'xRange': D_XRANGE,
'yRange': D_YRANGE}
return(True)
def addAxes(self, *args, **kwargs):
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
......
......@@ -50,7 +50,7 @@ def readStupidData(ipath=D_IPATH, *args, **kwargs):
print(DBUG + 'I read the raw data with readStupidData')
print(DBUG + str(G_RAWDATAS['current'].name))
return(lrawdata.name)
return(lrawdata)
# READ STUPID DATA 2 ---------------------------------------------------
......@@ -68,5 +68,5 @@ def readStupidData2(ipath=D_IPATH, *args, **kwargs):
print(DBUG + 'I read the raw data with readStupidData2')
print(DBUG + str(G_RAWDATAS['current'].name))
return(lrawdata.name)
return(lrawdata)
from .. import os
from .. import D_OFORMAT, D_OPATH
from .. import MyFig, MyWin
from .. import MyWin
from .. import SEVR, WARN, DBUG, SPCE
from .. import _G_WINDOWS
from . import readStupidData, readStupidData2
from . import FigTest2
# TESTER
def myTest(function, expected, failed, debug):
......@@ -44,11 +45,12 @@ def test400(debug):
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
win = MyWin(fig)
try:
if win.canvas.figure.keywords['debug']:
if win.canvas.figure.debug:
pass
status = True
......@@ -64,18 +66,18 @@ def test400(debug):
return(status)
# TEST 300: test the update of inputarg (EXPECTED: TRUE)
# TEST 300: test the update of rawdata (EXPECTED: TRUE)
def test300(debug):
status = True
data1 = readStupidData('data1')
data2 = readStupidData2('data2')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
fig.update(inputarg=data2)
fig.update(rawdata=(data2,))
if(fig.keywords['inputarg'] != data2):
if(fig.rawdata != (data2,)):
status = False
del fig
......@@ -95,7 +97,7 @@ def test301(debug):
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
fig.print2file('test301.png', opath='myplotlib/img/') # create img/test301.png
if not os.path.exists("./myplotlib/img/test301.png"):
......@@ -117,7 +119,7 @@ def test302(debug):
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test302.eps")
......@@ -140,7 +142,7 @@ def test303(debug):
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if debug:
print(DBUG + "Try to print MyFig with data1 in a non existing file.")
......@@ -163,7 +165,7 @@ def test304(debug):
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test304.eps")
......@@ -189,7 +191,7 @@ def test305(debug):
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test305.png")
......@@ -215,7 +217,7 @@ def test306(debug):
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test306.png")
......@@ -236,7 +238,7 @@ def test401(debug):
# create figure
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if debug:
print(DBUG + "An object MyFig with data1 has been created.")
......@@ -267,19 +269,19 @@ def test402(debug):
data1 = readStupidData('data1')
data2 = readStupidData2('data2')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if(debug): print(DBUG + "Created a MyFig object with data1.")
win = MyWin(fig)
if(debug): print(DBUG + "Created window num=0 for fig.")
fig.update(inputarg=data2)
fig.update(rawdata=(data2,))
if(debug): print(DBUG + "Updated fig with data2")
win.refresh()
if(debug): print(DBUG + "Refreshed the window with actualized fig.")
if(win.canvas.figure.keywords['inputarg'] != data2):
if(win.canvas.figure.rawdata != (data2,)):
status = False
win.close()
......@@ -300,7 +302,7 @@ def test403(debug):
data1 = readStupidData('data1')
data2 = readStupidData2('data2')
fig = MyFig(inputarg=data1)
fig = FigTest2((data1,))
if(debug):
print(DBUG + "Put a MyFig into a MyWin with data1")
......@@ -311,8 +313,8 @@ def test403(debug):
fig.print2file('dummyTestFile.png', opath='myplotlib/img/')
if(debug):
print(SPCE + "Update inputarg to data2")
fig.update(inputarg=data2)
print(SPCE + "Update rawdata to data2")
fig.update(rawdata=(data2,))
if(debug):
print(SPCE + "Now refresh the window.")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment