Skip to content

Commit 540939a

Browse files
committed
Add parameter filtering to RecipeOrganizer.show.
Allow filtering of recipe parameters with a regular expression pattern. Sort constraints and restraints according to numeric values within names. Add optional `show` argument `textwidth` to adjust output trimming.
1 parent 365273a commit 540939a

1 file changed

Lines changed: 42 additions & 33 deletions

File tree

diffpy/srfit/fitbase/recipeorganizer.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from diffpy.srfit.util.nameutils import validateName
4141
from diffpy.srfit.interface import _recipeorganizer_interface
4242
from diffpy.srfit.util import _DASHEDLINE
43+
from diffpy.srfit.util import sortKeyForNumericString as numstr
4344

4445

4546
class RecipeContainer(Observable, Configurable, Validatable):
@@ -878,13 +879,10 @@ def _formatManaged(self, prefix=""):
878879
Returns
879880
-------
880881
list
881-
List of formatted lines per each Parameter together with
882-
a leading header and separator lines. Return empty list
883-
when no parameters were defined.
882+
List of formatted lines, one per each Parameter.
884883
"""
885884
lines = []
886885
formatstr = "{:<W}{}"
887-
toplevel = not prefix
888886
# Format own parameters.
889887
if self._parameters:
890888
w0 = max(len(n) for n in self._parameters)
@@ -899,9 +897,6 @@ def _formatManaged(self, prefix=""):
899897
tlines = obj._formatManaged(prefix=oprefix)
900898
lines.extend([""] if lines and tlines else [])
901899
lines.extend(tlines)
902-
# Insert header lines if there are defined parameters.
903-
if toplevel and lines:
904-
lines[:0] = ["Parameters", _DASHEDLINE]
905900
return lines
906901

907902

@@ -914,9 +909,8 @@ def _formatConstraints(self):
914909
Returns
915910
-------
916911
list
917-
List of formatted lines displaying the defined constraints
918-
together with a leading header. Return empty list when no
919-
constraints were defined.
912+
List of formatted lines displaying the defined constraints.
913+
Return empty list when no constraints were defined.
920914
"""
921915
cdict = self._getConstraints()
922916
# Find each constraint and format the equation
@@ -928,10 +922,7 @@ def _formatConstraints(self):
928922
clines.append("%s <-- %s" % (locstr, con.eqstr))
929923
else:
930924
clines.append("%s <-- %s" % (par.name, con.eqstr))
931-
if clines:
932-
# FIXME try to reuse constraints ordering as in FitRecipe._prepare
933-
clines.sort()
934-
clines[:0] = ["Constraints", _DASHEDLINE]
925+
clines.sort(key=numstr)
935926
return clines
936927

937928

@@ -944,48 +935,66 @@ def _formatRestraints(self):
944935
Returns
945936
-------
946937
list
947-
List of formatted lines displaying the defined restraints
948-
together with a leading header. Return empty list when no
949-
restraints were defined.
938+
List of formatted lines displaying the defined restraints.
939+
Return empty list when no restraints were defined.
950940
"""
951941
rset = self._getRestraints()
952942
rlines = []
953943
for res in rset:
954944
line = "%s: lb = %f, ub = %f, sig = %f, scaled = %s"%\
955945
(res.eqstr, res.lb, res.ub, res.sig, res.scaled)
956946
rlines.append(line)
957-
if rlines:
958-
rlines.sort()
959-
rlines[:0] = ["Restraints", _DASHEDLINE]
947+
rlines.sort(key=numstr)
960948
return rlines
961949

962950

963-
def show(self):
964-
"""Show the configuration on screen.
951+
def show(self, pattern="", textwidth=78):
952+
"""Show the configuration hierarchy on the screen.
965953
966-
This will print a summary of all contained objects.
954+
This will print out a summary of all contained objects.
955+
956+
Parameters
957+
----------
958+
pattern : str, optional
959+
Limit output to only those parameters that match this regular
960+
expression (match all by default).
961+
textwidth : int, optional
962+
Trim formatted lines at this text width to avoid folding at
963+
the screen width. Do not trim when negative or 0.
967964
"""
965+
regexp = re.compile(pattern)
966+
pmatch = lambda s : (len(s.split(None, 1)) < 2 or
967+
regexp.search(s.split(None, 1)[0]))
968968
# Show sub objects and their parameters
969969
lines = []
970970
tlines = self._formatManaged()
971-
lines.extend(tlines)
971+
if tlines:
972+
lines.extend(["Parameters", _DASHEDLINE])
973+
lines.extend(filter(pmatch, tlines))
972974

973975
# FIXME - parameter names in equations not particularly informative
974976
# Show constraints
975977
tlines = self._formatConstraints()
976-
if lines and tlines:
977-
lines.append("")
978-
lines.extend(tlines)
978+
if tlines:
979+
if lines:
980+
lines.append("")
981+
lines.extend(["Constraints", _DASHEDLINE])
982+
lines.extend(filter(pmatch, tlines))
979983

980984
# FIXME - parameter names in equations not particularly informative
981985
# Show restraints
982986
tlines = self._formatRestraints()
983-
if lines and tlines:
984-
lines.append("")
985-
lines.extend(tlines)
986-
987-
print "\n".join(lines)
988-
987+
if tlines:
988+
if lines:
989+
lines.append("")
990+
lines.extend(["Restraints", _DASHEDLINE])
991+
lines.extend(filter(pmatch, tlines))
992+
993+
# Determine effective text width tw.
994+
tw = textwidth if (textwidth is not None and textwidth > 0) else None
995+
# Avoid outputting "\n" when there is no output.
996+
if lines:
997+
print("\n".join(s[:tw] for s in lines))
989998
return
990999

9911000
# End RecipeOrganizer

0 commit comments

Comments
 (0)