From 482ef6acdecedb21e95ea124b7ba47604fd4a95f Mon Sep 17 00:00:00 2001 From: realanu0812 Date: Thu, 26 Mar 2026 16:50:27 +0530 Subject: [PATCH 1/3] Improve rand() initializer annotations --- brian2tools/mdexport/expander.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/brian2tools/mdexport/expander.py b/brian2tools/mdexport/expander.py index 75085d43..88a769e0 100644 --- a/brian2tools/mdexport/expander.py +++ b/brian2tools/mdexport/expander.py @@ -700,24 +700,27 @@ def expand_initializer(self, initializer): init_str += self.render_expression(initializer['value']) # not a good checking - if (isinstance(initializer['index'], str) and - (initializer['index'] != 'True' and initializer['index'] != 'False')): - init_str += ' if ' + self.render_expression(initializer['index']) - elif (isinstance(initializer['index'], bool) or - (initializer['index'] == 'True' or - initializer['index'] == 'False')): - if initializer['index'] is True or initializer['index'] == 'True': + index_value = initializer['index'] + if isinstance(index_value, str): + if index_value not in ('True', 'False'): + init_str += ' if ' + self.render_expression(index_value) + elif index_value == 'True': + init_str += '' # "to all members" implied + else: + raise AssertionError('Initialization with \'False\' as index?') + elif isinstance(index_value, bool): + if index_value: init_str += '' # "to all members" implied else: raise AssertionError('Initialization with \'False\' as index?') else: init_str += (' to member' + - self.check_plural(initializer['index']) + ' ') - if not hasattr(initializer['index'], '__iter___'): - init_str += str(initializer['index']) + self.check_plural(index_value) + ' ') + if not hasattr(index_value, '__iter___'): + init_str += str(index_value) else: init_str += ','.join( - [str(ind) for ind in initializer['index']] + [str(ind) for ind in index_value] ) if 'identifiers' in initializer: init_str += (', where ' + self.expand_identifiers(initializer['identifiers']) + '.') From adf38fb5f8d822ee3f207b20a028799280de5bbe Mon Sep 17 00:00:00 2001 From: realanu0812 Date: Thu, 26 Mar 2026 16:58:35 +0530 Subject: [PATCH 2/3] Add rand() initializer annotation --- brian2tools/mdexport/expander.py | 38 ++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/brian2tools/mdexport/expander.py b/brian2tools/mdexport/expander.py index 88a769e0..fd34b09e 100644 --- a/brian2tools/mdexport/expander.py +++ b/brian2tools/mdexport/expander.py @@ -20,7 +20,7 @@ select_autoescape, ) from markdown_strings import * -from sympy import Derivative, symbols +from sympy import Derivative, Symbol, symbols from sympy.abc import * from sympy.printing import latex @@ -304,6 +304,35 @@ def render_expression(self, expression, differential=False): # to remove `$` (in most md compiler single $ is used) return rend_exp[1:][:-1] + def _rand_uniform_annotation(self, expression): + """ + Return a simple Uniform(lower, upper) annotation for expressions + containing exactly one rand() call. Otherwise return None. + """ + if not isinstance(expression, str): + return None + + if expression.count('rand()') != 1: + return None + + if 'randn()' in expression: + return None + + try: + expr_str = expression.replace('rand()', 'RANDX') + sym_expr = str_to_sympy(expr_str) + rand_sym = Symbol('RANDX') + + lower = sym_expr.subs(rand_sym, 0) + upper = sym_expr.subs(rand_sym, 1) + + lower_str = self.render_expression(str(lower)) + upper_str = self.render_expression(str(upper)) + + return f' (approximately Uniform({lower_str}, {upper_str}))' + except Exception: + return None + def create_md_string(self, net_dict, template_name): """ Create markdown text by checking the standard dictionary and call @@ -697,7 +726,12 @@ def expand_initializer(self, initializer): ' initialized with ') else: init_str += '= ' - init_str += self.render_expression(initializer['value']) + rendered_value = self.render_expression(initializer['value']) + init_str += rendered_value + + annotation = self._rand_uniform_annotation(initializer['value']) + if annotation is not None: + init_str += annotation # not a good checking index_value = initializer['index'] From 5c8eebca794b6b1e05a51a70fc65a0b3b577a9e8 Mon Sep 17 00:00:00 2001 From: realanu0812 Date: Tue, 7 Apr 2026 23:36:50 +0530 Subject: [PATCH 3/3] Fix rand() annotation bound substitution --- brian2tools/mdexport/expander.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/brian2tools/mdexport/expander.py b/brian2tools/mdexport/expander.py index 2f5431d0..d5bcd19f 100644 --- a/brian2tools/mdexport/expander.py +++ b/brian2tools/mdexport/expander.py @@ -321,13 +321,18 @@ def _rand_uniform_annotation(self, expression): try: expr_str = expression.replace('rand()', 'RANDX') sym_expr = str_to_sympy(expr_str) - rand_sym = Symbol('RANDX') + rand_symbols = [symbol for symbol in sym_expr.free_symbols + if str(symbol) == 'RANDX'] + if len(rand_symbols) != 1: + return None + + rand_sym = rand_symbols[0] lower = sym_expr.subs(rand_sym, 0) upper = sym_expr.subs(rand_sym, 1) - lower_str = self.render_expression(str(lower)) - upper_str = self.render_expression(str(upper)) + lower_str = self.render_expression(lower) + upper_str = self.render_expression(upper) return f' (approximately Uniform({lower_str}, {upper_str}))' except Exception: