Commit 02b99b12 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

Now version with new organisation of the module, tests go throught, but may...

Now version with new organisation of the module, tests go throught, but may need check relevance and new tests are required
parents 46e73026 c8792c08
......@@ -48,7 +48,7 @@ from matplotlib import is_interactive
from matplotlib import rcParams
from matplotlib import use
rcParams['backend'] = u'TkAgg'
rcParams['backend'] = u'GTKAgg'
# myplotlib
from myData import MyData
......@@ -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
......@@ -100,11 +100,8 @@ rc('figure', dpi = 75.0)
# MyAxes: Overlay on matplotlib.Axes class
from .myAxes import MyAxes
# MyIOs.*: input/output function for testing
from .myIOs import readStupidData, readStupidData2
# 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':
......@@ -121,13 +118,6 @@ from .myTool import print2file, print2screen, printListCurrentWindows
from .myTool import getWindow, getFigOnWindow, drawFigOnWindow, giveDataToWindow
from .myTool import closeWindow, closeAllWindows
# MyFig4Test: Overlay of Figure for testing
from .myFig4Test import MyFig4Test
# some plotting routine for testing
from .myPlot4Test import somePlottingRoutine
# Import the testing interface
from .test import myTest
# import the tests
......
......@@ -51,10 +51,8 @@ from . import rcParams
class MyAxes(Axes):
# CONSTRUCTOR --------------------------------------------------------
def __init__(self, fig, ratio, frameRect, **kwargs):
def __init__(self, fig, ratio, frameRect, *args, **kwargs):
self.debug = kwargs.get('debug', 0)
# get the frame allowed
framePosX, framePosY, frameWidth, frameHeight = frameRect
# get the size of the figure
......@@ -96,29 +94,8 @@ class MyAxes(Axes):
# PLOTTING -----------------------------------------------------------
# the plotting function (need to be overwrite from child
def plotting(self):
try:
self.plot(self.data['xdata'], self.data['ydata'])
except KeyError:
print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
return(False)
return(True)
pass
# FORMATTING ---------------------------------------------------------
def formatRawData(self, rawdata):
# give value to data a dict
# with xdata, ydata1, zdata, ydata2 ...
try:
self.data = {'xdata': [rawdata.data[0], rawdata.data[1]],
'ydata': [rawdata.data[2], rawdata.data[3]]}
except (TypeError, KeyError):
print(SEVR + 'The Raw Data could not be formatted --> EXIT')
return(False)
if(self.debug):
print(DBUG + 'I formatted the raw data!\n')
print(DBUG + 'formatted data:\n' + str(self.formattedDatas))
return(True)
pass
......@@ -70,34 +70,50 @@ 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
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
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])
# If some user keywords are defined
if hasattr(self, 'keywords'):
# check keywords
for keyword in self.keywords.keys():
# if in kwargs
if keyword in kwargs.keys():
# overwrite the default value
self.keywords[keyword] = kwargs[keyword]
# suppress keyword from kwargs for Figure.__init__
del kwargs[keyword]
else:
self.keywords = {}
self.rawdata = rawdata
# add figsize in the kwargs for Figure.__init__
kwargs['figsize'] = self.FIGSIZE
......@@ -114,53 +130,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):
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)
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)
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(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)
pass
# 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():
attributes = ('fignum', 'reformat', 'debug', 'formatted')
# check attributes in keywords
for keyword in kwargs.keys():
# if it is an attribute
if keyword in attributes:
# update value
setattr(self, keyword, kwargs[keyword])
# 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 -------------------------------------------------------
......@@ -189,7 +188,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
......@@ -237,3 +236,99 @@ class MyFig(Figure):
self.canvas = winCanvas
return(True)
# CLASS MyFig ==========================================================
class MyFig(MyFig_base):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, rawdata, *args, **kwargs):
# define user keywords before
# __init__ to erase them from kwargs
self.defineKeywords()
MyFig_base.__init__(self, rawdata, *args, **kwargs)
self._initialize(*args, **kwargs)
# INITIALIZE -------------------------------------------------------
def _initialize(self, *args, **kwargs):
# update the attributes and keywords
self.update(**kwargs)
# add the axes
self.addAxes()
# need to be done for the first init
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()
if(not status): return(False)
# 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, *args, **kwargs):
self.clf()
self.defineKeywords()
self._initialize(*args, **kwargs)
self.plot()
......@@ -40,7 +40,7 @@ from . import INFO, SEVR, WARN, SPCE
from . import ion, ioff, is_interactive
from . import MyWin, MyFig
from . import MyWin
from . import np
......
......@@ -173,7 +173,6 @@ class MyWin_GTKAgg(FigureManagerGTKAgg):
self.set_figure(self.canvas.figure)
# refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
......
......@@ -58,8 +58,15 @@ from . import np
# For the backend
from matplotlib.backends.backend_tkagg import FigureManagerTkAgg, FigureCanvasTkAgg
from matplotlib.externals import six
from matplotlib.externals.six.moves import tkinter as Tk
# Test mpl version:
from matplotlib import __version__ as mplvers
if int(mplvers.replace('.','')) < 150 :
import six
from six.moves import tkinter as Tk
else :
from matplotlib.externals import six
from matplotlib.externals.six.moves import tkinter as Tk
# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
......@@ -182,7 +189,6 @@ class MyWin_TkAgg(FigureManagerTkAgg):
self.set_figure(self.canvas.figure)
# refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
......
......@@ -138,7 +138,6 @@ class MyWin_WXAgg(FigureManagerWx):
# if show is on, then show the window
if show:
self.canvas.figure.clf()
self.canvas.figure.plot()
self.show()
self.canvas.draw()
......@@ -187,7 +186,6 @@ class MyWin_WXAgg(FigureManagerWx):
self.set_figure(self.canvas.figure)
#refresh
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
self.canvas.figure.plot()
......
from myIOs import readStupidData, readStupidData2
from axTest1 import AxTest1
from figTest import FigTest1, FigTest2
from tests import myTest
# import the tests
from tests import test100
from tests import test200, test201, test202
from tests import test300, test301, test302, test303, test304, test305
from tests import test306, test307, test308, test309
from tests import test400, test401, test402, test403
from tests import test600
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
# myplotlib v0.0.0,
#
# @file myAxes.py
# @author Yori 'AGy' Fournier
# @licence CC-BY-SA
#
# MyAxes class: Overlay of matplotlib Axes class
# It is done such that the user can concentrate on
# the important aspect of the figure and not the
# technical part. It consists of a constructor,
# that requires a Figure, the ratio of the xaxis over
# the yaxis, and the frame in which the figure
# should be plotted.
#
# @Class MyAxes
#
# @Constructor(self, fig, ratio, frame, +user defined kw):
#
# @section Functions
#
# - plotting: this is the overwritten function
# from Axes. It is called by it's
# parent-figure's function .plot()
#
# - formatRawData: it computes out of the rawData
# given as argument some quantities that
# are returned as a dictionary and
# become accessable for plotting.
#
# @section History
#
# v 0.0.0 - MyAxes class for the myplotlib module, consists
# of a constructor, a pltting function and
# formatRawData.
#
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
from .. import SEVR, DBUG
from .. import MyAxes
# Class MyAxes Overwriting Matplotlib.figure.Axes
class AxTest1(MyAxes):
# CONSTRUCTOR --------------------------------------------------------
def __init__(self, fig, ratio, frameRect, *args, **kwargs):
self.fig = fig
# parent constructor
MyAxes.__init__(self, fig, ratio, frameRect, *args, **kwargs)
# PLOTTING -----------------------------------------------------------
# the plotting function (need to be overwrite from child
def plotting(self):
try:
self.plot(self.data['xdata'], self.data['ydata'])
except KeyError:
print(SEVR + 'The formatting of the data was apparently wrong. --> EXIT')
return(False)
return(True)
# FORMATTING ---------------------------------------------------------
def formatRawData(self, rawdata):
# give value to data a dict
# with xdata, ydata1, zdata, ydata2 ...
try:
self.data = {'xdata': [rawdata.data[0], rawdata.data[1]],
'ydata': [rawdata.data[2], rawdata.data[3]]}
except (TypeError, KeyError):
print(SEVR + 'The Raw Data could not be formatted --> EXIT')
return(False)
if(self.fig.debug):
print(DBUG + 'I formatted the raw data!')
return(True)
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
# myplotlib v1.0.1,
#
# @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.
# inputarg: 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: update the parameters in the
# keywords dictionary.
#
# @section History
#
# v 0.1.1 - MyFig class for the myplotlib module.
#
# v 1.0.0 - Changed rawdata with inputarg to allows any type
# of rawdata, can be an array.
#
# replace the attribute format (reserved word) with
# formatted.
#
# v 1.0.1 - Suppress the explicit definitions of the keywords.
# by the introduction of the keywords dictionary.
# All parameters in this dictionary are automatically
# updated.
#
# suppress the default value of fignum and hardcode it
# to be -1.
#
# add the attribute boundedToWin required for the
# interactive mode of myplotlib.
#
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
from .. import os
from .. import DBUG, SEVR, INFO, SPCE, WARN
from .. import MyFig, MyFig_base
from .axTest1 import AxTest1
D_XRANGE = [-1, 1]
D_YRANGE = [-2, 2]
# Class MyFig Overwriting Matplotlib.figure.Figure
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, rawdata, *args, **kwargs):
# parent constructor
MyFig.__init__(self, rawdata, *args, **kwargs)
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):
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))