Skip to content
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

Parsing optional statements in model interface. #158

Merged
merged 67 commits into from
Apr 28, 2020
Merged

Conversation

MichaelClerx
Copy link
Contributor

@MichaelClerx MichaelClerx commented Apr 9, 2020

Addresses #157. Fixes #105.

Significant rewrite of ModelInterface.modify_model(), based on #157 (comment)

  • Before modifications begin, information from inputs, outputs, defines, clamp-tos, and var statements is collated and stored in ProtocolVariable objects.
  • Where possible, ProtocolVariables are resolved to model variables, and sanity-checking is performed
    • To allow nesting, this is done slightly more leniently than before, e.g. multiple input declarations are allowed on the same variable, as long as they don't make conflicting demands
  • Unit conversion of inputs is slightly rewritten, but more or less the same: I wanted to do outputs here too but this caused more bookkeeping due to transitive variables & the state_variable annotation
  • Variable creation requires units, units must sometimes be inferred from the RHS, and this requires the RHS in sympy which requires all RHS variables to exist. To deal with this situation, creating and modifying variables is now performed in a single method, and in several passes. The system is deemed irresolvable if a single pass is unable to make any progress.

@MichaelClerx MichaelClerx force-pushed the 157-optionals branch 2 times, most recently from ae3105c to 8711791 Compare April 21, 2020 18:13
…enarios where units are inferred from the RHS for some new variables.
@MichaelClerx MichaelClerx changed the base branch from 154-var-declarations to master April 22, 2020 01:13
@MichaelClerx MichaelClerx marked this pull request as ready for review April 22, 2020 01:13
@MichaelClerx MichaelClerx requested a review from jonc125 April 22, 2020 01:20
@MichaelClerx
Copy link
Contributor Author

MichaelClerx commented Apr 22, 2020

The s1_s2_noble test causes an error in cellmlmanip, where the to_unit becomes None but a conversion is still triggered, seems to go wrong here:

https://github.com/ModellingWebLab/cellmlmanip/blob/877918347d9d9e7be08692ba5bcc01780175eb71/cellmlmanip/units.py#L775-L778

if to_units is not None or was_converted:
  # Convert the result if required
  expr = expr.func(*new_args)
  expr, was_converted, actual_units = maybe_convert_expr(expr, was_converted, actual_units, to_units)

These lines do look wrong to me: maybe_convert_expr() can be called with to_units=None which seems to cause this error?

@MichaelClerx
Copy link
Contributor Author

Stopped this from happening now, but the lines above still look dubious to me @jonc125 :D

@MichaelClerx MichaelClerx self-assigned this Apr 22, 2020
@MichaelClerx
Copy link
Contributor Author

MichaelClerx commented Apr 22, 2020

I think the bit with the output variables can be tidied up further:

  • make get_variables_transitively return variables + terms, apply this to outputs in collate to create ProtocolVariables for these outputs
  • stop calling get_variables_transitively in _convert_output_units (now becomes a simple loop)
  • store outputs better, and use this in protocol.py to pass to code generation, stop calling get_variables_transitively in code_generation.py
  • presumably the rule is "If a non-optional output doesn't resolve directly, but does resolve transitively, then that's fine."?

@MichaelClerx
Copy link
Contributor Author

Wondering if ModelInterface._expr and ModelInterface.merge can be simplified, and we no longer need to store all the base actions for inputs/outputs/optional etc.? (At least not separately)
Also duplicates would be fine in current code, so merge can be easier?

@MichaelClerx MichaelClerx changed the title Started on parsing optional statements in model interface. Parsing optional statements in model interface. Apr 22, 2020
jonc125 added a commit to ModellingWebLab/cellmlmanip that referenced this pull request Apr 22, 2020
Copy link
Contributor

@jonc125 jonc125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good overall, and does tidy things up nicely. There are some instances of broken code suggesting that we're missing some important test coverage, and a couple of bits I'm not sure about. Plus a few cosmetic changes :)

fc/parsing/actions.py Outdated Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
test/protocols/test_interface_inconsistent_clamp_1.txt Outdated Show resolved Hide resolved
test/test_model_interface.py Outdated Show resolved Hide resolved
@MichaelClerx
Copy link
Contributor Author

MichaelClerx commented Apr 28, 2020

Ready for final(?) review @jonc125

@MichaelClerx
Copy link
Contributor Author

@jonc125 optional outputs?

Copy link
Contributor

@jonc125 jonc125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I've just suggested a few minor tweaks.

test/protocols/test_create_annotated.txt Outdated Show resolved Hide resolved
test/protocols/test_create_annotated_no_units.txt Outdated Show resolved Hide resolved
fc/parsing/actions.py Show resolved Hide resolved
fc/parsing/actions.py Show resolved Hide resolved
fc/parsing/actions.py Outdated Show resolved Hide resolved
@jonc125
Copy link
Contributor

jonc125 commented Apr 28, 2020

@jonc125 optional outputs?

I thought they would have been removed by this point? Maybe not. No tests failed though!

@MichaelClerx
Copy link
Contributor Author

I think the protocol_variables list just gives all the information about variables specified in the protocol, including optional ones that couldn't be resolved. So probably if we add a test with an optional output that's also optional in post-processing this could fail?

@jonc125
Copy link
Contributor

jonc125 commented Apr 28, 2020

Quite possibly. In which case please do revert that change! (Perhaps with a comment explaining the situation.)

@MichaelClerx
Copy link
Contributor Author

I agree it's a very ugly if-block 😅

@MichaelClerx
Copy link
Contributor Author

Fixed now?

Copy link
Contributor

@jonc125 jonc125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@jonc125
Copy link
Contributor

jonc125 commented Apr 28, 2020

Huh, except that the assertion keeps tripping?

@MichaelClerx
Copy link
Contributor Author

I forgot to set is_vector on the magic state_variable annotation. Fixed now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consistency checks for model interface
3 participants