Nektar++
Classes | Functions | Variables
CellMLToNektar.utilities Namespace Reference

Classes

class  Colourable
 
class  NotifyHandler
 
class  OnlyDebugFilter
 
class  OnlyTheseSourcesFilter
 
class  OnlyWarningsFilter
 
class  Sentinel
 
class  unitary_iterator
 

Functions

def DEBUG (facility, *args)
 
def LOG (facility, level, *args)
 
def amara_parse (source, uri=None, rules=None, binderobj=None, prefixes=None)
 
def element_path (elt)
 
def element_path_cmp (e1, e2)
 
def element_xpath (elt)
 
def brief_xml (elt)
 
def call_if (tf, callable, *args, **kwargs)
 
def max_i (it)
 
def prid (obj, show_cls=False)
 
def add_dicts (r, *ds)
 
def open_output_stream (fname)
 
def close_output_stream (stream)
 

Variables

list __all__
 
 DFS = Enum('White', 'Gray', 'Black')
 

Detailed Description

Copyright (c) 2005-2016, University of Oxford.
All rights reserved.

University of Oxford means the Chancellor, Masters and Scholars of the
University of Oxford, having an administrative office at Wellington
Square, Oxford OX1 2JD, UK.

This file is part of Chaste.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
 * Neither the name of the University of Oxford nor the names of its
   contributors may be used to endorse or promote products derived from this
   software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Function Documentation

◆ add_dicts()

def CellMLToNektar.utilities.add_dicts (   r,
ds 
)
Add multiple dictionaries together.

Updates the first input dictionary by adding values from
subsequent inputs.  Produces a dictionary with keys taken from the
input dictionaries, and values being the sum of the corresponding
values in all inputs.
Assumes values are numeric.

Definition at line 284 of file utilities.py.

284def add_dicts(r, *ds):
285 """Add multiple dictionaries together.
286
287 Updates the first input dictionary by adding values from
288 subsequent inputs. Produces a dictionary with keys taken from the
289 input dictionaries, and values being the sum of the corresponding
290 values in all inputs.
291 Assumes values are numeric.
292 """
293 for d in ds:
294 for k, v in d.iteritems():
295 r[k] = r.get(k, 0) + v
296
297

Referenced by CellMLToNektar.pycml.mathml_apply.tree_complexity(), and CellMLToNektar.pycml.mathml_piecewise.tree_complexity().

◆ amara_parse()

def CellMLToNektar.utilities.amara_parse (   source,
  uri = None,
  rules = None,
  binderobj = None,
  prefixes = None 
)
Convenience function for parsing XML.

Works just as amara.parse, except that if source is '-' then
it reads from standard input.

Definition at line 191 of file utilities.py.

192 prefixes=None):
193 """Convenience function for parsing XML.
194
195 Works just as amara.parse, except that if source is '-' then
196 it reads from standard input.
197 """
198 if source == '-':
199 return amara.parse(sys.stdin, uri=uri, rules=rules,
200 binderobj=binderobj, prefixes=prefixes)
201 else:
202 return amara.parse(source, uri=uri, rules=rules,
203 binderobj=binderobj, prefixes=prefixes)
204
205

Referenced by CellMLToNektar.pycml.amara_parse_cellml(), and CellMLToNektar.translators.ConfigurationStore.read_configuration_file().

◆ brief_xml()

def CellMLToNektar.utilities.brief_xml (   elt)
Print a more concise version of elt.xml() that omits all attributes.

Definition at line 232 of file utilities.py.

232def brief_xml(elt):
233 """Print a more concise version of elt.xml() that omits all attributes."""
234 s = ''
235 if getattr(elt, 'nodeType', None) == Node.ELEMENT_NODE:
236 children = getattr(elt, 'xml_children', [])
237 if children:
238 s += '<' + elt.localName + '>'
239 for child in children:
240 s += brief_xml(child)
241 s += '</' + elt.localName + '>'
242 else:
243 s += '<' + elt.localName + '/>'
244 else:
245 s += str(elt)
246 return s
247
248

References CellMLToNektar.utilities.brief_xml().

Referenced by CellMLToNektar.utilities.brief_xml().

◆ call_if()

def CellMLToNektar.utilities.call_if (   tf,
  callable,
args,
**  kwargs 
)
                                                   #

Miscellaneous functions # #

Call the given callable with the given arguments iff tf is True.

Definition at line 255 of file utilities.py.

255def call_if(tf, callable, *args, **kwargs):
256 """Call the given callable with the given arguments iff tf is True."""
257 if tf:
258 return callable(*args, **kwargs)
259
260
def call_if(tf, callable, *args, **kwargs)
Definition: utilities.py:255

