nmodl icon indicating copy to clipboard operation
nmodl copied to clipboard

Support for Random number generator as language constructs

Open pramodk opened this issue 5 years ago • 7 comments

  • Proposal for Random number generators as language constructs (instead of VERBATIM blocks)
  • Review use cases from BBP models as well as ModelDB
  • Present this during next NEURON meeting
  • Start this implement in NMODL (and also discuss with Michael Hines about NEURON implementation)

@ohm314 : During Yale visit last year we put together a draft PR (or temporary branch). Could you link that here?

Also ping Sam Yates on this topic as well

pramodk avatar Aug 02 '20 22:08 pramodk

There was a very early proposal described in an issue in neuronsimulator/nrn: https://github.com/neuronsimulator/nrn/issues/239

ohm314 avatar Aug 04 '20 12:08 ohm314

To my shame I was unaware of neuronsimulator/nrn#239 . It looks very good and very clean. No VERBATIM blocks remain! And I think similar things could be done with

VECTOR vec

This could be implemented in nocmodl but if new nmodl is coming to NEURON sooner rather than later...

nrnhines avatar Aug 04 '20 12:08 nrnhines

The ticket on the neuron repo has been quite a bit extended with lots of details towards having a complete specification.

As a first step I will try to add the parsing of such declarations

RANDOM DISTRIBUTION(param_1, ..., param_n) varname_1, ..., varname_m

ohm314 avatar Jan 26 '21 14:01 ohm314

Some implementation notes for the discussion:

Considering the example:

RANDOM UNIFORM(0.0, 1.0) ur1, ur2
  • The whole line represent RandomStatement.
  • ur1 and ur2 are random variables.
  • We add following AST nodes
    • RandomStatement : Statement
      • distribution_type : Name
      • distribution_range : ExpressionVector; vector: True
      • variables : RandomVarVector; vector : True
    • RandomVar : Identifier
      • name : Name; node_name: true
  • Bison production will be something like
    euron_statement RANDOM NAME_PTR "(" expression_list ")" random_var_list
    
  • In RandomVar we are not storing any information about the distribution type etc.
  • Arguments checking etc will be done in separate semantic check visitor
    • we want to check if distributiob type and it's arguments are mathcing
    • such checks can be added in separate semantic visitor instead of trying to achieve everything via Bison grammar. This way we don't need to check lexer/parser if we add new distribution type.
  • During the code generation, we will need to know the information about distribution type etc
    • Similar use cases we handle with CodegenInfo structure i.e. we can store information about different variables, their types, ranges etc. in some map or structure. we don't need to put all information in the AST nodes

pramodk avatar Mar 23 '21 08:03 pramodk

Seems sufficient to begin an implementation. Note that I presume this will NOT make use of the interpreter Random. Currently, nrnran123.h supports

DiscreteUniform( 0 to 2**32 - 1)
Uniform(0.0 to 1.0)  (I'm not sure but I think there are either  2**32  or (2**32 - 1)distinct values)
    /* nrnran123_dblpick minimum value is 2.3283064e-10 and max value is 1-min */
NegExp (mean 1.0)
   /* nrnran123_negexp min value is 2.3283064e-10, max is 22.18071 */
Normal (mean 0.0, stddev 1.0)

nrnhines avatar Mar 23 '21 12:03 nrnhines

Correct, this will not use interpreter random, but the distribution arguments can be set via parameters. Thanks for sharing the available distributions in random123, then we'll make sure to support those directly. Actually a start of an implementation is already in a branch. Will report back when this gets a bit further along...

ohm314 avatar Mar 25 '21 06:03 ohm314

Here's also a gist with example code the way it could look like when generated: https://gist.github.com/ohm314/1d47a6cfecc6f71d238161358aaf59d1

ohm314 avatar Sep 20 '23 12:09 ohm314

This has been implemented.

1uc avatar Nov 13 '24 12:11 1uc