Commit 108904de authored by Philipp Gast's avatar Philipp Gast
Browse files

starting change of alias system. Logic in place. Testing needed.

parent 0c99804a
Pipeline #516 failed with stage
in 11 minutes and 55 seconds
......@@ -169,6 +169,7 @@ class MplFig():
self.reformat = kwargs.pop('reformat', D_REFORMAT)
self.debug = kwargs.pop('debug', D_DEBUG)
self.formatted = kwargs.pop('formatted', D_FORMATTED)
self._tagged_keywords = {}
self.aliases = {}
self.FIGSIZE = kwargs.pop('figsize', self.FIGSIZE)
......@@ -195,7 +196,10 @@ class MplFig():
self.addAxes()
# This will throw a pep8 error but its needed to prevent
# confusion with matplotlib -- need another name
# automatically create keys for the axes keywords to provide a 1 to 1 mapping
self._generate_tagged_keywords()
# declare the aliases
self.declare_aliases()
......@@ -205,6 +209,12 @@ class MplFig():
# PRIVATE UPDATE ---------------------------------------------------
def _update(self, **kwargs):
# this dics will hold the cleaned up keywords.
# Two are nesessary to ensure the higher priority of explicit
# keys over alias groups
forax_low = {}
forax_high = {}
# check attributes in keywords
for keyword in kwargs.keys():
......@@ -223,37 +233,62 @@ class MplFig():
# update value
setattr(self, keyword, kwargs[keyword])
# For each axes update the keywords
# Check if a key of kwargs is an alias and replace it
# by its _tagged_keywords group
elif keyword in self.aliases.keys():
alias_value = kwargs[keyword]
key_group = self.aliases[keyword]
forax_low.update({key: alias_value for key in key_group})
# keyword is a direct setting for a tagged keyword
elif keyword in self._tagged_keywords.keys():
forax_high.update({keyword: kwargs[keyword]})
else:
print(WARN+' MAJOR CHANGE IN FUNCTIONALITY')
print(WARN+' ignoring unknown keyword ', keyword)
# Now kwargs are cleard of attributs and aliases.
# translate this into axes keywords:
# ensure higher priority of explicit settings by overwriting:
forax_low.update(forax_high)
del forax_high # to avoid confusion
# For each axes collect the keywords
for ax in self.get_axes():
forax = {}
for keyword in kwargs.keys():
# ignore figure attributes
if keyword in ['rawdata'] + self._attributes_to_update_keys:
pass
# Check if a key of kwargs has an alias for this axes
elif keyword in self.aliases.keys():
alax, alkey = self.aliases[keyword]
# If an alias is found then update axkwargs
if ax == alax:
forax.update(**{alkey: kwargs[keyword]})
# use keyword as it is for the axes
else:
forax.update(**{keyword: kwargs[keyword]})
ax_specific_keys = {}
for keyword in forax_low.keys():
alax, alkey = self._tagged_keywords[keyword]
if ax == alax:
ax_specific_keys.update({alkey: forax_low[keyword]})
# Then eventually all collected Keywords are updated in one go
if (forax):
if (ax_specific_keys) :
if self.debug:
print (DBUG + ' fig.update ', ax, 'keywords: ', forax)
ax.update(**forax)
print (DBUG+' fig.update ', ax, 'keywords: ', forax)
ax.update(**ax_specific_keys)
return(True)
# GENERATE TAGGED KEYWORDS -----------------------------------------
def _generate_tagged_keywords(self):
""" make the axes keywords accessible in the figure context by inserting
a generated name to _tagged_keywords. An auto-naming scheme is
applied like so:
<axes_name>_<keyword_from_axes_context>"""
for ax in self.axes:
for key,val in ax.keywords.items()
self._tagged_keywords.update({ax.name+"_"+key : (ax , key) })
# GET AXES BY NAME -------------------------------------------------
def get_axes_by_name(self, name):
......@@ -274,11 +309,18 @@ class MplFig():
if unknownax == ax :
return ax.name
# LIST KEYWORDS AVAILABLE IN THE FIGURE ----------------------------
def list_keywords(self):
""" list all keywords (_tagged_keywords) and aliases plus their
current state"""
#~ return {alias_key : current_value for all_axes}
pass
# GET AXES ---------------------------------------------------------
def get_axes(self):
return(self.axes)
# DEBUG --------------------------------------------------------------
# DEBUG ------------------------------------------------------------
def print_debug(self):
class_name = str(self.__class__.__name__)
print('\n' + DBUG + " {0} PARAMETERS: ".format(class_name))
......@@ -896,33 +938,9 @@ class MplFig():
state = dict()
# test for double keywords and print a waring
for ax in self.axes:
# find aliases for this specific axes
masks = dict()
for alias in self.aliases.keys():
axes_for_alias, keyword = self.aliases[alias]
if axes_for_alias == ax:
masks.update({keyword: alias})
# collect all keywords of the axes and replace the keys with the
# aliases
ax_keywords = ax.keywords.copy()
for mask in masks:
if mask in ax_keywords:
ax_keywords[masks[mask]] = ax_keywords.pop(mask)
# Test if kewords are already set by an other axes
for key in ax_keywords.keys():
if key in state.keys():
print(
'Warning: The keyword \"',
key,
'\" appears in multiple axes. The Defaults will'
' be overwritten. Prevent this by using aliases')
# update the global keyword index
state.update(ax_keywords)
for tagged_key,key_value in self._tagged_keywords.items():
ax, ax_key = key_value
state.update({tagged_key : ax.keyword[ax_key]})
return state
......
......@@ -106,7 +106,6 @@ class FigTest2(MyFig): # Fig wi
p1 = self.getAxesByName("p1")
p2 = self.getAxesByName("p2")
self.aliases = {'xRange_p1': (p1, "xRange"),
'xRange_p2': (p2, "xRange")}
self.aliases = {'xRange': ("p1_xRange","p2_xRange")}
return(True)
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