Commit 05c0eb08 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

add design module:

It is now possible to interactively design plots
from text files.

The file is going to be read and executes part of it
as the place of key function of FigDesign and AxDesign

Both of these classes are simulating behavior in an interactive
environment.

The result can be saved under the form of classes with the
tool savePlot.
parent 8d208bce
_D_PATH2_MYDESIGNS = './myplotlib/design/myDesigns/'
_D_PATH2_MYFIGURES = './myplotlib/design/myFigures/'
_D_PATH2_MYAXES = './myplotlib/design/myAxes/'
from myDesignTool import getSectionFromDesign
from myDesignTool import savePlot
from axDesign import AxDesign
from figDesign import FigDesign
#!/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, INFO
from .. import MyAxes
class AxDesign(MyAxes):
def __init__(self, fig, emulatedAxes, ratio, frameRect, *args, **kwargs):
self.emulatedAxes = emulatedAxes
MyAxes.__init__(self, fig, ratio, frameRect, *args, **kwargs)
def plotting(self):
exec(''.join(self.fig.designSections[self.emulatedAxes]['plotting']))
return(True)
def formatRawData(self, rawdata):
exec(''.join(self.fig.designSections[self.emulatedAxes]['formatRawData']))
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
from . import AxDesign
from myDesignTool import getSectionFromDesign
class FigDesign(MyFig):
FIGSIZE = (8., 6.)
def __init__(self, rawdata, designFile, *args, **kwargs):
self.designFile = designFile
self.readDesignFile()
MyFig.__init__(self, rawdata, *args, **kwargs)
def readDesignFile(self):
print(INFO + "I read the design file again!")
self.designSections = getSectionFromDesign(self.designFile)
# look for the emulated figure
figureFound = False
self.emulatedFigure = None
# for all sections
for className in self.designSections.keys():
# if one section has addAxes then set it to be the figure
if 'addAxes' in self.designSections[className].keys():
if not figureFound:
self.emulatedFigure = str(className)
figureFound = True
else:
print(WARN + "apparently you have two figures in that design file... only the first one will be set up")
if self.emulatedFigure is None:
print(WARN + "No figure found... will exit")
return(False)
return(True)
def addAxes(self):
exec(''.join(self.designSections[self.emulatedFigure]['addAxes']))
def declareKeywords(self):
if 'declareKeywords' in self.designSections[self.emulatedFigure].keys():
exec(''.join(self.designSections[self.emulatedFigure]['declareKeywords']))
else:
pass
def update(self, **kwargs):
status = MyFig.update(self, **kwargs)
if not status: return(False)
status = self.readDesignFile()
return(status)
#_D_PATH2DESIGN = './myplotlib/test/design/'
#_D_PATH2_MYFIGURE = './myplotlib/test/design/'
#_D_PATH2_MYAXES = './myplotlib/test/design/
_D_PATH2DESIGN = './design/'
_D_PATH2_MYFIGURE = './design/'
_D_PATH2_MYAXES = './design/'
from . import _D_PATH2_MYDESIGNS
from . import _D_PATH2_MYFIGURES
from . import _D_PATH2_MYAXES
from .. import WARN, INFO
WARN = ' > warn-:'
INFO = ' > info-:'
def getSectionFromDesign(designFile):
f = open(_D_PATH2DESIGN + str(designFile))
f = open(_D_PATH2_MYDESIGNS + str(designFile))
designSections = {}
......@@ -121,7 +118,7 @@ def savePlot(filename):
addAxes=addAxesString)
# CREATE THE FIGURE FILE
f = open(_D_PATH2_MYFIGURE + str(figureClassName)[0].lower() + str(figureClassName)[1:] + '.py', 'w')
f = open(_D_PATH2_MYFIGURES + str(figureClassName)[0].lower() + str(figureClassName)[1:] + '.py', 'w')
# WRITE THE FIGURE
f.writelines(template)
......@@ -186,4 +183,3 @@ class {className}(MyFig):
{addAxes}
'''
# FIGTEST =============================================================
< FigTest::addAxes
< FigMyCrazy::addAxes
ratio = 3./8.
frame = [0., 0., 1.0, 0.5]
frame2 = [0., 0.5, 1.0, 0.5]
self.add_axes(AxFullDesign(self, 'AxTest', ratio, frame))
self.add_axes(AxFullDesign(self, 'AxTest2', ratio, frame2))
frame = [0., 0.1, 1.0, 0.6]
frame2 = [0., 0.6, 1.0, 0.4]
self.add_axes(AxDesign(self, 'AxMyCrazy1', ratio, frame))
self.add_axes(AxDesign(self, 'AxMyCrazy2', ratio, frame2))
/>
# ---------------------------------------------------------------------
< FigTest::declareKeywords
< FigMyCrazy::declareKeywords
self.keywords = {'xrange': [0, 5],
'yrange': [0, 10]}
......@@ -21,7 +21,7 @@ self.keywords = {'xrange': [0, 5],
# AXTEST ==============================================================
< AxTest::formatRawData
< AxMyCrazy1::formatRawData
print("AxTest::formatRawData")
self.data = {'xdata': [0., 5.],
......@@ -31,35 +31,42 @@ self.data = {'xdata': [0., 5.],
# ---------------------------------------------------------------------
< AxTest::plotting
< AxMyCrazy1::plotting
print("AxTest::plotting")
self.set_xlim(self.fig.keywords['xrange'])
self.set_ylim(self.fig.keywords['yrange'])
self.plot(self.data['xdata'], self.data['ydata'])
self.set_xlabel(r'$B_{\rm quench}$')
self.set_ylabel(r'$C_{\rm S}$')
self.lines[0].set_color('k')
self.lines[0].set_linestyle('--')
/>
# AXTEST2 ==============================================================
< AxTest2::formatRawData
< AxMyCrazy2::formatRawData
print("AxTest2::formatRawData")
self.data = {'xdata': [5., 0.],
'ydata': [1., 6.]}
'ydata': [1., 8.]}
/>
# ---------------------------------------------------------------------
< AxTest2::plotting
< AxMyCrazy2::plotting
print("AxTest::plotting")
self.set_xlim(self.fig.keywords['xrange'])
self.set_ylim(self.fig.keywords['yrange'])
self.plot(self.data['xdata'], self.data['ydata'])
self.lines[0].set_color('g')
self.lines[0].set_linestyle('-.')
self.set_xlabel(r'Time [$\tau$]', fontsize=16)
/>
......
......@@ -19,8 +19,8 @@ class FigTest(MyFig):
ratio = 3./8.
frame = [0., 0., 1.0, 0.5]
frame2 = [0., 0.5, 1.0, 0.5]
self.add_axes(AxFullDesign(self, 'AxTest', ratio, frame))
self.add_axes(AxFullDesign(self, 'AxTest2', ratio, frame2))
self.add_axes(AxDesign(self, 'AxTest', ratio, frame))
self.add_axes(AxDesign(self, 'AxTest2', ratio, frame2))
......@@ -319,6 +319,8 @@ class MyFig(MyFig_base):
status = True
self.update()
if(self.debug):
print(DBUG + "currently formatting the data...")
......
from myIOs import readStupidData, readStupidData2
from axTest1 import AxTest1
from axTest1 import AxDesign
from axTest1 import AxFullDesign
from figTest import FigTest2
from figTest import FigDesing
from figTest import FigFullDesing
from tests import myTest
......
......@@ -77,46 +77,3 @@ class AxTest1(MyAxes):
print(DBUG + 'I formatted the raw data!')
return(True)
class AxDesign(MyAxes):
def plotting(self):
f = open('./axplotting.input', 'r')
for line in f.readlines():
print(INFO + str(line))
exec(line)
f.close()
return(True)
def formatRawData(self, rawdata):
self.data = {'xdata' : [0, 1],
'ydata' : [0, 1]}
return(True)
class AxFullDesign(MyAxes):
def __init__(self, fig, emulatedAxes, ratio, frameRect, *args, **kwargs):
self.emulatedAxes = emulatedAxes
MyAxes.__init__(self, fig, ratio, frameRect, *args, **kwargs)
def plotting(self):
exec(''.join(self.fig.designSections[self.emulatedAxes]['plotting']))
return(True)
def formatRawData(self, rawdata):
exec(''.join(self.fig.designSections[self.emulatedAxes]['formatRawData']))
return(True)
# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!
# CLASS FigTest========================================================
class FigTest(MyFig):
# DECLARE KEYWORDS ------------------------------------------------
def declareKeywords(self):
self.keywords = {'xrange': [0, 5],
'yrange': [0, 10]}
# ADD AXES --------------------------------------------------------
def addAxes(self):
ratio = 3./8.
frame = [0., 0., 1.0, 0.5]
frame2 = [0., 0.5, 1.0, 0.5]
self.add_axes(AxFullDesign(self, 'AxTest', ratio, frame))
self.add_axes(AxFullDesign(self, 'AxTest2', ratio, frame2))
......@@ -69,13 +69,11 @@ from .. import os
from .. import DBUG, SEVR, INFO, SPCE, WARN
from .. import MyFig, MyFig_base
from .axTest1 import AxTest1, AxDesign, AxFullDesign
from .axTest1 import AxTest1
D_XRANGE = [-1, 1]
D_YRANGE = [-2, 2]
_D_PATH2DESIGN = './myplotlib/test/design/'
# Class MyFig Overwriting Matplotlib.figure.Figure
class FigTest2(MyFig):
......@@ -97,155 +95,3 @@ class FigTest2(MyFig):
frame = [0.0, 0.0, 1.0, 1.0] # part of the fig that is available
self.add_axes(AxTest1(self, ratio, frame))
class FigDesing(MyFig):
FIGSIZE = (8., 6.)
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(AxDesign(self, ratio, frame))
class FigFullDesing(MyFig):
FIGSIZE = (8., 6.)
def __init__(self, rawdata, designFile, *args, **kwargs):
self.designFile = designFile
self.readDesignFile()
MyFig.__init__(self, rawdata, *args, **kwargs)
def readDesignFile(self):
self.designSections = self.getSectionFromDesign()
# look for the emulated figure
figureFound = False
self.emulatedFigure = None
# for all sections
for className in self.designSections.keys():
# if one section has addAxes then set it to be the figure
if 'addAxes' in self.designSections[className].keys():
if not figureFound:
self.emulatedFigure = str(className)
figureFound = True
else:
print(WARN + "apparently you have two figures in that design file... only the first one will be set up")
if self.emulatedFigure is None:
print(WARN + "No figure found... will exit")
return(False)
return(True)
def getSectionFromDesign(self):
f = open(_D_PATH2DESIGN + str(self.designFile))
designSections = {}
startProcedure = False
className = ''
functionName = ''
procedure = []
for line in f.readlines():
# If recording a procedure
if startProcedure:
# and found the end of it
if line.startswith('/>'):
print(INFO + str(className) + ' ' + str(functionName))
# Save the procedure
if str(className) in designSections.keys():
designSections[str(className)].update({str(functionName): procedure})
else:
designSections.update({str(className): {}})
designSections[str(className)].update({str(functionName): procedure})
# reset
className = ''
functionName = ''
procedure = []
startProcedure = False
# and found bgining of a new one print WARNING
elif line.startswith('<'):
print(WARN + "I found a line starting with '<' before the end of the section, you may have forgot to close it.")
# record the procedure
else:
procedure.append(line)
# if not recording and found begining of a section
if (line.startswith('<')) and (startProcedure is False):
# check the format
if line.startswith('< '):
# get the ClassName and FunctionName
label = line.split(' ')
if len(label) < 2:
print(WARN + "apparently the format you use to declare this section is incrorrect. use: '< ClassName::FunctionName'")
className, functionName = label[1].split('::')
if('\n' in str(className)):
className = className[:-1]
if('\n' in str(functionName)):
functionName = functionName[:-1]
print(INFO + str(className) + ' ' + str(functionName))
procedure = []
# start recording
startProcedure = True
# if format wrong print WARNING
else:
print(WARN + "apparently you forgot a space after opening the section. please strictly use: '< ClassName::FunctionName'")
for className in designSections.keys():
print(INFO + str(className) + ": " + str(designSections[className].keys()))
f.close()
return(designSections)
def addAxes(self):
exec(''.join(self.designSections[self.emulatedFigure]['addAxes']))
def declareKeywords(self):
if 'declareKeywords' in self.designSections[self.emulatedFigure].keys():
exec(''.join(self.designSections[self.emulatedFigure]['declareKeywords']))
else:
pass
def update(self, **kwargs):
status = MyFig.update(self, **kwargs)
if not status: return(False)
status = self.readDesignFile()
return(status)
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