Skip to content

Commit

Permalink
Merge pull request #51 from sparsh-989/spatialneuron
Browse files Browse the repository at this point in the history
Change for adaption of equations of spatial neuron except morphology
  • Loading branch information
mstimberg authored Aug 23, 2024
2 parents edf0432 + 3224d66 commit 9ba2402
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 1 deletion.
90 changes: 90 additions & 0 deletions brian2tools/baseexport/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,96 @@ def collect_NeuronGroup(group, run_namespace):

return neuron_dict

def collect_SpatialNeuron(group, run_namespace):

"""
Collect information from `brian2.spatialneuron.spatialneuron.SpatialNeuron`
and return them in a dictionary format
Parameters
----------
group : brian2.spatialneuron.spatialneuron.SpatialNeuron
SpatialNeuron object
run_namespace : dict
Namespace dictionary
Returns
-------
neuron_dict : dict
Dictionary with extracted information
"""


neuron_dict = {}

# identifiers belonging to the NeuronGroup
identifiers = set()

# get name
neuron_dict['name'] = group.name

# get size
neuron_dict['N'] = group._N

# get user defined stateupdation method
if isinstance(group.method_choice, str):
neuron_dict['user_method'] = group.method_choice
# if not specified by user
# TODO collect from run time
else:
neuron_dict['user_method'] = None

# get equations
neuron_dict['equations'] = collect_Equations(group.user_equations)
identifiers = identifiers | group.user_equations.identifiers

neuron_dict['morphology'] = {}
if group is not None and group.morphology is not None:
morphology = group.morphology
neuron_dict["morphology"]["total_compartments"] = group.N
area_variable = group.area.get_item(group)
if isinstance(area_variable, np.ndarray):
neuron_dict["morphology"]["area"] = np.sum(area_variable)
neuron_dict["morphology"]["total_sections"] = morphology.total_sections


# check spike event is defined
if group.events:
neuron_dict['events'], event_identifiers = collect_Events(group)
identifiers = identifiers | event_identifiers

# check any `run_regularly` / CodeRunner objects associated
for obj in group.contained_objects:
# Note: Thresholder, StateUpdater, Resetter are all derived from
# CodeRunner, so to identify `run_regularly` object we use type()
if type(obj) == CodeRunner:
if 'run_regularly' not in neuron_dict:
neuron_dict['run_regularly'] = []
neuron_dict['run_regularly'].append({
'name': obj.name,
'code': obj.abstract_code,
'dt': obj.clock.dt,
'when': obj.when,
'order': obj.order
})
identifiers = identifiers | get_identifiers(obj.abstract_code)

# check StateUpdater when/order and assign to group level
if isinstance(obj, StateUpdater):
neuron_dict['when'] = obj.when
neuron_dict['order'] = obj.order

# resolve group-specific identifiers
identifiers = group.resolve_all(identifiers, run_namespace)
# with the identifiers connected to group, prune away unwanted
identifiers = _prepare_identifiers(identifiers)
# check the dictionary is not empty
if identifiers:
neuron_dict['identifiers'] = identifiers

return neuron_dict


def collect_Equations(equations):
"""
Expand Down
4 changes: 3 additions & 1 deletion brian2tools/baseexport/device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from brian2.core.variables import DynamicArrayVariable
from brian2.devices.device import RuntimeDevice, Device, all_devices
from brian2.groups import NeuronGroup
from brian2.spatialneuron import SpatialNeuron
from brian2.input import PoissonGroup, SpikeGeneratorGroup
from brian2 import (get_local_namespace, StateMonitor, SpikeMonitor,
EventMonitor, PopulationRateMonitor, Synapses,
Expand Down Expand Up @@ -53,7 +54,7 @@ def __init__(self):
self.build_on_run = True
self.has_been_run = False
self.build_options = None
self.supported_objs = (NeuronGroup, SpikeGeneratorGroup,
self.supported_objs = (NeuronGroup, SpatialNeuron, SpikeGeneratorGroup,
PoissonGroup, StateMonitor, SpikeMonitor,
EventMonitor, PopulationRateMonitor, Synapses,
PoissonInput)
Expand Down Expand Up @@ -139,6 +140,7 @@ def network_run(self, network, duration, namespace=None, level=0, **kwds):

# dictionary to store objects and its collector functions
collector_map={'neurongroup': {'f': collect_NeuronGroup, 'n': True},
'spatialneuron': {'f': collect_SpatialNeuron, 'n': True},
'poissongroup': {'f': collect_PoissonGroup, 'n': True},
'spikegeneratorgroup': {'f': collect_SpikeGenerator,
'n': True},
Expand Down
5 changes: 5 additions & 0 deletions brian2tools/mdexport/expander.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ def create_md_string(self, net_dict, template_name):
"h": "Neuron population",
"order": 1,
},
"spatialneuron": {
"hb": "SpatialNeuron",
"h": "Neuron population",
"order": 1
},
"poissongroup": {
"hb": "PoissonGroup",
"h": "Poisson spike source",
Expand Down
43 changes: 43 additions & 0 deletions brian2tools/templates/SpatialNeuron-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Network details
**Neuron population:**
Group {{ (group['name']) }}, consisting of {{ (group['N']) }} neurons.
{{ tab }}{{ ('Model dynamics:') }}
{{ expander.expand_equations(group['equations']) }}
# **Morphology:**

{% if 'area' in group['morphology'] %}
Area: {{ group['morphology']['area'] }}
{% endif %}

{% if 'total_sections' in group['morphology'] %}
Total Sections: {{ group['morphology']['total_sections'] }}
{% endif %}

{% if 'total_compartments' in group['morphology'] %}
Total Compartments: {{ group['morphology']['total_compartments'] }}
{% endif %}
{% 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/SpatialNeuron-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 %} |

0 comments on commit 9ba2402

Please sign in to comment.