Skip to content

Commit

Permalink
Merge pull request #50 from sparsh-989/Template_Support
Browse files Browse the repository at this point in the history
Template support
  • Loading branch information
mstimberg authored Aug 21, 2024
2 parents 178eed8 + cf98e96 commit 28f7425
Show file tree
Hide file tree
Showing 21 changed files with 276 additions and 358 deletions.
376 changes: 75 additions & 301 deletions brian2tools/mdexport/expander.py

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions brian2tools/mdexport/mdexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MdExporter(BaseExporter):
"""

def build(self, direct_call=True, debug=False, expander=None,
filename=None, additional_formats=None, template_type=None):
filename=None, additional_formats=None, template_name='default', template_dir=None):
"""
Build the exporter
Expand All @@ -41,7 +41,7 @@ def build(self, direct_call=True, debug=False, expander=None,
can specify them under this variable and the options are pdf,
latex, html and all.
template_type : str
template_name : str
Based on your selected template, it will rendered otherwise
a default template will be used for rendering
"""
Expand Down Expand Up @@ -79,8 +79,12 @@ def build(self, direct_call=True, debug=False, expander=None,
# default standard md expander
self.expander = MdExpander()

if template_dir is not None:
self.expander.set_temp_dir_path(template_dir)


# start creating markdown descriptions using expander
self.md_text = self.expander.create_md_string(self.runs, template_type)
self.md_text = self.expander.create_md_string(self.runs, template_name)

# check output filename
if filename:
Expand Down
54 changes: 0 additions & 54 deletions brian2tools/mdexport/template.py

This file was deleted.

7 changes: 7 additions & 0 deletions brian2tools/templates/EventMonitor-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Monitors variable{{ expander.check_plural(group['variables']) }}: {%- for var in group['variables'] -%}
{{ expander.render_expression(var) }}{%- if not loop.last -%}, {%- endif -%}
{%- endfor %} of {{ expander.expand_SpikeSource(group['source']) }} {%- if group['record'] is boolean -%}
{%- if group['record'] -%} for all members {%- else -%} for no member {%- endif -%}
{%- else -%}
{%- if group['record']|length == 0 -%} for no member {%- else -%} for member{{ expander.check_plural(group['record']) }}: {{ group['record'] | join(', ') }} {%- endif -%}
{%- endif -%} when event **{{ group['event'] }}** is triggered
10 changes: 10 additions & 0 deletions brian2tools/templates/EventMonitor-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
| **Monitors** | **Variables** | **Source** | **Record** | **Event** |
|-----------------|------------------------------------------------------------------------------------------------------------|-----------------------------------------------------|------------------------------------------------------------------------------------------------------------|-------------------------------------|
| Monitors variable{{ expander.check_plural(group['variables']) }}: {%- for var in group['variables'] -%}
{{ expander.render_expression(var) }}{%- if not loop.last -%}, {%- endif -%}
{%- endfor %} | of {{ expander.expand_SpikeSource(group['source']) }} |
{%- if group['record'] is boolean -%}
{%- if group['record'] -%} for all members {%- else -%} for no member {%- endif -%}
{%- else -%}
{%- if group['record']|length == 0 -%} for no member {%- else -%} for member{{ expander.check_plural(group['record']) }}: {{ group['record'] | join(', ') }} {%- endif -%}
{%- endif -%} | when event **{{ group['event'] }}** is triggered |
30 changes: 30 additions & 0 deletions brian2tools/templates/NeuronGroup-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Network details
**Neuron population:**
Group {{ (group['name']) }}, consisting of {{ (group['N']) }} neurons.
{{ tab }}{{ ('Model dynamics:') }}
{{ expander.expand_equations(group['equations']) }}
{% if group['user_method'] %}
{{ tab }}The equations are integrated with the '{{ group['user_method'] }}' method.
{% endif %}
{% if 'events' in group %}
{{ tab }}{{ ('Events:') }}
{{ expander.expand_events(group['events']) }}
{% endif %}
{% if 'identifiers' in group %}
{{ tab }}{{ ('Constants:') }} {{ expander.expand_identifiers(group['identifiers']) }}
{% endif %}
{% if not expander.keep_initializer_order and 'initializer' in group and group['initializer']|length %}
{{ tab }}{{ ('Initial values:') }}
{% for initializer in group['initializer'] %}
{{ tab }}* {{ expander.expand_initializer(initializer) }}
{% endfor %}
{% endif %}
{% if 'run_regularly' in group %}
{{ tab }}{{ ('Run regularly') }}{{ expander.check_plural(group['run_regularly']) }}:
{% for run_reg in group['run_regularly'] %}
{{ expander.expand_runregularly(run_reg) }}
{% endfor %}
{% endif %}



8 changes: 8 additions & 0 deletions brian2tools/templates/NeuronGroup-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
|-------------------------------|------------------------------------------------|
| **Neuron population** | Group {{ (group['name']) }}, consisting of {{ (group['N']) }} neurons. |
| **Model dynamics** | {{ expander.expand_equations(group['equations']) }} |
| **Integration method** | {% if group['user_method'] %} The equations are integrated with the '{{ group['user_method'] }}' method. {% endif %} |
| **Events** (if present) | {% if 'events' in group %} {{ expander.expand_events(group['events']) }} {% endif %} |
| **Constants** (if present) | {% if 'identifiers' in group %} {{ expander.expand_identifiers(group['identifiers']) }} {% endif %} |
| **Initial values** (if present)| {% if not expander.keep_initializer_order and 'initializer' in group and group['initializer']|length %} {% for initializer in group['initializer'] %} {{ initializer.variable }}: {{ initializer.value }}{% if initializer.unit %} [{{ initializer.unit }}]{% endif %} {% if not loop.last %}\n{% endif %}{% endfor %} {% endif %} |
| **Run regularly** (if present)| {% if 'run_regularly' in group %} {% for run_reg in group['run_regularly'] %} {{ expander.expand_runregularly(run_reg) }} {% if not loop.last %}\n{% endif %}{% endfor %} {% endif %} |
17 changes: 17 additions & 0 deletions brian2tools/templates/PoissonGroup-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Name {{ group['name'] }} of population size {{ group['N'] }} and rate as {{ expander.render_expression(group['rates']) }}.

{% if group.get('identifiers', None) %}
Constants:
{{ expander.expand_identifiers(group['identifiers']) }}
{% endif %}

{% if group.get('run_regularly', None) %}
Run regularly:
{% for run_reg in group['run_regularly'] %}
{{ expander.expand_runregularly(run_reg) }}
{% endfor %}
{% endif %}


7 changes: 7 additions & 0 deletions brian2tools/templates/PoissonGroup-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| **Variable** | **Value** |
|---------------------------------|------------------------------------------------|
| **Name** | {{ group['name'] }} |
| **Population size** | {{ group['N'] }} |
| **Rate** | {{ expander.render_expression(group['rates']) }} |
| **Constants** (if present) | {% if group.get('identifiers', None) %} {{ expander.expand_identifiers(group['identifiers']) }} {% endif %} |
| **Run regularly** (if present) | {% if group.get('run_regularly', None) %} {% for run_reg in group['run_regularly'] %} {{ expander.expand_runregularly(run_reg) }} {% endfor %} {% endif %} |
10 changes: 10 additions & 0 deletions brian2tools/templates/PoissonInput-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{# Jinja2 template for PoissonInput in a simple text format #}

PoissonInput with size {{ group['N'] }} gives input to variable {{ expander.render_expression(group['target_var']) }} with rate {{ expander.render_expression(group['rate']) }} and weight of {{ expander.render_expression(group['weight']) }}.

{% if 'identifiers' in group %}
**Constants:**
{% for identifier, value in group['identifiers'].items() %}
- {{ identifier }}: {{ value }}
{% endfor %}
{% endif %}
8 changes: 8 additions & 0 deletions brian2tools/templates/PoissonInput-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{# Jinja2 template for PoissonInput in table format #}

|-------------------------------|------------------------------------------------|
| **PoissonInput Size** | {{ group['N'] }} |
| **Target Variable** | {{ expander.render_expression(group['target_var']) }} |
| **Rate** | {{ expander.render_expression(group['rate']) }} |
| **Weight** | {{ expander.render_expression(group['weight']) }} |
| **Constants** (if present) | {% if 'identifiers' in group %} {% for identifier, value in group['identifiers'].items() %} {{ identifier }}: {{ value }} {% if not loop.last %}\n{% endif %}{% endfor %} {% endif %} |
5 changes: 5 additions & 0 deletions brian2tools/templates/PopulationRateMonitor-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{# Jinja2 template for PopRateMonitor in simple text format #}

Monitors the population of {{ expander.expand_SpikeSource(group['source']) }}.

{{ endll }}
4 changes: 4 additions & 0 deletions brian2tools/templates/PopulationRateMonitor-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{# Jinja2 template for PopRateMonitor in table format #}

|-------------------------------|--------------------------------------------------|
| **Monitored Population** | {{ expander.expand_SpikeSource(group['source']) }} |
18 changes: 18 additions & 0 deletions brian2tools/templates/SpikeGeneratorGroup-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{# Basic information about the SpikeGeneratorGroup #}
{{ tab }}Name {{ (group['name']) }},
with population size {{ (group['N']) }},
has neuron{{ 's' if group['indices']|length > 1 else '' }}:
{{ expander.prepare_array(group['indices']) }}
that spike at times
{{ expander.prepare_array(group['times']) }},
with period {{ group['period'] }}.
{{ endll }}

{# Check for the 'run_regularly' key and expand if it exists #}
{% if 'run_regularly' in group %}
{{ tab }}{{ ('Run regularly:') }}
{{ endll }}
{% for run_reg in group['run_regularly'] %}
{{ expander.expand_runregularly(run_reg) }}
{% endfor %}
{% endif %}
9 changes: 9 additions & 0 deletions brian2tools/templates/SpikeGeneratorGroup-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{# Jinja2 template for SpikeGeneratorGroup in table format #}

|-------------------------------|------------------------------------------------|
| **Name** | {{ group['name'] }} |
| **Population Size** | {{ group['N'] }} |
| **Neurons** | {{ expander.prepare_array(group['indices']) }} |
| **Spike Times** | {{ expander.prepare_array(group['times']) }} |
| **Period** | {{ group['period'] }} |
| **Run Regularly** (if present) | {% if 'run_regularly' in group %} {% for run_reg in group['run_regularly'] %} {{ expander.expand_runregularly(run_reg) }}{% if not loop.last %}\n{% endif %}{% endfor %} {% endif %} |
1 change: 1 addition & 0 deletions brian2tools/templates/SpikeMonitor-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{%extends "EventMonitor-default.md" %}
1 change: 1 addition & 0 deletions brian2tools/templates/SpikeMonitor-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{%extends "EventMonitor-table.md" %}
9 changes: 9 additions & 0 deletions brian2tools/templates/StateMonitor-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{# Jinja2 template for Statemon in simple text format #}
Monitors variable{{ expander.check_plural(group['variables']) }}: {%- for var in group['variables'] -%}
{{ expander.render_expression(var) }}{%- if not loop.last -%}, {%- endif -%}
{%- endfor %} of {{ expander.expand_SpikeSource(group['source']) }} {%- if group['record'] is boolean -%}
{%- if group['record'] -%} for all members {%- else -%} for no member {%- endif -%}
{%- else -%}
{%- if group['record']|length == 0 -%} for no member {%- else -%} for member{{ expander.check_plural(group['record']) }}: {{ group['record'] | join(', ') }} {%- endif -%}
{%- endif -%}

10 changes: 10 additions & 0 deletions brian2tools/templates/StateMonitor-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
| **Monitors** | **Variables** | **Source** | **Record** |
|-----------------|------------------------------------------------------------------------------------------------------------|-----------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| Monitors variable{{ expander.check_plural(group['variables']) }}: {%- for var in group['variables'] -%}
{{ expander.render_expression(var) }}{%- if not loop.last -%}, {%- endif -%}
{%- endfor %} | of {{ expander.expand_SpikeSource(group['source']) }} |
{%- if group['record'] is boolean -%}
{%- if group['record'] -%} for all members {%- else -%} for no member {%- endif -%}
{%- else -%}
{%- if group['record']|length == 0 -%} for no member {%- else -%} for member{{ expander.check_plural(group['record']) }}: {{ group['record'] | join(', ') }} {%- endif -%}
{%- endif -%} |
31 changes: 31 additions & 0 deletions brian2tools/templates/Synapses-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Connections {{ group['name'] }}, connecting {{ expander.expand_SpikeSource(group['source']) }} to {{ expander.expand_SpikeSource(group['target']) }}
{% if not expander.keep_initializer_order and 'connectors' in group and group['connectors']|length -%}
{{ tab }}{{ expander.expand_connector(group['connectors'][0]) }}
{% endif %}
{% if 'equations' in group -%}
{{ tab }}{{ bold('Model dynamics:') }}
{{ expander.expand_equations(group['equations']) }}
{% if 'user_method' in group -%}
{{ tab }}The equations are integrated with the '{{ group['user_method'] }}' method.{{ endll }}
{% endif %}
{% endif %}
{% if 'pathways' in group -%}
{{ expander.expand_pathways(group['pathways']) }}
{% if 'equations' not in group and 'identifiers' in group -%}
{{ tab }}, where {{ expander.expand_identifiers(group['identifiers']) }}.
{% endif %}
{% endif %}
{% if 'summed_variables' in group -%}
{{ tab }}{{ bold('Summed variables:') }}
{{ expander.expand_summed_variables(group['summed_variables']) }}
{% endif %}
{% if 'identifiers' in group and 'equations' in group -%}
{{ tab }}{{ bold('Constants:') }} {{ expander.expand_identifiers(group['identifiers']) }}
{% endif %}
{% if not expander.keep_initializer_order and 'initializer' in group and group['initializer']|length -%}
{{ tab }}{{ bold('Initial values:') }}
{% for initializer in group['initializer'] -%}
{{ tab }}* {{ expander.expand_initializer(initializer) }}
{% endfor %}
{% endif %}

9 changes: 9 additions & 0 deletions brian2tools/templates/Synapses-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
|-------------------------------|------------------------------------------------|
| **Connections** | {{ group['name'] }}, connecting {{ expander.expand_SpikeSource(group['source']) }} to {{ expander.expand_SpikeSource(group['target']) }} |
| **Connector** (if present) | {% if not expander.keep_initializer_order and 'connectors' in group and group['connectors']|length %} {{ expander.expand_connector(group['connectors'][0]) }} {% endif %} |
| **Model dynamics** (if present)| {% if 'equations' in group %} {% for key, eqn in group['equations'].items() %} $\frac{d}{d t} {{ key }}$ = {{ eqn.expr }}{% if eqn.unit %} [{{ eqn.unit }}]{% endif %} {% if not loop.last %}\n{% endif %}{% endfor %} {% endif %} |
| **Integration method** (if present) | {% if 'user_method' in group %} The equations are integrated with the '{{ group['user_method'] }}' method. {% endif %} |
| **Pathways** (if present) | {% if 'pathways' in group %} {{ expander.expand_pathways(group['pathways']) }} {% if 'equations' not in group and 'identifiers' in group %} , where {{ expander.expand_identifiers(group['identifiers']) }}. {% endif %} {% endif %} |
| **Summed variables** (if present) | {% if 'summed_variables' in group %} {{ expander.expand_summed_variables(group['summed_variables']) }} {% endif %} |
| **Constants** (if present) | {% if 'identifiers' in group and 'equations' in group %} {% for identifier, value in group['identifiers'].items() %} {{ identifier }}: {{ value }} {% if not loop.last %}\n{% endif %}{% endfor %} {% endif %} |
| **Initial values** (if present) | {% if not expander.keep_initializer_order and 'initializer' in group and group['initializer']|length %} {% for initializer in group['initializer'] %} {{ initializer.variable }}: {{ initializer.value }}{% if initializer.unit %} [{{ initializer.unit }}]{% endif %} {% if not loop.last %}\n{% endif %}{% endfor %} {% endif %} |

0 comments on commit 28f7425

Please sign in to comment.