psamm.util
– Internal utilities¶
Various utilities.
-
psamm.util.
mkdir_p
(path)¶ Make directory path if it does not already exist.
-
class
psamm.util.
LoggerFile
(logger, level)¶ File-like object that forwards to a logger.
The Cplex API takes a file-like object for writing log output. This class allows us to forward the Cplex messages to the Python logging system.
-
write
(s)¶ Write message to logger.
-
flush
()¶ Flush stream.
This is a noop.
-
-
class
psamm.util.
MaybeRelative
(s)¶ Helper type for parsing possibly relative parameters.
>>> arg = MaybeRelative('40%') >>> arg.reference = 200.0 >>> float(arg) 80.0
>>> arg = MaybeRelative('24.5') >>> arg.reference = 150.0 >>> float(arg) 24.5
-
relative
¶ Whether the parsed number was relative.
-
reference
¶ The reference used for converting to absolute value.
-
-
class
psamm.util.
FrozenOrderedSet
(seq=[])¶ An immutable set that retains insertion order.
-
class
psamm.util.
DictView
(d)¶ An immutable wrapper around another dict-like object.
-
psamm.util.
create_unique_id
(prefix, existing_ids)¶ Return a unique string ID from the prefix.
First check if the prefix is itself a unique ID in the set-like parameter existing_ids. If not, try integers in ascending order appended to the prefix until a unique ID is found.
-
psamm.util.
git_try_describe
(repo_path)¶ Try to describe the current commit of a Git repository.
Return a string containing a string with the commit ID and/or a base tag, if successful. Otherwise, return None.
-
psamm.util.
convex_cardinality_relaxed
(f, epsilon=1e-05)¶ Transform L1-norm optimization function into cardinality optimization.
The given function must optimize a convex problem with a weighted L1-norm as the objective. The transformed function will apply the iterated weighted L1 heuristic to approximately optimize the cardinality of the solution. This method is described by S. Boyd, “L1-norm norm methods for convex cardinality problems.” Lecture Notes for EE364b, Stanford University, 2007. Available online at www.stanford.edu/class/ee364b/.
The given function must take an optional keyword parameter weights (dictionary), and the weights must be set to one if not specified. The function must return the non-weighted solution as an iterator over (identifier, value)-tuples, either directly or as the first element of a tuple.