Skip to content

Commit

Permalink
Merge pull request #179 from NeuralEnsemble/experimental
Browse files Browse the repository at this point in the history
Use natsort to properly sort segments/groups when optimising
  • Loading branch information
pgleeson authored Sep 19, 2023
2 parents ae16f2b + 7cb0adc commit 55bf626
Show file tree
Hide file tree
Showing 10 changed files with 888 additions and 597 deletions.
110 changes: 110 additions & 0 deletions doc/userdocs/coreclasses_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,17 @@ DecayingPoolConcentrationModel



DerivedParameter
################

.. autoclass:: neuroml.nml.nml.DerivedParameter
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



DerivedVariable
###############

Expand Down Expand Up @@ -715,6 +726,17 @@ ElectricalProjection



EventOut
########

.. autoclass:: neuroml.nml.nml.EventOut
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



ExpCondSynapse
##############

Expand Down Expand Up @@ -1482,6 +1504,50 @@ NeuroMLDocument



OnCondition
###########

.. autoclass:: neuroml.nml.nml.OnCondition
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



OnEntry
#######

.. autoclass:: neuroml.nml.nml.OnEntry
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



OnEvent
#######

.. autoclass:: neuroml.nml.nml.OnEvent
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



OnStart
#######

.. autoclass:: neuroml.nml.nml.OnStart
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



OpenState
#########

Expand Down Expand Up @@ -1691,6 +1757,17 @@ ReactionScheme



Regime
######

.. autoclass:: neuroml.nml.nml.Regime
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



Region
######

Expand Down Expand Up @@ -1955,6 +2032,17 @@ Standalone



StateAssignment
###############

.. autoclass:: neuroml.nml.nml.StateAssignment
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



StateVariable
#############

Expand Down Expand Up @@ -2032,6 +2120,28 @@ TransientPoissonFiringSynapse



Transition
##########

.. autoclass:: neuroml.nml.nml.Transition
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



TrueOrFalse
###########

.. autoclass:: neuroml.nml.nml.TrueOrFalse
:members:
:undoc-members:
:show-inheritance:
:inherited-members:



UnstructuredLayout
##################

Expand Down
9 changes: 5 additions & 4 deletions neuroml/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from neuroml.nml.nml import parseString as nmlparsestring

import neuroml
from neuroml import (NeuroMLDocument)
import neuroml.utils as utils

import os
Expand All @@ -18,7 +19,7 @@ class NeuroMLLoader(object):
@classmethod
def load(cls, src):
doc = cls.__nml2_doc(src)
if isinstance(doc, neuroml.nml.nml.NeuroMLDocument):
if isinstance(doc, NeuroMLDocument):
return doc
else:
raise TypeError(
Expand Down Expand Up @@ -188,7 +189,7 @@ def read_neuroml2_file(
already_included: list = None,
print_method: Callable = neuroml.print_,
optimized: bool = False,
) -> neuroml.nml.nml.NeuroMLDocument:
) -> NeuroMLDocument:
"""
Read a NeuroML2 file into a NeuroMLDocument object
Expand Down Expand Up @@ -234,7 +235,7 @@ def read_neuroml2_string(
print_method: Callable = neuroml.print_,
optimized: bool = False,
base_path: Optional[str] = None,
) -> neuroml.nml.nml.NeuroMLDocument:
) -> NeuroMLDocument:
"""
Read a NeuroML2 string into a NeuroMLDocument object
Expand Down Expand Up @@ -277,7 +278,7 @@ def _read_neuroml2(
print_method: Callable = neuroml.print_,
optimized: bool = False,
base_path: Optional[str] = None,
) -> neuroml.nml.nml.NeuroMLDocument:
) -> NeuroMLDocument:
"""
Read a NeuroML2 file or string into a NeuroMLDocument object.
Expand Down
1 change: 1 addition & 0 deletions neuroml/nml/gds_imports-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect
import networkx as nx
import numpy
import natsort

import neuroml
import neuroml.neuro_lex_ids
58 changes: 58 additions & 0 deletions neuroml/nml/generatedssupersuper.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,61 @@ def _check_arg_list(self, **kwargs):
print(err)
self.info()
raise ValueError(err)

@classmethod
def get_class_hierarchy(cls):
"""Get the class hierarchy for a component classs.
Reference: https://stackoverflow.com/a/75161393/375067
See the methods in neuroml.utils to use this generated hierarchy.
:returns: nested single key dictionaries where the key of each
dictionary is the root node of that subtree, and keys are its
immediate descendents
"""
# classes that don't have any members, like ZeroOrNone, which is an Enum
schema = sys.modules[cls.__module__]
try:
allmembers = cls._get_members()
except AttributeError:
return {cls.__name__: []}

retlist = []
for member in allmembers:
if member is not None:
# is it a complex type, which will have a corresponding class?
member_class = getattr(schema, member.get_data_type(), None)
# if it isn't a class, so a simple type, just added it with an
# empty list
if member_class is None:
retlist.append({member.get_name(): []})
else:
# if it is a class, see if it has a hierarchy
try:
retlist.append(member_class.get_class_hierarchy())
except AttributeError:
retlist.append({member_class.__name__: []})

return {cls.__name__: retlist}

@classmethod
def get_nml2_class_hierarchy(cls):
"""Return the NeuroML class hierarchy.
The root here is NeuroMLDocument.
This is useful in calculating paths to different components to aid in
construction of relative paths.
This caches the value as a class variable so that it is not
re-calculated when used multiple times.
"""
# if hierarchy exists, return
try:
return cls.__nml_hier
# first run
except AttributeError:
schema = sys.modules[cls.__module__]
cls.__nml_hier = schema.NeuroMLDocument.get_class_hierarchy()
return cls.__nml_hier
6 changes: 4 additions & 2 deletions neuroml/nml/helper_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,8 @@ def optimise_segment_group(self, seg_group_id):
if i not in new_includes:
new_includes.append(i)
includes = set(new_includes)
seg_group.includes = list(includes)
# sorted
seg_group.includes = natsort.natsorted(includes, key=lambda x:x.segment_groups)
# remove members that are included by included segment groups
if len(includes) > 0 and len(members) > 0:
Expand All @@ -1782,7 +1783,8 @@ def optimise_segment_group(self, seg_group_id):
for i in members:
if i.segments not in all_segment_ids_in_group:
new_members.append(i)
seg_group.members = list(new_members)
# sorted
seg_group.members = natsort.natsorted(new_members, key=lambda x: x.segments)
def set_spike_thresh(self, v, group_id="all"):
Expand Down
Loading

0 comments on commit 55bf626

Please sign in to comment.