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

This version is object oriented, the interactive mode is now ready and seems...

This version is object oriented, the interactive mode is now ready and seems to work. It requires more testing though. In anycase tests have to be added. myTool may also be removed, and another class MyCanvasToPrint may be created instead of the print2file function. MyWin.py should be commented and a header is needed, as well as a correction of the other headers. remarks: the default value of fignum may be removed and set to -1 hard coded (indeed another number may lead to truble in interactive mode)
parent b807b619
......@@ -49,6 +49,7 @@ from matplotlib import rcParams
# For interactive usage (require GTKAgg !) NOT YET COMPATIBLE WITH OTHER BACKENDS
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtkagg import FigureManagerGTKAgg as FigureManager
# myplotlib
from myData import MyData
......@@ -62,7 +63,7 @@ SPCE = " > -----: "
# CONFIGURATION --------------------------------------------------------
# myFig
D_FIGNUM = 0 # default figure number
D_FIGNUM = -1 # default figure number
D_FIGSIZE = (8., 6.) # default figure size
D_REFORMAT = True # default reformat value
D_FORMATTED = False # default formatted value
......@@ -81,7 +82,7 @@ D_DEBUG = True # default debug value
# GLOBAL VARIABLE ------------------------------------------------------
G_RAWDATAS = {'current': MyData()} # raw data Object
G_MANAGERS = [] # LIST OF THE ACTUAL MANAGERS
_G_WINDOWS = []
# PLOTTING CONFIGURATION -----------------------------------------------
rc('font', family='serif')
......@@ -106,8 +107,10 @@ from myIOs import readStupidData, readStupidData2
# MyFig: Overlay on matplotlib.Figure class
from myFig import MyFig
# MyFig: Overlay on matplotlib.FigureManager class
from myWin import MyWin
# myTool.*: interface functions to use myplotlib interactively
from myTool import createWindow, refreshWindow, closeWindow, updateFig
from myTool import print2file
# MyFig4Test: Overlay of Figure for testing
......@@ -117,6 +120,6 @@ from myFig4Test import MyFig4Test
from myPlot4Test import somePlottingRoutine
from test import myTest
from test import test1, test2, test3, test4
from test import test400, test300, test401, test402
testList = [test1, test2, test3, test4]
testList = [test400, test300, test401, test402]
......@@ -42,7 +42,7 @@
#
# IMPORT ---------------------------------------------------------------
from myplotlib import D_OPATH, D_OFORMAT
from myplotlib import G_RAWDATAS, G_MANAGERS
from myplotlib import G_RAWDATAS
from myplotlib import INFO, SEVR, WARN, SPCE
from myplotlib import ion, ioff, is_interactive
......@@ -63,150 +63,6 @@ def setCurrentData(name):
return(True)
def refreshWindow(window):
window.canvas.figure.clf()
window.canvas.figure.plot()
# window.canvas.draw()
window.canvas.figure.canvas.draw()
return(True)
# create windows without testing... be careful using that may lead to memory leakage
def _createWindow_(fig, *args, **kwargs):
# interactive off
if is_interactive():
ioff()
# Check the figure number (same as window number)
fignum = kwargs.get('fignum', 0)
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtkagg import FigureManagerGTKAgg as FigureManager
# ADD Window in the Managers List
print(INFO + "I add the window in G_MANAGERS.")
G_MANAGERS.append(FigureManager(FigureCanvas(fig), fignum))
# G_MANAGERS.append(FigureManager(fig.canvas, fignum))
if fig.dpi != 75:
fig.dpi = 75
fig.boundedToWin = True
fig.keywords['fignum'] = fignum
fig.clf()
fig.plot()
G_MANAGERS[-1].canvas.draw()
G_MANAGERS[-1].show()
win = G_MANAGERS[-1]
return(win)
def updateFig(win, fig):
# unbound the current fig of the window
win.canvas.figure.boundedToWin = False # unbound the figure to the window
win.canvas.figure.keywords['fignum'] = -1 # setup a non existing fignum to avoid problems.
# fig is already bounded close its former frame
if fig.boundedToWin:
closeWindow(fig.keywords['fignum'])
fig.canvas = win.canvas
win.canvas.figure = fig
fig.boundedToWin = True
fig.keywords['fignum'] = win.num
refreshWindow(win)
return(True)
def createWindow(fig, *args, **kwargs):
# interactive off
if is_interactive():
ioff()
if fig.dpi != 75:
fig.dpi = 75
# Check the figure number (same as window number)
fignum = kwargs.get('fignum', 0)
# Assume the window does not yet exists
alreadyExists = False
# Verify in G_MANAGERS if the window exists
for window in G_MANAGERS:
# if it exists just refresh it
if(window.num == fignum):
alreadyExists = True
index = G_MANAGERS.index(window)
# if window exists warn and refresh
if alreadyExists:
win = G_MANAGERS[index]
if(win.canvas.figure == fig):
print(WARN + "The window already exists, I'll just refresh it.")
refreshWindow(win)
else:
print(WARN + "The window already exists, and figure is different, I'll close and recreate it.")
# if fig not yet bounded
if not fig.boundedToWin:
closeWindow(win.num)
win = _createWindow_(fig, *args, **kwargs)
# if fig bounded then close the former frame and switch to new one.
else:
print(WARN + "The figure is already plotted, I'll close and recreate it in the new window.")
# if yes then first close and recreate
print(INFO + str(fig.keywords['fignum']))
closeWindow(fig.keywords['fignum'])
win = _createWindow_(fig, *args, **kwargs)
# if it does not exists
else:
# check if the figure is already bounded to a window
if not fig.boundedToWin:
win = _createWindow_(fig, *args, **kwargs)
else:
print(WARN + "The fihure is already used, I'll close its window and recreate it.")
# if yes then first close and recreate
print(INFO + str(fig.keywords['fignum']))
closeWindow(fig.keywords['fignum'])
win = _createWindow_(fig, *args, **kwargs)
return(win)
def closeWindow(num):
index = None
try:
index = np.where([(win.num == num) for win in G_MANAGERS])[0][0]
status = True
except:
print(WARN + "The window you want to close does not exists.")
status = False
if index is not None:
G_MANAGERS[index].canvas.figure.boundedToWin = False
G_MANAGERS[index].canvas.figure.keywords['fignum'] = -1
G_MANAGERS[index].destroy()
del G_MANAGERS[index]
return(status)
#
# PRINT TO FILE --------------------------------------------------------
def print2file(ClassName, inputArg, filename,
......
from myplotlib import INFO, SEVR, WARN, DBUG, SPCE
from myplotlib import FigureManager, FigureCanvas
from myplotlib import _G_WINDOWS
from myplotlib import np
class MyWin(FigureManager):
def __init__(self, fig, show=True, *args, **kwargs):
if(fig.dpi != 75.):
fig.dpi = 75.
num = kwargs.get('fignum', 0)
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
FigureManager.__init__(self, FigureCanvas(fig), num)
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
_G_WINDOWS.append(self)
if show:
self.refresh()
self.show()
def _boundFigure_(self, fig):
fig.canvas = self.canvas
self.canvas.figure = fig
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = self.num
def _unboundFigure_(self):
self.canvas.figure.boundedToWin = False
self.canvas.figure.keywords['fignum'] = -1
def set_figure(self, fig):
self._unboundFigure_()
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
self._boundFigure_(fig)
def refresh(self):
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
def drawFigure(self, fig):
if(fig.keywords['fignum'] != self.num):
self.set_figure(fig)
self.refresh()
def close(self):
self._unboundFigure_()
self.destroy()
del _G_WINDOWS[np.where([(window == self) for window in _G_WINDOWS])[0][0]]
from myplotlib import readStupidData, readStupidData2
from myplotlib import MyFig
from myplotlib import createWindow, closeWindow, refreshWindow
from myplotlib import MyFig, MyWin
from myplotlib import SEVR, WARN, DBUG
from myplotlib import G_MANAGERS
from myplotlib import _G_WINDOWS
# TESTER
......@@ -30,18 +29,23 @@ def myTest(function, expected, failed, debug):
return(True)
# TEST 1.* for testing MyData
# TEST 2.* for testing MyAxes
# TEST 3.* for testing MyFig
# TEST 4.* for testing MyWin
# TEST 1: test create of MyFig and PRINT TO SCREEN (EXPECTED: TRUE)
def test1(debug):
# TEST 4.00: test create of MyFig and PRINT TO SCREEN (EXPECTED: TRUE)
def test400(debug):
status = True
data1 = readStupidData('data1')
fig = MyFig(inputarg=data1)
manager = createWindow(fig)
win = MyWin(fig)
try:
if manager.canvas.figure.keywords['debug']:
if win.canvas.figure.keywords['debug']:
pass
status = True
......@@ -49,15 +53,16 @@ def test1(debug):
print(SEVR + "I can not access the figure from the manager...")
status = False
closeWindow(0)
win.close()
del win
del fig
del data1
return(status)
# TEST 2: test the update of inputarg (EXPECTED: TRUE)
def test2(debug):
# TEST 300: test the update of inputarg (EXPECTED: TRUE)
def test300(debug):
status = True
......@@ -77,8 +82,8 @@ def test2(debug):
return(status)
# TEST 3: Verify the well behaving of closeWindow (EXPECTED: TRUE)
def test3(debug):
# TEST 401: Verify the well behaving of closeWindow (EXPECTED: TRUE)
def test401(debug):
# create figure
data1 = readStupidData('data1')
......@@ -86,38 +91,27 @@ def test3(debug):
if debug:
print(DBUG + "An object MyFig with data1 has been created.")
#create window num=0
createWindow(fig)
#close window num=0
status = closeWindow(0)
if status and debug:
win = MyWin(fig)
win.close()
if debug:
print(DBUG + "The window 0 has been closed.")
# verify that window suppressed from G_MANAGERS
# verify that window suppressed from _G_WINDOWS
try:
G_MANAGERS[0].canvas.figure.clf()
_G_WINDOWS[0].canvas.figure.clf()
status = False
except:
status = True
# recreate window num=0
createWindow(fig)
# try to close window 1 wich does not exists
# should return False
status = not closeWindow(1)
# close window num=0
status = closeWindow(0)
if status and debug:
print(DBUG + "The window 0 has been closed.")
del win
del fig
del data1
return(status)
# TEST 4: Test refreshWindow (EXPECTED: True)
def test4(debug):
# TEST 402: Test refreshWindow (EXPECTED: True)
def test402(debug):
status = True
......@@ -127,24 +121,21 @@ def test4(debug):
fig = MyFig(inputarg=data1)
if(debug): print(DBUG + "Created a MyFig object with data1.")
win = createWindow(fig)
win = MyWin(fig)
if(debug): print(DBUG + "Created window num=0 for fig.")
fig.update(inputarg=data2)
if(debug): print(DBUG + "Updated fig with data2")
if(fig.keywords['inputarg'] != data2):
status = False
refreshWindow(win)
win.refresh()
if(debug): print(DBUG + "Refreshed the window with actualized fig.")
if(win.canvas.figure.keywords['inputarg'] != data2):
status = False
closeWindow(0)
if(debug): print(DBUG + "Closed the window.")
win.close()
del win
del fig
del data1
del data2
......
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