Skip to content

Commit

Permalink
feat(nml): use cached property values
Browse files Browse the repository at this point in the history
Instead of looking up again and caching independently
  • Loading branch information
sanjayankur31 committed Sep 11, 2024
1 parent 7e7cc01 commit a1ece67
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
12 changes: 5 additions & 7 deletions neuroml/nml/helper_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,21 +911,19 @@ def __str__(self):
source='''\
# Get segment object by its id
@lru_cache(maxsize=1000)
def get_segment(self, segment_id: int) -> Segment:
"""Get segment object by its id
:param segment_id: ID of segment
:type segment_id: int
:return: segment
:raises ValueError: if the segment is not found in the cell
"""
for segment in self.morphology.segments:
if segment.id == segment_id:
return segment
raise ValueError("Segment with id "+str(segment_id)+" not found in cell "+str(self.id))
try:
return self.segment_ids_vs_segments[segment_id]
except KeyError:
raise ValueError("Segment with id "+str(segment_id)+" not found in cell "+str(self.id))
def get_segments_by_substring(self, substring: str) -> typing.Dict[str, Segment]:
"""Get a dictionary of segment IDs and the segment matching the specified substring
Expand Down
21 changes: 11 additions & 10 deletions neuroml/nml/nml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

#
# Generated Wed Sep 11 16:48:39 2024 by generateDS.py version 2.44.1.
# Generated Wed Sep 11 17:09:38 2024 by generateDS.py version 2.44.1.
# Python 3.11.9 (main, Aug 23 2024, 00:00:00) [GCC 14.2.1 20240801 (Red Hat 14.2.1-1)]
#
# Command line options:
Expand Down Expand Up @@ -48430,23 +48430,24 @@ def _buildChildren(
super(Cell, self)._buildChildren(child_, node, nodeName_, True)

# Get segment object by its id
@lru_cache(maxsize=1000)
def get_segment(self, segment_id: int) -> Segment:
"""Get segment object by its id

:param segment_id: ID of segment
:type segment_id: int
:return: segment

:raises ValueError: if the segment is not found in the cell
"""

for segment in self.morphology.segments:
if segment.id == segment_id:
return segment

raise ValueError(
"Segment with id " + str(segment_id) + " not found in cell " + str(self.id)
)
try:
return self.segment_ids_vs_segments[segment_id]
except KeyError:
raise ValueError(
"Segment with id "
+ str(segment_id)
+ " not found in cell "
+ str(self.id)
)

def get_segments_by_substring(self, substring: str) -> typing.Dict[str, Segment]:
"""Get a dictionary of segment IDs and the segment matching the specified substring
Expand Down
2 changes: 1 addition & 1 deletion neuroml/test/test_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def test_get_morphology_root(self):

# clear the cache, otherwise it'll return the old value again in
# other function calls also
acell.get_segment.cache_clear()
acell.get_segment_ids_vs_segments(cached=False)

# also update all descendents to ensure cell remains valid
for seg in acell.morphology.segments:
Expand Down

0 comments on commit a1ece67

Please sign in to comment.