Referenced by CellMLToNektar.translators.ConfigurationStore._find_transmembrane_currents_from_voltage_ode().

◆ close_output_stream()

def CellMLToNektar.utilities.close_output_stream (   stream)
Close the given output stream, unless it's one of the standard streams
(i.e. sys.stdout or sys.stderr).
Note that closing a stream multiple times is safe.

Definition at line 314 of file utilities.py.

314def close_output_stream(stream):
315 """
316 Close the given output stream, unless it's one of the standard streams
317 (i.e. sys.stdout or sys.stderr).
318 Note that closing a stream multiple times is safe.
319 """
320 if not stream is sys.stdout and not stream is sys.stderr:
321 stream.close()
def close_output_stream(stream)
Definition: utilities.py:314

Referenced by CellMLToNektar.translators.run(), and CellMLToNektar.validator.run().

◆ DEBUG()

def CellMLToNektar.utilities.DEBUG (   facility,
args 
)
Log a debug message to facility.

Arguments are treated as for the print statement.

Definition at line 95 of file utilities.py.

95def DEBUG(facility, *args):
96 """Log a debug message to facility.
97
98 Arguments are treated as for the print statement.
99 """
100 logger = logging.getLogger(facility)
101 if logger.isEnabledFor(logging.DEBUG):
102 logger.debug(' '.join(map(str, args)))
103
104
def DEBUG(facility, *args)
Definition: utilities.py:95

