-
Notifications
You must be signed in to change notification settings - Fork 1
Model.add_number and add_variable now allow unit objects as well as strings #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2d5f40b
585044f
b1faa7f
2f08502
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,19 +92,24 @@ def add_number(self, value, units): | |
| Creates and returns a :class:`NumberDummy` to represent a number with units in sympy expressions. | ||
|
|
||
| :param number: A number (anything convertible to float). | ||
| :param units: A string unit representation. | ||
| :param units: A `pint` units representation | ||
|
|
||
| :return: A :class:`NumberDummy` object. | ||
| """ | ||
| return NumberDummy(value, self.units.get_quantity(units)) | ||
|
|
||
| # Check units | ||
| if not isinstance(units, self.units.ureg.Unit): | ||
| units = self.units.get_quantity(units) | ||
|
|
||
| return NumberDummy(value, units) | ||
|
|
||
| def add_variable(self, name, units, initial_value=None, | ||
| public_interface=None, private_interface=None, cmeta_id=None): | ||
| """ | ||
| Adds a variable to the model and returns a :class:`VariableDummy` to represent it in sympy expressions. | ||
|
|
||
| :param name: A string name. | ||
| :param units: A string units representation. | ||
| :param units: A `pint` units representation. | ||
| :param initial_value: An optional initial value. | ||
| :param public_interface: An optional public interface specifier (only required when parsing CellML). | ||
| :param private_interface: An optional private interface specifier (only required when parsing CellML). | ||
|
|
@@ -116,10 +121,14 @@ def add_variable(self, name, units, initial_value=None, | |
| if name in self._name_to_symbol: | ||
| raise ValueError('Variable %s already exists.' % name) | ||
|
|
||
| # Check units | ||
| if not isinstance(units, self.units.ureg.Unit): | ||
| units = self.units.get_quantity(units) | ||
|
|
||
| # Add variable | ||
| self._name_to_symbol[name] = var = VariableDummy( | ||
| name=name, | ||
| units=self.units.get_quantity(units), | ||
| units=units, | ||
| initial_value=initial_value, | ||
| public_interface=public_interface, | ||
| private_interface=private_interface, | ||
|
|
@@ -172,7 +181,7 @@ def connect_variables(self, source_name: str, target_name: str): | |
| factor = self.units.convert_to(1 * source.units, target.units).magnitude | ||
|
|
||
| # Dummy to represent this factor in equations, having units for conversion | ||
| factor_dummy = self.add_number(factor, str(target.units / source.units)) | ||
| factor_dummy = self.add_number(factor, target.units / source.units) | ||
|
|
||
| # Add an equations making the connection with the required conversion | ||
| self.equations.append(sympy.Eq(target, source.assigned_to * factor_dummy)) | ||
|
|
@@ -395,6 +404,12 @@ def get_ontology_terms_by_symbol(self, symbol, namespace_uri=None): | |
| ontology_terms.append(uri_parts[-1]) | ||
| return ontology_terms | ||
|
|
||
| def get_units(self, name): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use this method above? |
||
| """ | ||
| Looks up and returns a pint `Unit` object with the given name. | ||
| """ | ||
| return self.units.get_quantity(name) | ||
|
|
||
| @property | ||
| def graph(self): | ||
| """ A ``networkx.DiGraph`` containing the model equations. """ | ||
|
|
@@ -456,8 +471,7 @@ def graph(self): | |
| else: | ||
| # this variable is a parameter - add to graph and connect to lhs | ||
| rhs.type = 'parameter' | ||
| unit = rhs.units | ||
| dummy = self.add_number(rhs.initial_value, str(unit)) | ||
| dummy = self.add_number(rhs.initial_value, rhs.units) | ||
| graph.add_node(rhs, equation=sympy.Eq(rhs, dummy), variable_type='parameter') | ||
| graph.add_edge(rhs, lhs) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,10 +250,10 @@ def _define_pint_unit(self, units_name, definition_string_or_instance): | |
| self.custom_defined.add(units_name) | ||
|
|
||
| def get_quantity(self, unit_name): | ||
| """Returns a pint.Unit with the given name from the UnitRegistry. | ||
| """Returns a pint `Unit` with the given name from the UnitRegistry. | ||
| :param unit_name: string name of the unit | ||
| :return: pint.Unit | ||
| throws pint.UndefinedUnitError if te unit is not present in registry | ||
| :return: `Unit` | ||
| throws pint.UndefinedUnitError if the unit is not present in registry | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use the Sphinx |
||
| """ | ||
| try: | ||
| return self.ureg.parse_expression(unit_name).units | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this also accepts strings, should we keep that option in the docs too? Or hide it just for use by our tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and should it be double backticks? My reST is rusty!