Commit 528fd2ee authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

add print2file in MyFig class, tested it (test302, 303, 304, 305), and verfy...

add print2file in MyFig class, tested it (test302, 303, 304, 305), and verfy that it works in combinasion with MyWin see test403. modified print2file in myTool (untested) and only TkAgg has been tested but WXAgg and GTKAgg are also implemented.
parent 4e69af9d
......@@ -128,6 +128,6 @@ from .myFig4Test import MyFig4Test
from .myPlot4Test import somePlottingRoutine
from .test import myTest
from .test import test400, test300, test401, test402
from .test import test400, test300, test301, test302, test303, test304, test305, test401, test402, test403
testList = [test400, test300, test401, test402]
testList = [test400, test300, test301, test302, test303, test304, test305, test401, test402, test403]
......@@ -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
......
......@@ -66,6 +66,7 @@
#
# IMPORT ---------------------------------------------------------------
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 +160,62 @@ 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):
oformat = kwargs.get('oformat', D_OFORMAT)
opath = kwargs.get('opath', D_OPATH)
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 unbound the fig so that the window
# knows that it should rebound by refreshing.
# Indeed the canvas of the fig will change.
self.boundedToWin = False
self.keywords['fignum'] = -1
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]
# 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)
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)
return(True)
......@@ -118,14 +118,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 +133,15 @@ def closeAllWindows():
# PRINT TO FILE --------------------------------------------------------
def print2file(ClassName, inputArg, filename,
opath=D_OPATH, oformat=D_OFORMAT,
*args, **kwargs):
# fignum = kwargs.get('fignum',0)
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
def print2file(ClassName, inputArg, filename, *args, **kwargs):
# 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,17 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
# REFRESH ----------------------------------------------------------
def refresh(self):
# in the case the figure got another canvas (i.e. print2file)
# rebound the figure.
if self.canvas != self.canvas.figure.canvas:
self.set_figure(self.canvas.figure)
# refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
return(True)
# DRAW -------------------------------------------------------------
......
......@@ -175,9 +175,17 @@ class MyWin_TkAgg(FigureManagerTkAgg):
# REFRESH ----------------------------------------------------------
def refresh(self):
# in the case the figure got another canvas (i.e. print2file)
# rebound the figure.
if self.canvas != self.canvas.figure.canvas:
self.set_figure(self.canvas.figure)
# refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
return(True)
# DRAW -------------------------------------------------------------
......
......@@ -180,11 +180,19 @@ class MyWin_WXAgg(FigureManagerWx):
# REFRESH ----------------------------------------------------------
def refresh(self):
# in the case the figure got another canvas (i.e. print2file)
# rebound the figure.
if self.canvas != self.canvas.figure.canvas:
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
from . import SEVR, WARN, DBUG, SPCE
from . import _G_WINDOWS
......@@ -33,6 +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 4.00: test create of MyFig and PRINT TO SCREEN (EXPECTED: TRUE)
......@@ -82,7 +85,127 @@ def test300(debug):
return(status)
# TEST 401: Verify the well behaving of closeWindow (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)
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 401: Verify the well behaving of MyWin::close (EXPECTED: TRUE)
def test401(debug):
# create figure
......@@ -110,7 +233,7 @@ def test401(debug):
return(status)
# TEST 402: Test refreshWindow (EXPECTED: True)
# TEST 402: Test MyWin::refresh (EXPECTED: True)
def test402(debug):
status = True
......@@ -141,3 +264,40 @@ def test402(debug):
del data2
return(status)
# TEST 305: 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)
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