Referenced by CellMLToNektar.processors.UnitsConverter._apply_special_conversion_for_nested_expr(), CellMLToNektar.pycml.cellml_model._check_assigned_vars(), CellMLToNektar.pycml.cellml_model._check_cellml_subset(), CellMLToNektar.optimize.LinearityAnalyser._check_expr(), CellMLToNektar.processors.UnitsConverter._check_special_conversion(), CellMLToNektar.pycml.cellml_model._check_variable_mappings(), CellMLToNektar.pycml.cellml_model._classify_variables(), CellMLToNektar.pycml.cellml_variable._decrement_usage_count(), CellMLToNektar.optimize.LookupTableAnalyser._determine_unneeded_tables(), CellMLToNektar.translators.ConfigurationStore._find_transmembrane_currents_from_voltage_ode(), CellMLToNektar.pycml.cellml_variable._get_binding_time(), CellMLToNektar.pycml.mathml_eq._get_binding_time(), CellMLToNektar.pycml.mathml_ci._reduce(), CellMLToNektar.pycml.mathml_apply._reduce(), CellMLToNektar.pycml.mathml_constructor._reduce_elt(), CellMLToNektar.pycml.mathml_units_mixin._set_element_in_units(), CellMLToNektar.pycml.mathml_units_mixin_choose_nearest._set_in_units(), CellMLToNektar.pycml.cellml_model._validate_component_hierarchies(), CellMLToNektar.optimize.LinearityAnalyser.analyse_for_jacobian(), CellMLToNektar.optimize.LookupTableAnalyser.analyse_for_lut(), CellMLToNektar.processors.UnitsConverter.convert_mapping(), CellMLToNektar.translators.ConfigurationStore.expose_variables(), CellMLToNektar.translators.ConfigurationStore.find_lookup_variables(), CellMLToNektar.translators.ConfigurationStore.find_membrane_capacitance(), CellMLToNektar.translators.ConfigurationStore.find_transmembrane_potential(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_method_start(), CellMLToNektar.translators.ConfigurationStore.read_configuration_file(), CellMLToNektar.pycml.cellml_variable.remove_rdf_annotations(), CellMLToNektar.translators.run(), CellMLToNektar.validator.CellMLValidator.validate(), and CellMLToNektar.pycml.cellml_model.validate().

◆ element_path()

def CellMLToNektar.utilities.element_path (   elt)
Find the path from the root element to this element.

Definition at line 206 of file utilities.py.

206def element_path(elt):
207 """Find the path from the root element to this element."""
208 if hasattr(elt, 'xml_parent'):
209 idx = 0
210 for child in elt.xml_parent.xml_children:
211 if getattr(child, 'nodeType', None) == Node.ELEMENT_NODE:
212 idx += 1
213 if child is elt:
214 break
215 return element_path(elt.xml_parent) + [idx]
216 else:
217 return []
218
219

References CellMLToNektar.utilities.element_path().

Referenced by CellMLToNektar.utilities.element_path(), CellMLToNektar.utilities.element_path_cmp(), and CellMLToNektar.utilities.element_xpath().

◆ element_path_cmp()

def CellMLToNektar.utilities.element_path_cmp (   e1,
  e2 
)
Compare 2 elements by comparing their paths from the root element.

Definition at line 220 of file utilities.py.

220def element_path_cmp(e1, e2):
221 """Compare 2 elements by comparing their paths from the root element."""
222 return cmp(element_path(e1), element_path(e2))
223
224
def element_path_cmp(e1, e2)
Definition: utilities.py:220

References CellMLToNektar.utilities.element_path().

◆ element_xpath()

def CellMLToNektar.utilities.element_xpath (   elt)
Return an xpath expression that will select this element.

Definition at line 225 of file utilities.py.

225def element_xpath(elt):
226 """Return an xpath expression that will select this element."""
227 indices = element_path(elt)
228 xpath = u'/*[' + u']/*['.join(map(str, indices)) + u']'
229 return xpath
230
231

References CellMLToNektar.utilities.element_path().

Referenced by CellMLToNektar.pycml.mathml_units_mixin._add_units_conversion(), and CellMLToNektar.pycml.MathsError._generate_message().

◆ LOG()

def CellMLToNektar.utilities.LOG (   facility,
  level,
args 
)
Log a message to facility with the given level.

Arguments are treated as for the print statement.

Definition at line 105 of file utilities.py.

105def LOG(facility, level, *args):
106 """Log a message to facility with the given level.
107
108 Arguments are treated as for the print statement.
109 """
110 logger = logging.getLogger(facility)
111 if logger.isEnabledFor(level):
112 logger.log(level, ' '.join(map(str, args)))
113
def LOG(facility, level, *args)
Definition: utilities.py:105

Referenced by CellMLToNektar.translators.ConfigurationStore.find_lookup_variables().

◆ max_i()

def CellMLToNektar.utilities.max_i (   it)
Find the maximum entry of an iterable, and return it with its index.

Returns (i, m) where i is the index of m, the maximum entry of `it`.

Definition at line 261 of file utilities.py.

261def max_i(it):
262 """Find the maximum entry of an iterable, and return it with its index.
263
264 Returns (i, m) where i is the index of m, the maximum entry of `it`.
265 """
266 idx, m = None, None
267 for i, val in enumerate(it):
268 if m is None or val > m:
269 m, idx = val, i
270 return idx, m
271
272

Referenced by CellMLToNektar.pycml.mathml_piecewise.tree_complexity().

◆ open_output_stream()

def CellMLToNektar.utilities.open_output_stream (   fname)
Open fname for output.

fname should be a local filename, or '-' for standard output.
Additionally, the names 'stdout' and 'stderr' have the usual
special meanings.

Definition at line 298 of file utilities.py.

298def open_output_stream(fname):
299 """Open fname for output.
300
301 fname should be a local filename, or '-' for standard output.
302 Additionally, the names 'stdout' and 'stderr' have the usual
303 special meanings.
304 """
305 if fname == '-' or fname == 'stdout':
306 stream = sys.stdout
307 elif fname == 'stderr':
308 stream = sys.stderr
309 else:
310 stream = open(fname, 'w')
311 return stream
312
313
def open_output_stream(fname)
Definition: utilities.py:298

Referenced by CellMLToNektar.translators.run(), and CellMLToNektar.validator.run().

◆ prid()

def CellMLToNektar.utilities.prid (   obj,
  show_cls = False 
)
Get the id of an object as a hex string, optionally with its class/type.

Definition at line 273 of file utilities.py.

273def prid(obj, show_cls=False):
274 """Get the id of an object as a hex string, optionally with its class/type."""
275 if obj is None:
276 s = 'None'
277 else:
278 s = hex(id(obj))
279 if show_cls:
280 s += str(type(obj))
281 return s
282
283
def prid(obj, show_cls=False)
Definition: utilities.py:273

Referenced by CellMLToNektar.pycml.mathml.clone().

Variable Documentation

◆ __all__

list CellMLToNektar.utilities.__all__
private
Initial value:
1= ['OnlyWarningsFilter', 'OnlyDebugFilter', 'OnlyTheseSourcesFilter', 'NotifyHandler',
2 'DEBUG', 'LOG',
3 'Colourable', 'DFS', 'Sentinel', 'unitary_iterator',
4 'amara_parse', 'element_path', 'element_path_cmp', 'element_xpath', 'brief_xml',
5 'call_if', 'max_i', 'prid', 'add_dicts',
6 'open_output_stream', 'close_output_stream']

Definition at line 45 of file utilities.py.

◆ DFS

CellMLToNektar.utilities.DFS = Enum('White', 'Gray', 'Black')
                                                  #

Miscellaneous classes # #

Definition at line 121 of file utilities.py.