Commit e1ff7d08 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

merge dev to add-tests so that I can use the new print2file methods in the tests

parents 904e694c b8100ee8
......@@ -118,7 +118,7 @@ else:
# myTool.*: interface functions to use myplotlib interactively
from .myTool import print2file, print2screen, printListCurrentWindows
from .myTool import getWindow, getFigOnWindow, drawFigOnWindow
from .myTool import getWindow, getFigOnWindow, drawFigOnWindow, giveDataToWindow
from .myTool import closeWindow, closeAllWindows
# MyFig4Test: Overlay of Figure for testing
......@@ -127,15 +127,21 @@ from .myFig4Test import MyFig4Test
# some plotting routine for testing
from .myPlot4Test import somePlottingRoutine
# Import the testing interface
from .test import myTest
# import the tests
from .test import test100
from .test import test200
from .test import test300
from .test import test400, test401, test402
from .test import test300, test301, test302, test303, test304, test305, test306
from .test import test400, test401, test402, test403
from .test import test600
# the tests to run
testList = [test100,
test200,
test300,
test400,
test401,
test402]
test300, test301, test302, test303, test304, test305,
test306,
test400, test401, test402,
test600,
]
......@@ -94,8 +94,13 @@ if run4Test:
print
print
print(INFO + '{title:{c}^{n}}'.format(title=' ' + str(test.__name__) + ' ', c='=', n=72))
status = myTest(test, True, fail, debug) # function, expected, fail, debug
if status: print(INFO + 72 * '-' + " [PASSED]")
testStatus = myTest(test, True, fail, debug) # function, expected, fail, debug
if testStatus:
print(INFO + 72 * '-' + " [PASSED]")
else:
print(INFO + 72 * '-' + " [FAILED] !")
status = status and testStatus
# Print final status
print
......
......@@ -65,7 +65,9 @@
#
#
# IMPORT ---------------------------------------------------------------
from . import os
from . import D_FIGSIZE, D_INPUTARG, D_DEBUG, D_REFORMAT, D_FORMATTED
from . import D_OFORMAT, D_OPATH
from . import DBUG, SEVR, INFO, SPCE, WARN
from . import G_RAWDATAS
from . import Figure, MyAxes
......@@ -159,3 +161,79 @@ class MyFig(Figure):
# if already in kwargs overwrite default value
if keyword in kwargs.keys():
self.keywords[keyword] = kwargs[keyword]
# PRINT 2 FILE -------------------------------------------------------
def print2file(self, filename, *args, **kwargs):
# get the keywords
debug = kwargs.get('debug', D_DEBUG)
oformat = kwargs.get('oformat', D_OFORMAT)
opath = kwargs.get('opath', D_OPATH)
# set the dpi
if(oformat == 'png'):
dpi = 300.
addMetaData = True # NOT YET IMPLEENTED
else:
dpi = 100.
addMetaData = False # NOT YET IMPLEMENTED
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
# if the figure is boundedToWin
# save to canvas in a local variable and restore after saving to file.
if self.boundedToWin:
winCanvas = self.canvas
else:
winCanvas = None
canvas = FigureCanvas(self) # the canvas of the figure changed
# Plot the figure
self.plot()
# draw the figure on the new canvas
canvas.draw() # not really needed (savefig does that)
# check if the extention is in the name
if '.png' in str(filename):
oformat = 'png'
filename = filename[:-4]
elif '.eps' in str(filename):
oformat = 'eps'
filename = filename[:-4]
# check if the path is in the name
if '/' in str(filename):
if(debug):
print(WARN + "I detect that the path is in the name of the file, will ignore D_OPATH.")
arg = filename
filename = os.path.basename(arg)
opath = os.path.dirname(arg)
# take care of the . in the extention
if oformat[0] == '.':
oformat = oformat[1:]
# clean the output path
if str(opath)[-1] == '/':
opath = str(opath)[:-1]
else:
opath = str(opath)
# print into a file
try:
self.savefig(str(opath) + '/' + str(filename) + '.' + str(oformat), dpi=dpi) # ! this not the save from plt but from fig
except IOError:
print(SEVR + 'the path to the file does not exists:')
print(SPCE + str(opath) + '/' + str(filename) + '.' + str(oformat) + ' --> EXIT')
return(False)
except:
print(SEVR + 'Can not save file. sorry --> EXIT')
return(False)
# restore the former canvas
if winCanvas is not None:
self.canvas = winCanvas
return(True)
......@@ -110,6 +110,16 @@ def drawFigOnWindow(fig, ID):
return(MyWin(fig, fignum=ID))
def giveDataToWindow(data, ID):
fig = getFigOnWindow(ID)
fig.update(inputarg = data)
win = getWindow(ID)
win.refresh()
return(True)
def closeWindow(ID):
for window in _G_WINDOWS:
......@@ -118,14 +128,14 @@ def closeWindow(ID):
def closeAllWindows():
# a for-loop won't work because close() acts on _G_WINDOWS
# for window in _G_WINDOWS:
while(len(_G_WINDOWS) > 0):
window = _G_WINDOWS[0]
print(INFO + "window :" + str(window.num))
window.close()
if(len(_G_WINDOWS) == 0):
return(True)
else:
......@@ -133,62 +143,15 @@ def closeAllWindows():
# PRINT TO FILE --------------------------------------------------------
def print2file(ClassName, inputArg, filename,
opath=D_OPATH, oformat=D_OFORMAT,
*args, **kwargs):
# fignum = kwargs.get('fignum',0)
def print2file(ClassName, inputArg, filename, *args, **kwargs):
if(oformat == 'png'):
dpi = 300.
else:
dpi = 100.
# interactive on
ioff()
# import the Agg Backend Canvas for printing to file and bypassing pyplot
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
# create the figure of type ClassName
# bypassing myfig to avoid figure from pyplot
# fig = myfig(ClassName, inputarg=inputArg, dpi=dpi, *args, **kwargs )
kwargs.update({'dpi': dpi, 'inputarg': inputArg})
fig = ClassName(*args, **kwargs)
canvas = FigureCanvas(fig)
fig.plot()
# canvas.draw() # NOT NEEDED (save does it)
if '.png' in str(filename):
oformat = 'png'
filename = filename[:-4]
elif '.eps' in str(filename):
oformat = 'eps'
filename = filename[:-4]
if oformat[0] == '.':
oformat = oformat[1:]
if str(opath)[-1] == '/':
opath = str(opath)[:-1]
else:
opath = str(opath)
try:
fig.savefig(str(opath) + '/' + str(filename) + '.' + str(oformat), dpi=dpi) # ! this not the save from plt but from fig
except IOError:
print(SEVR + 'the path to the file does not exists:')
print(SPCE + str(opath) + '/' + str(filename) + '.' + str(oformat) + ' --> EXIT')
return(False)
fig = ClassName(inputarg=inputArg, *args, **kwargs)
except:
print(SEVR + 'Can not save file. sorry --> EXIT')
print(SEVR + 'The type of figure you gave does not exists.')
return(False)
# clean behind
fig.clf()
close(fig)
del(canvas)
del(fig)
fig.print2file(filename, *args, **kwargs)
# end
return(True)
......@@ -166,9 +166,18 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
# REFRESH ----------------------------------------------------------
def refresh(self):
# in the case the figure got another canvas
# rebound the figure. This situation should not happend!!
if self.canvas != self.canvas.figure.canvas:
print(WARN + "Something weird happend, the canvas of the figure have been changed")
self.set_figure(self.canvas.figure)
# refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
return(True)
# DRAW -------------------------------------------------------------
......
......@@ -175,9 +175,18 @@ class MyWin_TkAgg(FigureManagerTkAgg):
# REFRESH ----------------------------------------------------------
def refresh(self):
# in the case the figure got another canvas
# rebound the figure. This situation should not happend!!
if self.canvas != self.canvas.figure.canvas:
print(WARN + "Something weird happend, the canvas of the figure have been changed")
self.set_figure(self.canvas.figure)
# refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
return(True)
# DRAW -------------------------------------------------------------
......
......@@ -180,11 +180,20 @@ class MyWin_WXAgg(FigureManagerWx):
# REFRESH ----------------------------------------------------------
def refresh(self):
# in the case the figure got another canvas
# rebound the figure. This situation should not happend!!
if self.canvas != self.canvas.figure.canvas:
print(WARN + "Something weird happend, the canvas of the figure have been changed")
self.set_figure(self.canvas.figure)
#refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
self.canvas.figure.plot()
self.canvas.draw()
return(True)
# DRAW -------------------------------------------------------------
......
from . import os
from . import D_OFORMAT, D_OPATH
from . import readStupidData, readStupidData2
from . import MyFig, MyWin
from . import SEVR, WARN, DBUG, SPCE
......@@ -33,7 +35,7 @@ def myTest(function, expected, failed, debug):
# TEST 2.* for testing MyAxes
# TEST 3.* for testing MyFig
# TEST 4.* for testing MyWin
# TEST 5.* for testing myTool
# TEST 1.00: test the Data Structure (EXPECTED: TRUE)
def test100(debug):
......@@ -170,7 +172,157 @@ def test300(debug):
return(status)
# TEST 4.00: test create of MyFig and PRINT TO SCREEN (EXPECTED: TRUE)
# TEST 301: Test MyFig::print2file (EXPECTED: True)
def test301(debug):
if os.path.exists("./myplotlib/img/test301.png"):
os.remove("./myplotlib/img/test301.png")
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test301.png")
fig.print2file('test301.png', opath='myplotlib/img/') # create img/test301.png
if not os.path.exists("./myplotlib/img/test301.png"):
status = False
del fig
del data1
return(status)
# TEST 302: Test MyFig::print2file (EXPECTED: True)
def test302(debug):
if os.path.exists("./myplotlib/img/test302.eps"):
os.remove("./myplotlib/img/test302.eps")
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test302.eps")
fig.print2file('test302.eps', opath='myplotlib/img/') # create img/test302.eps
if not os.path.exists("./myplotlib/img/test302.eps"):
status = False
del fig
del data1
return(status)
# TEST 303: Test MyFig::print2file (EXPECTED: True)
def test303(debug):
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
if debug:
print(DBUG + "Try to print MyFig with data1 in a non existing file.")
status = fig.print2file('test303', opath='somePathThatDoesNotExists/NoWhere') # should return False
del fig
del data1
return(not status)
# TEST 304: Test MyFig::print2file (EXPECTED: True)
def test304(debug):
if os.path.exists("./myplotlib/img/test304.eps"):
os.remove("./myplotlib/img/test304.eps")
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test304.eps")
fig.print2file('test304', oformat='.eps', opath='myplotlib/img/') # create img/test304.eps
if not os.path.exists("./myplotlib/img/test304.eps"):
status = False
del fig
del data1
return(status)
# TEST 305: Test MyFig::print2file (EXPECTED: True)
def test305(debug):
if os.path.exists("./myplotlib/img/test305.png"):
os.remove("./myplotlib/img/test305.png")
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test305.png")
fig.print2file('test305', oformat='png', opath='myplotlib/img/') # create img/test305.png
if not os.path.exists("./myplotlib/img/test305.png"):
status = False
del fig
del data1
return(status)
# TEST 306: Test MyFig::print2file (EXPECTED: True)
def test306(debug):
if os.path.exists("./myplotlib/img/test306.png"):
os.remove("./myplotlib/img/test306.png")
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
if debug:
print(DBUG + "Try to print MyFig with data1 in myplotlib/img/test306.png")
fig.print2file('myplotlib/img/test306.png') # create img/test306.png
if not os.path.exists("./myplotlib/img/test306.png"):
status = False
del fig
del data1
return(status)
# TEST 4.00: test MyWin (EXPECTED: TRUE)
def test400(debug):
status = True
......@@ -196,7 +348,7 @@ def test400(debug):
return(status)
# TEST 401: Verify the well behaving of closeWindow (EXPECTED: TRUE)
# TEST 401: Verify the well behaving of MyWin::close (EXPECTED: TRUE)
def test401(debug):
# create figure
......@@ -224,7 +376,7 @@ def test401(debug):
return(status)
# TEST 402: Test refreshWindow (EXPECTED: True)
# TEST 402: Test MyWin::refresh (EXPECTED: True)
def test402(debug):
status = True
......@@ -257,6 +409,43 @@ def test402(debug):
return(status)
# TEST 403: Test MyFig::print2file And MyWin::refresh (EXPECTED: True)
def test403(debug):
status = True
data1 = readStupidData('data1')
data2 = readStupidData2('data2')
fig = MyFig(inputarg=data1)
if(debug):
print(DBUG + "Put a MyFig into a MyWin with data1")
win = MyWin(fig)
if(debug):
print(SPCE + "Now print the fig into dummyTestFile.png")
fig.print2file('dummyTestFile.png', opath='myplotlib/img/')
if(debug):
print(SPCE + "Update inputarg to data2")
fig.update(inputarg=data2)
if(debug):
print(SPCE + "Now refresh the window.")
# refresh the window (This should return True)
status = win.refresh()
fig.print2file('dummyTestFile2.png', opath='myplotlib/img/')
win.close()
del fig
del data1
del data2
return(status)
# TEST 600: Test Adding Axes on the Fly (EXPECTED: True)
def test600(debug):
......@@ -273,6 +462,6 @@ def test600(debug):
win.canvas.draw()
status = fig.print2file('myplotlib/img/test600.png') # Not Yet Implemented
status = fig.print2file('myplotlib/img/test600.png')
return(status)
Supports Markdown
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